Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc-extra / emu / rrz80 / map.c
blobd95a17a2d0c97c40506eac36881d4875d2dc2fcd
1 #include "types.h"
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <stdio.h>
5 #include <string.h>
7 typedef struct {
8 uint16 addr;
9 const char *szName;
10 } SYM_ENTRY;
12 struct _MAP {
13 SYM_ENTRY *syms;
14 int numSyms;
15 int maxSyms;
18 static MAP *_grow(MAP *pMap)
20 assert(pMap);
22 if (pMap->numSyms == pMap->maxSyms) {
23 pMap->maxSyms = (pMap->maxSyms + 1)*2;
24 pMap->syms = (SYM_ENTRY*) realloc(pMap->syms, pMap->maxSyms*sizeof(*pMap->syms));
25 assert(pMap->syms);
27 else {
28 /* Already big enough. */
30 return pMap;
33 static char *_chomp(char *sz)
35 do {
36 char *p = strrchr(sz, '\n');
37 if (p != NULL) {
38 *p = '\0';
39 continue;
41 p = strrchr(sz, '\r');
42 if (p != NULL) {
43 *p = '\0';
44 continue;
46 break;
47 } while (1);
49 return sz;
52 MAP *map_load(const char *fname)
54 FILE *fp = fopen(fname, "r");
55 char line[MAX_LINE];
57 if (fp == NULL) {
58 return NULL;
60 else {
61 MAP *pmap = (MAP*) calloc(1, sizeof(*pmap));
63 while (fgets(line, sizeof(line), fp) != NULL) {
64 int drop, addr;
65 char *start;
67 /* Drop comment lines. */
68 if (line[0] == ';') {
69 continue;
71 /* Read in the segment and address. */
72 if (sscanf(line, "%X:%X", &drop, &addr) != 2) {
73 continue;
76 start = strrchr(line, ' ');
77 if (start == NULL) {
78 /* Invalid? */
79 continue;
82 _chomp(line);
83 /* Move on past the space. */
84 start++;
86 /* Drop any internal names, like the segment start and length
87 symbols. */
88 if (!strncmp(start, "s__", 3)) {
89 continue;
92 pmap = _grow(pmap);
93 pmap->syms[pmap->numSyms].addr = addr;
94 pmap->syms[pmap->numSyms].szName = strdup(start);
96 pmap->numSyms++;
98 fclose(fp);
99 return pmap;
103 const char *map_find(MAP *self, uint16 addr)
105 if (self != NULL) {
106 int i;
107 for (i = 0; i < self->numSyms; i++) {
108 if (self->syms[i].addr == addr) {
109 return self->syms[i].szName;
113 return NULL;
116 const char *map_lookup(MAP *self, uint16 addr, char *pInto)
118 if (self != NULL) {
119 int i;
120 for (i = 0; i < self->numSyms; i++) {
121 if (self->syms[i].addr == addr) {
122 return self->syms[i].szName;
126 sprintf(pInto, "%04X", addr);
128 return pInto;