X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Fconvto.h;h=1fc6848290bc84407b2c542890d68018ea8aef96;hb=c05f81cac83e80c7727594e3929e0709eccca689;hp=c306283fc7e85a4333164239a22c51db479cf15d;hpb=77730fd5f09f8fc193205654c8bba84d34365670;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/convto.h b/include/convto.h index c306283fc..1fc684829 100644 --- a/include/convto.h +++ b/include/convto.h @@ -1,8 +1,8 @@ /* * InspIRCd -- Internet Relay Chat Daemon * - * Copyright (C) 2014 Attila Molnar - * Copyright (C) 2006 Craig Edwards + * Copyright (C) 2017-2019 Sadie Powell + * Copyright (C) 2016 Attila Molnar * * This file is part of InspIRCd. InspIRCd is free software: you can * redistribute it and/or modify it under the terms of the GNU General Public @@ -89,17 +89,8 @@ 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) -{ - std::stringstream tmp; - if (!(tmp << in)) - return 0; - return atol(tmp.str().c_str()); -} - template inline TOut ConvToNum(const std::string& in) { TOut ret; @@ -108,3 +99,19 @@ template inline TOut ConvToNum(const std::string& in) 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. + int16_t num = ConvToNum(in); + return num >= INT8_MIN && num <= INT8_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. + uint16_t num = ConvToNum(in); + return num <= UINT8_MAX ? num : 0; +}