/
exercise_6-6.c
355 lines (296 loc) · 8.04 KB
/
exercise_6-6.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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
enum constants
{
GETCH_BUFFER_SIZE = 1000,
HASH_SIZE = 101,
MAX_LINE_LENGTH = 1000,
MAX_NUM_LINES = 1000,
MAX_NUM_WORDS_PER_LINE = 1000,
MAX_WORD_LENGTH = 100,
POSSIBLE_DEFINE = 3,
POSSIBLE_UNDEFINE = 2,
};
static int getch_buffer[GETCH_BUFFER_SIZE];
static int getch_index = 0;
int getch()
{
return (getch_index > 0 ? getch_buffer[--getch_index] : getchar());
}
void ungetch(int chr)
{
if (getch_index >= GETCH_BUFFER_SIZE)
{
printf("error: cannot ungetch(%c) as the buffer is full\n", chr);
}
else
{
getch_buffer[getch_index++] = chr;
}
}
struct list
{
char* name;
char* replacement;
struct list* next;
};
static struct list* hash_table[HASH_SIZE];
char* str_duplicate(char* str)
{
char* duplicate = malloc(sizeof(str) + 1);
if (duplicate == NULL)
{
return NULL;
}
strcpy(duplicate, str);
return duplicate;
}
unsigned hash(char* str)
{
unsigned hash_value;
for (hash_value = 0; *str != '\0'; ++str)
{
hash_value = *str + 31 * hash_value;
}
return hash_value % HASH_SIZE;
}
struct list* lookup(char* str)
{
for (struct list* node = hash_table[hash(str)]; node != NULL; node = node->next)
{
if (strcmp(str, node->name) == 0)
{
return node;
}
}
return NULL;
}
struct list* define(char* name, char* replacement)
{
struct list* node;
unsigned hash_value;
if ((node = lookup(name)) == NULL)
{
node = malloc(sizeof(struct list));
if (node == NULL || (node->name = str_duplicate(name)) == NULL)
{
return NULL;
}
hash_value = hash(name);
node->next = hash_table[hash_value];
hash_table[hash_value] = node;
}
else
{
free(node->replacement);
}
if ((node->replacement = str_duplicate(replacement)) == NULL)
{
return NULL;
}
return node;
}
void undefine(char* name)
{
unsigned hash_value = hash(name);
struct list* node = hash_table[hash_value];
if (node == NULL)
{
return;
}
/*
* The first node is the one we need to remove. The for loop below
* won't find this so we have to do it here.
*/
if (strcmp(name, node->name) == 0)
{
hash_table[hash_value] = NULL;
return;
}
for (; node->next != NULL; node = node->next)
{
if (strcmp(name, node->next->name) == 0)
{
node->next = node->next->next;
return;
}
}
}
int get_line(char line[])
{
int current_char = EOF;
int index;
for (index = 0; index < MAX_LINE_LENGTH - 1 && (current_char = getch()) != EOF && current_char != '\n'; ++index)
{
line[index] = current_char;
}
if (current_char == '\n')
{
line[index++] = current_char;
}
line[index] = '\0';
return index;
}
int tokenize(char line[], char* words[])
{
int line_char_index = 0;
int word_char_index = 0;
int word_index = 0;
int current_char = EOF;
while (true)
{
char* word = malloc(MAX_WORD_LENGTH + 1);
while (isspace(current_char = line[line_char_index++]))
{
/* Skip any leading whitespace. */
}
/* Essentially the same as ungetch(current_char). */
--line_char_index;
while ((current_char = line[line_char_index++]) != EOF && !isspace(current_char) && word_char_index < MAX_WORD_LENGTH)
{
word[word_char_index++] = current_char;
}
word[word_char_index] = '\0';
/* Ignore words of zero length. */
if (word_char_index != 0)
{
words[word_index++] = word;
}
word_char_index = 0;
if (current_char == EOF || current_char == '\n')
{
break;
}
else
{
--line_char_index;
}
}
/*
* Even though this is an array index (i.e., starts at zero) we don't need to add one to this
* as word_index is post-incremented and so when the last word has been read, word_index
* is still incremented, thus achieving the same effect as adding one here.
*/
return word_index;
}
bool handle_possible_define(char* words[])
{
if (strcmp(words[0], "#define") == 0)
{
define(words[1], words[2]);
return true;
}
return false;
}
bool handle_possible_undefine(char* words[])
{
if (strcmp(words[0], "#undef") == 0)
{
undefine(words[1]);
return true;
}
return false;
}
bool suitable_for_replacement(char* line, char* occurence, int replacement_length)
{
if (occurence == NULL)
{
return false;
}
if (occurence == line)
{
return !isalnum(*(occurence + replacement_length));
}
else
{
return !(isalnum(*(occurence - 1)) || isalnum(*(occurence + replacement_length)));
}
}
int main()
{
char line[MAX_LINE_LENGTH] = { 0 };
char* lines[MAX_NUM_LINES]; char* words[MAX_NUM_WORDS_PER_LINE] = { 0 };
/*
* lines_to_ignore[line_number] is set to true when line is a #define/#undef statement.
* If we didn't do this, the following line:
* #define foo bar
* would be converted to:
* #define bar bar
* which is clearly not the desired behaviour.
*/
bool lines_to_ignore[MAX_NUM_LINES] = { 0 };
int num_lines = 0;
while (get_line(line) > 0)
{
lines[num_lines++] = line;
int num_words = tokenize(line, words);
switch (num_words)
{
/*
* This does not (yet) support defines with multiple words
* following the name of the macro.
*/
case POSSIBLE_DEFINE:
lines_to_ignore[num_lines - 1] = handle_possible_define(words);
break;
case POSSIBLE_UNDEFINE:
lines_to_ignore[num_lines - 1] = handle_possible_undefine(words);
break;
default:
break;
}
/*
* For each #defined word for each line, do a strstr for the defined symbol.
* If a substring is found and the characters before and after the substring aren't:
* numeric
* alphabetic
* and we are not:
* in a string
* in a comment
* ignoring this line (because it is the line in which the symbol was defined)
* then replace the symbol with the replacement text.
*/
for (int i = 0; i < num_lines; ++i)
{
if (lines_to_ignore[i])
{
continue;
}
for (int j = 0; j < HASH_SIZE; ++j)
{
struct list* node = hash_table[j];
for (; node != NULL; node = node->next)
{
char* start = lines[i];
char* occurence = strstr(lines[i], node->name);
while (occurence != NULL)
{
/* Print characters before the occurence. */
for (char* c = start; c < occurence; ++c)
{
putchar(*c);
}
if (suitable_for_replacement(lines[i], occurence, strlen(node->replacement)))
{
printf("%s", node->replacement);
}
else
{
char* c = occurence;
for (; c < occurence + strlen(node->name); ++c)
{
putchar(*c);
}
start = c;
}
occurence = strstr(occurence + strlen(node->name), node->name);
}
}
}
}
}
return 0;
}