# HG changeset patch # User Me # Date 1274583016 25200 # Node ID 481dd533f0e89fddcf587a9e4910c0b593b088e2 initial add diff -r 000000000000 -r 481dd533f0e8 Param.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Param.h Sat May 22 19:50:16 2010 -0700 @@ -0,0 +1,52 @@ +/* + * File: Param.h + * Author: SeanHalle@yahoo.com + * + * Created on November 19, 2009, 6:30 PM + */ + +#ifndef _PARAM_H +#define _PARAM_H + +typedef +struct + { int type; + int intValue; + char * strValue; + float floatValue; + } +ParamStruc; + +#define INT_PARAM_TYPE 0 +#define STRING_PARAM_TYPE 1 +#define FLOAT_PARAM_TYPE 2 + +#define HASHSIZE 101 + +typedef +struct _HashEntry + { + char *key; + ParamStruc *param; + struct _HashEntry *next; + } +HashEntry; + +typedef +struct + { int bagSz; + HashEntry* *entries; + } +ParamBag; + +ParamBag *makeParamBag(); +ParamStruc *getParamFromBag( char *key, ParamBag * bag ); +int addParamToBag( char* key, ParamStruc *param, ParamBag *bag ); +void freeParamBag( ParamBag *bag ); +//char *paramBagToString( ParamBag * bag ) +ParamStruc *makeParamStruc(); +ParamStruc *makeParamFromStrs( char * type, char *value ); +ssize_t getline( char **lineptr, size_t *n, FILE *stream ); + +#endif /* _PARAM_H */ + diff -r 000000000000 -r 481dd533f0e8 ParamBag.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ParamBag.c Sat May 22 19:50:16 2010 -0700 @@ -0,0 +1,195 @@ +/* + * Copyright 2009 OpenSourceCodeStewardshipFoundation.org + * Licensed under GNU General Public License version 2 + * + * Based on code posted to a discussion group on the web. (Forgot to mark + * down where got it from) + * + * Author: seanhalle@yahoo.com + * + * Created on November 14, 2009, 9:00 PM + */ +#include +#include +#include + +#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 nullOutParamBagHashEntries( ParamBag *bag ); + + ParamBag * +makeParamBag() + { ParamBag * retBag; + retBag = malloc( sizeof( ParamBag ) ); + retBag->entries = malloc( HASHSIZE * sizeof( HashEntry *) ); + retBag->bagSz = HASHSIZE; + nullOutParamBagHashEntries( retBag ); + + return retBag; + } + + void +nullOutParamBagHashEntries( ParamBag *bag ) + { int i, bagSz; + bagSz = bag->bagSz; + HashEntry ** entries = bag->entries; + for( i = 0; i < bagSz; i++ ) + entries[ i ] = NULL; + } + + unsigned int +hashKey( char *s, int hashSz ) + { unsigned int h = 0; + + for( ; *s != 0; s++ ) + h = *s + h*31; + return h % hashSz; + } + +/*Need this to be separated out, for use in both getParam and putParam + */ + HashEntry * +lookupKeyInHash( char *key, ParamBag * bag ) + { unsigned int + hashIndex = hashKey( key, bag->bagSz ); + HashEntry* + hashEntry = bag->entries[ hashIndex ]; + for( ; hashEntry != NULL; hashEntry = hashEntry->next ) + { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; + } + return NULL; + } + + ParamStruc * +getParamFromBag( char *key, ParamBag * bag ) + { HashEntry *entry; + entry = lookupKeyInHash( key, bag ); + if( entry == NULL ) return NULL; + + return entry->param; + } + + int +addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) + { unsigned int hashIdx; + HashEntry* hashEntry; + hashEntry = lookupKeyInHash( key, bag ); + if( hashEntry == NULL ) + { hashIdx = hashKey( key, bag->bagSz ); + hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); + if( hashEntry == NULL ) return 0; + hashEntry->key = strdup_m( key ); + if( hashEntry->key == NULL ) return 0; + hashEntry->next = (bag->entries)[hashIdx]; + (bag->entries)[hashIdx] = hashEntry; + } + else + { freeParamStruc( hashEntry->param ); + } + hashEntry->param = param; + 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; + + entries = bag->entries; + for( i=0; i < bag->bagSz; i++ ) + { if( entries[i] != NULL ) + { hashEntry = entries[i]; + while( hashEntry != NULL ) + { + temp = hashEntry->next; + freeHashEntry( hashEntry ); + hashEntry = temp; + } + } + } + } + + void +freeHashEntry( HashEntry *entry ) + { + freeParamStruc( entry->param ); + free( entry->key ); //was malloc'd above, so free it + free( entry ); + } + + void +freeParamStruc( ParamStruc * param ) + { if( param->type == STRING_PARAM_TYPE ) free( param->strValue ); + free( param ); + } + + ParamStruc * +makeParamStruc() + { ParamStruc *retStruc; + retStruc = malloc( sizeof( ParamStruc ) ); + retStruc->floatValue = 0.0; + retStruc->intValue = 0; + retStruc->strValue = NULL; + } + + ParamStruc * +makeParamFromStrs( char * type, char *value ) + { ParamStruc *retParam; + retParam = makeParamStruc(); + switch(*type) + { case 'i': + { retParam->type = INT_PARAM_TYPE; + retParam->intValue = atoi( value ); + } break; + case 's': + { retParam->type = STRING_PARAM_TYPE; + retParam->strValue = malloc( strlen(value) + 1); + strcpy( retParam->strValue, value ); + } break; + case 'f': + { retParam->type = FLOAT_PARAM_TYPE; + retParam->floatValue = atof( value ); + } break; + } + return retParam; + } + diff -r 000000000000 -r 481dd533f0e8 ReadParamsFromFile.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ReadParamsFromFile.c Sat May 22 19:50:16 2010 -0700 @@ -0,0 +1,109 @@ +/* + * Author: SeanHalle@yahoo.com + * + * Created on June 15, 2009, 10:12 AM + */ + +#include +#include +#include + +#include "../Matrix_Mult.h" + +ParamStruc * makeParamFromStrs( char * type, char *value ); + +#define _GNU_SOURCE + +/*Copied from gnu's win32 lib + */ + ssize_t +getline( char **lineptr, size_t *n, FILE *stream ) + { + if ( lineptr == NULL || n == NULL) return -1; + if (*lineptr == NULL || *n == 0) + { *n = 500; //max length of line in a config file + *lineptr = (char *) malloc( *n ); + if (*lineptr == NULL) return -1; + } + if( fgets( *lineptr, *n, stream ) ) + return *n; + else + return -1; + } + + void +readParamFileIntoBag( char *paramFileName, ParamBag * bag ) + { + size_t lineSz = 0; + FILE* paramFile; + char* line = NULL; + + char* paramType;// = malloc( 12 ); //"double" is the longest type + char* paramName;// = malloc( 500 ); //max of 500 chars in name + char* paramValue;// = malloc( 500 ); //max of 500 chars in value + + lineSz = 500; //max length of line in a config file + line = (char *) malloc( lineSz ); + if( line == NULL ) + { BLIS_DKU__throwError( "no mem for line" ); + return; + } + + + paramFile = fopen( paramFileName, "r" ); + fseek( paramFile, 0, SEEK_SET ); + while( !feof( paramFile ) ) + { while( getline( &line, &lineSz, paramFile ) != -1 ) + { + char *lineEnd = line + strlen(line) +1; + char *searchPos = line; + + if( *line == '\n') continue; //blank line + if( *line == '/' ) continue; //comment line + + //read the param type + paramType = line; //start of string + int foundIt = 0; + for( ; searchPos < lineEnd && !foundIt; searchPos++) + { if( *searchPos == ',' ) + { foundIt = 1; + *searchPos = 0; //mark end of string + } + } + //get rid of leading spaces + for( ; searchPos < lineEnd; searchPos++) + { if( *searchPos != ' ' ) break; + } + //read the param name + paramName = searchPos; + foundIt = 0; + for( ; searchPos < lineEnd && !foundIt; searchPos++) + { if( *searchPos == ',' ) + { foundIt = 1; + *searchPos = 0; //mark end + } + } + //get rid of leading spaces + for( ; searchPos < lineEnd; searchPos++) + { if( *searchPos != ' ' ) break; + } + //read the param value + paramValue = searchPos; + foundIt = 0; + for( ; searchPos < lineEnd && !foundIt; searchPos++) + { if( *searchPos == '\n' ) + { foundIt = 1; + *searchPos = 0; //mark end + } + } + if( foundIt ) + { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue ); + ParamStruc * + paramStruc = makeParamFromStrs( paramType, paramValue ); + addParamToBag( paramName, paramStruc, bag ); + } + } + } + free( line ); + } +