Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > ParamHelper
comparison ParamBag.c @ 0:481dd533f0e8
initial add
| author | Me |
|---|---|
| date | Sat, 22 May 2010 19:50:16 -0700 |
| parents | |
| children | 8f6d8a258491 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:e210cdd06686 |
|---|---|
| 1 /* | |
| 2 * Copyright 2009 OpenSourceCodeStewardshipFoundation.org | |
| 3 * Licensed under GNU General Public License version 2 | |
| 4 * | |
| 5 * Based on code posted to a discussion group on the web. (Forgot to mark | |
| 6 * down where got it from) | |
| 7 * | |
| 8 * Author: seanhalle@yahoo.com | |
| 9 * | |
| 10 * Created on November 14, 2009, 9:00 PM | |
| 11 */ | |
| 12 #include <string.h> | |
| 13 #include <stdio.h> | |
| 14 #include <stdlib.h> | |
| 15 | |
| 16 #include "Param.h" | |
| 17 | |
| 18 void freeParamStruc( ParamStruc * param ); | |
| 19 void freeHashEntry( HashEntry *entry ); | |
| 20 char* strdup_m(char *o); | |
| 21 HashEntry * lookupKeyInHash( char *key, ParamBag * bag ); | |
| 22 unsigned int hashKey( char *s, int hashSz ); | |
| 23 void nullOutParamBagHashEntries( ParamBag *bag ); | |
| 24 | |
| 25 ParamBag * | |
| 26 makeParamBag() | |
| 27 { ParamBag * retBag; | |
| 28 retBag = malloc( sizeof( ParamBag ) ); | |
| 29 retBag->entries = malloc( HASHSIZE * sizeof( HashEntry *) ); | |
| 30 retBag->bagSz = HASHSIZE; | |
| 31 nullOutParamBagHashEntries( retBag ); | |
| 32 | |
| 33 return retBag; | |
| 34 } | |
| 35 | |
| 36 void | |
| 37 nullOutParamBagHashEntries( ParamBag *bag ) | |
| 38 { int i, bagSz; | |
| 39 bagSz = bag->bagSz; | |
| 40 HashEntry ** entries = bag->entries; | |
| 41 for( i = 0; i < bagSz; i++ ) | |
| 42 entries[ i ] = NULL; | |
| 43 } | |
| 44 | |
| 45 unsigned int | |
| 46 hashKey( char *s, int hashSz ) | |
| 47 { unsigned int h = 0; | |
| 48 | |
| 49 for( ; *s != 0; s++ ) | |
| 50 h = *s + h*31; | |
| 51 return h % hashSz; | |
| 52 } | |
| 53 | |
| 54 /*Need this to be separated out, for use in both getParam and putParam | |
| 55 */ | |
| 56 HashEntry * | |
| 57 lookupKeyInHash( char *key, ParamBag * bag ) | |
| 58 { unsigned int | |
| 59 hashIndex = hashKey( key, bag->bagSz ); | |
| 60 HashEntry* | |
| 61 hashEntry = bag->entries[ hashIndex ]; | |
| 62 for( ; hashEntry != NULL; hashEntry = hashEntry->next ) | |
| 63 { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; | |
| 64 } | |
| 65 return NULL; | |
| 66 } | |
| 67 | |
| 68 ParamStruc * | |
| 69 getParamFromBag( char *key, ParamBag * bag ) | |
| 70 { HashEntry *entry; | |
| 71 entry = lookupKeyInHash( key, bag ); | |
| 72 if( entry == NULL ) return NULL; | |
| 73 | |
| 74 return entry->param; | |
| 75 } | |
| 76 | |
| 77 int | |
| 78 addParamToBag( char* key, ParamStruc *param, ParamBag *bag ) | |
| 79 { unsigned int hashIdx; | |
| 80 HashEntry* hashEntry; | |
| 81 hashEntry = lookupKeyInHash( key, bag ); | |
| 82 if( hashEntry == NULL ) | |
| 83 { hashIdx = hashKey( key, bag->bagSz ); | |
| 84 hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); | |
| 85 if( hashEntry == NULL ) return 0; | |
| 86 hashEntry->key = strdup_m( key ); | |
| 87 if( hashEntry->key == NULL ) return 0; | |
| 88 hashEntry->next = (bag->entries)[hashIdx]; | |
| 89 (bag->entries)[hashIdx] = hashEntry; | |
| 90 } | |
| 91 else | |
| 92 { freeParamStruc( hashEntry->param ); | |
| 93 } | |
| 94 hashEntry->param = param; | |
| 95 return 1; | |
| 96 } | |
| 97 | |
| 98 char* | |
| 99 strdup_m( char *o ) | |
| 100 { int l = strlen(o)+1; | |
| 101 char *ns = (char*) malloc( l * sizeof(char) ); | |
| 102 strcpy( ns, o ); | |
| 103 return ns; | |
| 104 } | |
| 105 | |
| 106 /* A pretty useless but good debugging function, | |
| 107 which simply displays the hashtable in (key.value) pairs | |
| 108 */ | |
| 109 /*void paramBagToString( ParamBag * bag ) | |
| 110 { int i; | |
| 111 HashEntry *t; | |
| 112 for( i = 0; i < bag->bagSz; i++ ) | |
| 113 { t = entries[i]; | |
| 114 if( t == NULL ) | |
| 115 strcat_m( retStr, &"()" ); | |
| 116 else | |
| 117 { strcat_m( retStr, &"(" ); | |
| 118 for( ; t != NULL; t = t->next ) | |
| 119 { strcat_m( retStr, &" " ); | |
| 120 strcat_m( retStr, t->key ); | |
| 121 strcat_m( retStr, &"." ); | |
| 122 strcat_m( retStr, paramToString( t->param ) ); | |
| 123 strcat_m( retStr, &" " ); | |
| 124 } | |
| 125 strcat_m( retStr, &")" ); | |
| 126 } | |
| 127 } | |
| 128 } | |
| 129 */ | |
| 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 |
