annotate ReadParamsFromFile.c @ 19:740df4b46535

new branch -- for universal -- changed malloc and free -- do PR__start before use
author Sean Halle <seanhalle@yahoo.com>
date Tue, 23 Jul 2013 07:20:05 -0700
parents 93b017e30c76
children 5837ad792168
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"
seanhalle@19 12 #include "PR__common_includes/Services_offered_by_PR/Memory_Handling/vmalloc__wrapper_library.h"
Me@6 13
Me@6 14 ParamStruc * makeParamFromStrs( char * type, char *value );
Me@6 15
nengel@16 16 #ifndef _GNU_SOURCE
Me@6 17 #define _GNU_SOURCE
nengel@16 18 #endif
Me@6 19
Me@6 20 /*Copied from gnu's win32 lib
Me@6 21 */
Me@6 22 ssize_t
Me@6 23 getline( char **lineptr, size_t *n, FILE *stream )
Me@6 24 {
Me@6 25 if ( lineptr == NULL || n == NULL) return -1;
Me@6 26 if (*lineptr == NULL || *n == 0)
Me@6 27 { *n = 500; //max length of line in a config file
seanhalle@19 28 *lineptr = (char *) PR__malloc( *n );
Me@6 29 if (*lineptr == NULL) return -1;
Me@6 30 }
Me@6 31 if( fgets( *lineptr, *n, stream ) )
Me@6 32 return *n;
Me@6 33 else
Me@6 34 return -1;
Me@6 35 }
Me@6 36
Me@6 37 void
Me@6 38 readParamFileIntoBag( char *paramFileName, ParamBag * bag )
Me@6 39 {
Me@6 40 size_t lineSz = 0;
Me@6 41 FILE* paramFile;
Me@6 42 char* line = NULL;
Me@6 43
Me@6 44 char* paramType;// = malloc( 12 ); //"double" is the longest type
Me@6 45 char* paramName;// = malloc( 500 ); //max of 500 chars in name
Me@6 46 char* paramValue;// = malloc( 500 ); //max of 500 chars in value
Me@6 47
Me@6 48 lineSz = 500; //max length of line in a config file
seanhalle@19 49 line = (char *) PR__malloc( lineSz );
Me@6 50 if( line == NULL )
Me@6 51 { printf( "\nIn readParamFileIntoBag: no mem for line\n" );
Me@6 52 return;
Me@6 53 }
Me@6 54
Me@6 55
Me@6 56 paramFile = fopen( paramFileName, "r" );
Me@6 57 if( paramFile == NULL )
Me@6 58 { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); }
Me@6 59 fseek( paramFile, 0, SEEK_SET );
Me@6 60 while( !feof( paramFile ) )
Me@6 61 { while( getline( &line, &lineSz, paramFile ) != -1 )
Me@6 62 {
Me@6 63 char *lineEnd = line + strlen(line) +1;
Me@6 64 char *searchPos = line;
Me@6 65
Me@6 66 if( *line == '\n') continue; //blank line
Me@6 67 if( *line == '/' ) continue; //comment line
Me@6 68
Me@6 69 //read the param type
Me@6 70 paramType = line; //start of string
Me@6 71 int foundIt = 0;
Me@6 72 for( ; searchPos < lineEnd && !foundIt; searchPos++)
Me@6 73 { if( *searchPos == ',' )
Me@6 74 { foundIt = 1;
Me@6 75 *searchPos = 0; //mark end of string
Me@6 76 }
Me@6 77 }
Me@6 78 //get rid of leading spaces
Me@6 79 for( ; searchPos < lineEnd; searchPos++)
Me@6 80 { if( *searchPos != ' ' ) break;
Me@6 81 }
Me@6 82 //read the param name
Me@6 83 paramName = searchPos;
Me@6 84 foundIt = 0;
Me@6 85 for( ; searchPos < lineEnd && !foundIt; searchPos++)
Me@6 86 { if( *searchPos == ',' )
Me@6 87 { foundIt = 1;
Me@6 88 *searchPos = 0; //mark end
Me@6 89 }
Me@6 90 }
Me@6 91 //get rid of leading spaces
Me@6 92 for( ; searchPos < lineEnd; searchPos++)
Me@6 93 { if( *searchPos != ' ' ) break;
Me@6 94 }
Me@6 95 //read the param value
Me@6 96 paramValue = searchPos;
Me@6 97 foundIt = 0;
Me@6 98 for( ; searchPos < lineEnd && !foundIt; searchPos++)
Me@6 99 { if( *searchPos == '\n' )
Me@6 100 { foundIt = 1;
Me@6 101 *searchPos = 0; //mark end
Me@6 102 }
Me@6 103 }
Me@6 104 if( foundIt )
Me@6 105 { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue );
Me@6 106 ParamStruc *
Me@6 107 paramStruc = makeParamFromStrs( paramType, paramValue );
Me@6 108 addParamToBag( paramName, paramStruc, bag );
Me@6 109 }
Me@6 110 }
Me@6 111 }
seanhalle@19 112 PR__free( line );
Me@6 113 }
Me@6 114