Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VReo > Reo__Matrix_Mult
diff ParamHelper/ReadParamsFromFile.c @ 0:c4b1849c05ef
init add
| author | Sean Halle <seanhalle@yahoo.com> |
|---|---|
| date | Sun, 02 Feb 2014 17:58:41 -0800 |
| parents | |
| children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ParamHelper/ReadParamsFromFile.c Sun Feb 02 17:58:41 2014 -0800 1.3 @@ -0,0 +1,110 @@ 1.4 +/* 1.5 + * Author: SeanHalle@yahoo.com 1.6 + * 1.7 + * Created on June 15, 2009, 10:12 AM 1.8 + */ 1.9 + 1.10 +#include <stdio.h> 1.11 +#include <stdlib.h> 1.12 +#include <string.h> 1.13 + 1.14 +#include "../Matrix_Mult.h" 1.15 + 1.16 +ParamStruc * makeParamFromStrs( char * type, char *value ); 1.17 + 1.18 +#define _GNU_SOURCE 1.19 + 1.20 +/*Copied from gnu's win32 lib 1.21 + */ 1.22 + ssize_t 1.23 +getline( char **lineptr, size_t *n, FILE *stream ) 1.24 + { 1.25 + if ( lineptr == NULL || n == NULL) return -1; 1.26 + if (*lineptr == NULL || *n == 0) 1.27 + { *n = 500; //max length of line in a config file 1.28 + *lineptr = (char *) malloc( *n ); 1.29 + if (*lineptr == NULL) return -1; 1.30 + } 1.31 + if( fgets( *lineptr, *n, stream ) ) 1.32 + return *n; 1.33 + else 1.34 + return -1; 1.35 + } 1.36 + 1.37 + void 1.38 +readParamFileIntoBag( char *paramFileName, ParamBag * bag ) 1.39 + { 1.40 + size_t lineSz = 0; 1.41 + FILE* paramFile; 1.42 + char* line = NULL; 1.43 + 1.44 + char* paramType;// = malloc( 12 ); //"double" is the longest type 1.45 + char* paramName;// = malloc( 500 ); //max of 500 chars in name 1.46 + char* paramValue;// = malloc( 500 ); //max of 500 chars in value 1.47 + 1.48 + lineSz = 500; //max length of line in a config file 1.49 + line = (char *) malloc( lineSz ); 1.50 + if( line == NULL ) 1.51 + { printf( "\nIn readParamFileIntoBag: no mem for line\n" ); 1.52 + return; 1.53 + } 1.54 + 1.55 + 1.56 + paramFile = fopen( paramFileName, "r" ); 1.57 + if( paramFile == NULL ) {printf("\ncouldn't open file\n"); exit(0);} 1.58 + fseek( paramFile, 0, SEEK_SET ); 1.59 + while( !feof( paramFile ) ) 1.60 + { while( getline( &line, &lineSz, paramFile ) != -1 ) 1.61 + { 1.62 + char *lineEnd = line + strlen(line) +1; 1.63 + char *searchPos = line; 1.64 + 1.65 + if( *line == '\n') continue; //blank line 1.66 + if( *line == '/' ) continue; //comment line 1.67 + 1.68 + //read the param type 1.69 + paramType = line; //start of string 1.70 + int foundIt = 0; 1.71 + for( ; searchPos < lineEnd && !foundIt; searchPos++) 1.72 + { if( *searchPos == ',' ) 1.73 + { foundIt = 1; 1.74 + *searchPos = 0; //mark end of string 1.75 + } 1.76 + } 1.77 + //get rid of leading spaces 1.78 + for( ; searchPos < lineEnd; searchPos++) 1.79 + { if( *searchPos != ' ' ) break; 1.80 + } 1.81 + //read the param name 1.82 + paramName = searchPos; 1.83 + foundIt = 0; 1.84 + for( ; searchPos < lineEnd && !foundIt; searchPos++) 1.85 + { if( *searchPos == ',' ) 1.86 + { foundIt = 1; 1.87 + *searchPos = 0; //mark end 1.88 + } 1.89 + } 1.90 + //get rid of leading spaces 1.91 + for( ; searchPos < lineEnd; searchPos++) 1.92 + { if( *searchPos != ' ' ) break; 1.93 + } 1.94 + //read the param value 1.95 + paramValue = searchPos; 1.96 + foundIt = 0; 1.97 + for( ; searchPos < lineEnd && !foundIt; searchPos++) 1.98 + { if( *searchPos == '\n' ) 1.99 + { foundIt = 1; 1.100 + *searchPos = 0; //mark end 1.101 + } 1.102 + } 1.103 + if( foundIt ) 1.104 + { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue ); 1.105 + ParamStruc * 1.106 + paramStruc = makeParamFromStrs( paramType, paramValue ); 1.107 + addParamToBag( paramName, paramStruc, bag ); 1.108 + } 1.109 + } 1.110 + } 1.111 + free( line ); 1.112 + } 1.113 +
