-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilestat.c
246 lines (191 loc) · 4.18 KB
/
filestat.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
// wc clone
#include <stdio.h>
#include "60lib.c"
//
// stat helpers
//
int is_word_break(char c)
{
return (c == ' ' || c == '.' || c == ',' || c == ';' || c == '\n' || c == EOF);
}
int is_visible(int c)
{
return (c && c != '\n' && c != EOF);
}
char LAST_CHAR_GARBAGE = 1;
// returns 1 on word end
int inc_stats(char c, long *chars, long *lines, long *words)
{
int res = 0;
// Increment counters
if (c == '\n')
(*lines)++;
if (!LAST_CHAR_GARBAGE && is_word_break(c))
{
(*words)++;
res = 1;
}
(*chars)++;
LAST_CHAR_GARBAGE = (is_word_break(c) || !is_visible(c));
return res;
}
//
// search helpers
//
#include <stdlib.h>
struct tnode
{
char *word;
int occurences;
struct tnode *left, *right;
};
void destroy_tree(struct tnode *p)
{
if (p == NULL)
return;
destroy_tree(p->left);
destroy_tree(p->right);
free(p);
}
// creates a node with word
struct tnode *make_tnode(char *word)
{
struct tnode *p = malloc(sizeof(struct tnode));
check_alloc(p, 1);
p->word = clone_string(word);
p->occurences = 1;
p->left = NULL;
p->right = NULL;
return p;
}
// adds a new node to the left or to the right of the given one
void add_tnode(struct tnode *p, char *word)
{
int cmp = strcmp(p->word, word);
if (cmp < 0)
{
struct tnode *new_right = make_tnode(word);
new_right->right = p->right;
p->right = new_right;
}
else if (cmp > 0)
{
struct tnode *new_left = make_tnode(word);
new_left->left = p->left;
p->left = new_left;
}
}
// returns 0 if doesn't find word
// 1 if does
// res is the last viewed node
int search(struct tnode **res, struct tnode *p, char *word)
{
int cmp = strcmp(p->word, word);
*res = p;
if (cmp < 0)
p = p->right;
else if (cmp > 0)
p = p->left;
else
return 1;
if (p == NULL)
return 0;
return search(res, p, word);
}
void reg_word(struct tnode* p, char *word)
{
struct tnode *res;
int found = search(&res, p, word);
if (found)
{
res->occurences++;
return;
}
// entry not found, create it
add_tnode(res, word);
// no need to increment occurences there, 1 is the default
}
int main(int argc, char **argv)
{
//
// Open the file
//
FILE *f;
if (argc > 1)
{
f = fopen(argv[1], "r");
printf("Using filename\n");
}
else
{
f = stdin;
printf("Using stdin\n");
}
if (f == NULL)
error("Can't open the file.\n", 1);
//
// Read the file, count stats and build the tree
//
// list head
struct tnode *tree = make_tnode(""); // empty string can't be a word
int c = fgetc(f);
long chars = 0;
long lines = 0;
long words = 0;
char *word = NULL;
int word_size = 0;
while (1)
{
// Increment stats
int word_ended = inc_stats(c, &chars, &lines, &words);
// Add character to word
if (is_visible(c) && !is_word_break(c))
{
word_size++;
word = realloc(word, word_size + 1);
word[word_size] = 0;
word[word_size - 1] = c;
}
// Add word to tree
if (word_ended)
{
reg_word(tree, word);
word_size = 0;
word[0] = 0;
}
int exit_sign = 0;
if (c == '\n')
exit_sign = 1;
// Next character
c = fgetc(f);
if (c == '\n' && exit_sign)
break;
if (c == EOF)
break;
}
//
// Output stats
//
printf("Lines: %ld, words: %ld, characters: %ld\n", lines, words, chars);
//
// Search: Reading the needle
//
free(word);
printf("Enter the word:\n");
word = read_string('\n');
printf("The word is: \"%s\"\n", word);
//
// Search: searching
//
struct tnode *t;
int found = search(&t, tree, word);
//
// Search: output
//
printf("%d occurences\n", (found ? t->occurences : 0));
//
// Finish the program: clean the memory, etc.
//
destroy_tree(tree);
return 0;
}