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