s502 assembler
A very simple assembler for the 6502 line of processors written in C
Public Member Functions | Data Fields | Private Member Functions
Map Class Reference

Simple key->value map (str->int) More...

#include <map.h>

Public Member Functions

Mapmap_new ()
 Create a new map with 0 elements. More...
 
int map_set (Map *map, char *name, int value)
 Sets a key in the map. More...
 
int map_get (Map *map, char *name)
 Get the value of a key. More...
 
void map_free (Map *map)
 Free the map with all associated memory. More...
 
void map_debug_print (Map *map)
 Print all key-value pairs of the map. More...
 

Data Fields

struct MapEntryhead
 

Private Member Functions

struct MapEntrymap_find (Map *map, char *key)
 Find a key in a map. More...
 

Detailed Description

Simple key->value map (str->int)

Implemented with a linked list.
Should only store non-negative values, as "NOT FOUND" value is -1

Definition at line 34 of file map.h.

Member Function Documentation

◆ map_debug_print()

void map_debug_print ( Map map)

Print all key-value pairs of the map.

Parameters
mapthe map to print

Intended for debugging purposes

Definition at line 85 of file map.c.

85  {
86  for (struct MapEntry* ptr = map->head; ptr != NULL; ptr = ptr->next)
87  printf("\t%s:\t\t%d\n", ptr->name, ptr->value);
88 }

Referenced by main().

◆ map_find()

struct MapEntry * map_find ( Map map,
char *  key 
)
private

Find a key in a map.

Returns
NULL if not found
Parameters
mapmap to search in
keykey to search for

Definition at line 35 of file map.c.

35  {
36  for (struct MapEntry* ptr = map->head; ptr != NULL; ptr = ptr->next)
37  if (strncmp(ptr->name, key, MAP_MAX_KEY_LEN) == 0)
38  return ptr;
39  return NULL;
40 }

References head, MAP_MAX_KEY_LEN, and MapEntry::next.

Referenced by map_get(), and map_set().

◆ map_free()

void map_free ( Map map)

Free the map with all associated memory.

Parameters
mapthe map to free

Frees the map itself, so it's pointer will be invalid and should be NULLed!

Definition at line 62 of file map.c.

62  {
63  if (!map) return;
64  struct MapEntry* ptr;
65  while (map->head != NULL) {
66  ptr = map->head->next;
67  free(map->head);
68  map->head = ptr;
69  }
70  map->head = NULL;
71  free(map);
72 }

Referenced by state_free().

◆ map_get()

int map_get ( Map map,
char *  name 
)

Get the value of a key.

Returns
the value of the key or -1 on not found
Parameters
mapthe map to search in
namekey to search for

Definition at line 75 of file map.c.

75  {
76  LOG(5, "Getting %s\n", name);
77  struct MapEntry* p = map_find(map, name);
78  if (p == NULL)
79  return -1;
80  return p->value;
81 
82 }

Referenced by number_get_raw(), and pass_one().

◆ map_new()

Map * map_new ( )

Create a new map with 0 elements.

Returns
the new map or NULL on error

Definition at line 17 of file map.c.

17  {
18  Map* ret = (Map*)malloc(sizeof(Map));
19  if (ret == NULL) {
20  ERROR("Memory allocation error in map_new()!\n");
21  return NULL;
22  }
23  ret->head = NULL;
24  return ret;
25 }

Referenced by state_new().

◆ map_set()

int map_set ( Map map,
char *  name,
int  value 
)

Sets a key in the map.

Parameters
mapmap to set key in
namekey to set
valuenon-negative value to set for the key
Returns
0 on success, -1 on error

Definition at line 43 of file map.c.

43  {
44  struct MapEntry* ptr = map_find(map, name);
45  if (ptr == 0) {
46  ptr = (struct MapEntry*)malloc(sizeof(struct MapEntry));
47  if (ptr == NULL) {
48  ERROR("Memory allocation error in map_set()!\n");
49  return -1;
50  }
51  ptr->next = map->head;
52  map->head = ptr;
53  strncpy(ptr->name, name, MAP_MAX_KEY_LEN - 1);
54  ptr->name[MAP_MAX_KEY_LEN - 1] = 0;
55  }
56 
57  ptr->value = value;
58  return 0;
59 }

Referenced by pass_one(), and state_parse_commandline().

Field Documentation

◆ head

struct MapEntry* Map::head

Definition at line 35 of file map.h.

Referenced by map_debug_print(), map_find(), map_free(), map_new(), and map_set().


The documentation for this class was generated from the following files:
Map::head
struct MapEntry * head
Definition: map.h:35
LOG
#define LOG(LVL,...)
logging macro - works like printf
Definition: logging.h:28
MapEntry
One entry in a map.
Definition: map.h:18
Map
Simple key->value map (str->int)
Definition: map.h:34
MapEntry::next
struct MapEntry * next
pointer to the next entry or NULL
Definition: map.h:24
MapEntry::name
char name[MAP_MAX_KEY_LEN]
key of the entry
Definition: map.h:22
MAP_MAX_KEY_LEN
@ MAP_MAX_KEY_LEN
Key buffer size for Map.
Definition: map.h:14
Map::map_find
struct MapEntry * map_find(Map *map, char *key)
Find a key in a map.
Definition: map.c:35
ERROR
#define ERROR(...)
Fancy-print an error (cause of faliure). Works like printf.
Definition: logging.h:40
MapEntry::value
int value
the value the entry holds
Definition: map.h:20