Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > ParamHelper
diff ParamBag.c @ 2:8f6d8a258491
Not sure what changed but works
| author | Me |
|---|---|
| date | Wed, 28 Jul 2010 13:17:25 -0700 |
| parents | 481dd533f0e8 |
| children | cf0a327945e6 |
line diff
1.1 --- a/ParamBag.c Sat May 22 19:56:44 2010 -0700 1.2 +++ b/ParamBag.c Wed Jul 28 13:17:25 2010 -0700 1.3 @@ -16,18 +16,17 @@ 1.4 #include "Param.h" 1.5 1.6 void freeParamStruc( ParamStruc * param ); 1.7 -void freeHashEntry( HashEntry *entry ); 1.8 -char* strdup_m(char *o); 1.9 -HashEntry * lookupKeyInHash( char *key, ParamBag * bag ); 1.10 -unsigned int hashKey( char *s, int hashSz ); 1.11 +void freeParamBagHashEntry( ParamBagHashEntry *entry ); 1.12 +ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag ); 1.13 +unsigned int hashThisKey( char *s, int hashSz ); 1.14 void nullOutParamBagHashEntries( ParamBag *bag ); 1.15 1.16 ParamBag * 1.17 makeParamBag() 1.18 { ParamBag * retBag; 1.19 retBag = malloc( sizeof( ParamBag ) ); 1.20 - retBag->entries = malloc( HASHSIZE * sizeof( HashEntry *) ); 1.21 - retBag->bagSz = HASHSIZE; 1.22 + retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) ); 1.23 + retBag->bagSz = PARAM_BAG_HASHSIZE; 1.24 nullOutParamBagHashEntries( retBag ); 1.25 1.26 return retBag; 1.27 @@ -37,7 +36,7 @@ 1.28 nullOutParamBagHashEntries( ParamBag *bag ) 1.29 { int i, bagSz; 1.30 bagSz = bag->bagSz; 1.31 - HashEntry ** entries = bag->entries; 1.32 + ParamBagHashEntry ** entries = bag->entries; 1.33 for( i = 0; i < bagSz; i++ ) 1.34 entries[ i ] = NULL; 1.35 } 1.36 @@ -53,11 +52,11 @@ 1.37 1.38 /*Need this to be separated out, for use in both getParam and putParam 1.39 */ 1.40 - HashEntry * 1.41 + ParamBagHashEntry * 1.42 lookupKeyInHash( char *key, ParamBag * bag ) 1.43 { unsigned int 1.44 hashIndex = hashKey( key, bag->bagSz ); 1.45 - HashEntry* 1.46 + ParamBagHashEntry* 1.47 hashEntry = bag->entries[ hashIndex ]; 1.48 for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 1.49 { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; 1.50 @@ -67,7 +66,7 @@ 1.51 1.52 ParamStruc * 1.53 getParamFromBag( char *key, ParamBag * bag ) 1.54 - { HashEntry *entry; 1.55 + { ParamBagHashEntry *entry; 1.56 entry = lookupKeyInHash( key, bag ); 1.57 if( entry == NULL ) return NULL; 1.58 1.59 @@ -77,13 +76,13 @@ 1.60 int 1.61 addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) 1.62 { unsigned int hashIdx; 1.63 - HashEntry* hashEntry; 1.64 + ParamBagHashEntry* hashEntry; 1.65 hashEntry = lookupKeyInHash( key, bag ); 1.66 if( hashEntry == NULL ) 1.67 { hashIdx = hashKey( key, bag->bagSz ); 1.68 - hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); 1.69 + hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) ); 1.70 if( hashEntry == NULL ) return 0; 1.71 - hashEntry->key = strdup_m( key ); 1.72 + hashEntry->key = strdup( key ); 1.73 if( hashEntry->key == NULL ) return 0; 1.74 hashEntry->next = (bag->entries)[hashIdx]; 1.75 (bag->entries)[hashIdx] = hashEntry; 1.76 @@ -95,44 +94,11 @@ 1.77 return 1; 1.78 } 1.79 1.80 - char* 1.81 -strdup_m( char *o ) 1.82 - { int l = strlen(o)+1; 1.83 - char *ns = (char*) malloc( l * sizeof(char) ); 1.84 - strcpy( ns, o ); 1.85 - return ns; 1.86 - } 1.87 - 1.88 -/* A pretty useless but good debugging function, 1.89 - which simply displays the hashtable in (key.value) pairs 1.90 -*/ 1.91 -/*void paramBagToString( ParamBag * bag ) 1.92 - { int i; 1.93 - HashEntry *t; 1.94 - for( i = 0; i < bag->bagSz; i++ ) 1.95 - { t = entries[i]; 1.96 - if( t == NULL ) 1.97 - strcat_m( retStr, &"()" ); 1.98 - else 1.99 - { strcat_m( retStr, &"(" ); 1.100 - for( ; t != NULL; t = t->next ) 1.101 - { strcat_m( retStr, &" " ); 1.102 - strcat_m( retStr, t->key ); 1.103 - strcat_m( retStr, &"." ); 1.104 - strcat_m( retStr, paramToString( t->param ) ); 1.105 - strcat_m( retStr, &" " ); 1.106 - } 1.107 - strcat_m( retStr, &")" ); 1.108 - } 1.109 - } 1.110 - } 1.111 -*/ 1.112 - 1.113 - 1.114 + 1.115 void 1.116 freeParamBag( ParamBag *bag ) 1.117 { int i; 1.118 - HashEntry *hashEntry, *temp, **entries; 1.119 + ParamBagHashEntry *hashEntry, *temp, **entries; 1.120 1.121 entries = bag->entries; 1.122 for( i=0; i < bag->bagSz; i++ ) 1.123 @@ -141,7 +107,7 @@ 1.124 while( hashEntry != NULL ) 1.125 { 1.126 temp = hashEntry->next; 1.127 - freeHashEntry( hashEntry ); 1.128 + freeParamBagHashEntry( hashEntry ); 1.129 hashEntry = temp; 1.130 } 1.131 } 1.132 @@ -149,7 +115,7 @@ 1.133 } 1.134 1.135 void 1.136 -freeHashEntry( HashEntry *entry ) 1.137 +freeParamBagHashEntry( ParamBagHashEntry *entry ) 1.138 { 1.139 freeParamStruc( entry->param ); 1.140 free( entry->key ); //was malloc'd above, so free it 1.141 @@ -169,9 +135,25 @@ 1.142 retStruc->floatValue = 0.0; 1.143 retStruc->intValue = 0; 1.144 retStruc->strValue = NULL; 1.145 + 1.146 + return retStruc; 1.147 } 1.148 1.149 - ParamStruc * 1.150 +void 1.151 +removeEndWhtSpaceFromStr( char *str ) 1.152 + { int n; 1.153 + 1.154 + n = strlen ( str ); 1.155 + while( --n >= 0 ) 1.156 + { 1.157 + if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r') 1.158 + break; 1.159 + } 1.160 + str[n + 1] = '\0'; 1.161 + } 1.162 + 1.163 + 1.164 +ParamStruc * 1.165 makeParamFromStrs( char * type, char *value ) 1.166 { ParamStruc *retParam; 1.167 retParam = makeParamStruc(); 1.168 @@ -184,6 +166,7 @@ 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 @@ -193,3 +176,28 @@ 1.177 return retParam; 1.178 } 1.179 1.180 + 1.181 +/* A pretty useless but good debugging function, 1.182 + which simply displays the hashtable in (key.value) pairs 1.183 +*/ 1.184 +/*void paramBagToString( ParamBag * bag ) 1.185 + { int i; 1.186 + ParamBagHashEntry *t; 1.187 + for( i = 0; i < bag->bagSz; i++ ) 1.188 + { t = entries[i]; 1.189 + if( t == NULL ) 1.190 + strcat_m( retStr, &"()" ); 1.191 + else 1.192 + { strcat_m( retStr, &"(" ); 1.193 + for( ; t != NULL; t = t->next ) 1.194 + { strcat_m( retStr, &" " ); 1.195 + strcat_m( retStr, t->key ); 1.196 + strcat_m( retStr, &"." ); 1.197 + strcat_m( retStr, paramToString( t->param ) ); 1.198 + strcat_m( retStr, &" " ); 1.199 + } 1.200 + strcat_m( retStr, &")" ); 1.201 + } 1.202 + } 1.203 + } 1.204 +*/
