aboutsummaryrefslogtreecommitdiff
path: root/ch6/6-02_common-prefix-printer.c
blob: f5314d78380ad9299af1a31fad169b9e2e4e56e8 (plain)
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

/* The C Programming Language: 2nd Edition
 *
 * Exercise 6-2: Write a program that reads a C program and prints in
 * alphabetical order each group of variable names that are identical in the
 * first 6 characters, but different somewhere thereafter. Don't count words
 * within string and comments. Make 6 a parameter that can be set from the
 * command line.
 *
 * Notes: First off, "variable names" should be "words". It doesn't make sense
 * otherwise.
 *
 * The trick to this is we can traverse the tree with no problem, but to do
 * comparisons on the words, we need to know the last one we worked with. With
 * two additional static variables in treeprint(), we have all we need to
 * manipulate what we're outputting.
 */

#define BUFSIZE 1000
#define MAXWORD 100
#define DEF_PRE_LEN 6

struct key {
	char *word;
	int count;
};
struct tnode {
	char *word;
	int count;
	struct tnode *left;
	struct tnode *right;
};

enum states {
	NORMAL,
	INQUOTE,
	INCOMMENT
};
char buf[BUFSIZE];
int bufp = 0;
int state = NORMAL;
int minlen;

int getch(void);
void ungetch(int);
int getword(char *, int);
struct key *binsearch(char *, struct key *, int);
struct tnode *addtree(struct tnode *, char *);
void treeprint(struct tnode *, int);
struct tnode *talloc(void);
char *mystrdup(char *);
int check_prefix(char *, char *, int);

int main(int argc, char *argv[]) {
	switch (argc) {
		case 1:
			minlen = DEF_PRE_LEN;
			break;
		case 2:
			minlen = strtol(argv[1], NULL, 10);
			break;
		default:
			printf("Too many arguments.\n");
			return 1;
	}
	struct tnode *root;
	char word[MAXWORD];
	root = NULL;
	while (getword(word, MAXWORD) != EOF) {
		/* Only add the words we need to the tree */
		if (isalpha(word[0]) && strlen(word) > minlen) {
			root = addtree(root, word);
			if (root == NULL) {
				printf("No words at that length or greater were found.\n");
				return 1;
			}
		}
	}
	treeprint(root, minlen);
	return 0;
}

int getword(char *word, int lim) {
	int c;
	char *w = word;
	while (isspace(c = getch())) {
	}
	if (c != EOF && c != '#' && c != '"' && c != '/' && c != '*') {
		*w++ = c;
	}
	if (c == '*' && state == INCOMMENT) {
		if ((c = getch()) == '/') {
			state == NORMAL;
			return '/';
		}
	}
	/* Ignore comments */
	if (c == '/') {
		c = getch();
		if (c == '/') {
			while ((c = getch()) != EOF && c != '\n') {
			}
			return '*';
		}
		if (c == '*') {
			state = INCOMMENT;
			return '*';
		}
	}
	/* Handle quotes */
	if (c == '"') {
		state = INQUOTE;
	}
	if (state == INQUOTE) {
		while ((c = getch()) != '"' && c != EOF) {
		}
		state = NORMAL;
	}
	/* Ignore preprocessor lines */
	if (c == '#') {
		while ((c = getch()) != '\n' && c != EOF) {
		}
	}
	/* Add exceptions for underscores */
	if (!isalpha(c) && c != '_') {
		*w = '\0';
		return c;
	}
	for (; --lim > 0; w++) {
		if (!isalnum(*w = getch()) && *w != '_') {
			ungetch(*w);
			break;
		}
	}
	*w = '\0';
	return word[0];
}

struct key *binsearch(char *word, struct key *tab, int n) {
	int cond;
	struct key *low = &tab[0];
	struct key *high = &tab[n];
	struct key *mid;
	while (low < high) {
		mid = low + (high - low) / 2;
		if ((cond = strcmp(word, mid->word)) < 0) {
			high = mid;
		} else if (cond > 0) {
			low = mid + 1;
		} else {
			return mid;
		}
	}
	return NULL;
}

int getch(void) {
	return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) {
	if (bufp >= BUFSIZE) {
		printf("ungetch: Too many characters.\n");
	} else {
		buf[bufp++] = c;
	}
}

struct tnode *addtree(struct tnode *p, char *w) {
	int cond;
	if (p == NULL) {
		p = talloc();
		p->word = mystrdup(w);
		p->count = 1;
		p->left = p->right = NULL;
	} else if ((cond = strcmp(w, p->word)) == 0) {
		p->count++;
	} else if (cond < 0) {
		p->left = addtree(p->left, w);
	} else {
		p->right = addtree(p->right, w);
	}
	return p;
}

int check_prefix(char *s1, char *s2, int len) {
	int i;
	if (len == 0) {
		return 1;
	}
	for (i == 0; *s1 == *s2 && *s1 != '\0' && i < len; i++, *s1++, *s2++) {
	}
	if (i == len) {
		return 1;
	}
	return 0;
}

void treeprint(struct tnode *p, int len) {
	static struct tnode *last; /* The last word we saw */
	static int output = 1; /* Controls whether we output 'last' or not */
	if (p != NULL) {
		treeprint(p->left, len);
		if (len == 0) {
			printf("%4d %s\n", p->count, p->word);
		} else {
			if (last != NULL) {
				if (check_prefix(last->word, p->word, len)) {
					if (output) {
						printf("%4d %s\n", last->count, last->word);
						output = 0;
					}
					printf("%4d %s\n", p->count, p->word);
				} else {
					output = 1;
				}
			}
			last = p;
		}
		treeprint(p->right, len);
	}
}

struct tnode *talloc(void) {
	return (struct tnode *) malloc(sizeof(struct tnode));
}

char *mystrdup(char *s) {
	char *p;
	p = (char *) malloc(strlen(s) + 1);
	if (p != NULL) {
		strcpy(p , s);
	}
	return p;
}