From: Aki Tuomi Date: Sat, 12 Dec 2015 08:03:24 +0000 (+0200) Subject: Add pdns_stou X-Git-Tag: dnsdist-1.0.0-alpha1~34^2~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a5c4e4a629f399badba12f4e13506b934af83858;p=thirdparty%2Fpdns.git Add pdns_stou --- diff --git a/pdns/misc.cc b/pdns/misc.cc index c6a18ebad2..a7b18c90e2 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 18e1dc2eef..14b887b950 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);