-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenize.c
More file actions
71 lines (70 loc) · 2.29 KB
/
tokenize.c
File metadata and controls
71 lines (70 loc) · 2.29 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
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "wf_table.h"
#include "stringbuf.h"
#include "tokenize.h"
#include "debugger.h"
int tokenize(int fd, wf_table *table, stringbuf *list) {
enum word_states word_state = wEmpty;
int bytesRead = 0;
char inter_buf[50];
bytesRead = read(fd, inter_buf, 50);
while ((bytesRead > 0)) {
int buf_read = 0;
while (buf_read < bytesRead) {
if (word_state == wEmpty) {
if (isalpha(inter_buf[buf_read]) || isdigit(inter_buf[buf_read]) || inter_buf[buf_read]=='-') {
int status = sb_append(list, inter_buf[buf_read]);
if (status==EXIT_FAILURE) {
return EXIT_FAILURE;
}
word_state = wIncomplete;
}
} else {
if (isalpha(inter_buf[buf_read]) || isdigit(inter_buf[buf_read]) || inter_buf[buf_read]=='-') {
int status = sb_append(list, inter_buf[buf_read]);
if (status==EXIT_FAILURE) {
return EXIT_FAILURE;
}
}
else if (isspace(inter_buf[buf_read])) {
word_state = wComplete;
}
}
if (word_state==wComplete) {
char *word = sb_get_lower_word(list);
/** if word is null, that means malloc failed */
if (!word) {
return EXIT_FAILURE;
}
word_state = wEmpty;
int status = hash_insert(table, word);
if (status==EXIT_FAILURE) {
return EXIT_FAILURE;
}
}
buf_read += 1;
}
bytesRead = read(fd, inter_buf, 50);
}
if (word_state==wIncomplete) {
char *word = sb_get_lower_word(list);
if (!word) {
return EXIT_FAILURE;
}
int status = hash_insert(table, word);
if (status==EXIT_FAILURE) {
return EXIT_FAILURE;
}
}
if (hash_lexical_list(table)==EXIT_FAILURE) {
return EXIT_FAILURE;
}
hash_comp_freq(table);
free(table->data);
return EXIT_SUCCESS;
}