1 // This file is part of the ustl library, an STL implementation.
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
9 #ifndef SISTREAM_H_0CCA102229A49F5D65EE852E62B27CE2
10 #define SISTREAM_H_0CCA102229A49F5D65EE852E62B27CE2
17 /// \class istringstream sistream.h ustl.h
18 /// \ingroup TextStreams
20 /// \brief A stream that reads textual data from a memory block.
22 class istringstream
: public istream
{
24 static const size_type c_MaxDelimiters
= 16; ///< Maximum number of word delimiters.
27 istringstream (const void* p
, size_type n
);
28 explicit istringstream (const cmemlink
& source
);
29 void iread (int8_t& v
);
30 void iread (int32_t& v
);
31 void iread (double& v
);
33 void iread (wchar_t& v
);
34 void iread (string
& v
);
36 void iread (int64_t& v
);
38 #if HAVE_LONG_LONG && (!HAVE_INT64_T || SIZE_OF_LONG_LONG > 8)
39 void iread (long long& v
);
41 inline string
str (void) const { string s
; s
.link (*this); return (s
); }
42 inline void str (const string
& s
) { link (s
); }
44 inline void get (char& c
) { c
= get(); }
45 void get (char* p
, size_type n
, char delim
= '\n');
46 void get (string
& s
, char delim
= '\n');
47 void getline (char* p
, size_type n
, char delim
= '\n');
48 void getline (string
& s
, char delim
= '\n');
49 void ignore (size_type n
, char delim
= '\0');
50 inline char peek (void) { int8_t v
; iread (v
); ungetc(); return (v
); }
51 inline void putback (char) { ungetc(); }
52 inline void unget (void) { ungetc(); }
53 void set_delimiters (const char* delimiters
);
54 inline void set_base (short base
);
55 inline void set_decimal_separator (char) { }
56 inline void set_thousand_separator (char) { }
57 void read (void* buffer
, size_type size
);
58 void read (memlink
& buf
);
59 inline void read_strz (string
& str
);
60 inline void sync (void) { skip (remaining()); }
62 char skip_delimiters (void);
64 inline bool is_delimiter (char c
) const;
65 template <typename T
> void read_number (T
& v
);
67 char m_Delimiters
[c_MaxDelimiters
];
71 /// Sets the numeric base used to read numbers.
72 inline void istringstream::set_base (short base
)
77 /// Reads a null-terminated character stream. This is not allowed in this class.
78 inline void istringstream::read_strz (string
&)
80 assert (!"Reading nul characters is not allowed from text streams");
83 /// Reads one type as another.
84 template <typename RealT
, typename CastT
>
85 inline void _cast_read (istringstream
& is
, RealT
& v
)
92 //----------------------------------------------------------------------
94 template <typename T
> struct object_text_reader
{
95 inline void operator()(istringstream
& is
, T
& v
) const { v
.text_read (is
); }
97 template <typename T
> struct integral_text_object_reader
{
98 inline void operator()(istringstream
& is
, T
& v
) const { is
.iread (v
); }
100 template <typename T
>
101 inline istringstream
& operator>> (istringstream
& is
, T
& v
) {
102 typedef typename
tm::Select
<numeric_limits
<T
>::is_integral
,
103 integral_text_object_reader
<T
>, object_text_reader
<T
> >::Result object_reader_t
;
104 object_reader_t()(is
, v
);
108 //----------------------------------------------------------------------
110 template <> struct object_text_reader
<string
> {
111 inline void operator()(istringstream
& is
, string
& v
) const { is
.iread (v
); }
113 #define ISTRSTREAM_CAST_OPERATOR(RealT, CastT) \
114 template <> struct integral_text_object_reader<RealT> { \
115 inline void operator() (istringstream& is, RealT& v) const \
116 { _cast_read<RealT,CastT>(is, v); } \
118 ISTRSTREAM_CAST_OPERATOR (uint8_t, int8_t)
119 ISTRSTREAM_CAST_OPERATOR (int16_t, int32_t)
120 ISTRSTREAM_CAST_OPERATOR (uint16_t, int32_t)
121 ISTRSTREAM_CAST_OPERATOR (uint32_t, int32_t)
122 ISTRSTREAM_CAST_OPERATOR (float, double)
123 #if HAVE_THREE_CHAR_TYPES
124 ISTRSTREAM_CAST_OPERATOR (char, int8_t)
127 ISTRSTREAM_CAST_OPERATOR (uint64_t, int64_t)
129 #if SIZE_OF_LONG == SIZE_OF_INT
130 ISTRSTREAM_CAST_OPERATOR (long, int)
131 ISTRSTREAM_CAST_OPERATOR (unsigned long,int)
133 #if HAVE_LONG_LONG && (!HAVE_INT64_T || SIZE_OF_LONG_LONG > 8)
134 ISTRSTREAM_CAST_OPERATOR (unsigned long long, long long)
136 #undef ISTRSTREAM_CAST_OPERATOR