1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19 
20 
21 #pragma once
22 
23 /** Template function to convert any input type to std::string
24  */
ConvNumeric(const T & in)25 template<typename T> inline std::string ConvNumeric(const T& in)
26 {
27 	if (in == 0)
28 		return "0";
29 	T quotient = in;
30 	std::string out;
31 	while (quotient)
32 	{
33 		out += "0123456789"[std::abs((long)quotient % 10)];
34 		quotient /= 10;
35 	}
36 	if (in < 0)
37 		out += '-';
38 	std::reverse(out.begin(), out.end());
39 	return out;
40 }
41 
42 /** Template function to convert any input type to std::string
43  */
ConvToStr(const int in)44 inline std::string ConvToStr(const int in)
45 {
46 	return ConvNumeric(in);
47 }
48 
49 /** Template function to convert any input type to std::string
50  */
ConvToStr(const long in)51 inline std::string ConvToStr(const long in)
52 {
53 	return ConvNumeric(in);
54 }
55 
56 /** Template function to convert any input type to std::string
57  */
ConvToStr(const char * in)58 inline std::string ConvToStr(const char* in)
59 {
60 	return in;
61 }
62 
63 /** Template function to convert any input type to std::string
64  */
ConvToStr(const bool in)65 inline std::string ConvToStr(const bool in)
66 {
67 	return (in ? "1" : "0");
68 }
69 
70 /** Template function to convert any input type to std::string
71  */
ConvToStr(char in)72 inline std::string ConvToStr(char in)
73 {
74 	return std::string(1, in);
75 }
76 
ConvToStr(const std::string & in)77 inline const std::string& ConvToStr(const std::string& in)
78 {
79 	return in;
80 }
81 
82 /** Template function to convert any input type to std::string
83  */
ConvToStr(const T & in)84 template <class T> inline std::string ConvToStr(const T& in)
85 {
86 	std::stringstream tmp;
87 	if (!(tmp << in))
88 		return std::string();
89 	return tmp.str();
90 }
91 
92 /** Template function to convert a std::string to any numeric type.
93  */
ConvToNum(const std::string & in)94 template<typename TOut> inline TOut ConvToNum(const std::string& in)
95 {
96 	TOut ret;
97 	std::istringstream tmp(in);
98 	if (!(tmp >> ret))
99 		return 0;
100 	return ret;
101 }
102 
103 template<> inline char ConvToNum<char>(const std::string& in)
104 {
105 	// We specialise ConvToNum for char to avoid istringstream treating
106 	// the input as a character literal.
107 	int16_t num = ConvToNum<int16_t>(in);
108 	return num >= INT8_MIN && num <= INT8_MAX ? num : 0;
109 }
110 
111 template<> inline unsigned char ConvToNum<unsigned char>(const std::string& in)
112 {
113 	// We specialise ConvToNum for unsigned char to avoid istringstream
114 	// treating the input as a character literal.
115 	uint16_t num = ConvToNum<uint16_t>(in);
116 	return num <= UINT8_MAX ? num : 0;
117 }
118