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