annotate ReadParamsFromFile.c @ 16:93b017e30c76

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