strfile.c revision 132578
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#if 0 38#ifndef lint 39static const char copyright[] = 40"@(#) Copyright (c) 1989, 1993\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42#endif /* not lint */ 43 44#ifndef lint 45static const char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93"; 46#endif /* not lint */ 47#endif 48#include <sys/cdefs.h> 49__FBSDID("$FreeBSD: head/games/fortune/strfile/strfile.c 132578 2004-07-23 11:36:35Z le $"); 50 51# include <sys/param.h> 52# include <arpa/inet.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 separated 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 - Allow comments marked by a double delimiter at line's beginning 73 * c - Change delimiting character from '%' to 'C' 74 * s - Silent. Give no summary of data processed at the end of 75 * the run. 76 * o - order the strings in alphabetic order 77 * i - if ordering, ignore case 78 * r - randomize the order of the strings 79 * x - set rotated bit 80 * 81 * Ken Arnold Sept. 7, 1978 -- 82 * 83 * Added ordering options. 84 */ 85 86# define TRUE 1 87# define FALSE 0 88 89# define STORING_PTRS (Oflag || Rflag) 90# define CHUNKSIZE 512 91 92# define ALLOC(ptr,sz) { \ 93 if (ptr == NULL) \ 94 ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \ 95 else if (((sz) + 1) % CHUNKSIZE == 0) \ 96 ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \ 97 if (ptr == NULL) { \ 98 fprintf(stderr, "out of space\n"); \ 99 exit(1); \ 100 } \ 101 } 102 103#ifdef NO_VOID 104# define void char 105#endif 106 107typedef struct { 108 int first; 109 long pos; 110} STR; 111 112static char *Infile = NULL, /* input file name */ 113 Outfile[MAXPATHLEN] = "", /* output file name */ 114 Delimch = '%'; /* delimiting character */ 115 116static int Cflag = FALSE; /* embedded comments */ 117static int Sflag = FALSE; /* silent run flag */ 118static int Oflag = FALSE; /* ordering flag */ 119static int Iflag = FALSE; /* ignore case flag */ 120static int Rflag = FALSE; /* randomize order flag */ 121static int Xflag = FALSE; /* set rotated bit */ 122static long Num_pts = 0; /* number of pointers/strings */ 123 124static long *Seekpts; 125 126static FILE *Sort_1, *Sort_2; /* pointers for sorting */ 127 128static STRFILE Tbl; /* statistics table */ 129 130static STR *Firstch; /* first chars of each string */ 131 132static void add_offset(FILE *, long); 133static int cmp_str(const void *, const void *); 134static int stable_collate_range_cmp(int, int); 135static void do_order(void); 136static void getargs(int, char **); 137static void randomize(void); 138static void usage(void); 139 140/* 141 * main: 142 * Drive the sucker. There are two main modes -- either we store 143 * the seek pointers, if the table is to be sorted or randomized, 144 * or we write the pointer directly to the file, if we are to stay 145 * in file order. If the former, we allocate and re-allocate in 146 * CHUNKSIZE blocks; if the latter, we just write each pointer, 147 * and then seek back to the beginning to write in the table. 148 */ 149int main(int ac, char *av[]) 150{ 151 char *sp, dc; 152 FILE *inf, *outf; 153 long last_off, pos, *p; 154 unsigned long length; 155 int first, cnt; 156 char *nsp; 157 STR *fp; 158 static char string[257]; 159 160 (void) setlocale(LC_ALL, ""); 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, (long) sizeof Tbl, 0); 175 176 /* 177 * Write the strings onto the file 178 */ 179 180 Tbl.str_longlen = 0; 181 Tbl.str_shortlen = ~((unsigned long) 0); 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((unsigned char)*nsp); nsp++) 204 continue; 205 ALLOC(Firstch, Num_pts); 206 fp = &Firstch[Num_pts - 1]; 207 if (Iflag && isupper((unsigned char)*nsp)) 208 fp->first = tolower((unsigned char)*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 Tbl.str_numstr = Num_pts - 1; 222 223 if (Cflag) 224 Tbl.str_flags |= STR_COMMENTS; 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 %ld strings\n", 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 rewind(outf); 247 Tbl.str_version = htonl(Tbl.str_version); 248 Tbl.str_numstr = htonl(Tbl.str_numstr); 249 Tbl.str_longlen = htonl(Tbl.str_longlen); 250 Tbl.str_shortlen = htonl(Tbl.str_shortlen); 251 Tbl.str_flags = htonl(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 *p = htonl(*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 */ 265void getargs(argc, argv) 266int argc; 267char **argv; 268{ 269 int ch; 270 271 while ((ch = getopt(argc, argv, "Cc:iorsx")) != EOF) 272 switch(ch) { 273 case 'C': /* embedded comments */ 274 Cflag++; 275 break; 276 case 'c': /* new delimiting char */ 277 Delimch = *optarg; 278 if (!isascii(Delimch)) { 279 printf("bad delimiting character: '\\%o\n'", 280 (unsigned char)Delimch); 281 } 282 break; 283 case 'i': /* ignore case in ordering */ 284 Iflag++; 285 break; 286 case 'o': /* order strings */ 287 Oflag++; 288 break; 289 case 'r': /* randomize pointers */ 290 Rflag++; 291 break; 292 case 's': /* silent */ 293 Sflag++; 294 break; 295 case 'x': /* set the rotated bit */ 296 Xflag++; 297 break; 298 case '?': 299 default: 300 usage(); 301 } 302 argv += optind; 303 304 if (*argv) { 305 Infile = *argv; 306 if (*++argv) 307 (void) strcpy(Outfile, *argv); 308 } 309 if (!Infile) { 310 puts("No input file name"); 311 usage(); 312 } 313 if (*Outfile == '\0') { 314 (void) strcpy(Outfile, Infile); 315 (void) strcat(Outfile, ".dat"); 316 } 317} 318 319void usage() 320{ 321 (void) fprintf(stderr, 322 "strfile [-Ciorsx] [-c char] sourcefile [datafile]\n"); 323 exit(1); 324} 325 326/* 327 * add_offset: 328 * Add an offset to the list, or write it out, as appropriate. 329 */ 330void add_offset(fp, off) 331FILE *fp; 332long off; 333{ 334 long net; 335 336 if (!STORING_PTRS) { 337 net = htonl(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 */ 350void do_order() 351{ 352 int i; 353 long *lp; 354 STR *fp; 355 356 Sort_1 = fopen(Infile, "r"); 357 Sort_2 = fopen(Infile, "r"); 358 qsort((char *) Firstch, (int) Tbl.str_numstr, sizeof *Firstch, cmp_str); 359 i = Tbl.str_numstr; 360 lp = Seekpts; 361 fp = Firstch; 362 while (i--) 363 *lp++ = fp++->pos; 364 (void) fclose(Sort_1); 365 (void) fclose(Sort_2); 366 Tbl.str_flags |= STR_ORDERED; 367} 368 369static int stable_collate_range_cmp(c1, c2) 370 int c1, c2; 371{ 372 static char s1[2], s2[2]; 373 int ret; 374 375 s1[0] = c1; 376 s2[0] = c2; 377 if ((ret = strcoll(s1, s2)) != 0) 378 return (ret); 379 return (c1 - c2); 380} 381 382/* 383 * cmp_str: 384 * Compare two strings in the file 385 */ 386int cmp_str(s1, s2) 387const void *s1, *s2; 388{ 389 const STR *p1, *p2; 390 int c1, c2; 391 int n1, n2; 392 int r; 393 394# define SET_N(nf,ch) (nf = (ch == '\n')) 395# define IS_END(ch,nf) (ch == EOF || (ch == (unsigned char) Delimch && nf)) 396 397 p1 = (const STR *) s1; 398 p2 = (const STR *) s2; 399 400 c1 = (unsigned char) p1->first; 401 c2 = (unsigned char) p2->first; 402 if ((r = stable_collate_range_cmp(c1, c2)) != 0) 403 return (r); 404 405 (void) fseek(Sort_1, p1->pos, 0); 406 (void) fseek(Sort_2, p2->pos, 0); 407 408 n1 = FALSE; 409 n2 = FALSE; 410 while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0' && c1 != EOF) 411 SET_N(n1, c1); 412 while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0' && c2 != EOF) 413 SET_N(n2, c2); 414 415 while (!IS_END(c1, n1) && !IS_END(c2, n2)) { 416 if (Iflag) { 417 if (isupper(c1)) 418 c1 = tolower(c1); 419 if (isupper(c2)) 420 c2 = tolower(c2); 421 } 422 if ((r = stable_collate_range_cmp(c1, c2)) != 0) 423 return (r); 424 SET_N(n1, c1); 425 SET_N(n2, c2); 426 c1 = getc(Sort_1); 427 c2 = getc(Sort_2); 428 } 429 if (IS_END(c1, n1)) 430 c1 = 0; 431 if (IS_END(c2, n2)) 432 c2 = 0; 433 return (stable_collate_range_cmp(c1, c2)); 434} 435 436/* 437 * randomize: 438 * Randomize the order of the string table. We must be careful 439 * not to randomize across delimiter boundaries. All 440 * randomization is done within each block. 441 */ 442void randomize() 443{ 444 int cnt, i; 445 long tmp; 446 long *sp; 447 448 srandomdev(); 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