updated top-level README and version_decl for V4.5 (#1847)
[WRF.git] / tools / symtab_gen.c
blob47870f6c883fc569191978cf85ca8bc99530e04d
1 /* symtab.c
3 Symbol Table Handler -- Generic
5 The routine symget() returns a pointer to a C structure matching a
6 given lexeme. If the lexeme does not already exist in the symbol
7 table, the routine will create a new symbol structure, store it, and
8 then return a pointer to the newly created structure.
10 It is up to the calling module to declare the symbol structure as
11 well as several routines for manipulating the symbol structure. The
12 routines are passed to symget as pointers.
14 name type description
16 newnode() *char returns a pointer to a symbol structure.
18 nodename() **char retrieves the lexeme name from a symbol
19 structure, returned as a pointer to a
20 character array.
22 nodenext() **char retrieves pointer to the next field of
23 the symbol structure (the next field
24 is itself a pointer to a symbol structure)
26 For a sample main or calling program see the end of this file.
28 ****
29 REVISED 2-19-90. Added code to make hashtable interchangible.
30 new routine: create_ht() creates new hashtable
31 rev routine: symget() added parameter to pass hash table
34 #include <stdio.h>
35 #include <string.h>
36 #ifndef _WIN32
37 # include <strings.h>
38 #endif
40 #define HASHSIZE 1024
42 /* commented out 2-29-90
43 static char * symtab[HASHSIZE] ;
46 void * malloc() ;
47 void * calloc() ;
49 char * symget(name,newnode,nodename,nodenext,symtab,flag)
50 char *name ;
51 char *(*newnode)(), **(*nodename)(), **(*nodenext)() ;
52 char *symtab[] ;
53 int flag ; /* 1 is create if not there, 0 return NULL if not there */
55 int index ;
56 int found ;
57 register char *s ;
58 register char *t ;
59 char **x ;
60 char *p ;
62 index = hash( name ) ;
63 p = symtab[index] ;
64 found = 0 ;
66 while (p) {
67 s = name ;
68 t = *(*nodename)(p) ;
69 while (*s && *t && *s == *t ) {
70 s++ ;
71 t++ ;
73 if (!*s && !*t) {
74 found = 1 ;
75 break ;
77 p = *(*nodenext)(p) ;
80 if (!found ) {
81 if (flag ) {
82 p = (*newnode)() ;
83 x = (*nodename)(p) ;
84 *x = (char *) malloc(strlen(name)+1) ;
85 strcpy(*x,name) ;
86 x = (*nodenext)(p) ;
87 *x = symtab[index] ;
88 symtab[index] = p ;
89 } else {
90 return(NULL) ;
94 return(p) ;
97 int
98 hash(name)
99 char * name ;
101 register int result = 0 ;
102 register char * p = name ;
104 while (*p)
105 result = 3*result + (int)*p++ ;
107 result = result % HASHSIZE ;
108 while (result < 0)
109 result = result + HASHSIZE ;
110 return(result) ;
114 /* added 2-19-90, attaches a new hash table to pointer */
117 create_ht( p )
118 char *** p ;
120 *p = (char **) calloc( HASHSIZE , sizeof( char * ) ) ;
121 return(0) ;
125 /* added 4-15-92.
127 This is a generic routine that, given a hash table pointer,
128 will traverse the hash table and apply a caller supplied
129 function to each entry
134 sym_traverse( ht, nodenext, f )
135 char *ht[] ;
136 char **(*nodenext)() ;
137 void (*f)() ;
139 char * p, **x ;
140 int i ;
141 for ( i = 0 ; i < HASHSIZE ; i++ )
143 if ( ( p = ht[i] ) != NULL )
145 while ( p )
147 (*f)(p) ;
148 x = (*nodenext)(p) ;
149 p = *x ;
153 return(0) ;
156 /**********************************************************************/
157 /**********************************************************************/
158 /**********************************************************************/
160 #ifdef COMMENTOUTSAMPLE
161 /* sample_main.c
163 sample main program for symget() in the file symtab.c
167 #include <stdio.h>
169 struct symnode {
170 char * name ;
171 struct symnode *next ;
174 extern struct symnode * symget() ;
176 struct symnode *
177 newnode()
179 struct symnode * malloc() ;
180 return( malloc( sizeof( struct symnode ) ) ) ;
183 char **
184 nodename(p)
185 struct symnode *p ;
187 char ** x ;
188 x = &(p->name) ;
189 return( x ) ;
192 struct symnode **
193 nodenext(p)
194 struct symnode *p ;
196 struct symnode **x ;
197 x = &(p->next) ;
198 return( x ) ;
201 #endif
203 /**********************************************************************/
204 /**********************************************************************/
205 /**********************************************************************/