s502 assembler
A very simple assembler for the 6502 line of processors written in C
Functions
map.c File Reference

Map member functions. More...

#include "debugmalloc.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "logging.h"
#include "map.h"

Go to the source code of this file.

Functions

Mapmap_new ()
 
int map_set (Map *map, char *name, int value)
 
void map_free (Map *map)
 
int map_get (Map *map, char *name)
 
void map_debug_print (Map *map)
 

Detailed Description

Map member functions.

See map.h for Map class!

Definition in file map.c.

Function Documentation

◆ map_debug_print()

void map_debug_print ( Map map)

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 }

References Map::head, and MapEntry::next.

◆ map_free()

void map_free ( Map map)

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 }

References Map::head, and MapEntry::next.

◆ map_get()

int map_get ( Map map,
char *  name 
)

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 }

References LOG, Map::map_find(), MapEntry::name, and MapEntry::value.

◆ map_new()

Map* map_new ( )

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 }

References ERROR, and Map::head.

◆ map_set()

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

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 }

References ERROR, Map::head, Map::map_find(), MAP_MAX_KEY_LEN, MapEntry::name, MapEntry::next, and MapEntry::value.

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