Me@6: /* Me@6: * Author: SeanHalle@yahoo.com Me@6: * Me@6: * Created on June 15, 2009, 10:12 AM Me@6: */ Me@6: Me@6: #include Me@6: #include Me@6: #include Me@6: seanhalle@21: #include seanhalle@21: #include Me@6: Me@6: ParamStruc * makeParamFromStrs( char * type, char *value ); Me@6: nengel@16: #ifndef _GNU_SOURCE Me@6: #define _GNU_SOURCE nengel@16: #endif Me@6: Me@6: /*Copied from gnu's win32 lib Me@6: */ Me@6: ssize_t Me@6: getline( char **lineptr, size_t *n, FILE *stream ) Me@6: { Me@6: if ( lineptr == NULL || n == NULL) return -1; Me@6: if (*lineptr == NULL || *n == 0) Me@6: { *n = 500; //max length of line in a config file seanhalle@19: *lineptr = (char *) PR__malloc( *n ); Me@6: if (*lineptr == NULL) return -1; Me@6: } Me@6: if( fgets( *lineptr, *n, stream ) ) Me@6: return *n; Me@6: else Me@6: return -1; Me@6: } Me@6: Me@6: void Me@6: readParamFileIntoBag( char *paramFileName, ParamBag * bag ) Me@6: { Me@6: size_t lineSz = 0; Me@6: FILE* paramFile; Me@6: char* line = NULL; Me@6: Me@6: char* paramType;// = malloc( 12 ); //"double" is the longest type Me@6: char* paramName;// = malloc( 500 ); //max of 500 chars in name Me@6: char* paramValue;// = malloc( 500 ); //max of 500 chars in value Me@6: Me@6: lineSz = 500; //max length of line in a config file seanhalle@19: line = (char *) PR__malloc( lineSz ); Me@6: if( line == NULL ) Me@6: { printf( "\nIn readParamFileIntoBag: no mem for line\n" ); Me@6: return; Me@6: } Me@6: Me@6: Me@6: paramFile = fopen( paramFileName, "r" ); Me@6: if( paramFile == NULL ) Me@6: { printf("\ncouldn't open param file: %s\n", paramFileName); exit(0); } Me@6: fseek( paramFile, 0, SEEK_SET ); Me@6: while( !feof( paramFile ) ) Me@6: { while( getline( &line, &lineSz, paramFile ) != -1 ) Me@6: { Me@6: char *lineEnd = line + strlen(line) +1; Me@6: char *searchPos = line; Me@6: Me@6: if( *line == '\n') continue; //blank line Me@6: if( *line == '/' ) continue; //comment line Me@6: Me@6: //read the param type Me@6: paramType = line; //start of string Me@6: int foundIt = 0; Me@6: for( ; searchPos < lineEnd && !foundIt; searchPos++) Me@6: { if( *searchPos == ',' ) Me@6: { foundIt = 1; Me@6: *searchPos = 0; //mark end of string Me@6: } Me@6: } Me@6: //get rid of leading spaces Me@6: for( ; searchPos < lineEnd; searchPos++) Me@6: { if( *searchPos != ' ' ) break; Me@6: } Me@6: //read the param name Me@6: paramName = searchPos; Me@6: foundIt = 0; Me@6: for( ; searchPos < lineEnd && !foundIt; searchPos++) Me@6: { if( *searchPos == ',' ) Me@6: { foundIt = 1; Me@6: *searchPos = 0; //mark end Me@6: } Me@6: } Me@6: //get rid of leading spaces Me@6: for( ; searchPos < lineEnd; searchPos++) Me@6: { if( *searchPos != ' ' ) break; Me@6: } Me@6: //read the param value Me@6: paramValue = searchPos; Me@6: foundIt = 0; Me@6: for( ; searchPos < lineEnd && !foundIt; searchPos++) Me@6: { if( *searchPos == '\n' ) Me@6: { foundIt = 1; Me@6: *searchPos = 0; //mark end Me@6: } Me@6: } Me@6: if( foundIt ) Me@6: { printf("Found: %s, %s, %s\n", paramType, paramName, paramValue ); Me@6: ParamStruc * Me@6: paramStruc = makeParamFromStrs( paramType, paramValue ); Me@6: addParamToBag( paramName, paramStruc, bag ); Me@6: } Me@6: } Me@6: } seanhalle@19: PR__free( line ); Me@6: } Me@6: