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