diff ParamBag.c @ 0:481dd533f0e8

initial add
author Me
date Sat, 22 May 2010 19:50:16 -0700
parents
children 8f6d8a258491
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ParamBag.c	Sat May 22 19:50:16 2010 -0700
     1.3 @@ -0,0 +1,195 @@
     1.4 +/*
     1.5 + *  Copyright 2009 OpenSourceCodeStewardshipFoundation.org
     1.6 + *  Licensed under GNU General Public License version 2
     1.7 + *
     1.8 + *  Based on code posted to a discussion group on the web.  (Forgot to mark
     1.9 + *   down where got it from)
    1.10 + *
    1.11 + * Author: seanhalle@yahoo.com
    1.12 + *
    1.13 + * Created on November 14, 2009, 9:00 PM
    1.14 + */
    1.15 +#include <string.h>
    1.16 +#include <stdio.h>
    1.17 +#include <stdlib.h>
    1.18 +
    1.19 +#include "Param.h"
    1.20 +
    1.21 +void         freeParamStruc( ParamStruc * param );
    1.22 +void         freeHashEntry( HashEntry *entry );
    1.23 +char*        strdup_m(char *o);
    1.24 +HashEntry *  lookupKeyInHash( char *key, ParamBag * bag );
    1.25 +unsigned int hashKey( char *s, int hashSz );
    1.26 +void         nullOutParamBagHashEntries( ParamBag *bag );
    1.27 +
    1.28 + ParamBag *
    1.29 +makeParamBag()
    1.30 + { ParamBag * retBag;
    1.31 +   retBag = malloc( sizeof( ParamBag ) );
    1.32 +   retBag->entries = malloc( HASHSIZE * sizeof( HashEntry *) );
    1.33 +   retBag->bagSz = HASHSIZE;
    1.34 +   nullOutParamBagHashEntries( retBag );
    1.35 +   
    1.36 +   return retBag;
    1.37 + }
    1.38 +
    1.39 + void
    1.40 +nullOutParamBagHashEntries( ParamBag *bag )
    1.41 + { int i, bagSz;
    1.42 +   bagSz = bag->bagSz;
    1.43 +   HashEntry ** entries = bag->entries;
    1.44 +   for( i = 0; i < bagSz; i++ )
    1.45 +      entries[ i ] = NULL;
    1.46 + }
    1.47 +
    1.48 + unsigned int
    1.49 +hashKey( char *s, int hashSz )
    1.50 + { unsigned int h = 0;
    1.51 +   
    1.52 +   for( ; *s != 0; s++ )
    1.53 +      h = *s + h*31;
    1.54 +   return h % hashSz;
    1.55 + }
    1.56 +
    1.57 +/*Need this to be separated out, for use in both getParam and putParam
    1.58 + */
    1.59 + HashEntry *
    1.60 +lookupKeyInHash( char *key, ParamBag * bag )
    1.61 + {  unsigned int
    1.62 +   hashIndex = hashKey( key, bag->bagSz );
    1.63 +    HashEntry*
    1.64 +   hashEntry = bag->entries[ hashIndex ];
    1.65 +   for( ; hashEntry != NULL; hashEntry = hashEntry->next )
    1.66 +    { if( strcmp( hashEntry->key, key ) == 0 )  return hashEntry;
    1.67 +    }
    1.68 +   return NULL;
    1.69 + }
    1.70 +
    1.71 + ParamStruc *
    1.72 +getParamFromBag( char *key, ParamBag * bag )
    1.73 + { HashEntry *entry;
    1.74 +   entry = lookupKeyInHash( key, bag );
    1.75 +   if( entry == NULL ) return NULL;
    1.76 +   
    1.77 +   return entry->param;
    1.78 + }
    1.79 +
    1.80 + int
    1.81 +addParamToBag( char* key, ParamStruc *param, ParamBag *bag )
    1.82 + { unsigned int hashIdx;
    1.83 +   HashEntry* hashEntry;
    1.84 +   hashEntry = lookupKeyInHash( key, bag );
    1.85 +   if( hashEntry == NULL )
    1.86 +    { hashIdx = hashKey( key, bag->bagSz );
    1.87 +      hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) );
    1.88 +            if( hashEntry == NULL )  return 0;
    1.89 +      hashEntry->key = strdup_m( key );
    1.90 +            if( hashEntry->key == NULL ) return 0;
    1.91 +      hashEntry->next = (bag->entries)[hashIdx];
    1.92 +      (bag->entries)[hashIdx] = hashEntry;
    1.93 +    }
    1.94 +   else
    1.95 +    { freeParamStruc( hashEntry->param );
    1.96 +    }
    1.97 +   hashEntry->param = param;
    1.98 +   return 1;
    1.99 + }
   1.100 +
   1.101 + char*
   1.102 +strdup_m( char *o )
   1.103 + { int l = strlen(o)+1;
   1.104 +   char *ns = (char*) malloc( l * sizeof(char) );
   1.105 +   strcpy( ns, o );
   1.106 +   return ns;
   1.107 + }
   1.108 +
   1.109 +/* A pretty useless but good debugging function,
   1.110 +   which simply displays the hashtable in (key.value) pairs
   1.111 +*/
   1.112 +/*void paramBagToString( ParamBag * bag )
   1.113 + { int i;
   1.114 +   HashEntry *t;
   1.115 +   for( i = 0; i < bag->bagSz; i++ )
   1.116 +    { t = entries[i];
   1.117 +      if( t == NULL )
   1.118 +         strcat_m( retStr, &"()" );
   1.119 +      else
   1.120 +       { strcat_m( retStr, &"(" );
   1.121 +         for( ; t != NULL; t = t->next )
   1.122 +          { strcat_m( retStr, &" " );
   1.123 +            strcat_m( retStr, t->key );
   1.124 +            strcat_m( retStr, &"." );
   1.125 +            strcat_m( retStr, paramToString( t->param ) );
   1.126 +            strcat_m( retStr, &" " );
   1.127 +          }
   1.128 +         strcat_m( retStr, &")" );
   1.129 +       }
   1.130 +    }
   1.131 + }
   1.132 +*/
   1.133 +
   1.134 +
   1.135 + void
   1.136 +freeParamBag( ParamBag *bag )
   1.137 + { int i;
   1.138 +   HashEntry *hashEntry, *temp, **entries;
   1.139 +
   1.140 +   entries = bag->entries;
   1.141 +   for( i=0; i < bag->bagSz; i++ )
   1.142 +    { if( entries[i] != NULL )
   1.143 +       { hashEntry = entries[i];
   1.144 +         while( hashEntry != NULL )
   1.145 +          {
   1.146 +            temp = hashEntry->next;
   1.147 +            freeHashEntry( hashEntry );
   1.148 +            hashEntry = temp;
   1.149 +          }
   1.150 +       }
   1.151 +    }
   1.152 + }
   1.153 +
   1.154 + void
   1.155 +freeHashEntry( HashEntry *entry )
   1.156 + {
   1.157 +   freeParamStruc( entry->param );
   1.158 +   free( entry->key ); //was malloc'd above, so free it
   1.159 +   free( entry );
   1.160 + }
   1.161 +
   1.162 + void
   1.163 +freeParamStruc( ParamStruc * param )
   1.164 + { if( param->type == STRING_PARAM_TYPE ) free( param->strValue );
   1.165 +   free( param );
   1.166 + }
   1.167 +
   1.168 + ParamStruc *
   1.169 +makeParamStruc()
   1.170 + { ParamStruc *retStruc;
   1.171 +   retStruc = malloc( sizeof( ParamStruc ) );
   1.172 +   retStruc->floatValue = 0.0;
   1.173 +   retStruc->intValue   = 0;
   1.174 +   retStruc->strValue   = NULL;
   1.175 + }
   1.176 +
   1.177 + ParamStruc *
   1.178 +makeParamFromStrs( char * type, char *value )
   1.179 + { ParamStruc *retParam;
   1.180 +   retParam = makeParamStruc();
   1.181 +   switch(*type)
   1.182 +    { case 'i':
   1.183 +       { retParam->type = INT_PARAM_TYPE;
   1.184 +         retParam->intValue = atoi( value );
   1.185 +       } break;
   1.186 +      case 's':
   1.187 +       { retParam->type = STRING_PARAM_TYPE;
   1.188 +         retParam->strValue = malloc( strlen(value) + 1);
   1.189 +         strcpy( retParam->strValue, value );
   1.190 +       } break;
   1.191 +      case 'f':
   1.192 +       { retParam->type = FLOAT_PARAM_TYPE;
   1.193 +         retParam->floatValue = atof( value );
   1.194 +       } break;
   1.195 +    }
   1.196 +   return retParam;
   1.197 + }
   1.198 +