]>
Commit | Line | Data |
---|---|---|
fae05df5 GL |
1 | ///////////////////////////////////////////////////////////////////////////////// |
2 | // Name: txtstrm.cpp | |
3 | // Purpose: Text stream classes | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 28/06/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Guilhem Lavaux | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "txtstrm.h" | |
14 | #endif | |
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_STREAMS | |
24 | ||
25 | #include "wx/txtstrm.h" | |
26 | ||
27 | wxTextInputStream::wxTextInputStream(wxInputStream& s) | |
28 | : m_input(&s) | |
29 | { | |
30 | } | |
31 | ||
32 | wxTextInputStream::~wxTextInputStream() | |
33 | { | |
34 | } | |
35 | ||
36 | wxUint32 wxTextInputStream::Read32() | |
37 | { | |
38 | /* I only implemented a simple integer parser */ | |
39 | int c; | |
40 | int sign; | |
41 | wxInt32 i; | |
42 | ||
43 | while (isspace( c = m_input->GetC() ) ) | |
44 | /* Do nothing */ ; | |
45 | ||
46 | i = 0; | |
47 | if (! (c == '-' || isdigit(c)) ) { | |
48 | m_input->Ungetch(c); | |
49 | return 0; | |
50 | } | |
51 | ||
52 | if (c == '-') { | |
53 | sign = -1; | |
54 | c = m_input->GetC(); | |
55 | } else if (c == '+') { | |
56 | sign = 1; | |
57 | c = m_input->GetC(); | |
58 | } else { | |
59 | sign = 1; | |
60 | } | |
61 | ||
62 | while (isdigit(c)) { | |
63 | i = i*10 + (c - (int)'0'); | |
64 | c = m_input->GetC(); | |
65 | } | |
66 | ||
67 | if (c != '\n' && c != '\r') | |
68 | m_input->Ungetch(c); | |
69 | ||
70 | i *= sign; | |
71 | ||
72 | return (wxUint32)i; | |
73 | } | |
74 | ||
75 | wxUint16 wxTextInputStream::Read16() | |
76 | { | |
77 | return (wxUint16)Read32(); | |
78 | } | |
79 | ||
80 | wxUint8 wxTextInputStream::Read8() | |
81 | { | |
82 | return (wxUint8)Read32(); | |
83 | } | |
84 | ||
85 | double wxTextInputStream::ReadDouble() | |
86 | { | |
87 | /* I only implemented a simple float parser */ | |
88 | double f; | |
89 | int c, sign; | |
90 | ||
91 | while (isspace( c = m_input->GetC() ) || c == '\n' || c == '\r') | |
92 | /* Do nothing */ ; | |
93 | ||
94 | f = 0.0; | |
95 | if (! (c == '-' || isdigit(c)) ) { | |
96 | m_input->Ungetch(c); | |
97 | return 0.0; | |
98 | } | |
99 | ||
100 | if (c == '-') { | |
101 | sign = -1; | |
102 | c = m_input->GetC(); | |
103 | } else if (c == '+') { | |
104 | sign = 1; | |
105 | c = m_input->GetC(); | |
106 | } else { | |
107 | sign = 1; | |
108 | } | |
109 | ||
110 | while (isdigit(c)) { | |
111 | f = f*10 + (c - '0'); | |
112 | c = m_input->GetC(); | |
113 | } | |
114 | ||
115 | if (c == '.') { | |
116 | double f_multiplicator = (double) 0.1; | |
117 | ||
118 | c = m_input->GetC(); | |
119 | ||
120 | while (isdigit(c)) { | |
121 | f += (c-'0')*f_multiplicator; | |
122 | f_multiplicator /= 10; | |
123 | c = m_input->GetC(); | |
124 | } | |
125 | ||
126 | if (c == 'e') { | |
127 | double f_multiplicator = 0.0; | |
128 | int i, e; | |
129 | ||
130 | c = m_input->GetC(); | |
131 | ||
132 | switch(c) { | |
133 | case '-': | |
134 | f_multiplicator = 0.1; | |
135 | break; | |
136 | case '+': | |
137 | f_multiplicator = 10.0; | |
138 | break; | |
139 | } | |
140 | ||
141 | e = Read8(); | |
142 | ||
143 | for (i=0;i<e;i++) | |
144 | f *= f_multiplicator; | |
145 | } else if (c != '\n' && c != '\r') | |
146 | m_input->Ungetch(c); | |
147 | ||
148 | } | |
149 | ||
150 | f *= sign; | |
151 | ||
152 | return f; | |
153 | } | |
154 | ||
155 | wxString wxTextInputStream::ReadString() | |
156 | { | |
157 | char c, last_endl = 0; | |
158 | bool end_line = FALSE; | |
159 | wxString line; | |
160 | ||
161 | while (!end_line) { | |
162 | c = m_input->GetC(); | |
163 | if (m_input->LastError() != wxStream_NOERROR) | |
164 | break; | |
165 | ||
166 | switch (c) { | |
167 | case '\n': | |
168 | end_line = TRUE; | |
169 | break; | |
170 | case '\r': | |
171 | last_endl = '\r'; | |
172 | break; | |
173 | default: | |
174 | if (last_endl == '\r') { | |
175 | end_line = TRUE; | |
176 | m_input->Ungetch(c); | |
177 | break; | |
178 | } | |
179 | line += c; | |
180 | break; | |
181 | } | |
182 | } | |
183 | return line; | |
184 | } | |
185 | ||
186 | wxTextInputStream& wxTextInputStream::operator>>(wxString& line) | |
187 | { | |
188 | line = ReadString(); | |
189 | return *this; | |
190 | } | |
191 | ||
192 | wxTextInputStream& wxTextInputStream::operator>>(wxInt8& c) | |
193 | { | |
194 | c = (wxInt8)Read8(); | |
195 | return *this; | |
196 | } | |
197 | ||
198 | wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i) | |
199 | { | |
200 | i = (wxInt16)Read16(); | |
201 | return *this; | |
202 | } | |
203 | ||
204 | wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i) | |
205 | { | |
206 | i = (wxInt32)Read32(); | |
207 | return *this; | |
208 | } | |
209 | ||
210 | wxTextInputStream& wxTextInputStream::operator>>(wxUint8& c) | |
211 | { | |
212 | c = Read8(); | |
213 | return *this; | |
214 | } | |
215 | ||
216 | wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i) | |
217 | { | |
218 | i = Read16(); | |
219 | return *this; | |
220 | } | |
221 | ||
222 | wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i) | |
223 | { | |
224 | i = Read32(); | |
225 | return *this; | |
226 | } | |
227 | ||
228 | wxTextInputStream& wxTextInputStream::operator>>(double& i) | |
229 | { | |
230 | i = ReadDouble(); | |
231 | return *this; | |
232 | } | |
233 | ||
234 | wxTextInputStream& wxTextInputStream::operator>>(float& f) | |
235 | { | |
236 | f = (float)ReadDouble(); | |
237 | return *this; | |
238 | } | |
239 | ||
240 | wxTextOutputStream::wxTextOutputStream(wxOutputStream& s) | |
241 | : m_output(&s) | |
242 | { | |
243 | } | |
244 | ||
245 | wxTextOutputStream::~wxTextOutputStream() | |
246 | { | |
247 | } | |
248 | ||
249 | void wxTextOutputStream::Write32(wxUint32 i) | |
250 | { | |
251 | wxString str; | |
252 | ||
253 | str.Printf(_T("%u"), i); | |
254 | WriteString(str); | |
255 | } | |
256 | ||
257 | void wxTextOutputStream::Write16(wxUint16 i) | |
258 | { | |
259 | wxString str; | |
260 | ||
261 | str.Printf(_T("%u"), i); | |
262 | WriteString(str); | |
263 | } | |
264 | ||
265 | void wxTextOutputStream::Write8(wxUint8 i) | |
266 | { | |
267 | wxString str; | |
268 | ||
269 | str.Printf(_T("%u"), i); | |
270 | WriteString(str); | |
271 | } | |
272 | ||
273 | void wxTextOutputStream::WriteDouble(double d) | |
274 | { | |
275 | wxString str; | |
276 | ||
277 | str.Printf(_T("%f"), d); | |
278 | WriteString(str); | |
279 | } | |
280 | ||
281 | void wxTextOutputStream::WriteString(const wxString& string) | |
282 | { | |
283 | #if wxUSE_UNICODE | |
284 | const wxWX2MBbuf buf = string.mb_str(); | |
285 | m_output->Write(buf, string.Len()); | |
286 | #else | |
287 | m_output->Write(string, string.Len()); | |
288 | #endif | |
289 | } | |
290 | ||
291 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string) | |
292 | { | |
293 | WriteString(wxString(string)); | |
294 | return *this; | |
295 | } | |
296 | ||
297 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string) | |
298 | { | |
299 | WriteString(string); | |
300 | return *this; | |
301 | } | |
302 | ||
303 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt8 c) | |
304 | { | |
305 | Write8((wxUint8)c); | |
306 | return *this; | |
307 | } | |
308 | ||
309 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c) | |
310 | { | |
311 | Write16((wxUint16)c); | |
312 | return *this; | |
313 | } | |
314 | ||
315 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c) | |
316 | { | |
317 | Write32((wxUint32)c); | |
318 | return *this; | |
319 | } | |
320 | ||
321 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint8 c) | |
322 | { | |
323 | Write8(c); | |
324 | return *this; | |
325 | } | |
326 | ||
327 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c) | |
328 | { | |
329 | Write16(c); | |
330 | return *this; | |
331 | } | |
332 | ||
333 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c) | |
334 | { | |
335 | Write32(c); | |
336 | return *this; | |
337 | } | |
338 | ||
339 | wxTextOutputStream &wxTextOutputStream::operator<<(double f) | |
340 | { | |
341 | WriteDouble(f); | |
342 | return *this; | |
343 | } | |
344 | ||
345 | wxTextOutputStream& wxTextOutputStream::operator<<(float f) | |
346 | { | |
347 | WriteDouble((double)f); | |
348 | return *this; | |
349 | } | |
350 | ||
351 | #endif | |
352 | // wxUSE_STREAMS |