]>
Commit | Line | Data |
---|---|---|
997358a6 MW |
1 | #include <ctype.h> |
2 | #include <stdlib.h> | |
3 | #include <stdio.h> | |
4 | #include <string.h> | |
5 | #include <assert.h> | |
6 | ||
7 | #define MAX_LINE 512 | |
8 | ||
9 | void die( char * ) ; | |
10 | ||
11 | char buffer[MAX_LINE+1] ; | |
12 | char *prog_name ; | |
13 | ||
14 | void die( char *message ) | |
15 | { | |
16 | fflush(stdout) ; | |
17 | fprintf(stderr, "%s: %s\n", prog_name, message) ; | |
18 | exit(1) ; | |
19 | } | |
20 | ||
21 | int main(int argc, char* argv[]) | |
22 | { | |
23 | int errors ; | |
24 | prog_name = *argv ; | |
25 | if( argc != 1 ) | |
26 | die("pure filter, takes no arguments") ; | |
27 | errors = 0 ; | |
28 | while( fgets(buffer, MAX_LINE, stdin)) | |
29 | errors += do_line(buffer) ; | |
30 | exit(errors ? 1 : 0 ) ; | |
31 | } | |
32 | ||
33 | int do_line(char *data) | |
34 | { | |
35 | char *p, *q, *r, *end, *before, *after ; | |
36 | // expecting two tab-separated fields | |
37 | // point r to 2nd, null terminate 1st | |
38 | for( r = data ; *r && *r != '\t' ; r++ ) | |
39 | ; | |
40 | if( *r != '\t' ) | |
41 | return(1) ; | |
42 | end = r++ ; | |
43 | *end = '\0' ; | |
44 | for( q = r ; *q ; q++ ) | |
45 | if( *q == '\n' ) | |
46 | *q = '\0' ; | |
47 | if( !strlen(r) ) | |
48 | return(1) ; | |
49 | // within 1st, parse as space-separated | |
50 | // p will point to current word, q past its end | |
51 | // before & after point to rest of text | |
52 | // spaces converted to nulls & back as req'd | |
53 | before = "" ; | |
54 | for( p = data ; p < end ; p = q + 1 ) { | |
55 | if( p > data ) { | |
56 | before = data ; | |
57 | p[-1] = '\0' ; | |
58 | } | |
59 | // find end of word | |
60 | for( q = p ; *q && *q != ' ' ; q++ ) | |
61 | ; | |
62 | if( q == end ) | |
63 | after = "" ; | |
64 | else if( q < end ) { | |
65 | after = q + 1 ; | |
66 | *q = '\0' ; | |
67 | } | |
68 | else assert(0) ; | |
69 | print_line(before, p, after, r) ; | |
70 | if( q < end ) | |
71 | *q = ' ' ; | |
72 | if( p > data ) | |
73 | p[-1] = ' ' ; | |
74 | } | |
75 | return(0) ; | |
76 | } | |
77 | ||
78 | // print formatted line for permuted index | |
79 | // two tab-separated fields | |
80 | // 1st is sort key | |
81 | // 2nd is printable line | |
82 | // pipe it through something like | |
83 | // sort -F | awk -F '\t' '{print $2}' | |
84 | // to get final output | |
85 | ||
86 | print_line( char *before, char *word, char *after, char *tag) | |
87 | { | |
88 | int i , x, y, z ; | |
89 | /* | |
90 | printf("%s\t%s\t%s\t%s\n", before, word, after, tag) ; | |
91 | */ | |
92 | if( list_word(word) ) | |
93 | return ; | |
94 | x = strlen(before) ; | |
95 | y = strlen(word) ; | |
96 | z = strlen(after) ; | |
97 | // put in sortable field | |
98 | // strip out with awk after sorting | |
99 | printf("%s %s\t", word, after) ; | |
100 | // shorten before string to fit field | |
101 | for( ; x > 30 ; x-- ) | |
102 | before++ ; | |
103 | printf("%30s", before) ; | |
104 | // print keyword, html tagged | |
105 | printf(" %s%s</a> ", tag, word) ; | |
106 | // padding, outside tag | |
107 | for( ; y < 18 ; y++ ) | |
108 | putchar(' ') ; | |
109 | if( z ) | |
110 | printf("%s", after) ; | |
111 | printf("\n") ; | |
112 | } | |
113 | ||
114 | // avoid indexing on common English words | |
115 | ||
116 | char *list[] = { | |
117 | "the", "of", "a", "an", "to", "and", "or", "if", "for", "at", | |
118 | "am", "is", "are", "was", "were", "have", "has", "had", "be", "been", | |
119 | "on", "some", "with", "any", "into", "as", "by", "in", "out", | |
120 | "that", "then", "this", "that", "than", "these", "those", | |
121 | "he", "his", "him", "she", "her", "hers", "it", "its", | |
122 | "&", "", "+", "-", "=", "--", "<", ">", "<=", ">=", | |
123 | "!", "?", "#", "$", "%", "/", "\\", "\"", "\'", | |
124 | NULL | |
125 | } ; | |
126 | // interrogative words like "how" and "where" deliberately left out of | |
127 | // above list because users might want to search for "how to..." etc. | |
128 | ||
129 | // return 1 if word in list, else 0 | |
130 | // case-insensitive comparison | |
131 | ||
132 | list_word( char *p ) | |
133 | { | |
134 | char **z ; | |
135 | for( z = list ; *z != NULL ; z++ ) | |
136 | if( ! strcasecmp( p, *z ) ) | |
137 | return 1 ; | |
138 | return 0 ; | |
139 | } | |
140 |