Me@0: /* Me@3: * Copyright 2009 OpenSourceStewardshipFoundation.org Me@0: * Licensed under GNU General Public License version 2 Me@0: * Me@0: * Based on code posted to a discussion group on the web. (Forgot to mark Me@0: * down where got it from) Me@0: * Me@0: * Author: seanhalle@yahoo.com Me@0: * Me@0: * Created on November 14, 2009, 9:00 PM Me@0: */ Me@0: #include Me@0: #include Me@0: #include Me@0: Me@0: #include "Param.h" Me@0: Me@0: void freeParamStruc( ParamStruc * param ); Me@2: void freeParamBagHashEntry( ParamBagHashEntry *entry ); Me@2: ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag ); Me@2: unsigned int hashThisKey( char *s, int hashSz ); Me@0: void nullOutParamBagHashEntries( ParamBag *bag ); Me@0: Me@0: ParamBag * Me@0: makeParamBag() Me@0: { ParamBag * retBag; Me@0: retBag = malloc( sizeof( ParamBag ) ); Me@2: retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) ); Me@2: retBag->bagSz = PARAM_BAG_HASHSIZE; Me@0: nullOutParamBagHashEntries( retBag ); Me@0: Me@0: return retBag; Me@0: } Me@0: Me@0: void Me@0: nullOutParamBagHashEntries( ParamBag *bag ) Me@0: { int i, bagSz; Me@0: bagSz = bag->bagSz; Me@2: ParamBagHashEntry ** entries = bag->entries; Me@0: for( i = 0; i < bagSz; i++ ) Me@0: entries[ i ] = NULL; Me@0: } Me@0: Me@0: unsigned int Me@0: hashKey( char *s, int hashSz ) Me@0: { unsigned int h = 0; Me@0: Me@0: for( ; *s != 0; s++ ) Me@0: h = *s + h*31; Me@0: return h % hashSz; Me@0: } Me@0: Me@0: /*Need this to be separated out, for use in both getParam and putParam Me@0: */ Me@2: ParamBagHashEntry * Me@0: lookupKeyInHash( char *key, ParamBag * bag ) Me@0: { unsigned int Me@0: hashIndex = hashKey( key, bag->bagSz ); Me@2: ParamBagHashEntry* Me@0: hashEntry = bag->entries[ hashIndex ]; Me@0: for( ; hashEntry != NULL; hashEntry = hashEntry->next ) Me@0: { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; Me@0: } Me@0: return NULL; Me@0: } Me@0: Me@0: ParamStruc * Me@0: getParamFromBag( char *key, ParamBag * bag ) Me@2: { ParamBagHashEntry *entry; Me@0: entry = lookupKeyInHash( key, bag ); Me@0: if( entry == NULL ) return NULL; Me@0: Me@0: return entry->param; Me@0: } Me@0: Me@0: int Me@0: addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) Me@0: { unsigned int hashIdx; Me@2: ParamBagHashEntry* hashEntry; Me@0: hashEntry = lookupKeyInHash( key, bag ); Me@0: if( hashEntry == NULL ) Me@0: { hashIdx = hashKey( key, bag->bagSz ); Me@2: hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) ); Me@0: if( hashEntry == NULL ) return 0; Me@2: hashEntry->key = strdup( key ); Me@0: if( hashEntry->key == NULL ) return 0; Me@0: hashEntry->next = (bag->entries)[hashIdx]; Me@0: (bag->entries)[hashIdx] = hashEntry; Me@0: } Me@0: else Me@0: { freeParamStruc( hashEntry->param ); Me@0: } Me@0: hashEntry->param = param; Me@0: return 1; Me@0: } Me@0: Me@2: Me@0: void Me@0: freeParamBag( ParamBag *bag ) Me@0: { int i; Me@2: ParamBagHashEntry *hashEntry, *temp, **entries; Me@0: Me@0: entries = bag->entries; Me@0: for( i=0; i < bag->bagSz; i++ ) Me@0: { if( entries[i] != NULL ) Me@0: { hashEntry = entries[i]; Me@0: while( hashEntry != NULL ) Me@0: { Me@0: temp = hashEntry->next; Me@2: freeParamBagHashEntry( hashEntry ); Me@0: hashEntry = temp; Me@0: } Me@0: } Me@0: } Me@0: } Me@0: Me@0: void Me@2: freeParamBagHashEntry( ParamBagHashEntry *entry ) Me@0: { Me@0: freeParamStruc( entry->param ); Me@0: free( entry->key ); //was malloc'd above, so free it Me@0: free( entry ); Me@0: } Me@0: Me@0: void Me@0: freeParamStruc( ParamStruc * param ) Me@0: { if( param->type == STRING_PARAM_TYPE ) free( param->strValue ); Me@0: free( param ); Me@0: } Me@0: Me@0: ParamStruc * Me@0: makeParamStruc() Me@0: { ParamStruc *retStruc; Me@0: retStruc = malloc( sizeof( ParamStruc ) ); Me@0: retStruc->floatValue = 0.0; Me@0: retStruc->intValue = 0; Me@0: retStruc->strValue = NULL; Me@2: Me@2: return retStruc; Me@0: } Me@0: Me@2: void Me@2: removeEndWhtSpaceFromStr( char *str ) Me@2: { int n; Me@2: Me@2: n = strlen ( str ); Me@2: while( --n >= 0 ) Me@2: { Me@2: if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r') Me@2: break; Me@2: } Me@2: str[n + 1] = '\0'; Me@2: } Me@2: Me@2: Me@2: ParamStruc * Me@0: makeParamFromStrs( char * type, char *value ) Me@0: { ParamStruc *retParam; Me@0: retParam = makeParamStruc(); Me@0: switch(*type) Me@0: { case 'i': Me@0: { retParam->type = INT_PARAM_TYPE; Me@0: retParam->intValue = atoi( value ); Me@0: } break; Me@0: case 's': Me@0: { retParam->type = STRING_PARAM_TYPE; Me@0: retParam->strValue = malloc( strlen(value) + 1); Me@0: strcpy( retParam->strValue, value ); Me@2: removeEndWhtSpaceFromStr( retParam->strValue ); Me@0: } break; Me@0: case 'f': Me@0: { retParam->type = FLOAT_PARAM_TYPE; Me@0: retParam->floatValue = atof( value ); Me@0: } break; Me@0: } Me@0: return retParam; Me@0: } Me@0: Me@2: Me@2: /* A pretty useless but good debugging function, Me@2: which simply displays the hashtable in (key.value) pairs Me@2: */ Me@2: /*void paramBagToString( ParamBag * bag ) Me@2: { int i; Me@2: ParamBagHashEntry *t; Me@2: for( i = 0; i < bag->bagSz; i++ ) Me@2: { t = entries[i]; Me@2: if( t == NULL ) Me@2: strcat_m( retStr, &"()" ); Me@2: else Me@2: { strcat_m( retStr, &"(" ); Me@2: for( ; t != NULL; t = t->next ) Me@2: { strcat_m( retStr, &" " ); Me@2: strcat_m( retStr, t->key ); Me@2: strcat_m( retStr, &"." ); Me@2: strcat_m( retStr, paramToString( t->param ) ); Me@2: strcat_m( retStr, &" " ); Me@2: } Me@2: strcat_m( retStr, &")" ); Me@2: } Me@2: } Me@2: } Me@2: */