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