view ReadParamsFromFile.c @ 17:c6544bc64a7c

New Brch -- ML_dev -- used to develop multi-lang capability in VMS, using VSs
author Sean Halle <seanhalle@yahoo.com>
date Fri, 08 Mar 2013 05:47:18 -0800
parents a8744027c1a9
children 1bf28dddc45f 93b017e30c76
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 #define _GNU_SOURCE
17 /*Copied from gnu's win32 lib
18 */
19 ssize_t
20 getline( char **lineptr, size_t *n, FILE *stream )
21 {
22 if ( lineptr == NULL || n == NULL) return -1;
23 if (*lineptr == NULL || *n == 0)
24 { *n = 500; //max length of line in a config file
25 *lineptr = (char *) malloc( *n );
26 if (*lineptr == NULL) return -1;
27 }
28 if( fgets( *lineptr, *n, stream ) )
29 return *n;
30 else
31 return -1;
32 }
34 void
35 readParamFileIntoBag( char *paramFileName, ParamBag * bag )
36 {
37 size_t lineSz = 0;
38 FILE* paramFile;
39 char* line = NULL;
41 char* paramType;// = malloc( 12 ); //"double" is the longest type
42 char* paramName;// = malloc( 500 ); //max of 500 chars in name
43 char* paramValue;// = malloc( 500 ); //max of 500 chars in value
45 lineSz = 500; //max length of line in a config file
46 line = (char *) malloc( lineSz );
47 if( line == NULL )
48 { printf( "\nIn readParamFileIntoBag: no mem for line\n" );
49 return;
50 }
53 paramFile = fopen( paramFileName, "r" );
54 if( paramFile == NULL )
55 { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); }
56 fseek( paramFile, 0, SEEK_SET );
57 while( !feof( paramFile ) )
58 { while( getline( &line, &lineSz, paramFile ) != -1 )
59 {
60 char *lineEnd = line + strlen(line) +1;
61 char *searchPos = line;
63 if( *line == '\n') continue; //blank line
64 if( *line == '/' ) continue; //comment line
66 //read the param type
67 paramType = line; //start of string
68 int foundIt = 0;
69 for( ; searchPos < lineEnd && !foundIt; searchPos++)
70 { if( *searchPos == ',' )
71 { foundIt = 1;
72 *searchPos = 0; //mark end of string
73 }
74 }
75 //get rid of leading spaces
76 for( ; searchPos < lineEnd; searchPos++)
77 { if( *searchPos != ' ' ) break;
78 }
79 //read the param name
80 paramName = searchPos;
81 foundIt = 0;
82 for( ; searchPos < lineEnd && !foundIt; searchPos++)
83 { if( *searchPos == ',' )
84 { foundIt = 1;
85 *searchPos = 0; //mark end
86 }
87 }
88 //get rid of leading spaces
89 for( ; searchPos < lineEnd; searchPos++)
90 { if( *searchPos != ' ' ) break;
91 }
92 //read the param value
93 paramValue = searchPos;
94 foundIt = 0;
95 for( ; searchPos < lineEnd && !foundIt; searchPos++)
96 { if( *searchPos == '\n' )
97 { foundIt = 1;
98 *searchPos = 0; //mark end
99 }
100 }
101 if( foundIt )
102 { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue );
103 ParamStruc *
104 paramStruc = makeParamFromStrs( paramType, paramValue );
105 addParamToBag( paramName, paramStruc, bag );
106 }
107 }
108 }
109 free( line );
110 }