annotate ParamBag.c @ 4:e5c04a3a2610

slightly more verbose error message
author Nina Engelhardt
date Mon, 29 Aug 2011 13:57:31 +0200
parents 8f6d8a258491
children d46150af45ad
rev   line source
Me@0 1 /*
Me@3 2 * Copyright 2009 OpenSourceStewardshipFoundation.org
Me@0 3 * Licensed under GNU General Public License version 2
Me@0 4 *
Me@0 5 * Based on code posted to a discussion group on the web. (Forgot to mark
Me@0 6 * down where got it from)
Me@0 7 *
Me@0 8 * Author: seanhalle@yahoo.com
Me@0 9 *
Me@0 10 * Created on November 14, 2009, 9:00 PM
Me@0 11 */
Me@0 12 #include <string.h>
Me@0 13 #include <stdio.h>
Me@0 14 #include <stdlib.h>
Me@0 15
Me@0 16 #include "Param.h"
Me@0 17
Me@0 18 void freeParamStruc( ParamStruc * param );
Me@2 19 void freeParamBagHashEntry( ParamBagHashEntry *entry );
Me@2 20 ParamBagHashEntry * lookupKeyInHash( char *key, ParamBag * bag );
Me@2 21 unsigned int hashThisKey( char *s, int hashSz );
Me@0 22 void nullOutParamBagHashEntries( ParamBag *bag );
Me@0 23
Me@0 24 ParamBag *
Me@0 25 makeParamBag()
Me@0 26 { ParamBag * retBag;
Me@0 27 retBag = malloc( sizeof( ParamBag ) );
Me@2 28 retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) );
Me@2 29 retBag->bagSz = PARAM_BAG_HASHSIZE;
Me@0 30 nullOutParamBagHashEntries( retBag );
Me@0 31
Me@0 32 return retBag;
Me@0 33 }
Me@0 34
Me@0 35 void
Me@0 36 nullOutParamBagHashEntries( ParamBag *bag )
Me@0 37 { int i, bagSz;
Me@0 38 bagSz = bag->bagSz;
Me@2 39 ParamBagHashEntry ** entries = bag->entries;
Me@0 40 for( i = 0; i < bagSz; i++ )
Me@0 41 entries[ i ] = NULL;
Me@0 42 }
Me@0 43
Me@0 44 unsigned int
Me@0 45 hashKey( char *s, int hashSz )
Me@0 46 { unsigned int h = 0;
Me@0 47
Me@0 48 for( ; *s != 0; s++ )
Me@0 49 h = *s + h*31;
Me@0 50 return h % hashSz;
Me@0 51 }
Me@0 52
Me@0 53 /*Need this to be separated out, for use in both getParam and putParam
Me@0 54 */
Me@2 55 ParamBagHashEntry *
Me@0 56 lookupKeyInHash( char *key, ParamBag * bag )
Me@0 57 { unsigned int
Me@0 58 hashIndex = hashKey( key, bag->bagSz );
Me@2 59 ParamBagHashEntry*
Me@0 60 hashEntry = bag->entries[ hashIndex ];
Me@0 61 for( ; hashEntry != NULL; hashEntry = hashEntry->next )
Me@0 62 { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry;
Me@0 63 }
Me@0 64 return NULL;
Me@0 65 }
Me@0 66
Me@0 67 ParamStruc *
Me@0 68 getParamFromBag( char *key, ParamBag * bag )
Me@2 69 { ParamBagHashEntry *entry;
Me@0 70 entry = lookupKeyInHash( key, bag );
Me@0 71 if( entry == NULL ) return NULL;
Me@0 72
Me@0 73 return entry->param;
Me@0 74 }
Me@0 75
Me@0 76 int
Me@0 77 addParamToBag( char* key, ParamStruc *param, ParamBag *bag )
Me@0 78 { unsigned int hashIdx;
Me@2 79 ParamBagHashEntry* hashEntry;
Me@0 80 hashEntry = lookupKeyInHash( key, bag );
Me@0 81 if( hashEntry == NULL )
Me@0 82 { hashIdx = hashKey( key, bag->bagSz );
Me@2 83 hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) );
Me@0 84 if( hashEntry == NULL ) return 0;
Me@2 85 hashEntry->key = strdup( key );
Me@0 86 if( hashEntry->key == NULL ) return 0;
Me@0 87 hashEntry->next = (bag->entries)[hashIdx];
Me@0 88 (bag->entries)[hashIdx] = hashEntry;
Me@0 89 }
Me@0 90 else
Me@0 91 { freeParamStruc( hashEntry->param );
Me@0 92 }
Me@0 93 hashEntry->param = param;
Me@0 94 return 1;
Me@0 95 }
Me@0 96
Me@2 97
Me@0 98 void
Me@0 99 freeParamBag( ParamBag *bag )
Me@0 100 { int i;
Me@2 101 ParamBagHashEntry *hashEntry, *temp, **entries;
Me@0 102
Me@0 103 entries = bag->entries;
Me@0 104 for( i=0; i < bag->bagSz; i++ )
Me@0 105 { if( entries[i] != NULL )
Me@0 106 { hashEntry = entries[i];
Me@0 107 while( hashEntry != NULL )
Me@0 108 {
Me@0 109 temp = hashEntry->next;
Me@2 110 freeParamBagHashEntry( hashEntry );
Me@0 111 hashEntry = temp;
Me@0 112 }
Me@0 113 }
Me@0 114 }
Me@0 115 }
Me@0 116
Me@0 117 void
Me@2 118 freeParamBagHashEntry( ParamBagHashEntry *entry )
Me@0 119 {
Me@0 120 freeParamStruc( entry->param );
Me@0 121 free( entry->key ); //was malloc'd above, so free it
Me@0 122 free( entry );
Me@0 123 }
Me@0 124
Me@0 125 void
Me@0 126 freeParamStruc( ParamStruc * param )
Me@0 127 { if( param->type == STRING_PARAM_TYPE ) free( param->strValue );
Me@0 128 free( param );
Me@0 129 }
Me@0 130
Me@0 131 ParamStruc *
Me@0 132 makeParamStruc()
Me@0 133 { ParamStruc *retStruc;
Me@0 134 retStruc = malloc( sizeof( ParamStruc ) );
Me@0 135 retStruc->floatValue = 0.0;
Me@0 136 retStruc->intValue = 0;
Me@0 137 retStruc->strValue = NULL;
Me@2 138
Me@2 139 return retStruc;
Me@0 140 }
Me@0 141
Me@2 142 void
Me@2 143 removeEndWhtSpaceFromStr( char *str )
Me@2 144 { int n;
Me@2 145
Me@2 146 n = strlen ( str );
Me@2 147 while( --n >= 0 )
Me@2 148 {
Me@2 149 if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r')
Me@2 150 break;
Me@2 151 }
Me@2 152 str[n + 1] = '\0';
Me@2 153 }
Me@2 154
Me@2 155
Me@2 156 ParamStruc *
Me@0 157 makeParamFromStrs( char * type, char *value )
Me@0 158 { ParamStruc *retParam;
Me@0 159 retParam = makeParamStruc();
Me@0 160 switch(*type)
Me@0 161 { case 'i':
Me@0 162 { retParam->type = INT_PARAM_TYPE;
Me@0 163 retParam->intValue = atoi( value );
Me@0 164 } break;
Me@0 165 case 's':
Me@0 166 { retParam->type = STRING_PARAM_TYPE;
Me@0 167 retParam->strValue = malloc( strlen(value) + 1);
Me@0 168 strcpy( retParam->strValue, value );
Me@2 169 removeEndWhtSpaceFromStr( retParam->strValue );
Me@0 170 } break;
Me@0 171 case 'f':
Me@0 172 { retParam->type = FLOAT_PARAM_TYPE;
Me@0 173 retParam->floatValue = atof( value );
Me@0 174 } break;
Me@0 175 }
Me@0 176 return retParam;
Me@0 177 }
Me@0 178
Me@2 179
Me@2 180 /* A pretty useless but good debugging function,
Me@2 181 which simply displays the hashtable in (key.value) pairs
Me@2 182 */
Me@2 183 /*void paramBagToString( ParamBag * bag )
Me@2 184 { int i;
Me@2 185 ParamBagHashEntry *t;
Me@2 186 for( i = 0; i < bag->bagSz; i++ )
Me@2 187 { t = entries[i];
Me@2 188 if( t == NULL )
Me@2 189 strcat_m( retStr, &"()" );
Me@2 190 else
Me@2 191 { strcat_m( retStr, &"(" );
Me@2 192 for( ; t != NULL; t = t->next )
Me@2 193 { strcat_m( retStr, &" " );
Me@2 194 strcat_m( retStr, t->key );
Me@2 195 strcat_m( retStr, &"." );
Me@2 196 strcat_m( retStr, paramToString( t->param ) );
Me@2 197 strcat_m( retStr, &" " );
Me@2 198 }
Me@2 199 strcat_m( retStr, &")" );
Me@2 200 }
Me@2 201 }
Me@2 202 }
Me@2 203 */