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