annotate ParamBag.c @ 16:93b017e30c76

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