ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/savestr.c
Revision: 2.11
Committed: Sun Apr 9 21:33:24 2017 UTC (7 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad5R1
Changes since 2.10: +9 -7 lines
Log Message:
Fixed issue with reading and saving empty strings

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.11 static const char RCSid[] = "$Id: savestr.c,v 2.10 2004/03/04 16:34:34 greg Exp $";
3 greg 1.1 #endif
4 greg 1.2 /*
5 greg 1.1 * savestr.c - routines for efficient string storage.
6     *
7     * Savestr(s) stores a shared read-only string.
8     * Freestr(s) indicates a client is finished with a string.
9     * All strings must be null-terminated. There is
10     * no imposed length limit.
11     * Strings stored with savestr(s) can be equated
12 greg 2.5 * reliably using their pointer values.
13 greg 1.1 * Calls to savestr(s) and freestr(s) should be
14     * balanced (obviously). The last call to freestr(s)
15     * frees memory associated with the string; it should
16     * never be referenced again.
17     *
18 greg 2.5 * External symbols declared in standard.h
19     */
20    
21 greg 2.6 #include "copyright.h"
22 greg 1.1
23 schorsch 2.7 #include <string.h>
24     #include <stdlib.h>
25    
26 schorsch 2.8 #include "rtmisc.h"
27 schorsch 2.9 #include "rterror.h"
28     #include "rtio.h"
29 schorsch 2.7
30 greg 1.1 #ifndef NHASH
31 greg 2.10 #define NHASH 2039 /* hash table size (prime!) */
32 greg 1.1 #endif
33    
34     typedef struct s_head {
35     struct s_head *next; /* next in hash list */
36     int nl; /* links count */
37     } S_HEAD; /* followed by the string itself */
38    
39     static S_HEAD *stab[NHASH];
40    
41 greg 2.3 #define hash(s) (shash(s)%NHASH)
42 greg 1.1
43     #define string(sp) ((char *)((sp)+1))
44    
45     #define salloc(str) (S_HEAD *)malloc(sizeof(S_HEAD)+1+strlen(str))
46    
47 greg 2.5 #define sfree(sp) free((void *)(sp))
48 greg 1.1
49    
50     char *
51 schorsch 2.9 savestr(char *str) /* save a string */
52 greg 1.1 {
53 greg 2.11 int hval;
54     S_HEAD *sp;
55 greg 1.1
56     if (str == NULL)
57     return(NULL);
58 greg 2.11 if (!*str)
59     return "";
60 greg 2.3 hval = hash(str);
61 greg 1.1 for (sp = stab[hval]; sp != NULL; sp = sp->next)
62     if (!strcmp(str, string(sp))) {
63     sp->nl++;
64     return(string(sp));
65     }
66     if ((sp = salloc(str)) == NULL) {
67     eputs("Out of memory in savestr\n");
68     quit(1);
69     }
70     strcpy(string(sp), str);
71     sp->nl = 1;
72     sp->next = stab[hval];
73     stab[hval] = sp;
74     return(string(sp));
75     }
76    
77    
78 greg 2.5 void
79 schorsch 2.9 freestr(char *s) /* free a string */
80 greg 1.1 {
81     int hval;
82 greg 2.11 S_HEAD *spl, *sp;
83 greg 1.1
84 greg 2.11 if (s == NULL || !*s)
85 greg 1.1 return;
86 greg 2.3 hval = hash(s);
87 greg 1.1 for (spl = NULL, sp = stab[hval]; sp != NULL; spl = sp, sp = sp->next)
88     if (s == string(sp)) {
89     if (--sp->nl > 0)
90     return;
91     if (spl != NULL)
92     spl->next = sp->next;
93     else
94     stab[hval] = sp->next;
95     sfree(sp);
96     return;
97     }
98     }
99    
100    
101 greg 2.3 int
102 greg 2.11 shash(char *s)
103 greg 1.1 {
104 greg 2.11 int h = 0;
105 greg 1.1
106     while (*s)
107 greg 2.4 h = (h<<1 & 0x7fff) ^ (*s++ & 0xff);
108 greg 2.3 return(h);
109 greg 1.1 }