1// This file is part of the ustl library, an STL implementation.
2//
3// Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4// This file is free software, distributed under the MIT License.
5//
6// sistream.h
7//
8
9#ifndef SISTREAM_H_0CCA102229A49F5D65EE852E62B27CE2
10#define SISTREAM_H_0CCA102229A49F5D65EE852E62B27CE2
11
12#include "mistream.h"
13#include "uassert.h"
14#include "ustring.h"
15
16namespace ustl {
17
18/// \class istringstream sistream.h ustl.h
19/// \ingroup TextStreams
20///
21/// \brief A stream that reads textual data from a memory block.
22///
23class istringstream : public istream {
24public:
25    static const size_type	c_MaxDelimiters = 16;	///< Maximum number of word delimiters.
26public:
27    				istringstream (void);
28				istringstream (const void* p, size_type n);
29    explicit			istringstream (const cmemlink& source);
30    void			iread (int8_t& v);
31    void			iread (int32_t& v);
32    void			iread (double& v);
33    void			iread (bool& v);
34    void			iread (wchar_t& v);
35    void			iread (string& v);
36#ifdef HAVE_INT64_T
37    void			iread (int64_t& v);
38#endif
39#if HAVE_LONG_LONG && (!HAVE_INT64_T || SIZE_OF_LONG_LONG > 8)
40    void			iread (long long& v);
41#endif
42    inline string		str (void) const	{ string s; s.link (*this); return (s); }
43    inline void			str (const string& s)	{ link (s); }
44    int				get (void);
45    inline void			get (char& c)	{ c = get(); }
46    void			get (char* p, size_type n, char delim = '\n');
47    void			get (string& s, char delim = '\n');
48    void			getline (char* p, size_type n, char delim = '\n');
49    void			getline (string& s, char delim = '\n');
50    void			ignore (size_type n, char delim = '\0');
51    inline char			peek (void)	{ int8_t v; iread (v); ungetc(); return (v); }
52    inline void			putback (char)	{ ungetc(); }
53    inline void			unget (void)	{ ungetc(); }
54    void			set_delimiters (const char* delimiters);
55    inline void			set_base (short base);
56    inline void			set_decimal_separator (char)	{ }
57    inline void			set_thousand_separator (char)	{ }
58    void			read (void* buffer, size_type size);
59    void			read (memlink& buf);
60    inline void			read_strz (string& str);
61    inline void			sync (void)	{ skip (remaining()); }
62protected:
63    char			skip_delimiters (void);
64private:
65    inline bool			is_delimiter (char c) const;
66    template <typename T> void	read_number (T& v);
67private:
68    char			m_Delimiters [c_MaxDelimiters];
69    uint8_t			m_Base;
70};
71
72/// Sets the numeric base used to read numbers.
73inline void istringstream::set_base (short base)
74{
75    m_Base = base;
76}
77
78/// Reads a null-terminated character stream. This is not allowed in this class.
79inline void istringstream::read_strz (string&)
80{
81    assert (!"Reading nul characters is not allowed from text streams");
82}
83
84/// Reads one type as another.
85template <typename RealT, typename CastT>
86inline void _cast_read (istringstream& is, RealT& v)
87{
88    CastT cv;
89    is.iread (cv);
90    v = RealT (cv);
91}
92
93inline istringstream& operator>> (istringstream& is, int8_t& v)	{ is.iread (v); return (is); }
94inline istringstream& operator>> (istringstream& is, int32_t& v){ is.iread (v); return (is); }
95inline istringstream& operator>> (istringstream& is, double& v)	{ is.iread (v); return (is); }
96inline istringstream& operator>> (istringstream& is, bool& v)	{ is.iread (v); return (is); }
97inline istringstream& operator>> (istringstream& is, wchar_t& v){ is.iread (v); return (is); }
98inline istringstream& operator>> (istringstream& is, string& v)	{ is.iread (v); return (is); }
99#if HAVE_INT64_T
100inline istringstream& operator>> (istringstream& is, int64_t& v){ is.iread (v); return (is); }
101#endif
102#if HAVE_LONG_LONG && (!HAVE_INT64_T || SIZE_OF_LONG_LONG > 8)
103inline istringstream& operator>> (istringstream& is, long long& v) { is.iread (v); return (is); }
104#endif
105
106#define ISTRSTREAM_CAST_OPERATOR(RealT, CastT)			\
107inline istringstream& operator>> (istringstream& is, RealT& v)	\
108{ _cast_read<RealT,CastT>(is, v); return (is); }
109
110ISTRSTREAM_CAST_OPERATOR (uint8_t,	int8_t)
111ISTRSTREAM_CAST_OPERATOR (int16_t,	int32_t)
112ISTRSTREAM_CAST_OPERATOR (uint16_t,	int32_t)
113ISTRSTREAM_CAST_OPERATOR (uint32_t,	int32_t)
114ISTRSTREAM_CAST_OPERATOR (float,	double)
115#if HAVE_THREE_CHAR_TYPES
116ISTRSTREAM_CAST_OPERATOR (char,		int8_t)
117#endif
118#if HAVE_INT64_T
119ISTRSTREAM_CAST_OPERATOR (uint64_t,	int64_t)
120#endif
121#if SIZE_OF_LONG == SIZE_OF_INT
122ISTRSTREAM_CAST_OPERATOR (long,		int)
123ISTRSTREAM_CAST_OPERATOR (unsigned long,int)
124#endif
125#if HAVE_LONG_LONG && (!HAVE_INT64_T || SIZE_OF_LONG_LONG > 8)
126ISTRSTREAM_CAST_OPERATOR (unsigned long long, long long)
127#endif
128#undef ISTRSTREAM_CAST_OPERATOR
129
130} // namespace ustl
131
132#endif
133
134