diff --git a/src/mrmc.cpp b/src/mrmc.cpp index 4a30f12c3..16f4cf9f4 100644 --- a/src/mrmc.cpp +++ b/src/mrmc.cpp @@ -100,7 +100,7 @@ bool parseOptions(const int argc, const char* argv[]) { std::cout << "Could not recover from settings error: " << e.what() << "." << std::endl; std::cout << std::endl << mrmc::settings::help; delete s; - return 1; + return false; } if (s->isSet("help")) { diff --git a/src/parser/SparseStateRewardParser.cpp b/src/parser/SparseStateRewardParser.cpp new file mode 100644 index 000000000..4caee50e1 --- /dev/null +++ b/src/parser/SparseStateRewardParser.cpp @@ -0,0 +1,74 @@ +/*! + * SparseStateRewardParser.cpp + * + * Created on: 23.12.2012 + * Author: Christian Dehnert + */ + +#include "src/parser/SparseStateRewardParser.h" +#include "src/exceptions/WrongFileFormatException.h" +#include "src/exceptions/FileIoException.h" + +#include "src/utility/OsDetection.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log4cplus/logger.h" +#include "log4cplus/loggingmacros.h" +extern log4cplus::Logger logger; + +namespace mrmc { +namespace parser { + + +/*! + * Reads a state reward file and puts the result in a state reward vector. + * + * @param stateCount The number of states. + * @param filename The filename of the state reward file. + * @return A pointer to the created state reward vector. + */ +SparseStateRewardParser::SparseStateRewardParser(uint_fast64_t stateCount, std::string const & filename) + : stateRewards(nullptr) { + // Open file. + MappedFile file(filename.c_str()); + char* buf = file.data; + + char separator[] = " \r\n\t"; + + // Create state reward vector with given state count. + this->stateRewards = std::shared_ptr>(new std::vector(stateCount)); + + { + // Now parse state reward assignments. + uint_fast64_t state; + double reward; + + // Iterate over states. + while (buf[0] != '\0') { + // Parse state number and reward value. + state = checked_strtol(buf, &buf); + reward = strtod(buf, &buf); + if (reward < 0.0) { + LOG4CPLUS_ERROR(logger, "Expected positive probability but got \"" << std::string(buf, 0, 16) << "\"."); + throw mrmc::exceptions::WrongFileFormatException() << "State reward file specifies illegal reward value."; + } + + (*this->stateRewards)[state] = reward; + + buf = trimWhitespaces(buf); + } + } +} + +} //namespace parser + +} //namespace mrmc diff --git a/src/parser/SparseStateRewardParser.h b/src/parser/SparseStateRewardParser.h new file mode 100644 index 000000000..604070759 --- /dev/null +++ b/src/parser/SparseStateRewardParser.h @@ -0,0 +1,32 @@ +#ifndef MRMC_PARSER_SPARSESTATEREWARDPARSER_H_ +#define MRMC_PARSER_SPARSESTATEREWARDPARSER_H_ + +#include "boost/integer/integer_mask.hpp" +#include "src/parser/Parser.h" +#include +#include + +namespace mrmc { + +namespace parser { + +/*! + * @brief Load state reward file and return vector of state rewards. + */ +class SparseStateRewardParser : Parser { + public: + SparseStateRewardParser(uint_fast64_t stateCount, std::string const &filename); + + std::shared_ptr> getStateRewards() { + return this->stateRewards; + } + + private: + std::shared_ptr> stateRewards; +}; + +} // namespace parser + +} // namespace mrmc + +#endif /* MRMC_PARSER_SPARSESTATEREWARDPARSER_H_ */