5 |
|
* Table lookup routines |
6 |
|
*/ |
7 |
|
|
8 |
– |
/* ==================================================================== |
9 |
– |
* The Radiance Software License, Version 1.0 |
10 |
– |
* |
11 |
– |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
12 |
– |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
13 |
– |
* |
14 |
– |
* Redistribution and use in source and binary forms, with or without |
15 |
– |
* modification, are permitted provided that the following conditions |
16 |
– |
* are met: |
17 |
– |
* |
18 |
– |
* 1. Redistributions of source code must retain the above copyright |
19 |
– |
* notice, this list of conditions and the following disclaimer. |
20 |
– |
* |
21 |
– |
* 2. Redistributions in binary form must reproduce the above copyright |
22 |
– |
* notice, this list of conditions and the following disclaimer in |
23 |
– |
* the documentation and/or other materials provided with the |
24 |
– |
* distribution. |
25 |
– |
* |
26 |
– |
* 3. The end-user documentation included with the redistribution, |
27 |
– |
* if any, must include the following acknowledgment: |
28 |
– |
* "This product includes Radiance software |
29 |
– |
* (http://radsite.lbl.gov/) |
30 |
– |
* developed by the Lawrence Berkeley National Laboratory |
31 |
– |
* (http://www.lbl.gov/)." |
32 |
– |
* Alternately, this acknowledgment may appear in the software itself, |
33 |
– |
* if and wherever such third-party acknowledgments normally appear. |
34 |
– |
* |
35 |
– |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
36 |
– |
* and "The Regents of the University of California" must |
37 |
– |
* not be used to endorse or promote products derived from this |
38 |
– |
* software without prior written permission. For written |
39 |
– |
* permission, please contact [email protected]. |
40 |
– |
* |
41 |
– |
* 5. Products derived from this software may not be called "Radiance", |
42 |
– |
* nor may "Radiance" appear in their name, without prior written |
43 |
– |
* permission of Lawrence Berkeley National Laboratory. |
44 |
– |
* |
45 |
– |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
46 |
– |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
47 |
– |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
48 |
– |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
49 |
– |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
50 |
– |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
51 |
– |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
52 |
– |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
53 |
– |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
54 |
– |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
55 |
– |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
56 |
– |
* SUCH DAMAGE. |
57 |
– |
* ==================================================================== |
58 |
– |
* |
59 |
– |
* This software consists of voluntary contributions made by many |
60 |
– |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
61 |
– |
* information on Lawrence Berkeley National Laboratory, please see |
62 |
– |
* <http://www.lbl.gov/>. |
63 |
– |
*/ |
64 |
– |
|
8 |
|
#include <stdio.h> |
9 |
|
#include <stdlib.h> |
10 |
+ |
#include <string.h> |
11 |
+ |
|
12 |
|
#include "lookup.h" |
13 |
|
|
14 |
< |
#ifdef NOSTRUCTASS |
15 |
< |
#define copystruct(d,s) bcopy((char *)(s),(char *)(d),sizeof(*(d))) |
16 |
< |
#else |
17 |
< |
#define copystruct(d,s) (*(d) = *(s)) |
18 |
< |
#endif |
14 |
> |
extern int |
15 |
> |
lu_strcmp( |
16 |
> |
const void *s1, |
17 |
> |
const void *s2 |
18 |
> |
) |
19 |
> |
{ |
20 |
> |
return strcmp((const char*)s1,(const char*)s2); |
21 |
> |
} |
22 |
|
|
23 |
< |
|
24 |
< |
int |
25 |
< |
lu_init(tbl, nel) /* initialize tbl for at least nel elements */ |
26 |
< |
register LUTAB *tbl; |
27 |
< |
int nel; |
23 |
> |
extern int |
24 |
> |
lu_init( /* initialize tbl for at least nel elements */ |
25 |
> |
register LUTAB *tbl, |
26 |
> |
int nel |
27 |
> |
) |
28 |
|
{ |
29 |
|
static int hsiztab[] = { |
30 |
|
31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, |
47 |
|
} |
48 |
|
|
49 |
|
|
50 |
< |
unsigned long |
51 |
< |
lu_shash(s) /* hash a nul-terminated string */ |
52 |
< |
char *s; |
50 |
> |
extern unsigned long |
51 |
> |
lu_shash( /* hash a nul-terminated string */ |
52 |
> |
const void *s |
53 |
> |
) |
54 |
|
{ |
55 |
|
static unsigned char shuffle[256] = { |
56 |
|
0, 157, 58, 215, 116, 17, 174, 75, 232, 133, 34, |
80 |
|
}; |
81 |
|
register int i = 0; |
82 |
|
register unsigned long h = 0; |
83 |
< |
register unsigned char *t = (unsigned char *)s; |
83 |
> |
register unsigned const char *t = (unsigned const char *)s; |
84 |
|
|
85 |
|
while (*t) |
86 |
|
h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf); |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
< |
LUENT * |
93 |
< |
lu_find(tbl, key) /* find a table entry */ |
94 |
< |
register LUTAB *tbl; |
95 |
< |
char *key; |
92 |
> |
extern LUENT * |
93 |
> |
lu_find( /* find a table entry */ |
94 |
> |
register LUTAB *tbl, |
95 |
> |
const char *key |
96 |
> |
) |
97 |
|
{ |
98 |
|
unsigned long hval; |
99 |
|
int i, n; |
134 |
|
* recursive call to lu_find(). |
135 |
|
*/ |
136 |
|
while (ndx--) |
137 |
< |
if (le[ndx].key != NULL) |
137 |
> |
if (le[ndx].key != NULL) { |
138 |
|
if (le[ndx].data != NULL) |
139 |
< |
copystruct(lu_find(tbl,le[ndx].key), &le[ndx]); |
139 |
> |
*lu_find(tbl,le[ndx].key) = le[ndx]; |
140 |
|
else if (tbl->freek != NULL) |
141 |
|
(*tbl->freek)(le[ndx].key); |
142 |
+ |
} |
143 |
|
free((void *)le); |
144 |
|
goto tryagain; /* should happen only once! */ |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
< |
void |
149 |
< |
lu_delete(tbl, key) /* delete a table entry */ |
150 |
< |
register LUTAB *tbl; |
151 |
< |
char *key; |
148 |
> |
extern void |
149 |
> |
lu_delete( /* delete a table entry */ |
150 |
> |
register LUTAB *tbl, |
151 |
> |
const char *key |
152 |
> |
) |
153 |
|
{ |
154 |
|
register LUENT *le; |
155 |
|
|
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
< |
int |
168 |
< |
lu_doall(tbl, f) /* loop through all valid table entries */ |
169 |
< |
register LUTAB *tbl; |
170 |
< |
int (*f)(); |
167 |
> |
extern int |
168 |
> |
lu_doall( /* loop through all valid table entries */ |
169 |
> |
register const LUTAB *tbl, |
170 |
> |
//int (*f)(const LUENT *) |
171 |
> |
lut_doallf_t *f, |
172 |
> |
void *p |
173 |
> |
) |
174 |
|
{ |
175 |
|
int rval = 0; |
176 |
< |
register LUENT *tp; |
176 |
> |
register const LUENT *tp; |
177 |
|
|
178 |
|
for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; ) |
179 |
< |
if (tp->data != NULL) |
180 |
< |
if (f != NULL) |
181 |
< |
rval += (*f)(tp); |
182 |
< |
else |
179 |
> |
if (tp->data != NULL) { |
180 |
> |
if (f != NULL) { |
181 |
> |
int r = (*f)(tp, p); |
182 |
> |
if (r < 0) |
183 |
> |
return(-1); |
184 |
> |
rval += r; |
185 |
> |
} else |
186 |
|
rval++; |
187 |
+ |
} |
188 |
|
return(rval); |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
< |
void |
193 |
< |
lu_done(tbl) /* free table and contents */ |
194 |
< |
register LUTAB *tbl; |
192 |
> |
extern void |
193 |
> |
lu_done( /* free table and contents */ |
194 |
> |
register LUTAB *tbl |
195 |
> |
) |
196 |
|
{ |
197 |
|
register LUENT *tp; |
198 |
|
|