4 * Handling of messages files
6 * © 1994-1998 Straylight
9 /*----- Licensing note ----------------------------------------------------*
11 * This file is part of Straylight's Steel library.
13 * Steel is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * Steel is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Steel. If not, write to the Free Software Foundation,
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 /*----- Special types -----------------------------------------------------*/
41 typedef struct msgs__node
43 struct msgs__node *left;
44 struct msgs__node *right;
50 /*----- Variables ---------------------------------------------------------*/
52 #define msgs__HASHSIZE 64
54 static msgs__node *msgs__all[msgs__HASHSIZE];
56 /*----- Main code ---------------------------------------------------------*/
59 * int msgs__hash(char *p)
62 * Hashes the string pointed to by p
65 static int msgs__hash(char *p)
68 unsigned int seed=0x46bc93d5;
69 for (;*p && *p!=':';p++)
71 h=(h*(*p)) ^ seed ^ h;
72 seed=seed*3141592621ul + 271828189;
74 return ((int)h & (msgs__HASHSIZE-1));
78 * void msgs__add(msgs__node *t,msgs__node **p)
81 * Adds node t to the tree with *p as its root
84 static void msgs__add(msgs__node *t,msgs__node **p)
89 s=strcmp(t->tag,(*p)->tag);
91 msgs__add(t,&(*p)->right);
93 msgs__add(t,&(*p)->left);
100 * int msgs__cmp(char *a,char *b)
103 * Compares a tag-and-default a with a tag b.
106 static int msgs__cmp(char *a,char *b)
110 if ((*a==':' || *a==0) && *b==0)
120 * msgs__node *msgs__find(char *tag,msgs__node *t)
123 * Finds the tag in the tree pointed to by t
126 static msgs__node *msgs__find(char *tag,msgs__node *t)
131 s=msgs__cmp(tag,t->tag);
133 return (msgs__find(tag,t->right));
135 return (msgs__find(tag,t->left));
141 * void msgs__insert(msgs__node *t)
144 * Inserts the given messages structure into the overall messages system.
147 * msgs__node *t == pointer to a filled in messages structure to fit in.
150 static void msgs__insert(msgs__node *t)
152 int i=msgs__hash(t->tag);
154 msgs__add(t,msgs__all+i);
158 * void msgs_readfile(char *name)
161 * Loads the messages file given by name into the messages area.
164 void msgs_readfile(char *name)
166 FILE *fp=res_openfile(name,"r");
188 while (state!=endoffile)
195 if (c==';' || c=='|' || c=='#')
199 else if (!isspace(c))
202 t[alts]=mem_alloc(sizeof(msgs__node));
207 msgs_lookup("msgsNEM:Not enough memory to read "
208 "messages file '%s'"),
231 msgs_lookup("msgsEOF:Unexpected end of file in "
232 "messages file '%s'"),
238 t[alts]=mem_alloc(sizeof(msgs__node));
245 msgs_lookup("msgsNEM:Not enough memory to read "
246 "messages file '%s'"),
257 if (c=='\n' || c=='/')
273 for (i=0;i<=alts;i++)
276 msgs_lookup("msgsEOF:Unexpected end of file in "
277 "messages file '%s'"),
286 if (c==EOF || c=='\n')
296 msgs_lookup("msgsNEM:Not enough memory to read "
297 "messages file '%s'"),
308 state=(c==EOF ? endoffile : newline);
322 * void msgs_init(void)
325 * Reads the messages file `Messages' into memory.
330 msgs_readfile("Messages");
334 * char *msgs_lookup(char *tag)
337 * Searches the messages for one with a given tag.
340 char *msgs_lookup(char *tag)
342 msgs__node *t=msgs__find(tag,msgs__all[msgs__hash(tag)]);
356 * void msgs_delete(void)
359 * Removes all messages from memory.
362 static void msgs__delTree(msgs__node *t)
366 msgs__delTree(t->left);
367 msgs__delTree(t->right);
372 void msgs_delete(void)
375 for (i=0;i<msgs__HASHSIZE;i++)
377 msgs__delTree(msgs__all[i]);