Me@0: /* Me@0: * Copyright 2009 OpenSourceCodeStewardshipFoundation.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@0: void freeHashEntry( HashEntry *entry ); Me@0: char* strdup_m(char *o); Me@0: HashEntry * lookupKeyInHash( char *key, ParamBag * bag ); Me@0: unsigned int hashKey( 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@0: retBag->entries = malloc( HASHSIZE * sizeof( HashEntry *) ); Me@0: retBag->bagSz = 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@0: HashEntry ** 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@0: HashEntry * Me@0: lookupKeyInHash( char *key, ParamBag * bag ) Me@0: { unsigned int Me@0: hashIndex = hashKey( key, bag->bagSz ); Me@0: HashEntry* 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@0: { HashEntry *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@0: HashEntry* hashEntry; Me@0: hashEntry = lookupKeyInHash( key, bag ); Me@0: if( hashEntry == NULL ) Me@0: { hashIdx = hashKey( key, bag->bagSz ); Me@0: hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); Me@0: if( hashEntry == NULL ) return 0; Me@0: hashEntry->key = strdup_m( 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@0: char* Me@0: strdup_m( char *o ) Me@0: { int l = strlen(o)+1; Me@0: char *ns = (char*) malloc( l * sizeof(char) ); Me@0: strcpy( ns, o ); Me@0: return ns; Me@0: } Me@0: Me@0: /* A pretty useless but good debugging function, Me@0: which simply displays the hashtable in (key.value) pairs Me@0: */ Me@0: /*void paramBagToString( ParamBag * bag ) Me@0: { int i; Me@0: HashEntry *t; Me@0: for( i = 0; i < bag->bagSz; i++ ) Me@0: { t = entries[i]; Me@0: if( t == NULL ) Me@0: strcat_m( retStr, &"()" ); Me@0: else Me@0: { strcat_m( retStr, &"(" ); Me@0: for( ; t != NULL; t = t->next ) Me@0: { strcat_m( retStr, &" " ); Me@0: strcat_m( retStr, t->key ); Me@0: strcat_m( retStr, &"." ); Me@0: strcat_m( retStr, paramToString( t->param ) ); Me@0: strcat_m( retStr, &" " ); Me@0: } Me@0: strcat_m( retStr, &")" ); Me@0: } Me@0: } Me@0: } Me@0: */ Me@0: Me@0: Me@0: void Me@0: freeParamBag( ParamBag *bag ) Me@0: { int i; Me@0: HashEntry *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@0: freeHashEntry( hashEntry ); Me@0: hashEntry = temp; Me@0: } Me@0: } Me@0: } Me@0: } Me@0: Me@0: void Me@0: freeHashEntry( HashEntry *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@0: } Me@0: Me@0: 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@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: