summaryrefslogtreecommitdiff
path: root/include/convto.h
blob: 1fc6848290bc84407b2c542890d68018ea8aef96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
 *   Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
 *
 * 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
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#pragma once

/** Template function to convert any input type to std::string
 */
template<typename T> inline std::string ConvNumeric(const T& in)
{
	if (in == 0)
		return "0";
	T quotient = in;
	std::string out;
	while (quotient)
	{
		out += "0123456789"[std::abs((long)quotient % 10)];
		quotient /= 10;
	}
	if (in < 0)
		out += '-';
	std::reverse(out.begin(), out.end());
	return out;
}

/** Template function to convert any input type to std::string
 */
inline std::string ConvToStr(const int in)
{
	return ConvNumeric(in);
}

/** Template function to convert any input type to std::string
 */
inline std::string ConvToStr(const long in)
{
	return ConvNumeric(in);
}

/** Template function to convert any input type to std::string
 */
inline std::string ConvToStr(const char* in)
{
	return in;
}

/** Template function to convert any input type to std::string
 */
inline std::string ConvToStr(const bool in)
{
	return (in ? "1" : "0");
}

/** Template function to convert any input type to std::string
 */
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 <class T> inline std::string ConvToStr(const T& in)
{
	std::stringstream tmp;
	if (!(tmp << in))
		return std::string();
	return tmp.str();
}

/** Template function to convert a std::string to any numeric type.
 */
template<typename TOut> inline TOut ConvToNum(const std::string& in)
{
	TOut ret;
	std::istringstream tmp(in);
	if (!(tmp >> ret))
		return 0;
	return ret;
}

template<> inline char ConvToNum<char>(const std::string& in)
{
	// We specialise ConvToNum for char to avoid istringstream treating
	// the input as a character literal.
	int16_t num = ConvToNum<int16_t>(in);
	return num >= INT8_MIN && num <= INT8_MAX ? num : 0;
}

template<> inline unsigned char ConvToNum<unsigned char>(const std::string& in)
{
	// We specialise ConvToNum for unsigned char to avoid istringstream
	// treating the input as a character literal.
	uint16_t num = ConvToNum<uint16_t>(in);
	return num <= UINT8_MAX ? num : 0;
}