X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Fconvto.h;h=9bad62b93d0894782aaa02a649c020a9e234173c;hb=a30a0074edac353cb60e134b43fa8ff0ffb67f8b;hp=82f13c46dc8b592fc894f735e425e03757b229cb;hpb=8f5a3bb7bc30ef83d529e90e9584229c740af732;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/convto.h b/include/convto.h index 82f13c46d..9bad62b93 100644 --- a/include/convto.h +++ b/include/convto.h @@ -1,8 +1,9 @@ /* * InspIRCd -- Internet Relay Chat Daemon * - * Copyright (C) 2014 Attila Molnar - * Copyright (C) 2006 Craig Edwards + * Copyright (C) 2020 Matt Schatz + * 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 @@ -74,6 +75,11 @@ inline std::string ConvToStr(char in) return std::string(1, in); } +inline const std::string& ConvToStr(const std::string& in) +{ + return in; +} + /** Template function to convert any input type to std::string */ template inline std::string ConvToStr(const T& in) @@ -84,22 +90,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) -{ - std::stringstream tmp; - if (!(tmp << in)) - return 0; - return atol(tmp.str().c_str()); -} - -inline uint64_t ConvToUInt64(const std::string& in) +template inline TOut ConvToNum(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. + 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; +}