-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.c
More file actions
239 lines (212 loc) · 5.31 KB
/
util.c
File metadata and controls
239 lines (212 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* util.c - Funções utilitárias do compilador
*
* Contém funções de alocação de memória, manipulação de strings,
* hash tables e tratamento de erros.
*/
#include "cc.h"
/*============================================================================
* Tratamento de Erros
*============================================================================*/
void error(const char *loc, const char *format, ...)
{
if ((opt&oDEBUG) && loc != NULL) fprintf(stderr, "%s: ", loc);
if (0 <= ix.tix && ix.tix < cd.nToken)
{
int filenumber = cd.token[ix.tix].filenumber;
char *srcfile = filenumber >= 0 ? mcc.srcFile[filenumber] : "startup";
fprintf(stderr, "%s:%d: ", srcfile, cd.token[ix.tix].linenumber);
}
else if (mcc.nPreFile >= 0)
{
fprintf(stderr, "%s:%d: ", mcc.srcFile[mcc.nPreFile], mcc.lines[mcc.nPreFile]);
}
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(1);
}
/*============================================================================
* Funções de Caracteres
*============================================================================*/
int htoi(int c)
{
return c >= 'A' ? (c - 'A' + 10) : (c - '0');
}
int esc_char(char *p)
{
if (*p == 'x' || *p == 'X') return (htoi(p[1]) << 4) + htoi(p[2]);
return *p == '0' ? '\0' : *p == 'r' ? '\r' : *p == 'n' ? '\n' : *p == 't' ? '\t' : *p;
}
int isAlpha(int c)
{
return (isalpha(c) || c == '_');
}
int isAlNum(int c)
{
return isAlpha(c) || isdigit(c);
}
int isKanji(int c)
{
return (c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xFC);
}
char *endOfQuote(char *p)
{
int delim = *p++;
while (*p != '\0' && *p != delim)
p += (*p == '\\' || isKanji(*p & 0xff)) ? 2 : 1;
if (*p != delim) error("endOfQuote", "missing terminating %c character", delim);
return ++p;
}
/*============================================================================
* Funções de Alocação de Memória
*============================================================================*/
void *xcheck(void *p)
{
if (p == NULL) error("xcheck", "");
return p;
}
void *xalloc(int size)
{
return xcheck(calloc(1, size));
}
void *xrealloc(void *ptr, size_t size)
{
return xcheck(realloc(ptr, size));
}
char *xstrdup(char *q)
{
return xcheck(strdup(q));
}
/*============================================================================
* Funções de String
*============================================================================*/
int xstrcpy(char *p, char *q)
{
int len = 0;
while (*q)
{
int fKanji = isKanji(*q & 0xff);
len += fKanji ? 2 : 1;
if (p != NULL && fKanji)
{
*p++ = *q;
*p++ = q[1];
}
else if (p != NULL)
{
*p++ = *q == '\\' ? esc_char(q + 1) : *q;
}
q += *q == '\\' ? (q[1] == 'x' ? 4 : 2) : (fKanji ? 2 : 1);
}
return len;
}
int xstrlen(char *p)
{
return xstrcpy(NULL, p);
}
/*============================================================================
* Hash Table
*============================================================================*/
void initHash(int type, int size, HASH *pHash)
{
pHash->type = type;
pHash->size = size;
pHash->tbl = xalloc(size * sizeof(HDATA));
pHash->entries = 0;
}
void reallocHash(int newSize, HASH *pHash)
{
int n, oldSize = pHash->size;
HDATA *oldTbl = pHash->tbl;
initHash(pHash->type, newSize, pHash);
for (n = 0; n < oldSize; n++)
{
if (oldTbl[n].key != NULL) put(oldTbl[n].key, oldTbl[n].val, pHash);
}
free(oldTbl);
}
void freeHash(HASH *pHash)
{
int n;
for (n = 0; n < pHash->size; n++)
{
if (pHash->tbl[n].key != NULL) free(pHash->tbl[n].key);
if ((pHash->type == 's' || pHash->type == 'n') && pHash->tbl[n].val != 0)
free(pHash->tbl[n].val);
}
free(pHash->tbl);
}
int hash(char *key, int size)
{
int n, h = 0;
for (n = 0; key[n] != '\0'; n++)
h = (h * 137 + (key[n] & 0xff)) % size;
return h;
}
int put(char *key, void *val, HASH *pHash)
{
int n, h = hash(key, pHash->size);
for (n = 0; n < pHash->size; n++)
{
int ix = (h + n) % pHash->size;
if (pHash->tbl[ix].key == NULL)
{
pHash->tbl[ix].key = xstrdup(key);
pHash->tbl[ix].val = pHash->type == 's' ? xstrdup(val) : val;
pHash->tbl[ix].seq = pHash->entries++;
if (pHash->type != 'x' && pHash->entries > pHash->size / 2)
reallocHash(pHash->size * 2, pHash);
return ix;
}
else if (strcmp(pHash->tbl[ix].key, key) == 0)
{
return ix;
}
}
error("lib.put", "Hash Table full!");
return -1;
}
void *get(char *key, HASH *pHash)
{
int n, h = hash(key, pHash->size);
for (n = 0; n < pHash->size; n++)
{
int ix = (h + n) % pHash->size;
if (pHash->tbl[ix].key == NULL) break;
if (strcmp(pHash->tbl[ix].key, key) == 0)
return pHash->tbl[ix].val;
}
return NULL;
}
int del(char *key, HASH *pHash)
{
int n, h = hash(key, pHash->size);
for (n = 0; n < pHash->size; n++)
{
int ix = (h + n) % pHash->size;
if (pHash->tbl[ix].key == NULL) break;
if (strcmp(pHash->tbl[ix].key, key) == 0)
{
free(pHash->tbl[ix].key);
pHash->tbl[ix].key = NULL;
if (pHash->type == 's' && pHash->tbl[ix].val != NULL)
free(pHash->tbl[ix].val);
pHash->tbl[ix].val = NULL;
pHash->entries--;
return 1;
}
}
return 0;
}
void printHash(HASH *pHash)
{
char *fmt = pHash->type == 's' ? "%4d %-8s %s\n" : "%4d %-8s %08X\n";
int n;
for (n = 0; n < pHash->size; n++)
{
if (pHash->tbl[n].key != NULL)
printf(fmt, n, pHash->tbl[n].key, pHash->tbl[n].val);
}
}