Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > ParamHelper
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 0:e210cdd06686 | 1:f0cd747e5fc8 |
|---|---|
| 14 #include <stdlib.h> | 14 #include <stdlib.h> |
| 15 | 15 |
| 16 #include "Param.h" | 16 #include "Param.h" |
| 17 | 17 |
| 18 void freeParamStruc( ParamStruc * param ); | 18 void freeParamStruc( ParamStruc * param ); |
| 19 void freeHashEntry( HashEntry *entry ); | 19 void freeParamBagHashEntry( ParamBagHashEntry *entry ); |
| 20 char* strdup_m(char *o); | 20 ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag ); |
| 21 HashEntry * lookupKeyInHash( char *key, ParamBag * bag ); | 21 unsigned int hashThisKey( char *s, int hashSz ); |
| 22 unsigned int hashKey( char *s, int hashSz ); | |
| 23 void nullOutParamBagHashEntries( ParamBag *bag ); | 22 void nullOutParamBagHashEntries( ParamBag *bag ); |
| 24 | 23 |
| 25 ParamBag * | 24 ParamBag * |
| 26 makeParamBag() | 25 makeParamBag() |
| 27 { ParamBag * retBag; | 26 { ParamBag * retBag; |
| 28 retBag = malloc( sizeof( ParamBag ) ); | 27 retBag = malloc( sizeof( ParamBag ) ); |
| 29 retBag->entries = malloc( HASHSIZE * sizeof( HashEntry *) ); | 28 retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) ); |
| 30 retBag->bagSz = HASHSIZE; | 29 retBag->bagSz = PARAM_BAG_HASHSIZE; |
| 31 nullOutParamBagHashEntries( retBag ); | 30 nullOutParamBagHashEntries( retBag ); |
| 32 | 31 |
| 33 return retBag; | 32 return retBag; |
| 34 } | 33 } |
| 35 | 34 |
| 36 void | 35 void |
| 37 nullOutParamBagHashEntries( ParamBag *bag ) | 36 nullOutParamBagHashEntries( ParamBag *bag ) |
| 38 { int i, bagSz; | 37 { int i, bagSz; |
| 39 bagSz = bag->bagSz; | 38 bagSz = bag->bagSz; |
| 40 HashEntry ** entries = bag->entries; | 39 ParamBagHashEntry ** entries = bag->entries; |
| 41 for( i = 0; i < bagSz; i++ ) | 40 for( i = 0; i < bagSz; i++ ) |
| 42 entries[ i ] = NULL; | 41 entries[ i ] = NULL; |
| 43 } | 42 } |
| 44 | 43 |
| 45 unsigned int | 44 unsigned int |
| 51 return h % hashSz; | 50 return h % hashSz; |
| 52 } | 51 } |
| 53 | 52 |
| 54 /*Need this to be separated out, for use in both getParam and putParam | 53 /*Need this to be separated out, for use in both getParam and putParam |
| 55 */ | 54 */ |
| 56 HashEntry * | 55 ParamBagHashEntry * |
| 57 lookupKeyInHash( char *key, ParamBag * bag ) | 56 lookupKeyInHash( char *key, ParamBag * bag ) |
| 58 { unsigned int | 57 { unsigned int |
| 59 hashIndex = hashKey( key, bag->bagSz ); | 58 hashIndex = hashKey( key, bag->bagSz ); |
| 60 HashEntry* | 59 ParamBagHashEntry* |
| 61 hashEntry = bag->entries[ hashIndex ]; | 60 hashEntry = bag->entries[ hashIndex ]; |
| 62 for( ; hashEntry != NULL; hashEntry = hashEntry->next ) | 61 for( ; hashEntry != NULL; hashEntry = hashEntry->next ) |
| 63 { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; | 62 { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; |
| 64 } | 63 } |
| 65 return NULL; | 64 return NULL; |
| 66 } | 65 } |
| 67 | 66 |
| 68 ParamStruc * | 67 ParamStruc * |
| 69 getParamFromBag( char *key, ParamBag * bag ) | 68 getParamFromBag( char *key, ParamBag * bag ) |
| 70 { HashEntry *entry; | 69 { ParamBagHashEntry *entry; |
| 71 entry = lookupKeyInHash( key, bag ); | 70 entry = lookupKeyInHash( key, bag ); |
| 72 if( entry == NULL ) return NULL; | 71 if( entry == NULL ) return NULL; |
| 73 | 72 |
| 74 return entry->param; | 73 return entry->param; |
| 75 } | 74 } |
| 76 | 75 |
| 77 int | 76 int |
| 78 addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) | 77 addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) |
| 79 { unsigned int hashIdx; | 78 { unsigned int hashIdx; |
| 80 HashEntry* hashEntry; | 79 ParamBagHashEntry* hashEntry; |
| 81 hashEntry = lookupKeyInHash( key, bag ); | 80 hashEntry = lookupKeyInHash( key, bag ); |
| 82 if( hashEntry == NULL ) | 81 if( hashEntry == NULL ) |
| 83 { hashIdx = hashKey( key, bag->bagSz ); | 82 { hashIdx = hashKey( key, bag->bagSz ); |
| 84 hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); | 83 hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) ); |
| 85 if( hashEntry == NULL ) return 0; | 84 if( hashEntry == NULL ) return 0; |
| 86 hashEntry->key = strdup_m( key ); | 85 hashEntry->key = strdup( key ); |
| 87 if( hashEntry->key == NULL ) return 0; | 86 if( hashEntry->key == NULL ) return 0; |
| 88 hashEntry->next = (bag->entries)[hashIdx]; | 87 hashEntry->next = (bag->entries)[hashIdx]; |
| 89 (bag->entries)[hashIdx] = hashEntry; | 88 (bag->entries)[hashIdx] = hashEntry; |
| 90 } | 89 } |
| 91 else | 90 else |
| 93 } | 92 } |
| 94 hashEntry->param = param; | 93 hashEntry->param = param; |
| 95 return 1; | 94 return 1; |
| 96 } | 95 } |
| 97 | 96 |
| 98 char* | 97 |
| 99 strdup_m( char *o ) | 98 void |
| 100 { int l = strlen(o)+1; | 99 freeParamBag( ParamBag *bag ) |
| 101 char *ns = (char*) malloc( l * sizeof(char) ); | 100 { int i; |
| 102 strcpy( ns, o ); | 101 ParamBagHashEntry *hashEntry, *temp, **entries; |
| 103 return ns; | 102 |
| 104 } | 103 entries = bag->entries; |
| 104 for( i=0; i < bag->bagSz; i++ ) | |
| 105 { if( entries[i] != NULL ) | |
| 106 { hashEntry = entries[i]; | |
| 107 while( hashEntry != NULL ) | |
| 108 { | |
| 109 temp = hashEntry->next; | |
| 110 freeParamBagHashEntry( hashEntry ); | |
| 111 hashEntry = temp; | |
| 112 } | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void | |
| 118 freeParamBagHashEntry( ParamBagHashEntry *entry ) | |
| 119 { | |
| 120 freeParamStruc( entry->param ); | |
| 121 free( entry->key ); //was malloc'd above, so free it | |
| 122 free( entry ); | |
| 123 } | |
| 124 | |
| 125 void | |
| 126 freeParamStruc( ParamStruc * param ) | |
| 127 { if( param->type == STRING_PARAM_TYPE ) free( param->strValue ); | |
| 128 free( param ); | |
| 129 } | |
| 130 | |
| 131 ParamStruc * | |
| 132 makeParamStruc() | |
| 133 { ParamStruc *retStruc; | |
| 134 retStruc = malloc( sizeof( ParamStruc ) ); | |
| 135 retStruc->floatValue = 0.0; | |
| 136 retStruc->intValue = 0; | |
| 137 retStruc->strValue = NULL; | |
| 138 | |
| 139 return retStruc; | |
| 140 } | |
| 141 | |
| 142 void | |
| 143 removeEndWhtSpaceFromStr( char *str ) | |
| 144 { int n; | |
| 145 | |
| 146 n = strlen ( str ); | |
| 147 while( --n >= 0 ) | |
| 148 { | |
| 149 if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r') | |
| 150 break; | |
| 151 } | |
| 152 str[n + 1] = '\0'; | |
| 153 } | |
| 154 | |
| 155 | |
| 156 ParamStruc * | |
| 157 makeParamFromStrs( char * type, char *value ) | |
| 158 { ParamStruc *retParam; | |
| 159 retParam = makeParamStruc(); | |
| 160 switch(*type) | |
| 161 { case 'i': | |
| 162 { retParam->type = INT_PARAM_TYPE; | |
| 163 retParam->intValue = atoi( value ); | |
| 164 } break; | |
| 165 case 's': | |
| 166 { retParam->type = STRING_PARAM_TYPE; | |
| 167 retParam->strValue = malloc( strlen(value) + 1); | |
| 168 strcpy( retParam->strValue, value ); | |
| 169 removeEndWhtSpaceFromStr( retParam->strValue ); | |
| 170 } break; | |
| 171 case 'f': | |
| 172 { retParam->type = FLOAT_PARAM_TYPE; | |
| 173 retParam->floatValue = atof( value ); | |
| 174 } break; | |
| 175 } | |
| 176 return retParam; | |
| 177 } | |
| 178 | |
| 105 | 179 |
| 106 /* A pretty useless but good debugging function, | 180 /* A pretty useless but good debugging function, |
| 107 which simply displays the hashtable in (key.value) pairs | 181 which simply displays the hashtable in (key.value) pairs |
| 108 */ | 182 */ |
| 109 /*void paramBagToString( ParamBag * bag ) | 183 /*void paramBagToString( ParamBag * bag ) |
| 110 { int i; | 184 { int i; |
| 111 HashEntry *t; | 185 ParamBagHashEntry *t; |
| 112 for( i = 0; i < bag->bagSz; i++ ) | 186 for( i = 0; i < bag->bagSz; i++ ) |
| 113 { t = entries[i]; | 187 { t = entries[i]; |
| 114 if( t == NULL ) | 188 if( t == NULL ) |
| 115 strcat_m( retStr, &"()" ); | 189 strcat_m( retStr, &"()" ); |
| 116 else | 190 else |
| 125 strcat_m( retStr, &")" ); | 199 strcat_m( retStr, &")" ); |
| 126 } | 200 } |
| 127 } | 201 } |
| 128 } | 202 } |
| 129 */ | 203 */ |
| 130 | |
| 131 | |
| 132 void | |
| 133 freeParamBag( ParamBag *bag ) | |
| 134 { int i; | |
| 135 HashEntry *hashEntry, *temp, **entries; | |
| 136 | |
| 137 entries = bag->entries; | |
| 138 for( i=0; i < bag->bagSz; i++ ) | |
| 139 { if( entries[i] != NULL ) | |
| 140 { hashEntry = entries[i]; | |
| 141 while( hashEntry != NULL ) | |
| 142 { | |
| 143 temp = hashEntry->next; | |
| 144 freeHashEntry( hashEntry ); | |
| 145 hashEntry = temp; | |
| 146 } | |
| 147 } | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void | |
| 152 freeHashEntry( HashEntry *entry ) | |
| 153 { | |
| 154 freeParamStruc( entry->param ); | |
| 155 free( entry->key ); //was malloc'd above, so free it | |
| 156 free( entry ); | |
| 157 } | |
| 158 | |
| 159 void | |
| 160 freeParamStruc( ParamStruc * param ) | |
| 161 { if( param->type == STRING_PARAM_TYPE ) free( param->strValue ); | |
| 162 free( param ); | |
| 163 } | |
| 164 | |
| 165 ParamStruc * | |
| 166 makeParamStruc() | |
| 167 { ParamStruc *retStruc; | |
| 168 retStruc = malloc( sizeof( ParamStruc ) ); | |
| 169 retStruc->floatValue = 0.0; | |
| 170 retStruc->intValue = 0; | |
| 171 retStruc->strValue = NULL; | |
| 172 } | |
| 173 | |
| 174 ParamStruc * | |
| 175 makeParamFromStrs( char * type, char *value ) | |
| 176 { ParamStruc *retParam; | |
| 177 retParam = makeParamStruc(); | |
| 178 switch(*type) | |
| 179 { case 'i': | |
| 180 { retParam->type = INT_PARAM_TYPE; | |
| 181 retParam->intValue = atoi( value ); | |
| 182 } break; | |
| 183 case 's': | |
| 184 { retParam->type = STRING_PARAM_TYPE; | |
| 185 retParam->strValue = malloc( strlen(value) + 1); | |
| 186 strcpy( retParam->strValue, value ); | |
| 187 } break; | |
| 188 case 'f': | |
| 189 { retParam->type = FLOAT_PARAM_TYPE; | |
| 190 retParam->floatValue = atof( value ); | |
| 191 } break; | |
| 192 } | |
| 193 return retParam; | |
| 194 } | |
| 195 |
