diff options
author | Lucien Van Elsen <lwvanels@mit.edu> | 1991-11-17 12:19:12 +0000 |
---|---|---|
committer | Lucien Van Elsen <lwvanels@mit.edu> | 1991-11-17 12:19:12 +0000 |
commit | fe7095d1dc0a6a79810cc2e81df3fa70370385be (patch) | |
tree | 245e0ff2c149d9853fab1ca8a18a2db82d61693a | |
parent | 7c93e5502f46bf27d0f87f5b9dbcb6eb7f7c8f1b (diff) |
Initial revision
-rw-r--r-- | server/zstring.c | 144 | ||||
-rw-r--r-- | server/zstring.h | 41 |
2 files changed, 185 insertions, 0 deletions
diff --git a/server/zstring.c b/server/zstring.c new file mode 100644 index 0000000..575adc9 --- /dev/null +++ b/server/zstring.c @@ -0,0 +1,144 @@ +/* + * Copyright (C) 1991 by the Massachusetts Institute of Technology. + * For copying and distribution information, see the file "mit-copyright.h". + * + * $Source$ + * $Id$ + * $Author$ + */ + +#include <mit-copyright.h> + +#include <ctype.h> +#ifdef __STDC__ +#include <stdlib.h> +#endif +#include <string.h> +#include "zstring.h" + +static ZSTRING *zhash[ZSTRING_HASH_TABLE_SIZE]; + +#ifdef __STDC__ +# define P(s) s +#else +# define P(s) () +#endif + +extern unsigned long hash P((const char *s)); +extern char *strsave P((const char *s)); +#undef P + +ZSTRING * +make_zstring(s, downcase) + char *s; + int downcase; +{ + char *new_s,*p; + ZSTRING *new_z,*hp; + int hash_val; + + if (downcase) { + new_s = strsave(s); + p = new_s; + while(*p) { + if (isascii(*p) && isupper(*p)) + *p = tolower(*p); + p++; + } + } else { + new_s = s; + } + + new_z = find_zstring(new_s,0); + if (new_z != NULL) { + if (downcase) + free(new_s); + new_z->ref_count++; + return(new_z); + } + + /* Initialize new ZSTRING */ + + if (!downcase) + new_s = strsave(s); + new_z = malloc(sizeof(ZSTRING)); + new_z->string = new_s; + new_z->len = strlen(new_s); + new_z->ref_count = 1; + + /* Add to beginning of hash table */ + hash_val = hash(new_s) % ZSTRING_HASH_TABLE_SIZE; + hp = zhash[hash_val]; + new_z->next = hp; + if (hp != NULL) + hp->prev = new_z; + new_z->prev = NULL; + zhash[hash_val] = new_z; + + return(new_z); +} + +void +free_zstring(z) + ZSTRING *z; +{ + ZSTRING *hp; + int hash_val; + + z->ref_count--; + if (z->ref_count > 0) + return; + + /* delete zstring completely */ + if(z->prev == NULL) + zhash[hash(z->string) % ZSTRING_HASH_TABLE_SIZE] = z->next; + else + z->prev->next = z->next; + + if (z->next != NULL) + z->next->prev = z->prev; + + free(z->string); + free(z); + return; +} + +ZSTRING * +find_zstring(s,downcase) + char *s; + int downcase; +{ + char *new_s,*p; + ZSTRING *z; + + if (downcase) { + new_s = strsave(s); + p = new_s; + while (*p) { + if (isascii(*p) && isupper(*p)) + *p = tolower(*p); + p++; + } + } else { + new_s = s; + } + + z = zhash[hash(new_s) % ZSTRING_HASH_TABLE_SIZE]; + while (z != (ZSTRING *)NULL) { + if (strcmp(new_s,z->string) == 0) + break; + z = z->next; + } + + if (downcase) + free(new_s); + + return(z); +} + +int +eq_zstring(a,b) + ZSTRING *a, *b; +{ + return(a == b); +} diff --git a/server/zstring.h b/server/zstring.h new file mode 100644 index 0000000..7d6fe31 --- /dev/null +++ b/server/zstring.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 1991 by the Massachusetts Institute of Technology. + * For copying and distribution information, see the file "mit-copyright.h". + * + * $Source$ + * $Id$ + * $Author$ + */ + +#include <mit-copyright.h> + +#ifndef __zstring_h +#define __zstring_h __FILE__ + +#define ZSTRING_HASH_TABLE_SIZE 1031 + +typedef struct t_zstring +{ + char *string; /* the string itself */ + int len; /* string length, for speed */ + int ref_count; /* for gc */ + struct t_zstring *next; /* for linking in hash table */ + struct t_zstring *prev; /* for linking in hash table */ +} ZSTRING; + +#ifdef __STDC__ +# define P(s) s +#else +# define P(s) () +#endif + +ZSTRING *make_zstring P((char *s, int downcase)); +void free_zstring P((ZSTRING *z)); +ZSTRING *find_zstring P((char *s, int downcase)); +int eq_zstring P((ZSTRING *a, ZSTRING *b)); + + +#undef P + + +#endif /* __zstring_h */ |