X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Fconvto.h;h=2f808d1fb3a722128a8047f8e6ca1973016a8fcf;hb=0a229e70a5839b30d87f3585429d542db37c4cfd;hp=eaf14f6dc549972faa951fe437b58123e78148bc;hpb=d23c030c9a8fd58807438245a004e4aa5b7288ba;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/convto.h b/include/convto.h index eaf14f6dc..2f808d1fb 100644 --- a/include/convto.h +++ b/include/convto.h @@ -89,22 +89,29 @@ template inline std::string ConvToStr(const T& in) return tmp.str(); } -/** Template function to convert any input type to any other type - * (usually an integer or numeric type) +/** Template function to convert a std::string to any numeric type. */ -template inline long ConvToInt(const T& in) +template inline TOut ConvToNum(const std::string& in) { - std::stringstream tmp; - if (!(tmp << in)) - return 0; - return atol(tmp.str().c_str()); -} - -inline uint64_t ConvToUInt64(const std::string& in) -{ - uint64_t ret; + TOut ret; std::istringstream tmp(in); if (!(tmp >> ret)) return 0; return ret; } + +template<> inline char ConvToNum(const std::string& in) +{ + // We specialise ConvToNum for char to avoid istringstream treating + // the input as a character literal. + uint16_t num = ConvToNum(in); + return num <= UINT8_MAX ? num : 0; +} + +template<> inline unsigned char ConvToNum(const std::string& in) +{ + // We specialise ConvToNum for unsigned char to avoid istringstream + // treating the input as a character literal. + int16_t num = ConvToNum(in); + return num >= INT8_MIN && num <= INT8_MAX ? num : 0; +}