diff options
author | Simon Howard | 2005-10-03 21:39:39 +0000 |
---|---|---|
committer | Simon Howard | 2005-10-03 21:39:39 +0000 |
commit | 4dfffeb54db996c6ea0e52e1707633b80a581f2e (patch) | |
tree | c6ca8b5e27aa17ffc864b4139302719046af5d7d /src/deh_text.c | |
parent | c41381ead5f96f61797e96ba302f4a09d9922a88 (diff) | |
download | chocolate-doom-4dfffeb54db996c6ea0e52e1707633b80a581f2e.tar.gz chocolate-doom-4dfffeb54db996c6ea0e52e1707633b80a581f2e.tar.bz2 chocolate-doom-4dfffeb54db996c6ea0e52e1707633b80a581f2e.zip |
Dehacked text substitutions
Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 160
Diffstat (limited to 'src/deh_text.c')
-rw-r--r-- | src/deh_text.c | 170 |
1 files changed, 168 insertions, 2 deletions
diff --git a/src/deh_text.c b/src/deh_text.c index 45b1d839..e2c86662 100644 --- a/src/deh_text.c +++ b/src/deh_text.c @@ -1,7 +1,7 @@ // Emacs style mode select -*- C++ -*- //----------------------------------------------------------------------------- // -// $Id: deh_text.c 157 2005-10-03 11:08:16Z fraggle $ +// $Id: deh_text.c 160 2005-10-03 21:39:39Z fraggle $ // // Copyright(C) 2005 Simon Howard // @@ -21,6 +21,9 @@ // 02111-1307, USA. // // $Log$ +// Revision 1.3 2005/10/03 21:39:39 fraggle +// Dehacked text substitutions +// // Revision 1.2 2005/10/03 11:08:16 fraggle // Replace end of section functions with NULLs as they arent currently being // used for anything. @@ -37,21 +40,184 @@ #include "doomdef.h" #include "doomtype.h" + +#include "z_zone.h" + #include "deh_defs.h" +#include "deh_io.h" + +typedef struct +{ + char *from_text; + char *to_text; +} deh_substitution_t; + +static deh_substitution_t **hash_table; +static int hash_table_entries; +static int hash_table_length; + +// This is the algorithm used by glib + +static unsigned int strhash(char *s) +{ + char *p = s; + unsigned int h = *p; + + if (h) + { + for (p += 1; *p; p++) + h = (h << 5) - h + *p; + } + + return h; +} + +// Look up a string to see if it has been replaced with something else +// This will be used throughout the program to substitute text + +char *DEH_String(char *s) +{ + int entry; + + entry = strhash(s) % hash_table_length; + + while (hash_table[entry] != NULL) + { + if (!strcmp(hash_table[entry]->from_text, s)) + { + // substitution found! + + return hash_table[entry]->to_text; + } + + entry = (entry + 1) % hash_table_length; + } + + // no substitution found + + return s; +} + +static void DEH_AddToHashtable(deh_substitution_t *sub); + +static void IncreaseHashtable(void) +{ + deh_substitution_t **old_table; + int old_table_length; + int i; + + // save the old table + + old_table = hash_table; + old_table_length = hash_table_length; + + // double the size + + hash_table_length *= 2; + hash_table = Z_Malloc(sizeof(deh_substitution_t *) * hash_table_length, + PU_STATIC, NULL); + memset(hash_table, 0, sizeof(deh_substitution_t *) * hash_table_length); + + // go through the old table and insert all the old entries + + for (i=0; i<old_table_length; ++i) + { + if (old_table[i] != NULL) + { + DEH_AddToHashtable(old_table[i]); + } + } + + // free the old table + + Z_Free(old_table); +} + +static void DEH_AddToHashtable(deh_substitution_t *sub) +{ + int entry; + + // if the hash table is more than 60% full, increase its size + + if ((hash_table_entries * 10) / hash_table_length > 6) + { + IncreaseHashtable(); + } + + // find where to insert it + + entry = strhash(sub->from_text) % hash_table_length; + + while (hash_table[entry] != NULL) + { + entry = (entry + 1) % hash_table_length; + } + + hash_table[entry] = sub; + ++hash_table_entries; +} + +static void DEH_TextInit(void) +{ + // init hash table + + hash_table_entries = 0; + hash_table_length = 16; + hash_table = Z_Malloc(sizeof(deh_substitution_t *) * hash_table_length, + PU_STATIC, NULL); + memset(hash_table, 0, sizeof(deh_substitution_t *) * hash_table_length); +} static void *DEH_TextStart(deh_context_t *context, char *line) { + deh_substitution_t *sub; + int fromlen, tolen; + int i; + + sscanf(line, "Text %i %i", &fromlen, &tolen); + + sub = Z_Malloc(sizeof(deh_substitution_t), PU_STATIC, NULL); + sub->from_text = Z_Malloc(fromlen + 1, PU_STATIC, NULL); + sub->to_text = Z_Malloc(tolen + 1, PU_STATIC, NULL); + + // read in the "from" text + + for (i=0; i<fromlen; ++i) + { + int c; + + c = DEH_GetChar(context); + + sub->from_text[i] = c; + } + sub->from_text[fromlen] = '\0'; + + // read in the "to" text + + for (i=0; i<tolen; ++i) + { + int c; + + c = DEH_GetChar(context); + + sub->to_text[i] = c; + } + sub->to_text[tolen] = '\0'; + + DEH_AddToHashtable(sub); + return NULL; } static void DEH_TextParseLine(deh_context_t *context, char *line, void *tag) { + // not used } deh_section_t deh_section_text = { "Text", - NULL, + DEH_TextInit, DEH_TextStart, DEH_TextParseLine, NULL, |