source: lmdz_wrf/trunk/WRFV3/tools/CodeBase/symtab_gen.c @ 951

Last change on this file since 951 was 1, checked in by lfita, 10 years ago
  • -- --- Opening of the WRF+LMDZ coupling repository --- -- -

WRF: version v3.3
LMDZ: version v1818

More details in:

File size: 4.0 KB
Line 
1/*  symtab.c
2
3Symbol Table Handler -- Generic
4
5The routine symget() returns a pointer to a C structure matching a
6given lexeme.  If the lexeme does not already exist in the symbol
7table, the routine will create a new symbol structure, store it, and
8then return a pointer to the newly created structure.
9
10It is up to the calling module to declare the symbol structure as
11well as several routines for manipulating the symbol structure.  The
12routines are passed to symget as pointers.
13
14        name       type         description
15
16        newnode()   *char       returns a pointer to a symbol structure.
17
18        nodename()  **char      retrieves the lexeme name from a symbol
19                                structure, returned as a pointer to a
20                                character array.
21       
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)
25
26For a sample main or calling program see the end of this file.
27
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
32*/
33
34#include <stdio.h>
35#include <string.h>
36#include <strings.h>
37
38#define HASHSIZE 1024
39
40/*  commented out 2-29-90
41static char * symtab[HASHSIZE] ;       
42*/
43
44void * malloc() ;
45void * calloc() ;
46
47char * symget(name,newnode,nodename,nodenext,symtab,flag)
48char *name ;
49char *(*newnode)(), **(*nodename)(), **(*nodenext)() ;
50char *symtab[] ;
51int flag ;              /* 1 is create if not there, 0 return NULL if not there */
52{
53    int index ; 
54    int found ;
55    register char *s ;
56    register char *t ;
57    char **x ;
58    char *p ;
59
60    index = hash( name ) ;
61    p = symtab[index] ;
62    found = 0 ;
63
64    while (p) {
65        s = name ;
66        t = *(*nodename)(p) ;
67        while (*s && *t && *s == *t ) {
68            s++ ;
69            t++ ;
70        }
71        if (!*s && !*t) {
72            found = 1 ;
73            break ;
74        }
75        p = *(*nodenext)(p) ;
76    }
77
78    if (!found ) {
79      if (flag ) {
80        p = (*newnode)() ;
81        x =  (*nodename)(p) ;
82        *x = (char *) malloc(strlen(name)+1) ;
83        strcpy(*x,name) ;
84        x =  (*nodenext)(p) ;
85        *x = symtab[index] ;
86        symtab[index] = p ;
87      } else {
88        return(NULL) ;
89      }
90    }
91
92    return(p) ;
93}
94
95hash(name)
96char * name ;
97{
98    register int result = 0  ;
99    register char * p = name ;
100
101    while (*p)
102        result = 3*result + (int)*p++ ;
103   
104    result = result % HASHSIZE ;
105    while (result < 0)
106        result = result + HASHSIZE ;
107    return(result) ;
108}
109
110
111/* added 2-19-90, attaches a new hash table to pointer  */
112
113int
114create_ht( p )
115char *** p ; 
116{
117    *p = (char **) calloc( HASHSIZE , sizeof( char * ) ) ;
118    return(0) ;
119}
120
121
122/* added 4-15-92.
123
124This is a generic routine that, given a hash table pointer,
125will traverse the hash table and apply a caller supplied
126function to each entry
127
128*/
129
130int
131sym_traverse( ht, nodenext, f )
132char *ht[] ;
133char **(*nodenext)() ;
134void (*f)() ;
135{
136    char * p, **x ;
137    int i ;
138    for ( i = 0 ; i < HASHSIZE ; i++ )
139    {
140        if ( ( p = ht[i] ) != NULL )
141        {
142            while ( p )
143            {
144                 (*f)(p) ;
145                 x = (*nodenext)(p) ;
146                 p = *x ;
147            }
148        }
149    }
150    return(0) ;
151}
152
153/**********************************************************************/
154/**********************************************************************/
155/**********************************************************************/
156
157#ifdef COMMENTOUTSAMPLE
158/* sample_main.c
159
160    sample main program for symget() in the file symtab.c
161
162*/
163
164#include <stdio.h>
165
166struct symnode {
167    char * name ;
168    struct symnode *next ;
169} ;
170
171extern struct symnode * symget() ;
172
173struct symnode *
174newnode()
175{
176    struct symnode * malloc() ;
177    return( malloc( sizeof( struct symnode ) ) ) ;
178}
179
180char **
181nodename(p)
182struct symnode *p ;
183{
184    char ** x ;
185    x = &(p->name) ;
186    return( x ) ;
187}
188
189struct symnode **
190nodenext(p)
191struct symnode *p ;
192{
193    struct symnode **x ;
194    x = &(p->next) ;
195    return( x ) ;
196}
197
198#endif
199
200/**********************************************************************/
201/**********************************************************************/
202/**********************************************************************/
203
Note: See TracBrowser for help on using the repository browser.