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