]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/server/zstring.c
r4262@bucket (orig r252): kcr | 2008-01-20 22:11:00 -0500
[1ts-debian.git] / zephyr / server / zstring.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains the main loop of the Zephyr server
3  *
4  *      Created by:     Lucien W. Van Elsen
5  *
6  *      $Id$
7  *
8  *      Copyright (c) 1991 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #include <zephyr/mit-copyright.h>
14 #include "zserver.h"
15
16 #ifndef lint
17 #ifndef SABER
18 static const char rcsid_zstring_c[] =
19 "$Id$";
20 #endif
21 #endif
22
23 static String *zhash[STRING_HASH_TABLE_SIZE];
24
25 String *
26 make_string(char *s,
27             int downcase)
28 {
29     char *new_s,*p;
30     String *new_z,*hp;
31     int i;
32
33     if (downcase) {
34         new_s = strsave(s);
35         p = new_s;
36         while(*p) {
37             if (isascii(*p) && isupper(*p))
38                 *p = tolower(*p);
39             p++;
40         }
41     } else {
42         new_s = s;
43     }
44
45     new_z = find_string(new_s,0);
46     if (new_z != NULL) {
47         if (downcase)
48             free(new_s);
49         new_z->ref_count++;
50         return(new_z);
51     }
52
53     /* Initialize new String */
54
55     if (!downcase)
56         new_s = strsave(s);
57     new_z = (String *) malloc(sizeof(String));
58     new_z->string = new_s;
59     new_z->ref_count = 1;
60   
61     /* Add to beginning of hash table */
62     new_z->hash_val = hash(new_s);
63     i = new_z->hash_val % STRING_HASH_TABLE_SIZE;
64     hp = zhash[i];
65     new_z->next = hp;
66     if (hp != NULL)
67         hp->prev = new_z;
68     new_z->prev = NULL;
69     zhash[i] = new_z;
70
71     return new_z;
72 }
73
74 void
75 free_string(String *z)
76 {
77     if (z == (String *) NULL)
78         return;
79
80     z->ref_count--;
81     if (z->ref_count > 0)
82         return;
83
84     /* delete string completely */
85     if(z->prev == NULL)
86         zhash[hash(z->string) % STRING_HASH_TABLE_SIZE] = z->next;
87     else
88         z->prev->next = z->next;
89   
90     if (z->next != NULL)
91         z->next->prev = z->prev;
92
93     free(z->string);
94     free(z);
95 }
96
97 String *
98 find_string(char *s,
99             int downcase)
100 {
101     char *new_s,*p;
102     String *z;
103
104     if (downcase) {
105         new_s = strsave(s);
106         p = new_s;
107         while (*p) {
108             if (isascii(*p) && isupper(*p))
109                 *p = tolower(*p);
110             p++;
111         }
112     } else {
113         new_s = s;
114     }
115
116     z = zhash[hash(new_s) % STRING_HASH_TABLE_SIZE];
117     while (z != NULL) {
118         if (strcmp(new_s, z->string) == 0)
119             break;
120         z = z->next;
121     }
122
123     if (downcase)
124         free(new_s);
125
126     return z;
127 }
128
129 int
130 comp_string(String *a,
131             String *b)
132 {
133     if (a->hash_val > b->hash_val)
134         return 1;
135     if (a->hash_val < b->hash_val)
136         return -1;
137     return strcmp(a->string,b->string);
138 }
139
140 void
141 print_string_table(FILE *f)
142 {
143     String *p;
144     int i;
145
146     for(i = 0; i < STRING_HASH_TABLE_SIZE; i++) {
147         p = zhash[i];
148         while (p != (String *) NULL) {
149             fprintf(f,"[%d] %s\n",p->ref_count,p->string);
150             p = p->next;
151         }
152     }
153 }
154
155 String *
156 dup_string(String *z)
157 {
158     z->ref_count++;
159     return z;
160 }
161