From a5c4e4a629f399badba12f4e13506b934af83858 Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Sat, 12 Dec 2015 10:03:24 +0200 Subject: [PATCH] Add pdns_stou --- pdns/misc.cc | 11 ++++++++++- pdns/misc.hh | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pdns/misc.cc b/pdns/misc.cc index c6a18ebad..a7b18c90e 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -1236,7 +1236,6 @@ double DiffTime(const struct timeval& first, const struct timeval& second) return seconds + useconds/1000000.0; } - uid_t strToUID(const string &str) { uid_t result = 0; @@ -1285,3 +1284,13 @@ gid_t strToGID(const string &str) return result; } +unsigned int pdns_stou(const std::string& str, size_t * idx, int base); +{ + if (str.empty()) return 0; // compability + unsigned long result = std::stoul(str, idx, base); + if (result > std::numeric_limits::max()) { + throw std::out_of_range("stou"); + } + return static_cast(result); +} + diff --git a/pdns/misc.hh b/pdns/misc.hh index 18e1dc2ee..14b887b95 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -658,3 +658,5 @@ double DiffTime(const struct timespec& first, const struct timespec& second); double DiffTime(const struct timeval& first, const struct timeval& second); uid_t strToUID(const string &str); gid_t strToGID(const string &str); + +unsigned int pdns_stou(const std::string& str, size_t * idx = 0, int base = 10); -- 2.40.0