annotate ParamBag.c @ 22:943e99459739

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