-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
363 lines (321 loc) · 6.99 KB
/
util.c
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "util.h"
#include <stdarg.h> // va_start(3)
#include <stdio.h> // fprintf(3)
#include <stdlib.h> // free(3)
#include <string.h> // strcmp(3)
char *ErrorMsg;
//
// ArgsIter
//
/**
* Creates a new ArgsIter object
*
* @return a pointer to a new ArgsIter object
* @param argc the count of the arguments
* @param argv the vector of the arguments
*/
ArgsIter *new_ArgsIter(int argc, char **argv) {
ArgsIter *self = malloc(sizeof(ArgsIter));
self->prog_name = *argv;
self->argc = argc - 1;
self->argv = argv + 1;
return self;
}
/**
* Deletes the ArgsIter object
*
* @param self a pointer to ArgsIter object
*/
void delete_ArgsIter(ArgsIter *self) {
free(self);
}
/**
* @return name used to invoke the program itself
*/
char *ArgsIter_getProgName(ArgsIter *self) {
return self->prog_name;
}
/**
* Returns true if the Args has a next argument.
*
* @return true if the Args has a next argument.
* @param self a pointer to ArgsIter object
*/
bool ArgsIter_hasNext(ArgsIter *self) {
return self->argc > 0;
}
/**
* Returns the next argument
*
* @return the next argument
* @param self a pointer to ArgsIter object
*/
char *ArgsIter_next(ArgsIter *self) {
char *ret = *(self->argv);
self->argc--;
self->argv++;
return ret;
}
//
// Vector
//
/**
* Creates a new Vector object
*
* @return a pointer to a new Vector object
*/
Vector *new_Vector() {
Vector *vec = calloc(1, sizeof(Vector));
vec->capacity = 16;
vec->data = calloc(vec->capacity, sizeof(void *));
vec->len = 0;
return vec;
}
/**
* Deletes the Vector object
*
* @param vec
*/
void delete_Vector(Vector *vec) {
for (int i = 0; i < vec->len; i++)
free(vec->data[i]);
free(vec->data);
free(vec);
}
/**
* Pushes the element to the tail of the Vector.
* automatically expand the Vector if necessary.
*
* @param vec
* @param elem
*/
void Vector_push(Vector *vec, void *elem) {
if (vec->capacity == vec->len) {
vec->capacity *= 2;
vec->data = realloc(vec->data, sizeof(void *) * vec->capacity);
}
vec->data[vec->len++] = elem;
}
/**
* Pops the element from the tail of the vector.
*
* @param vec
*/
void *Vector_pop(Vector *vec) {
return vec->data[--vec->len];
}
/**
* Returns the element from tail of the Vector.
*
* @return the element from tail of the Vector.
* @param vec
*/
void *Vector_last(Vector *vec) {
return vec->data[vec->len - 1];
}
//
// Map
//
/**
* Creates a new Map object
*
* @return a pointer to a new Map object
*/
Map *new_Map() {
Map *map = malloc(sizeof(Map));
map->keys = new_Vector();
map->vals = new_Vector();
return map;
}
/**
* Destroys the Map object
*
* @param map
*/
void delete_Map(Map *map) {
delete_Vector(map->vals);
delete_Vector(map->keys);
free(map);
}
/**
* Associates the value with the key in this map
*
* @param map
* @param key
* @param val
*/
void Map_put(Map *map, char *key, void *val) {
Vector_push(map->keys, key);
Vector_push(map->vals, val);
}
/**
* Returns the value to which the key is mapped, or NULL if the map contains no
* mapping for the key.
*
* @return the value to witch the key is mapped, or NULL if the map contains no
* mapping for the key.
* @param map
* @param key
*/
void *Map_get(Map *map, const char *key) {
for (int i = map->keys->len - 1; i >= 0; i--) {
if (strcmp(map->keys->data[i], key) == 0) {
return map->vals->data[i];
}
}
return NULL;
}
//
// StringBuffer
//
/**
* Creates a new StringBuffer object.
*
* @return a pointer to a new StringBuffer object
*/
StringBuffer *new_StringBuffer() {
StringBuffer *sb = calloc(1, sizeof(StringBuffer));
sb->len = 0;
sb->_body = new_Vector();
sb->_buf_siz = 255 + 1;
sb->_buf = calloc(sb->_buf_siz, sizeof(char));
return sb;
}
/**
* Destroys the StringBuffer object.
*
* @param sb
*/
void delete_StringBuffer(StringBuffer *sb) {
delete_Vector(sb->_body);
free(sb->_buf);
free(sb);
}
/**
* Appends the string to StringBuffer.
*
* @param sb
* @param string
*/
void StringBuffer_append(StringBuffer *sb, const char *string) {
if (sb->_buf_len != 0) {
sb->_buf[sb->_buf_len] = '\0';
Vector_push(sb->_body, strdup(sb->_buf));
sb->_buf_len = 0;
}
Vector_push(sb->_body, strdup(string));
sb->len += strlen(string);
}
/**
* Appends the char to StringBuffer.
*
* @param sb
* @param c
*/
void StringBuffer_appendChar(StringBuffer *sb, char c) {
if (sb->_buf_len + 1 >= sb->_buf_siz) {
sb->_buf[sb->_buf_len] = '\0';
Vector_push(sb->_body, strdup(sb->_buf));
sb->_buf_len = 0;
}
sb->_buf[sb->_buf_len] = c;
sb->_buf_len++;
sb->len++;
}
/**
* Creates string by StringBuffer
*
* Caller must free the allocated memory stores the string.
*
* @return a string
* @param sb
*/
char *StringBuffer_toString(StringBuffer *sb) {
char *str = calloc(sb->len + 1, sizeof(char));
for (int i = 0; i < sb->_body->len; i++)
strcat(str, sb->_body->data[i]);
sb->_buf[sb->_buf_len] = '\0';
strcat(str, sb->_buf);
return str;
}
/**
* duplicate an integer
*
* @return a pointer to the duplicated integer
* @param n
*/
int *intdup(int n) {
int *num = malloc(sizeof(int));
*num = n;
return num;
}
//
// for testing
//
/**
* put a error message to stderr, then exit with failure status code
*
* @parma fmt
*/
noreturn void error(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
/**
* Assert the expected and actual are equal integer.
*
* @param line
* @param expected
* @param actual
*/
void expect(int line, int expected, int actual) {
if (expected == actual)
return;
error("%d: %d expected, but got %d", line, expected, actual);
}
/**
* Assert the extected and actual are equal string.
*
* @param line
* @param expected
* @param actual
*/
void expect_str(int line, const char *expected, const char *actual) {
if (expected == NULL)
error("%d: non-NULL is expected, but \"expected\" is NULL", line);
if (actual == NULL)
error("%d: non-NULL is expected, but \"actual\" is NULL", line);
if (strcmp(expected, actual) != 0)
error("%d: \"%s\" expected, but got \"%s\"", line, expected, actual);
return;
}
/**
* Assert the expected and actual point are equal.
*
* @param line
* @param expected
* @param actual
*/
void expect_ptr(int line, const void *expected, const void *actual) {
if (expected == actual)
return;
error("%d: %d expected, but got %d", line, expected, actual);
}
/**
* Assert the expected and the actual are equal boolean value
*
* @param line
* @param expected
* @param actual
*/
void expect_bool(int line, bool expected, bool actual) {
if (expected == actual)
return;
if (actual == true)
error("%d: 'false' expected, but got 'true'", line);
else
error("%d: 'true' expected, but got 'false'", line);
}