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 $
37 * $DragonFly: src/games/fortune/strfile/strfile.c,v 1.6 2008/07/10 18:29:51 swildner Exp $
39 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
40 * @(#)strfile.c 8.1 (Berkeley) 5/31/93
41 * $FreeBSD: src/games/fortune/strfile/strfile.c,v 1.15.2.2 2001/03/05 11:52:37 kris Exp $
44 # include <sys/param.h>
45 # include <netinet/in.h>
57 * This program takes a file composed of strings separated by
58 * lines starting with two consecutive delimiting character (default
59 * character is '%') and creates another file which consists of a table
60 * describing the file (structure from "strfile.h"), a table of seek
61 * pointers to the start of the strings, and the strings, each terminated
62 * by a null byte. Usage:
64 * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
66 * C - Allow comments marked by a double delimiter at line's beginning
67 * c - Change delimiting character from '%' to 'C'
68 * s - Silent. Give no summary of data processed at the end of
70 * o - order the strings in alphabetic order
71 * i - if ordering, ignore case
72 * r - randomize the order of the strings
75 * Ken Arnold Sept. 7, 1978 --
77 * Added ordering options.
80 # define STORING_PTRS (Oflag || Rflag)
81 # define CHUNKSIZE 512
83 # define ALLOC(ptr,sz) { \
85 ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \
86 else if (((sz) + 1) % CHUNKSIZE == 0) \
87 ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \
89 fprintf(stderr, "out of space\n"); \
99 char *Infile = NULL, /* input file name */
100 Outfile[MAXPATHLEN] = "", /* output file name */
101 Delimch = '%'; /* delimiting character */
103 int Cflag = false; /* embedded comments */
104 int Sflag = false; /* silent run flag */
105 int Oflag = false; /* ordering flag */
106 int Iflag = false; /* ignore case flag */
107 int Rflag = false; /* randomize order flag */
108 int Xflag = false; /* set rotated bit */
109 long Num_pts = 0; /* number of pointers/strings */
113 FILE *Sort_1, *Sort_2; /* pointers for sorting */
115 STRFILE Tbl; /* statistics table */
117 STR *Firstch; /* first chars of each string */
119 void add_offset (FILE *, long);
120 int cmp_str (const void *, const void *);
121 static int collate_range_cmp (int, int);
122 void do_order (void);
123 void getargs (int, char **);
124 void randomize (void);
129 * Drive the sucker. There are two main modes -- either we store
130 * the seek pointers, if the table is to be sorted or randomized,
131 * or we write the pointer directly to the file, if we are to stay
132 * in file order. If the former, we allocate and re-allocate in
133 * CHUNKSIZE blocks; if the latter, we just write each pointer,
134 * and then seek back to the beginning to write in the table.
137 main(int ac, char **av)
141 long last_off, length, pos, *p;
145 static char string[257];
147 (void) setlocale(LC_ALL, "");
149 getargs(ac, av); /* evalute arguments */
151 if ((inf = fopen(Infile, "r")) == NULL) {
156 if ((outf = fopen(Outfile, "w")) == NULL) {
161 (void) fseek(outf, (long) sizeof Tbl, 0);
164 * Write the strings onto the file
168 Tbl.str_shortlen = ~((unsigned long) 0);
170 Tbl.str_version = VERSION;
172 add_offset(outf, ftell(inf));
175 sp = fgets(string, 256, inf);
176 if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
178 length = pos - last_off - (sp ? strlen(sp) : 0);
182 add_offset(outf, pos);
183 if (Tbl.str_longlen < (unsigned long)length)
184 Tbl.str_longlen = length;
185 if (Tbl.str_shortlen > (unsigned long)length)
186 Tbl.str_shortlen = length;
190 for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
192 ALLOC(Firstch, Num_pts);
193 fp = &Firstch[Num_pts - 1];
194 if (Iflag && isupper((unsigned char)*nsp))
195 fp->first = tolower((unsigned char)*nsp);
198 fp->pos = Seekpts[Num_pts - 1];
201 } while (sp != NULL);
204 * write the tables in
208 Tbl.str_numstr = Num_pts - 1;
211 Tbl.str_flags |= STR_COMMENTS;
219 Tbl.str_flags |= STR_ROTATED;
222 printf("\"%s\" created\n", Outfile);
224 puts("There was 1 string");
226 printf("There were %ld strings\n", Num_pts - 1);
227 printf("Longest string: %lu byte%s\n", Tbl.str_longlen,
228 Tbl.str_longlen == 1 ? "" : "s");
229 printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen,
230 Tbl.str_shortlen == 1 ? "" : "s");
234 Tbl.str_version = htonl(Tbl.str_version);
235 Tbl.str_numstr = htonl(Tbl.str_numstr);
236 Tbl.str_longlen = htonl(Tbl.str_longlen);
237 Tbl.str_shortlen = htonl(Tbl.str_shortlen);
238 Tbl.str_flags = htonl(Tbl.str_flags);
239 (void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
241 for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
243 (void) fwrite((char *) Seekpts, sizeof *Seekpts, (int) Num_pts, outf);
250 * This routine evaluates arguments from the command line
253 getargs(int argc, char **argv)
257 while ((ch = getopt(argc, argv, "Cc:iorsx")) != -1)
259 case 'C': /* embedded comments */
262 case 'c': /* new delimiting char */
264 if (!isascii(Delimch)) {
265 printf("bad delimiting character: '\\%o\n'",
266 (unsigned char)Delimch);
269 case 'i': /* ignore case in ordering */
272 case 'o': /* order strings */
275 case 'r': /* randomize pointers */
278 case 's': /* silent */
281 case 'x': /* set the rotated bit */
293 (void) strcpy(Outfile, *argv);
296 puts("No input file name");
299 if (*Outfile == '\0') {
300 (void) strcpy(Outfile, Infile);
301 (void) strcat(Outfile, ".dat");
308 (void) fprintf(stderr,
309 "strfile [-Ciorsx] [-c char] sourcefile [datafile]\n");
315 * Add an offset to the list, or write it out, as appropriate.
318 add_offset(FILE *fp, long off)
324 fwrite(&net, 1, sizeof net, fp);
326 ALLOC(Seekpts, Num_pts + 1);
327 Seekpts[Num_pts] = off;
334 * Order the strings alphabetically (possibly ignoring case).
343 Sort_1 = fopen(Infile, "r");
344 Sort_2 = fopen(Infile, "r");
345 qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
351 (void) fclose(Sort_1);
352 (void) fclose(Sort_2);
353 Tbl.str_flags |= STR_ORDERED;
357 collate_range_cmp (int c1, int c2)
359 static char s1[2], s2[2];
368 if ((ret = strcoll(s1, s2)) != 0)
375 * Compare two strings in the file
378 cmp_str(const void *s1, const void *s2)
385 # define SET_N(nf,ch) (nf = (ch == '\n'))
386 # define IS_END(ch,nf) (ch == EOF || (ch == (unsigned char) Delimch && nf))
388 p1 = (const STR *) s1;
389 p2 = (const STR *) s2;
391 c1 = (unsigned char) p1->first;
392 c2 = (unsigned char) p2->first;
393 if ((r = collate_range_cmp(c1, c2)) != 0)
396 (void) fseek(Sort_1, p1->pos, 0);
397 (void) fseek(Sort_2, p2->pos, 0);
401 while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0' && c1 != EOF)
403 while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0' && c2 != EOF)
406 while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
413 if ((r = collate_range_cmp(c1, c2)) != 0)
424 return collate_range_cmp(c1, c2);
429 * Randomize the order of the string table. We must be careful
430 * not to randomize across delimiter boundaries. All
431 * randomization is done within each block.
442 Tbl.str_flags |= STR_RANDOM;
443 cnt = Tbl.str_numstr;
446 * move things around randomly
449 for (sp = Seekpts; cnt > 0; cnt--, sp++) {