Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > ParamHelper
view ParamBag.c @ 19:740df4b46535
new branch -- for universal -- changed malloc and free -- do PR__start before use
| author | Sean Halle <seanhalle@yahoo.com> |
|---|---|
| date | Tue, 23 Jul 2013 07:20:05 -0700 |
| parents | c6544bc64a7c |
| children | 5837ad792168 |
line source
1 /*
2 * Copyright 2009 OpenSourceResearchInstitute.org
3 * Licensed under GNU General Public License version 2
4 *
5 * Based on code posted to a discussion group on the web. (Forgot to mark
6 * down where got it from)
7 *
8 * Author: seanhalle@yahoo.com
9 *
10 * Created on November 14, 2009, 9:00 PM
11 */
12 #include <string.h>
13 #include <stdio.h>
14 #include <stdlib.h>
16 #include "Param.h"
17 #include "PR__common_includes/Services_offered_by_PR/Memory_Handling/vmalloc__wrapper_library.h"
19 void freeParamStruc( ParamStruc * param );
20 void freeParamBagHashEntry( ParamBagHashEntry *entry );
21 ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag );
22 unsigned int hashThisKey( char *s, int hashSz );
23 void nullOutParamBagHashEntries( ParamBag *bag );
25 //TODO: Bug -- need internal, App/WL, and external versions
26 ParamBag *
27 makeParamBag()
28 { ParamBag * retBag;
29 retBag = PR__malloc( sizeof( ParamBag ) );
30 retBag->entries = PR__malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) );
31 retBag->bagSz = PARAM_BAG_HASHSIZE;
32 nullOutParamBagHashEntries( retBag );
34 return retBag;
35 }
37 void
38 nullOutParamBagHashEntries( ParamBag *bag )
39 { int i, bagSz;
40 bagSz = bag->bagSz;
41 ParamBagHashEntry ** entries = bag->entries;
42 for( i = 0; i < bagSz; i++ )
43 entries[ i ] = NULL;
44 }
46 unsigned int
47 hashKey( char *s, int hashSz )
48 { unsigned int h = 0;
50 for( ; *s != 0; s++ )
51 h = *s + h*31;
52 return h % hashSz;
53 }
55 /*Need this to be separated out, for use in both getParam and putParam
56 */
57 ParamBagHashEntry *
58 lookupKeyInHash( char *key, ParamBag * bag )
59 { unsigned int
60 hashIndex = hashKey( key, bag->bagSz );
61 ParamBagHashEntry*
62 hashEntry = bag->entries[ hashIndex ];
63 for( ; hashEntry != NULL; hashEntry = hashEntry->next )
64 { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry;
65 }
66 return NULL;
67 }
69 ParamStruc *
70 getParamFromBag( char *key, ParamBag * bag )
71 { ParamBagHashEntry *entry;
72 entry = lookupKeyInHash( key, bag );
73 if( entry == NULL ) return NULL;
75 return entry->param;
76 }
78 int
79 addParamToBag( char* key, ParamStruc *param, ParamBag *bag )
80 { unsigned int hashIdx;
81 ParamBagHashEntry* hashEntry;
82 hashEntry = lookupKeyInHash( key, bag );
83 if( hashEntry == NULL )
84 { hashIdx = hashKey( key, bag->bagSz );
85 hashEntry = (ParamBagHashEntry*) PR__malloc( sizeof( ParamBagHashEntry ) );
86 if( hashEntry == NULL ) return 0;
87 hashEntry->key = strdup( key );
88 if( hashEntry->key == NULL ) return 0;
89 hashEntry->next = (bag->entries)[hashIdx];
90 (bag->entries)[hashIdx] = hashEntry;
91 }
92 else
93 { freeParamStruc( hashEntry->param );
94 }
95 hashEntry->param = param;
96 return 1;
97 }
100 void
101 freeParamBag( ParamBag *bag )
102 { int i;
103 ParamBagHashEntry *hashEntry, *temp, **entries;
105 entries = bag->entries;
106 for( i=0; i < bag->bagSz; i++ )
107 { if( entries[i] != NULL )
108 { hashEntry = entries[i];
109 while( hashEntry != NULL )
110 {
111 temp = hashEntry->next;
112 freeParamBagHashEntry( hashEntry );
113 hashEntry = temp;
114 }
115 }
116 }
117 }
119 void
120 freeParamBagHashEntry( ParamBagHashEntry *entry )
121 {
122 freeParamStruc( entry->param );
123 PR__free( entry->key ); //was malloc'd above, so free it
124 PR__free( entry );
125 }
127 void
128 freeParamStruc( ParamStruc * param )
129 { if( param->type == STRING_PARAM_TYPE ) PR__free( param->strValue );
130 PR__free( param );
131 }
133 ParamStruc *
134 makeParamStruc()
135 { ParamStruc *retStruc;
136 retStruc = PR__malloc( sizeof( ParamStruc ) );
137 retStruc->floatValue = 0.0;
138 retStruc->intValue = 0;
139 retStruc->strValue = NULL;
141 return retStruc;
142 }
144 void
145 removeEndWhtSpaceFromStr( char *str )
146 { int n;
148 n = strlen ( str );
149 while( --n >= 0 )
150 {
151 if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r')
152 break;
153 }
154 str[n + 1] = '\0';
155 }
158 ParamStruc *
159 makeParamFromStrs( char * type, char *value )
160 { ParamStruc *retParam;
161 retParam = makeParamStruc();
162 switch(*type)
163 { case 'i':
164 { retParam->type = INT_PARAM_TYPE;
165 retParam->intValue = atoi( value );
166 } break;
167 case 's':
168 { retParam->type = STRING_PARAM_TYPE;
169 retParam->strValue = PR__malloc( strlen(value) + 1);
170 strcpy( retParam->strValue, value );
171 removeEndWhtSpaceFromStr( retParam->strValue );
172 } break;
173 case 'f':
174 { retParam->type = FLOAT_PARAM_TYPE;
175 retParam->floatValue = atof( value );
176 } break;
177 }
178 return retParam;
179 }
182 /* A pretty useless but good debugging function,
183 which simply displays the hashtable in (key.value) pairs
184 */
185 /*void paramBagToString( ParamBag * bag )
186 { int i;
187 ParamBagHashEntry *t;
188 for( i = 0; i < bag->bagSz; i++ )
189 { t = entries[i];
190 if( t == NULL )
191 strcat_m( retStr, &"()" );
192 else
193 { strcat_m( retStr, &"(" );
194 for( ; t != NULL; t = t->next )
195 { strcat_m( retStr, &" " );
196 strcat_m( retStr, t->key );
197 strcat_m( retStr, &"." );
198 strcat_m( retStr, paramToString( t->param ) );
199 strcat_m( retStr, &" " );
200 }
201 strcat_m( retStr, &")" );
202 }
203 }
204 }
205 */
