#include "storm/settings/ArgumentValidators.h" #include #include #include "storm/settings/Argument.h" #include "storm/utility/macros.h" #include "storm/exceptions/InvalidArgumentException.h" #include "storm/exceptions/IllegalArgumentException.h" #include "storm/exceptions/IllegalArgumentValueException.h" #include "storm/exceptions/IllegalFunctionCallException.h" namespace storm { namespace settings { template RangeArgumentValidator::RangeArgumentValidator(boost::optional const& lower, boost::optional const& upper, bool lowerIncluded, bool upperIncluded) : lower(lower), upper(upper), lowerIncluded(lowerIncluded), upperIncluded(upperIncluded) { // Intentionally left empty. } template bool RangeArgumentValidator::isValid(ValueType const& value) { bool result = true; if (lower) { if (lowerIncluded) { result &= value >= lower.get(); } else { result &= value > lower.get(); } } if (upper) { if (upperIncluded) { result &= value <= upper.get(); } else { result &= value < upper.get(); } } return result; } template std::string RangeArgumentValidator::toString() const { std::stringstream stream; stream << "in "; if (lower) { if (lowerIncluded) { stream << "["; } else { stream << "("; } stream << lower.get(); } else { stream << "(-inf"; } stream << ", "; if (upper) { stream << upper.get(); if (upperIncluded) { stream << "]"; } else { stream << ")"; } } else { stream << "+inf)"; } return stream.str(); } FileValidator::FileValidator(Mode mode) : mode(mode) { // Intentionally left empty. } bool FileValidator::isValid(std::string const& filename) { if (mode == Mode::Exists) { // First check existence as ifstream::good apparently also returns true for directories. struct stat info; stat(filename.c_str(), &info); STORM_LOG_THROW(info.st_mode & S_IFREG, storm::exceptions::IllegalArgumentValueException, "Unable to read from non-existing file '" << filename << "'."); // Now that we know it's a file, we can check its readability. std::ifstream istream(filename); STORM_LOG_THROW(istream.good(), storm::exceptions::IllegalArgumentValueException, "Unable to read from file '" << filename << "'."); return true; } else if (mode == Mode::Writable) { std::ofstream filestream(filename); STORM_LOG_THROW(filestream.is_open(), storm::exceptions::IllegalArgumentValueException , "Could not open file '" << filename << "' for writing."); filestream.close(); std::remove(filename.c_str()); return true; } return false; } std::string FileValidator::toString() const { if (mode == Mode::Exists) { return "existing file"; } else { return "writable file"; } } MultipleChoiceValidator::MultipleChoiceValidator(std::vector const& legalValues) : legalValues(legalValues) { // Intentionally left empty. } bool MultipleChoiceValidator::isValid(std::string const& value) { for (auto const& legalValue : legalValues) { if (legalValue == value) { return true; } } return false; } std::string MultipleChoiceValidator::toString() const { return "in {" + boost::join(legalValues, ", ") + "}"; } std::shared_ptr> ArgumentValidatorFactory::createIntegerRangeValidatorExcluding(int_fast64_t lowerBound, int_fast64_t upperBound) { return createRangeValidatorExcluding(lowerBound, upperBound); } std::shared_ptr> ArgumentValidatorFactory::createUnsignedRangeValidatorExcluding(uint64_t lowerBound, uint64_t upperBound) { return createRangeValidatorExcluding(lowerBound, upperBound); } std::shared_ptr> ArgumentValidatorFactory::createUnsignedRangeValidatorIncluding(uint64_t lowerBound, uint64_t upperBound) { return createRangeValidatorIncluding(lowerBound, upperBound); } std::shared_ptr> ArgumentValidatorFactory::createDoubleRangeValidatorExcluding(double lowerBound, double upperBound) { return createRangeValidatorExcluding(lowerBound, upperBound); } std::shared_ptr> ArgumentValidatorFactory::createDoubleRangeValidatorIncluding(double lowerBound, double upperBound) { return createRangeValidatorIncluding(lowerBound, upperBound); } std::shared_ptr> ArgumentValidatorFactory::createIntegerGreaterValidator(int_fast64_t lowerBound) { return createGreaterValidator(lowerBound, false); } std::shared_ptr> ArgumentValidatorFactory::createUnsignedGreaterValidator(uint64_t lowerBound) { return createGreaterValidator(lowerBound, false); } std::shared_ptr> ArgumentValidatorFactory::createDoubleGreaterValidator(double lowerBound) { return createGreaterValidator(lowerBound, false); } std::shared_ptr> ArgumentValidatorFactory::createIntegerGreaterEqualValidator(int_fast64_t lowerBound) { return createGreaterValidator(lowerBound, true); } std::shared_ptr> ArgumentValidatorFactory::createUnsignedGreaterEqualValidator(uint64_t lowerBound) { return createGreaterValidator(lowerBound, true); } std::shared_ptr> ArgumentValidatorFactory::createDoubleGreaterEqualValidator(double lowerBound) { return createGreaterValidator(lowerBound, true); } std::shared_ptr> ArgumentValidatorFactory::createExistingFileValidator() { return std::make_unique(FileValidator::Mode::Exists); } std::shared_ptr> ArgumentValidatorFactory::createWritableFileValidator() { return std::make_unique(FileValidator::Mode::Writable); } std::shared_ptr> ArgumentValidatorFactory::createMultipleChoiceValidator(std::vector const& choices) { return std::make_unique(choices); } template std::shared_ptr> ArgumentValidatorFactory::createRangeValidatorExcluding(ValueType lowerBound, ValueType upperBound) { return std::make_unique>(lowerBound, upperBound, false, false); } template std::shared_ptr> ArgumentValidatorFactory::createRangeValidatorIncluding(ValueType lowerBound, ValueType upperBound) { return std::make_unique>(lowerBound, upperBound, true, true); } template std::shared_ptr> ArgumentValidatorFactory::createGreaterValidator(ValueType lowerBound, bool equalAllowed) { return std::make_unique>(lowerBound, boost::none, equalAllowed, false); } } }