18aa99b3cSbostic /*-
2*2047fd46Sbostic * Copyright (c) 1989, 1993
3*2047fd46Sbostic * The Regents of the University of California. All rights reserved.
4557920e4Sbostic *
5557920e4Sbostic * This code is derived from software contributed to Berkeley by
6557920e4Sbostic * Ken Arnold.
7557920e4Sbostic *
88aa99b3cSbostic * %sccs.include.redist.c%
9557920e4Sbostic */
10557920e4Sbostic
11557920e4Sbostic #ifndef lint
12*2047fd46Sbostic static char copyright[] =
13*2047fd46Sbostic "@(#) Copyright (c) 1989, 1993\n\
14*2047fd46Sbostic The Regents of the University of California. All rights reserved.\n";
15557920e4Sbostic #endif /* not lint */
16557920e4Sbostic
17557920e4Sbostic #ifndef lint
18*2047fd46Sbostic static char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 05/31/93";
19557920e4Sbostic #endif /* not lint */
2007dabd2dSbostic
2107f88b87Sbostic # include <machine/endian.h>
22d7c3b200Sbostic # include <sys/param.h>
2307dabd2dSbostic # include <stdio.h>
2407dabd2dSbostic # include <ctype.h>
2507dabd2dSbostic # include "strfile.h"
26922b6d83Sbostic
27922b6d83Sbostic # ifndef MAXPATHLEN
28922b6d83Sbostic # define MAXPATHLEN 1024
29922b6d83Sbostic # endif /* MAXPATHLEN */
3007dabd2dSbostic
3107dabd2dSbostic /*
3207dabd2dSbostic * This program takes a file composed of strings seperated by
3307dabd2dSbostic * lines starting with two consecutive delimiting character (default
3407dabd2dSbostic * character is '%') and creates another file which consists of a table
3507dabd2dSbostic * describing the file (structure from "strfile.h"), a table of seek
36ee31de86Sbostic * pointers to the start of the strings, and the strings, each terminated
3707dabd2dSbostic * by a null byte. Usage:
3807dabd2dSbostic *
394b09ca94Sbostic * % strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
4007dabd2dSbostic *
4107dabd2dSbostic * c - Change delimiting character from '%' to 'C'
4207dabd2dSbostic * s - Silent. Give no summary of data processed at the end of
4307dabd2dSbostic * the run.
4407dabd2dSbostic * o - order the strings in alphabetic order
4507dabd2dSbostic * i - if ordering, ignore case
4607dabd2dSbostic * r - randomize the order of the strings
474b09ca94Sbostic * x - set rotated bit
4807dabd2dSbostic *
4907dabd2dSbostic * Ken Arnold Sept. 7, 1978 --
5007dabd2dSbostic *
5107dabd2dSbostic * Added ordering options.
5207dabd2dSbostic */
5307dabd2dSbostic
5407dabd2dSbostic # define TRUE 1
5507dabd2dSbostic # define FALSE 0
5607dabd2dSbostic
57922b6d83Sbostic # define STORING_PTRS (Oflag || Rflag)
58922b6d83Sbostic # define CHUNKSIZE 512
59922b6d83Sbostic
60922b6d83Sbostic #ifdef lint
61922b6d83Sbostic # define ALWAYS atoi("1")
62922b6d83Sbostic #else
63922b6d83Sbostic # define ALWAYS 1
64922b6d83Sbostic #endif
65922b6d83Sbostic # define ALLOC(ptr,sz) if (ALWAYS) { \
66922b6d83Sbostic if (ptr == NULL) \
67922b6d83Sbostic ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \
68922b6d83Sbostic else if (((sz) + 1) % CHUNKSIZE == 0) \
69922b6d83Sbostic ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \
70922b6d83Sbostic if (ptr == NULL) { \
71922b6d83Sbostic fprintf(stderr, "out of space\n"); \
72922b6d83Sbostic exit(1); \
73922b6d83Sbostic } \
74922b6d83Sbostic } else
75922b6d83Sbostic
76922b6d83Sbostic #ifdef NO_VOID
77922b6d83Sbostic # define void char
78922b6d83Sbostic #endif
7907dabd2dSbostic
8007dabd2dSbostic typedef struct {
8107dabd2dSbostic char first;
82922b6d83Sbostic off_t pos;
8307dabd2dSbostic } STR;
8407dabd2dSbostic
8507dabd2dSbostic char *Infile = NULL, /* input file name */
86922b6d83Sbostic Outfile[MAXPATHLEN] = "", /* output file name */
87ee31de86Sbostic Delimch = '%'; /* delimiting character */
8807dabd2dSbostic
8907dabd2dSbostic int Sflag = FALSE; /* silent run flag */
9007dabd2dSbostic int Oflag = FALSE; /* ordering flag */
9107dabd2dSbostic int Iflag = FALSE; /* ignore case flag */
9207dabd2dSbostic int Rflag = FALSE; /* randomize order flag */
934b09ca94Sbostic int Xflag = FALSE; /* set rotated bit */
9475ec9249Sbostic long Num_pts = 0; /* number of pointers/strings */
9507dabd2dSbostic
96922b6d83Sbostic off_t *Seekpts;
9707dabd2dSbostic
9807dabd2dSbostic FILE *Sort_1, *Sort_2; /* pointers for sorting */
9907dabd2dSbostic
10007dabd2dSbostic STRFILE Tbl; /* statistics table */
10107dabd2dSbostic
10207dabd2dSbostic STR *Firstch; /* first chars of each string */
10307dabd2dSbostic
104922b6d83Sbostic char *fgets(), *strcpy(), *strcat();
10507dabd2dSbostic
106922b6d83Sbostic void *malloc(), *realloc();
10707dabd2dSbostic
108922b6d83Sbostic /*
109922b6d83Sbostic * main:
110922b6d83Sbostic * Drive the sucker. There are two main modes -- either we store
111922b6d83Sbostic * the seek pointers, if the table is to be sorted or randomized,
112922b6d83Sbostic * or we write the pointer directly to the file, if we are to stay
113922b6d83Sbostic * in file order. If the former, we allocate and re-allocate in
114922b6d83Sbostic * CHUNKSIZE blocks; if the latter, we just write each pointer,
115922b6d83Sbostic * and then seek back to the beginning to write in the table.
116922b6d83Sbostic */
main(ac,av)11707dabd2dSbostic main(ac, av)
11807dabd2dSbostic int ac;
11907dabd2dSbostic char **av;
12007dabd2dSbostic {
12107dabd2dSbostic register char *sp, dc;
12207dabd2dSbostic register FILE *inf, *outf;
12375ec9249Sbostic register off_t last_off, length, pos, *p;
12475ec9249Sbostic register int first, cnt;
12507dabd2dSbostic register char *nsp;
12607dabd2dSbostic register STR *fp;
12707dabd2dSbostic static char string[257];
12807dabd2dSbostic
12907dabd2dSbostic getargs(ac, av); /* evalute arguments */
13007dabd2dSbostic dc = Delimch;
13107dabd2dSbostic if ((inf = fopen(Infile, "r")) == NULL) {
13207dabd2dSbostic perror(Infile);
1331f54991aSbostic exit(1);
13407dabd2dSbostic }
13507dabd2dSbostic
13607dabd2dSbostic if ((outf = fopen(Outfile, "w")) == NULL) {
13707dabd2dSbostic perror(Outfile);
1381f54991aSbostic exit(1);
13907dabd2dSbostic }
140922b6d83Sbostic if (!STORING_PTRS)
141922b6d83Sbostic (void) fseek(outf, sizeof Tbl, 0);
14207dabd2dSbostic
14307dabd2dSbostic /*
144922b6d83Sbostic * Write the strings onto the file
14507dabd2dSbostic */
14607dabd2dSbostic
14707dabd2dSbostic Tbl.str_longlen = 0;
14807dabd2dSbostic Tbl.str_shortlen = (unsigned int) 0xffffffff;
149922b6d83Sbostic Tbl.str_delim = dc;
15075ec9249Sbostic Tbl.str_version = VERSION;
15107dabd2dSbostic first = Oflag;
152922b6d83Sbostic add_offset(outf, ftell(inf));
153922b6d83Sbostic last_off = 0;
15407dabd2dSbostic do {
15507dabd2dSbostic sp = fgets(string, 256, inf);
156ee31de86Sbostic if (sp == NULL || sp[0] == dc && sp[1] == '\n') {
157922b6d83Sbostic pos = ftell(inf);
15807f88b87Sbostic length = pos - last_off - (sp ? strlen(sp) : 0);
159922b6d83Sbostic last_off = pos;
160ee31de86Sbostic if (!length)
161ee31de86Sbostic continue;
162ee31de86Sbostic add_offset(outf, pos);
163922b6d83Sbostic if (Tbl.str_longlen < length)
164922b6d83Sbostic Tbl.str_longlen = length;
165922b6d83Sbostic if (Tbl.str_shortlen > length)
166922b6d83Sbostic Tbl.str_shortlen = length;
16707dabd2dSbostic first = Oflag;
16807dabd2dSbostic }
169ee31de86Sbostic else if (first) {
17007dabd2dSbostic for (nsp = sp; !isalnum(*nsp); nsp++)
17107dabd2dSbostic continue;
172922b6d83Sbostic ALLOC(Firstch, Num_pts);
173922b6d83Sbostic fp = &Firstch[Num_pts - 1];
17407dabd2dSbostic if (Iflag && isupper(*nsp))
17507dabd2dSbostic fp->first = tolower(*nsp);
17607dabd2dSbostic else
17707dabd2dSbostic fp->first = *nsp;
178922b6d83Sbostic fp->pos = Seekpts[Num_pts - 1];
17907dabd2dSbostic first = FALSE;
18007dabd2dSbostic }
18107dabd2dSbostic } while (sp != NULL);
18207dabd2dSbostic
18307dabd2dSbostic /*
18407dabd2dSbostic * write the tables in
18507dabd2dSbostic */
18607dabd2dSbostic
18707dabd2dSbostic (void) fclose(inf);
18807dabd2dSbostic
18907dabd2dSbostic if (Oflag)
190922b6d83Sbostic do_order();
19107dabd2dSbostic else if (Rflag)
192922b6d83Sbostic randomize();
19307dabd2dSbostic
1944b09ca94Sbostic if (Xflag)
1954b09ca94Sbostic Tbl.str_flags |= STR_ROTATED;
1964b09ca94Sbostic
19707dabd2dSbostic if (!Sflag) {
198922b6d83Sbostic printf("\"%s\" created\n", Outfile);
199922b6d83Sbostic if (Num_pts == 2)
20007dabd2dSbostic puts("There was 1 string");
20107dabd2dSbostic else
202d7c3b200Sbostic printf("There were %d strings\n", Num_pts - 1);
203d7c3b200Sbostic printf("Longest string: %lu byte%s\n", Tbl.str_longlen,
20407dabd2dSbostic Tbl.str_longlen == 1 ? "" : "s");
205d7c3b200Sbostic printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen,
20607dabd2dSbostic Tbl.str_shortlen == 1 ? "" : "s");
20707dabd2dSbostic }
20875ec9249Sbostic
20975ec9249Sbostic (void) fseek(outf, (off_t) 0, 0);
21075ec9249Sbostic Tbl.str_version = htonl(Tbl.str_version);
21175ec9249Sbostic Tbl.str_numstr = htonl(Num_pts - 1);
21275ec9249Sbostic Tbl.str_longlen = htonl(Tbl.str_longlen);
21375ec9249Sbostic Tbl.str_shortlen = htonl(Tbl.str_shortlen);
21475ec9249Sbostic Tbl.str_flags = htonl(Tbl.str_flags);
21575ec9249Sbostic (void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
21675ec9249Sbostic if (STORING_PTRS) {
21775ec9249Sbostic for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
21875ec9249Sbostic *p = htonl(*p);
21975ec9249Sbostic (void) fwrite((char *) Seekpts, sizeof *Seekpts, (int) Num_pts, outf);
22075ec9249Sbostic }
22175ec9249Sbostic (void) fclose(outf);
22207dabd2dSbostic exit(0);
22307dabd2dSbostic }
22407dabd2dSbostic
22507dabd2dSbostic /*
22607dabd2dSbostic * This routine evaluates arguments from the command line
22707dabd2dSbostic */
getargs(argc,argv)228ee31de86Sbostic getargs(argc, argv)
229ee31de86Sbostic int argc;
230ee31de86Sbostic char **argv;
23107dabd2dSbostic {
232ee31de86Sbostic extern char *optarg;
233ee31de86Sbostic extern int optind;
234ee31de86Sbostic int ch;
23507dabd2dSbostic
2364b09ca94Sbostic while ((ch = getopt(argc, argv, "c:iorsx")) != EOF)
237ee31de86Sbostic switch(ch) {
23807dabd2dSbostic case 'c': /* new delimiting char */
239ee31de86Sbostic Delimch = *optarg;
240922b6d83Sbostic if (!isascii(Delimch)) {
24107dabd2dSbostic printf("bad delimiting character: '\\%o\n'",
24207dabd2dSbostic Delimch);
24307dabd2dSbostic }
24407dabd2dSbostic break;
24507dabd2dSbostic case 'i': /* ignore case in ordering */
24607dabd2dSbostic Iflag++;
24707dabd2dSbostic break;
248ee31de86Sbostic case 'o': /* order strings */
249ee31de86Sbostic Oflag++;
250ee31de86Sbostic break;
2514b09ca94Sbostic case 'r': /* randomize pointers */
25207dabd2dSbostic Rflag++;
25307dabd2dSbostic break;
254ee31de86Sbostic case 's': /* silent */
255ee31de86Sbostic Sflag++;
25607dabd2dSbostic break;
2574b09ca94Sbostic case 'x': /* set the rotated bit */
2584b09ca94Sbostic Xflag++;
2594b09ca94Sbostic break;
260ee31de86Sbostic case '?':
261ee31de86Sbostic default:
262ee31de86Sbostic usage();
26307dabd2dSbostic }
264ee31de86Sbostic argv += optind;
265ee31de86Sbostic
266ee31de86Sbostic if (*argv) {
267ee31de86Sbostic Infile = *argv;
268ee31de86Sbostic if (*++argv)
269ee31de86Sbostic (void) strcpy(Outfile, *argv);
27007dabd2dSbostic }
27107dabd2dSbostic if (!Infile) {
27207dabd2dSbostic puts("No input file name");
273ee31de86Sbostic usage();
27407dabd2dSbostic }
275ee31de86Sbostic if (*Outfile == '\0') {
27607dabd2dSbostic (void) strcpy(Outfile, Infile);
27707dabd2dSbostic (void) strcat(Outfile, ".dat");
27807dabd2dSbostic }
27907dabd2dSbostic }
280ee31de86Sbostic
usage()281ee31de86Sbostic usage()
282ee31de86Sbostic {
283ee31de86Sbostic (void) fprintf(stderr,
2844b09ca94Sbostic "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
285ee31de86Sbostic exit(1);
28607dabd2dSbostic }
28707dabd2dSbostic
28807dabd2dSbostic /*
289922b6d83Sbostic * add_offset:
290922b6d83Sbostic * Add an offset to the list, or write it out, as appropriate.
291922b6d83Sbostic */
add_offset(fp,off)292922b6d83Sbostic add_offset(fp, off)
293922b6d83Sbostic FILE *fp;
294922b6d83Sbostic off_t off;
295922b6d83Sbostic {
29675ec9249Sbostic off_t net;
29775ec9249Sbostic
29875ec9249Sbostic if (!STORING_PTRS) {
29975ec9249Sbostic net = htonl(off);
30075ec9249Sbostic fwrite(&net, 1, sizeof net, fp);
30175ec9249Sbostic } else {
302922b6d83Sbostic ALLOC(Seekpts, Num_pts + 1);
303922b6d83Sbostic Seekpts[Num_pts] = off;
304922b6d83Sbostic }
305922b6d83Sbostic Num_pts++;
306922b6d83Sbostic }
307922b6d83Sbostic
308922b6d83Sbostic /*
30907dabd2dSbostic * do_order:
31007dabd2dSbostic * Order the strings alphabetically (possibly ignoring case).
31107dabd2dSbostic */
do_order()312922b6d83Sbostic do_order()
31307dabd2dSbostic {
31407dabd2dSbostic register int i;
315922b6d83Sbostic register off_t *lp;
31607dabd2dSbostic register STR *fp;
31707dabd2dSbostic extern int cmp_str();
31807dabd2dSbostic
319922b6d83Sbostic Sort_1 = fopen(Infile, "r");
320922b6d83Sbostic Sort_2 = fopen(Infile, "r");
321922b6d83Sbostic qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
32207dabd2dSbostic i = Tbl.str_numstr;
323922b6d83Sbostic lp = Seekpts;
32407dabd2dSbostic fp = Firstch;
32507dabd2dSbostic while (i--)
32607dabd2dSbostic *lp++ = fp++->pos;
32707dabd2dSbostic (void) fclose(Sort_1);
32807dabd2dSbostic (void) fclose(Sort_2);
32907dabd2dSbostic Tbl.str_flags |= STR_ORDERED;
33007dabd2dSbostic }
33107dabd2dSbostic
33207dabd2dSbostic /*
33307dabd2dSbostic * cmp_str:
33407dabd2dSbostic * Compare two strings in the file
33507dabd2dSbostic */
336922b6d83Sbostic char *
unctrl(c)337922b6d83Sbostic unctrl(c)
338922b6d83Sbostic char c;
339922b6d83Sbostic {
340922b6d83Sbostic static char buf[3];
341922b6d83Sbostic
342922b6d83Sbostic if (isprint(c)) {
343922b6d83Sbostic buf[0] = c;
344922b6d83Sbostic buf[1] = '\0';
345922b6d83Sbostic }
346922b6d83Sbostic else if (c == 0177) {
347922b6d83Sbostic buf[0] = '^';
348922b6d83Sbostic buf[1] = '?';
349922b6d83Sbostic }
350922b6d83Sbostic else {
351922b6d83Sbostic buf[0] = '^';
352922b6d83Sbostic buf[1] = c + 'A' - 1;
353922b6d83Sbostic }
354922b6d83Sbostic return buf;
355922b6d83Sbostic }
356922b6d83Sbostic
cmp_str(p1,p2)35707dabd2dSbostic cmp_str(p1, p2)
35807dabd2dSbostic STR *p1, *p2;
35907dabd2dSbostic {
36007dabd2dSbostic register int c1, c2;
361922b6d83Sbostic register int n1, n2;
362922b6d83Sbostic
363922b6d83Sbostic # define SET_N(nf,ch) (nf = (ch == '\n'))
364922b6d83Sbostic # define IS_END(ch,nf) (ch == Delimch && nf)
36507dabd2dSbostic
36607dabd2dSbostic c1 = p1->first;
36707dabd2dSbostic c2 = p2->first;
36807dabd2dSbostic if (c1 != c2)
36907dabd2dSbostic return c1 - c2;
37007dabd2dSbostic
37107dabd2dSbostic (void) fseek(Sort_1, p1->pos, 0);
37207dabd2dSbostic (void) fseek(Sort_2, p2->pos, 0);
37307dabd2dSbostic
374922b6d83Sbostic n1 = FALSE;
375922b6d83Sbostic n2 = FALSE;
37607dabd2dSbostic while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0')
377922b6d83Sbostic SET_N(n1, c1);
37807dabd2dSbostic while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0')
379922b6d83Sbostic SET_N(n2, c2);
38007dabd2dSbostic
381922b6d83Sbostic while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
38207dabd2dSbostic if (Iflag) {
38307dabd2dSbostic if (isupper(c1))
38407dabd2dSbostic c1 = tolower(c1);
38507dabd2dSbostic if (isupper(c2))
38607dabd2dSbostic c2 = tolower(c2);
38707dabd2dSbostic }
38807dabd2dSbostic if (c1 != c2)
38907dabd2dSbostic return c1 - c2;
390922b6d83Sbostic SET_N(n1, c1);
391922b6d83Sbostic SET_N(n2, c2);
39207dabd2dSbostic c1 = getc(Sort_1);
39307dabd2dSbostic c2 = getc(Sort_2);
39407dabd2dSbostic }
395922b6d83Sbostic if (IS_END(c1, n1))
396922b6d83Sbostic c1 = 0;
397922b6d83Sbostic if (IS_END(c2, n2))
398922b6d83Sbostic c2 = 0;
39907dabd2dSbostic return c1 - c2;
40007dabd2dSbostic }
40107dabd2dSbostic
40207dabd2dSbostic /*
40307dabd2dSbostic * randomize:
40407dabd2dSbostic * Randomize the order of the string table. We must be careful
40507dabd2dSbostic * not to randomize across delimiter boundaries. All
40607dabd2dSbostic * randomization is done within each block.
40707dabd2dSbostic */
randomize()408922b6d83Sbostic randomize()
40907dabd2dSbostic {
410922b6d83Sbostic register int cnt, i;
411922b6d83Sbostic register off_t tmp;
412922b6d83Sbostic register off_t *sp;
413922b6d83Sbostic extern time_t time();
41407dabd2dSbostic
415dc985337Sbostic srandom((int)(time((time_t *) NULL) + getpid()));
416dc985337Sbostic
41707dabd2dSbostic Tbl.str_flags |= STR_RANDOM;
41807dabd2dSbostic cnt = Tbl.str_numstr;
41907dabd2dSbostic
42007dabd2dSbostic /*
42107dabd2dSbostic * move things around randomly
42207dabd2dSbostic */
42307dabd2dSbostic
424922b6d83Sbostic for (sp = Seekpts; cnt > 0; cnt--, sp++) {
425d7c3b200Sbostic i = random() % cnt;
426922b6d83Sbostic tmp = sp[0];
427922b6d83Sbostic sp[0] = sp[i];
428922b6d83Sbostic sp[i] = tmp;
42907dabd2dSbostic }
43007dabd2dSbostic }
431