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