]> git.sur5r.net Git - cc65/blob - test/ref/wf1.c
remote TABs in doc/ and test/
[cc65] / test / ref / wf1.c
1 /*
2   !!DESCRIPTION!! print word frequencies; uses structures
3   !!ORIGIN!!      LCC 4.1 Testsuite
4   !!LICENCE!!     own, freely distributeable for non-profit. read CPYRIGHT.LCC
5 */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <ctype.h>
11
12 #define MAXWORDS 250
13
14 FILE *in;
15 #define getchar() fgetc(in)
16
17 struct node
18 {
19         int count;              /* frequency count */
20         struct node *left;      /* left subtree */
21         struct node *right;     /* right subtree */
22         char *word;             /* word itself */
23 } words[MAXWORDS];
24 int next;               /* index of next free entry in words */
25
26 /*struct node *lookup();*/
27
28 #if defined(NO_NEW_PROTOTYPES_FOR_OLD_FUNC_DECL) && !defined(NO_OLD_FUNC_DECL)
29
30 #else
31
32 int err(char *s);
33 int getword(char *buf);
34 void tprint(struct node *tree);
35 struct node *lookup(char *word, struct node **p);
36
37 #endif
38
39 int isletter(char c);
40
41 /* err - print error message s and die  */
42 #ifndef NO_OLD_FUNC_DECL
43 err(s) char *s; {
44 #else
45 int err(char *s) {
46 #endif
47         printf("? %s\n", s);
48         exit(1);
49 }
50
51 /* getword - get next input word into buf, return 0 on EOF */
52 #ifndef NO_OLD_FUNC_DECL
53 int getword(buf) char *buf;
54 #else
55 int getword(char *buf)
56 #endif
57 {
58         char *s;
59         int c;
60
61         while (((c = getchar()) != -1) && (isletter(c) == 0))
62                 ;
63         for (s = buf; (c = isletter(c)); c = getchar())
64                 *s++ = c;
65         *s = 0;
66         if (s > buf)
67                 return 1;
68         return 0;
69 }
70
71 /* isletter - return folded version of c if it is a letter, 0 otherwise */
72 int isletter(char c)
73 {
74         if ((c >= 'A') && (c <= 'Z')) c += 'a' - 'A';
75         if ((c >= 'a') && (c <= 'z')) return c;
76         return 0;
77 }
78
79 /* lookup - lookup word in tree; install if necessary */
80 #ifndef NO_OLD_FUNC_DECL
81 struct node *lookup(word, p)
82 char *word; struct node **p;
83 #else
84 struct node *lookup(char *word, struct node **p)
85 #endif
86 {
87         int cond;
88 /*      char *malloc(); */
89
90         if (*p) {
91                 cond = strcmp(word, (*p)->word);
92                 if (cond < 0)
93                         return lookup(word, &(*p)->left);
94                 else if (cond > 0)
95                         return lookup(word, &(*p)->right);
96                 else
97                         return *p;
98         }
99         if (next >= MAXWORDS)
100                 err("out of node storage");
101         words[next].count = 0;
102         words[next].left = words[next].right = 0;
103         words[next].word = malloc(strlen(word) + 1);
104         if (words[next].word == 0)
105                 err("out of word storage");
106         strcpy(words[next].word, word);
107         return *p = &words[next++];
108 }
109
110 /* tprint - print tree */
111 #ifndef NO_OLD_FUNC_DECL
112 void tprint(tree) struct node *tree; {
113 #else
114 void tprint(struct node *tree) {
115 #endif
116         if (tree) {
117                 tprint(tree->left);
118                 printf("%d:%s\n", tree->count, tree->word);
119                 tprint(tree->right);
120         }
121 }
122
123 int main(void)
124 {
125         struct node *root;
126         char word[20];
127
128         in = fopen("wf1.in","rb");
129         if (in == NULL) {
130             return EXIT_FAILURE;
131         }
132
133         root = 0;
134         next = 0;
135         while (getword(word))
136                 lookup(word, &root)->count++;
137         tprint(root);
138
139         fclose(in);
140         return 0;
141 }