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