# HG changeset patch # User Me@portablequad # Date 1328647889 28800 # Node ID d46150af45adf8e814ea46adc8f928e79bb9ee8a # Parent a8744027c1a9fa2a573341e7dcfebc9085c1be20 Newly created project repository -- commit sub-states diff -r a8744027c1a9 -r d46150af45ad ParamBag.c --- a/ParamBag.c Fri Jan 27 15:51:11 2012 +0100 +++ b/ParamBag.c Tue Feb 07 12:51:29 2012 -0800 @@ -1,203 +1,203 @@ -/* - * Copyright 2009 OpenSourceStewardshipFoundation.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 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( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) ); - retBag->bagSz = PARAM_BAG_HASHSIZE; - nullOutParamBagHashEntries( retBag ); - - return retBag; - } - - void -nullOutParamBagHashEntries( ParamBag *bag ) - { int i, bagSz; - bagSz = bag->bagSz; - ParamBagHashEntry ** 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 - */ - ParamBagHashEntry * -lookupKeyInHash( char *key, ParamBag * bag ) - { unsigned int - hashIndex = hashKey( key, bag->bagSz ); - ParamBagHashEntry* - 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 ) - { ParamBagHashEntry *entry; - entry = lookupKeyInHash( key, bag ); - if( entry == NULL ) return NULL; - - return entry->param; - } - - int -addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) - { unsigned int hashIdx; - ParamBagHashEntry* hashEntry; - hashEntry = lookupKeyInHash( key, bag ); - if( hashEntry == NULL ) - { hashIdx = hashKey( key, bag->bagSz ); - hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) ); - if( hashEntry == NULL ) return 0; - hashEntry->key = strdup( 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; - } - - - void -freeParamBag( ParamBag *bag ) - { int i; - ParamBagHashEntry *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; - freeParamBagHashEntry( hashEntry ); - hashEntry = temp; - } - } - } - } - - void -freeParamBagHashEntry( ParamBagHashEntry *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; - - return retStruc; - } - -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(); - 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 ); - removeEndWhtSpaceFromStr( retParam->strValue ); - } break; - case 'f': - { retParam->type = FLOAT_PARAM_TYPE; - retParam->floatValue = atof( value ); - } break; - } - 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, &")" ); - } - } - } -*/ +/* + * Copyright 2009 OpenSourceStewardshipFoundation.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 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( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) ); + retBag->bagSz = PARAM_BAG_HASHSIZE; + nullOutParamBagHashEntries( retBag ); + + return retBag; + } + + void +nullOutParamBagHashEntries( ParamBag *bag ) + { int i, bagSz; + bagSz = bag->bagSz; + ParamBagHashEntry ** 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 + */ + ParamBagHashEntry * +lookupKeyInHash( char *key, ParamBag * bag ) + { unsigned int + hashIndex = hashKey( key, bag->bagSz ); + ParamBagHashEntry* + 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 ) + { ParamBagHashEntry *entry; + entry = lookupKeyInHash( key, bag ); + if( entry == NULL ) return NULL; + + return entry->param; + } + + int +addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) + { unsigned int hashIdx; + ParamBagHashEntry* hashEntry; + hashEntry = lookupKeyInHash( key, bag ); + if( hashEntry == NULL ) + { hashIdx = hashKey( key, bag->bagSz ); + hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) ); + if( hashEntry == NULL ) return 0; + hashEntry->key = strdup( 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; + } + + + void +freeParamBag( ParamBag *bag ) + { int i; + ParamBagHashEntry *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; + freeParamBagHashEntry( hashEntry ); + hashEntry = temp; + } + } + } + } + + void +freeParamBagHashEntry( ParamBagHashEntry *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; + + return retStruc; + } + +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(); + 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 ); + removeEndWhtSpaceFromStr( retParam->strValue ); + } break; + case 'f': + { retParam->type = FLOAT_PARAM_TYPE; + retParam->floatValue = atof( value ); + } break; + } + 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 a8744027c1a9 -r d46150af45ad ReadParamsFromFile.c --- a/ReadParamsFromFile.c Fri Jan 27 15:51:11 2012 +0100 +++ b/ReadParamsFromFile.c Tue Feb 07 12:51:29 2012 -0800 @@ -1,111 +1,111 @@ -/* - * Author: SeanHalle@yahoo.com - * - * Created on June 15, 2009, 10:12 AM - */ - -#include -#include -#include - -#include "Param.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 ) - { printf( "\nIn readParamFileIntoBag: no mem for line\n" ); - return; - } - - - paramFile = fopen( paramFileName, "r" ); - if( paramFile == NULL ) - { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); } - 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 ); - } - +/* + * Author: SeanHalle@yahoo.com + * + * Created on June 15, 2009, 10:12 AM + */ + +#include +#include +#include + +#include "Param.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 ) + { printf( "\nIn readParamFileIntoBag: no mem for line\n" ); + return; + } + + + paramFile = fopen( paramFileName, "r" ); + if( paramFile == NULL ) + { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); } + 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 ); + } +