]>
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 | ||
940ddb19 | 192 | wxTextInputStream& wxTextInputStream::operator>>(wxChar& c) |
fae05df5 | 193 | { |
940ddb19 GL |
194 | // TODO |
195 | /* | |
196 | m_input->Read(&c, sizeof(wxChar)); | |
197 | */ | |
fae05df5 GL |
198 | return *this; |
199 | } | |
200 | ||
201 | wxTextInputStream& wxTextInputStream::operator>>(wxInt16& i) | |
202 | { | |
203 | i = (wxInt16)Read16(); | |
204 | return *this; | |
205 | } | |
206 | ||
207 | wxTextInputStream& wxTextInputStream::operator>>(wxInt32& i) | |
208 | { | |
209 | i = (wxInt32)Read32(); | |
210 | return *this; | |
211 | } | |
212 | ||
fae05df5 GL |
213 | wxTextInputStream& wxTextInputStream::operator>>(wxUint16& i) |
214 | { | |
215 | i = Read16(); | |
216 | return *this; | |
217 | } | |
218 | ||
219 | wxTextInputStream& wxTextInputStream::operator>>(wxUint32& i) | |
220 | { | |
221 | i = Read32(); | |
222 | return *this; | |
223 | } | |
224 | ||
225 | wxTextInputStream& wxTextInputStream::operator>>(double& i) | |
226 | { | |
227 | i = ReadDouble(); | |
228 | return *this; | |
229 | } | |
230 | ||
231 | wxTextInputStream& wxTextInputStream::operator>>(float& f) | |
232 | { | |
233 | f = (float)ReadDouble(); | |
234 | return *this; | |
235 | } | |
236 | ||
237 | wxTextOutputStream::wxTextOutputStream(wxOutputStream& s) | |
238 | : m_output(&s) | |
239 | { | |
240 | } | |
241 | ||
242 | wxTextOutputStream::~wxTextOutputStream() | |
243 | { | |
244 | } | |
245 | ||
246 | void wxTextOutputStream::Write32(wxUint32 i) | |
247 | { | |
248 | wxString str; | |
249 | ||
250 | str.Printf(_T("%u"), i); | |
251 | WriteString(str); | |
252 | } | |
253 | ||
254 | void wxTextOutputStream::Write16(wxUint16 i) | |
255 | { | |
256 | wxString str; | |
257 | ||
258 | str.Printf(_T("%u"), i); | |
259 | WriteString(str); | |
260 | } | |
261 | ||
262 | void wxTextOutputStream::Write8(wxUint8 i) | |
263 | { | |
264 | wxString str; | |
265 | ||
266 | str.Printf(_T("%u"), i); | |
267 | WriteString(str); | |
268 | } | |
269 | ||
270 | void wxTextOutputStream::WriteDouble(double d) | |
271 | { | |
272 | wxString str; | |
273 | ||
274 | str.Printf(_T("%f"), d); | |
275 | WriteString(str); | |
276 | } | |
277 | ||
278 | void wxTextOutputStream::WriteString(const wxString& string) | |
279 | { | |
280 | #if wxUSE_UNICODE | |
281 | const wxWX2MBbuf buf = string.mb_str(); | |
282 | m_output->Write(buf, string.Len()); | |
283 | #else | |
284 | m_output->Write(string, string.Len()); | |
285 | #endif | |
286 | } | |
287 | ||
288 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxChar *string) | |
289 | { | |
290 | WriteString(wxString(string)); | |
291 | return *this; | |
292 | } | |
293 | ||
294 | wxTextOutputStream& wxTextOutputStream::operator<<(const wxString& string) | |
295 | { | |
296 | WriteString(string); | |
297 | return *this; | |
298 | } | |
299 | ||
940ddb19 | 300 | wxTextOutputStream& wxTextOutputStream::operator<<(wxChar c) |
fae05df5 | 301 | { |
940ddb19 GL |
302 | wxString tmp_str; |
303 | tmp_str.Printf("%c", c); | |
304 | WriteString(tmp_str); | |
fae05df5 GL |
305 | return *this; |
306 | } | |
307 | ||
308 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt16 c) | |
309 | { | |
310 | Write16((wxUint16)c); | |
311 | return *this; | |
312 | } | |
313 | ||
314 | wxTextOutputStream& wxTextOutputStream::operator<<(wxInt32 c) | |
315 | { | |
316 | Write32((wxUint32)c); | |
317 | return *this; | |
318 | } | |
319 | ||
fae05df5 GL |
320 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint16 c) |
321 | { | |
322 | Write16(c); | |
323 | return *this; | |
324 | } | |
325 | ||
326 | wxTextOutputStream& wxTextOutputStream::operator<<(wxUint32 c) | |
327 | { | |
328 | Write32(c); | |
329 | return *this; | |
330 | } | |
331 | ||
332 | wxTextOutputStream &wxTextOutputStream::operator<<(double f) | |
333 | { | |
334 | WriteDouble(f); | |
335 | return *this; | |
336 | } | |
337 | ||
338 | wxTextOutputStream& wxTextOutputStream::operator<<(float f) | |
339 | { | |
340 | WriteDouble((double)f); | |
341 | return *this; | |
342 | } | |
343 | ||
344 | #endif | |
345 | // wxUSE_STREAMS |