Mercurial > hg > octave-nkf
annotate src/oct-stream.h @ 11586:12df7854fa7c
strip trailing whitespace from source files
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:24:59 -0500 |
parents | cda4aa780d58 |
children | 7dd7cccf0757 |
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, |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
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; } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
218 |
3640 | 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); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
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, | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
309 octave_idx_type& num_elts); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
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 ()) | |
11584
cda4aa780d58
Another round of initialising members in the constructor initialisation list
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11570
diff
changeset
|
336 : count (0), md (arg_md), flt_fmt (ff), fail (false), open_state (true), |
cda4aa780d58
Another round of initialising members in the constructor initialisation list
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
11570
diff
changeset
|
337 errmsg () |
3340 | 338 { } |
2117 | 339 |
340 virtual ~octave_base_stream (void) { } | |
341 | |
342 // The remaining functions are not specific to input or output only, | |
343 // and must be provided by the derived classes. | |
344 | |
345 // Position a stream at OFFSET relative to ORIGIN. | |
346 | |
4797 | 347 virtual int seek (long offset, int origin) = 0; |
2117 | 348 |
349 // Return current stream position. | |
350 | |
4797 | 351 virtual long tell (void) = 0; |
2117 | 352 |
3340 | 353 // Return TRUE if EOF has been reached on this stream. |
2117 | 354 |
355 virtual bool eof (void) const = 0; | |
356 | |
357 // The name of the file. | |
358 | |
3523 | 359 virtual std::string name (void) const = 0; |
2117 | 360 |
361 // If the derived class provides this function and it returns a | |
362 // pointer to a valid istream, scanf(), read(), getl(), and gets() | |
363 // will automatically work for this stream. | |
364 | |
3523 | 365 virtual std::istream *input_stream (void) { return 0; } |
2117 | 366 |
367 // If the derived class provides this function and it returns a | |
368 // pointer to a valid ostream, flush(), write(), and printf() will | |
369 // automatically work for this stream. | |
370 | |
3523 | 371 virtual std::ostream *output_stream (void) { return 0; } |
2117 | 372 |
3340 | 373 // Return TRUE if this stream is open. |
374 | |
375 bool is_open (void) const { return open_state; } | |
376 | |
3652 | 377 virtual void do_close (void) { } |
378 | |
379 void close (void) | |
380 { | |
381 if (is_open ()) | |
10313 | 382 { |
383 open_state = false; | |
384 do_close (); | |
385 } | |
3652 | 386 } |
3340 | 387 |
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
|
388 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
|
389 { |
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 // 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
|
391 |
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 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
|
393 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
|
394 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
|
395 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
|
396 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
|
397 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
|
398 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
|
399 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
|
400 } |
3145 | 401 |
2117 | 402 bool ok (void) const { return ! fail; } |
403 | |
404 // Return current error message for this stream. | |
405 | |
3523 | 406 std::string error (bool clear, int& err_num); |
2117 | 407 |
408 protected: | |
409 | |
3340 | 410 int mode (void) const { return md; } |
2117 | 411 |
3340 | 412 oct_mach_info::float_format float_format (void) const { return flt_fmt; } |
2117 | 413 |
414 // Set current error state and set fail to TRUE. | |
415 | |
3523 | 416 void error (const std::string& msg); |
4468 | 417 void error (const std::string& who, const std::string& msg); |
2117 | 418 |
419 // Clear any error message and set fail to FALSE. | |
420 | |
421 void clear (void); | |
422 | |
4889 | 423 // Clear stream state. |
424 | |
425 void clearerr (void); | |
426 | |
2117 | 427 private: |
428 | |
3340 | 429 // A reference count. |
5275 | 430 octave_idx_type count; |
3340 | 431 |
2117 | 432 // The permission bits for the file. Should be some combination of |
3544 | 433 // std::ios::open_mode bits. |
2117 | 434 int md; |
435 | |
436 // Data format. | |
2317 | 437 oct_mach_info::float_format flt_fmt; |
2117 | 438 |
439 // TRUE if an error has occurred. | |
440 bool fail; | |
441 | |
3340 | 442 // TRUE if this stream is open. |
443 bool open_state; | |
444 | |
2117 | 445 // Should contain error message if fail is TRUE. |
3523 | 446 std::string errmsg; |
2117 | 447 |
448 // Functions that are defined for all input streams (input streams | |
449 // are those that define is). | |
450 | |
5275 | 451 std::string do_gets (octave_idx_type max_len, bool& err, bool strip_newline, |
10313 | 452 const std::string& who /* = "gets" */); |
2117 | 453 |
5275 | 454 std::string getl (octave_idx_type max_len, bool& err, const std::string& who /* = "getl" */); |
455 std::string gets (octave_idx_type max_len, bool& err, const std::string& who /* = "gets" */); | |
9701 | 456 long skipl (long count, bool& err, const std::string& who /* = "skipl" */); |
2117 | 457 |
5275 | 458 octave_value do_scanf (scanf_format_list& fmt_list, octave_idx_type nr, octave_idx_type nc, |
10313 | 459 bool one_elt_size_spec, octave_idx_type& count, |
460 const std::string& who /* = "scanf" */); | |
2117 | 461 |
4468 | 462 octave_value scanf (const std::string& fmt, const Array<double>& size, |
10313 | 463 octave_idx_type& count, const std::string& who /* = "scanf" */); |
2117 | 464 |
4468 | 465 bool do_oscanf (const scanf_format_elt *elt, octave_value&, |
10313 | 466 const std::string& who /* = "scanf" */); |
2117 | 467 |
4468 | 468 octave_value_list oscanf (const std::string& fmt, |
10313 | 469 const std::string& who /* = "scanf" */); |
2215 | 470 |
2117 | 471 // Functions that are defined for all output streams (output streams |
472 // are those that define os). | |
473 | |
474 int flush (void); | |
475 | |
4468 | 476 int do_printf (printf_format_list& fmt_list, const octave_value_list& args, |
10313 | 477 const std::string& who /* = "printf" */); |
2117 | 478 |
4468 | 479 int printf (const std::string& fmt, const octave_value_list& args, |
10313 | 480 const std::string& who /* = "printf" */); |
2117 | 481 |
4468 | 482 int puts (const std::string& s, const std::string& who /* = "puts" */); |
2117 | 483 |
484 // We can always do this in terms of seek(), so the derived class | |
485 // only has to provide that. | |
486 | |
4468 | 487 void invalid_operation (const std::string& who, const char *rw); |
2117 | 488 |
489 // No copying! | |
490 | |
491 octave_base_stream (const octave_base_stream&); | |
492 | |
493 octave_base_stream& operator = (const octave_base_stream&); | |
494 }; | |
495 | |
496 class | |
6109 | 497 OCTINTERP_API |
2117 | 498 octave_stream |
499 { | |
500 public: | |
501 | |
3340 | 502 octave_stream (octave_base_stream *bs = 0); |
503 | |
504 ~octave_stream (void); | |
2117 | 505 |
3340 | 506 octave_stream (const octave_stream&); |
507 | |
508 octave_stream& operator = (const octave_stream&); | |
2117 | 509 |
510 int flush (void); | |
511 | |
5275 | 512 std::string getl (octave_idx_type max_len, bool& err, const std::string& who /* = "getl" */); |
4468 | 513 std::string getl (const octave_value& max_len, bool& err, |
10313 | 514 const std::string& who /* = "getl" */); |
2117 | 515 |
5275 | 516 std::string gets (octave_idx_type max_len, bool& err, const std::string& who /* = "gets" */); |
4468 | 517 std::string gets (const octave_value& max_len, bool& err, |
10313 | 518 const std::string& who /* = "gets" */); |
2117 | 519 |
9701 | 520 long skipl (long count, bool& err, const std::string& who /* = "skipl" */); |
521 long skipl (const octave_value& count, bool& err, const std::string& who /* = "skipl" */); | |
522 | |
4797 | 523 int seek (long offset, int origin); |
2117 | 524 int seek (const octave_value& offset, const octave_value& origin); |
525 | |
4797 | 526 long tell (void); |
2117 | 527 |
528 int rewind (void); | |
529 | |
3340 | 530 bool is_open (void) const; |
531 | |
532 void close (void); | |
533 | |
5275 | 534 octave_value read (const Array<double>& size, octave_idx_type block_size, |
10313 | 535 oct_data_conv::data_type input_type, |
536 oct_data_conv::data_type output_type, | |
537 octave_idx_type skip, oct_mach_info::float_format flt_fmt, | |
538 octave_idx_type& count); | |
2117 | 539 |
5275 | 540 octave_idx_type write (const octave_value& data, octave_idx_type block_size, |
10313 | 541 oct_data_conv::data_type output_type, |
542 octave_idx_type skip, oct_mach_info::float_format flt_fmt); | |
4944 | 543 |
544 template <class T> | |
5275 | 545 octave_idx_type write (const Array<T>&, octave_idx_type block_size, |
10313 | 546 oct_data_conv::data_type output_type, |
547 octave_idx_type skip, oct_mach_info::float_format flt_fmt); | |
2117 | 548 |
4468 | 549 octave_value scanf (const std::string& fmt, const Array<double>& size, |
10313 | 550 octave_idx_type& count, const std::string& who /* = "scanf" */); |
2117 | 551 |
5279 | 552 octave_value scanf (const octave_value& fmt, const Array<double>& size, |
10313 | 553 octave_idx_type& count, const std::string& who /* = "scanf" */); |
5279 | 554 |
4468 | 555 octave_value_list oscanf (const std::string& fmt, |
10313 | 556 const std::string& who /* = "scanf" */); |
2215 | 557 |
5279 | 558 octave_value_list oscanf (const octave_value& fmt, |
10313 | 559 const std::string& who /* = "scanf" */); |
5279 | 560 |
4468 | 561 int printf (const std::string& fmt, const octave_value_list& args, |
10313 | 562 const std::string& who /* = "printf" */); |
2117 | 563 |
5279 | 564 int printf (const octave_value& fmt, const octave_value_list& args, |
10313 | 565 const std::string& who /* = "printf" */); |
5279 | 566 |
4468 | 567 int puts (const std::string& s, const std::string& who /* = "puts" */); |
568 int puts (const octave_value& s, const std::string& who /* = "puts" */); | |
2117 | 569 |
570 bool eof (void) const; | |
571 | |
3523 | 572 std::string error (bool clear, int& err_num); |
2117 | 573 |
3523 | 574 std::string error (bool clear = false) |
2117 | 575 { |
2435 | 576 int err_num; |
577 return error (clear, err_num); | |
2117 | 578 } |
579 | |
4799 | 580 // Set the error message and state. |
581 | |
582 void error (const std::string& msg) | |
583 { | |
584 if (rep) | |
10313 | 585 rep->error (msg); |
4799 | 586 } |
587 | |
588 void error (const char *msg) { error (std::string (msg)); } | |
589 | |
3148 | 590 int file_number (void) { return rep ? rep->file_number () : -1; } |
3145 | 591 |
3340 | 592 bool is_valid (void) const { return (rep != 0); } |
593 | |
2117 | 594 bool ok (void) const { return rep && rep->ok (); } |
595 | |
3145 | 596 operator bool () const { return ok (); } |
2117 | 597 |
3523 | 598 std::string name (void) const; |
2117 | 599 |
3340 | 600 int mode (void) const; |
2117 | 601 |
3340 | 602 oct_mach_info::float_format float_format (void) const; |
2117 | 603 |
3523 | 604 static std::string mode_as_string (int mode); |
2117 | 605 |
6757 | 606 std::istream *input_stream (void) |
607 { | |
608 return rep ? rep->input_stream () : 0; | |
609 } | |
2902 | 610 |
6757 | 611 std::ostream *output_stream (void) |
612 { | |
613 return rep ? rep->output_stream () : 0; | |
614 } | |
2902 | 615 |
4889 | 616 void clearerr (void) { if (rep) rep->clearerr (); } |
617 | |
2117 | 618 private: |
619 | |
620 // The actual representation of this stream. | |
621 octave_base_stream *rep; | |
622 | |
5659 | 623 bool stream_ok (bool clear = true) const |
624 { | |
625 bool retval = true; | |
626 | |
627 if (rep) | |
10313 | 628 { |
629 if (clear) | |
630 rep->clear (); | |
631 } | |
5659 | 632 else |
10313 | 633 retval = false; |
5659 | 634 |
635 return retval; | |
636 } | |
4944 | 637 |
638 void invalid_operation (const std::string& who, const char *rw) | |
639 { | |
640 if (rep) | |
10313 | 641 rep->invalid_operation (who, rw); |
4944 | 642 } |
2117 | 643 }; |
644 | |
645 class | |
6109 | 646 OCTINTERP_API |
2117 | 647 octave_stream_list |
648 { | |
649 protected: | |
650 | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
651 octave_stream_list (void) : list (), lookup_cache (list.end ()) { } |
2117 | 652 |
653 public: | |
654 | |
655 ~octave_stream_list (void) { } | |
656 | |
2926 | 657 static bool instance_ok (void); |
658 | |
6757 | 659 static int insert (octave_stream& os); |
2117 | 660 |
4468 | 661 static octave_stream |
662 lookup (int fid, const std::string& who = std::string ()); | |
663 | |
664 static octave_stream | |
665 lookup (const octave_value& fid, const std::string& who = std::string ()); | |
2117 | 666 |
3523 | 667 static int remove (int fid, const std::string& who = std::string ()); |
3341 | 668 static int remove (const octave_value& fid, |
10313 | 669 const std::string& who = std::string ()); |
2117 | 670 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
671 static void clear (bool flush = true); |
2117 | 672 |
673 static string_vector get_info (int fid); | |
674 static string_vector get_info (const octave_value& fid); | |
675 | |
3523 | 676 static std::string list_open_files (void); |
2117 | 677 |
678 static octave_value open_file_numbers (void); | |
679 | |
2609 | 680 static int get_file_number (const octave_value& fid); |
681 | |
2117 | 682 private: |
683 | |
6757 | 684 typedef std::map<int, octave_stream> ostrl_map; |
2117 | 685 |
6757 | 686 ostrl_map list; |
2117 | 687 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
688 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
|
689 |
2117 | 690 static octave_stream_list *instance; |
691 | |
6757 | 692 int do_insert (octave_stream& os); |
2117 | 693 |
3523 | 694 octave_stream do_lookup (int fid, const std::string& who = std::string ()) const; |
3341 | 695 octave_stream do_lookup (const octave_value& fid, |
10313 | 696 const std::string& who = std::string ()) const; |
2117 | 697 |
3523 | 698 int do_remove (int fid, const std::string& who = std::string ()); |
699 int do_remove (const octave_value& fid, const std::string& who = std::string ()); | |
2117 | 700 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
701 void do_clear (bool flush = true); |
2117 | 702 |
703 string_vector do_get_info (int fid) const; | |
704 string_vector do_get_info (const octave_value& fid) const; | |
705 | |
3523 | 706 std::string do_list_open_files (void) const; |
2117 | 707 |
708 octave_value do_open_file_numbers (void) const; | |
709 | |
2609 | 710 int do_get_file_number (const octave_value& fid) const; |
2117 | 711 }; |
712 | |
713 #endif |