source: lmdz_wrf/branches/LMDZ_WRFmeas/WRFV3/tools/symtab_gen.c @ 102

Last change on this file since 102 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#ifndef _WIN32
37# include <strings.h>
38#endif
39
40#define HASHSIZE 1024
41
42/*  commented out 2-29-90
43static char * symtab[HASHSIZE] ;       
44*/
45
46void * malloc() ;
47void * calloc() ;
48
49char * symget(name,newnode,nodename,nodenext,symtab,flag)
50char *name ;
51char *(*newnode)(), **(*nodename)(), **(*nodenext)() ;
52char *symtab[] ;
53int flag ;              /* 1 is create if not there, 0 return NULL if not there */
54{
55    int index ; 
56    int found ;
57    register char *s ;
58    register char *t ;
59    char **x ;
60    char *p ;
61
62    index = hash( name ) ;
63    p = symtab[index] ;
64    found = 0 ;
65
66    while (p) {
67        s = name ;
68        t = *(*nodename)(p) ;
69        while (*s && *t && *s == *t ) {
70            s++ ;
71            t++ ;
72        }
73        if (!*s && !*t) {
74            found = 1 ;
75            break ;
76        }
77        p = *(*nodenext)(p) ;
78    }
79
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) ;
91      }
92    }
93
94    return(p) ;
95}
96
97int
98hash(name)
99char * name ;
100{
101    register int result = 0  ;
102    register char * p = name ;
103
104    while (*p)
105        result = 3*result + (int)*p++ ;
106   
107    result = result % HASHSIZE ;
108    while (result < 0)
109        result = result + HASHSIZE ;
110    return(result) ;
111}
112
113
114/* added 2-19-90, attaches a new hash table to pointer  */
115
116int
117create_ht( p )
118char *** p ; 
119{
120    *p = (char **) calloc( HASHSIZE , sizeof( char * ) ) ;
121    return(0) ;
122}
123
124
125/* added 4-15-92.
126
127This is a generic routine that, given a hash table pointer,
128will traverse the hash table and apply a caller supplied
129function to each entry
130
131*/
132
133int
134sym_traverse( ht, nodenext, f )
135char *ht[] ;
136char **(*nodenext)() ;
137void (*f)() ;
138{
139    char * p, **x ;
140    int i ;
141    for ( i = 0 ; i < HASHSIZE ; i++ )
142    {
143        if ( ( p = ht[i] ) != NULL )
144        {
145            while ( p )
146            {
147                 (*f)(p) ;
148                 x = (*nodenext)(p) ;
149                 p = *x ;
150            }
151        }
152    }
153    return(0) ;
154}
155
156/**********************************************************************/
157/**********************************************************************/
158/**********************************************************************/
159
160#ifdef COMMENTOUTSAMPLE
161/* sample_main.c
162
163    sample main program for symget() in the file symtab.c
164
165*/
166
167#include <stdio.h>
168
169struct symnode {
170    char * name ;
171    struct symnode *next ;
172} ;
173
174extern struct symnode * symget() ;
175
176struct symnode *
177newnode()
178{
179    struct symnode * malloc() ;
180    return( malloc( sizeof( struct symnode ) ) ) ;
181}
182
183char **
184nodename(p)
185struct symnode *p ;
186{
187    char ** x ;
188    x = &(p->name) ;
189    return( x ) ;
190}
191
192struct symnode **
193nodenext(p)
194struct symnode *p ;
195{
196    struct symnode **x ;
197    x = &(p->next) ;
198    return( x ) ;
199}
200
201#endif
202
203/**********************************************************************/
204/**********************************************************************/
205/**********************************************************************/
206
Note: See TracBrowser for help on using the repository browser.