# HG changeset patch # User Me # Date 1280348245 25200 # Node ID 8f6d8a258491425258da3764e986684e99209c19 # Parent 396fda650c303d83e53c1eddc4c5895b08558308 Not sure what changed but works diff -r 396fda650c30 -r 8f6d8a258491 Param.h --- a/Param.h Sat May 22 19:56:44 2010 -0700 +++ b/Param.h Wed Jul 28 13:17:25 2010 -0700 @@ -21,29 +21,33 @@ #define STRING_PARAM_TYPE 1 #define FLOAT_PARAM_TYPE 2 -#define HASHSIZE 101 +#define PARAM_BAG_HASHSIZE 1024 -typedef -struct _HashEntry +typedef struct _ParamBagHashEntry ParamBagHashEntry; + +struct _ParamBagHashEntry { - char *key; - ParamStruc *param; - struct _HashEntry *next; + char *key; + ParamStruc *param; + struct _ParamBagHashEntry *next; } -HashEntry; +/*ParamBagHashEntry*/; + typedef struct { int bagSz; - HashEntry* *entries; + ParamBagHashEntry* *entries; } ParamBag; + ParamBag *makeParamBag(); +void readParamFileIntoBag( char *paramFileName, ParamBag * bag ); ParamStruc *getParamFromBag( char *key, ParamBag * bag ); int addParamToBag( char* key, ParamStruc *param, ParamBag *bag ); void freeParamBag( ParamBag *bag ); -//char *paramBagToString( ParamBag * bag ) +//char *paramBagToString( ParamBag * bag ); ParamStruc *makeParamStruc(); ParamStruc *makeParamFromStrs( char * type, char *value ); ssize_t getline( char **lineptr, size_t *n, FILE *stream ); diff -r 396fda650c30 -r 8f6d8a258491 ParamBag.c --- a/ParamBag.c Sat May 22 19:56:44 2010 -0700 +++ b/ParamBag.c Wed Jul 28 13:17:25 2010 -0700 @@ -16,18 +16,17 @@ #include "Param.h" void freeParamStruc( ParamStruc * param ); -void freeHashEntry( HashEntry *entry ); -char* strdup_m(char *o); -HashEntry * lookupKeyInHash( char *key, ParamBag * bag ); -unsigned int hashKey( char *s, int hashSz ); +void freeParamBagHashEntry( ParamBagHashEntry *entry ); +ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag ); +unsigned int hashThisKey( char *s, int hashSz ); void nullOutParamBagHashEntries( ParamBag *bag ); ParamBag * makeParamBag() { ParamBag * retBag; retBag = malloc( sizeof( ParamBag ) ); - retBag->entries = malloc( HASHSIZE * sizeof( HashEntry *) ); - retBag->bagSz = HASHSIZE; + retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) ); + retBag->bagSz = PARAM_BAG_HASHSIZE; nullOutParamBagHashEntries( retBag ); return retBag; @@ -37,7 +36,7 @@ nullOutParamBagHashEntries( ParamBag *bag ) { int i, bagSz; bagSz = bag->bagSz; - HashEntry ** entries = bag->entries; + ParamBagHashEntry ** entries = bag->entries; for( i = 0; i < bagSz; i++ ) entries[ i ] = NULL; } @@ -53,11 +52,11 @@ /*Need this to be separated out, for use in both getParam and putParam */ - HashEntry * + ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag ) { unsigned int hashIndex = hashKey( key, bag->bagSz ); - HashEntry* + ParamBagHashEntry* hashEntry = bag->entries[ hashIndex ]; for( ; hashEntry != NULL; hashEntry = hashEntry->next ) { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; @@ -67,7 +66,7 @@ ParamStruc * getParamFromBag( char *key, ParamBag * bag ) - { HashEntry *entry; + { ParamBagHashEntry *entry; entry = lookupKeyInHash( key, bag ); if( entry == NULL ) return NULL; @@ -77,13 +76,13 @@ int addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) { unsigned int hashIdx; - HashEntry* hashEntry; + ParamBagHashEntry* hashEntry; hashEntry = lookupKeyInHash( key, bag ); if( hashEntry == NULL ) { hashIdx = hashKey( key, bag->bagSz ); - hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); + hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) ); if( hashEntry == NULL ) return 0; - hashEntry->key = strdup_m( key ); + hashEntry->key = strdup( key ); if( hashEntry->key == NULL ) return 0; hashEntry->next = (bag->entries)[hashIdx]; (bag->entries)[hashIdx] = hashEntry; @@ -95,44 +94,11 @@ return 1; } - char* -strdup_m( char *o ) - { int l = strlen(o)+1; - char *ns = (char*) malloc( l * sizeof(char) ); - strcpy( ns, o ); - return ns; - } - -/* A pretty useless but good debugging function, - which simply displays the hashtable in (key.value) pairs -*/ -/*void paramBagToString( ParamBag * bag ) - { int i; - HashEntry *t; - for( i = 0; i < bag->bagSz; i++ ) - { t = entries[i]; - if( t == NULL ) - strcat_m( retStr, &"()" ); - else - { strcat_m( retStr, &"(" ); - for( ; t != NULL; t = t->next ) - { strcat_m( retStr, &" " ); - strcat_m( retStr, t->key ); - strcat_m( retStr, &"." ); - strcat_m( retStr, paramToString( t->param ) ); - strcat_m( retStr, &" " ); - } - strcat_m( retStr, &")" ); - } - } - } -*/ - - + void freeParamBag( ParamBag *bag ) { int i; - HashEntry *hashEntry, *temp, **entries; + ParamBagHashEntry *hashEntry, *temp, **entries; entries = bag->entries; for( i=0; i < bag->bagSz; i++ ) @@ -141,7 +107,7 @@ while( hashEntry != NULL ) { temp = hashEntry->next; - freeHashEntry( hashEntry ); + freeParamBagHashEntry( hashEntry ); hashEntry = temp; } } @@ -149,7 +115,7 @@ } void -freeHashEntry( HashEntry *entry ) +freeParamBagHashEntry( ParamBagHashEntry *entry ) { freeParamStruc( entry->param ); free( entry->key ); //was malloc'd above, so free it @@ -169,9 +135,25 @@ retStruc->floatValue = 0.0; retStruc->intValue = 0; retStruc->strValue = NULL; + + return retStruc; } - ParamStruc * +void +removeEndWhtSpaceFromStr( char *str ) + { int n; + + n = strlen ( str ); + while( --n >= 0 ) + { + if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r') + break; + } + str[n + 1] = '\0'; + } + + +ParamStruc * makeParamFromStrs( char * type, char *value ) { ParamStruc *retParam; retParam = makeParamStruc(); @@ -184,6 +166,7 @@ { retParam->type = STRING_PARAM_TYPE; retParam->strValue = malloc( strlen(value) + 1); strcpy( retParam->strValue, value ); + removeEndWhtSpaceFromStr( retParam->strValue ); } break; case 'f': { retParam->type = FLOAT_PARAM_TYPE; @@ -193,3 +176,28 @@ return retParam; } + +/* A pretty useless but good debugging function, + which simply displays the hashtable in (key.value) pairs +*/ +/*void paramBagToString( ParamBag * bag ) + { int i; + ParamBagHashEntry *t; + for( i = 0; i < bag->bagSz; i++ ) + { t = entries[i]; + if( t == NULL ) + strcat_m( retStr, &"()" ); + else + { strcat_m( retStr, &"(" ); + for( ; t != NULL; t = t->next ) + { strcat_m( retStr, &" " ); + strcat_m( retStr, t->key ); + strcat_m( retStr, &"." ); + strcat_m( retStr, paramToString( t->param ) ); + strcat_m( retStr, &" " ); + } + strcat_m( retStr, &")" ); + } + } + } +*/ diff -r 396fda650c30 -r 8f6d8a258491 ReadParamsFromFile.c --- a/ReadParamsFromFile.c Sat May 22 19:56:44 2010 -0700 +++ b/ReadParamsFromFile.c Wed Jul 28 13:17:25 2010 -0700 @@ -45,12 +45,13 @@ lineSz = 500; //max length of line in a config file line = (char *) malloc( lineSz ); if( line == NULL ) - { BLIS_DKU__throwError( "no mem for line" ); + { printf( "\nIn readParamFileIntoBag: no mem for line\n" ); return; } paramFile = fopen( paramFileName, "r" ); + if( paramFile == NULL ) {printf("\ncouldn't open file\n"); exit(0);} fseek( paramFile, 0, SEEK_SET ); while( !feof( paramFile ) ) { while( getline( &line, &lineSz, paramFile ) != -1 )