Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > ParamHelper
view ReadParamsFromFile.c @ 2:8f6d8a258491
Not sure what changed but works
| author | Me |
|---|---|
| date | Wed, 28 Jul 2010 13:17:25 -0700 |
| parents | 481dd533f0e8 |
| children | e5c04a3a2610 |
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 "../Matrix_Mult.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 ) {printf("\ncouldn't open file\n"); exit(0);}
55 fseek( paramFile, 0, SEEK_SET );
56 while( !feof( paramFile ) )
57 { while( getline( &line, &lineSz, paramFile ) != -1 )
58 {
59 char *lineEnd = line + strlen(line) +1;
60 char *searchPos = line;
62 if( *line == '\n') continue; //blank line
63 if( *line == '/' ) continue; //comment line
65 //read the param type
66 paramType = line; //start of string
67 int foundIt = 0;
68 for( ; searchPos < lineEnd && !foundIt; searchPos++)
69 { if( *searchPos == ',' )
70 { foundIt = 1;
71 *searchPos = 0; //mark end of string
72 }
73 }
74 //get rid of leading spaces
75 for( ; searchPos < lineEnd; searchPos++)
76 { if( *searchPos != ' ' ) break;
77 }
78 //read the param name
79 paramName = searchPos;
80 foundIt = 0;
81 for( ; searchPos < lineEnd && !foundIt; searchPos++)
82 { if( *searchPos == ',' )
83 { foundIt = 1;
84 *searchPos = 0; //mark end
85 }
86 }
87 //get rid of leading spaces
88 for( ; searchPos < lineEnd; searchPos++)
89 { if( *searchPos != ' ' ) break;
90 }
91 //read the param value
92 paramValue = searchPos;
93 foundIt = 0;
94 for( ; searchPos < lineEnd && !foundIt; searchPos++)
95 { if( *searchPos == '\n' )
96 { foundIt = 1;
97 *searchPos = 0; //mark end
98 }
99 }
100 if( foundIt )
101 { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue );
102 ParamStruc *
103 paramStruc = makeParamFromStrs( paramType, paramValue );
104 addParamToBag( paramName, paramStruc, bag );
105 }
106 }
107 }
108 free( line );
109 }
