comparison ReadParamsFromFile.c @ 0:481dd533f0e8

initial add
author Me
date Sat, 22 May 2010 19:50:16 -0700
parents
children 8f6d8a258491
comparison
equal deleted inserted replaced
-1:000000000000 0:57e4588e7a64
1 /*
2 * Author: SeanHalle@yahoo.com
3 *
4 * Created on June 15, 2009, 10:12 AM
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "../Matrix_Mult.h"
12
13 ParamStruc * makeParamFromStrs( char * type, char *value );
14
15 #define _GNU_SOURCE
16
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 }
33
34 void
35 readParamFileIntoBag( char *paramFileName, ParamBag * bag )
36 {
37 size_t lineSz = 0;
38 FILE* paramFile;
39 char* line = NULL;
40
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
44
45 lineSz = 500; //max length of line in a config file
46 line = (char *) malloc( lineSz );
47 if( line == NULL )
48 { BLIS_DKU__throwError( "no mem for line" );
49 return;
50 }
51
52
53 paramFile = fopen( paramFileName, "r" );
54 fseek( paramFile, 0, SEEK_SET );
55 while( !feof( paramFile ) )
56 { while( getline( &line, &lineSz, paramFile ) != -1 )
57 {
58 char *lineEnd = line + strlen(line) +1;
59 char *searchPos = line;
60
61 if( *line == '\n') continue; //blank line
62 if( *line == '/' ) continue; //comment line
63
64 //read the param type
65 paramType = line; //start of string
66 int foundIt = 0;
67 for( ; searchPos < lineEnd && !foundIt; searchPos++)
68 { if( *searchPos == ',' )
69 { foundIt = 1;
70 *searchPos = 0; //mark end of string
71 }
72 }
73 //get rid of leading spaces
74 for( ; searchPos < lineEnd; searchPos++)
75 { if( *searchPos != ' ' ) break;
76 }
77 //read the param name
78 paramName = searchPos;
79 foundIt = 0;
80 for( ; searchPos < lineEnd && !foundIt; searchPos++)
81 { if( *searchPos == ',' )
82 { foundIt = 1;
83 *searchPos = 0; //mark end
84 }
85 }
86 //get rid of leading spaces
87 for( ; searchPos < lineEnd; searchPos++)
88 { if( *searchPos != ' ' ) break;
89 }
90 //read the param value
91 paramValue = searchPos;
92 foundIt = 0;
93 for( ; searchPos < lineEnd && !foundIt; searchPos++)
94 { if( *searchPos == '\n' )
95 { foundIt = 1;
96 *searchPos = 0; //mark end
97 }
98 }
99 if( foundIt )
100 { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue );
101 ParamStruc *
102 paramStruc = makeParamFromStrs( paramType, paramValue );
103 addParamToBag( paramName, paramStruc, bag );
104 }
105 }
106 }
107 free( line );
108 }
109