From 5c7c23f4d6e63d5a14cadcf0c8b89d742f1be7f7 Mon Sep 17 00:00:00 2001 From: Thomas Heinemann Date: Mon, 20 Aug 2012 14:18:34 +0200 Subject: [PATCH] Some minor changes (especially limiting the range of variables if possible) --- src/exceptions/file_IO_exception.h | 2 +- src/exceptions/wrong_file_format.h | 2 +- src/parser/read_tra_file.cpp | 24 +++++++++++++----------- test/parser/readme.txt | 3 +++ 4 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 test/parser/readme.txt diff --git a/src/exceptions/file_IO_exception.h b/src/exceptions/file_IO_exception.h index 8f19652e6..b2270fbe2 100644 --- a/src/exceptions/file_IO_exception.h +++ b/src/exceptions/file_IO_exception.h @@ -2,7 +2,7 @@ * file_IO_exception.h * * Created on: 16.08.2012 - * Author: thomas + * Author: Thomas Heinemann */ #ifndef MRMC_EXCEPTIONS_FILE_IO_EXCEPTION_H_ diff --git a/src/exceptions/wrong_file_format.h b/src/exceptions/wrong_file_format.h index 27ebbb45e..afe996733 100644 --- a/src/exceptions/wrong_file_format.h +++ b/src/exceptions/wrong_file_format.h @@ -2,7 +2,7 @@ * wrong_file_format.h * * Created on: 16.08.2012 - * Author: thomas + * Author: Thomas Heinemann */ #ifndef WRONG_FILE_FORMAT_H_ diff --git a/src/parser/read_tra_file.cpp b/src/parser/read_tra_file.cpp index ad15b89e9..86dbb51c2 100644 --- a/src/parser/read_tra_file.cpp +++ b/src/parser/read_tra_file.cpp @@ -40,10 +40,8 @@ static uint_fast32_t make_first_pass(FILE* p) { pantheios::log_ERROR("make_first_pass was called with NULL! (SHOULD NEVER HAPPEN)"); throw exceptions::file_IO_exception ("make_first_pass: File not readable (this should be checked before calling this function!)"); } - - char s[1024], states[7], transitions[12]; - int rows=0, row=0, col=0, non_zero=0; - double val=0.0; + char s[1024]; //String buffer + uint_fast32_t rows=0, non_zero=0; //Reading No. of states if (fgets(s, 1024, p) != NULL) { @@ -64,6 +62,8 @@ static uint_fast32_t make_first_pass(FILE* p) { //And increase number of transitions while (NULL != fgets( s, 1024, p )) { + uint_fast32_t row=0, col=0; + double val=0.0; if (sscanf( s, "%d%d%lf", &row, &col, &val ) != 3) { throw mrmc::exceptions::wrong_file_format(); } @@ -84,10 +84,8 @@ static uint_fast32_t make_first_pass(FILE* p) { sparse::StaticSparseMatrix * read_tra_file(const char * filename) { FILE *p; - char s[1024]; - uint_fast32_t rows, row, col, nnz, non_zero; - double val = 0.0; - int count = 0; + char s[1024]; + uint_fast32_t rows, non_zero; sparse::StaticSparseMatrix *sp = NULL; p = fopen(filename, "r"); @@ -108,8 +106,12 @@ sparse::StaticSparseMatrix * read_tra_file(const char * filename) { } } - //Reading No. of transitions + /* Reading No. of transitions + * Note that the result is not used in this function as make_first_pass() + * computes the relevant number (non_zero) + */ if (fgets(s, 1024, p) != NULL) { + uint_fast32_t nnz=0; if (sscanf( s, "TRANSITIONS %d", &nnz) == 0) { throw mrmc::exceptions::wrong_file_format(); } @@ -133,6 +135,8 @@ sparse::StaticSparseMatrix * read_tra_file(const char * filename) { //Reading transitions (one per line) and saving the results in the matrix while (NULL != fgets( s, 1024, p )) { + uint_fast32_t row=0, col=0; + double val = 0.0; if (sscanf( s, "%d%d%lf", &row, &col, &val) != 3) { throw mrmc::exceptions::wrong_file_format(); } @@ -142,12 +146,10 @@ sparse::StaticSparseMatrix * read_tra_file(const char * filename) { pantheios::integer(row), " x ", pantheios::integer(col)); sp->addNextValue(row,col,val); - ++count; } (void)fclose(p); - pantheios::log_DEBUG("Written ", pantheios::integer(count), " Elements"); pantheios::log_DEBUG("Finalizing Matrix"); sp->finalize(); return sp; diff --git a/test/parser/readme.txt b/test/parser/readme.txt new file mode 100644 index 000000000..114fd8ab0 --- /dev/null +++ b/test/parser/readme.txt @@ -0,0 +1,3 @@ +Note: *.tra-Files starting with "wrong_format" are to produce an error when +being parsed. Hence, spelling errors and wrong formats are on purpose. +DO NOT CORRECT THEM! \ No newline at end of file