Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > ParamHelper
diff ParamBag.c @ 6:d46150af45ad
Newly created project repository -- commit sub-states
| author | Me@portablequad |
|---|---|
| date | Tue, 07 Feb 2012 12:51:29 -0800 |
| parents | cf0a327945e6 |
| children | 1bf28dddc45f 253da493308e |
line diff
1.1 --- a/ParamBag.c Fri Jan 27 15:51:11 2012 +0100 1.2 +++ b/ParamBag.c Tue Feb 07 12:51:29 2012 -0800 1.3 @@ -1,203 +1,203 @@ 1.4 -/* 1.5 - * Copyright 2009 OpenSourceStewardshipFoundation.org 1.6 - * Licensed under GNU General Public License version 2 1.7 - * 1.8 - * Based on code posted to a discussion group on the web. (Forgot to mark 1.9 - * down where got it from) 1.10 - * 1.11 - * Author: seanhalle@yahoo.com 1.12 - * 1.13 - * Created on November 14, 2009, 9:00 PM 1.14 - */ 1.15 -#include <string.h> 1.16 -#include <stdio.h> 1.17 -#include <stdlib.h> 1.18 - 1.19 -#include "Param.h" 1.20 - 1.21 -void freeParamStruc( ParamStruc * param ); 1.22 -void freeParamBagHashEntry( ParamBagHashEntry *entry ); 1.23 -ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag ); 1.24 -unsigned int hashThisKey( char *s, int hashSz ); 1.25 -void nullOutParamBagHashEntries( ParamBag *bag ); 1.26 - 1.27 - ParamBag * 1.28 -makeParamBag() 1.29 - { ParamBag * retBag; 1.30 - retBag = malloc( sizeof( ParamBag ) ); 1.31 - retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) ); 1.32 - retBag->bagSz = PARAM_BAG_HASHSIZE; 1.33 - nullOutParamBagHashEntries( retBag ); 1.34 - 1.35 - return retBag; 1.36 - } 1.37 - 1.38 - void 1.39 -nullOutParamBagHashEntries( ParamBag *bag ) 1.40 - { int i, bagSz; 1.41 - bagSz = bag->bagSz; 1.42 - ParamBagHashEntry ** entries = bag->entries; 1.43 - for( i = 0; i < bagSz; i++ ) 1.44 - entries[ i ] = NULL; 1.45 - } 1.46 - 1.47 - unsigned int 1.48 -hashKey( char *s, int hashSz ) 1.49 - { unsigned int h = 0; 1.50 - 1.51 - for( ; *s != 0; s++ ) 1.52 - h = *s + h*31; 1.53 - return h % hashSz; 1.54 - } 1.55 - 1.56 -/*Need this to be separated out, for use in both getParam and putParam 1.57 - */ 1.58 - ParamBagHashEntry * 1.59 -lookupKeyInHash( char *key, ParamBag * bag ) 1.60 - { unsigned int 1.61 - hashIndex = hashKey( key, bag->bagSz ); 1.62 - ParamBagHashEntry* 1.63 - hashEntry = bag->entries[ hashIndex ]; 1.64 - for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 1.65 - { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; 1.66 - } 1.67 - return NULL; 1.68 - } 1.69 - 1.70 - ParamStruc * 1.71 -getParamFromBag( char *key, ParamBag * bag ) 1.72 - { ParamBagHashEntry *entry; 1.73 - entry = lookupKeyInHash( key, bag ); 1.74 - if( entry == NULL ) return NULL; 1.75 - 1.76 - return entry->param; 1.77 - } 1.78 - 1.79 - int 1.80 -addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) 1.81 - { unsigned int hashIdx; 1.82 - ParamBagHashEntry* hashEntry; 1.83 - hashEntry = lookupKeyInHash( key, bag ); 1.84 - if( hashEntry == NULL ) 1.85 - { hashIdx = hashKey( key, bag->bagSz ); 1.86 - hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) ); 1.87 - if( hashEntry == NULL ) return 0; 1.88 - hashEntry->key = strdup( key ); 1.89 - if( hashEntry->key == NULL ) return 0; 1.90 - hashEntry->next = (bag->entries)[hashIdx]; 1.91 - (bag->entries)[hashIdx] = hashEntry; 1.92 - } 1.93 - else 1.94 - { freeParamStruc( hashEntry->param ); 1.95 - } 1.96 - hashEntry->param = param; 1.97 - return 1; 1.98 - } 1.99 - 1.100 - 1.101 - void 1.102 -freeParamBag( ParamBag *bag ) 1.103 - { int i; 1.104 - ParamBagHashEntry *hashEntry, *temp, **entries; 1.105 - 1.106 - entries = bag->entries; 1.107 - for( i=0; i < bag->bagSz; i++ ) 1.108 - { if( entries[i] != NULL ) 1.109 - { hashEntry = entries[i]; 1.110 - while( hashEntry != NULL ) 1.111 - { 1.112 - temp = hashEntry->next; 1.113 - freeParamBagHashEntry( hashEntry ); 1.114 - hashEntry = temp; 1.115 - } 1.116 - } 1.117 - } 1.118 - } 1.119 - 1.120 - void 1.121 -freeParamBagHashEntry( ParamBagHashEntry *entry ) 1.122 - { 1.123 - freeParamStruc( entry->param ); 1.124 - free( entry->key ); //was malloc'd above, so free it 1.125 - free( entry ); 1.126 - } 1.127 - 1.128 - void 1.129 -freeParamStruc( ParamStruc * param ) 1.130 - { if( param->type == STRING_PARAM_TYPE ) free( param->strValue ); 1.131 - free( param ); 1.132 - } 1.133 - 1.134 - ParamStruc * 1.135 -makeParamStruc() 1.136 - { ParamStruc *retStruc; 1.137 - retStruc = malloc( sizeof( ParamStruc ) ); 1.138 - retStruc->floatValue = 0.0; 1.139 - retStruc->intValue = 0; 1.140 - retStruc->strValue = NULL; 1.141 - 1.142 - return retStruc; 1.143 - } 1.144 - 1.145 -void 1.146 -removeEndWhtSpaceFromStr( char *str ) 1.147 - { int n; 1.148 - 1.149 - n = strlen ( str ); 1.150 - while( --n >= 0 ) 1.151 - { 1.152 - if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r') 1.153 - break; 1.154 - } 1.155 - str[n + 1] = '\0'; 1.156 - } 1.157 - 1.158 - 1.159 -ParamStruc * 1.160 -makeParamFromStrs( char * type, char *value ) 1.161 - { ParamStruc *retParam; 1.162 - retParam = makeParamStruc(); 1.163 - switch(*type) 1.164 - { case 'i': 1.165 - { retParam->type = INT_PARAM_TYPE; 1.166 - retParam->intValue = atoi( value ); 1.167 - } break; 1.168 - case 's': 1.169 - { retParam->type = STRING_PARAM_TYPE; 1.170 - retParam->strValue = malloc( strlen(value) + 1); 1.171 - strcpy( retParam->strValue, value ); 1.172 - removeEndWhtSpaceFromStr( retParam->strValue ); 1.173 - } break; 1.174 - case 'f': 1.175 - { retParam->type = FLOAT_PARAM_TYPE; 1.176 - retParam->floatValue = atof( value ); 1.177 - } break; 1.178 - } 1.179 - return retParam; 1.180 - } 1.181 - 1.182 - 1.183 -/* A pretty useless but good debugging function, 1.184 - which simply displays the hashtable in (key.value) pairs 1.185 -*/ 1.186 -/*void paramBagToString( ParamBag * bag ) 1.187 - { int i; 1.188 - ParamBagHashEntry *t; 1.189 - for( i = 0; i < bag->bagSz; i++ ) 1.190 - { t = entries[i]; 1.191 - if( t == NULL ) 1.192 - strcat_m( retStr, &"()" ); 1.193 - else 1.194 - { strcat_m( retStr, &"(" ); 1.195 - for( ; t != NULL; t = t->next ) 1.196 - { strcat_m( retStr, &" " ); 1.197 - strcat_m( retStr, t->key ); 1.198 - strcat_m( retStr, &"." ); 1.199 - strcat_m( retStr, paramToString( t->param ) ); 1.200 - strcat_m( retStr, &" " ); 1.201 - } 1.202 - strcat_m( retStr, &")" ); 1.203 - } 1.204 - } 1.205 - } 1.206 -*/ 1.207 +/* 1.208 + * Copyright 2009 OpenSourceStewardshipFoundation.org 1.209 + * Licensed under GNU General Public License version 2 1.210 + * 1.211 + * Based on code posted to a discussion group on the web. (Forgot to mark 1.212 + * down where got it from) 1.213 + * 1.214 + * Author: seanhalle@yahoo.com 1.215 + * 1.216 + * Created on November 14, 2009, 9:00 PM 1.217 + */ 1.218 +#include <string.h> 1.219 +#include <stdio.h> 1.220 +#include <stdlib.h> 1.221 + 1.222 +#include "Param.h" 1.223 + 1.224 +void freeParamStruc( ParamStruc * param ); 1.225 +void freeParamBagHashEntry( ParamBagHashEntry *entry ); 1.226 +ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag ); 1.227 +unsigned int hashThisKey( char *s, int hashSz ); 1.228 +void nullOutParamBagHashEntries( ParamBag *bag ); 1.229 + 1.230 + ParamBag * 1.231 +makeParamBag() 1.232 + { ParamBag * retBag; 1.233 + retBag = malloc( sizeof( ParamBag ) ); 1.234 + retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) ); 1.235 + retBag->bagSz = PARAM_BAG_HASHSIZE; 1.236 + nullOutParamBagHashEntries( retBag ); 1.237 + 1.238 + return retBag; 1.239 + } 1.240 + 1.241 + void 1.242 +nullOutParamBagHashEntries( ParamBag *bag ) 1.243 + { int i, bagSz; 1.244 + bagSz = bag->bagSz; 1.245 + ParamBagHashEntry ** entries = bag->entries; 1.246 + for( i = 0; i < bagSz; i++ ) 1.247 + entries[ i ] = NULL; 1.248 + } 1.249 + 1.250 + unsigned int 1.251 +hashKey( char *s, int hashSz ) 1.252 + { unsigned int h = 0; 1.253 + 1.254 + for( ; *s != 0; s++ ) 1.255 + h = *s + h*31; 1.256 + return h % hashSz; 1.257 + } 1.258 + 1.259 +/*Need this to be separated out, for use in both getParam and putParam 1.260 + */ 1.261 + ParamBagHashEntry * 1.262 +lookupKeyInHash( char *key, ParamBag * bag ) 1.263 + { unsigned int 1.264 + hashIndex = hashKey( key, bag->bagSz ); 1.265 + ParamBagHashEntry* 1.266 + hashEntry = bag->entries[ hashIndex ]; 1.267 + for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 1.268 + { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; 1.269 + } 1.270 + return NULL; 1.271 + } 1.272 + 1.273 + ParamStruc * 1.274 +getParamFromBag( char *key, ParamBag * bag ) 1.275 + { ParamBagHashEntry *entry; 1.276 + entry = lookupKeyInHash( key, bag ); 1.277 + if( entry == NULL ) return NULL; 1.278 + 1.279 + return entry->param; 1.280 + } 1.281 + 1.282 + int 1.283 +addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) 1.284 + { unsigned int hashIdx; 1.285 + ParamBagHashEntry* hashEntry; 1.286 + hashEntry = lookupKeyInHash( key, bag ); 1.287 + if( hashEntry == NULL ) 1.288 + { hashIdx = hashKey( key, bag->bagSz ); 1.289 + hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) ); 1.290 + if( hashEntry == NULL ) return 0; 1.291 + hashEntry->key = strdup( key ); 1.292 + if( hashEntry->key == NULL ) return 0; 1.293 + hashEntry->next = (bag->entries)[hashIdx]; 1.294 + (bag->entries)[hashIdx] = hashEntry; 1.295 + } 1.296 + else 1.297 + { freeParamStruc( hashEntry->param ); 1.298 + } 1.299 + hashEntry->param = param; 1.300 + return 1; 1.301 + } 1.302 + 1.303 + 1.304 + void 1.305 +freeParamBag( ParamBag *bag ) 1.306 + { int i; 1.307 + ParamBagHashEntry *hashEntry, *temp, **entries; 1.308 + 1.309 + entries = bag->entries; 1.310 + for( i=0; i < bag->bagSz; i++ ) 1.311 + { if( entries[i] != NULL ) 1.312 + { hashEntry = entries[i]; 1.313 + while( hashEntry != NULL ) 1.314 + { 1.315 + temp = hashEntry->next; 1.316 + freeParamBagHashEntry( hashEntry ); 1.317 + hashEntry = temp; 1.318 + } 1.319 + } 1.320 + } 1.321 + } 1.322 + 1.323 + void 1.324 +freeParamBagHashEntry( ParamBagHashEntry *entry ) 1.325 + { 1.326 + freeParamStruc( entry->param ); 1.327 + free( entry->key ); //was malloc'd above, so free it 1.328 + free( entry ); 1.329 + } 1.330 + 1.331 + void 1.332 +freeParamStruc( ParamStruc * param ) 1.333 + { if( param->type == STRING_PARAM_TYPE ) free( param->strValue ); 1.334 + free( param ); 1.335 + } 1.336 + 1.337 + ParamStruc * 1.338 +makeParamStruc() 1.339 + { ParamStruc *retStruc; 1.340 + retStruc = malloc( sizeof( ParamStruc ) ); 1.341 + retStruc->floatValue = 0.0; 1.342 + retStruc->intValue = 0; 1.343 + retStruc->strValue = NULL; 1.344 + 1.345 + return retStruc; 1.346 + } 1.347 + 1.348 +void 1.349 +removeEndWhtSpaceFromStr( char *str ) 1.350 + { int n; 1.351 + 1.352 + n = strlen ( str ); 1.353 + while( --n >= 0 ) 1.354 + { 1.355 + if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r') 1.356 + break; 1.357 + } 1.358 + str[n + 1] = '\0'; 1.359 + } 1.360 + 1.361 + 1.362 +ParamStruc * 1.363 +makeParamFromStrs( char * type, char *value ) 1.364 + { ParamStruc *retParam; 1.365 + retParam = makeParamStruc(); 1.366 + switch(*type) 1.367 + { case 'i': 1.368 + { retParam->type = INT_PARAM_TYPE; 1.369 + retParam->intValue = atoi( value ); 1.370 + } break; 1.371 + case 's': 1.372 + { retParam->type = STRING_PARAM_TYPE; 1.373 + retParam->strValue = malloc( strlen(value) + 1); 1.374 + strcpy( retParam->strValue, value ); 1.375 + removeEndWhtSpaceFromStr( retParam->strValue ); 1.376 + } break; 1.377 + case 'f': 1.378 + { retParam->type = FLOAT_PARAM_TYPE; 1.379 + retParam->floatValue = atof( value ); 1.380 + } break; 1.381 + } 1.382 + return retParam; 1.383 + } 1.384 + 1.385 + 1.386 +/* A pretty useless but good debugging function, 1.387 + which simply displays the hashtable in (key.value) pairs 1.388 +*/ 1.389 +/*void paramBagToString( ParamBag * bag ) 1.390 + { int i; 1.391 + ParamBagHashEntry *t; 1.392 + for( i = 0; i < bag->bagSz; i++ ) 1.393 + { t = entries[i]; 1.394 + if( t == NULL ) 1.395 + strcat_m( retStr, &"()" ); 1.396 + else 1.397 + { strcat_m( retStr, &"(" ); 1.398 + for( ; t != NULL; t = t->next ) 1.399 + { strcat_m( retStr, &" " ); 1.400 + strcat_m( retStr, t->key ); 1.401 + strcat_m( retStr, &"." ); 1.402 + strcat_m( retStr, paramToString( t->param ) ); 1.403 + strcat_m( retStr, &" " ); 1.404 + } 1.405 + strcat_m( retStr, &")" ); 1.406 + } 1.407 + } 1.408 + } 1.409 +*/
