28 |
|
|
29 |
#endif |
#endif |
30 |
|
|
31 |
|
#define BUFSIZE 256 |
32 |
|
|
33 |
void usage () |
void usage () |
34 |
{ |
{ |
35 |
fputs("Compresses or uncompresses sorted word lists.\n" , stderr); |
fputs("Compresses or uncompresses sorted word lists.\n" , stderr); |
36 |
fputs("For best result the locale should be set to C\n" , stderr); |
fputs("For best result the locale should be set to C\n" , stderr); |
37 |
fputs("before sorting by setting the environmental\n" , stderr); |
fputs("before sorting by setting the environmental\n" , stderr); |
38 |
fputs("variable LANG to \"C\" before sorting.\n" , stderr); |
fputs("variable LANG to \"C\" before sorting.\n" , stderr); |
39 |
fputs("Copyright 2001 by Kevin Atkinson.\n" , stderr); |
fputs("Copyright 2001,2004 by Kevin Atkinson.\n" , stderr); |
40 |
fputs("Usage: word-list-compress c[ompress]|d[ecompress]\n" , stderr); |
fputs("Usage: word-list-compress c[ompress]|d[ecompress]\n" , stderr); |
41 |
} |
} |
42 |
|
|
43 |
static int get_word(FILE * in, char * w) |
// PRECOND: bufsize >= 2 |
44 |
|
static int get_word(FILE * in, char * w, size_t bufsize) |
45 |
{ |
{ |
46 |
int c; |
int c; |
47 |
while (c = getc(in), c != EOF && c <= 32); |
while (c = getc(in), c != EOF && c <= 32); |
48 |
if (c == EOF) return 0; |
if (c == EOF) return 0; |
49 |
do { |
do { |
50 |
*w++ = (char)(c); |
*w++ = (char)(c); |
51 |
} while (c = getc(in), c != EOF && c > 32); |
--bufsize; |
52 |
|
} while (c = getc(in), c != EOF && c > 32 && bufsize > 1); |
53 |
*w = '\0'; |
*w = '\0'; |
54 |
ungetc(c, in); |
ungetc(c, in); |
55 |
if (c == EOF) return 0; |
if (c == EOF) return 0; |
65 |
|
|
66 |
} else if (argv[1][0] == 'c') { |
} else if (argv[1][0] == 'c') { |
67 |
|
|
68 |
char s1[256]; |
char s1[BUFSIZE]; |
69 |
char s2[256]; |
char s2[BUFSIZE]; |
70 |
char * prev = s2; |
char * prev = s2; |
71 |
char * cur = s1; |
char * cur = s1; |
72 |
*prev = '\0'; |
*prev = '\0'; |
73 |
|
|
74 |
SETBIN (stdout); |
SETBIN (stdout); |
75 |
|
|
76 |
while (get_word(stdin, cur)) { |
while (get_word(stdin, cur, BUFSIZE)) { |
77 |
int i = 0; |
int i = 0; |
78 |
/* get the length of the prefix */ |
/* get the length of the prefix */ |
79 |
while (prev[i] != '\0' && cur[i] != '\0' && prev[i] == cur[i]) |
while (prev[i] != '\0' && cur[i] != '\0' && prev[i] == cur[i]) |
80 |
++i; |
++i; |
81 |
if (i > 31) { |
if (i > 31) { |
82 |
putc('\0', stdout); |
putc('\0', stdout); |
83 |
} |
} |
84 |
putc(i+1, stdout); |
putc(i+1, stdout); |
85 |
fputs(cur+i, stdout); |
fputs(cur+i, stdout); |
86 |
if (cur == s1) { |
if (cur == s1) { |
87 |
prev = s1; cur = s2; |
prev = s1; cur = s2; |
88 |
} else { |
} else { |
89 |
prev = s2; cur = s1; |
prev = s2; cur = s1; |
90 |
} |
} |
91 |
} |
} |
92 |
return 0; |
return 0; |
102 |
i = getc(stdin); |
i = getc(stdin); |
103 |
while (i != -1 ) { |
while (i != -1 ) { |
104 |
if (i == 0) |
if (i == 0) |
105 |
i = getc(stdin); |
i = getc(stdin); |
106 |
--i; |
--i; |
107 |
while ((c = getc(stdin)) > 32) |
if (i < 0) goto error; |
108 |
cur[i++] = (char)c; |
while ((c = getc(stdin)) > 32 && i < BUFSIZE) |
109 |
|
cur[i++] = (char)c; |
110 |
|
if (i >= BUFSIZE) goto error; |
111 |
cur[i] = '\0'; |
cur[i] = '\0'; |
112 |
fputs(cur, stdout); |
fputs(cur, stdout); |
113 |
putc('\n', stdout); |
putc('\n', stdout); |
115 |
} |
} |
116 |
return 0; |
return 0; |
117 |
|
|
118 |
|
error: |
119 |
|
fputs("ERROR: Corrupt Input.\n", stderr); |
120 |
|
return 2; |
121 |
|
|
122 |
} else { |
} else { |
123 |
|
|
124 |
usage(); |
usage(); |