cbp2make
Makefile generation tool for Code::Blocks IDE
stlconvert.h
Go to the documentation of this file.
1 /*
2  cbp2make : Makefile generation tool for the Code::Blocks IDE
3  Copyright (C) 2010-2013 Mirai Computing (mirai.computing@gmail.com)
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 //------------------------------------------------------------------------------
20 #ifndef STL_CONVERT_H
21 #define STL_CONVERT_H
22 //------------------------------------------------------------------------------
23 #include <cstdio>
24 #include <sstream>
25 //------------------------------------------------------------------------------
26 #include "stlstrings.h"
27 //------------------------------------------------------------------------------
28 
29 inline double IntToFloat(const int Value)
30 {
31  return (double)Value;
32 }
33 
34 inline bool IntToBool(const int Value)
35 {
36  return (0!=Value);
37 }
38 
39 inline std::string IntToStr(const int Value)
40 {
41  std::string result;
42  std::stringstream tmp;
43  tmp<<Value;
44  tmp>>result;
45  return result;
46 }
47 
48 inline char IntToChar(const int Value)
49 {
50  return (Value & 0xff);
51 }
52 
53 inline int FloatToInt(const double Value)
54 {
55  return (int)Value;
56 }
57 
58 inline bool FloatToBool(const double Value)
59 {
60  return (0.0!=Value);
61 }
62 
63 inline std::string FloatToStr(const double Value)
64 {
65  std::string result;
66  std::stringstream tmp;
67  tmp<<Value;
68  tmp>>result;
69  return result;
70 }
71 
72 inline char FloatToChar(const double Value)
73 {
74  return IntToChar(FloatToInt(Value));
75 }
76 
77 inline double BoolToFloat(const bool Value)
78 {
79  return (double)Value;
80 }
81 
82 inline int BoolToInt(const bool Value)
83 {
84  return (int)Value;
85 }
86 
87 inline std::string BoolToStr(const bool Value)
88 {
89  if (Value) return "true"; else return "false";
90 }
91 
92 inline char BoolToChar(const bool Value)
93 {
94  if (Value) return '1'; else return '0';
95 }
96 
97 inline double StrToFloat(const std::string& Value)
98 {
99  double result;
100  std::stringstream tmp;
101  tmp<<Value;
102  tmp>>result;
103  return result;
104 }
105 
106 inline int StrToInt(const std::string& Value)
107 {
108  int result;
109  std::stringstream tmp;
110  tmp<<Value;
111  tmp>>result;
112  return result;
113 }
114 
115 inline bool StrToBool(const std::string& Value)
116 {
117  return (UpperCase(Value).GetString().compare("TRUE")==0);
118 }
119 
120 inline char StrToChar(const std::string& Value)
121 {
122  char result = 0;
123  if (Value.size()) result = Value[0];
124  return result;
125 }
126 
127 inline double CharToFloat(const char Value)
128 {
129  return (double)Value;
130 }
131 
132 inline int CharToInt(const char Value)
133 {
134  return (int)Value;
135 }
136 
137 inline bool CharToBool(const char Value)
138 {
139  return ('0'!=Value);
140 }
141 
142 inline std::string CharToStr(const char Value)
143 {
144  std::string result;
145  result.resize(1);
146  result[0] = Value;
147  return result;
148 }
149 
150 //------------------------------------------------------------------------------
151 
152 inline CString IntegerToString(const int Value)
153 {
154  CString result;
155  result = (int)Value;
156  return result;
157 }
158 
159 inline CString Int64ToString(const long long int Value)
160 {
161  CString result;
162  result = (long long int)Value;
163  return result;
164 }
165 
166 inline CString FloatToString(const double Value)
167 {
168  CString result;
169  result = Value;
170  return result;
171 }
172 
173 inline CString FloatToString(const double Value, const CString& Format)
174 {
175  CString result; result.SetLength(MAX_SHORTSTRING_LENGTH);
176  sprintf(result.GetCString(),Format.GetCString(),Value);
177  result.SetLength();
178  return result;
179 }
180 
181 inline CString BooleanToString(const bool Value)
182 {
183  if (Value) return CString("true"); else return CString("false");
184 }
185 
186 inline CString BooleanToYesNoString(const bool Value)
187 {
188  if (Value) return CString("yes"); else return CString("no");
189 }
190 
191 inline double StringToFloat(const CString& Value)
192 {
193  return Value.GetFloat();
194 }
195 
196 inline int StringToInteger(const CString& Value)
197 {
198  return Value.GetInteger();
199 }
200 
201 inline bool StringToBoolean(const CString& Value)
202 {
203  return ((CString(Value)=="1")||(UpperCase(Value)=="TRUE")||(UpperCase(Value)=="YES"));
204 }
205 
206 inline char StringToChar(const CString& Value)
207 {
208  return Value.GetChar(0);
209 }
210 
211 inline CString CharToString(const char Value)
212 {
213  CString result;
214  result.Assign((int)Value);
215  return result;
216 }
217 
218 #endif
219 //------------------------------------------------------------------------------
CString BooleanToYesNoString(const bool Value)
Definition: stlconvert.h:186
char FloatToChar(const double Value)
Definition: stlconvert.h:72
bool FloatToBool(const double Value)
Definition: stlconvert.h:58
int CharToInt(const char Value)
Definition: stlconvert.h:132
int GetInteger(void) const
Definition: stlstrings.cpp:298
int StrToInt(const std::string &Value)
Definition: stlconvert.h:106
CString Int64ToString(const long long int Value)
Definition: stlconvert.h:159
std::string IntToStr(const int Value)
Definition: stlconvert.h:39
#define MAX_SHORTSTRING_LENGTH
Definition: stlstrings.h:29
CString CharToString(const char Value)
Definition: stlconvert.h:211
char BoolToChar(const bool Value)
Definition: stlconvert.h:92
CString BooleanToString(const bool Value)
Definition: stlconvert.h:181
char StrToChar(const std::string &Value)
Definition: stlconvert.h:120
int StringToInteger(const CString &Value)
Definition: stlconvert.h:196
CString & Assign(const char AChar)
Definition: stlstrings.cpp:90
double StrToFloat(const std::string &Value)
Definition: stlconvert.h:97
bool StringToBoolean(const CString &Value)
Definition: stlconvert.h:201
CString UpperCase(const CString &AString)
Definition: stlstrings.h:336
char GetChar(const int Index) const
Definition: stlstrings.cpp:70
double GetFloat(void) const
Definition: stlstrings.cpp:303
double StringToFloat(const CString &Value)
Definition: stlconvert.h:191
bool CharToBool(const char Value)
Definition: stlconvert.h:137
Definition: stlstrings.h:32
CString IntegerToString(const int Value)
Definition: stlconvert.h:152
CString & SetLength(void)
Definition: stlstrings.cpp:53
int FloatToInt(const double Value)
Definition: stlconvert.h:53
std::string CharToStr(const char Value)
Definition: stlconvert.h:142
std::string FloatToStr(const double Value)
Definition: stlconvert.h:63
bool StrToBool(const std::string &Value)
Definition: stlconvert.h:115
bool IntToBool(const int Value)
Definition: stlconvert.h:34
char * GetCString(void) const
Definition: stlstrings.cpp:38
std::string BoolToStr(const bool Value)
Definition: stlconvert.h:87
char StringToChar(const CString &Value)
Definition: stlconvert.h:206
double IntToFloat(const int Value)
Definition: stlconvert.h:29
char IntToChar(const int Value)
Definition: stlconvert.h:48
int BoolToInt(const bool Value)
Definition: stlconvert.h:82
double BoolToFloat(const bool Value)
Definition: stlconvert.h:77
double CharToFloat(const char Value)
Definition: stlconvert.h:127
CString FloatToString(const double Value)
Definition: stlconvert.h:166