view ParamBag.c @ 17:c6544bc64a7c

New Brch -- ML_dev -- used to develop multi-lang capability in VMS, using VSs
author Sean Halle <seanhalle@yahoo.com>
date Fri, 08 Mar 2013 05:47:18 -0800
parents 253da493308e
children 740df4b46535
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"
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 //TODO: Bug -- need internal, App/WL, and external versions
25 ParamBag *
26 makeParamBag()
27 { ParamBag * retBag;
28 retBag = malloc( sizeof( ParamBag ) );
29 retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) );
30 retBag->bagSz = PARAM_BAG_HASHSIZE;
31 nullOutParamBagHashEntries( retBag );
33 return retBag;
34 }
36 void
37 nullOutParamBagHashEntries( ParamBag *bag )
38 { int i, bagSz;
39 bagSz = bag->bagSz;
40 ParamBagHashEntry ** entries = bag->entries;
41 for( i = 0; i < bagSz; i++ )
42 entries[ i ] = NULL;
43 }
45 unsigned int
46 hashKey( char *s, int hashSz )
47 { unsigned int h = 0;
49 for( ; *s != 0; s++ )
50 h = *s + h*31;
51 return h % hashSz;
52 }
54 /*Need this to be separated out, for use in both getParam and putParam
55 */
56 ParamBagHashEntry *
57 lookupKeyInHash( char *key, ParamBag * bag )
58 { unsigned int
59 hashIndex = hashKey( key, bag->bagSz );
60 ParamBagHashEntry*
61 hashEntry = bag->entries[ hashIndex ];
62 for( ; hashEntry != NULL; hashEntry = hashEntry->next )
63 { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry;
64 }
65 return NULL;
66 }
68 ParamStruc *
69 getParamFromBag( char *key, ParamBag * bag )
70 { ParamBagHashEntry *entry;
71 entry = lookupKeyInHash( key, bag );
72 if( entry == NULL ) return NULL;
74 return entry->param;
75 }
77 int
78 addParamToBag( char* key, ParamStruc *param, ParamBag *bag )
79 { unsigned int hashIdx;
80 ParamBagHashEntry* hashEntry;
81 hashEntry = lookupKeyInHash( key, bag );
82 if( hashEntry == NULL )
83 { hashIdx = hashKey( key, bag->bagSz );
84 hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) );
85 if( hashEntry == NULL ) return 0;
86 hashEntry->key = strdup( key );
87 if( hashEntry->key == NULL ) return 0;
88 hashEntry->next = (bag->entries)[hashIdx];
89 (bag->entries)[hashIdx] = hashEntry;
90 }
91 else
92 { freeParamStruc( hashEntry->param );
93 }
94 hashEntry->param = param;
95 return 1;
96 }
99 void
100 freeParamBag( ParamBag *bag )
101 { int i;
102 ParamBagHashEntry *hashEntry, *temp, **entries;
104 entries = bag->entries;
105 for( i=0; i < bag->bagSz; i++ )
106 { if( entries[i] != NULL )
107 { hashEntry = entries[i];
108 while( hashEntry != NULL )
109 {
110 temp = hashEntry->next;
111 freeParamBagHashEntry( hashEntry );
112 hashEntry = temp;
113 }
114 }
115 }
116 }
118 void
119 freeParamBagHashEntry( ParamBagHashEntry *entry )
120 {
121 freeParamStruc( entry->param );
122 free( entry->key ); //was malloc'd above, so free it
123 free( entry );
124 }
126 void
127 freeParamStruc( ParamStruc * param )
128 { if( param->type == STRING_PARAM_TYPE ) free( param->strValue );
129 free( param );
130 }
132 ParamStruc *
133 makeParamStruc()
134 { ParamStruc *retStruc;
135 retStruc = malloc( sizeof( ParamStruc ) );
136 retStruc->floatValue = 0.0;
137 retStruc->intValue = 0;
138 retStruc->strValue = NULL;
140 return retStruc;
141 }
143 void
144 removeEndWhtSpaceFromStr( char *str )
145 { int n;
147 n = strlen ( str );
148 while( --n >= 0 )
149 {
150 if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r')
151 break;
152 }
153 str[n + 1] = '\0';
154 }
157 ParamStruc *
158 makeParamFromStrs( char * type, char *value )
159 { ParamStruc *retParam;
160 retParam = makeParamStruc();
161 switch(*type)
162 { case 'i':
163 { retParam->type = INT_PARAM_TYPE;
164 retParam->intValue = atoi( value );
165 } break;
166 case 's':
167 { retParam->type = STRING_PARAM_TYPE;
168 retParam->strValue = malloc( strlen(value) + 1);
169 strcpy( retParam->strValue, value );
170 removeEndWhtSpaceFromStr( retParam->strValue );
171 } break;
172 case 'f':
173 { retParam->type = FLOAT_PARAM_TYPE;
174 retParam->floatValue = atof( value );
175 } break;
176 }
177 return retParam;
178 }
181 /* A pretty useless but good debugging function,
182 which simply displays the hashtable in (key.value) pairs
183 */
184 /*void paramBagToString( ParamBag * bag )
185 { int i;
186 ParamBagHashEntry *t;
187 for( i = 0; i < bag->bagSz; i++ )
188 { t = entries[i];
189 if( t == NULL )
190 strcat_m( retStr, &"()" );
191 else
192 { strcat_m( retStr, &"(" );
193 for( ; t != NULL; t = t->next )
194 { strcat_m( retStr, &" " );
195 strcat_m( retStr, t->key );
196 strcat_m( retStr, &"." );
197 strcat_m( retStr, paramToString( t->param ) );
198 strcat_m( retStr, &" " );
199 }
200 strcat_m( retStr, &")" );
201 }
202 }
203 }
204 */