Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > ParamHelper
view ParamBag.c @ 2:8f6d8a258491
Not sure what changed but works
| author | Me |
|---|---|
| date | Wed, 28 Jul 2010 13:17:25 -0700 |
| parents | 481dd533f0e8 |
| children | cf0a327945e6 |
line source
1 /*
2 * Copyright 2009 OpenSourceCodeStewardshipFoundation.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"
18 void freeParamStruc( ParamStruc * param );
19 void freeParamBagHashEntry( ParamBagHashEntry *entry );
20 ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag );
21 unsigned int hashThisKey( char *s, int hashSz );
22 void nullOutParamBagHashEntries( ParamBag *bag );
24 ParamBag *
25 makeParamBag()
26 { ParamBag * retBag;
27 retBag = malloc( sizeof( ParamBag ) );
28 retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) );
29 retBag->bagSz = PARAM_BAG_HASHSIZE;
30 nullOutParamBagHashEntries( retBag );
32 return retBag;
33 }
35 void
36 nullOutParamBagHashEntries( ParamBag *bag )
37 { int i, bagSz;
38 bagSz = bag->bagSz;
39 ParamBagHashEntry ** entries = bag->entries;
40 for( i = 0; i < bagSz; i++ )
41 entries[ i ] = NULL;
42 }
44 unsigned int
45 hashKey( char *s, int hashSz )
46 { unsigned int h = 0;
48 for( ; *s != 0; s++ )
49 h = *s + h*31;
50 return h % hashSz;
51 }
53 /*Need this to be separated out, for use in both getParam and putParam
54 */
55 ParamBagHashEntry *
56 lookupKeyInHash( char *key, ParamBag * bag )
57 { unsigned int
58 hashIndex = hashKey( key, bag->bagSz );
59 ParamBagHashEntry*
60 hashEntry = bag->entries[ hashIndex ];
61 for( ; hashEntry != NULL; hashEntry = hashEntry->next )
62 { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry;
63 }
64 return NULL;
65 }
67 ParamStruc *
68 getParamFromBag( char *key, ParamBag * bag )
69 { ParamBagHashEntry *entry;
70 entry = lookupKeyInHash( key, bag );
71 if( entry == NULL ) return NULL;
73 return entry->param;
74 }
76 int
77 addParamToBag( char* key, ParamStruc *param, ParamBag *bag )
78 { unsigned int hashIdx;
79 ParamBagHashEntry* hashEntry;
80 hashEntry = lookupKeyInHash( key, bag );
81 if( hashEntry == NULL )
82 { hashIdx = hashKey( key, bag->bagSz );
83 hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) );
84 if( hashEntry == NULL ) return 0;
85 hashEntry->key = strdup( key );
86 if( hashEntry->key == NULL ) return 0;
87 hashEntry->next = (bag->entries)[hashIdx];
88 (bag->entries)[hashIdx] = hashEntry;
89 }
90 else
91 { freeParamStruc( hashEntry->param );
92 }
93 hashEntry->param = param;
94 return 1;
95 }
98 void
99 freeParamBag( ParamBag *bag )
100 { int i;
101 ParamBagHashEntry *hashEntry, *temp, **entries;
103 entries = bag->entries;
104 for( i=0; i < bag->bagSz; i++ )
105 { if( entries[i] != NULL )
106 { hashEntry = entries[i];
107 while( hashEntry != NULL )
108 {
109 temp = hashEntry->next;
110 freeParamBagHashEntry( hashEntry );
111 hashEntry = temp;
112 }
113 }
114 }
115 }
117 void
118 freeParamBagHashEntry( ParamBagHashEntry *entry )
119 {
120 freeParamStruc( entry->param );
121 free( entry->key ); //was malloc'd above, so free it
122 free( entry );
123 }
125 void
126 freeParamStruc( ParamStruc * param )
127 { if( param->type == STRING_PARAM_TYPE ) free( param->strValue );
128 free( param );
129 }
131 ParamStruc *
132 makeParamStruc()
133 { ParamStruc *retStruc;
134 retStruc = malloc( sizeof( ParamStruc ) );
135 retStruc->floatValue = 0.0;
136 retStruc->intValue = 0;
137 retStruc->strValue = NULL;
139 return retStruc;
140 }
142 void
143 removeEndWhtSpaceFromStr( char *str )
144 { int n;
146 n = strlen ( str );
147 while( --n >= 0 )
148 {
149 if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r')
150 break;
151 }
152 str[n + 1] = '\0';
153 }
156 ParamStruc *
157 makeParamFromStrs( char * type, char *value )
158 { ParamStruc *retParam;
159 retParam = makeParamStruc();
160 switch(*type)
161 { case 'i':
162 { retParam->type = INT_PARAM_TYPE;
163 retParam->intValue = atoi( value );
164 } break;
165 case 's':
166 { retParam->type = STRING_PARAM_TYPE;
167 retParam->strValue = malloc( strlen(value) + 1);
168 strcpy( retParam->strValue, value );
169 removeEndWhtSpaceFromStr( retParam->strValue );
170 } break;
171 case 'f':
172 { retParam->type = FLOAT_PARAM_TYPE;
173 retParam->floatValue = atof( value );
174 } break;
175 }
176 return retParam;
177 }
180 /* A pretty useless but good debugging function,
181 which simply displays the hashtable in (key.value) pairs
182 */
183 /*void paramBagToString( ParamBag * bag )
184 { int i;
185 ParamBagHashEntry *t;
186 for( i = 0; i < bag->bagSz; i++ )
187 { t = entries[i];
188 if( t == NULL )
189 strcat_m( retStr, &"()" );
190 else
191 { strcat_m( retStr, &"(" );
192 for( ; t != NULL; t = t->next )
193 { strcat_m( retStr, &" " );
194 strcat_m( retStr, t->key );
195 strcat_m( retStr, &"." );
196 strcat_m( retStr, paramToString( t->param ) );
197 strcat_m( retStr, &" " );
198 }
199 strcat_m( retStr, &")" );
200 }
201 }
202 }
203 */
