Mercurial > hg > octave-nkf
annotate src/oct-stream.h @ 11570:57632dea2446
attempt better backward compatibility for Array constructors
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 19 Jan 2011 17:55:56 -0500 |
parents | fd0a3ac60b0e |
children | cda4aa780d58 |
rev | line source |
---|---|
2117 | 1 /* |
2 | |
11523 | 3 Copyright (C) 1996-2011 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
2117 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
2117 | 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 |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
31 #include <iosfwd> |
5765 | 32 #include <sstream> |
2877 | 33 #include <string> |
6757 | 34 #include <map> |
2117 | 35 |
36 #include "Array.h" | |
2317 | 37 #include "data-conv.h" |
3640 | 38 #include "lo-utils.h" |
2317 | 39 #include "mach-info.h" |
2117 | 40 |
3640 | 41 class |
6109 | 42 OCTINTERP_API |
2117 | 43 scanf_format_elt |
44 { | |
3640 | 45 public: |
46 | |
3483 | 47 enum special_conversion |
48 { | |
49 whitespace_conversion = 1, | |
50 literal_conversion = 2 | |
51 }; | |
52 | |
2215 | 53 scanf_format_elt (const char *txt = 0, int w = 0, bool d = false, |
10313 | 54 char typ = '\0', char mod = '\0', |
55 const std::string& ch_class = std::string ()) | |
3640 | 56 : text (strsave (txt)), width (w), discard (d), type (typ), |
57 modifier (mod), char_class (ch_class) { } | |
58 | |
59 scanf_format_elt (const scanf_format_elt& e) | |
60 : text (strsave (e.text)), width (e.width), discard (e.discard), | |
61 type (e.type), modifier (e.modifier), char_class (e.char_class) { } | |
2117 | 62 |
3640 | 63 scanf_format_elt& operator = (const scanf_format_elt& e) |
64 { | |
65 if (this != &e) | |
10313 | 66 { |
67 text = strsave (e.text); | |
68 width = e.width; | |
69 discard = e.discard; | |
70 type = e.type; | |
71 modifier = e.modifier; | |
72 char_class = e.char_class; | |
73 } | |
2117 | 74 |
3640 | 75 return *this; |
76 } | |
77 | |
78 ~scanf_format_elt (void) { delete [] text; } | |
79 | |
80 // The C-style format string. | |
2117 | 81 const char *text; |
3640 | 82 |
83 // The maximum field width. | |
2215 | 84 int width; |
3640 | 85 |
86 // TRUE if we are not storing the result of this conversion. | |
2117 | 87 bool discard; |
3640 | 88 |
89 // Type of conversion -- `d', `i', `o', `u', `x', `e', `f', `g', | |
90 // `c', `s', `p', `%', or `['. | |
2117 | 91 char type; |
3640 | 92 |
93 // A length modifier -- `h', `l', or `L'. | |
2117 | 94 char modifier; |
3640 | 95 |
96 // The class of characters in a `[' format. | |
3523 | 97 std::string char_class; |
2117 | 98 }; |
99 | |
100 class | |
6109 | 101 OCTINTERP_API |
2117 | 102 scanf_format_list |
103 { | |
104 public: | |
105 | |
3523 | 106 scanf_format_list (const std::string& fmt = std::string ()); |
2117 | 107 |
108 ~scanf_format_list (void); | |
109 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
110 octave_idx_type num_conversions (void) { return nconv; } |
2117 | 111 |
2215 | 112 // The length can be different than the number of conversions. |
113 // For example, "x %d y %d z" has 2 conversions but the length of | |
114 // the list is 3 because of the characters that appear after the | |
115 // last conversion. | |
116 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
117 octave_idx_type length (void) { return list.length (); } |
2215 | 118 |
2117 | 119 const scanf_format_elt *first (void) |
120 { | |
121 curr_idx = 0; | |
122 return current (); | |
123 } | |
124 | |
125 const scanf_format_elt *current (void) const | |
126 { return list.length () > 0 ? list.elem (curr_idx) : 0; } | |
127 | |
3640 | 128 const scanf_format_elt *next (bool cycle = true) |
2117 | 129 { |
130 curr_idx++; | |
3640 | 131 |
2117 | 132 if (curr_idx >= list.length ()) |
10313 | 133 { |
134 if (cycle) | |
135 curr_idx = 0; | |
136 else | |
137 return 0; | |
138 } | |
2117 | 139 return current (); |
140 } | |
141 | |
142 void printme (void) const; | |
143 | |
144 bool ok (void) const { return (nconv >= 0); } | |
145 | |
3145 | 146 operator bool () const { return ok (); } |
2117 | 147 |
148 bool all_character_conversions (void); | |
149 | |
150 bool all_numeric_conversions (void); | |
151 | |
152 private: | |
153 | |
3642 | 154 // Number of conversions specified by this format string, or -1 if |
2117 | 155 // 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
|
156 octave_idx_type nconv; |
2117 | 157 |
158 // 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
|
159 octave_idx_type curr_idx; |
2117 | 160 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
161 // FIXME -- maybe LIST should be a std::list object? |
2117 | 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 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
295 // FIXME -- maybe LIST should be a std::list object? |
2117 | 296 // List of format elements. |
297 Array<printf_format_elt*> list; | |
298 | |
299 // Temporary buffer. | |
5765 | 300 std::ostringstream *buf; |
2117 | 301 |
3640 | 302 void add_elt_to_list (int args, const std::string& flags, int fw, |
10313 | 303 int prec, char type, char modifier, |
304 octave_idx_type& num_elts); | |
3640 | 305 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
306 void process_conversion (const std::string& s, size_t& i, size_t n, |
10313 | 307 int& args, std::string& flags, int& fw, |
308 int& prec, char& modifier, char& type, | |
309 octave_idx_type& num_elts); | |
3640 | 310 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
311 void finish_conversion (const std::string& s, size_t& i, int args, |
10313 | 312 const std::string& flags, int fw, int prec, |
313 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
|
314 octave_idx_type& num_elts); |
2117 | 315 |
316 // No copying! | |
317 | |
318 printf_format_list (const printf_format_list&); | |
319 | |
320 printf_format_list& operator = (const printf_format_list&); | |
321 }; | |
322 | |
323 // Provide an interface for Octave streams. | |
324 | |
325 class | |
6109 | 326 OCTINTERP_API |
2117 | 327 octave_base_stream |
328 { | |
329 friend class octave_stream; | |
330 | |
331 public: | |
332 | |
3544 | 333 octave_base_stream (std::ios::openmode arg_md = std::ios::in|std::ios::out, |
10313 | 334 oct_mach_info::float_format ff |
335 = oct_mach_info::native_float_format ()) | |
3340 | 336 : count (0), md (arg_md), flt_fmt (ff), fail (false), open_state (true) |
337 { } | |
2117 | 338 |
339 virtual ~octave_base_stream (void) { } | |
340 | |
341 // The remaining functions are not specific to input or output only, | |
342 // and must be provided by the derived classes. | |
343 | |
344 // Position a stream at OFFSET relative to ORIGIN. | |
345 | |
4797 | 346 virtual int seek (long offset, int origin) = 0; |
2117 | 347 |
348 // Return current stream position. | |
349 | |
4797 | 350 virtual long tell (void) = 0; |
2117 | 351 |
3340 | 352 // Return TRUE if EOF has been reached on this stream. |
2117 | 353 |
354 virtual bool eof (void) const = 0; | |
355 | |
356 // The name of the file. | |
357 | |
3523 | 358 virtual std::string name (void) const = 0; |
2117 | 359 |
360 // If the derived class provides this function and it returns a | |
361 // pointer to a valid istream, scanf(), read(), getl(), and gets() | |
362 // will automatically work for this stream. | |
363 | |
3523 | 364 virtual std::istream *input_stream (void) { return 0; } |
2117 | 365 |
366 // If the derived class provides this function and it returns a | |
367 // pointer to a valid ostream, flush(), write(), and printf() will | |
368 // automatically work for this stream. | |
369 | |
3523 | 370 virtual std::ostream *output_stream (void) { return 0; } |
2117 | 371 |
3340 | 372 // Return TRUE if this stream is open. |
373 | |
374 bool is_open (void) const { return open_state; } | |
375 | |
3652 | 376 virtual void do_close (void) { } |
377 | |
378 void close (void) | |
379 { | |
380 if (is_open ()) | |
10313 | 381 { |
382 open_state = false; | |
383 do_close (); | |
384 } | |
3652 | 385 } |
3340 | 386 |
11007
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
387 virtual int file_number (void) const |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
388 { |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
389 // Kluge alert! |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
390 |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
391 if (name () == "stdin") |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
392 return 0; |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
393 else if (name () == "stdout") |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
394 return 1; |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
395 else if (name () == "stderr") |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
396 return 2; |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
397 else |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
398 return -1; |
ffe58182db89
src/oct-stream.h (octave_base_stream::file_number): return 0, 1, and 2 for stdin, stdout, and stderr, -1 otherwise
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
399 } |
3145 | 400 |
2117 | 401 bool ok (void) const { return ! fail; } |
402 | |
403 // Return current error message for this stream. | |
404 | |
3523 | 405 std::string error (bool clear, int& err_num); |
2117 | 406 |
407 protected: | |
408 | |
3340 | 409 int mode (void) const { return md; } |
2117 | 410 |
3340 | 411 oct_mach_info::float_format float_format (void) const { return flt_fmt; } |
2117 | 412 |
413 // Set current error state and set fail to TRUE. | |
414 | |
3523 | 415 void error (const std::string& msg); |
4468 | 416 void error (const std::string& who, const std::string& msg); |
2117 | 417 |
418 // Clear any error message and set fail to FALSE. | |
419 | |
420 void clear (void); | |
421 | |
4889 | 422 // Clear stream state. |
423 | |
424 void clearerr (void); | |
425 | |
2117 | 426 private: |
427 | |
3340 | 428 // A reference count. |
5275 | 429 octave_idx_type count; |
3340 | 430 |
2117 | 431 // The permission bits for the file. Should be some combination of |
3544 | 432 // std::ios::open_mode bits. |
2117 | 433 int md; |
434 | |
435 // Data format. | |
2317 | 436 oct_mach_info::float_format flt_fmt; |
2117 | 437 |
438 // TRUE if an error has occurred. | |
439 bool fail; | |
440 | |
3340 | 441 // TRUE if this stream is open. |
442 bool open_state; | |
443 | |
2117 | 444 // Should contain error message if fail is TRUE. |
3523 | 445 std::string errmsg; |
2117 | 446 |
447 // Functions that are defined for all input streams (input streams | |
448 // are those that define is). | |
449 | |
5275 | 450 std::string do_gets (octave_idx_type max_len, bool& err, bool strip_newline, |
10313 | 451 const std::string& who /* = "gets" */); |
2117 | 452 |
5275 | 453 std::string getl (octave_idx_type max_len, bool& err, const std::string& who /* = "getl" */); |
454 std::string gets (octave_idx_type max_len, bool& err, const std::string& who /* = "gets" */); | |
9701 | 455 long skipl (long count, bool& err, const std::string& who /* = "skipl" */); |
2117 | 456 |
5275 | 457 octave_value do_scanf (scanf_format_list& fmt_list, octave_idx_type nr, octave_idx_type nc, |
10313 | 458 bool one_elt_size_spec, octave_idx_type& count, |
459 const std::string& who /* = "scanf" */); | |
2117 | 460 |
4468 | 461 octave_value scanf (const std::string& fmt, const Array<double>& size, |
10313 | 462 octave_idx_type& count, const std::string& who /* = "scanf" */); |
2117 | 463 |
4468 | 464 bool do_oscanf (const scanf_format_elt *elt, octave_value&, |
10313 | 465 const std::string& who /* = "scanf" */); |
2117 | 466 |
4468 | 467 octave_value_list oscanf (const std::string& fmt, |
10313 | 468 const std::string& who /* = "scanf" */); |
2215 | 469 |
2117 | 470 // Functions that are defined for all output streams (output streams |
471 // are those that define os). | |
472 | |
473 int flush (void); | |
474 | |
4468 | 475 int do_printf (printf_format_list& fmt_list, const octave_value_list& args, |
10313 | 476 const std::string& who /* = "printf" */); |
2117 | 477 |
4468 | 478 int printf (const std::string& fmt, const octave_value_list& args, |
10313 | 479 const std::string& who /* = "printf" */); |
2117 | 480 |
4468 | 481 int puts (const std::string& s, const std::string& who /* = "puts" */); |
2117 | 482 |
483 // We can always do this in terms of seek(), so the derived class | |
484 // only has to provide that. | |
485 | |
4468 | 486 void invalid_operation (const std::string& who, const char *rw); |
2117 | 487 |
488 // No copying! | |
489 | |
490 octave_base_stream (const octave_base_stream&); | |
491 | |
492 octave_base_stream& operator = (const octave_base_stream&); | |
493 }; | |
494 | |
495 class | |
6109 | 496 OCTINTERP_API |
2117 | 497 octave_stream |
498 { | |
499 public: | |
500 | |
3340 | 501 octave_stream (octave_base_stream *bs = 0); |
502 | |
503 ~octave_stream (void); | |
2117 | 504 |
3340 | 505 octave_stream (const octave_stream&); |
506 | |
507 octave_stream& operator = (const octave_stream&); | |
2117 | 508 |
509 int flush (void); | |
510 | |
5275 | 511 std::string getl (octave_idx_type max_len, bool& err, const std::string& who /* = "getl" */); |
4468 | 512 std::string getl (const octave_value& max_len, bool& err, |
10313 | 513 const std::string& who /* = "getl" */); |
2117 | 514 |
5275 | 515 std::string gets (octave_idx_type max_len, bool& err, const std::string& who /* = "gets" */); |
4468 | 516 std::string gets (const octave_value& max_len, bool& err, |
10313 | 517 const std::string& who /* = "gets" */); |
2117 | 518 |
9701 | 519 long skipl (long count, bool& err, const std::string& who /* = "skipl" */); |
520 long skipl (const octave_value& count, bool& err, const std::string& who /* = "skipl" */); | |
521 | |
4797 | 522 int seek (long offset, int origin); |
2117 | 523 int seek (const octave_value& offset, const octave_value& origin); |
524 | |
4797 | 525 long tell (void); |
2117 | 526 |
527 int rewind (void); | |
528 | |
3340 | 529 bool is_open (void) const; |
530 | |
531 void close (void); | |
532 | |
5275 | 533 octave_value read (const Array<double>& size, octave_idx_type block_size, |
10313 | 534 oct_data_conv::data_type input_type, |
535 oct_data_conv::data_type output_type, | |
536 octave_idx_type skip, oct_mach_info::float_format flt_fmt, | |
537 octave_idx_type& count); | |
2117 | 538 |
5275 | 539 octave_idx_type write (const octave_value& data, octave_idx_type block_size, |
10313 | 540 oct_data_conv::data_type output_type, |
541 octave_idx_type skip, oct_mach_info::float_format flt_fmt); | |
4944 | 542 |
543 template <class T> | |
5275 | 544 octave_idx_type write (const Array<T>&, octave_idx_type block_size, |
10313 | 545 oct_data_conv::data_type output_type, |
546 octave_idx_type skip, oct_mach_info::float_format flt_fmt); | |
2117 | 547 |
4468 | 548 octave_value scanf (const std::string& fmt, const Array<double>& size, |
10313 | 549 octave_idx_type& count, const std::string& who /* = "scanf" */); |
2117 | 550 |
5279 | 551 octave_value scanf (const octave_value& fmt, const Array<double>& size, |
10313 | 552 octave_idx_type& count, const std::string& who /* = "scanf" */); |
5279 | 553 |
4468 | 554 octave_value_list oscanf (const std::string& fmt, |
10313 | 555 const std::string& who /* = "scanf" */); |
2215 | 556 |
5279 | 557 octave_value_list oscanf (const octave_value& fmt, |
10313 | 558 const std::string& who /* = "scanf" */); |
5279 | 559 |
4468 | 560 int printf (const std::string& fmt, const octave_value_list& args, |
10313 | 561 const std::string& who /* = "printf" */); |
2117 | 562 |
5279 | 563 int printf (const octave_value& fmt, const octave_value_list& args, |
10313 | 564 const std::string& who /* = "printf" */); |
5279 | 565 |
4468 | 566 int puts (const std::string& s, const std::string& who /* = "puts" */); |
567 int puts (const octave_value& s, const std::string& who /* = "puts" */); | |
2117 | 568 |
569 bool eof (void) const; | |
570 | |
3523 | 571 std::string error (bool clear, int& err_num); |
2117 | 572 |
3523 | 573 std::string error (bool clear = false) |
2117 | 574 { |
2435 | 575 int err_num; |
576 return error (clear, err_num); | |
2117 | 577 } |
578 | |
4799 | 579 // Set the error message and state. |
580 | |
581 void error (const std::string& msg) | |
582 { | |
583 if (rep) | |
10313 | 584 rep->error (msg); |
4799 | 585 } |
586 | |
587 void error (const char *msg) { error (std::string (msg)); } | |
588 | |
3148 | 589 int file_number (void) { return rep ? rep->file_number () : -1; } |
3145 | 590 |
3340 | 591 bool is_valid (void) const { return (rep != 0); } |
592 | |
2117 | 593 bool ok (void) const { return rep && rep->ok (); } |
594 | |
3145 | 595 operator bool () const { return ok (); } |
2117 | 596 |
3523 | 597 std::string name (void) const; |
2117 | 598 |
3340 | 599 int mode (void) const; |
2117 | 600 |
3340 | 601 oct_mach_info::float_format float_format (void) const; |
2117 | 602 |
3523 | 603 static std::string mode_as_string (int mode); |
2117 | 604 |
6757 | 605 std::istream *input_stream (void) |
606 { | |
607 return rep ? rep->input_stream () : 0; | |
608 } | |
2902 | 609 |
6757 | 610 std::ostream *output_stream (void) |
611 { | |
612 return rep ? rep->output_stream () : 0; | |
613 } | |
2902 | 614 |
4889 | 615 void clearerr (void) { if (rep) rep->clearerr (); } |
616 | |
2117 | 617 private: |
618 | |
619 // The actual representation of this stream. | |
620 octave_base_stream *rep; | |
621 | |
5659 | 622 bool stream_ok (bool clear = true) const |
623 { | |
624 bool retval = true; | |
625 | |
626 if (rep) | |
10313 | 627 { |
628 if (clear) | |
629 rep->clear (); | |
630 } | |
5659 | 631 else |
10313 | 632 retval = false; |
5659 | 633 |
634 return retval; | |
635 } | |
4944 | 636 |
637 void invalid_operation (const std::string& who, const char *rw) | |
638 { | |
639 if (rep) | |
10313 | 640 rep->invalid_operation (who, rw); |
4944 | 641 } |
2117 | 642 }; |
643 | |
644 class | |
6109 | 645 OCTINTERP_API |
2117 | 646 octave_stream_list |
647 { | |
648 protected: | |
649 | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
650 octave_stream_list (void) : list (), lookup_cache (list.end ()) { } |
2117 | 651 |
652 public: | |
653 | |
654 ~octave_stream_list (void) { } | |
655 | |
2926 | 656 static bool instance_ok (void); |
657 | |
6757 | 658 static int insert (octave_stream& os); |
2117 | 659 |
4468 | 660 static octave_stream |
661 lookup (int fid, const std::string& who = std::string ()); | |
662 | |
663 static octave_stream | |
664 lookup (const octave_value& fid, const std::string& who = std::string ()); | |
2117 | 665 |
3523 | 666 static int remove (int fid, const std::string& who = std::string ()); |
3341 | 667 static int remove (const octave_value& fid, |
10313 | 668 const std::string& who = std::string ()); |
2117 | 669 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
670 static void clear (bool flush = true); |
2117 | 671 |
672 static string_vector get_info (int fid); | |
673 static string_vector get_info (const octave_value& fid); | |
674 | |
3523 | 675 static std::string list_open_files (void); |
2117 | 676 |
677 static octave_value open_file_numbers (void); | |
678 | |
2609 | 679 static int get_file_number (const octave_value& fid); |
680 | |
2117 | 681 private: |
682 | |
6757 | 683 typedef std::map<int, octave_stream> ostrl_map; |
2117 | 684 |
6757 | 685 ostrl_map list; |
2117 | 686 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
687 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
|
688 |
2117 | 689 static octave_stream_list *instance; |
690 | |
6757 | 691 int do_insert (octave_stream& os); |
2117 | 692 |
3523 | 693 octave_stream do_lookup (int fid, const std::string& who = std::string ()) const; |
3341 | 694 octave_stream do_lookup (const octave_value& fid, |
10313 | 695 const std::string& who = std::string ()) const; |
2117 | 696 |
3523 | 697 int do_remove (int fid, const std::string& who = std::string ()); |
698 int do_remove (const octave_value& fid, const std::string& who = std::string ()); | |
2117 | 699 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
700 void do_clear (bool flush = true); |
2117 | 701 |
702 string_vector do_get_info (int fid) const; | |
703 string_vector do_get_info (const octave_value& fid) const; | |
704 | |
3523 | 705 std::string do_list_open_files (void) const; |
2117 | 706 |
707 octave_value do_open_file_numbers (void) const; | |
708 | |
2609 | 709 int do_get_file_number (const octave_value& fid) const; |
2117 | 710 }; |
711 | |
712 #endif |