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