]> git.saurik.com Git - wxWidgets.git/blob - src/common/stdstream.cpp
af8475cdd3b331b37edd2966c7c61859f17e8a45
[wxWidgets.git] / src / common / stdstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/stdstream.h
3 // Purpose: Implementation of std::istream and std::ostream derived
4 // wrappers for wxInputStream and wxOutputStream
5 // Author: Jonathan Liu <net147@gmail.com>
6 // Created: 2009-05-02
7 // RCS-ID: $Id$
8 // Copyright: (c) 2009 Jonathan Liu
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ==========================================================================
13 // Declarations
14 // ==========================================================================
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_STD_IOSTREAM
24
25 #ifndef WX_PRECOMP
26 #endif
27
28 #include "wx/stdstream.h"
29
30 #include <ios>
31 #include <istream>
32 #include <ostream>
33 #include <streambuf>
34
35 // ==========================================================================
36 // Helpers
37 // ==========================================================================
38
39 namespace
40 {
41
42 inline bool
43 IosSeekDirToWxSeekMode(std::ios_base::seekdir way,
44 wxSeekMode& seekMode)
45 {
46 switch ( way )
47 {
48 case std::ios_base::beg:
49 seekMode = wxFromStart;
50 break;
51 case std::ios_base::cur:
52 seekMode = wxFromCurrent;
53 break;
54 case std::ios_base::end:
55 seekMode = wxFromEnd;
56 break;
57 default:
58 return false;
59 }
60
61 return true;
62 }
63
64 } // anonymous namespace
65
66 // ==========================================================================
67 // wxStdInputStreamBuffer
68 // ==========================================================================
69
70 wxStdInputStreamBuffer::wxStdInputStreamBuffer(wxInputStream& stream) :
71 m_stream(stream), m_lastChar(EOF)
72 {
73 }
74
75 std::streambuf *
76 wxStdInputStreamBuffer::setbuf(char *WXUNUSED(s),
77 std::streamsize WXUNUSED(n))
78 {
79 return NULL;
80 }
81
82 std::streampos
83 wxStdInputStreamBuffer::seekoff(std::streamoff off,
84 std::ios_base::seekdir way,
85 std::ios_base::openmode which)
86 {
87 wxSeekMode seekMode;
88
89 if ( !IosSeekDirToWxSeekMode(way, seekMode) )
90 return -1;
91 if ( !(which & std::ios_base::in) )
92 return -1;
93
94 off_t newPos = m_stream.SeekI((off_t) off, seekMode);
95
96 if ( newPos != wxInvalidOffset )
97 return (std::streampos) newPos;
98 else
99 return -1;
100 }
101
102 std::streampos
103 wxStdInputStreamBuffer::seekpos(std::streampos sp,
104 std::ios_base::openmode which)
105 {
106 if ( !(which & std::ios_base::in) )
107 return -1;
108
109 off_t newPos = m_stream.SeekI((off_t) sp);
110
111 if ( newPos != wxInvalidOffset )
112 return (std::streampos) newPos;
113 else
114 return -1;
115 }
116
117 std::streamsize
118 wxStdInputStreamBuffer::showmanyc()
119 {
120 if ( m_stream.CanRead() && (off_t) m_stream.GetSize() > m_stream.TellI() )
121 return m_stream.GetSize() - m_stream.TellI();
122 else
123 return 0;
124 }
125
126 std::streamsize
127 wxStdInputStreamBuffer::xsgetn(char *s, std::streamsize n)
128 {
129 m_stream.Read((void *) s, (size_t) n);
130
131 std::streamsize read = m_stream.LastRead();
132
133 if ( read > 0 )
134 m_lastChar = (unsigned char) s[read - 1];
135
136 return read;
137 }
138
139 int
140 wxStdInputStreamBuffer::underflow()
141 {
142 int ch = m_stream.GetC();
143
144 if ( m_stream.LastRead() == 1 )
145 {
146 m_stream.Ungetch((char) ch);
147 return ch;
148 }
149 else
150 {
151 return EOF;
152 }
153 }
154
155 int
156 wxStdInputStreamBuffer::uflow()
157 {
158 int ch = m_stream.GetC();
159
160 if ( m_stream.LastRead() == 1 )
161 {
162 m_lastChar = ch;
163 return ch;
164 }
165 else
166 {
167 return EOF;
168 }
169 }
170
171 int
172 wxStdInputStreamBuffer::pbackfail(int c)
173 {
174 if ( c == EOF )
175 {
176 if ( m_lastChar == EOF )
177 return EOF;
178
179 c = m_lastChar;
180 m_lastChar = EOF;
181 }
182
183 return m_stream.Ungetch((char) c) ? c : EOF;
184 }
185
186 // ==========================================================================
187 // wxStdInputStream
188 // ==========================================================================
189
190 wxStdInputStream::wxStdInputStream(wxInputStream& stream) :
191 std::istream(NULL), m_streamBuffer(stream)
192 {
193 std::ios::init(&m_streamBuffer);
194 }
195
196 // ==========================================================================
197 // wxStdOutputStreamBuffer
198 // ==========================================================================
199
200 wxStdOutputStreamBuffer::wxStdOutputStreamBuffer(wxOutputStream& stream) :
201 m_stream(stream)
202 {
203 }
204
205 std::streambuf *
206 wxStdOutputStreamBuffer::setbuf(char *WXUNUSED(s),
207 std::streamsize WXUNUSED(n))
208 {
209 return NULL;
210 }
211
212 std::streampos
213 wxStdOutputStreamBuffer::seekoff(std::streamoff off,
214 std::ios_base::seekdir way,
215 std::ios_base::openmode which)
216 {
217 wxSeekMode seekMode;
218
219 if ( !IosSeekDirToWxSeekMode(way, seekMode) )
220 return -1;
221 if ( !(which & std::ios_base::out) )
222 return -1;
223
224 off_t newPos = m_stream.SeekO((off_t) off, seekMode);
225
226 if ( newPos != wxInvalidOffset )
227 return (std::streampos) newPos;
228 else
229 return -1;
230 }
231
232 std::streampos
233 wxStdOutputStreamBuffer::seekpos(std::streampos sp,
234 std::ios_base::openmode which)
235 {
236 if ( !(which & std::ios_base::out) )
237 return -1;
238
239 off_t newPos = m_stream.SeekO((off_t) sp);
240
241 if ( newPos != wxInvalidOffset )
242 return (std::streampos) newPos;
243 else
244 return -1;
245 }
246
247 std::streamsize
248 wxStdOutputStreamBuffer::xsputn(const char *s,
249 std::streamsize n)
250 {
251 m_stream.Write((const void *) s, (size_t) n);
252 return (std::streamsize) m_stream.LastWrite();
253 }
254
255 int
256 wxStdOutputStreamBuffer::overflow(int c)
257 {
258 m_stream.PutC(c);
259 return m_stream.IsOk() ? c : EOF;
260 }
261
262 // ==========================================================================
263 // wxStdOutputStream
264 // ==========================================================================
265
266 wxStdOutputStream::wxStdOutputStream(wxOutputStream& stream) :
267 std::ostream(NULL), m_streamBuffer(stream)
268 {
269 std::ios::init(&m_streamBuffer);
270 }
271
272 #endif // wxUSE_STD_IOSTREAM