15 #ifndef INCG_IRIS_IUTEST_STRING_STREAM_HPP_F446E84B_1C7D_49C4_9A2D_5002CCE3F486_
16 #define INCG_IRIS_IUTEST_STRING_STREAM_HPP_F446E84B_1C7D_49C4_9A2D_5002CCE3F486_
21 #if IUTEST_HAS_STRINGSTREAM
23 #elif IUTEST_HAS_STRSTREAM
26 #if IUTEST_HAS_IOMANIP
29 #if IUTEST_HAS_EXCEPTIONS
40 bool StringToValue(const ::std::string& s, T& out)
42 ::std::istringstream strm(s);
50 inline bool StringToValue(const ::std::string& s,
float& out)
52 #if IUTEST_HAS_STD_STR_TO_VALUE
56 const char* p = s.c_str();
58 #if !defined(IUTEST_OS_WINDOWS_MINGW) || !defined(__STRICT_ANSI__)
59 const floating_point<float> v = strtof(p, &endptr);
61 const floating_point<float> v =
static_cast<float>(strtod(p, &endptr));
63 #if IUTEST_HAS_EXCEPTIONS
66 throw ::std::invalid_argument(p);
68 if((errno == ERANGE) || v.is_inf() )
70 throw ::std::out_of_range(p);
78 inline bool StringToValue(const ::std::string& s,
double& out)
80 #if IUTEST_HAS_STD_STR_TO_VALUE
84 const char* p = s.c_str();
86 const floating_point<double> v = strtod(s.c_str(), &endptr);
87 #if IUTEST_HAS_EXCEPTIONS
90 throw ::std::invalid_argument(p);
92 if((errno == ERANGE) || v.is_inf() )
94 throw ::std::out_of_range(p);
102 #if IUTEST_HAS_LONG_DOUBLE
104 inline bool StringToValue(const ::std::string& s,
long double& out)
106 #if IUTEST_HAS_STD_STR_TO_VALUE
107 out = ::std::stold(s);
110 const char* p = s.c_str();
112 const floating_point<long double> v = strtold(s.c_str(), &endptr);
113 #if IUTEST_HAS_EXCEPTIONS
116 throw ::std::invalid_argument(p);
118 if((errno == ERANGE) || v.is_inf() )
120 throw ::std::out_of_range(p);
130 #if !IUTEST_HAS_STRINGSTREAM && !IUTEST_HAS_STRSTREAM
132 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_BEGIN()
137 template<class _Elem, class _Traits>
138 class iu_basic_ostream
140 typedef iu_basic_ostream<_Elem, _Traits> _Myt;
143 typedef ::std::basic_string<_Elem, _Traits> string;
150 template<
typename TMP,
typename TN>
153 template<
typename TA,
typename TB>
154 static const TA constant(
const TA a,
const TB b)
160 template<
typename TMP>
161 struct impl_select<TMP, wchar_t>
163 template<
typename TA,
typename TB>
164 static const TB constant(
const TA a,
const TB b)
172 typedef impl_select<void, T> select;
174 #define IIUT_PP_XCS(txt_) xcs<_Elem>::select::constant(txt_, L##txt_)
179 static int vastring(E* dst,
const E* fmt, va_list va);
180 static int vastring(
char* dst,
size_t len,
const char* fmt, va_list va)
183 return vsprintf(dst, fmt, va);
185 static int vastring(
wchar_t* dst,
size_t len,
const wchar_t* fmt, va_list va)
187 #ifdef IUTEST_OS_WINDOWS_MINGW
188 return _vsnwprintf(dst, len, fmt, va);
190 return vswprintf(dst, len, fmt, va);
195 static int tostring(E* dst,
size_t len,
const E* fmt, ...)
199 int ret = vastring(dst, len, fmt, va);
205 iu_basic_ostream() {}
206 explicit iu_basic_ostream(
const char* str) : s(str) {}
207 explicit iu_basic_ostream(const ::std::string& str) : s(str) {}
210 inline _Myt& operator<< (
char v)
215 inline _Myt& operator<< (
signed char v)
217 s +=
static_cast<char>(v);
220 inline _Myt& operator<< (
unsigned char v)
222 s +=
static_cast<char>(v);
225 inline _Myt& operator<< (
const _Elem* v)
240 inline _Myt& operator<< (
bool v)
244 impl::tostring(a, 16, IIUT_PP_XCS(
"%i"), v);
247 s += (v ? IIUT_PP_XCS(
"true") : IIUT_PP_XCS(
"false"));
251 inline _Myt& operator<< (
short v)
254 impl::tostring(a, 64, IIUT_PP_XCS(
"%i"), v);
258 inline _Myt& operator<< (
unsigned short v)
261 impl::tostring(a, 64, IIUT_PP_XCS(
"%u"), v);
265 inline _Myt& operator<< (
int v)
268 impl::tostring(a, 64, IIUT_PP_XCS(
"%i"), v);
272 inline _Myt& operator<< (
unsigned int v)
275 impl::tostring(a, 64, IIUT_PP_XCS(
"%u"), v);
279 inline _Myt& operator<< (
long v)
282 impl::tostring(a, 64, IIUT_PP_XCS(
"%i"), v);
286 inline _Myt& operator<< (
unsigned long v)
289 impl::tostring(a, 64, IIUT_PP_XCS(
"%u"), v);
293 inline _Myt& operator<< (
long long int v)
296 impl::tostring(a, 64, IIUT_PP_XCS(
"%lld"), v);
300 inline _Myt& operator<< (
unsigned long long int v)
303 impl::tostring(a, 64, IIUT_PP_XCS(
"%llu"), v);
307 inline _Myt& operator<< (
float v)
310 impl::tostring(a, 64, IIUT_PP_XCS(
"%f"), v);
314 inline _Myt& operator<< (
double v)
317 impl::tostring(a, 64, IIUT_PP_XCS(
"%l"), v);
321 inline _Myt& operator<< (
long double v)
324 impl::tostring(a, 64, IIUT_PP_XCS(
"%L"), v);
328 inline _Myt& operator<< (
const void* v)
331 impl::tostring(a, 64, IIUT_PP_XCS(
"%t"), v);
335 inline _Myt& operator<< (const ::std::string& v)
341 const string& str()
const {
return s; }
342 void copyfmt(
const _Myt&) {}
347 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_END()
353 #if IUTEST_HAS_STRINGSTREAM
355 typedef ::std::stringstream stlstream;
357 #elif IUTEST_HAS_STRSTREAM
359 IUTEST_PRAGMA_MSC_WARN_PUSH()
360 IUTEST_PRAGMA_MSC_WARN_DISABLE(4250)
361 class stlstream : public ::std::strstream
366 : ::std::strstream(buf, sizeof(buf)-2, ::std::ios::out)
368 explicit stlstream(
const char* str)
369 : ::std::strstream(buf, sizeof(buf)-2, ::std::ios::out)
373 explicit stlstream(const ::std::string& str)
374 : ::std::strstream(buf, sizeof(buf)-2, ::std::ios::out)
379 ::std::string str()
const
381 return const_cast<stlstream*
>(
this)->str();
383 virtual ::std::string str()
385 *
this << ::std::ends;
386 ::std::string str = ::std::strstream::str();
391 IUTEST_PRAGMA_MSC_WARN_POP()
395 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_END()
399 #if IUTEST_HAS_STRINGSTREAM || IUTEST_HAS_STRSTREAM
401 typedef ::std::ostream iu_ostream;
402 typedef detail::stlstream iu_stringstream;
406 typedef detail::iu_basic_ostream<char, ::std::char_traits<char> > iu_ostream;
407 typedef detail::iu_basic_ostream<wchar_t, ::std::char_traits<wchar_t> > iu_wostream;
408 typedef iu_ostream iu_stringstream;
412 #if IUTEST_HAS_IOMANIP
413 typedef iu_ostream& (*iu_basic_iomanip)(iu_ostream&);
416 #if !defined(IUTEST_HAS_BIGGESTINT_OSTREAM)
417 # if IUTEST_HAS_STRINGSTREAM || IUTEST_HAS_STRSTREAM
418 # if (defined(_STLPORT_VERSION) && !defined(_STLP_LONG_LONG)) || (defined(_MSC_VER) && _MSC_VER < 1310)
419 # define IUTEST_HAS_BIGGESTINT_OSTREAM 0
424 #if !defined(IUTEST_HAS_BIGGESTINT_OSTREAM)
425 # define IUTEST_HAS_BIGGESTINT_OSTREAM 1
430 #endif // INCG_IRIS_IUTEST_STRING_STREAM_HPP_F446E84B_1C7D_49C4_9A2D_5002CCE3F486_