annotate 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
rev   line source
Me@6 1 /*
Me@6 2 * Author: SeanHalle@yahoo.com
Me@6 3 *
Me@6 4 * Created on June 15, 2009, 10:12 AM
Me@6 5 */
Me@6 6
Me@6 7 #include <stdio.h>
Me@6 8 #include <stdlib.h>
Me@6 9 #include <string.h>
Me@6 10
Me@6 11 #include "Param.h"
Me@6 12
Me@6 13 ParamStruc * makeParamFromStrs( char * type, char *value );
Me@6 14
Me@6 15 #define _GNU_SOURCE
Me@6 16
Me@6 17 /*Copied from gnu's win32 lib
Me@6 18 */
Me@6 19 ssize_t
Me@6 20 getline( char **lineptr, size_t *n, FILE *stream )
Me@6 21 {
Me@6 22 if ( lineptr == NULL || n == NULL) return -1;
Me@6 23 if (*lineptr == NULL || *n == 0)
Me@6 24 { *n = 500; //max length of line in a config file
Me@6 25 *lineptr = (char *) malloc( *n );
Me@6 26 if (*lineptr == NULL) return -1;
Me@6 27 }
Me@6 28 if( fgets( *lineptr, *n, stream ) )
Me@6 29 return *n;
Me@6 30 else
Me@6 31 return -1;
Me@6 32 }
Me@6 33
Me@6 34 void
Me@6 35 readParamFileIntoBag( char *paramFileName, ParamBag * bag )
Me@6 36 {
Me@6 37 size_t lineSz = 0;
Me@6 38 FILE* paramFile;
Me@6 39 char* line = NULL;
Me@6 40
Me@6 41 char* paramType;// = malloc( 12 ); //"double" is the longest type
Me@6 42 char* paramName;// = malloc( 500 ); //max of 500 chars in name
Me@6 43 char* paramValue;// = malloc( 500 ); //max of 500 chars in value
Me@6 44
Me@6 45 lineSz = 500; //max length of line in a config file
Me@6 46 line = (char *) malloc( lineSz );
Me@6 47 if( line == NULL )
Me@6 48 { printf( "\nIn readParamFileIntoBag: no mem for line\n" );
Me@6 49 return;
Me@6 50 }
Me@6 51
Me@6 52
Me@6 53 paramFile = fopen( paramFileName, "r" );
Me@6 54 if( paramFile == NULL )
Me@6 55 { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); }
Me@6 56 fseek( paramFile, 0, SEEK_SET );
Me@6 57 while( !feof( paramFile ) )
Me@6 58 { while( getline( &line, &lineSz, paramFile ) != -1 )
Me@6 59 {
Me@6 60 char *lineEnd = line + strlen(line) +1;
Me@6 61 char *searchPos = line;
Me@6 62
Me@6 63 if( *line == '\n') continue; //blank line
Me@6 64 if( *line == '/' ) continue; //comment line
Me@6 65
Me@6 66 //read the param type
Me@6 67 paramType = line; //start of string
Me@6 68 int foundIt = 0;
Me@6 69 for( ; searchPos < lineEnd && !foundIt; searchPos++)
Me@6 70 { if( *searchPos == ',' )
Me@6 71 { foundIt = 1;
Me@6 72 *searchPos = 0; //mark end of string
Me@6 73 }
Me@6 74 }
Me@6 75 //get rid of leading spaces
Me@6 76 for( ; searchPos < lineEnd; searchPos++)
Me@6 77 { if( *searchPos != ' ' ) break;
Me@6 78 }
Me@6 79 //read the param name
Me@6 80 paramName = searchPos;
Me@6 81 foundIt = 0;
Me@6 82 for( ; searchPos < lineEnd && !foundIt; searchPos++)
Me@6 83 { if( *searchPos == ',' )
Me@6 84 { foundIt = 1;
Me@6 85 *searchPos = 0; //mark end
Me@6 86 }
Me@6 87 }
Me@6 88 //get rid of leading spaces
Me@6 89 for( ; searchPos < lineEnd; searchPos++)
Me@6 90 { if( *searchPos != ' ' ) break;
Me@6 91 }
Me@6 92 //read the param value
Me@6 93 paramValue = searchPos;
Me@6 94 foundIt = 0;
Me@6 95 for( ; searchPos < lineEnd && !foundIt; searchPos++)
Me@6 96 { if( *searchPos == '\n' )
Me@6 97 { foundIt = 1;
Me@6 98 *searchPos = 0; //mark end
Me@6 99 }
Me@6 100 }
Me@6 101 if( foundIt )
Me@6 102 { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue );
Me@6 103 ParamStruc *
Me@6 104 paramStruc = makeParamFromStrs( paramType, paramValue );
Me@6 105 addParamToBag( paramName, paramStruc, bag );
Me@6 106 }
Me@6 107 }
Me@6 108 }
Me@6 109 free( line );
Me@6 110 }
Me@6 111