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