-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.c
362 lines (303 loc) · 7.45 KB
/
utility.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
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"
/********************************* FIFO Queues for client servicing and logging **************************************/
struct ClientQueue
{
int front, rear, size;
unsigned capacity;
int *array;
};
struct LogNode
{
char *word;
int correctness; //case: 0 = spelled incorrecly, 1 = spelled correctly
struct LogNode *next;
};
struct LogQueue
{
struct LogNode *head;
struct LogNode *tail;
int size;
int capacity;
};
struct WordNode
{
char *word;
struct WordNode *next;
};
struct WordQueue
{
struct WordNode *head;
struct WordNode *tail;
int size;
int capacity;
};
int enqueue_client(struct ClientQueue *queue, int item)
{
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
return 0;
}
int dequeue_client(struct ClientQueue *queue)
{
int item = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size = queue->size - 1;
return item;
}
void enqueue_log(struct LogQueue *q, struct LogNode *n)
{
if (q->tail == NULL)
{
q->head = q->tail = n;
q->size += 1;
return;
}
q->tail->next = n;
q->tail = n;
q->size += 1;
}
struct LogNode *dequeue_log(struct LogQueue *q)
{
struct LogNode *first_log = q->head;
q->head = q->head->next;
q->size -= 1;
if (q->head == NULL)
{
q->tail = NULL;
}
return first_log;
}
void enqueue_word(struct WordQueue *q, struct WordNode *n)
{
if (q->tail == NULL)
{
q->head = q->tail = n;
q->size += 1;
return;
}
q->tail->next = n;
q->tail = n;
q->size += 1;
}
char *dequeue_word(struct WordQueue *q)
{
char *first_word = q->head->word;
q->head = q->head->next;
q->size -= 1;
if (q->head == NULL)
{
q->tail = NULL;
}
return first_word;
}
//Create new log
struct LogNode *create_new_log(char *word, int correctness)
{
struct LogNode *temp = (struct LogNode *)malloc(sizeof(struct LogNode));
temp->word = (char *)malloc(sizeof(char) * strlen(word));
strcpy(temp->word, word);
temp->correctness = correctness;
temp->next = NULL;
return temp;
}
//Create new word node
struct WordNode *create_new_word_node(char *word)
{
struct WordNode *temp = (struct WordNode *)malloc(sizeof(struct WordNode));
temp->word = (char *)malloc(sizeof(char) * strlen(word));
strcpy(temp->word, word);
temp->next = NULL;
return temp;
}
//Create new log queue
struct LogQueue *allocate_log_queue_with_capacity(unsigned capacity)
{
struct LogQueue *q = (struct LogQueue *)malloc(sizeof(struct LogQueue));
q->head = q->tail = NULL;
q->size = 0;
q->capacity = capacity;
return q;
}
//Create new word queue
struct WordQueue *allocate_word_queue_with_capacity(unsigned capacity)
{
struct WordQueue *q = (struct WordQueue *)malloc(sizeof(struct WordQueue));
q->head = q->tail = NULL;
q->capacity = capacity;
return q;
}
struct ClientQueue *allocate_client_queue_with_capacity(unsigned capacity)
{
struct ClientQueue *queue = (struct ClientQueue *)malloc(sizeof(struct ClientQueue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1;
queue->array = (int *)malloc(queue->capacity * sizeof(int));
return queue;
}
int queue_is_full(struct ClientQueue *queue)
{
return (queue->size == queue->capacity);
}
int log_queue_is_full(struct LogQueue *queue)
{
return (queue->size == queue->capacity);
}
int queue_is_empty(struct ClientQueue *queue)
{
return (queue->size <= 0);
}
int log_queue_is_empty(struct LogQueue *queue)
{
return (queue->size <= 0);
}
int word_queue_is_empty(struct WordQueue *queue)
{
return (queue->size <= 0);
}
/********************************* Spell Checking Helpers **************************************/
//Perform binary search using strcmp - return -1 if not found, else return the index
int binary_search(char *word, char **words_array)
{
int length = 0;
char **ptr = words_array;
while (*ptr != NULL)
{
length += 1;
ptr++;
}
int mid;
int left = 0;
int right = length - 1;
while (left <= right)
{
mid = left + (right - left) / 2;
if (strcmp(words_array[mid], word) == 0)
return mid;
if (strcmp(words_array[mid], word) > 0)
right = mid - 1;
else
left = mid + 1;
}
return -1;
}
int linear_search(char *word, char **words_array)
{
char **ptr = words_array;
while (*ptr != NULL)
{
if (strcmp(*ptr, word) == 0)
{
return 0;
}
ptr++;
}
return -1;
}
char **create_word_array_from_file(char *file_name)
{
char **words_array;
char *buffer = NULL;
size_t linecap = 0;
ssize_t numchars;
int i = 0;
FILE *fp;
fp = fopen(file_name, "r");
if (fp == NULL)
{
fprintf(stderr, "error opening file: %s", strerror(errno));
exit(0);
}
//Count number of words in file for malloc
int words = 0;
char ch;
while ((ch = fgetc(fp)) != EOF)
{
// Check words
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
rewind(fp);
words_array = malloc(words * sizeof(char *));
while ((numchars = getline(&buffer, &linecap, fp)) > 0)
{
words_array[i] = malloc(numchars * sizeof(char));
if (buffer[numchars - 1] == '\n')
{
buffer[numchars - 1] = '\0';
}
strcpy(words_array[i], buffer);
i++;
}
fclose(fp);
return words_array;
}
struct WordQueue *create_words_queue_from_file(char *file_name)
{
struct WordQueue *words_queue;
struct WordNode *word;
char *buffer = NULL;
size_t linecap = 0;
ssize_t numchars;
int i = 0;
FILE *fp;
fp = fopen(file_name, "r");
if (fp == NULL)
{
fprintf(stderr, "error opening file: %s", strerror(errno));
exit(0);
}
//Count number of words in file for initalizing queue
int words = 0;
char ch;
while ((ch = fgetc(fp)) != EOF)
{
// Check words
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
rewind(fp);
words_queue = allocate_word_queue_with_capacity(words);
while ((numchars = getline(&buffer, &linecap, fp)) > 0)
{
word = create_new_word_node(buffer);
enqueue_word(words_queue, word);
i++;
}
fclose(fp);
return words_queue;
}
/********************************* Wrapper functions **************************************/
void remove_newline_from_string(char *str)
{
str[strlen(str) - 1] = '\0';
}
void posix_error(int code, char *msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(code));
exit(EXIT_FAILURE);
}
void unix_error(char *msg) /* Unix-style error */
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(EXIT_FAILURE);
}
void Pthread_create(pthread_t *tid, pthread_attr_t *attr,
void *(*start_func)(void *), void *arg)
{
int rc;
if ((rc = pthread_create(tid, attr, start_func, arg)) != 0)
posix_error(rc, "Pthread_create error");
}
void Pthread_detach(pthread_t tid)
{
int rc;
if ((rc = pthread_detach(tid)) != 0)
posix_error(rc, "Pthread_detach error");
}