Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > ParamHelper
changeset 0:481dd533f0e8
initial add
| author | Me |
|---|---|
| date | Sat, 22 May 2010 19:50:16 -0700 |
| parents | |
| children | 396fda650c30 |
| files | Param.h ParamBag.c ReadParamsFromFile.c |
| diffstat | 3 files changed, 356 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Param.h Sat May 22 19:50:16 2010 -0700 1.3 @@ -0,0 +1,52 @@ 1.4 +/* 1.5 + * File: Param.h 1.6 + * Author: SeanHalle@yahoo.com 1.7 + * 1.8 + * Created on November 19, 2009, 6:30 PM 1.9 + */ 1.10 + 1.11 +#ifndef _PARAM_H 1.12 +#define _PARAM_H 1.13 + 1.14 +typedef 1.15 +struct 1.16 + { int type; 1.17 + int intValue; 1.18 + char * strValue; 1.19 + float floatValue; 1.20 + } 1.21 +ParamStruc; 1.22 + 1.23 +#define INT_PARAM_TYPE 0 1.24 +#define STRING_PARAM_TYPE 1 1.25 +#define FLOAT_PARAM_TYPE 2 1.26 + 1.27 +#define HASHSIZE 101 1.28 + 1.29 +typedef 1.30 +struct _HashEntry 1.31 + { 1.32 + char *key; 1.33 + ParamStruc *param; 1.34 + struct _HashEntry *next; 1.35 + } 1.36 +HashEntry; 1.37 + 1.38 +typedef 1.39 +struct 1.40 + { int bagSz; 1.41 + HashEntry* *entries; 1.42 + } 1.43 +ParamBag; 1.44 + 1.45 +ParamBag *makeParamBag(); 1.46 +ParamStruc *getParamFromBag( char *key, ParamBag * bag ); 1.47 +int addParamToBag( char* key, ParamStruc *param, ParamBag *bag ); 1.48 +void freeParamBag( ParamBag *bag ); 1.49 +//char *paramBagToString( ParamBag * bag ) 1.50 +ParamStruc *makeParamStruc(); 1.51 +ParamStruc *makeParamFromStrs( char * type, char *value ); 1.52 +ssize_t getline( char **lineptr, size_t *n, FILE *stream ); 1.53 + 1.54 +#endif /* _PARAM_H */ 1.55 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/ParamBag.c Sat May 22 19:50:16 2010 -0700 2.3 @@ -0,0 +1,195 @@ 2.4 +/* 2.5 + * Copyright 2009 OpenSourceCodeStewardshipFoundation.org 2.6 + * Licensed under GNU General Public License version 2 2.7 + * 2.8 + * Based on code posted to a discussion group on the web. (Forgot to mark 2.9 + * down where got it from) 2.10 + * 2.11 + * Author: seanhalle@yahoo.com 2.12 + * 2.13 + * Created on November 14, 2009, 9:00 PM 2.14 + */ 2.15 +#include <string.h> 2.16 +#include <stdio.h> 2.17 +#include <stdlib.h> 2.18 + 2.19 +#include "Param.h" 2.20 + 2.21 +void freeParamStruc( ParamStruc * param ); 2.22 +void freeHashEntry( HashEntry *entry ); 2.23 +char* strdup_m(char *o); 2.24 +HashEntry * lookupKeyInHash( char *key, ParamBag * bag ); 2.25 +unsigned int hashKey( char *s, int hashSz ); 2.26 +void nullOutParamBagHashEntries( ParamBag *bag ); 2.27 + 2.28 + ParamBag * 2.29 +makeParamBag() 2.30 + { ParamBag * retBag; 2.31 + retBag = malloc( sizeof( ParamBag ) ); 2.32 + retBag->entries = malloc( HASHSIZE * sizeof( HashEntry *) ); 2.33 + retBag->bagSz = HASHSIZE; 2.34 + nullOutParamBagHashEntries( retBag ); 2.35 + 2.36 + return retBag; 2.37 + } 2.38 + 2.39 + void 2.40 +nullOutParamBagHashEntries( ParamBag *bag ) 2.41 + { int i, bagSz; 2.42 + bagSz = bag->bagSz; 2.43 + HashEntry ** entries = bag->entries; 2.44 + for( i = 0; i < bagSz; i++ ) 2.45 + entries[ i ] = NULL; 2.46 + } 2.47 + 2.48 + unsigned int 2.49 +hashKey( char *s, int hashSz ) 2.50 + { unsigned int h = 0; 2.51 + 2.52 + for( ; *s != 0; s++ ) 2.53 + h = *s + h*31; 2.54 + return h % hashSz; 2.55 + } 2.56 + 2.57 +/*Need this to be separated out, for use in both getParam and putParam 2.58 + */ 2.59 + HashEntry * 2.60 +lookupKeyInHash( char *key, ParamBag * bag ) 2.61 + { unsigned int 2.62 + hashIndex = hashKey( key, bag->bagSz ); 2.63 + HashEntry* 2.64 + hashEntry = bag->entries[ hashIndex ]; 2.65 + for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 2.66 + { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; 2.67 + } 2.68 + return NULL; 2.69 + } 2.70 + 2.71 + ParamStruc * 2.72 +getParamFromBag( char *key, ParamBag * bag ) 2.73 + { HashEntry *entry; 2.74 + entry = lookupKeyInHash( key, bag ); 2.75 + if( entry == NULL ) return NULL; 2.76 + 2.77 + return entry->param; 2.78 + } 2.79 + 2.80 + int 2.81 +addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) 2.82 + { unsigned int hashIdx; 2.83 + HashEntry* hashEntry; 2.84 + hashEntry = lookupKeyInHash( key, bag ); 2.85 + if( hashEntry == NULL ) 2.86 + { hashIdx = hashKey( key, bag->bagSz ); 2.87 + hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); 2.88 + if( hashEntry == NULL ) return 0; 2.89 + hashEntry->key = strdup_m( key ); 2.90 + if( hashEntry->key == NULL ) return 0; 2.91 + hashEntry->next = (bag->entries)[hashIdx]; 2.92 + (bag->entries)[hashIdx] = hashEntry; 2.93 + } 2.94 + else 2.95 + { freeParamStruc( hashEntry->param ); 2.96 + } 2.97 + hashEntry->param = param; 2.98 + return 1; 2.99 + } 2.100 + 2.101 + char* 2.102 +strdup_m( char *o ) 2.103 + { int l = strlen(o)+1; 2.104 + char *ns = (char*) malloc( l * sizeof(char) ); 2.105 + strcpy( ns, o ); 2.106 + return ns; 2.107 + } 2.108 + 2.109 +/* A pretty useless but good debugging function, 2.110 + which simply displays the hashtable in (key.value) pairs 2.111 +*/ 2.112 +/*void paramBagToString( ParamBag * bag ) 2.113 + { int i; 2.114 + HashEntry *t; 2.115 + for( i = 0; i < bag->bagSz; i++ ) 2.116 + { t = entries[i]; 2.117 + if( t == NULL ) 2.118 + strcat_m( retStr, &"()" ); 2.119 + else 2.120 + { strcat_m( retStr, &"(" ); 2.121 + for( ; t != NULL; t = t->next ) 2.122 + { strcat_m( retStr, &" " ); 2.123 + strcat_m( retStr, t->key ); 2.124 + strcat_m( retStr, &"." ); 2.125 + strcat_m( retStr, paramToString( t->param ) ); 2.126 + strcat_m( retStr, &" " ); 2.127 + } 2.128 + strcat_m( retStr, &")" ); 2.129 + } 2.130 + } 2.131 + } 2.132 +*/ 2.133 + 2.134 + 2.135 + void 2.136 +freeParamBag( ParamBag *bag ) 2.137 + { int i; 2.138 + HashEntry *hashEntry, *temp, **entries; 2.139 + 2.140 + entries = bag->entries; 2.141 + for( i=0; i < bag->bagSz; i++ ) 2.142 + { if( entries[i] != NULL ) 2.143 + { hashEntry = entries[i]; 2.144 + while( hashEntry != NULL ) 2.145 + { 2.146 + temp = hashEntry->next; 2.147 + freeHashEntry( hashEntry ); 2.148 + hashEntry = temp; 2.149 + } 2.150 + } 2.151 + } 2.152 + } 2.153 + 2.154 + void 2.155 +freeHashEntry( HashEntry *entry ) 2.156 + { 2.157 + freeParamStruc( entry->param ); 2.158 + free( entry->key ); //was malloc'd above, so free it 2.159 + free( entry ); 2.160 + } 2.161 + 2.162 + void 2.163 +freeParamStruc( ParamStruc * param ) 2.164 + { if( param->type == STRING_PARAM_TYPE ) free( param->strValue ); 2.165 + free( param ); 2.166 + } 2.167 + 2.168 + ParamStruc * 2.169 +makeParamStruc() 2.170 + { ParamStruc *retStruc; 2.171 + retStruc = malloc( sizeof( ParamStruc ) ); 2.172 + retStruc->floatValue = 0.0; 2.173 + retStruc->intValue = 0; 2.174 + retStruc->strValue = NULL; 2.175 + } 2.176 + 2.177 + ParamStruc * 2.178 +makeParamFromStrs( char * type, char *value ) 2.179 + { ParamStruc *retParam; 2.180 + retParam = makeParamStruc(); 2.181 + switch(*type) 2.182 + { case 'i': 2.183 + { retParam->type = INT_PARAM_TYPE; 2.184 + retParam->intValue = atoi( value ); 2.185 + } break; 2.186 + case 's': 2.187 + { retParam->type = STRING_PARAM_TYPE; 2.188 + retParam->strValue = malloc( strlen(value) + 1); 2.189 + strcpy( retParam->strValue, value ); 2.190 + } break; 2.191 + case 'f': 2.192 + { retParam->type = FLOAT_PARAM_TYPE; 2.193 + retParam->floatValue = atof( value ); 2.194 + } break; 2.195 + } 2.196 + return retParam; 2.197 + } 2.198 +
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/ReadParamsFromFile.c Sat May 22 19:50:16 2010 -0700 3.3 @@ -0,0 +1,109 @@ 3.4 +/* 3.5 + * Author: SeanHalle@yahoo.com 3.6 + * 3.7 + * Created on June 15, 2009, 10:12 AM 3.8 + */ 3.9 + 3.10 +#include <stdio.h> 3.11 +#include <stdlib.h> 3.12 +#include <string.h> 3.13 + 3.14 +#include "../Matrix_Mult.h" 3.15 + 3.16 +ParamStruc * makeParamFromStrs( char * type, char *value ); 3.17 + 3.18 +#define _GNU_SOURCE 3.19 + 3.20 +/*Copied from gnu's win32 lib 3.21 + */ 3.22 + ssize_t 3.23 +getline( char **lineptr, size_t *n, FILE *stream ) 3.24 + { 3.25 + if ( lineptr == NULL || n == NULL) return -1; 3.26 + if (*lineptr == NULL || *n == 0) 3.27 + { *n = 500; //max length of line in a config file 3.28 + *lineptr = (char *) malloc( *n ); 3.29 + if (*lineptr == NULL) return -1; 3.30 + } 3.31 + if( fgets( *lineptr, *n, stream ) ) 3.32 + return *n; 3.33 + else 3.34 + return -1; 3.35 + } 3.36 + 3.37 + void 3.38 +readParamFileIntoBag( char *paramFileName, ParamBag * bag ) 3.39 + { 3.40 + size_t lineSz = 0; 3.41 + FILE* paramFile; 3.42 + char* line = NULL; 3.43 + 3.44 + char* paramType;// = malloc( 12 ); //"double" is the longest type 3.45 + char* paramName;// = malloc( 500 ); //max of 500 chars in name 3.46 + char* paramValue;// = malloc( 500 ); //max of 500 chars in value 3.47 + 3.48 + lineSz = 500; //max length of line in a config file 3.49 + line = (char *) malloc( lineSz ); 3.50 + if( line == NULL ) 3.51 + { BLIS_DKU__throwError( "no mem for line" ); 3.52 + return; 3.53 + } 3.54 + 3.55 + 3.56 + paramFile = fopen( paramFileName, "r" ); 3.57 + fseek( paramFile, 0, SEEK_SET ); 3.58 + while( !feof( paramFile ) ) 3.59 + { while( getline( &line, &lineSz, paramFile ) != -1 ) 3.60 + { 3.61 + char *lineEnd = line + strlen(line) +1; 3.62 + char *searchPos = line; 3.63 + 3.64 + if( *line == '\n') continue; //blank line 3.65 + if( *line == '/' ) continue; //comment line 3.66 + 3.67 + //read the param type 3.68 + paramType = line; //start of string 3.69 + int foundIt = 0; 3.70 + for( ; searchPos < lineEnd && !foundIt; searchPos++) 3.71 + { if( *searchPos == ',' ) 3.72 + { foundIt = 1; 3.73 + *searchPos = 0; //mark end of string 3.74 + } 3.75 + } 3.76 + //get rid of leading spaces 3.77 + for( ; searchPos < lineEnd; searchPos++) 3.78 + { if( *searchPos != ' ' ) break; 3.79 + } 3.80 + //read the param name 3.81 + paramName = searchPos; 3.82 + foundIt = 0; 3.83 + for( ; searchPos < lineEnd && !foundIt; searchPos++) 3.84 + { if( *searchPos == ',' ) 3.85 + { foundIt = 1; 3.86 + *searchPos = 0; //mark end 3.87 + } 3.88 + } 3.89 + //get rid of leading spaces 3.90 + for( ; searchPos < lineEnd; searchPos++) 3.91 + { if( *searchPos != ' ' ) break; 3.92 + } 3.93 + //read the param value 3.94 + paramValue = searchPos; 3.95 + foundIt = 0; 3.96 + for( ; searchPos < lineEnd && !foundIt; searchPos++) 3.97 + { if( *searchPos == '\n' ) 3.98 + { foundIt = 1; 3.99 + *searchPos = 0; //mark end 3.100 + } 3.101 + } 3.102 + if( foundIt ) 3.103 + { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue ); 3.104 + ParamStruc * 3.105 + paramStruc = makeParamFromStrs( paramType, paramValue ); 3.106 + addParamToBag( paramName, paramStruc, bag ); 3.107 + } 3.108 + } 3.109 + } 3.110 + free( line ); 3.111 + } 3.112 +
