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