annotate ParamBag.c @ 9:b80587574901

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