diff ReadParamsFromFile.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/ReadParamsFromFile.c	Sat May 22 19:50:16 2010 -0700
     1.3 @@ -0,0 +1,109 @@
     1.4 +/* 
     1.5 + * Author: SeanHalle@yahoo.com
     1.6 + *
     1.7 + * Created on June 15, 2009, 10:12 AM
     1.8 + */
     1.9 +
    1.10 +#include <stdio.h>
    1.11 +#include <stdlib.h>
    1.12 +#include <string.h>
    1.13 +
    1.14 +#include "../Matrix_Mult.h"
    1.15 +
    1.16 +ParamStruc * makeParamFromStrs( char * type, char *value );
    1.17 +
    1.18 +#define _GNU_SOURCE
    1.19 +
    1.20 +/*Copied from gnu's win32 lib
    1.21 + */
    1.22 + ssize_t
    1.23 +getline( char **lineptr, size_t *n, FILE *stream )
    1.24 + {
    1.25 +   if ( lineptr == NULL || n == NULL)  return -1;
    1.26 +   if (*lineptr == NULL || *n == 0)
    1.27 +    { *n = 500; //max length of line in a config file
    1.28 +      *lineptr = (char *) malloc( *n );
    1.29 +      if (*lineptr == NULL) return -1;
    1.30 +    }
    1.31 +   if( fgets( *lineptr, *n, stream ) )
    1.32 +      return *n;
    1.33 +   else
    1.34 +      return -1;
    1.35 + }
    1.36 +
    1.37 + void
    1.38 +readParamFileIntoBag( char *paramFileName, ParamBag * bag )
    1.39 + {
    1.40 +   size_t lineSz = 0;
    1.41 +   FILE* paramFile;
    1.42 +   char* line = NULL;
    1.43 +
    1.44 +   char* paramType;//  = malloc( 12 );  //"double" is the longest type
    1.45 +   char* paramName;//  = malloc( 500 ); //max of 500 chars in name
    1.46 +   char* paramValue;// = malloc( 500 ); //max of 500 chars in value
    1.47 +   
    1.48 +   lineSz = 500; //max length of line in a config file
    1.49 +   line = (char *) malloc( lineSz );
    1.50 +   if( line == NULL )
    1.51 +    { BLIS_DKU__throwError( "no mem for line" );
    1.52 +      return;
    1.53 +    }
    1.54 +   
    1.55 +   
    1.56 +   paramFile = fopen( paramFileName, "r" );
    1.57 +   fseek( paramFile, 0, SEEK_SET );
    1.58 +   while( !feof( paramFile ) )
    1.59 +    { while( getline( &line, &lineSz, paramFile ) != -1 )
    1.60 +       {
    1.61 +         char *lineEnd = line + strlen(line) +1;
    1.62 +         char *searchPos = line;
    1.63 +         
    1.64 +         if( *line == '\n') continue; //blank line
    1.65 +         if( *line == '/' ) continue; //comment line
    1.66 +
    1.67 +            //read the param type
    1.68 +         paramType = line; //start of string
    1.69 +         int foundIt = 0;
    1.70 +         for( ; searchPos < lineEnd && !foundIt; searchPos++)
    1.71 +          { if( *searchPos == ',' )
    1.72 +             { foundIt = 1;
    1.73 +               *searchPos = 0; //mark end of string
    1.74 +             }
    1.75 +          }
    1.76 +            //get rid of leading spaces
    1.77 +         for( ; searchPos < lineEnd; searchPos++)
    1.78 +          { if( *searchPos != ' ' ) break;
    1.79 +          }
    1.80 +            //read the param name
    1.81 +         paramName = searchPos;
    1.82 +         foundIt = 0;
    1.83 +         for( ; searchPos < lineEnd && !foundIt; searchPos++)
    1.84 +          { if( *searchPos == ',' )
    1.85 +             { foundIt = 1;
    1.86 +               *searchPos = 0; //mark end
    1.87 +             }
    1.88 +          }
    1.89 +            //get rid of leading spaces
    1.90 +         for( ; searchPos < lineEnd; searchPos++)
    1.91 +          { if( *searchPos != ' ' ) break;
    1.92 +          }
    1.93 +            //read the param value
    1.94 +         paramValue = searchPos;
    1.95 +         foundIt = 0;
    1.96 +         for( ; searchPos < lineEnd && !foundIt; searchPos++)
    1.97 +          { if( *searchPos == '\n' )
    1.98 +             { foundIt = 1;
    1.99 +               *searchPos = 0; //mark end
   1.100 +             }
   1.101 +          }
   1.102 +         if( foundIt )
   1.103 +          { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue );
   1.104 +             ParamStruc *
   1.105 +            paramStruc = makeParamFromStrs( paramType, paramValue );
   1.106 +            addParamToBag( paramName, paramStruc, bag );
   1.107 +          }
   1.108 +       }
   1.109 +    }
   1.110 +   free( line );
   1.111 + }
   1.112 +