-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsl.c
237 lines (199 loc) · 5.59 KB
/
sl.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
/*
* sl.c - Sanalaskuri
*
* Sanalaskuri is a word counting program that counts number of
* occurences of words in the given input stream. A word is defined as being
* at least two consecutive alphabetical characters as recognized by
* C's standard isalpha()-implementation.
*
* The program accepts up to two command line parameters. First one
* defines the input file to be used and the second one the output file.
* If the second one is omitted, stdout will be used for output.
* If the first one is omitted, stdin will be used for input.
*
* Functions defined in this file:
* int main(int argc, char *argv[])
* void printNode(BstNode *node)
*
* Dependencies:
* The implementation of sl depends on the existence of bst.c and bst.h.
* The binary search tree implementation provided by these files is used
* to store the words found and their counts.
*
* Functions accessed from bst:
* BstNode *bst_newNode(char *key, int data);
* BstTree *bst_newTree();
* void bst_inorderTreeWalk(BstTree *tree, void (*callback)(BstNode*));
* void bst_destroyTree(BstTree *tree);
* void bst_insertOrUpdate(BstTree tree*, BstNode *newNode);
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "bst.h"
#include "sl.h"
/* This needs to be accessible globally from all the functions */
FILE *output;
/*
* The main program for sl.
*
* Returns exit status.
*/
int main(int argc, char *argv[])
{
FILE *input = stdin;
char word[MAX_WORD_LENGTH+1];
int charCounter = 0;
int c;
BstTree *tree;
BstNode *node;
/* Default output to stdout */
output = stdout;
tree = bst_newTree();
/* Let's make sure the tree really is empty */
assert(tree->root == NULL);
/* Let's check for command line parameters */
if (argc == 1)
{
#ifndef NDEBUG
printf("Using stdin and stdout\n");
#endif
}
else
{
/* We want to use first command line parameter as input file */
#ifndef NDEBUG
printf("Trying to open '%s' for reading\n", argv[1]);
#endif
if ((input = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "Error opening ");
perror(argv[1]);
bst_destroyTree(tree);
return EXIT_FAILURE;
}
#ifndef NDEBUG
printf("Input file opened succesfully\n");
#endif
if (argc > 2)
{
/* We also have a second command line parameter so we'll use that for output. Rest are ignored. */
#ifndef NDEBUG
printf("Trying to open '%s' for writing\n", argv[2]);
#endif
if ((output = fopen(argv[2], "w")) == NULL)
{
fprintf(stderr, "Error opening ");
perror(argv[2]);
bst_destroyTree(tree);
return EXIT_FAILURE;
}
}
#ifndef NDEBUG
else
{
/* We didn't have a second command line parameter, so let's default to stdout. */
printf("Using stdout\n");
}
printf("Output file opened succesfully\n");
printf("Let's scan the input file...\n");
#endif
}
/* Read a character at a time... */
while ((c = fgetc(input)) != EOF)
{
/*... and see if it's alphabetical */
if (isalpha(c))
{
if (charCounter < MAX_WORD_LENGTH)
/* If the word is longer than the maximum word length, we just ignore the rest of it. */
{
/* Store the letter. */
#ifndef NDEBUG
printf("%c...", c);
#endif
word[charCounter] = c;
charCounter++;
}
#ifndef NDEBUG
else
{
printf("Word is unfortunately too long, ignoring letter...");
}
#endif
}
else
{
/* Character wasn't alphabetical so end the word and reset the counter */
word[charCounter] = '\0';
charCounter = 0;
#ifndef NDEBUG
printf("\nGot word: '%s'\n", word);
#endif
/* Words need to be atleast two characters long */
if (strlen(word) < 2)
{
#ifndef NDEBUG
printf("Too short, skipping\n");
#endif
}
else
{
/* Create a new BST node with the word as key and 1 as data */
node = bst_newNode(word, 1);
/* Make sure the node has correct data in it */
assert(node->parent == NULL && node->left == NULL && node->right == NULL && node->data == 1 && (strcmp(node->key, word) == 0));
#ifndef NDEBUG
printf("Trying to update the tree...\n");
#endif
bst_insertOrUpdate(tree, node);
/* Under no circumstances can the root of the tree be null at this point anymore */
assert(tree->root != NULL);
}
}
}
if (ferror(input))
{
/* In case we have an error while reading the file instead of hitting EOF */
perror("Error");
bst_destroyTree(tree);
return EXIT_FAILURE;
}
#ifndef NDEBUG
printf("Let's print the tree now, then. This will look ugly with all the debug information\n");
#endif
/* call bst_inorderTreeWalk with the tree as parameter and printNode function as callback (function pointer) to print it out */
bst_inorderTreeWalk(tree, printNode);
#ifndef NDEBUG
printf("We're done here, let's get rid of that tree... ");
#endif
bst_destroyTree(tree);
/* Close the input and output files */
if ((fclose(input)) == EOF)
{
perror("Error");
return EXIT_FAILURE;
}
if ((fclose(output)) == EOF)
{
perror("Error");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
* A callback function for bst_inorderTreeWalk. Tries to print
* the node's key and data to the defined output stream.
*
* Doesn't return anything.
*/
void printNode(BstNode *node)
{
if ((fprintf(output, "%s: %d\n", node->key, node->data)) < 0)
{
perror("Error");
exit(EXIT_FAILURE);
}
}