/tags/rel-1-3-26/SWIG/Tools/WAD/Wad/string.c
C | 131 lines | 81 code | 18 blank | 32 comment | 15 complexity | 29d93c0e3521ddf5b788e7e5e784661d MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
1/* ----------------------------------------------------------------------------- 2 * string.c 3 * 4 * This file provides support for string storage in WAD. Since strings are 5 * used frequently in WAD, this file implements string interning and 6 * some lookup functions that can be used to return a previously stored 7 * string rather than making a new copy. 8 * 9 * Author(s) : David Beazley (beazley@cs.uchicago.edu) 10 * 11 * Copyright (C) 2000. The University of Chicago. 12 * 13 * This library is free software; you can redistribute it and/or 14 * modify it under the terms of the GNU Lesser General Public 15 * License as published by the Free Software Foundation; either 16 * version 2.1 of the License, or (at your option) any later version. 17 * 18 * This library is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 * Lesser General Public License for more details. 22 * 23 * You should have received a copy of the GNU Lesser General Public 24 * License along with this library; if not, write to the Free Software 25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 26 * 27 * See the file COPYING for a complete copy of the LGPL. 28 * ----------------------------------------------------------------------------- */ 29 30#include "wad.h" 31 32static char cvs[] = "$Header$"; 33 34/* Hash table containing stab strings and such */ 35typedef struct stringtype { 36 char *str; 37 struct stringtype *next; 38} stringtype; 39 40#define STRING_HASH_SIZE 1013 41 42static stringtype *strings[STRING_HASH_SIZE]; 43static int strings_init = 0; 44 45static int shash(char *name) { 46 unsigned int h = 0; 47 char *c; 48 int i; 49 c = name; 50 for (i = 0; (i < 16) && (*c); i++, c++) { 51 h = ((h^~i) << 6) + *c; 52 } 53 return h % STRING_HASH_SIZE; 54} 55 56char * 57wad_string_lookup(char *s) { 58 int h; 59 int i; 60 stringtype *st; 61 62 if (!strings_init) { 63 for (i = 0; i < STRING_HASH_SIZE; i++) { 64 strings[i] = 0; 65 } 66 strings_init = 1; 67 } 68 69 h = shash(s); 70 st = strings[h]; 71 while (st) { 72 if (strcmp(st->str,s) == 0) return st->str; 73 st = st->next; 74 } 75 76 /* Not found. Add the string to the hash table */ 77 st = (stringtype *) wad_malloc(sizeof(stringtype)); 78 st->str = wad_strdup(s); 79 st->next = strings[h]; 80 strings[h] = st; 81 return st->str; 82} 83 84void wad_string_debug() { 85 if (wad_debug_mode & DEBUG_STRING) { 86 int maxdepth = 0; 87 int total = 0; 88 int stringlen = 0; 89 int i; 90 91 for (i = 0; i < STRING_HASH_SIZE; i++) { 92 stringtype *s; 93 int c = 0; 94 s = strings[i]; 95 while (s) { 96 c++; 97 stringlen += strlen(s->str); 98 s = s->next; 99 } 100 /* wad_printf("WAD: stringhash[%d] = %d\n", i, c);*/ 101 if (c > maxdepth) maxdepth = c; 102 total += c; 103 } 104 wad_printf("WAD: nstrings = %d (%d bytes)\n", total, stringlen + total*sizeof(stringtype)); 105 wad_printf("WAD: maxdepth = %d\n", maxdepth); 106 } 107} 108 109/* Our own string copy */ 110char *wad_strcpy(char *t, const char *s) { 111 if (s) 112 for (; *s; s++, t++) *t = *s; 113 *t = 0; 114 return t; 115} 116 117char * 118wad_strcat(char *t, const char *s) { 119 while (*t) t++; 120 return wad_strcpy(t,s); 121} 122 123int 124wad_strlen(const char *s) { 125 int count = 0; 126 while (*(s++)) count++; 127 return count; 128} 129 130 131