changeset 2:8f6d8a258491

Not sure what changed but works
author Me
date Wed, 28 Jul 2010 13:17:25 -0700
parents 396fda650c30
children cf0a327945e6
files Param.h ParamBag.c ReadParamsFromFile.c
diffstat 3 files changed, 74 insertions(+), 61 deletions(-) [+]
line diff
     1.1 --- a/Param.h	Sat May 22 19:56:44 2010 -0700
     1.2 +++ b/Param.h	Wed Jul 28 13:17:25 2010 -0700
     1.3 @@ -21,29 +21,33 @@
     1.4  #define STRING_PARAM_TYPE 1
     1.5  #define FLOAT_PARAM_TYPE  2
     1.6  
     1.7 -#define HASHSIZE 101
     1.8 +#define PARAM_BAG_HASHSIZE 1024
     1.9  
    1.10 -typedef
    1.11 -struct _HashEntry
    1.12 +typedef struct _ParamBagHashEntry ParamBagHashEntry;
    1.13 +
    1.14 +struct _ParamBagHashEntry
    1.15   {
    1.16 -  char       *key;
    1.17 -  ParamStruc *param;
    1.18 -  struct _HashEntry *next;
    1.19 +   char       *key;
    1.20 +   ParamStruc *param;
    1.21 +   struct _ParamBagHashEntry *next;
    1.22   }
    1.23 -HashEntry;
    1.24 +/*ParamBagHashEntry*/;
    1.25 +
    1.26  
    1.27  typedef
    1.28  struct
    1.29   { int bagSz;
    1.30 -   HashEntry* *entries;
    1.31 +   ParamBagHashEntry* *entries;
    1.32   }
    1.33  ParamBag;
    1.34  
    1.35 +
    1.36  ParamBag    *makeParamBag();
    1.37 +void         readParamFileIntoBag( char *paramFileName, ParamBag * bag );
    1.38  ParamStruc  *getParamFromBag( char *key, ParamBag * bag );
    1.39  int          addParamToBag( char* key, ParamStruc *param, ParamBag *bag );
    1.40  void         freeParamBag( ParamBag *bag );
    1.41 -//char        *paramBagToString( ParamBag * bag )
    1.42 +//char        *paramBagToString( ParamBag * bag );
    1.43  ParamStruc  *makeParamStruc();
    1.44  ParamStruc  *makeParamFromStrs( char * type, char *value );
    1.45  ssize_t      getline( char **lineptr, size_t *n, FILE *stream );
     2.1 --- a/ParamBag.c	Sat May 22 19:56:44 2010 -0700
     2.2 +++ b/ParamBag.c	Wed Jul 28 13:17:25 2010 -0700
     2.3 @@ -16,18 +16,17 @@
     2.4  #include "Param.h"
     2.5  
     2.6  void         freeParamStruc( ParamStruc * param );
     2.7 -void         freeHashEntry( HashEntry *entry );
     2.8 -char*        strdup_m(char *o);
     2.9 -HashEntry *  lookupKeyInHash( char *key, ParamBag * bag );
    2.10 -unsigned int hashKey( char *s, int hashSz );
    2.11 +void         freeParamBagHashEntry( ParamBagHashEntry *entry );
    2.12 +ParamBagHashEntry *  lookupKeyInHash( char *key, ParamBag * bag );
    2.13 +unsigned int hashThisKey( char *s, int hashSz );
    2.14  void         nullOutParamBagHashEntries( ParamBag *bag );
    2.15  
    2.16   ParamBag *
    2.17  makeParamBag()
    2.18   { ParamBag * retBag;
    2.19     retBag = malloc( sizeof( ParamBag ) );
    2.20 -   retBag->entries = malloc( HASHSIZE * sizeof( HashEntry *) );
    2.21 -   retBag->bagSz = HASHSIZE;
    2.22 +   retBag->entries = malloc( PARAM_BAG_HASHSIZE * sizeof( ParamBagHashEntry *) );
    2.23 +   retBag->bagSz = PARAM_BAG_HASHSIZE;
    2.24     nullOutParamBagHashEntries( retBag );
    2.25     
    2.26     return retBag;
    2.27 @@ -37,7 +36,7 @@
    2.28  nullOutParamBagHashEntries( ParamBag *bag )
    2.29   { int i, bagSz;
    2.30     bagSz = bag->bagSz;
    2.31 -   HashEntry ** entries = bag->entries;
    2.32 +   ParamBagHashEntry ** entries = bag->entries;
    2.33     for( i = 0; i < bagSz; i++ )
    2.34        entries[ i ] = NULL;
    2.35   }
    2.36 @@ -53,11 +52,11 @@
    2.37  
    2.38  /*Need this to be separated out, for use in both getParam and putParam
    2.39   */
    2.40 - HashEntry *
    2.41 + ParamBagHashEntry *
    2.42  lookupKeyInHash( char *key, ParamBag * bag )
    2.43   {  unsigned int
    2.44     hashIndex = hashKey( key, bag->bagSz );
    2.45 -    HashEntry*
    2.46 +    ParamBagHashEntry*
    2.47     hashEntry = bag->entries[ hashIndex ];
    2.48     for( ; hashEntry != NULL; hashEntry = hashEntry->next )
    2.49      { if( strcmp( hashEntry->key, key ) == 0 )  return hashEntry;
    2.50 @@ -67,7 +66,7 @@
    2.51  
    2.52   ParamStruc *
    2.53  getParamFromBag( char *key, ParamBag * bag )
    2.54 - { HashEntry *entry;
    2.55 + { ParamBagHashEntry *entry;
    2.56     entry = lookupKeyInHash( key, bag );
    2.57     if( entry == NULL ) return NULL;
    2.58     
    2.59 @@ -77,13 +76,13 @@
    2.60   int
    2.61  addParamToBag( char* key, ParamStruc *param, ParamBag *bag )
    2.62   { unsigned int hashIdx;
    2.63 -   HashEntry* hashEntry;
    2.64 +   ParamBagHashEntry* hashEntry;
    2.65     hashEntry = lookupKeyInHash( key, bag );
    2.66     if( hashEntry == NULL )
    2.67      { hashIdx = hashKey( key, bag->bagSz );
    2.68 -      hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) );
    2.69 +      hashEntry = (ParamBagHashEntry*) malloc( sizeof( ParamBagHashEntry ) );
    2.70              if( hashEntry == NULL )  return 0;
    2.71 -      hashEntry->key = strdup_m( key );
    2.72 +      hashEntry->key = strdup( key );
    2.73              if( hashEntry->key == NULL ) return 0;
    2.74        hashEntry->next = (bag->entries)[hashIdx];
    2.75        (bag->entries)[hashIdx] = hashEntry;
    2.76 @@ -95,44 +94,11 @@
    2.77     return 1;
    2.78   }
    2.79  
    2.80 - char*
    2.81 -strdup_m( char *o )
    2.82 - { int l = strlen(o)+1;
    2.83 -   char *ns = (char*) malloc( l * sizeof(char) );
    2.84 -   strcpy( ns, o );
    2.85 -   return ns;
    2.86 - }
    2.87 -
    2.88 -/* A pretty useless but good debugging function,
    2.89 -   which simply displays the hashtable in (key.value) pairs
    2.90 -*/
    2.91 -/*void paramBagToString( ParamBag * bag )
    2.92 - { int i;
    2.93 -   HashEntry *t;
    2.94 -   for( i = 0; i < bag->bagSz; i++ )
    2.95 -    { t = entries[i];
    2.96 -      if( t == NULL )
    2.97 -         strcat_m( retStr, &"()" );
    2.98 -      else
    2.99 -       { strcat_m( retStr, &"(" );
   2.100 -         for( ; t != NULL; t = t->next )
   2.101 -          { strcat_m( retStr, &" " );
   2.102 -            strcat_m( retStr, t->key );
   2.103 -            strcat_m( retStr, &"." );
   2.104 -            strcat_m( retStr, paramToString( t->param ) );
   2.105 -            strcat_m( retStr, &" " );
   2.106 -          }
   2.107 -         strcat_m( retStr, &")" );
   2.108 -       }
   2.109 -    }
   2.110 - }
   2.111 -*/
   2.112 -
   2.113 -
   2.114 + 
   2.115   void
   2.116  freeParamBag( ParamBag *bag )
   2.117   { int i;
   2.118 -   HashEntry *hashEntry, *temp, **entries;
   2.119 +   ParamBagHashEntry *hashEntry, *temp, **entries;
   2.120  
   2.121     entries = bag->entries;
   2.122     for( i=0; i < bag->bagSz; i++ )
   2.123 @@ -141,7 +107,7 @@
   2.124           while( hashEntry != NULL )
   2.125            {
   2.126              temp = hashEntry->next;
   2.127 -            freeHashEntry( hashEntry );
   2.128 +            freeParamBagHashEntry( hashEntry );
   2.129              hashEntry = temp;
   2.130            }
   2.131         }
   2.132 @@ -149,7 +115,7 @@
   2.133   }
   2.134  
   2.135   void
   2.136 -freeHashEntry( HashEntry *entry )
   2.137 +freeParamBagHashEntry( ParamBagHashEntry *entry )
   2.138   {
   2.139     freeParamStruc( entry->param );
   2.140     free( entry->key ); //was malloc'd above, so free it
   2.141 @@ -169,9 +135,25 @@
   2.142     retStruc->floatValue = 0.0;
   2.143     retStruc->intValue   = 0;
   2.144     retStruc->strValue   = NULL;
   2.145 +
   2.146 +   return retStruc;
   2.147   }
   2.148  
   2.149 - ParamStruc *
   2.150 +void
   2.151 +removeEndWhtSpaceFromStr( char *str )
   2.152 + { int n;
   2.153 +
   2.154 +   n = strlen ( str );
   2.155 +   while( --n >= 0 )
   2.156 +    {
   2.157 +      if(str[n] != ' ' && str[n] != '\t' && str[n] != '\n' && str[n] != '\r')
   2.158 +         break;
   2.159 +    }
   2.160 +   str[n + 1] = '\0';
   2.161 + }
   2.162 +
   2.163 +
   2.164 +ParamStruc *
   2.165  makeParamFromStrs( char * type, char *value )
   2.166   { ParamStruc *retParam;
   2.167     retParam = makeParamStruc();
   2.168 @@ -184,6 +166,7 @@
   2.169         { retParam->type = STRING_PARAM_TYPE;
   2.170           retParam->strValue = malloc( strlen(value) + 1);
   2.171           strcpy( retParam->strValue, value );
   2.172 +         removeEndWhtSpaceFromStr( retParam->strValue );
   2.173         } break;
   2.174        case 'f':
   2.175         { retParam->type = FLOAT_PARAM_TYPE;
   2.176 @@ -193,3 +176,28 @@
   2.177     return retParam;
   2.178   }
   2.179  
   2.180 +
   2.181 +/* A pretty useless but good debugging function,
   2.182 +   which simply displays the hashtable in (key.value) pairs
   2.183 +*/
   2.184 +/*void paramBagToString( ParamBag * bag )
   2.185 + { int i;
   2.186 +   ParamBagHashEntry *t;
   2.187 +   for( i = 0; i < bag->bagSz; i++ )
   2.188 +    { t = entries[i];
   2.189 +      if( t == NULL )
   2.190 +         strcat_m( retStr, &"()" );
   2.191 +      else
   2.192 +       { strcat_m( retStr, &"(" );
   2.193 +         for( ; t != NULL; t = t->next )
   2.194 +          { strcat_m( retStr, &" " );
   2.195 +            strcat_m( retStr, t->key );
   2.196 +            strcat_m( retStr, &"." );
   2.197 +            strcat_m( retStr, paramToString( t->param ) );
   2.198 +            strcat_m( retStr, &" " );
   2.199 +          }
   2.200 +         strcat_m( retStr, &")" );
   2.201 +       }
   2.202 +    }
   2.203 + }
   2.204 +*/
     3.1 --- a/ReadParamsFromFile.c	Sat May 22 19:56:44 2010 -0700
     3.2 +++ b/ReadParamsFromFile.c	Wed Jul 28 13:17:25 2010 -0700
     3.3 @@ -45,12 +45,13 @@
     3.4     lineSz = 500; //max length of line in a config file
     3.5     line = (char *) malloc( lineSz );
     3.6     if( line == NULL )
     3.7 -    { BLIS_DKU__throwError( "no mem for line" );
     3.8 +    { printf( "\nIn readParamFileIntoBag: no mem for line\n" );
     3.9        return;
    3.10      }
    3.11     
    3.12     
    3.13     paramFile = fopen( paramFileName, "r" );
    3.14 +   if( paramFile == NULL ) {printf("\ncouldn't open file\n"); exit(0);}
    3.15     fseek( paramFile, 0, SEEK_SET );
    3.16     while( !feof( paramFile ) )
    3.17      { while( getline( &line, &lineSz, paramFile ) != -1 )