| 1 | // |
|---|---|
| 2 | // IntValidator.cpp |
| 3 | // |
| 4 | // Library: Util |
| 5 | // Package: Options |
| 6 | // Module: IntValidator |
| 7 | // |
| 8 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Util/IntValidator.h" |
| 16 | #include "Poco/Util/Option.h" |
| 17 | #include "Poco/Util/OptionException.h" |
| 18 | #include "Poco/NumberParser.h" |
| 19 | #include "Poco/Format.h" |
| 20 | |
| 21 | |
| 22 | using Poco::NumberParser; |
| 23 | using Poco::format; |
| 24 | |
| 25 | |
| 26 | namespace Poco { |
| 27 | namespace Util { |
| 28 | |
| 29 | |
| 30 | IntValidator::IntValidator(int min, int max): |
| 31 | _min(min), |
| 32 | _max(max) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | |
| 37 | IntValidator::~IntValidator() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | |
| 42 | void IntValidator::validate(const Option& option, const std::string& value) |
| 43 | { |
| 44 | int n; |
| 45 | if (NumberParser::tryParse(value, n)) |
| 46 | { |
| 47 | if (n < _min || n > _max) |
| 48 | throw InvalidArgumentException(format("argument for %s must be in range %d to %d", option.fullName(), _min, _max)); |
| 49 | } |
| 50 | else throw InvalidArgumentException(format("argument for %s must be an integer", option.fullName())); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | } } // namespace Poco::Util |
| 55 |