diff --git a/common/utils/UtilsTest.cpp b/common/utils/UtilsTest.cpp index 8cecc84d347dcaea1e1d2ae71246d4d13cc71793..762235cb7b1e3465218cff6effda777bce53e1aa 100644 --- a/common/utils/UtilsTest.cpp +++ b/common/utils/UtilsTest.cpp @@ -321,6 +321,51 @@ TEST_F(cta_UtilsTest, toUint16_too_big) { ASSERT_THROW(utils::toUint16("65536"), std::exception); } +TEST_F(cta_UtilsTest, toUint32_12345) { + using namespace cta; + + uint32_t i = 0; + + ASSERT_NO_THROW(i = utils::toUint32("12345")); + ASSERT_EQ((uint32_t)12345, i); +} + +TEST_F(cta_UtilsTest, toUint32_zero) { + using namespace cta; + + uint32_t i = 0; + + ASSERT_NO_THROW(i = utils::toUint32("0")); + ASSERT_EQ((uint32_t)0, i); +} + +TEST_F(cta_UtilsTest, toUint32_4294967295) { + using namespace cta; + + uint32_t i = 0; + + ASSERT_NO_THROW(i = utils::toUint32("4294967295")); + ASSERT_EQ((uint32_t)4294967295, i); +} + +TEST_F(cta_UtilsTest, toUint32_empty_string) { + using namespace cta; + + ASSERT_THROW(utils::toUint32(""), std::exception); +} + +TEST_F(cta_UtilsTest, toUint32_negative) { + using namespace cta; + + ASSERT_THROW(utils::toUint32("-12345"), std::exception); +} + +TEST_F(cta_UtilsTest, toUint32_too_big) { + using namespace cta; + + ASSERT_THROW(utils::toUint32("4294967296"), std::exception); +} + TEST_F(cta_UtilsTest, toUid_12345) { using namespace cta; diff --git a/common/utils/utils.cpp b/common/utils/utils.cpp index 441f3c3b594fd847c4107afbcf1a97db99174582..5d6ca059b4538804fc9e8cc934471545f2d03f51 100644 --- a/common/utils/utils.cpp +++ b/common/utils/utils.cpp @@ -524,6 +524,42 @@ uint16_t toUint16(const std::string &str) { return value; } +//------------------------------------------------------------------------------ +// toUint32 +//------------------------------------------------------------------------------ +uint32_t toUint32(const std::string &str) { + if(str.empty()) { + std::ostringstream msg; + msg << "Failed to convert empty string to uint32_t: An empty string is not" + " a valid unsigned integer"; + throw exception::Exception(msg.str()); + } + + errno = 0; + const long int value = strtol(str.c_str(), (char **) NULL, 10); + const int savedErrno = errno; + if(savedErrno) { + std::ostringstream msg; + msg << "Failed to convert \'" << str << "' to uint32_t: " << + errnoToString(savedErrno); + throw exception::Exception(msg.str()); + } + + if(0 > value) { + std::ostringstream msg; + msg << "Failed to convert \'" << str << "' to uint32_t: Negative number"; + throw exception::Exception(msg.str()); + } + + if(4294967295 < value) { + std::ostringstream msg; + msg << "Failed to convert \'" << str << "' to uint32_t: Number too big"; + throw exception::Exception(msg.str()); + } + + return value; +} + //------------------------------------------------------------------------------ // toUid //------------------------------------------------------------------------------ diff --git a/common/utils/utils.hpp b/common/utils/utils.hpp index a0d0afa2f96831ae128241141fb12c7b8539dac2..000aa85524eca7864cb2048caedb05f8ae6daccc 100644 --- a/common/utils/utils.hpp +++ b/common/utils/utils.hpp @@ -247,6 +247,14 @@ namespace utils { */ uint16_t toUint16(const std::string &str); + /** + * Converts the specified string to an unsigned integer. + * + * @param str The string. + * @return The unsigned integer. + */ + uint32_t toUint32(const std::string &str); + /** * Converts the specified string to a uid. *