2117
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2117
|
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 |
2877
|
26 class Matrix; |
|
27 class string_vector; |
|
28 class octave_value; |
|
29 class octave_value_list; |
2117
|
30 |
2877
|
31 class istream; |
|
32 class ostream; |
|
33 class ostrstream; |
|
34 |
|
35 #include <string> |
2117
|
36 |
|
37 #include "Array.h" |
2317
|
38 #include "data-conv.h" |
|
39 #include "mach-info.h" |
2117
|
40 |
|
41 struct |
|
42 scanf_format_elt |
|
43 { |
2215
|
44 scanf_format_elt (const char *txt = 0, int w = 0, bool d = false, |
2117
|
45 char typ = '\0', char mod = '\0') |
2215
|
46 : text (txt), width (w), discard (d), type (typ), modifier (mod) { } |
2117
|
47 |
|
48 ~scanf_format_elt (void) { delete text; } |
|
49 |
|
50 const char *text; |
2215
|
51 int width; |
2117
|
52 bool discard; |
|
53 char type; |
|
54 char modifier; |
|
55 }; |
|
56 |
|
57 class |
|
58 scanf_format_list |
|
59 { |
|
60 public: |
|
61 |
|
62 scanf_format_list (const string& fmt = string ()); |
|
63 |
|
64 ~scanf_format_list (void); |
|
65 |
|
66 int num_conversions (void) { return nconv; } |
|
67 |
2215
|
68 // The length can be different than the number of conversions. |
|
69 // For example, "x %d y %d z" has 2 conversions but the length of |
|
70 // the list is 3 because of the characters that appear after the |
|
71 // last conversion. |
|
72 |
|
73 int length (void) { return list.length (); } |
|
74 |
2117
|
75 const scanf_format_elt *first (void) |
|
76 { |
|
77 curr_idx = 0; |
|
78 return current (); |
|
79 } |
|
80 |
|
81 const scanf_format_elt *current (void) const |
|
82 { return list.length () > 0 ? list.elem (curr_idx) : 0; } |
|
83 |
|
84 const scanf_format_elt *next (void) |
|
85 { |
|
86 curr_idx++; |
|
87 if (curr_idx >= list.length ()) |
|
88 curr_idx = 0; |
|
89 return current (); |
|
90 } |
|
91 |
|
92 void printme (void) const; |
|
93 |
|
94 bool ok (void) const { return (nconv >= 0); } |
|
95 |
2800
|
96 operator void* () const |
|
97 { |
|
98 return ok () |
|
99 ? static_cast<void *> (-1) : static_cast<void *> (0); |
|
100 } |
2117
|
101 |
|
102 bool all_character_conversions (void); |
|
103 |
|
104 bool all_numeric_conversions (void); |
|
105 |
|
106 private: |
|
107 |
|
108 // Number of conversions specified by this format string, or -1 if |
|
109 // invalid conversions have been found. |
|
110 int nconv; |
|
111 |
|
112 // Index to current element; |
|
113 int curr_idx; |
|
114 |
|
115 // List of format elements. |
|
116 Array<scanf_format_elt*> list; |
|
117 |
|
118 // Temporary buffer. |
|
119 ostrstream *buf; |
|
120 |
2215
|
121 void add_elt_to_list (int width, bool discard, char type, char modifier, |
2117
|
122 int& num_elts); |
|
123 |
2215
|
124 void process_conversion (const string& s, int& i, int n, int& width, |
|
125 bool& discard, char& type, char& modifier, |
|
126 int& num_elts); |
2117
|
127 |
2215
|
128 int finish_conversion (const string& s, int& i, int n, int& width, |
|
129 bool discard, char& type, char modifier, |
|
130 int& num_elts); |
2117
|
131 // No copying! |
|
132 |
|
133 scanf_format_list (const scanf_format_list&); |
|
134 |
|
135 scanf_format_list& operator = (const scanf_format_list&); |
|
136 }; |
|
137 |
|
138 struct |
|
139 printf_format_elt |
|
140 { |
|
141 printf_format_elt (const char *txt = 0, int n = 0, char typ = '\0', |
|
142 char mod = '\0') |
|
143 : text (txt), args (n), type (typ), modifier (mod) { } |
|
144 |
|
145 ~printf_format_elt (void) { delete text; } |
|
146 |
|
147 const char *text; |
|
148 int args; |
|
149 char type; |
|
150 char modifier; |
|
151 }; |
|
152 |
|
153 class |
|
154 printf_format_list |
|
155 { |
|
156 public: |
|
157 |
|
158 printf_format_list (const string& fmt = string ()); |
|
159 |
|
160 ~printf_format_list (void); |
|
161 |
|
162 int num_conversions (void) { return nconv; } |
|
163 |
|
164 const printf_format_elt *first (void) |
|
165 { |
|
166 curr_idx = 0; |
|
167 return current (); |
|
168 } |
|
169 |
|
170 const printf_format_elt *current (void) const |
|
171 { return list.length () > 0 ? list.elem (curr_idx) : 0; } |
|
172 |
|
173 const printf_format_elt *next (void) |
|
174 { |
|
175 curr_idx++; |
|
176 if (curr_idx >= list.length ()) |
|
177 curr_idx = 0; |
|
178 return current (); |
|
179 } |
|
180 |
|
181 void printme (void) const; |
|
182 |
|
183 bool ok (void) const { return (nconv >= 0); } |
|
184 |
2800
|
185 operator void* () const |
|
186 { |
|
187 return ok () |
|
188 ? static_cast<void *> (-1) : static_cast<void *> (0); |
|
189 } |
2117
|
190 |
|
191 private: |
|
192 |
|
193 // Number of conversions specified by this format string, or -1 if |
|
194 // invalid conversions have been found. |
|
195 int nconv; |
|
196 |
|
197 // Index to current element; |
|
198 int curr_idx; |
|
199 |
|
200 // List of format elements. |
|
201 Array<printf_format_elt*> list; |
|
202 |
|
203 // Temporary buffer. |
|
204 ostrstream *buf; |
|
205 |
|
206 void add_elt_to_list (int args, char type, char modifier, |
|
207 int& num_elts); |
|
208 |
|
209 void process_conversion (const string& s, int& i, int n, int& args, |
|
210 char& modifier, char& type, int& num_elts); |
|
211 |
|
212 void finish_conversion (const string& s, int& i, int args, |
|
213 char modifier, char& type, int& num_elts); |
|
214 |
|
215 // No copying! |
|
216 |
|
217 printf_format_list (const printf_format_list&); |
|
218 |
|
219 printf_format_list& operator = (const printf_format_list&); |
|
220 }; |
|
221 |
|
222 // Provide an interface for Octave streams. |
|
223 |
|
224 class |
|
225 octave_base_stream |
|
226 { |
|
227 friend class octave_stream; |
|
228 |
|
229 public: |
|
230 |
|
231 octave_base_stream (ios::openmode arg_md = ios::in|ios::out, |
2317
|
232 oct_mach_info::float_format ff = oct_mach_info::native) |
|
233 : md (arg_md), flt_fmt (ff), fail (false) { } |
2117
|
234 |
|
235 virtual ~octave_base_stream (void) { } |
|
236 |
|
237 // The remaining functions are not specific to input or output only, |
|
238 // and must be provided by the derived classes. |
|
239 |
|
240 // Position a stream at OFFSET relative to ORIGIN. |
|
241 |
2610
|
242 virtual int seek (streamoff offset, ios::seek_dir origin) = 0; |
2117
|
243 |
|
244 // Return current stream position. |
|
245 |
|
246 virtual long tell (void) const = 0; |
|
247 |
|
248 // Return non-zero if EOF has been reached on this stream. |
|
249 |
|
250 virtual bool eof (void) const = 0; |
|
251 |
|
252 // The name of the file. |
|
253 |
|
254 virtual string name (void) = 0; |
|
255 |
|
256 // If the derived class provides this function and it returns a |
|
257 // pointer to a valid istream, scanf(), read(), getl(), and gets() |
|
258 // will automatically work for this stream. |
|
259 |
|
260 virtual istream *input_stream (void) { return 0; } |
|
261 |
|
262 // If the derived class provides this function and it returns a |
|
263 // pointer to a valid ostream, flush(), write(), and printf() will |
|
264 // automatically work for this stream. |
|
265 |
|
266 virtual ostream *output_stream (void) { return 0; } |
|
267 |
|
268 bool ok (void) const { return ! fail; } |
|
269 |
|
270 // Return current error message for this stream. |
|
271 |
2435
|
272 string error (bool clear, int& err_num); |
2117
|
273 |
|
274 protected: |
|
275 |
|
276 int mode (void) { return md; } |
|
277 |
2317
|
278 oct_mach_info::float_format float_format (void) { return flt_fmt; } |
2117
|
279 |
|
280 // Set current error state and set fail to TRUE. |
|
281 |
|
282 void error (const string& msg); |
|
283 |
|
284 // Clear any error message and set fail to FALSE. |
|
285 |
|
286 void clear (void); |
|
287 |
|
288 private: |
|
289 |
|
290 // The permission bits for the file. Should be some combination of |
|
291 // ios::open_mode bits. |
|
292 int md; |
|
293 |
|
294 // Data format. |
2317
|
295 oct_mach_info::float_format flt_fmt; |
2117
|
296 |
|
297 // TRUE if an error has occurred. |
|
298 bool fail; |
|
299 |
|
300 // Should contain error message if fail is TRUE. |
|
301 string errmsg; |
|
302 |
|
303 // Functions that are defined for all input streams (input streams |
|
304 // are those that define is). |
|
305 |
2317
|
306 string do_gets (int max_len, bool& err, bool strip_newline, |
|
307 const char *fcn); |
2117
|
308 |
|
309 string getl (int max_len, bool& err); |
|
310 string gets (int max_len, bool& err); |
|
311 |
2317
|
312 octave_value do_read (int nr, int nc, oct_data_conv::data_type dt, |
|
313 int skip, oct_mach_info::float_format flt_fmt, |
|
314 int& count); |
2117
|
315 |
2317
|
316 octave_value read (const Matrix& size, oct_data_conv::data_type dt, |
|
317 int skip, oct_mach_info::float_format flt_fmt, |
|
318 int& count); |
2117
|
319 |
|
320 octave_value do_char_scanf (scanf_format_list& fmt_list, |
|
321 int nr, int nc, int& count); |
|
322 |
|
323 octave_value do_real_scanf (scanf_format_list& fmt_list, |
|
324 int nr, int nc, int& count); |
|
325 |
|
326 octave_value do_scanf (scanf_format_list& fmt_list, int nr, int nc, |
|
327 int& count); |
|
328 |
|
329 octave_value scanf (const string& fmt, const Matrix& size, int& count); |
|
330 |
2712
|
331 bool do_oscanf (const scanf_format_elt *elt, octave_value&); |
2215
|
332 |
|
333 octave_value_list oscanf (const string& fmt); |
|
334 |
2117
|
335 // Functions that are defined for all output streams (output streams |
|
336 // are those that define os). |
|
337 |
|
338 int flush (void); |
|
339 |
2317
|
340 int do_write (const Matrix& m, oct_data_conv::data_type dt, int skip, |
|
341 oct_mach_info::float_format flt_fmt); |
2117
|
342 |
2317
|
343 int write (const octave_value& data, oct_data_conv::data_type dt, |
|
344 int skip, oct_mach_info::float_format flt_fmt); |
2117
|
345 |
|
346 int do_printf (printf_format_list& fmt_list, const octave_value_list& args); |
|
347 |
|
348 int printf (const string& fmt, const octave_value_list& args); |
|
349 |
|
350 int puts (const string& s); |
|
351 |
|
352 // We can always do this in terms of seek(), so the derived class |
|
353 // only has to provide that. |
|
354 |
|
355 int rewind (void); |
|
356 |
|
357 void invalid_operation (const char *op, const char *rw); |
|
358 |
|
359 // No copying! |
|
360 |
|
361 octave_base_stream (const octave_base_stream&); |
|
362 |
|
363 octave_base_stream& operator = (const octave_base_stream&); |
|
364 }; |
|
365 |
|
366 class |
|
367 octave_stream |
|
368 { |
|
369 public: |
|
370 |
2148
|
371 octave_stream (octave_base_stream *bs = 0, bool pf = false) |
|
372 : rep (bs), preserve (pf) { } |
2117
|
373 |
2148
|
374 ~octave_stream (void) |
|
375 { |
|
376 if (! preserve) |
|
377 delete rep; |
|
378 } |
2117
|
379 |
|
380 int flush (void); |
|
381 |
|
382 string getl (int max_len, bool& err); |
|
383 string getl (const octave_value& max_len, bool& err); |
|
384 |
|
385 string gets (int max_len, bool& err); |
|
386 string gets (const octave_value& max_len, bool& err); |
|
387 |
2610
|
388 int seek (streamoff offset, ios::seek_dir origin); |
2117
|
389 int seek (const octave_value& offset, const octave_value& origin); |
|
390 |
|
391 long tell (void) const; |
|
392 |
|
393 int rewind (void); |
|
394 |
2317
|
395 octave_value read (const Matrix& size, oct_data_conv::data_type dt, |
|
396 int skip, oct_mach_info::float_format flt_fmt, |
2117
|
397 int& count); |
|
398 |
2317
|
399 int write (const octave_value& data, oct_data_conv::data_type dt, |
|
400 int skip, oct_mach_info::float_format flt_fmt); |
2117
|
401 |
|
402 octave_value scanf (const string& fmt, const Matrix& size, int& count); |
|
403 |
2215
|
404 octave_value_list oscanf (const string& fmt); |
|
405 |
2117
|
406 int printf (const string& fmt, const octave_value_list& args); |
|
407 |
|
408 int puts (const string& s); |
|
409 int puts (const octave_value& s); |
|
410 |
|
411 bool eof (void) const; |
|
412 |
2435
|
413 string error (bool clear, int& err_num); |
2117
|
414 |
|
415 string error (bool clear = false) |
|
416 { |
2435
|
417 int err_num; |
|
418 return error (clear, err_num); |
2117
|
419 } |
|
420 |
|
421 bool ok (void) const { return rep && rep->ok (); } |
|
422 |
2800
|
423 operator void* () const |
|
424 { |
|
425 return ok () |
|
426 ? static_cast<void *> (-1) : static_cast<void *> (0); |
|
427 } |
2117
|
428 |
|
429 string name (void); |
|
430 |
|
431 int mode (void); |
|
432 |
2317
|
433 oct_mach_info::float_format float_format (void); |
2117
|
434 |
|
435 static string mode_as_string (int mode); |
|
436 |
2902
|
437 istream *input_stream (void) { return rep->input_stream (); } |
|
438 |
|
439 ostream *output_stream (void) { return rep->output_stream (); } |
|
440 |
2117
|
441 private: |
|
442 |
|
443 // The actual representation of this stream. |
|
444 octave_base_stream *rep; |
|
445 |
2225
|
446 // If true, do not delete rep. |
2148
|
447 bool preserve; |
|
448 |
2117
|
449 void invalid_stream_error (const char *op) const; |
|
450 |
|
451 bool stream_ok (const char *op, bool clear = true) const |
|
452 { |
|
453 bool retval = true; |
|
454 |
|
455 if (rep) |
|
456 { |
|
457 if (clear) |
|
458 rep->clear (); |
|
459 } |
|
460 else |
|
461 { |
|
462 retval = false; |
|
463 invalid_stream_error (op); |
|
464 } |
|
465 |
|
466 return retval; |
|
467 } |
|
468 |
|
469 void error (const string& msg) |
|
470 { |
|
471 if (rep) |
|
472 rep->error (msg); |
|
473 } |
|
474 |
|
475 // Must create named streams. |
|
476 |
|
477 octave_stream (void); |
|
478 |
|
479 // No copying! |
|
480 |
|
481 octave_stream (const octave_stream&); |
|
482 |
|
483 octave_stream& operator = (const octave_stream&); |
|
484 }; |
|
485 |
|
486 class |
|
487 octave_stream_list |
|
488 { |
|
489 protected: |
|
490 |
|
491 octave_stream_list (void) : list (32), curr_len (0) { } |
|
492 |
|
493 public: |
|
494 |
|
495 ~octave_stream_list (void) { } |
|
496 |
2902
|
497 static octave_value insert (octave_base_stream *obs); |
2117
|
498 |
|
499 static octave_stream *lookup (int fid); |
|
500 static octave_stream *lookup (const octave_value& fid); |
|
501 |
|
502 static int remove (int fid); |
|
503 static int remove (const octave_value& fid); |
|
504 |
|
505 static void clear (void); |
|
506 |
|
507 static string_vector get_info (int fid); |
|
508 static string_vector get_info (const octave_value& fid); |
|
509 |
|
510 static string list_open_files (void); |
|
511 |
|
512 static octave_value open_file_numbers (void); |
|
513 |
2609
|
514 static int get_file_number (const octave_value& fid); |
|
515 |
2117
|
516 private: |
|
517 |
|
518 Array<octave_stream*> list; |
|
519 |
|
520 int curr_len; |
|
521 |
|
522 static octave_stream_list *instance; |
|
523 |
2902
|
524 octave_value do_insert (octave_base_stream *obs); |
2117
|
525 |
|
526 octave_stream *do_lookup (int fid) const; |
|
527 octave_stream *do_lookup (const octave_value& fid) const; |
|
528 |
|
529 int do_remove (int fid); |
|
530 int do_remove (const octave_value& fid); |
|
531 |
|
532 void do_clear (void); |
|
533 |
|
534 string_vector do_get_info (int fid) const; |
|
535 string_vector do_get_info (const octave_value& fid) const; |
|
536 |
|
537 string do_list_open_files (void) const; |
|
538 |
|
539 octave_value do_open_file_numbers (void) const; |
|
540 |
2609
|
541 int do_get_file_number (const octave_value& fid) const; |
2117
|
542 }; |
|
543 |
|
544 #endif |
|
545 |
|
546 /* |
|
547 ;;; Local Variables: *** |
|
548 ;;; mode: C++ *** |
|
549 ;;; End: *** |
|
550 */ |