1 /* -----------------------------------------------------------------------------
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.
9 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
11 * Copyright (C) 2000. The University of Chicago.
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.
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.
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
27 * See the file COPYING for a complete copy of the LGPL.
28 * ----------------------------------------------------------------------------- */
32 static char cvs[] = "$Id: string.c 10001 2007-10-17 21:33:57Z wsfulton $";
34 /* Hash table containing stab strings and such */
35 typedef struct stringtype {
37 struct stringtype *next;
40 #define STRING_HASH_SIZE 1013
42 static stringtype *strings[STRING_HASH_SIZE];
43 static int strings_init = 0;
45 static int shash(char *name) {
50 for (i = 0; (i < 16) && (*c); i++, c++) {
51 h = ((h^~i) << 6) + *c;
53 return h % STRING_HASH_SIZE;
57 wad_string_lookup(char *s) {
63 for (i = 0; i < STRING_HASH_SIZE; i++) {
72 if (strcmp(st->str,s) == 0) return st->str;
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];
84 void wad_string_debug() {
85 if (wad_debug_mode & DEBUG_STRING) {
91 for (i = 0; i < STRING_HASH_SIZE; i++) {
97 stringlen += strlen(s->str);
100 /* wad_printf("WAD: stringhash[%d] = %d\n", i, c);*/
101 if (c > maxdepth) maxdepth = c;
104 wad_printf("WAD: nstrings = %d (%d bytes)\n", total, stringlen + total*sizeof(stringtype));
105 wad_printf("WAD: maxdepth = %d\n", maxdepth);
109 /* Our own string copy */
110 char *wad_strcpy(char *t, const char *s) {
112 for (; *s; s++, t++) *t = *s;
118 wad_strcat(char *t, const char *s) {
120 return wad_strcpy(t,s);
124 wad_strlen(const char *s) {
126 while (*(s++)) count++;