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) |
3340
|
225 : count (0), md (arg_md), flt_fmt (ff), fail (false), open_state (true) |
|
226 { } |
2117
|
227 |
|
228 virtual ~octave_base_stream (void) { } |
|
229 |
|
230 // The remaining functions are not specific to input or output only, |
|
231 // and must be provided by the derived classes. |
|
232 |
|
233 // Position a stream at OFFSET relative to ORIGIN. |
|
234 |
2610
|
235 virtual int seek (streamoff offset, ios::seek_dir origin) = 0; |
2117
|
236 |
|
237 // Return current stream position. |
|
238 |
|
239 virtual long tell (void) const = 0; |
|
240 |
3340
|
241 // Return TRUE if EOF has been reached on this stream. |
2117
|
242 |
|
243 virtual bool eof (void) const = 0; |
|
244 |
|
245 // The name of the file. |
|
246 |
3340
|
247 virtual string name (void) const = 0; |
2117
|
248 |
|
249 // If the derived class provides this function and it returns a |
|
250 // pointer to a valid istream, scanf(), read(), getl(), and gets() |
|
251 // will automatically work for this stream. |
|
252 |
|
253 virtual istream *input_stream (void) { return 0; } |
|
254 |
|
255 // If the derived class provides this function and it returns a |
|
256 // pointer to a valid ostream, flush(), write(), and printf() will |
|
257 // automatically work for this stream. |
|
258 |
|
259 virtual ostream *output_stream (void) { return 0; } |
|
260 |
3340
|
261 // Return TRUE if this stream is open. |
|
262 |
|
263 bool is_open (void) const { return open_state; } |
|
264 |
|
265 void close (void) { open_state = false; } |
|
266 |
3148
|
267 int file_number (void); |
3145
|
268 |
2117
|
269 bool ok (void) const { return ! fail; } |
|
270 |
|
271 // Return current error message for this stream. |
|
272 |
2435
|
273 string error (bool clear, int& err_num); |
2117
|
274 |
|
275 protected: |
|
276 |
3340
|
277 int mode (void) const { return md; } |
2117
|
278 |
3340
|
279 oct_mach_info::float_format float_format (void) const { return flt_fmt; } |
2117
|
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 |
3340
|
291 // A reference count. |
|
292 int count; |
|
293 |
2117
|
294 // The permission bits for the file. Should be some combination of |
|
295 // ios::open_mode bits. |
|
296 int md; |
|
297 |
|
298 // Data format. |
2317
|
299 oct_mach_info::float_format flt_fmt; |
2117
|
300 |
|
301 // TRUE if an error has occurred. |
|
302 bool fail; |
|
303 |
3340
|
304 // TRUE if this stream is open. |
|
305 bool open_state; |
|
306 |
2117
|
307 // Should contain error message if fail is TRUE. |
|
308 string errmsg; |
|
309 |
|
310 // Functions that are defined for all input streams (input streams |
|
311 // are those that define is). |
|
312 |
2317
|
313 string do_gets (int max_len, bool& err, bool strip_newline, |
|
314 const char *fcn); |
2117
|
315 |
|
316 string getl (int max_len, bool& err); |
|
317 string gets (int max_len, bool& err); |
|
318 |
2317
|
319 octave_value do_read (int nr, int nc, oct_data_conv::data_type dt, |
|
320 int skip, oct_mach_info::float_format flt_fmt, |
|
321 int& count); |
2117
|
322 |
2317
|
323 octave_value read (const Matrix& size, oct_data_conv::data_type dt, |
|
324 int skip, oct_mach_info::float_format flt_fmt, |
|
325 int& count); |
2117
|
326 |
|
327 octave_value do_char_scanf (scanf_format_list& fmt_list, |
|
328 int nr, int nc, int& count); |
|
329 |
|
330 octave_value do_real_scanf (scanf_format_list& fmt_list, |
|
331 int nr, int nc, int& count); |
|
332 |
|
333 octave_value do_scanf (scanf_format_list& fmt_list, int nr, int nc, |
3268
|
334 bool one_elt_size_spec, int& count); |
2117
|
335 |
|
336 octave_value scanf (const string& fmt, const Matrix& size, int& count); |
|
337 |
2712
|
338 bool do_oscanf (const scanf_format_elt *elt, octave_value&); |
2215
|
339 |
|
340 octave_value_list oscanf (const string& fmt); |
|
341 |
2117
|
342 // Functions that are defined for all output streams (output streams |
|
343 // are those that define os). |
|
344 |
|
345 int flush (void); |
|
346 |
2317
|
347 int do_write (const Matrix& m, oct_data_conv::data_type dt, int skip, |
|
348 oct_mach_info::float_format flt_fmt); |
2117
|
349 |
2317
|
350 int write (const octave_value& data, oct_data_conv::data_type dt, |
|
351 int skip, oct_mach_info::float_format flt_fmt); |
2117
|
352 |
|
353 int do_printf (printf_format_list& fmt_list, const octave_value_list& args); |
|
354 |
|
355 int printf (const string& fmt, const octave_value_list& args); |
|
356 |
|
357 int puts (const string& s); |
|
358 |
|
359 // We can always do this in terms of seek(), so the derived class |
|
360 // only has to provide that. |
|
361 |
|
362 int rewind (void); |
|
363 |
|
364 void invalid_operation (const char *op, const char *rw); |
|
365 |
|
366 // No copying! |
|
367 |
|
368 octave_base_stream (const octave_base_stream&); |
|
369 |
|
370 octave_base_stream& operator = (const octave_base_stream&); |
|
371 }; |
|
372 |
|
373 class |
|
374 octave_stream |
|
375 { |
|
376 public: |
|
377 |
3340
|
378 octave_stream (octave_base_stream *bs = 0); |
|
379 |
|
380 ~octave_stream (void); |
2117
|
381 |
3340
|
382 octave_stream (const octave_stream&); |
|
383 |
|
384 octave_stream& operator = (const octave_stream&); |
2117
|
385 |
|
386 int flush (void); |
|
387 |
|
388 string getl (int max_len, bool& err); |
|
389 string getl (const octave_value& max_len, bool& err); |
|
390 |
|
391 string gets (int max_len, bool& err); |
|
392 string gets (const octave_value& max_len, bool& err); |
|
393 |
2610
|
394 int seek (streamoff offset, ios::seek_dir origin); |
2117
|
395 int seek (const octave_value& offset, const octave_value& origin); |
|
396 |
|
397 long tell (void) const; |
|
398 |
|
399 int rewind (void); |
|
400 |
3340
|
401 bool is_open (void) const; |
|
402 |
|
403 void close (void); |
|
404 |
2317
|
405 octave_value read (const Matrix& size, oct_data_conv::data_type dt, |
|
406 int skip, oct_mach_info::float_format flt_fmt, |
2117
|
407 int& count); |
|
408 |
2317
|
409 int write (const octave_value& data, oct_data_conv::data_type dt, |
|
410 int skip, oct_mach_info::float_format flt_fmt); |
2117
|
411 |
|
412 octave_value scanf (const string& fmt, const Matrix& size, int& count); |
|
413 |
2215
|
414 octave_value_list oscanf (const string& fmt); |
|
415 |
2117
|
416 int printf (const string& fmt, const octave_value_list& args); |
|
417 |
|
418 int puts (const string& s); |
|
419 int puts (const octave_value& s); |
|
420 |
|
421 bool eof (void) const; |
|
422 |
2435
|
423 string error (bool clear, int& err_num); |
2117
|
424 |
|
425 string error (bool clear = false) |
|
426 { |
2435
|
427 int err_num; |
|
428 return error (clear, err_num); |
2117
|
429 } |
|
430 |
3148
|
431 int file_number (void) { return rep ? rep->file_number () : -1; } |
3145
|
432 |
3340
|
433 bool is_valid (void) const { return (rep != 0); } |
|
434 |
2117
|
435 bool ok (void) const { return rep && rep->ok (); } |
|
436 |
3145
|
437 operator bool () const { return ok (); } |
2117
|
438 |
3340
|
439 string name (void) const; |
2117
|
440 |
3340
|
441 int mode (void) const; |
2117
|
442 |
3340
|
443 oct_mach_info::float_format float_format (void) const; |
2117
|
444 |
|
445 static string mode_as_string (int mode); |
|
446 |
3340
|
447 istream *input_stream (void) { return rep ? rep->input_stream () : 0; } |
2902
|
448 |
3340
|
449 ostream *output_stream (void) { return rep ? rep->output_stream () : 0; } |
2902
|
450 |
2117
|
451 private: |
|
452 |
|
453 // The actual representation of this stream. |
|
454 octave_base_stream *rep; |
|
455 |
|
456 void invalid_stream_error (const char *op) const; |
|
457 |
|
458 bool stream_ok (const char *op, bool clear = true) const |
|
459 { |
|
460 bool retval = true; |
|
461 |
|
462 if (rep) |
|
463 { |
|
464 if (clear) |
|
465 rep->clear (); |
|
466 } |
|
467 else |
|
468 { |
|
469 retval = false; |
|
470 invalid_stream_error (op); |
|
471 } |
|
472 |
|
473 return retval; |
|
474 } |
|
475 |
|
476 void error (const string& msg) |
|
477 { |
|
478 if (rep) |
|
479 rep->error (msg); |
|
480 } |
|
481 }; |
|
482 |
|
483 class |
|
484 octave_stream_list |
|
485 { |
|
486 protected: |
|
487 |
|
488 octave_stream_list (void) : list (32), curr_len (0) { } |
|
489 |
|
490 public: |
|
491 |
|
492 ~octave_stream_list (void) { } |
|
493 |
2926
|
494 static bool instance_ok (void); |
|
495 |
3340
|
496 static octave_value insert (const octave_stream& os); |
2117
|
497 |
3340
|
498 static octave_stream lookup (int fid); |
|
499 static octave_stream lookup (const octave_value& fid); |
2117
|
500 |
|
501 static int remove (int fid); |
|
502 static int remove (const octave_value& fid); |
|
503 |
|
504 static void clear (void); |
|
505 |
|
506 static string_vector get_info (int fid); |
|
507 static string_vector get_info (const octave_value& fid); |
|
508 |
|
509 static string list_open_files (void); |
|
510 |
|
511 static octave_value open_file_numbers (void); |
|
512 |
2609
|
513 static int get_file_number (const octave_value& fid); |
|
514 |
2117
|
515 private: |
|
516 |
3340
|
517 Array<octave_stream> list; |
2117
|
518 |
|
519 int curr_len; |
|
520 |
|
521 static octave_stream_list *instance; |
|
522 |
3340
|
523 octave_value do_insert (const octave_stream& os); |
2117
|
524 |
3340
|
525 octave_stream do_lookup (int fid) const; |
|
526 octave_stream do_lookup (const octave_value& fid) const; |
2117
|
527 |
|
528 int do_remove (int fid); |
|
529 int do_remove (const octave_value& fid); |
|
530 |
|
531 void do_clear (void); |
|
532 |
|
533 string_vector do_get_info (int fid) const; |
|
534 string_vector do_get_info (const octave_value& fid) const; |
|
535 |
|
536 string do_list_open_files (void) const; |
|
537 |
|
538 octave_value do_open_file_numbers (void) const; |
|
539 |
2609
|
540 int do_get_file_number (const octave_value& fid) const; |
2117
|
541 }; |
|
542 |
|
543 #endif |
|
544 |
|
545 /* |
|
546 ;;; Local Variables: *** |
|
547 ;;; mode: C++ *** |
|
548 ;;; End: *** |
|
549 */ |