2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/games/fortune/strfile/strfile.c,v 1.15.2.2 2001/03/05 11:52:37 kris Exp $
40 static const char copyright[] =
41 "@(#) Copyright (c) 1989, 1993\n\
42 The Regents of the University of California. All rights reserved.\n";
47 static const char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
49 static const char rcsid[] =
50 "$FreeBSD: src/games/fortune/strfile/strfile.c,v 1.15.2.2 2001/03/05 11:52:37 kris Exp $";
54 # include <sys/param.h>
65 * This program takes a file composed of strings separated by
66 * lines starting with two consecutive delimiting character (default
67 * character is '%') and creates another file which consists of a table
68 * describing the file (structure from "strfile.h"), a table of seek
69 * pointers to the start of the strings, and the strings, each terminated
70 * by a null byte. Usage:
72 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
74 * C - Allow comments marked by a double delimiter at line's beginning
75 * c - Change delimiting character from '%' to 'C'
76 * s - Silent. Give no summary of data processed at the end of
78 * o - order the strings in alphabetic order
79 * i - if ordering, ignore case
80 * r - randomize the order of the strings
83 * Ken Arnold Sept. 7, 1978 --
85 * Added ordering options.
91 # define STORING_PTRS (Oflag || Rflag)
92 # define CHUNKSIZE 512
94 # define ALLOC(ptr,sz) { \
96 ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \
97 else if (((sz) + 1) % CHUNKSIZE == 0) \
98 ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \
100 fprintf(stderr, "out of space\n"); \
114 char *Infile = NULL, /* input file name */
115 Outfile[MAXPATHLEN] = "", /* output file name */
116 Delimch = '%'; /* delimiting character */
118 int Cflag = FALSE; /* embedded comments */
119 int Sflag = FALSE; /* silent run flag */
120 int Oflag = FALSE; /* ordering flag */
121 int Iflag = FALSE; /* ignore case flag */
122 int Rflag = FALSE; /* randomize order flag */
123 int Xflag = FALSE; /* set rotated bit */
124 long Num_pts = 0; /* number of pointers/strings */
128 FILE *Sort_1, *Sort_2; /* pointers for sorting */
130 STRFILE Tbl; /* statistics table */
132 STR *Firstch; /* first chars of each string */
134 void add_offset __P((FILE *, long));
135 int cmp_str __P((const void *, const void *));
136 static int collate_range_cmp __P((int, int));
137 void do_order __P((void));
138 void getargs __P((int, char **));
139 void randomize __P((void));
140 void usage __P((void));
144 * Drive the sucker. There are two main modes -- either we store
145 * the seek pointers, if the table is to be sorted or randomized,
146 * or we write the pointer directly to the file, if we are to stay
147 * in file order. If the former, we allocate and re-allocate in
148 * CHUNKSIZE blocks; if the latter, we just write each pointer,
149 * and then seek back to the beginning to write in the table.
157 long last_off, length, pos, *p;
161 static char string[257];
163 (void) setlocale(LC_ALL, "");
165 getargs(ac, av); /* evalute arguments */
167 if ((inf = fopen(Infile, "r")) == NULL) {
172 if ((outf = fopen(Outfile, "w")) == NULL) {
177 (void) fseek(outf, (long) sizeof Tbl, 0);
180 * Write the strings onto the file
184 Tbl.str_shortlen = ~((unsigned long) 0);
186 Tbl.str_version = VERSION;
188 add_offset(outf, ftell(inf));
191 sp = fgets(string, 256, inf);
192 if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
194 length = pos - last_off - (sp ? strlen(sp) : 0);
198 add_offset(outf, pos);
199 if (Tbl.str_longlen < length)
200 Tbl.str_longlen = length;
201 if (Tbl.str_shortlen > length)
202 Tbl.str_shortlen = length;
206 for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
208 ALLOC(Firstch, Num_pts);
209 fp = &Firstch[Num_pts - 1];
210 if (Iflag && isupper((unsigned char)*nsp))
211 fp->first = tolower((unsigned char)*nsp);
214 fp->pos = Seekpts[Num_pts - 1];
217 } while (sp != NULL);
220 * write the tables in
224 Tbl.str_numstr = Num_pts - 1;
227 Tbl.str_flags |= STR_COMMENTS;
235 Tbl.str_flags |= STR_ROTATED;
238 printf("\"%s\" created\n", Outfile);
240 puts("There was 1 string");
242 printf("There were %ld strings\n", Num_pts - 1);
243 printf("Longest string: %lu byte%s\n", Tbl.str_longlen,
244 Tbl.str_longlen == 1 ? "" : "s");
245 printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen,
246 Tbl.str_shortlen == 1 ? "" : "s");
250 Tbl.str_version = htonl(Tbl.str_version);
251 Tbl.str_numstr = htonl(Tbl.str_numstr);
252 Tbl.str_longlen = htonl(Tbl.str_longlen);
253 Tbl.str_shortlen = htonl(Tbl.str_shortlen);
254 Tbl.str_flags = htonl(Tbl.str_flags);
255 (void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
257 for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
259 (void) fwrite((char *) Seekpts, sizeof *Seekpts, (int) Num_pts, outf);
266 * This routine evaluates arguments from the command line
268 void getargs(argc, argv)
274 while ((ch = getopt(argc, argv, "Cc:iorsx")) != EOF)
276 case 'C': /* embedded comments */
279 case 'c': /* new delimiting char */
281 if (!isascii(Delimch)) {
282 printf("bad delimiting character: '\\%o\n'",
283 (unsigned char)Delimch);
286 case 'i': /* ignore case in ordering */
289 case 'o': /* order strings */
292 case 'r': /* randomize pointers */
295 case 's': /* silent */
298 case 'x': /* set the rotated bit */
310 (void) strcpy(Outfile, *argv);
313 puts("No input file name");
316 if (*Outfile == '\0') {
317 (void) strcpy(Outfile, Infile);
318 (void) strcat(Outfile, ".dat");
324 (void) fprintf(stderr,
325 "strfile [-Ciorsx] [-c char] sourcefile [datafile]\n");
331 * Add an offset to the list, or write it out, as appropriate.
333 void add_offset(fp, off)
341 fwrite(&net, 1, sizeof net, fp);
343 ALLOC(Seekpts, Num_pts + 1);
344 Seekpts[Num_pts] = off;
351 * Order the strings alphabetically (possibly ignoring case).
359 Sort_1 = fopen(Infile, "r");
360 Sort_2 = fopen(Infile, "r");
361 qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
367 (void) fclose(Sort_1);
368 (void) fclose(Sort_2);
369 Tbl.str_flags |= STR_ORDERED;
372 static int collate_range_cmp (c1, c2)
375 static char s1[2], s2[2];
384 if ((ret = strcoll(s1, s2)) != 0)
391 * Compare two strings in the file
401 # define SET_N(nf,ch) (nf = (ch == '\n'))
402 # define IS_END(ch,nf) (ch == EOF || (ch == (unsigned char) Delimch && nf))
404 p1 = (const STR *) s1;
405 p2 = (const STR *) s2;
407 c1 = (unsigned char) p1->first;
408 c2 = (unsigned char) p2->first;
409 if ((r = collate_range_cmp(c1, c2)) != 0)
412 (void) fseek(Sort_1, p1->pos, 0);
413 (void) fseek(Sort_2, p2->pos, 0);
417 while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0' && c1 != EOF)
419 while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0' && c2 != EOF)
422 while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
429 if ((r = collate_range_cmp(c1, c2)) != 0)
440 return collate_range_cmp(c1, c2);
445 * Randomize the order of the string table. We must be careful
446 * not to randomize across delimiter boundaries. All
447 * randomization is done within each block.
457 Tbl.str_flags |= STR_RANDOM;
458 cnt = Tbl.str_numstr;
461 * move things around randomly
464 for (sp = Seekpts; cnt > 0; cnt--, sp++) {