Browse Source

some more reformatting

tempestpy_adaptions
gereon 12 years ago
parent
commit
64bf554cad
  1. 38
      src/parser/parser.cpp
  2. 6
      src/parser/parser.h
  3. 44
      src/parser/readLabFile.cpp
  4. 6
      src/parser/readLabFile.h
  5. 15
      src/parser/readTraFile.cpp
  6. 6
      src/parser/readTraFile.h
  7. 6
      src/utility/settings.cpp

38
src/parser/parser.cpp

@ -27,11 +27,9 @@ extern log4cplus::Logger logger;
* @param end New pointer will be written there
* @return Result of strtol()
*/
uint_fast64_t mrmc::parser::Parser::checked_strtol(const char* str, char** end)
{
uint_fast64_t mrmc::parser::Parser::checked_strtol(const char* str, char** end) {
uint_fast64_t res = strtol(str, end, 10);
if (str == *end)
{
if (str == *end) {
LOG4CPLUS_ERROR(logger, "Error while parsing integer. Next input token is not a number.");
LOG4CPLUS_ERROR(logger, "\tUpcoming input is: \"" << std::string(str, 0, 16) << "\"");
throw mrmc::exceptions::wrong_file_format();
@ -45,8 +43,7 @@ uint_fast64_t mrmc::parser::Parser::checked_strtol(const char* str, char** end)
* @param buf String buffer
* @return pointer to first non-whitespace character
*/
char* mrmc::parser::Parser::skipWS(char* buf)
{
char* mrmc::parser::Parser::skipWS(char* buf) {
while ((*buf == ' ') || (*buf == '\t') || (*buf == '\n') || (*buf == '\r')) buf++;
return buf;
}
@ -57,33 +54,29 @@ char* mrmc::parser::Parser::skipWS(char* buf)
* and a log entry is written.
* @param filename file to be opened
*/
mrmc::parser::MappedFile::MappedFile(const char* filename)
{
mrmc::parser::MappedFile::MappedFile(const char* filename) {
#if defined LINUX || defined MACOSX
/*
* Do file mapping for reasonable systems.
* stat64(), open(), mmap()
*/
#ifdef MACOSX
if (stat(filename, &(this->st)) != 0)
if (stat(filename, &(this->st)) != 0) {
#else
if (stat64(filename, &(this->st)) != 0)
if (stat64(filename, &(this->st)) != 0) {
#endif
{
LOG4CPLUS_ERROR(logger, "Error in stat(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in stat()");
}
this->file = open(filename, O_RDONLY);
if (this->file < 0)
{
if (this->file < 0) {
LOG4CPLUS_ERROR(logger, "Error in open(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in open()");
}
this->data = (char*) mmap(NULL, this->st.st_size, PROT_READ, MAP_PRIVATE, this->file, 0);
if (this->data == (char*)-1)
{
if (this->data == (char*)-1) {
close(this->file);
LOG4CPLUS_ERROR(logger, "Error in mmap(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in mmap()");
@ -94,30 +87,26 @@ mrmc::parser::MappedFile::MappedFile(const char* filename)
* Do file mapping for windows.
* _stat64(), CreateFile(), CreateFileMapping(), MapViewOfFile()
*/
if (_stat64(filename, &(this->st)) != 0)
{
if (_stat64(filename, &(this->st)) != 0) {
LOG4CPLUS_ERROR(logger, "Error in _stat(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in stat()");
}
this->file = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (this->file == INVALID_HANDLE_VALUE)
{
if (this->file == INVALID_HANDLE_VALUE) {
LOG4CPLUS_ERROR(logger, "Error in CreateFileA(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in CreateFileA()");
}
this->mapping = CreateFileMappingA(this->file, NULL, PAGE_READONLY, (DWORD)(st.st_size >> 32), (DWORD)st.st_size, NULL);
if (this->mapping == NULL)
{
if (this->mapping == NULL) {
CloseHandle(this->file);
LOG4CPLUS_ERROR(logger, "Error in CreateFileMappingA(" << filename << ").");
throw exceptions::file_IO_exception("mrmc::parser::MappedFile Error in CreateFileMappingA()");
}
this->data = static_cast<char*>(MapViewOfFile(this->mapping, FILE_MAP_READ, 0, 0, this->st.st_size));
if (this->data == NULL)
{
if (this->data == NULL) {
CloseHandle(this->mapping);
CloseHandle(this->file);
LOG4CPLUS_ERROR(logger, "Error in MapViewOfFile(" << filename << ").");
@ -130,8 +119,7 @@ mrmc::parser::MappedFile::MappedFile(const char* filename)
/*!
* Will unmap the data and close the file.
*/
mrmc::parser::MappedFile::~MappedFile()
{
mrmc::parser::MappedFile::~MappedFile() {
#if defined LINUX || defined MACOSX
munmap(this->data, this->st.st_size);
close(this->file);

6
src/parser/parser.h

@ -52,8 +52,7 @@ namespace parser {
#error Platform not supported
#endif
class MappedFile
{
class MappedFile {
private:
#if defined LINUX || defined MACOSX
/*!
@ -104,8 +103,7 @@ namespace parser {
~MappedFile();
};
class Parser
{
class Parser {
protected:
/*!
* @brief Parses integer and checks, if something has been parsed.

44
src/parser/readLabFile.cpp

@ -69,37 +69,31 @@ LabParser::LabParser(uint_fast64_t node_count, const char * filename)
/*
* iterate over tokens until we hit #END or end of file
*/
while(buf[0] != '\0')
{
while(buf[0] != '\0') {
buf += cnt;
cnt = strcspn(buf, separator); // position of next separator
if (cnt > 0)
{
if (cnt > 0) {
/*
* next token is #DECLARATION: just skip it
* next token is #END: stop search
* otherwise increase proposition_count
*/
if (strncmp(buf, "#DECLARATION", cnt) == 0)
{
if (strncmp(buf, "#DECLARATION", cnt) == 0) {
foundDecl = true;
continue;
}
else if (strncmp(buf, "#END", cnt) == 0)
{
else if (strncmp(buf, "#END", cnt) == 0) {
foundEnd = true;
break;
}
proposition_count++;
}
else buf++; // next char is separator, one step forward
} else buf++; // next char is separator, one step forward
}
/*
* If #DECLARATION or #END were not found, the file format is wrong
*/
if (! (foundDecl && foundEnd))
{
if (! (foundDecl && foundEnd)) {
LOG4CPLUS_ERROR(logger, "Wrong file format in (" << filename << "). File header is corrupted.");
if (! foundDecl) LOG4CPLUS_ERROR(logger, "\tDid not find #DECLARATION token.");
if (! foundEnd) LOG4CPLUS_ERROR(logger, "\tDid not find #END token.");
@ -125,20 +119,16 @@ LabParser::LabParser(uint_fast64_t node_count, const char * filename)
*/
char proposition[128]; // buffer for proposition names
size_t cnt = 0;
do
{
do {
buf += cnt;
cnt = strcspn(buf, separator); // position of next separator
if (cnt >= sizeof(proposition))
{
if (cnt >= sizeof(proposition)) {
/*
* if token is longer than our buffer, the following strncpy code might get risky...
*/
LOG4CPLUS_ERROR(logger, "Wrong file format in (" << filename << "). Atomic proposition with length > " << (sizeof(proposition)-1) << " was found.");
throw mrmc::exceptions::wrong_file_format();
}
else if (cnt > 0)
{
} else if (cnt > 0) {
/*
* next token is: #DECLARATION: just skip it
* next token is: #END: stop search
@ -149,8 +139,7 @@ LabParser::LabParser(uint_fast64_t node_count, const char * filename)
strncpy(proposition, buf, cnt);
proposition[cnt] = '\0';
this->labeling->addAtomicProposition(proposition);
}
else cnt = 1; // next char is separator, one step forward
} else cnt = 1; // next char is separator, one step forward
} while (cnt > 0);
/*
* Right here, the buf pointer is still pointing to our last token,
@ -169,17 +158,14 @@ LabParser::LabParser(uint_fast64_t node_count, const char * filename)
/*
* iterate over nodes
*/
while (buf[0] != '\0')
{
while (buf[0] != '\0') {
/*
* parse node number, then iterate over propositions
*/
node = checked_strtol(buf, &buf);
while ((buf[0] != '\n') && (buf[0] != '\0'))
{
while ((buf[0] != '\n') && (buf[0] != '\0')) {
cnt = strcspn(buf, separator);
if (cnt == 0)
{
if (cnt == 0) {
/*
* next char is a separator
* if it's a newline, we continue with next node
@ -187,9 +173,7 @@ LabParser::LabParser(uint_fast64_t node_count, const char * filename)
*/
if (buf[0] == '\n') break;
buf++;
}
else
{
} else {
/*
* copy proposition to buffer and add it to labeling
*/

6
src/parser/readLabFile.h

@ -17,13 +17,11 @@ namespace parser {
* Note that this class creates a new AtomicPropositionsLabeling object that can
* be accessed via getLabeling(). However, it will not delete this object!
*/
class LabParser : Parser
{
class LabParser : Parser {
public:
LabParser(uint_fast64_t node_count, const char* filename);
std::shared_ptr<mrmc::models::AtomicPropositionsLabeling> getLabeling()
{
std::shared_ptr<mrmc::models::AtomicPropositionsLabeling> getLabeling() {
return this->labeling;
}

15
src/parser/readTraFile.cpp

@ -48,23 +48,20 @@ namespace parser{
* @param buf Data to scan. Is expected to be some char array.
* @param maxnode Is set to highest id of all nodes.
*/
uint_fast64_t TraParser::firstPass(char* buf, uint_fast64_t &maxnode)
{
uint_fast64_t TraParser::firstPass(char* buf, uint_fast64_t &maxnode) {
uint_fast64_t non_zero = 0;
/*
* check file header and extract number of transitions
*/
if (strncmp(buf, "STATES ", 7) != 0)
{
if (strncmp(buf, "STATES ", 7) != 0) {
LOG4CPLUS_ERROR(logger, "Expected \"STATES\" but got \"" << std::string(buf, 0, 16) << "\".");
return 0;
}
buf += 7; // skip "STATES "
if (strtol(buf, &buf, 10) == 0) return 0;
buf = skipWS(buf);
if (strncmp(buf, "TRANSITIONS ", 12) != 0)
{
if (strncmp(buf, "TRANSITIONS ", 12) != 0) {
LOG4CPLUS_ERROR(logger, "Expected \"TRANSITIONS\" but got \"" << std::string(buf, 0, 16) << "\".");
return 0;
}
@ -78,8 +75,7 @@ uint_fast64_t TraParser::firstPass(char* buf, uint_fast64_t &maxnode)
double val;
maxnode = 0;
char* tmp;
while (buf[0] != '\0')
{
while (buf[0] != '\0') {
/*
* read row and column
*/
@ -95,8 +91,7 @@ uint_fast64_t TraParser::firstPass(char* buf, uint_fast64_t &maxnode)
* if row == col, we have a diagonal element which is treated seperately and this non_zero must be decreased.
*/
val = strtod(buf, &tmp);
if (val == 0.0)
{
if (val == 0.0) {
LOG4CPLUS_ERROR(logger, "Expected a positive probability but got \"" << std::string(buf, 0, 16) << "\".");
return 0;
}

6
src/parser/readTraFile.h

@ -17,13 +17,11 @@ namespace parser {
* Note that this class creates a new StaticSparseMatrix object that can be
* accessed via getMatrix(). However, it does not delete this object!
*/
class TraParser : Parser
{
class TraParser : Parser {
public:
TraParser(const char* filename);
std::shared_ptr<mrmc::storage::SquareSparseMatrix<double>> getMatrix()
{
std::shared_ptr<mrmc::storage::SquareSparseMatrix<double>> getMatrix() {
return this->matrix;
}

6
src/utility/settings.cpp

@ -147,8 +147,7 @@ void Settings::firstRun(const int argc, const char* argv[], const char* filename
*/
if (this->vm.count("configfile")) {
bpo::store(bpo::parse_config_file<char>(this->vm["configfile"].as<std::string>().c_str(), *(Settings::desc)), this->vm, true);
}
else if (filename != NULL) {
} else if (filename != NULL) {
bpo::store(bpo::parse_config_file<char>(filename, *(Settings::desc)), this->vm, true);
}
}
@ -167,8 +166,7 @@ void Settings::secondRun(const int argc, const char* argv[], const char* filenam
*/
if (this->vm.count("configfile")) {
bpo::store(bpo::parse_config_file<char>(this->vm["configfile"].as<std::string>().c_str(), *(Settings::desc)), this->vm, true);
}
else if (filename != NULL) {
} else if (filename != NULL) {
bpo::store(bpo::parse_config_file<char>(filename, *(Settings::desc)), this->vm, true);
}
}

Loading…
Cancel
Save