forked from brown-cs0330/lab06-alloc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtester.c
More file actions
275 lines (240 loc) · 8.26 KB
/
tester.c
File metadata and controls
275 lines (240 loc) · 8.26 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
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
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "linkedlist.h"
#include "string_fns.h"
#define BUF_SIZE 1024
// Forward declarations
void print_help();
char *skip_ws(char *str);
char *skip_nonws(char *str);
void next_word(char **currp, char **nextp);
int add_file(LinkedList *linkedlist, char *file);
int remove_file(LinkedList *linkedlist, char *curr);
int main(int argc, char **argv) {
char buf[BUF_SIZE];
LinkedList *linkedlist =
linkedlist_new(&string_equals, &string_copy, &string_delete);
if (!linkedlist) {
fprintf(stderr, "linkedlist_new() returned NULL\n");
return 1;
}
linkedlist_print(linkedlist, stdout, &string_print);
printf("\n\n");
while (1) {
printf("> ");
fflush(stdout);
if (fgets(buf, BUF_SIZE, stdin) == NULL) break;
// preprocess comment out of buffer
char *comment = strstr(buf, "//");
if (comment != NULL) {
*comment = 0;
}
char *curr;
char *next = buf;
next_word(&curr, &next);
if (!*curr) {
// Blank line -- do nothing
continue;
} else if (!strcasecmp(curr, "a")) {
next_word(&curr, &next);
if (*(skip_ws(next))) {
printf("Too many arguments\n");
} else {
linkedlist_append(linkedlist, (void *)curr);
printf("Element appended\n");
linkedlist_print(linkedlist, stdout, &string_print);
printf("\n");
}
} else if (!strcasecmp(curr, "i")) {
next_word(&curr, &next);
if (!*(skip_ws(next))) {
printf("Too few arguments\n");
} else {
char *str = curr;
next_word(&curr, &next);
if (*(skip_ws(next))) {
printf("Too many arguments\n");
} else {
char *index = curr;
if (linkedlist_insert(linkedlist, (void *)str, atoi(index)))
printf("Element inserted\n");
else
printf("Index is invalid\n");
}
linkedlist_print(linkedlist, stdout, &string_print);
printf("\n");
}
} else if (!strcasecmp(curr, "c")) {
next_word(&curr, &next);
if (*(skip_ws(next))) {
printf("Too many arguments\n");
} else {
printf(linkedlist_contains(linkedlist, (void *)curr)
? "true\n"
: "false\n");
}
} else if (!strcasecmp(curr, "r")) {
next_word(&curr, &next);
if (*(skip_ws(next))) {
printf("Too many arguments\n");
} else {
if (linkedlist_remove(linkedlist, (void *)curr))
printf("Element removed\n");
else
printf("Element not found\n");
linkedlist_print(linkedlist, stdout, &string_print);
printf("\n");
}
} else if (!strcasecmp(curr, "x")) {
next_word(&curr, &next);
if (*(skip_ws(next))) {
printf("Too many arguments\n");
} else {
linkedlist_delete(linkedlist);
linkedlist = linkedlist_new(&string_equals, &string_copy,
&string_delete);
if (!linkedlist) {
fprintf(stderr, "linkedlist_new() returned NULL\n");
return 1;
}
linkedlist_print(linkedlist, stdout, &string_print);
printf("\n");
}
} else if (!strcasecmp(curr, "s")) {
if (*(skip_ws(next))) {
printf("Too many arguments\n");
} else {
printf("%d\n", linkedlist_size(linkedlist));
}
} else if (!strcasecmp(curr, "p")) {
if (*(skip_ws(next))) {
printf("Too many arguments\n");
} else {
linkedlist_print(linkedlist, stdout, &string_print);
printf("\n");
}
} else if (!strcasecmp(curr, "fa")) {
next_word(&curr, &next);
if (*(skip_ws(next))) {
printf("Too many arguments\n");
} else {
if (add_file(linkedlist, curr)) {
printf("error loading file.\n");
} else {
linkedlist_print(linkedlist, stdout, &string_print);
printf("\n");
}
}
} else if (!strcasecmp(curr, "fr")) {
next_word(&curr, &next);
if (*(skip_ws(next))) {
printf("Too many arguments\n");
} else {
if (remove_file(linkedlist, curr)) {
printf("error loading file.\n");
} else {
linkedlist_print(linkedlist, stdout, &string_print);
printf("\n");
}
}
} else if (!strcasecmp(curr, "quit")) {
if (*(skip_ws(next))) {
printf("Too many arguments\n");
} else {
break;
}
} else if (!strcasecmp(curr, "h") || !strcasecmp(curr, "help")) {
print_help();
} else {
printf("Invalid command\n");
}
printf("\n");
}
if (ferror(stdin)) perror("fgets() failed");
linkedlist_delete(linkedlist);
return 0;
}
/* Add contents of file to linkedlist */
int add_file(LinkedList *linkedlist, char *curr) {
FILE *f = fopen(curr, "r");
if (!f) {
fprintf(stderr, "error opening file %s: %s\n", curr, strerror(errno));
return 1;
}
char buf[BUF_SIZE];
while (fgets(buf, BUF_SIZE, f)) {
char *curr;
char *next = buf;
next_word(&curr, &next);
while (*curr) {
linkedlist_append(linkedlist, (void *)curr);
printf("added \"%s\"\n", curr);
next_word(&curr, &next);
}
}
fclose(f);
return 0;
}
/* Add contents of file to linkedlist */
int remove_file(LinkedList *linkedlist, char *curr) {
FILE *f = fopen(curr, "r");
if (!f) {
fprintf(stderr, "error opening file %s: %s\n", curr, strerror(errno));
return 1;
}
char buf[1024];
while (fgets(buf, 1024, f)) {
char *curr;
char *next = buf;
next_word(&curr, &next);
while (*curr) {
if (linkedlist_remove(linkedlist, (void *)curr))
printf("removed \"%s\"\n", curr);
next_word(&curr, &next);
}
}
fclose(f);
return 0;
}
/* Print formatted help information about the given command */
void print_command(const char *name, const char *explanation) {
printf(" %-9s %s\n", name, explanation);
}
/* Print help information */
void print_help() {
printf("The following commands are avaiable:\n");
print_command("a <str>", "Add <str> to the list");
print_command("i <str> <index>", "Add <str> to the list before <index>");
print_command("c <str>", "Test if <str> is in the list");
print_command("r <str>", "Remove <str> from the list");
print_command("x", "Delete and re-instantiate the list");
print_command("s", "Print the list size");
print_command("p", "Print a representation of the list");
print_command("fa <file>", "Add each word of <file> to the list");
print_command("fr <file>", "Remove each word of <file> from the list");
print_command("help", "Print help information");
print_command("quit", "Delete the list and exit the program");
}
/* Advance pointer until first non-whitespace character */
char *skip_ws(char *str) {
while (isspace(*str)) str++;
return str;
}
/* Advance pointer until first whitespace or null character */
char *skip_nonws(char *str) {
while (*str && !(isspace(*str))) str++;
return str;
}
/* Advance to the next word and null-terminate */
void next_word(char **currp, char **nextp) {
*currp = skip_ws(*nextp);
*nextp = skip_nonws(*currp);
if (**nextp) {
**nextp = 0;
(*nextp)++;
}
}