annotate ParamBag.c @ 0:481dd533f0e8

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