Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > ParamHelper
view ReadParamsFromFile.c @ 22:943e99459739
adding netbeans project directories to repository
| author | Sean Halle <seanhalle@yahoo.com> |
|---|---|
| date | Fri, 14 Feb 2014 07:49:50 -0800 |
| parents | 740df4b46535 |
| children |
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 <PR__include/prparam.h>
12 #include <PR__include/prmalloc.h>
14 ParamStruc * makeParamFromStrs( char * type, char *value );
16 #ifndef _GNU_SOURCE
17 #define _GNU_SOURCE
18 #endif
20 /*Copied from gnu's win32 lib
21 */
22 ssize_t
23 getline( char **lineptr, size_t *n, FILE *stream )
24 {
25 if ( lineptr == NULL || n == NULL) return -1;
26 if (*lineptr == NULL || *n == 0)
27 { *n = 500; //max length of line in a config file
28 *lineptr = (char *) PR__malloc( *n );
29 if (*lineptr == NULL) return -1;
30 }
31 if( fgets( *lineptr, *n, stream ) )
32 return *n;
33 else
34 return -1;
35 }
37 void
38 readParamFileIntoBag( char *paramFileName, ParamBag * bag )
39 {
40 size_t lineSz = 0;
41 FILE* paramFile;
42 char* line = NULL;
44 char* paramType;// = malloc( 12 ); //"double" is the longest type
45 char* paramName;// = malloc( 500 ); //max of 500 chars in name
46 char* paramValue;// = malloc( 500 ); //max of 500 chars in value
48 lineSz = 500; //max length of line in a config file
49 line = (char *) PR__malloc( lineSz );
50 if( line == NULL )
51 { printf( "\nIn readParamFileIntoBag: no mem for line\n" );
52 return;
53 }
56 paramFile = fopen( paramFileName, "r" );
57 if( paramFile == NULL )
58 { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); }
59 fseek( paramFile, 0, SEEK_SET );
60 while( !feof( paramFile ) )
61 { while( getline( &line, &lineSz, paramFile ) != -1 )
62 {
63 char *lineEnd = line + strlen(line) +1;
64 char *searchPos = line;
66 if( *line == '\n') continue; //blank line
67 if( *line == '/' ) continue; //comment line
69 //read the param type
70 paramType = line; //start of string
71 int foundIt = 0;
72 for( ; searchPos < lineEnd && !foundIt; searchPos++)
73 { if( *searchPos == ',' )
74 { foundIt = 1;
75 *searchPos = 0; //mark end of string
76 }
77 }
78 //get rid of leading spaces
79 for( ; searchPos < lineEnd; searchPos++)
80 { if( *searchPos != ' ' ) break;
81 }
82 //read the param name
83 paramName = searchPos;
84 foundIt = 0;
85 for( ; searchPos < lineEnd && !foundIt; searchPos++)
86 { if( *searchPos == ',' )
87 { foundIt = 1;
88 *searchPos = 0; //mark end
89 }
90 }
91 //get rid of leading spaces
92 for( ; searchPos < lineEnd; searchPos++)
93 { if( *searchPos != ' ' ) break;
94 }
95 //read the param value
96 paramValue = searchPos;
97 foundIt = 0;
98 for( ; searchPos < lineEnd && !foundIt; searchPos++)
99 { if( *searchPos == '\n' )
100 { foundIt = 1;
101 *searchPos = 0; //mark end
102 }
103 }
104 if( foundIt )
105 { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue );
106 ParamStruc *
107 paramStruc = makeParamFromStrs( paramType, paramValue );
108 addParamToBag( paramName, paramStruc, bag );
109 }
110 }
111 }
112 PR__free( line );
113 }
