2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
5 * Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /** Template function to convert any input type to std::string
25 template<typename T> inline std::string ConvNumeric(const T& in)
33 out += "0123456789"[std::abs((long)quotient % 10)];
38 std::reverse(out.begin(), out.end());
42 /** Template function to convert any input type to std::string
44 inline std::string ConvToStr(const int in)
46 return ConvNumeric(in);
49 /** Template function to convert any input type to std::string
51 inline std::string ConvToStr(const long in)
53 return ConvNumeric(in);
56 /** Template function to convert any input type to std::string
58 inline std::string ConvToStr(const char* in)
63 /** Template function to convert any input type to std::string
65 inline std::string ConvToStr(const bool in)
67 return (in ? "1" : "0");
70 /** Template function to convert any input type to std::string
72 inline std::string ConvToStr(char in)
74 return std::string(1, in);
77 inline const std::string& ConvToStr(const std::string& in)
82 /** Template function to convert any input type to std::string
84 template <class T> inline std::string ConvToStr(const T& in)
86 std::stringstream tmp;
92 /** Template function to convert a std::string to any numeric type.
94 template<typename TOut> inline TOut ConvToNum(const std::string& in)
97 std::istringstream tmp(in);
103 template<> inline char ConvToNum<char>(const std::string& in)
105 // We specialise ConvToNum for char to avoid istringstream treating
106 // the input as a character literal.
107 uint16_t num = ConvToNum<uint16_t>(in);
108 return num <= UINT8_MAX ? num : 0;
111 template<> inline unsigned char ConvToNum<unsigned char>(const std::string& in)
113 // We specialise ConvToNum for unsigned char to avoid istringstream
114 // treating the input as a character literal.
115 int16_t num = ConvToNum<int16_t>(in);
116 return num >= INT8_MIN && num <= INT8_MAX ? num : 0;