Mercurial > hg > octave-avbm
annotate src/oct-stream.h @ 10996:72640afb02b9
mkoctfile.m: Change help text to be consistent with the shell version.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sun, 19 Sep 2010 12:25:31 -0400 |
parents | f3b65e1ae355 |
children | 594adb99a25e |
rev | line source |
---|---|
2117 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
8920 | 4 2005, 2006, 2007, 2009 John W. Eaton |
2117 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2117 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2117 | 21 |
22 */ | |
23 | |
24 #if !defined (octave_octave_stream_h) | |
25 #define octave_octave_stream_h 1 | |
26 | |
2877 | 27 class Matrix; |
28 class string_vector; | |
29 class octave_value; | |
30 class octave_value_list; | |
2117 | 31 |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
32 #include <iosfwd> |
5765 | 33 #include <sstream> |
2877 | 34 #include <string> |
6757 | 35 #include <map> |
2117 | 36 |
37 #include "Array.h" | |
2317 | 38 #include "data-conv.h" |
3640 | 39 #include "lo-utils.h" |
2317 | 40 #include "mach-info.h" |
2117 | 41 |
3640 | 42 class |
6109 | 43 OCTINTERP_API |
2117 | 44 scanf_format_elt |
45 { | |
3640 | 46 public: |
47 | |
3483 | 48 enum special_conversion |
49 { | |
50 whitespace_conversion = 1, | |
51 literal_conversion = 2 | |
52 }; | |
53 | |
2215 | 54 scanf_format_elt (const char *txt = 0, int w = 0, bool d = false, |
10313 | 55 char typ = '\0', char mod = '\0', |
56 const std::string& ch_class = std::string ()) | |
3640 | 57 : text (strsave (txt)), width (w), discard (d), type (typ), |
58 modifier (mod), char_class (ch_class) { } | |
59 | |
60 scanf_format_elt (const scanf_format_elt& e) | |
61 : text (strsave (e.text)), width (e.width), discard (e.discard), | |
62 type (e.type), modifier (e.modifier), char_class (e.char_class) { } | |
2117 | 63 |
3640 | 64 scanf_format_elt& operator = (const scanf_format_elt& e) |
65 { | |
66 if (this != &e) | |
10313 | 67 { |
68 text = strsave (e.text); | |
69 width = e.width; | |
70 discard = e.discard; | |
71 type = e.type; | |
72 modifier = e.modifier; | |
73 char_class = e.char_class; | |
74 } | |
2117 | 75 |
3640 | 76 return *this; |
77 } | |
78 | |
79 ~scanf_format_elt (void) { delete [] text; } | |
80 | |
81 // The C-style format string. | |
2117 | 82 const char *text; |
3640 | 83 |
84 // The maximum field width. | |
2215 | 85 int width; |
3640 | 86 |
87 // TRUE if we are not storing the result of this conversion. | |
2117 | 88 bool discard; |
3640 | 89 |
90 // Type of conversion -- `d', `i', `o', `u', `x', `e', `f', `g', | |
91 // `c', `s', `p', `%', or `['. | |
2117 | 92 char type; |
3640 | 93 |
94 // A length modifier -- `h', `l', or `L'. | |
2117 | 95 char modifier; |
3640 | 96 |
97 // The class of characters in a `[' format. | |
3523 | 98 std::string char_class; |
2117 | 99 }; |
100 | |
101 class | |
6109 | 102 OCTINTERP_API |
2117 | 103 scanf_format_list |
104 { | |
105 public: | |
106 | |
3523 | 107 scanf_format_list (const std::string& fmt = std::string ()); |
2117 | 108 |
109 ~scanf_format_list (void); | |
110 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
111 octave_idx_type num_conversions (void) { return nconv; } |
2117 | 112 |
2215 | 113 // The length can be different than the number of conversions. |
114 // For example, "x %d y %d z" has 2 conversions but the length of | |
115 // the list is 3 because of the characters that appear after the | |
116 // last conversion. | |
117 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
118 octave_idx_type length (void) { return list.length (); } |
2215 | 119 |
2117 | 120 const scanf_format_elt *first (void) |
121 { | |
122 curr_idx = 0; | |
123 return current (); | |
124 } | |
125 | |
126 const scanf_format_elt *current (void) const | |
127 { return list.length () > 0 ? list.elem (curr_idx) : 0; } | |
128 | |
3640 | 129 const scanf_format_elt *next (bool cycle = true) |
2117 | 130 { |
131 curr_idx++; | |
3640 | 132 |
2117 | 133 if (curr_idx >= list.length ()) |
10313 | 134 { |
135 if (cycle) | |
136 curr_idx = 0; | |
137 else | |
138 return 0; | |
139 } | |
2117 | 140 return current (); |
141 } | |
142 | |
143 void printme (void) const; | |
144 | |
145 bool ok (void) const { return (nconv >= 0); } | |
146 | |
3145 | 147 operator bool () const { return ok (); } |
2117 | 148 |
149 bool all_character_conversions (void); | |
150 | |
151 bool all_numeric_conversions (void); | |
152 | |
153 private: | |
154 | |
3642 | 155 // Number of conversions specified by this format string, or -1 if |
2117 | 156 // invalid conversions have been found. |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
157 octave_idx_type nconv; |
2117 | 158 |
159 // Index to current element; | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
160 octave_idx_type curr_idx; |
2117 | 161 |
162 // List of format elements. | |
163 Array<scanf_format_elt*> list; | |
164 | |
165 // Temporary buffer. | |
5765 | 166 std::ostringstream *buf; |
2117 | 167 |
2215 | 168 void add_elt_to_list (int width, bool discard, char type, char modifier, |
10313 | 169 octave_idx_type& num_elts, |
170 const std::string& char_class = std::string ()); | |
2117 | 171 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
172 void process_conversion (const std::string& s, size_t& i, size_t n, |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
173 int& width, bool& discard, char& type, |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
174 char& modifier, octave_idx_type& num_elts); |
2117 | 175 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
176 int finish_conversion (const std::string& s, size_t& i, size_t n, |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
177 int& width, bool discard, char& type, |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
178 char modifier, octave_idx_type& num_elts); |
2117 | 179 // No copying! |
180 | |
181 scanf_format_list (const scanf_format_list&); | |
182 | |
183 scanf_format_list& operator = (const scanf_format_list&); | |
184 }; | |
185 | |
3640 | 186 class |
2117 | 187 printf_format_elt |
188 { | |
3640 | 189 public: |
190 | |
191 printf_format_elt (const char *txt = 0, int n = 0, int w = 0, | |
10313 | 192 int p = 0, const std::string& f = std::string (), |
193 char typ = '\0', char mod = '\0') | |
3640 | 194 : text (strsave (txt)), args (n), fw (w), prec (p), flags (f), |
195 type (typ), modifier (mod) { } | |
196 | |
197 printf_format_elt (const printf_format_elt& e) | |
198 : text (strsave (e.text)), args (e.args), fw (e.fw), prec (e.prec), | |
199 flags (e.flags), type (e.type), modifier (e.modifier) { } | |
200 | |
201 printf_format_elt& operator = (const printf_format_elt& e) | |
202 { | |
203 if (this != &e) | |
10313 | 204 { |
205 text = strsave (e.text); | |
206 args = e.args; | |
207 fw = e.fw; | |
208 prec = e.prec; | |
209 flags = e.flags; | |
210 type = e.type; | |
211 modifier = e.modifier; | |
212 } | |
2117 | 213 |
3640 | 214 return *this; |
215 } | |
2117 | 216 |
3640 | 217 ~printf_format_elt (void) { delete [] text; } |
218 | |
219 // The C-style format string. | |
2117 | 220 const char *text; |
3640 | 221 |
222 // How many args do we expect to consume? | |
2117 | 223 int args; |
3640 | 224 |
225 // Field width. | |
226 int fw; | |
227 | |
228 // Precision. | |
229 int prec; | |
230 | |
231 // Flags -- `-', `+', ` ', `0', or `#'. | |
3642 | 232 std::string flags; |
3640 | 233 |
234 // Type of conversion -- `d', `i', `o', `x', `X', `u', `c', `s', | |
235 // `f', `e', `E', `g', `G', `p', or `%' | |
2117 | 236 char type; |
3640 | 237 |
238 // A length modifier -- `h', `l', or `L'. | |
2117 | 239 char modifier; |
240 }; | |
241 | |
242 class | |
6109 | 243 OCTINTERP_API |
2117 | 244 printf_format_list |
245 { | |
246 public: | |
247 | |
3523 | 248 printf_format_list (const std::string& fmt = std::string ()); |
2117 | 249 |
250 ~printf_format_list (void); | |
251 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
252 octave_idx_type num_conversions (void) { return nconv; } |
2117 | 253 |
254 const printf_format_elt *first (void) | |
255 { | |
256 curr_idx = 0; | |
257 return current (); | |
258 } | |
259 | |
260 const printf_format_elt *current (void) const | |
261 { return list.length () > 0 ? list.elem (curr_idx) : 0; } | |
262 | |
3640 | 263 const printf_format_elt *next (bool cycle = true) |
2117 | 264 { |
265 curr_idx++; | |
3640 | 266 |
2117 | 267 if (curr_idx >= list.length ()) |
10313 | 268 { |
269 if (cycle) | |
270 curr_idx = 0; | |
271 else | |
272 return 0; | |
273 } | |
3640 | 274 |
2117 | 275 return current (); |
276 } | |
277 | |
3640 | 278 bool last_elt_p (void) { return (curr_idx + 1 == list.length ()); } |
279 | |
2117 | 280 void printme (void) const; |
281 | |
282 bool ok (void) const { return (nconv >= 0); } | |
283 | |
3145 | 284 operator bool () const { return ok (); } |
2117 | 285 |
286 private: | |
287 | |
3642 | 288 // Number of conversions specified by this format string, or -1 if |
2117 | 289 // invalid conversions have been found. |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
290 octave_idx_type nconv; |
2117 | 291 |
292 // Index to current element; | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
293 octave_idx_type curr_idx; |
2117 | 294 |
295 // List of format elements. | |
296 Array<printf_format_elt*> list; | |
297 | |
298 // Temporary buffer. | |
5765 | 299 std::ostringstream *buf; |
2117 | 300 |
3640 | 301 void add_elt_to_list (int args, const std::string& flags, int fw, |
10313 | 302 int prec, char type, char modifier, |
303 octave_idx_type& num_elts); | |
3640 | 304 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
305 void process_conversion (const std::string& s, size_t& i, size_t n, |
10313 | 306 int& args, std::string& flags, int& fw, |
307 int& prec, char& modifier, char& type, | |
308 octave_idx_type& num_elts); | |
3640 | 309 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
310 void finish_conversion (const std::string& s, size_t& i, int args, |
10313 | 311 const std::string& flags, int fw, int prec, |
312 char modifier, char& type, | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
313 octave_idx_type& num_elts); |
2117 | 314 |
315 // No copying! | |
316 | |
317 printf_format_list (const printf_format_list&); | |
318 | |
319 printf_format_list& operator = (const printf_format_list&); | |
320 }; | |
321 | |
322 // Provide an interface for Octave streams. | |
323 | |
324 class | |
6109 | 325 OCTINTERP_API |
2117 | 326 octave_base_stream |
327 { | |
328 friend class octave_stream; | |
329 | |
330 public: | |
331 | |
3544 | 332 octave_base_stream (std::ios::openmode arg_md = std::ios::in|std::ios::out, |
10313 | 333 oct_mach_info::float_format ff |
334 = oct_mach_info::native_float_format ()) | |
3340 | 335 : count (0), md (arg_md), flt_fmt (ff), fail (false), open_state (true) |
336 { } | |
2117 | 337 |
338 virtual ~octave_base_stream (void) { } | |
339 | |
340 // The remaining functions are not specific to input or output only, | |
341 // and must be provided by the derived classes. | |
342 | |
343 // Position a stream at OFFSET relative to ORIGIN. | |
344 | |
4797 | 345 virtual int seek (long offset, int origin) = 0; |
2117 | 346 |
347 // Return current stream position. | |
348 | |
4797 | 349 virtual long tell (void) = 0; |
2117 | 350 |
3340 | 351 // Return TRUE if EOF has been reached on this stream. |
2117 | 352 |
353 virtual bool eof (void) const = 0; | |
354 | |
355 // The name of the file. | |
356 | |
3523 | 357 virtual std::string name (void) const = 0; |
2117 | 358 |
359 // If the derived class provides this function and it returns a | |
360 // pointer to a valid istream, scanf(), read(), getl(), and gets() | |
361 // will automatically work for this stream. | |
362 | |
3523 | 363 virtual std::istream *input_stream (void) { return 0; } |
2117 | 364 |
365 // If the derived class provides this function and it returns a | |
366 // pointer to a valid ostream, flush(), write(), and printf() will | |
367 // automatically work for this stream. | |
368 | |
3523 | 369 virtual std::ostream *output_stream (void) { return 0; } |
2117 | 370 |
3340 | 371 // Return TRUE if this stream is open. |
372 | |
373 bool is_open (void) const { return open_state; } | |
374 | |
3652 | 375 virtual void do_close (void) { } |
376 | |
377 void close (void) | |
378 { | |
379 if (is_open ()) | |
10313 | 380 { |
381 open_state = false; | |
382 do_close (); | |
383 } | |
3652 | 384 } |
3340 | 385 |
3148 | 386 int file_number (void); |
3145 | 387 |
2117 | 388 bool ok (void) const { return ! fail; } |
389 | |
390 // Return current error message for this stream. | |
391 | |
3523 | 392 std::string error (bool clear, int& err_num); |
2117 | 393 |
394 protected: | |
395 | |
3340 | 396 int mode (void) const { return md; } |
2117 | 397 |
3340 | 398 oct_mach_info::float_format float_format (void) const { return flt_fmt; } |
2117 | 399 |
400 // Set current error state and set fail to TRUE. | |
401 | |
3523 | 402 void error (const std::string& msg); |
4468 | 403 void error (const std::string& who, const std::string& msg); |
2117 | 404 |
405 // Clear any error message and set fail to FALSE. | |
406 | |
407 void clear (void); | |
408 | |
4889 | 409 // Clear stream state. |
410 | |
411 void clearerr (void); | |
412 | |
2117 | 413 private: |
414 | |
3340 | 415 // A reference count. |
5275 | 416 octave_idx_type count; |
3340 | 417 |
2117 | 418 // The permission bits for the file. Should be some combination of |
3544 | 419 // std::ios::open_mode bits. |
2117 | 420 int md; |
421 | |
422 // Data format. | |
2317 | 423 oct_mach_info::float_format flt_fmt; |
2117 | 424 |
425 // TRUE if an error has occurred. | |
426 bool fail; | |
427 | |
3340 | 428 // TRUE if this stream is open. |
429 bool open_state; | |
430 | |
2117 | 431 // Should contain error message if fail is TRUE. |
3523 | 432 std::string errmsg; |
2117 | 433 |
434 // Functions that are defined for all input streams (input streams | |
435 // are those that define is). | |
436 | |
5275 | 437 std::string do_gets (octave_idx_type max_len, bool& err, bool strip_newline, |
10313 | 438 const std::string& who /* = "gets" */); |
2117 | 439 |
5275 | 440 std::string getl (octave_idx_type max_len, bool& err, const std::string& who /* = "getl" */); |
441 std::string gets (octave_idx_type max_len, bool& err, const std::string& who /* = "gets" */); | |
9701 | 442 long skipl (long count, bool& err, const std::string& who /* = "skipl" */); |
2117 | 443 |
5275 | 444 octave_value do_scanf (scanf_format_list& fmt_list, octave_idx_type nr, octave_idx_type nc, |
10313 | 445 bool one_elt_size_spec, octave_idx_type& count, |
446 const std::string& who /* = "scanf" */); | |
2117 | 447 |
4468 | 448 octave_value scanf (const std::string& fmt, const Array<double>& size, |
10313 | 449 octave_idx_type& count, const std::string& who /* = "scanf" */); |
2117 | 450 |
4468 | 451 bool do_oscanf (const scanf_format_elt *elt, octave_value&, |
10313 | 452 const std::string& who /* = "scanf" */); |
2117 | 453 |
4468 | 454 octave_value_list oscanf (const std::string& fmt, |
10313 | 455 const std::string& who /* = "scanf" */); |
2215 | 456 |
2117 | 457 // Functions that are defined for all output streams (output streams |
458 // are those that define os). | |
459 | |
460 int flush (void); | |
461 | |
4468 | 462 int do_printf (printf_format_list& fmt_list, const octave_value_list& args, |
10313 | 463 const std::string& who /* = "printf" */); |
2117 | 464 |
4468 | 465 int printf (const std::string& fmt, const octave_value_list& args, |
10313 | 466 const std::string& who /* = "printf" */); |
2117 | 467 |
4468 | 468 int puts (const std::string& s, const std::string& who /* = "puts" */); |
2117 | 469 |
470 // We can always do this in terms of seek(), so the derived class | |
471 // only has to provide that. | |
472 | |
4468 | 473 void invalid_operation (const std::string& who, const char *rw); |
2117 | 474 |
475 // No copying! | |
476 | |
477 octave_base_stream (const octave_base_stream&); | |
478 | |
479 octave_base_stream& operator = (const octave_base_stream&); | |
480 }; | |
481 | |
482 class | |
6109 | 483 OCTINTERP_API |
2117 | 484 octave_stream |
485 { | |
486 public: | |
487 | |
3340 | 488 octave_stream (octave_base_stream *bs = 0); |
489 | |
490 ~octave_stream (void); | |
2117 | 491 |
3340 | 492 octave_stream (const octave_stream&); |
493 | |
494 octave_stream& operator = (const octave_stream&); | |
2117 | 495 |
496 int flush (void); | |
497 | |
5275 | 498 std::string getl (octave_idx_type max_len, bool& err, const std::string& who /* = "getl" */); |
4468 | 499 std::string getl (const octave_value& max_len, bool& err, |
10313 | 500 const std::string& who /* = "getl" */); |
2117 | 501 |
5275 | 502 std::string gets (octave_idx_type max_len, bool& err, const std::string& who /* = "gets" */); |
4468 | 503 std::string gets (const octave_value& max_len, bool& err, |
10313 | 504 const std::string& who /* = "gets" */); |
2117 | 505 |
9701 | 506 long skipl (long count, bool& err, const std::string& who /* = "skipl" */); |
507 long skipl (const octave_value& count, bool& err, const std::string& who /* = "skipl" */); | |
508 | |
4797 | 509 int seek (long offset, int origin); |
2117 | 510 int seek (const octave_value& offset, const octave_value& origin); |
511 | |
4797 | 512 long tell (void); |
2117 | 513 |
514 int rewind (void); | |
515 | |
3340 | 516 bool is_open (void) const; |
517 | |
518 void close (void); | |
519 | |
5275 | 520 octave_value read (const Array<double>& size, octave_idx_type block_size, |
10313 | 521 oct_data_conv::data_type input_type, |
522 oct_data_conv::data_type output_type, | |
523 octave_idx_type skip, oct_mach_info::float_format flt_fmt, | |
524 octave_idx_type& count); | |
2117 | 525 |
5275 | 526 octave_idx_type write (const octave_value& data, octave_idx_type block_size, |
10313 | 527 oct_data_conv::data_type output_type, |
528 octave_idx_type skip, oct_mach_info::float_format flt_fmt); | |
4944 | 529 |
530 template <class T> | |
5275 | 531 octave_idx_type write (const Array<T>&, octave_idx_type block_size, |
10313 | 532 oct_data_conv::data_type output_type, |
533 octave_idx_type skip, oct_mach_info::float_format flt_fmt); | |
2117 | 534 |
4468 | 535 octave_value scanf (const std::string& fmt, const Array<double>& size, |
10313 | 536 octave_idx_type& count, const std::string& who /* = "scanf" */); |
2117 | 537 |
5279 | 538 octave_value scanf (const octave_value& fmt, const Array<double>& size, |
10313 | 539 octave_idx_type& count, const std::string& who /* = "scanf" */); |
5279 | 540 |
4468 | 541 octave_value_list oscanf (const std::string& fmt, |
10313 | 542 const std::string& who /* = "scanf" */); |
2215 | 543 |
5279 | 544 octave_value_list oscanf (const octave_value& fmt, |
10313 | 545 const std::string& who /* = "scanf" */); |
5279 | 546 |
4468 | 547 int printf (const std::string& fmt, const octave_value_list& args, |
10313 | 548 const std::string& who /* = "printf" */); |
2117 | 549 |
5279 | 550 int printf (const octave_value& fmt, const octave_value_list& args, |
10313 | 551 const std::string& who /* = "printf" */); |
5279 | 552 |
4468 | 553 int puts (const std::string& s, const std::string& who /* = "puts" */); |
554 int puts (const octave_value& s, const std::string& who /* = "puts" */); | |
2117 | 555 |
556 bool eof (void) const; | |
557 | |
3523 | 558 std::string error (bool clear, int& err_num); |
2117 | 559 |
3523 | 560 std::string error (bool clear = false) |
2117 | 561 { |
2435 | 562 int err_num; |
563 return error (clear, err_num); | |
2117 | 564 } |
565 | |
4799 | 566 // Set the error message and state. |
567 | |
568 void error (const std::string& msg) | |
569 { | |
570 if (rep) | |
10313 | 571 rep->error (msg); |
4799 | 572 } |
573 | |
574 void error (const char *msg) { error (std::string (msg)); } | |
575 | |
3148 | 576 int file_number (void) { return rep ? rep->file_number () : -1; } |
3145 | 577 |
3340 | 578 bool is_valid (void) const { return (rep != 0); } |
579 | |
2117 | 580 bool ok (void) const { return rep && rep->ok (); } |
581 | |
3145 | 582 operator bool () const { return ok (); } |
2117 | 583 |
3523 | 584 std::string name (void) const; |
2117 | 585 |
3340 | 586 int mode (void) const; |
2117 | 587 |
3340 | 588 oct_mach_info::float_format float_format (void) const; |
2117 | 589 |
3523 | 590 static std::string mode_as_string (int mode); |
2117 | 591 |
6757 | 592 std::istream *input_stream (void) |
593 { | |
594 return rep ? rep->input_stream () : 0; | |
595 } | |
2902 | 596 |
6757 | 597 std::ostream *output_stream (void) |
598 { | |
599 return rep ? rep->output_stream () : 0; | |
600 } | |
2902 | 601 |
4889 | 602 void clearerr (void) { if (rep) rep->clearerr (); } |
603 | |
2117 | 604 private: |
605 | |
606 // The actual representation of this stream. | |
607 octave_base_stream *rep; | |
608 | |
5659 | 609 bool stream_ok (bool clear = true) const |
610 { | |
611 bool retval = true; | |
612 | |
613 if (rep) | |
10313 | 614 { |
615 if (clear) | |
616 rep->clear (); | |
617 } | |
5659 | 618 else |
10313 | 619 retval = false; |
5659 | 620 |
621 return retval; | |
622 } | |
4944 | 623 |
624 void invalid_operation (const std::string& who, const char *rw) | |
625 { | |
626 if (rep) | |
10313 | 627 rep->invalid_operation (who, rw); |
4944 | 628 } |
2117 | 629 }; |
630 | |
631 class | |
6109 | 632 OCTINTERP_API |
2117 | 633 octave_stream_list |
634 { | |
635 protected: | |
636 | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
637 octave_stream_list (void) : list (), lookup_cache (list.end ()) { } |
2117 | 638 |
639 public: | |
640 | |
641 ~octave_stream_list (void) { } | |
642 | |
2926 | 643 static bool instance_ok (void); |
644 | |
6757 | 645 static int insert (octave_stream& os); |
2117 | 646 |
4468 | 647 static octave_stream |
648 lookup (int fid, const std::string& who = std::string ()); | |
649 | |
650 static octave_stream | |
651 lookup (const octave_value& fid, const std::string& who = std::string ()); | |
2117 | 652 |
3523 | 653 static int remove (int fid, const std::string& who = std::string ()); |
3341 | 654 static int remove (const octave_value& fid, |
10313 | 655 const std::string& who = std::string ()); |
2117 | 656 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
657 static void clear (bool flush = true); |
2117 | 658 |
659 static string_vector get_info (int fid); | |
660 static string_vector get_info (const octave_value& fid); | |
661 | |
3523 | 662 static std::string list_open_files (void); |
2117 | 663 |
664 static octave_value open_file_numbers (void); | |
665 | |
2609 | 666 static int get_file_number (const octave_value& fid); |
667 | |
2117 | 668 private: |
669 | |
6757 | 670 typedef std::map<int, octave_stream> ostrl_map; |
2117 | 671 |
6757 | 672 ostrl_map list; |
2117 | 673 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
674 mutable ostrl_map::const_iterator lookup_cache; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
675 |
2117 | 676 static octave_stream_list *instance; |
677 | |
6757 | 678 int do_insert (octave_stream& os); |
2117 | 679 |
3523 | 680 octave_stream do_lookup (int fid, const std::string& who = std::string ()) const; |
3341 | 681 octave_stream do_lookup (const octave_value& fid, |
10313 | 682 const std::string& who = std::string ()) const; |
2117 | 683 |
3523 | 684 int do_remove (int fid, const std::string& who = std::string ()); |
685 int do_remove (const octave_value& fid, const std::string& who = std::string ()); | |
2117 | 686 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
687 void do_clear (bool flush = true); |
2117 | 688 |
689 string_vector do_get_info (int fid) const; | |
690 string_vector do_get_info (const octave_value& fid) const; | |
691 | |
3523 | 692 std::string do_list_open_files (void) const; |
2117 | 693 |
694 octave_value do_open_file_numbers (void) const; | |
695 | |
2609 | 696 int do_get_file_number (const octave_value& fid) const; |
2117 | 697 }; |
698 | |
699 #endif |