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