diff options
Diffstat (limited to 'tester/covoar/ConfigFile.cc')
-rw-r--r-- | tester/covoar/ConfigFile.cc | 138 |
1 files changed, 61 insertions, 77 deletions
diff --git a/tester/covoar/ConfigFile.cc b/tester/covoar/ConfigFile.cc index c16b64a..1fbefd1 100644 --- a/tester/covoar/ConfigFile.cc +++ b/tester/covoar/ConfigFile.cc @@ -7,14 +7,21 @@ #include "ConfigFile.h" #include <string.h> -#include <stdio.h> #include <ctype.h> +#include <iostream> +#include <fstream> +#include <sstream> + +static void print_invalid_line_number( const std::string& file, int line_no ) +{ + std::cerr << file << ": line" << line_no << " is invalid: " << line + << std::endl; +} + namespace Configuration { - FileReader::FileReader( - Options_t *options - ) + FileReader::FileReader( Options_t *options ) { options_m = options; } @@ -23,44 +30,40 @@ namespace Configuration { { } - bool FileReader::processFile( - const char* const file - ) + bool FileReader::processFile( const std::string& file ) { #define METHOD "FileReader::processFile - " - FILE * in; - char line[256]; - char option[256]; - char value[256]; - int line_no; - int i; - int j; - - if ( file == NULL ) { - fprintf( stderr, METHOD "NULL filename\n" ); + #define MAX_LENGTH 256 + + std::ifstream in; + std::string line; + char option[MAX_LENGTH]; + char value[MAX_LENGTH]; + int line_no; + int i; + int j; + + if ( file.empty() ) { + std::cerr << METHOD << "Empty filename" << std::endl; return false; } - in = fopen( file, "r" ); - if ( !in ) { - fprintf( stderr, METHOD "unable to open %s\n", file ); + in.open( file ); + if ( !in.is_open() ) { + std::cerr << METHOD << "unable to open " << file << std::endl; return false; } line_no = 0; - while (fgets(line, sizeof(line), in) != NULL) { + while ( std::getline( line, MAX_LENGTH ) ) { int length; line_no++; - length = (int) strlen( line ); - if ( line[length - 1] != '\n' ) { - fprintf( - stderr, - "%s: line %d is too long", - file, - line_no - ); + length = (int) line.length(); + if ( length > MAX_LENGTH ) { + std::cerr << file << ": line " << line_no << " is too long" + << std::endl; continue; } @@ -72,7 +75,7 @@ namespace Configuration { * * LHS = RHS # comment */ - for (i=0 ; i<length ; i++ ) { + for ( i = 0; i < length; i++ ) { if ( line[i] == '#' ) { line[i] = '\0'; length = i; @@ -83,70 +86,51 @@ namespace Configuration { /* * Strip off trailing white space */ - for (i=length-1 ; i>=0 && isspace(line[i]) ; i-- ) + for ( i = length - 1; i >= 0 && isspace( line[i] ); i-- ) ; line[i+1] = '\0'; - length = i+1; + length = i + 1; /* Ignore empty lines. We have stripped * all comments and blanks therefore, only * an empty string needs to be checked. */ - if (line[0] == '\0') + if ( line[0] == '\0' ) { continue; + } - if (sscanf(line, "%s", option) != 1) { - fprintf( - stderr, - "%s: line %d is invalid: %s\n", - file, - line_no, - line - ); + if ( std::sscanf( line.c_str(), "%s", option ) != 1 ) { + print_invalid_line_number( file, line_no ); continue; } - for (i=0; ((line[i] != '=') && (i<length)); i++) + for ( i=0; ( ( line[i] != '=' ) && ( i < length ) ); i++ ) ; - if (i == length) { - fprintf( - stderr, - "%s: line %d is invalid: %s\n", - file, - line_no, - line - ); + if ( i == length ) { + print_invalid_line_number( file, line_no ); continue; } i++; value[0] = '\0'; - while ( isspace(line[i]) ) + while ( isspace( line[i] ) ) { i++; - for (j=0; line[i] != '\0'; i++, j++ ) + } + + for ( j = 0; line[i] != '\0'; i++, j++ ) { value[j] = line[i]; + } + value[j] = '\0'; - if (value[0] == '\0') { - fprintf( - stderr, - "%s: line %d is invalid: %s\n", - file, - line_no, - line - ); + if ( value[0] == '\0' ) { + print_invalid_line_number( file, line_no ); continue; } - if ( !setOption(option, value) ) { - fprintf( - stderr, - "%s: line %d: option %s is unknown\n", - file, - line_no, - option - ); + if ( !setOption( option, value ) ) { + print_invalid_line_number( file, line_no ); continue; } @@ -160,37 +144,37 @@ namespace Configuration { const char* const value ) { - Options_t *o; + Options_t* o; - for ( o=options_m ; o->option ; o++ ) { + for ( o = options_m; o->option; o++ ) { if ( !strcmp( o->option, option ) ) { o->value = strdup( value ); return true; } } + return false; } - const char *FileReader::getOption( - const char* const option - ) + const char *FileReader::getOption( const char* const option ) { Options_t *o; - for ( o=options_m ; o->option ; o++ ) { + for ( o = options_m; o->option; o++ ) { if ( !strcmp( o->option, option ) ) { return o->value; } } + return NULL; } - void FileReader::printOptions(void) + void FileReader::printOptions() { Options_t *o; - for ( o=options_m ; o->option ; o++ ) { - fprintf( stderr, "(%s)=(%s)\n", o->option, o->value ); + for ( o = options_m; o->option; o++ ) { + std::cerr << '(' << o->option << ")=(" << o->value << ')' << std::endl; } } } |