2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/localization/fhash.cpp $
17 * Revision 1.3 2002/06/09 04:41:22 relnev
18 * added copyright header
20 * Revision 1.2 2002/05/07 03:16:46 theoddone33
21 * The Great Newline Fix
23 * Revision 1.1.1.1 2002/05/03 03:28:09 root
27 * 5 2/23/99 9:25a Dave
28 * Stubbed out a bunch of stuff to get cfile and lcl functions in.
30 * 4 12/01/98 4:46p Dave
31 * Put in targa bitmap support (16 bit).
42 // -----------------------------------------------------------------------------------------------
47 typedef struct fhash_node {
48 char *str; // allocated dynamically
50 fhash_node *next, *prev; // for chaining in an individual hash table entry (non-circular, doubly linked list)
53 // hash table itself (with chained nodes)
54 #define HASH_TABLE_SIZE 253 // works better when not a power of 2, and is prime
55 fhash_node *Hash_table_fred[HASH_TABLE_SIZE];
57 // if the hash table is active
61 // -----------------------------------------------------------------------------------------------
62 // HASH FORWARD DECLARATIONS
65 // hash a string. return an index into the hash table where it should be inserted
66 int fhash_get_hash_index(char *str);
68 // insert a string into hash table index N, will take care of allocating/chaining everything
69 void fhash_insert(char *str, int id, int n);
72 // -----------------------------------------------------------------------------------------------
76 // initialize the hash table
79 memset(Hash_table_fred, 0, sizeof(fhash_node) * HASH_TABLE_SIZE);
82 // set the hash table to be active for parsing
88 // set the hash table to be inactive for parsing
89 void fhash_deactivate()
94 // if the hash table is active
100 // flush out the hash table, freeing up everything
104 fhash_node *moveup, *backup;
106 // go through each element
107 for(idx=0; idx<HASH_TABLE_SIZE; idx++){
108 if(Hash_table_fred[idx] != NULL){
109 moveup = Hash_table_fred[idx];
110 while(moveup != NULL){
113 moveup = moveup->next;
115 // free up this element
116 if(backup->str != NULL){
123 Hash_table_fred[idx] = NULL;
128 // add a string with the given id# to the has table
129 void fhash_add_str(char *str, int id)
133 // if the hash table isn't active, don't bother
134 Assert(Fhash_active);
139 // determine where the string goes in the has table
144 hash_index = fhash_get_hash_index(str);
146 // insert into the hash table
147 fhash_insert(str, id, hash_index);
150 // determine if the passed string exists in the table
151 // returns : -2 if the string doesn't exit, or >= -1 as the string id # otherwise
152 int fhash_string_exists(char *str)
162 // get the hash index for this string
163 hash_index = fhash_get_hash_index(str);
165 // if there are no entries, it doesn't exist
166 if(Hash_table_fred[hash_index] == NULL){
170 // otherwise compare against all strings in this code
171 moveup = Hash_table_fred[hash_index];
172 while(moveup != NULL){
173 // do a string compare on this item
174 Assert(moveup->str != NULL);
175 if(moveup->str != NULL){
176 if(!strcmp(moveup->str, str)){
182 moveup = moveup->next;
190 // -----------------------------------------------------------------------------------------------
191 // HASH FORWARD DEFINITIONS
194 // hash a string. return an index into the hash table where it should be inserted
195 int fhash_get_hash_index(char *str)
202 str_len = strlen(str);
203 for(idx=0; idx<str_len; idx++){
207 ret = abs(accum) % 253;
211 // insert a string into hash table index N, will take care of allocating/chaining everything
212 void fhash_insert(char *str, int id, int n)
214 fhash_node *new_node;
217 // allocate the new node
218 new_node = (fhash_node*)malloc(sizeof(fhash_node));
220 if(new_node == NULL){
225 new_node->str = strdup(str);
227 new_node->next = NULL;
228 new_node->prev = NULL;
230 // if this hash index is NULL, just assign it
231 if(Hash_table_fred[n] == NULL){
232 Hash_table_fred[n] = new_node;
234 moveup = Hash_table_fred[n];
235 while(moveup->next != NULL){
236 moveup = moveup->next;
238 new_node->prev = moveup;
239 moveup->next = new_node;