Mercurial > hg > octave-avbm
comparison src/oct-stream.h @ 2117:b240b2fce8ed
[project @ 1996-05-10 07:20:36 by jwe]
Initial revision
author | jwe |
---|---|
date | Fri, 10 May 1996 07:20:36 +0000 |
parents | |
children | ee08cc210438 |
comparison
equal
deleted
inserted
replaced
2116:99658f9b74c8 | 2117:b240b2fce8ed |
---|---|
1 /* | |
2 | |
3 Copyright (C) 1996 John W. Eaton | |
4 | |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2, or (at your option) any | |
10 later version. | |
11 | |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Octave; see the file COPYING. If not, write to the Free | |
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 | |
21 */ | |
22 | |
23 #if !defined (octave_octave_stream_h) | |
24 #define octave_octave_stream_h 1 | |
25 | |
26 #include <string> | |
27 | |
28 #include <iostream.h> | |
29 #include <strstream.h> | |
30 | |
31 #include "Array.h" | |
32 | |
33 #include "oct-obj.h" | |
34 #include "str-vec.h" | |
35 | |
36 struct | |
37 scanf_format_elt | |
38 { | |
39 scanf_format_elt (const char *txt = 0, bool d = false, | |
40 char typ = '\0', char mod = '\0') | |
41 : text (txt), discard (d), type (typ), modifier (mod) { } | |
42 | |
43 ~scanf_format_elt (void) { delete text; } | |
44 | |
45 const char *text; | |
46 bool discard; | |
47 char type; | |
48 char modifier; | |
49 }; | |
50 | |
51 class | |
52 scanf_format_list | |
53 { | |
54 public: | |
55 | |
56 scanf_format_list (const string& fmt = string ()); | |
57 | |
58 ~scanf_format_list (void); | |
59 | |
60 int num_conversions (void) { return nconv; } | |
61 | |
62 const scanf_format_elt *first (void) | |
63 { | |
64 curr_idx = 0; | |
65 return current (); | |
66 } | |
67 | |
68 const scanf_format_elt *current (void) const | |
69 { return list.length () > 0 ? list.elem (curr_idx) : 0; } | |
70 | |
71 const scanf_format_elt *next (void) | |
72 { | |
73 curr_idx++; | |
74 if (curr_idx >= list.length ()) | |
75 curr_idx = 0; | |
76 return current (); | |
77 } | |
78 | |
79 void printme (void) const; | |
80 | |
81 bool ok (void) const { return (nconv >= 0); } | |
82 | |
83 operator void* () const { return ok () ? (void *) -1 : (void *) 0; } | |
84 | |
85 bool all_character_conversions (void); | |
86 | |
87 bool all_numeric_conversions (void); | |
88 | |
89 private: | |
90 | |
91 // Number of conversions specified by this format string, or -1 if | |
92 // invalid conversions have been found. | |
93 int nconv; | |
94 | |
95 // Index to current element; | |
96 int curr_idx; | |
97 | |
98 // List of format elements. | |
99 Array<scanf_format_elt*> list; | |
100 | |
101 // Temporary buffer. | |
102 ostrstream *buf; | |
103 | |
104 void add_elt_to_list (bool discard, char type, char modifier, | |
105 int& num_elts); | |
106 | |
107 void process_conversion (const string& s, int& i, int n, bool& discard, | |
108 char& type, char& modifier, int& num_elts); | |
109 | |
110 int finish_conversion (const string& s, int& i, int n, bool discard, | |
111 char& type, char modifier, int& num_elts); | |
112 // No copying! | |
113 | |
114 scanf_format_list (const scanf_format_list&); | |
115 | |
116 scanf_format_list& operator = (const scanf_format_list&); | |
117 }; | |
118 | |
119 struct | |
120 printf_format_elt | |
121 { | |
122 printf_format_elt (const char *txt = 0, int n = 0, char typ = '\0', | |
123 char mod = '\0') | |
124 : text (txt), args (n), type (typ), modifier (mod) { } | |
125 | |
126 ~printf_format_elt (void) { delete text; } | |
127 | |
128 const char *text; | |
129 int args; | |
130 char type; | |
131 char modifier; | |
132 }; | |
133 | |
134 class | |
135 printf_format_list | |
136 { | |
137 public: | |
138 | |
139 printf_format_list (const string& fmt = string ()); | |
140 | |
141 ~printf_format_list (void); | |
142 | |
143 int num_conversions (void) { return nconv; } | |
144 | |
145 const printf_format_elt *first (void) | |
146 { | |
147 curr_idx = 0; | |
148 return current (); | |
149 } | |
150 | |
151 const printf_format_elt *current (void) const | |
152 { return list.length () > 0 ? list.elem (curr_idx) : 0; } | |
153 | |
154 const printf_format_elt *next (void) | |
155 { | |
156 curr_idx++; | |
157 if (curr_idx >= list.length ()) | |
158 curr_idx = 0; | |
159 return current (); | |
160 } | |
161 | |
162 void printme (void) const; | |
163 | |
164 bool ok (void) const { return (nconv >= 0); } | |
165 | |
166 operator void* () const { return ok () ? (void *) -1 : (void *) 0; } | |
167 | |
168 private: | |
169 | |
170 // Number of conversions specified by this format string, or -1 if | |
171 // invalid conversions have been found. | |
172 int nconv; | |
173 | |
174 // Index to current element; | |
175 int curr_idx; | |
176 | |
177 // List of format elements. | |
178 Array<printf_format_elt*> list; | |
179 | |
180 // Temporary buffer. | |
181 ostrstream *buf; | |
182 | |
183 void add_elt_to_list (int args, char type, char modifier, | |
184 int& num_elts); | |
185 | |
186 void process_conversion (const string& s, int& i, int n, int& args, | |
187 char& modifier, char& type, int& num_elts); | |
188 | |
189 void finish_conversion (const string& s, int& i, int args, | |
190 char modifier, char& type, int& num_elts); | |
191 | |
192 // No copying! | |
193 | |
194 printf_format_list (const printf_format_list&); | |
195 | |
196 printf_format_list& operator = (const printf_format_list&); | |
197 }; | |
198 | |
199 // Provide an interface for Octave streams. | |
200 | |
201 class | |
202 octave_base_stream | |
203 { | |
204 friend class octave_stream; | |
205 | |
206 public: | |
207 | |
208 enum arch_type | |
209 { | |
210 at_unknown, | |
211 at_native | |
212 }; | |
213 | |
214 enum data_type | |
215 { | |
216 dt_unknown, | |
217 dt_char, | |
218 dt_schar, | |
219 dt_uchar, | |
220 dt_short, | |
221 dt_ushort, | |
222 dt_int, | |
223 dt_uint, | |
224 dt_long, | |
225 dt_ulong, | |
226 dt_float, | |
227 dt_double, | |
228 dt_float_complex, | |
229 dt_double_complex | |
230 }; | |
231 | |
232 octave_base_stream (ios::openmode arg_md = ios::in|ios::out, | |
233 arch_type arg_at = at_native) | |
234 : md (arg_md), at (arg_at), fail (false) { } | |
235 | |
236 virtual ~octave_base_stream (void) { } | |
237 | |
238 // The remaining functions are not specific to input or output only, | |
239 // and must be provided by the derived classes. | |
240 | |
241 // Position a stream at OFFSET relative to ORIGIN. | |
242 | |
243 virtual int seek (streampos offset, ios::seek_dir origin) = 0; | |
244 | |
245 // Return current stream position. | |
246 | |
247 virtual long tell (void) const = 0; | |
248 | |
249 // Return non-zero if EOF has been reached on this stream. | |
250 | |
251 virtual bool eof (void) const = 0; | |
252 | |
253 // The name of the file. | |
254 | |
255 virtual string name (void) = 0; | |
256 | |
257 // If the derived class provides this function and it returns a | |
258 // pointer to a valid istream, scanf(), read(), getl(), and gets() | |
259 // will automatically work for this stream. | |
260 | |
261 virtual istream *input_stream (void) { return 0; } | |
262 | |
263 // If the derived class provides this function and it returns a | |
264 // pointer to a valid ostream, flush(), write(), and printf() will | |
265 // automatically work for this stream. | |
266 | |
267 virtual ostream *output_stream (void) { return 0; } | |
268 | |
269 bool ok (void) const { return ! fail; } | |
270 | |
271 // Return current error message for this stream. | |
272 | |
273 string error (bool clear, int& errno); | |
274 | |
275 protected: | |
276 | |
277 int mode (void) { return md; } | |
278 | |
279 arch_type architecture (void) { return at; } | |
280 | |
281 // Set current error state and set fail to TRUE. | |
282 | |
283 void error (const string& msg); | |
284 | |
285 // Clear any error message and set fail to FALSE. | |
286 | |
287 void clear (void); | |
288 | |
289 private: | |
290 | |
291 // The permission bits for the file. Should be some combination of | |
292 // ios::open_mode bits. | |
293 int md; | |
294 | |
295 // Data format. | |
296 arch_type at; | |
297 | |
298 // TRUE if an error has occurred. | |
299 bool fail; | |
300 | |
301 // Should contain error message if fail is TRUE. | |
302 string errmsg; | |
303 | |
304 // Functions that are defined for all input streams (input streams | |
305 // are those that define is). | |
306 | |
307 string do_gets (int max_len, bool& err, bool strip_newline, const char *fcn); | |
308 | |
309 string getl (int max_len, bool& err); | |
310 string gets (int max_len, bool& err); | |
311 | |
312 octave_value do_read (int nr, int nc, data_type dt, int skip, | |
313 arch_type at, int& count); | |
314 | |
315 octave_value read (const Matrix& size, data_type dt, int skip, | |
316 arch_type at, int& count); | |
317 | |
318 octave_value do_char_scanf (scanf_format_list& fmt_list, | |
319 int nr, int nc, int& count); | |
320 | |
321 octave_value do_real_scanf (scanf_format_list& fmt_list, | |
322 int nr, int nc, int& count); | |
323 | |
324 octave_value do_scanf (scanf_format_list& fmt_list, int nr, int nc, | |
325 int& count); | |
326 | |
327 octave_value scanf (const string& fmt, const Matrix& size, int& count); | |
328 | |
329 // Functions that are defined for all output streams (output streams | |
330 // are those that define os). | |
331 | |
332 int flush (void); | |
333 | |
334 int do_write (const double *d, int n, data_type dt, int skip, | |
335 arch_type at); | |
336 | |
337 int write (const octave_value& data, data_type dt, int skip, | |
338 arch_type at); | |
339 | |
340 int do_printf (printf_format_list& fmt_list, const octave_value_list& args); | |
341 | |
342 int printf (const string& fmt, const octave_value_list& args); | |
343 | |
344 int puts (const string& s); | |
345 | |
346 // We can always do this in terms of seek(), so the derived class | |
347 // only has to provide that. | |
348 | |
349 int rewind (void); | |
350 | |
351 void invalid_operation (const char *op, const char *rw); | |
352 | |
353 // No copying! | |
354 | |
355 octave_base_stream (const octave_base_stream&); | |
356 | |
357 octave_base_stream& operator = (const octave_base_stream&); | |
358 }; | |
359 | |
360 class | |
361 octave_stream | |
362 { | |
363 public: | |
364 | |
365 octave_stream (octave_base_stream *bs = 0) : rep (bs) { } | |
366 | |
367 ~octave_stream (void) { delete rep; } | |
368 | |
369 int flush (void); | |
370 | |
371 string getl (int max_len, bool& err); | |
372 string getl (const octave_value& max_len, bool& err); | |
373 | |
374 string gets (int max_len, bool& err); | |
375 string gets (const octave_value& max_len, bool& err); | |
376 | |
377 int seek (streampos offset, ios::seek_dir origin); | |
378 int seek (const octave_value& offset, const octave_value& origin); | |
379 | |
380 long tell (void) const; | |
381 | |
382 int rewind (void); | |
383 | |
384 octave_value read (const Matrix& size, | |
385 octave_base_stream::data_type dt, | |
386 int skip, octave_base_stream::arch_type at, | |
387 int& count); | |
388 | |
389 int write (const octave_value& data, | |
390 octave_base_stream::data_type dt, int skip, | |
391 octave_base_stream::arch_type at); | |
392 | |
393 octave_value scanf (const string& fmt, const Matrix& size, int& count); | |
394 | |
395 int printf (const string& fmt, const octave_value_list& args); | |
396 | |
397 int puts (const string& s); | |
398 int puts (const octave_value& s); | |
399 | |
400 bool eof (void) const; | |
401 | |
402 string error (bool clear, int& errno); | |
403 | |
404 string error (bool clear = false) | |
405 { | |
406 int errno; | |
407 return error (clear, errno); | |
408 } | |
409 | |
410 bool ok (void) const { return rep && rep->ok (); } | |
411 | |
412 operator void* () const { return ok () ? (void *) -1 : (void *) 0; } | |
413 | |
414 string name (void); | |
415 | |
416 int mode (void); | |
417 | |
418 octave_base_stream::arch_type architecture (void); | |
419 | |
420 static string mode_as_string (int mode); | |
421 | |
422 static string arch_as_string (octave_base_stream::arch_type at); | |
423 | |
424 static octave_base_stream::data_type string_to_data_type (const string& s); | |
425 static octave_base_stream::arch_type string_to_arch_type (const string& s); | |
426 | |
427 private: | |
428 | |
429 // The actual representation of this stream. | |
430 octave_base_stream *rep; | |
431 | |
432 void invalid_stream_error (const char *op) const; | |
433 | |
434 bool stream_ok (const char *op, bool clear = true) const | |
435 { | |
436 bool retval = true; | |
437 | |
438 if (rep) | |
439 { | |
440 if (clear) | |
441 rep->clear (); | |
442 } | |
443 else | |
444 { | |
445 retval = false; | |
446 invalid_stream_error (op); | |
447 } | |
448 | |
449 return retval; | |
450 } | |
451 | |
452 void error (const string& msg) | |
453 { | |
454 if (rep) | |
455 rep->error (msg); | |
456 } | |
457 | |
458 // Must create named streams. | |
459 | |
460 octave_stream (void); | |
461 | |
462 // No copying! | |
463 | |
464 octave_stream (const octave_stream&); | |
465 | |
466 octave_stream& operator = (const octave_stream&); | |
467 }; | |
468 | |
469 class | |
470 octave_stream_list | |
471 { | |
472 protected: | |
473 | |
474 octave_stream_list (void) : list (32), curr_len (0) { } | |
475 | |
476 public: | |
477 | |
478 ~octave_stream_list (void) { } | |
479 | |
480 static int insert (octave_base_stream *obs); | |
481 | |
482 static octave_stream *lookup (int fid); | |
483 static octave_stream *lookup (const octave_value& fid); | |
484 | |
485 static int remove (int fid); | |
486 static int remove (const octave_value& fid); | |
487 | |
488 static void clear (void); | |
489 | |
490 static string_vector get_info (int fid); | |
491 static string_vector get_info (const octave_value& fid); | |
492 | |
493 static string list_open_files (void); | |
494 | |
495 static octave_value open_file_numbers (void); | |
496 | |
497 private: | |
498 | |
499 Array<octave_stream*> list; | |
500 | |
501 int curr_len; | |
502 | |
503 static octave_stream_list *instance; | |
504 | |
505 int do_insert (octave_base_stream *obs); | |
506 | |
507 octave_stream *do_lookup (int fid) const; | |
508 octave_stream *do_lookup (const octave_value& fid) const; | |
509 | |
510 int do_remove (int fid); | |
511 int do_remove (const octave_value& fid); | |
512 | |
513 void do_clear (void); | |
514 | |
515 string_vector do_get_info (int fid) const; | |
516 string_vector do_get_info (const octave_value& fid) const; | |
517 | |
518 string do_list_open_files (void) const; | |
519 | |
520 octave_value do_open_file_numbers (void) const; | |
521 | |
522 int get_file_number (const octave_value& fid) const; | |
523 }; | |
524 | |
525 #endif | |
526 | |
527 /* | |
528 ;;; Local Variables: *** | |
529 ;;; mode: C++ *** | |
530 ;;; End: *** | |
531 */ |