Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VReo > Reo__Matrix_Mult
diff ParamHelper/ParamBag.c @ 0:c4b1849c05ef
init add
| author | Sean Halle <seanhalle@yahoo.com> |
|---|---|
| date | Sun, 02 Feb 2014 17:58:41 -0800 |
| parents | |
| children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ParamHelper/ParamBag.c Sun Feb 02 17:58:41 2014 -0800 1.3 @@ -0,0 +1,203 @@ 1.4 +/* 1.5 + * Copyright 2009 OpenSourceStewardshipFoundation.org 1.6 + * Licensed under GNU General Public License version 2 1.7 + * 1.8 + * Based on code posted to a discussion group on the web. (Forgot to mark 1.9 + * down where got it from) 1.10 + * 1.11 + * Author: seanhalle@yahoo.com 1.12 + * 1.13 + * Created on November 14, 2009, 9:00 PM 1.14 + */ 1.15 +#include <string.h> 1.16 +#include <stdio.h> 1.17 +#include <stdlib.h> 1.18 + 1.19 +#include "Param.h" 1.20 + 1.21 +void freeParamStruc( ParamStruc * param ); 1.22 +void freeParamBagHashEntry( ParamBagHashEntry *entry ); 1.23 +ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag ); 1.24 +unsigned int hashThisKey( char *s, int hashSz ); 1.25 +void nullOutParamBagHashEntries( ParamBag *bag ); 1.26 + 1.27 + ParamBag * 1.28 +makeParamBag() 1.29 + { ParamBag * retBag; 1.30 + retBag = malloc( sizeof( ParamBag ) ); 1.31 + retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) ); 1.32 + retBag->bagSz = PARAM_BAG_HASHSIZE; 1.33 + nullOutParamBagHashEntries( retBag ); 1.34 + 1.35 + return retBag; 1.36 + } 1.37 + 1.38 + void 1.39 +nullOutParamBagHashEntries( ParamBag *bag ) 1.40 + { int i, bagSz; 1.41 + bagSz = bag->bagSz; 1.42 + ParamBagHashEntry ** entries = bag->entries; 1.43 + for( i = 0; i < bagSz; i++ ) 1.44 + entries[ i ] = NULL; 1.45 + } 1.46 + 1.47 + unsigned int 1.48 +hashKey( char *s, int hashSz ) 1.49 + { unsigned int h = 0; 1.50 + 1.51 + for( ; *s != 0; s++ ) 1.52 + h = *s + h*31; 1.53 + return h % hashSz; 1.54 + } 1.55 + 1.56 +/*Need this to be separated out, for use in both getParam and putParam 1.57 + */ 1.58 + ParamBagHashEntry * 1.59 +lookupKeyInHash( char *key, ParamBag * bag ) 1.60 + { unsigned int 1.61 + hashIndex = hashKey( key, bag->bagSz ); 1.62 + ParamBagHashEntry* 1.63 + hashEntry = bag->entries[ hashIndex ]; 1.64 + for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 1.65 + { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; 1.66 + } 1.67 + return NULL; 1.68 + } 1.69 + 1.70 + ParamStruc * 1.71 +getParamFromBag( char *key, ParamBag * bag ) 1.72 + { ParamBagHashEntry *entry; 1.73 + entry = lookupKeyInHash( key, bag ); 1.74 + if( entry == NULL ) return NULL; 1.75 + 1.76 + return entry->param; 1.77 + } 1.78 + 1.79 + int 1.80 +addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) 1.81 + { unsigned int hashIdx; 1.82 + ParamBagHashEntry* hashEntry; 1.83 + hashEntry = lookupKeyInHash( key, bag ); 1.84 + if( hashEntry == NULL ) 1.85 + { hashIdx = hashKey( key, bag->bagSz ); 1.86 + hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) ); 1.87 + if( hashEntry == NULL ) return 0; 1.88 + hashEntry->key = strdup( key ); 1.89 + if( hashEntry->key == NULL ) return 0; 1.90 + hashEntry->next = (bag->entries)[hashIdx]; 1.91 + (bag->entries)[hashIdx] = hashEntry; 1.92 + } 1.93 + else 1.94 + { freeParamStruc( hashEntry->param ); 1.95 + } 1.96 + hashEntry->param = param; 1.97 + return 1; 1.98 + } 1.99 + 1.100 + 1.101 + void 1.102 +freeParamBag( ParamBag *bag ) 1.103 + { int i; 1.104 + ParamBagHashEntry *hashEntry, *temp, **entries; 1.105 + 1.106 + entries = bag->entries; 1.107 + for( i=0; i < bag->bagSz; i++ ) 1.108 + { if( entries[i] != NULL ) 1.109 + { hashEntry = entries[i]; 1.110 + while( hashEntry != NULL ) 1.111 + { 1.112 + temp = hashEntry->next; 1.113 + freeParamBagHashEntry( hashEntry ); 1.114 + hashEntry = temp; 1.115 + } 1.116 + } 1.117 + } 1.118 + } 1.119 + 1.120 + void 1.121 +freeParamBagHashEntry( ParamBagHashEntry *entry ) 1.122 + { 1.123 + freeParamStruc( entry->param ); 1.124 + free( entry->key ); //was malloc'd above, so free it 1.125 + free( entry ); 1.126 + } 1.127 + 1.128 + void 1.129 +freeParamStruc( ParamStruc * param ) 1.130 + { if( param->type == STRING_PARAM_TYPE ) free( param->strValue ); 1.131 + free( param ); 1.132 + } 1.133 + 1.134 + ParamStruc * 1.135 +makeParamStruc() 1.136 + { ParamStruc *retStruc; 1.137 + retStruc = malloc( sizeof( ParamStruc ) ); 1.138 + retStruc->floatValue = 0.0; 1.139 + retStruc->intValue = 0; 1.140 + retStruc->strValue = NULL; 1.141 + 1.142 + return retStruc; 1.143 + } 1.144 + 1.145 +void 1.146 +removeEndWhtSpaceFromStr( char *str ) 1.147 + { int n; 1.148 + 1.149 + n = strlen ( str ); 1.150 + while( --n >= 0 ) 1.151 + { 1.152 + if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r') 1.153 + break; 1.154 + } 1.155 + str[n + 1] = '\0'; 1.156 + } 1.157 + 1.158 + 1.159 +ParamStruc * 1.160 +makeParamFromStrs( char * type, char *value ) 1.161 + { ParamStruc *retParam; 1.162 + retParam = makeParamStruc(); 1.163 + switch(*type) 1.164 + { case 'i': 1.165 + { retParam->type = INT_PARAM_TYPE; 1.166 + retParam->intValue = atoi( value ); 1.167 + } break; 1.168 + case 's': 1.169 + { retParam->type = STRING_PARAM_TYPE; 1.170 + retParam->strValue = malloc( strlen(value) + 1); 1.171 + strcpy( retParam->strValue, value ); 1.172 + removeEndWhtSpaceFromStr( retParam->strValue ); 1.173 + } break; 1.174 + case 'f': 1.175 + { retParam->type = FLOAT_PARAM_TYPE; 1.176 + retParam->floatValue = atof( value ); 1.177 + } break; 1.178 + } 1.179 + return retParam; 1.180 + } 1.181 + 1.182 + 1.183 +/* A pretty useless but good debugging function, 1.184 + which simply displays the hashtable in (key.value) pairs 1.185 +*/ 1.186 +/*void paramBagToString( ParamBag * bag ) 1.187 + { int i; 1.188 + ParamBagHashEntry *t; 1.189 + for( i = 0; i < bag->bagSz; i++ ) 1.190 + { t = entries[i]; 1.191 + if( t == NULL ) 1.192 + strcat_m( retStr, &"()" ); 1.193 + else 1.194 + { strcat_m( retStr, &"(" ); 1.195 + for( ; t != NULL; t = t->next ) 1.196 + { strcat_m( retStr, &" " ); 1.197 + strcat_m( retStr, t->key ); 1.198 + strcat_m( retStr, &"." ); 1.199 + strcat_m( retStr, paramToString( t->param ) ); 1.200 + strcat_m( retStr, &" " ); 1.201 + } 1.202 + strcat_m( retStr, &")" ); 1.203 + } 1.204 + } 1.205 + } 1.206 +*/
