annotate ReadParamsFromFile.c @ 5:a8744027c1a9

ParamHelper is now independent from the Matrix_Multiply header file
author hausers
date Fri, 27 Jan 2012 15:51:11 +0100
parents e5c04a3a2610
children d46150af45ad
rev   line source
Me@0 1 /*
Me@0 2 * Author: SeanHalle@yahoo.com
Me@0 3 *
Me@0 4 * Created on June 15, 2009, 10:12 AM
Me@0 5 */
Me@0 6
Me@0 7 #include <stdio.h>
Me@0 8 #include <stdlib.h>
Me@0 9 #include <string.h>
Me@0 10
hausers@5 11 #include "Param.h"
Me@0 12
Me@0 13 ParamStruc * makeParamFromStrs( char * type, char *value );
Me@0 14
Me@0 15 #define _GNU_SOURCE
Me@0 16
Me@0 17 /*Copied from gnu's win32 lib
Me@0 18 */
Me@0 19 ssize_t
Me@0 20 getline( char **lineptr, size_t *n, FILE *stream )
Me@0 21 {
Me@0 22 if ( lineptr == NULL || n == NULL) return -1;
Me@0 23 if (*lineptr == NULL || *n == 0)
Me@0 24 { *n = 500; //max length of line in a config file
Me@0 25 *lineptr = (char *) malloc( *n );
Me@0 26 if (*lineptr == NULL) return -1;
Me@0 27 }
Me@0 28 if( fgets( *lineptr, *n, stream ) )
Me@0 29 return *n;
Me@0 30 else
Me@0 31 return -1;
Me@0 32 }
Me@0 33
Me@0 34 void
Me@0 35 readParamFileIntoBag( char *paramFileName, ParamBag * bag )
Me@0 36 {
Me@0 37 size_t lineSz = 0;
Me@0 38 FILE* paramFile;
Me@0 39 char* line = NULL;
Me@0 40
Me@0 41 char* paramType;// = malloc( 12 ); //"double" is the longest type
Me@0 42 char* paramName;// = malloc( 500 ); //max of 500 chars in name
Me@0 43 char* paramValue;// = malloc( 500 ); //max of 500 chars in value
Me@0 44
Me@0 45 lineSz = 500; //max length of line in a config file
Me@0 46 line = (char *) malloc( lineSz );
Me@0 47 if( line == NULL )
Me@2 48 { printf( "\nIn readParamFileIntoBag: no mem for line\n" );
Me@0 49 return;
Me@0 50 }
Me@0 51
Me@0 52
Me@0 53 paramFile = fopen( paramFileName, "r" );
Nina@4 54 if( paramFile == NULL )
Nina@4 55 { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); }
Me@0 56 fseek( paramFile, 0, SEEK_SET );
Me@0 57 while( !feof( paramFile ) )
Me@0 58 { while( getline( &line, &lineSz, paramFile ) != -1 )
Me@0 59 {
Me@0 60 char *lineEnd = line + strlen(line) +1;
Me@0 61 char *searchPos = line;
Me@0 62
Me@0 63 if( *line == '\n') continue; //blank line
Me@0 64 if( *line == '/' ) continue; //comment line
Me@0 65
Me@0 66 //read the param type
Me@0 67 paramType = line; //start of string
Me@0 68 int foundIt = 0;
Me@0 69 for( ; searchPos < lineEnd && !foundIt; searchPos++)
Me@0 70 { if( *searchPos == ',' )
Me@0 71 { foundIt = 1;
Me@0 72 *searchPos = 0; //mark end of string
Me@0 73 }
Me@0 74 }
Me@0 75 //get rid of leading spaces
Me@0 76 for( ; searchPos < lineEnd; searchPos++)
Me@0 77 { if( *searchPos != ' ' ) break;
Me@0 78 }
Me@0 79 //read the param name
Me@0 80 paramName = searchPos;
Me@0 81 foundIt = 0;
Me@0 82 for( ; searchPos < lineEnd && !foundIt; searchPos++)
Me@0 83 { if( *searchPos == ',' )
Me@0 84 { foundIt = 1;
Me@0 85 *searchPos = 0; //mark end
Me@0 86 }
Me@0 87 }
Me@0 88 //get rid of leading spaces
Me@0 89 for( ; searchPos < lineEnd; searchPos++)
Me@0 90 { if( *searchPos != ' ' ) break;
Me@0 91 }
Me@0 92 //read the param value
Me@0 93 paramValue = searchPos;
Me@0 94 foundIt = 0;
Me@0 95 for( ; searchPos < lineEnd && !foundIt; searchPos++)
Me@0 96 { if( *searchPos == '\n' )
Me@0 97 { foundIt = 1;
Me@0 98 *searchPos = 0; //mark end
Me@0 99 }
Me@0 100 }
Me@0 101 if( foundIt )
Me@0 102 { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue );
Me@0 103 ParamStruc *
Me@0 104 paramStruc = makeParamFromStrs( paramType, paramValue );
Me@0 105 addParamToBag( paramName, paramStruc, bag );
Me@0 106 }
Me@0 107 }
Me@0 108 }
Me@0 109 free( line );
Me@0 110 }
Me@0 111