Home | History | Annotate | Line # | Download | only in strfile
strfile.c revision 1.2
      1 /*-
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Ken Arnold.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     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.
     23  *
     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
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 char copyright[] =
     39 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     40  All rights reserved.\n";
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 /*static char sccsid[] = "from: @(#)strfile.c	5.12 (Berkeley) 4/8/91";*/
     45 static char rcsid[] = "$Id: strfile.c,v 1.2 1993/08/01 18:54:43 mycroft Exp $";
     46 #endif /* not lint */
     47 
     48 # include	<machine/endian.h>
     49 # include	<sys/param.h>
     50 # include	<stdio.h>
     51 # include	<ctype.h>
     52 # include	"strfile.h"
     53 
     54 # ifndef MAXPATHLEN
     55 # define	MAXPATHLEN	1024
     56 # endif	/* MAXPATHLEN */
     57 
     58 /*
     59  *	This program takes a file composed of strings seperated by
     60  * lines starting with two consecutive delimiting character (default
     61  * character is '%') and creates another file which consists of a table
     62  * describing the file (structure from "strfile.h"), a table of seek
     63  * pointers to the start of the strings, and the strings, each terminated
     64  * by a null byte.  Usage:
     65  *
     66  *	% strfile [-iorsx] [ -cC ] sourcefile [ datafile ]
     67  *
     68  *	c - Change delimiting character from '%' to 'C'
     69  *	s - Silent.  Give no summary of data processed at the end of
     70  *	    the run.
     71  *	o - order the strings in alphabetic order
     72  *	i - if ordering, ignore case
     73  *	r - randomize the order of the strings
     74  *	x - set rotated bit
     75  *
     76  *		Ken Arnold	Sept. 7, 1978 --
     77  *
     78  *	Added ordering options.
     79  */
     80 
     81 # define	TRUE	1
     82 # define	FALSE	0
     83 
     84 # define	STORING_PTRS	(Oflag || Rflag)
     85 # define	CHUNKSIZE	512
     86 
     87 #ifdef lint
     88 # define	ALWAYS	atoi("1")
     89 #else
     90 # define	ALWAYS	1
     91 #endif
     92 # define	ALLOC(ptr,sz)	if (ALWAYS) { \
     93 			if (ptr == NULL) \
     94 				ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \
     95 			else if (((sz) + 1) % CHUNKSIZE == 0) \
     96 				ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \
     97 			if (ptr == NULL) { \
     98 				fprintf(stderr, "out of space\n"); \
     99 				exit(1); \
    100 			} \
    101 		} else
    102 
    103 #ifdef NO_VOID
    104 # define	void	char
    105 #endif
    106 
    107 typedef struct {
    108 	char	first;
    109 	off_t	pos;
    110 } STR;
    111 
    112 char	*Infile		= NULL,		/* input file name */
    113 	Outfile[MAXPATHLEN] = "",	/* output file name */
    114 	Delimch		= '%';		/* delimiting character */
    115 
    116 int	Sflag		= FALSE;	/* silent run flag */
    117 int	Oflag		= FALSE;	/* ordering flag */
    118 int	Iflag		= FALSE;	/* ignore case flag */
    119 int	Rflag		= FALSE;	/* randomize order flag */
    120 int	Xflag		= FALSE;	/* set rotated bit */
    121 long	Num_pts		= 0;		/* number of pointers/strings */
    122 
    123 off_t	*Seekpts;
    124 
    125 FILE	*Sort_1, *Sort_2;		/* pointers for sorting */
    126 
    127 STRFILE	Tbl;				/* statistics table */
    128 
    129 STR	*Firstch;			/* first chars of each string */
    130 
    131 char	*fgets(), *strcpy(), *strcat();
    132 
    133 void	*malloc(), *realloc();
    134 
    135 /*
    136  * main:
    137  *	Drive the sucker.  There are two main modes -- either we store
    138  *	the seek pointers, if the table is to be sorted or randomized,
    139  *	or we write the pointer directly to the file, if we are to stay
    140  *	in file order.  If the former, we allocate and re-allocate in
    141  *	CHUNKSIZE blocks; if the latter, we just write each pointer,
    142  *	and then seek back to the beginning to write in the table.
    143  */
    144 main(ac, av)
    145 int	ac;
    146 char	**av;
    147 {
    148 	register char		*sp, dc;
    149 	register FILE		*inf, *outf;
    150 	register off_t		last_off, length, pos, *p;
    151 	register int		first, cnt;
    152 	register char		*nsp;
    153 	register STR		*fp;
    154 	static char		string[257];
    155 
    156 	getargs(ac, av);		/* evalute arguments */
    157 	dc = Delimch;
    158 	if ((inf = fopen(Infile, "r")) == NULL) {
    159 		perror(Infile);
    160 		exit(1);
    161 	}
    162 
    163 	if ((outf = fopen(Outfile, "w")) == NULL) {
    164 		perror(Outfile);
    165 		exit(1);
    166 	}
    167 	if (!STORING_PTRS)
    168 		(void) fseek(outf, sizeof Tbl, 0);
    169 
    170 	/*
    171 	 * Write the strings onto the file
    172 	 */
    173 
    174 	Tbl.str_longlen = 0;
    175 	Tbl.str_shortlen = (unsigned int) 0xffffffff;
    176 	Tbl.str_delim = dc;
    177 	Tbl.str_version = VERSION;
    178 	first = Oflag;
    179 	add_offset(outf, ftell(inf));
    180 	last_off = 0;
    181 	do {
    182 		sp = fgets(string, 256, inf);
    183 		if (sp == NULL || sp[0] == dc && sp[1] == '\n') {
    184 			pos = ftell(inf);
    185 			length = pos - last_off - (sp ? strlen(sp) : 0);
    186 			last_off = pos;
    187 			if (!length)
    188 				continue;
    189 			add_offset(outf, pos);
    190 			if (Tbl.str_longlen < length)
    191 				Tbl.str_longlen = length;
    192 			if (Tbl.str_shortlen > length)
    193 				Tbl.str_shortlen = length;
    194 			first = Oflag;
    195 		}
    196 		else if (first) {
    197 			for (nsp = sp; !isalnum(*nsp); nsp++)
    198 				continue;
    199 			ALLOC(Firstch, Num_pts);
    200 			fp = &Firstch[Num_pts - 1];
    201 			if (Iflag && isupper(*nsp))
    202 				fp->first = tolower(*nsp);
    203 			else
    204 				fp->first = *nsp;
    205 			fp->pos = Seekpts[Num_pts - 1];
    206 			first = FALSE;
    207 		}
    208 	} while (sp != NULL);
    209 
    210 	/*
    211 	 * write the tables in
    212 	 */
    213 
    214 	(void) fclose(inf);
    215 
    216 	if (Oflag)
    217 		do_order();
    218 	else if (Rflag)
    219 		randomize();
    220 
    221 	if (Xflag)
    222 		Tbl.str_flags |= STR_ROTATED;
    223 
    224 	if (!Sflag) {
    225 		printf("\"%s\" created\n", Outfile);
    226 		if (Num_pts == 2)
    227 			puts("There was 1 string");
    228 		else
    229 			printf("There were %d strings\n", Num_pts - 1);
    230 		printf("Longest string: %lu byte%s\n", Tbl.str_longlen,
    231 		       Tbl.str_longlen == 1 ? "" : "s");
    232 		printf("Shortest string: %lu byte%s\n", Tbl.str_shortlen,
    233 		       Tbl.str_shortlen == 1 ? "" : "s");
    234 	}
    235 
    236 	(void) fseek(outf, (off_t) 0, 0);
    237 	Tbl.str_version = htonl(Tbl.str_version);
    238 	Tbl.str_numstr = htonl(Num_pts - 1);
    239 	Tbl.str_longlen = htonl(Tbl.str_longlen);
    240 	Tbl.str_shortlen = htonl(Tbl.str_shortlen);
    241 	Tbl.str_flags = htonl(Tbl.str_flags);
    242 	(void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
    243 	if (STORING_PTRS) {
    244 		for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
    245 			*p = htonl(*p);
    246 		(void) fwrite((char *) Seekpts, sizeof *Seekpts, (int) Num_pts, outf);
    247 	}
    248 	(void) fclose(outf);
    249 	exit(0);
    250 }
    251 
    252 /*
    253  *	This routine evaluates arguments from the command line
    254  */
    255 getargs(argc, argv)
    256 int	argc;
    257 char	**argv;
    258 {
    259 	extern char	*optarg;
    260 	extern int	optind;
    261 	int	ch;
    262 
    263 	while ((ch = getopt(argc, argv, "c:iorsx")) != EOF)
    264 		switch(ch) {
    265 		case 'c':			/* new delimiting char */
    266 			Delimch = *optarg;
    267 			if (!isascii(Delimch)) {
    268 				printf("bad delimiting character: '\\%o\n'",
    269 				       Delimch);
    270 			}
    271 			break;
    272 		case 'i':			/* ignore case in ordering */
    273 			Iflag++;
    274 			break;
    275 		case 'o':			/* order strings */
    276 			Oflag++;
    277 			break;
    278 		case 'r':			/* randomize pointers */
    279 			Rflag++;
    280 			break;
    281 		case 's':			/* silent */
    282 			Sflag++;
    283 			break;
    284 		case 'x':			/* set the rotated bit */
    285 			Xflag++;
    286 			break;
    287 		case '?':
    288 		default:
    289 			usage();
    290 		}
    291 	argv += optind;
    292 
    293 	if (*argv) {
    294 		Infile = *argv;
    295 		if (*++argv)
    296 			(void) strcpy(Outfile, *argv);
    297 	}
    298 	if (!Infile) {
    299 		puts("No input file name");
    300 		usage();
    301 	}
    302 	if (*Outfile == '\0') {
    303 		(void) strcpy(Outfile, Infile);
    304 		(void) strcat(Outfile, ".dat");
    305 	}
    306 }
    307 
    308 usage()
    309 {
    310 	(void) fprintf(stderr,
    311 	    "strfile [-iorsx] [-c char] sourcefile [datafile]\n");
    312 	exit(1);
    313 }
    314 
    315 /*
    316  * add_offset:
    317  *	Add an offset to the list, or write it out, as appropriate.
    318  */
    319 add_offset(fp, off)
    320 FILE	*fp;
    321 off_t	off;
    322 {
    323 	off_t net;
    324 
    325 	if (!STORING_PTRS) {
    326 		net = htonl(off);
    327 		fwrite(&net, 1, sizeof net, fp);
    328 	} else {
    329 		ALLOC(Seekpts, Num_pts + 1);
    330 		Seekpts[Num_pts] = off;
    331 	}
    332 	Num_pts++;
    333 }
    334 
    335 /*
    336  * do_order:
    337  *	Order the strings alphabetically (possibly ignoring case).
    338  */
    339 do_order()
    340 {
    341 	register int	i;
    342 	register off_t	*lp;
    343 	register STR	*fp;
    344 	extern int	cmp_str();
    345 
    346 	Sort_1 = fopen(Infile, "r");
    347 	Sort_2 = fopen(Infile, "r");
    348 	qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str);
    349 	i = Tbl.str_numstr;
    350 	lp = Seekpts;
    351 	fp = Firstch;
    352 	while (i--)
    353 		*lp++ = fp++->pos;
    354 	(void) fclose(Sort_1);
    355 	(void) fclose(Sort_2);
    356 	Tbl.str_flags |= STR_ORDERED;
    357 }
    358 
    359 /*
    360  * cmp_str:
    361  *	Compare two strings in the file
    362  */
    363 char *
    364 unctrl(c)
    365 char c;
    366 {
    367 	static char	buf[3];
    368 
    369 	if (isprint(c)) {
    370 		buf[0] = c;
    371 		buf[1] = '\0';
    372 	}
    373 	else if (c == 0177) {
    374 		buf[0] = '^';
    375 		buf[1] = '?';
    376 	}
    377 	else {
    378 		buf[0] = '^';
    379 		buf[1] = c + 'A' - 1;
    380 	}
    381 	return buf;
    382 }
    383 
    384 cmp_str(p1, p2)
    385 STR	*p1, *p2;
    386 {
    387 	register int	c1, c2;
    388 	register int	n1, n2;
    389 
    390 # define	SET_N(nf,ch)	(nf = (ch == '\n'))
    391 # define	IS_END(ch,nf)	(ch == Delimch && nf)
    392 
    393 	c1 = p1->first;
    394 	c2 = p2->first;
    395 	if (c1 != c2)
    396 		return c1 - c2;
    397 
    398 	(void) fseek(Sort_1, p1->pos, 0);
    399 	(void) fseek(Sort_2, p2->pos, 0);
    400 
    401 	n1 = FALSE;
    402 	n2 = FALSE;
    403 	while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0')
    404 		SET_N(n1, c1);
    405 	while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0')
    406 		SET_N(n2, c2);
    407 
    408 	while (!IS_END(c1, n1) && !IS_END(c2, n2)) {
    409 		if (Iflag) {
    410 			if (isupper(c1))
    411 				c1 = tolower(c1);
    412 			if (isupper(c2))
    413 				c2 = tolower(c2);
    414 		}
    415 		if (c1 != c2)
    416 			return c1 - c2;
    417 		SET_N(n1, c1);
    418 		SET_N(n2, c2);
    419 		c1 = getc(Sort_1);
    420 		c2 = getc(Sort_2);
    421 	}
    422 	if (IS_END(c1, n1))
    423 		c1 = 0;
    424 	if (IS_END(c2, n2))
    425 		c2 = 0;
    426 	return c1 - c2;
    427 }
    428 
    429 /*
    430  * randomize:
    431  *	Randomize the order of the string table.  We must be careful
    432  *	not to randomize across delimiter boundaries.  All
    433  *	randomization is done within each block.
    434  */
    435 randomize()
    436 {
    437 	register int	cnt, i;
    438 	register off_t	tmp;
    439 	register off_t	*sp;
    440 	extern time_t	time();
    441 
    442 	srandom((int)(time((time_t *) NULL) + getpid()));
    443 
    444 	Tbl.str_flags |= STR_RANDOM;
    445 	cnt = Tbl.str_numstr;
    446 
    447 	/*
    448 	 * move things around randomly
    449 	 */
    450 
    451 	for (sp = Seekpts; cnt > 0; cnt--, sp++) {
    452 		i = random() % cnt;
    453 		tmp = sp[0];
    454 		sp[0] = sp[i];
    455 		sp[i] = tmp;
    456 	}
    457 }
    458