InspIRCd  3.0
convto.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2014 Attila Molnar <[email protected]>
5  * Copyright (C) 2006 Craig Edwards <[email protected]>
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 
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 
44 inline std::string ConvToStr(const int in)
45 {
46  return ConvNumeric(in);
47 }
48 
51 inline std::string ConvToStr(const long in)
52 {
53  return ConvNumeric(in);
54 }
55 
58 inline std::string ConvToStr(const char* in)
59 {
60  return in;
61 }
62 
65 inline std::string ConvToStr(const bool in)
66 {
67  return (in ? "1" : "0");
68 }
69 
72 inline std::string ConvToStr(char in)
73 {
74  return std::string(1, in);
75 }
76 
77 inline const std::string& ConvToStr(const std::string& in)
78 {
79  return in;
80 }
81 
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 
95 template<typename T> inline long ConvToInt(const T& in)
96 {
97  std::stringstream tmp;
98  if (!(tmp << in))
99  return 0;
100  return atol(tmp.str().c_str());
101 }
102 
103 inline uint64_t ConvToUInt64(const std::string& in)
104 {
105  uint64_t ret;
106  std::istringstream tmp(in);
107  if (!(tmp >> ret))
108  return 0;
109  return ret;
110 }