s502 assembler
A very simple assembler for the 6502 line of processors written in C
map.c
Go to the documentation of this file.
1 #include "debugmalloc.h"
2 
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 
7 #include "logging.h"
8 #include "map.h"
9 
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 }
26 
35 struct MapEntry* map_find(Map* map, char* key) {
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 }
41 
42 
43 int map_set(Map* map, char* name, int value) {
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 }
60 
61 
62 void map_free(Map* map) {
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 }
73 
74 
75 int map_get(Map* map, char* name) {
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 }
83 
84 
85 void map_debug_print(Map* map) {
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 }
Map::head
struct MapEntry * head
Definition: map.h:35
Map::map_set
int map_set(Map *map, char *name, int value)
Sets a key in the map.
Definition: map.c:43
LOG
#define LOG(LVL,...)
logging macro - works like printf
Definition: logging.h:28
Map::map_debug_print
void map_debug_print(Map *map)
Print all key-value pairs of the map.
Definition: map.c:85
MapEntry
One entry in a map.
Definition: map.h:18
Map
Simple key->value map (str->int)
Definition: map.h:34
map.h
Map class.
MapEntry::next
struct MapEntry * next
pointer to the next entry or NULL
Definition: map.h:24
Map::map_free
void map_free(Map *map)
Free the map with all associated memory.
Definition: map.c:62
Map::map_get
int map_get(Map *map, char *name)
Get the value of a key.
Definition: map.c:75
MapEntry::name
char name[MAP_MAX_KEY_LEN]
key of the entry
Definition: map.h:22
logging.h
logging and fancy-printing
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
Map::map_new
Map * map_new()
Create a new map with 0 elements.
Definition: map.c:17
MapEntry::value
int value
the value the entry holds
Definition: map.h:20