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