1 #ifndef NEO_STRING_CONVERSIONS_H
2 #define NEO_STRING_CONVERSIONS_H
8 #pragma warning(disable:4996) // disable Microsoft warning about vsnprintf
10 #define NEO_VSNPRINTF(buf, size, fmt, ap) _vsntprintf(buf, size, fmt, ap);
23 int n = NEO_VSNPRINTF(0, 0, fmt, ap);
33 n = NEO_VSNPRINTF(&str[0], n+1, fmt, ap);
43 #pragma warning(pop) // enable warning again
49 inline tstring neoToString(
signed char value)
51 inline tstring neoToString(
short value)
53 inline tstring neoToString(
int value)
55 inline tstring neoToString(
long value)
57 inline tstring neoToString(
long long value)
60 inline tstring neoToString(
unsigned char value)
62 inline tstring neoToString(
unsigned short value)
64 inline tstring neoToString(
unsigned int value)
66 inline tstring neoToString(
unsigned long value)
68 inline tstring neoToString(
unsigned long long value)
71 inline tstring neoToString(
float value)
73 inline tstring neoToString(
double value)
75 inline tstring neoToString(
long double value)
76 {
return neoFormat(_T(
"%.17Lg"), value); }
81 inline signed char neoParseNumber(
signed char,
const tstring& text)
82 {
return std::stoi(text); }
83 inline short neoParseNumber(
short,
const tstring& text)
84 {
return std::stoi(text); }
85 inline int neoParseNumber(
int,
const tstring& text)
86 {
return std::stoi(text); }
87 inline long neoParseNumber(
long,
const tstring& text)
88 {
return std::stol(text); }
89 inline long long neoParseNumber(
long long,
const tstring& text)
90 {
return std::stoll(text); }
92 inline unsigned char neoParseNumber(
unsigned char,
const tstring& text)
93 {
return static_cast<unsigned char>(std::stoul(text)); }
94 inline unsigned short neoParseNumber(
unsigned short,
const tstring& text)
95 {
return static_cast<unsigned short>(std::stoul(text)); }
96 inline unsigned int neoParseNumber(
unsigned int,
const tstring& text)
97 {
return std::stoul(text); }
98 inline unsigned long neoParseNumber(
unsigned long,
const tstring& text)
99 {
return std::stoul(text); }
100 inline unsigned long long neoParseNumber(
unsigned long long,
const tstring& text)
101 {
return std::stoull(text); }
103 inline float neoParseNumber(
float,
const tstring& text)
104 {
return std::stof(text); }
105 inline double neoParseNumber(
double,
const tstring& text)
106 {
return std::stod(text); }
107 inline long double neoParseNumber(
long double,
const tstring& text)
108 {
return std::stold(text); }
111 bool neoParse(
const tstring& text, T& result)
115 T parsed = neoParseNumber(T(), text);
127 inline tstring neoToString(
const bool& value)
129 return value ? _T(
"1") : _T(
"0");
132 inline bool neoParse(
const tstring& text,
bool& result)
135 if(neoParse(text, i))
146 bool configStructWrite(Config& conf,
const tstring& key,
const T& value)
148 return conf.Write(key, neoToString(value));
152 bool configStructRead(
const Config& conf,
const tstring& key, T& result)
155 return conf.Read(key, text) &&
156 neoParse(text, result);
161 inline bool configStructWrite(Config& conf,
const tstring& key,
const Point& value)
163 return conf.Write(key + _T(
".x"), value.x) &&
164 conf.Write(key + _T(
".y"), value.y);
167 inline bool configStructRead(
const Config& conf,
const tstring& key, Point& result)
170 if( conf.Read(key + _T(
".x"), x) &&
171 conf.Read(key + _T(
".y"), y))
182 inline bool configStructWrite(Config& conf,
const tstring& key,
const Size& value)
184 return conf.Write(key + _T(
".w"), value.cx) &&
185 conf.Write(key + _T(
".h"), value.cy);
188 inline bool configStructRead(
const Config& conf,
const tstring& key, Size& result)
191 if( conf.Read(key + _T(
".w"), w) &&
192 conf.Read(key + _T(
".h"), h))
203 inline bool configStructWrite(Config& conf,
const tstring& key,
const Rect& value)
205 return conf.Write(key + _T(
".x"), value.left) &&
206 conf.Write(key + _T(
".y"), value.top) &&
207 conf.Write(key + _T(
".w"), value.Width()) &&
208 conf.Write(key + _T(
".h"), value.Height());
211 inline bool configStructRead(
const Config& conf,
const tstring& key, Rect& result)
214 if( conf.Read(key + _T(
".x"), x) &&
215 conf.Read(key + _T(
".y"), y) &&
216 conf.Read(key + _T(
".w"), w) &&
217 conf.Read(key + _T(
".h"), h))
219 result = Rect(x, y, w, h);
228 bool configStructWrite(Config& conf,
const tstring& key,
const std::vector<T>& value)
230 if(conf.Write(key + _T(
".n"), value.size()))
232 for(
size_t i = 0; i < value.size(); ++i)
234 conf.Write(key + _T(
".") + std::to_wstring(i), value[i]);
242 bool configStructRead(
const Config& conf,
const tstring& key, std::vector<T>& result)
245 if(!conf.Read(key + _T(
".n"), n))
249 for(
size_t i = 0; i < n; ++i)
252 if(!conf.Read(key + _T(
".") + std::to_wstring(i), item))
tstring neoFormat(const TCHAR *fmt,...)
printf-style function
Definition: StringConversions.h:18