PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/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. #include "wad.h"
  30. static char cvs[] = "$Header$";
  31. /* Hash table containing stab strings and such */
  32. typedef struct stringtype {
  33. char *str;
  34. struct stringtype *next;
  35. } stringtype;
  36. #define STRING_HASH_SIZE 1013
  37. static stringtype *strings[STRING_HASH_SIZE];
  38. static int strings_init = 0;
  39. static int shash(char *name) {
  40. unsigned int h = 0;
  41. char *c;
  42. int i;
  43. c = name;
  44. for (i = 0; (i < 16) && (*c); i++, c++) {
  45. h = ((h^~i) << 6) + *c;
  46. }
  47. return h % STRING_HASH_SIZE;
  48. }
  49. char *
  50. wad_string_lookup(char *s) {
  51. int h;
  52. int i;
  53. stringtype *st;
  54. if (!strings_init) {
  55. for (i = 0; i < STRING_HASH_SIZE; i++) {
  56. strings[i] = 0;
  57. }
  58. strings_init = 1;
  59. }
  60. h = shash(s);
  61. st = strings[h];
  62. while (st) {
  63. if (strcmp(st->str,s) == 0) return st->str;
  64. st = st->next;
  65. }
  66. /* Not found. Add the string to the hash table */
  67. st = (stringtype *) wad_malloc(sizeof(stringtype));
  68. st->str = wad_strdup(s);
  69. st->next = strings[h];
  70. strings[h] = st;
  71. return st->str;
  72. }
  73. void wad_string_debug() {
  74. if (wad_debug_mode & DEBUG_STRING) {
  75. int maxdepth = 0;
  76. int total = 0;
  77. int stringlen = 0;
  78. int i;
  79. for (i = 0; i < STRING_HASH_SIZE; i++) {
  80. stringtype *s;
  81. int c = 0;
  82. s = strings[i];
  83. while (s) {
  84. c++;
  85. stringlen += strlen(s->str);
  86. s = s->next;
  87. }
  88. /* wad_printf("WAD: stringhash[%d] = %d\n", i, c);*/
  89. if (c > maxdepth) maxdepth = c;
  90. total += c;
  91. }
  92. wad_printf("WAD: nstrings = %d (%d bytes)\n", total, stringlen + total*sizeof(stringtype));
  93. wad_printf("WAD: maxdepth = %d\n", maxdepth);
  94. }
  95. }
  96. /* Our own string copy */
  97. char *wad_strcpy(char *t, const char *s) {
  98. if (s)
  99. for (; *s; s++, t++) *t = *s;
  100. *t = 0;
  101. return t;
  102. }
  103. char *
  104. wad_strcat(char *t, const char *s) {
  105. while (*t) t++;
  106. return wad_strcpy(t,s);
  107. }
  108. int
  109. wad_strlen(const char *s) {
  110. int count = 0;
  111. while (*(s++)) count++;
  112. return count;
  113. }