]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/server/zstring.c
e4e26b842345fe976166f6cdc83e5da79a9c378a
[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(s, downcase)
27     char *s;
28     int downcase;
29 {
30     char *new_s,*p;
31     String *new_z,*hp;
32     int i;
33
34     if (downcase) {
35         new_s = strsave(s);
36         p = new_s;
37         while(*p) {
38             if (isascii(*p) && isupper(*p))
39                 *p = tolower(*p);
40             p++;
41         }
42     } else {
43         new_s = s;
44     }
45
46     new_z = find_string(new_s,0);
47     if (new_z != NULL) {
48         if (downcase)
49             free(new_s);
50         new_z->ref_count++;
51         return(new_z);
52     }
53
54     /* Initialize new String */
55
56     if (!downcase)
57         new_s = strsave(s);
58     new_z = (String *) malloc(sizeof(String));
59     new_z->string = new_s;
60     new_z->ref_count = 1;
61   
62     /* Add to beginning of hash table */
63     new_z->hash_val = hash(new_s);
64     i = new_z->hash_val % STRING_HASH_TABLE_SIZE;
65     hp = zhash[i];
66     new_z->next = hp;
67     if (hp != NULL)
68         hp->prev = new_z;
69     new_z->prev = NULL;
70     zhash[i] = new_z;
71
72     return new_z;
73 }
74
75 void
76 free_string(z)
77     String *z;
78 {
79     if (z == (String *) NULL)
80         return;
81
82     z->ref_count--;
83     if (z->ref_count > 0)
84         return;
85
86     /* delete string completely */
87     if(z->prev == NULL)
88         zhash[hash(z->string) % STRING_HASH_TABLE_SIZE] = z->next;
89     else
90         z->prev->next = z->next;
91   
92     if (z->next != NULL)
93         z->next->prev = z->prev;
94
95     free(z->string);
96     free(z);
97 }
98
99 String *
100 find_string(s,downcase)
101     char *s;
102     int downcase;
103 {
104     char *new_s,*p;
105     String *z;
106
107     if (downcase) {
108         new_s = strsave(s);
109         p = new_s;
110         while (*p) {
111             if (isascii(*p) && isupper(*p))
112                 *p = tolower(*p);
113             p++;
114         }
115     } else {
116         new_s = s;
117     }
118
119     z = zhash[hash(new_s) % STRING_HASH_TABLE_SIZE];
120     while (z != NULL) {
121         if (strcmp(new_s, z->string) == 0)
122             break;
123         z = z->next;
124     }
125
126     if (downcase)
127         free(new_s);
128
129     return z;
130 }
131
132 int
133 comp_string(a,b)
134     String *a, *b;
135 {
136     if (a->hash_val > b->hash_val)
137         return 1;
138     if (a->hash_val < b->hash_val)
139         return -1;
140     return strcmp(a->string,b->string);
141 }
142
143 void
144 print_string_table(f)
145     FILE *f;
146 {
147     String *p;
148     int i;
149
150     for(i = 0; i < STRING_HASH_TABLE_SIZE; i++) {
151         p = zhash[i];
152         while (p != (String *) NULL) {
153             fprintf(f,"[%d] %s\n",p->ref_count,p->string);
154             p = p->next;
155         }
156     }
157 }
158
159 String *
160 dup_string(z)
161     String *z;
162 {
163     z->ref_count++;
164     return z;
165 }
166