CMake netCDF Compatibility with WPS (#2121)
[WRF.git] / tools / symtab_gen.c
bloba6ddcf96615e241be1ce3b111e256f04636f50d1
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 <stdlib.h>
36 #include <string.h>
37 #ifndef _WIN32
38 # include <strings.h>
39 #endif
40 #include "protos.h"
42 #define HASHSIZE 1024
44 /* commented out 2-29-90
45 static char * symtab[HASHSIZE] ;
48 void * malloc() ;
49 void * calloc() ;
52 char * symget(char *name,char *(*newnode)(),char **(*nodename)(char *),char **(*nodenext)(char *), char *symtab[], int flag) /* flag = 1 is create if not there, 0 return NULL if not there */
54 int index ;
55 int found ;
56 register char *s ;
57 register char *t ;
58 char **x ;
59 char *p ;
61 index = hash( name ) ;
62 p = symtab[index] ;
63 found = 0 ;
65 while (p) {
66 s = name ;
67 t = *(*nodename)(p) ;
68 while (*s && *t && *s == *t ) {
69 s++ ;
70 t++ ;
72 if (!*s && !*t) {
73 found = 1 ;
74 break ;
76 p = *(*nodenext)(p) ;
79 if (!found ) {
80 if (flag ) {
81 p = (*newnode)() ;
82 x = (*nodename)(p) ;
83 *x = (char *) malloc(strlen(name)+1) ;
84 strcpy(*x,name) ;
85 x = (*nodenext)(p) ;
86 *x = symtab[index] ;
87 symtab[index] = p ;
88 } else {
89 return(NULL) ;
93 return(p) ;
96 int hash(char * name )
98 register int result = 0 ;
99 register char * p = name ;
101 while (*p)
102 result = 3*result + (int)*p++ ;
104 result = result % HASHSIZE ;
105 while (result < 0)
106 result = result + HASHSIZE ;
107 return(result) ;
111 /* added 2-19-90, attaches a new hash table to pointer */
113 int create_ht(char *** p )
115 *p = (char **) calloc( HASHSIZE , sizeof( char * ) ) ;
116 return(0) ;
120 /* added 4-15-92.
122 This is a generic routine that, given a hash table pointer,
123 will traverse the hash table and apply a caller supplied
124 function to each entry
128 int sym_traverse( char *ht[] , char **(*nodenext)(char *), void (*f)(char *) )
130 char * p, **x ;
131 int i ;
132 for ( i = 0 ; i < HASHSIZE ; i++ )
134 if ( ( p = ht[i] ) != NULL )
136 while ( p )
138 (*f)(p) ;
139 x = (*nodenext)(p) ;
140 p = *x ;
144 return(0) ;
147 /**********************************************************************/
148 /**********************************************************************/
149 /**********************************************************************/
151 #ifdef COMMENTOUTSAMPLE
152 /* sample_main.c
154 sample main program for symget() in the file symtab.c
158 #include <stdio.h>
160 struct symnode {
161 char * name ;
162 struct symnode *next ;
165 extern struct symnode * symget() ;
167 struct symnode * newnode()
169 struct symnode * malloc() ;
170 return( malloc( sizeof( struct symnode ) ) ) ;
173 char ** nodename(struct symnode *p)
175 char ** x ;
176 x = &(p->name) ;
177 return( x ) ;
180 struct symnode ** nodenext(struct symnode *p)
182 struct symnode **x ;
183 x = &(p->next) ;
184 return( x ) ;
187 #endif
189 /**********************************************************************/
190 /**********************************************************************/
191 /**********************************************************************/