Mercurial > octave-nkf
annotate libinterp/interp-core/oct-stream.h @ 16011:8122286c69a9
initial large file support for 32-bit systems
* bootstrap.conf (gnulib_modules): Explicitly list largefile.
* c-file-ptr-stream.cc, c-file-ptr-stream.h, oct-fstrm.cc,
oct-fstrm.h, oct-iostrm.cc, oct-iostrm.h, oct-stdstrm.h,
oct-stream.cc, oct-stream.h, oct-strstrm.cc, oct-strstrm.h,
file-io.cc: Use off_t instead of long for seek and tell file position
values.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 07 Feb 2013 11:58:46 -0500 |
parents | 049e8bbff782 |
children | 4b6c44096862 |
rev | line source |
---|---|
2117 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13985
diff
changeset
|
3 Copyright (C) 1996-2012 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" |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13983
diff
changeset
|
40 #include "oct-refcount.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 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
90 // Type of conversion -- 'd', 'i', 'o', 'u', 'x', 'e', 'f', 'g', |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
91 // 'c', 's', 'p', '%', or '['. |
2117 | 92 char type; |
3640 | 93 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
94 // A length modifier -- 'h', 'l', or 'L'. |
2117 | 95 char modifier; |
3640 | 96 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
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 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
162 // FIXME -- maybe LIST should be a std::list object? |
2117 | 163 // List of format elements. |
164 Array<scanf_format_elt*> list; | |
165 | |
166 // Temporary buffer. | |
5765 | 167 std::ostringstream *buf; |
2117 | 168 |
2215 | 169 void add_elt_to_list (int width, bool discard, char type, char modifier, |
10313 | 170 octave_idx_type& num_elts, |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
171 const std::string& char_class = std::string ()); |
2117 | 172 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
173 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
|
174 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
|
175 char& modifier, octave_idx_type& num_elts); |
2117 | 176 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
177 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
|
178 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
|
179 char modifier, octave_idx_type& num_elts); |
2117 | 180 // No copying! |
181 | |
182 scanf_format_list (const scanf_format_list&); | |
183 | |
184 scanf_format_list& operator = (const scanf_format_list&); | |
185 }; | |
186 | |
3640 | 187 class |
2117 | 188 printf_format_elt |
189 { | |
3640 | 190 public: |
191 | |
192 printf_format_elt (const char *txt = 0, int n = 0, int w = 0, | |
10313 | 193 int p = 0, const std::string& f = std::string (), |
194 char typ = '\0', char mod = '\0') | |
3640 | 195 : text (strsave (txt)), args (n), fw (w), prec (p), flags (f), |
196 type (typ), modifier (mod) { } | |
197 | |
198 printf_format_elt (const printf_format_elt& e) | |
199 : text (strsave (e.text)), args (e.args), fw (e.fw), prec (e.prec), | |
200 flags (e.flags), type (e.type), modifier (e.modifier) { } | |
201 | |
202 printf_format_elt& operator = (const printf_format_elt& e) | |
203 { | |
204 if (this != &e) | |
10313 | 205 { |
206 text = strsave (e.text); | |
207 args = e.args; | |
208 fw = e.fw; | |
209 prec = e.prec; | |
210 flags = e.flags; | |
211 type = e.type; | |
212 modifier = e.modifier; | |
213 } | |
2117 | 214 |
3640 | 215 return *this; |
216 } | |
2117 | 217 |
3640 | 218 ~printf_format_elt (void) { delete [] text; } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
219 |
3640 | 220 // The C-style format string. |
2117 | 221 const char *text; |
3640 | 222 |
223 // How many args do we expect to consume? | |
2117 | 224 int args; |
3640 | 225 |
226 // Field width. | |
227 int fw; | |
228 | |
229 // Precision. | |
230 int prec; | |
231 | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
232 // Flags -- '-', '+', ' ', '0', or '#'. |
3642 | 233 std::string flags; |
3640 | 234 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
235 // Type of conversion -- 'd', 'i', 'o', 'x', 'X', 'u', 'c', 's', |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
236 // 'f', 'e', 'E', 'g', 'G', 'p', or '%' |
2117 | 237 char type; |
3640 | 238 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
239 // A length modifier -- 'h', 'l', or 'L'. |
2117 | 240 char modifier; |
241 }; | |
242 | |
243 class | |
6109 | 244 OCTINTERP_API |
2117 | 245 printf_format_list |
246 { | |
247 public: | |
248 | |
3523 | 249 printf_format_list (const std::string& fmt = std::string ()); |
2117 | 250 |
251 ~printf_format_list (void); | |
252 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
253 octave_idx_type num_conversions (void) { return nconv; } |
2117 | 254 |
255 const printf_format_elt *first (void) | |
256 { | |
257 curr_idx = 0; | |
258 return current (); | |
259 } | |
260 | |
261 const printf_format_elt *current (void) const | |
262 { return list.length () > 0 ? list.elem (curr_idx) : 0; } | |
263 | |
3640 | 264 const printf_format_elt *next (bool cycle = true) |
2117 | 265 { |
266 curr_idx++; | |
3640 | 267 |
2117 | 268 if (curr_idx >= list.length ()) |
10313 | 269 { |
270 if (cycle) | |
271 curr_idx = 0; | |
272 else | |
273 return 0; | |
274 } | |
3640 | 275 |
2117 | 276 return current (); |
277 } | |
278 | |
3640 | 279 bool last_elt_p (void) { return (curr_idx + 1 == list.length ()); } |
280 | |
2117 | 281 void printme (void) const; |
282 | |
283 bool ok (void) const { return (nconv >= 0); } | |
284 | |
3145 | 285 operator bool () const { return ok (); } |
2117 | 286 |
287 private: | |
288 | |
3642 | 289 // Number of conversions specified by this format string, or -1 if |
2117 | 290 // 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
|
291 octave_idx_type nconv; |
2117 | 292 |
293 // 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
|
294 octave_idx_type curr_idx; |
2117 | 295 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
296 // FIXME -- maybe LIST should be a std::list object? |
2117 | 297 // List of format elements. |
298 Array<printf_format_elt*> list; | |
299 | |
300 // Temporary buffer. | |
5765 | 301 std::ostringstream *buf; |
2117 | 302 |
3640 | 303 void add_elt_to_list (int args, const std::string& flags, int fw, |
10313 | 304 int prec, char type, char modifier, |
305 octave_idx_type& num_elts); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
306 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
307 void process_conversion (const std::string& s, size_t& i, size_t n, |
10313 | 308 int& args, std::string& flags, int& fw, |
309 int& prec, char& modifier, char& type, | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
310 octave_idx_type& num_elts); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
311 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
312 void finish_conversion (const std::string& s, size_t& i, int args, |
10313 | 313 const std::string& flags, int fw, int prec, |
314 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
|
315 octave_idx_type& num_elts); |
2117 | 316 |
317 // No copying! | |
318 | |
319 printf_format_list (const printf_format_list&); | |
320 | |
321 printf_format_list& operator = (const printf_format_list&); | |
322 }; | |
323 | |
324 // Provide an interface for Octave streams. | |
325 | |
326 class | |
6109 | 327 OCTINTERP_API |
2117 | 328 octave_base_stream |
329 { | |
330 friend class octave_stream; | |
331 | |
332 public: | |
333 | |
3544 | 334 octave_base_stream (std::ios::openmode arg_md = std::ios::in|std::ios::out, |
10313 | 335 oct_mach_info::float_format ff |
336 = 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
|
337 : 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
|
338 errmsg () |
3340 | 339 { } |
2117 | 340 |
341 virtual ~octave_base_stream (void) { } | |
342 | |
343 // The remaining functions are not specific to input or output only, | |
344 // and must be provided by the derived classes. | |
345 | |
346 // Position a stream at OFFSET relative to ORIGIN. | |
347 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
348 virtual int seek (off_t offset, int origin) = 0; |
2117 | 349 |
350 // Return current stream position. | |
351 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
352 virtual off_t tell (void) = 0; |
2117 | 353 |
3340 | 354 // Return TRUE if EOF has been reached on this stream. |
2117 | 355 |
356 virtual bool eof (void) const = 0; | |
357 | |
358 // The name of the file. | |
359 | |
3523 | 360 virtual std::string name (void) const = 0; |
2117 | 361 |
362 // If the derived class provides this function and it returns a | |
363 // pointer to a valid istream, scanf(), read(), getl(), and gets() | |
364 // will automatically work for this stream. | |
365 | |
3523 | 366 virtual std::istream *input_stream (void) { return 0; } |
2117 | 367 |
368 // If the derived class provides this function and it returns a | |
369 // pointer to a valid ostream, flush(), write(), and printf() will | |
370 // automatically work for this stream. | |
371 | |
3523 | 372 virtual std::ostream *output_stream (void) { return 0; } |
2117 | 373 |
14210
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
374 // If the derived class is locale-aware, it must implement this function |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
375 // in order to set a new locale. By default, this function avoids messing |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
376 // with locales and ignores its input argument. |
14410
f3c958ae393d
oct-stream.h: Silence unused variable warning
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14210
diff
changeset
|
377 virtual std::locale imbue ( const std::locale &) |
14210
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
378 { return std::locale::classic (); } |
14410
f3c958ae393d
oct-stream.h: Silence unused variable warning
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14210
diff
changeset
|
379 |
3340 | 380 // Return TRUE if this stream is open. |
381 | |
382 bool is_open (void) const { return open_state; } | |
383 | |
3652 | 384 virtual void do_close (void) { } |
385 | |
386 void close (void) | |
387 { | |
388 if (is_open ()) | |
10313 | 389 { |
390 open_state = false; | |
391 do_close (); | |
392 } | |
3652 | 393 } |
3340 | 394 |
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
|
395 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
|
396 { |
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 // 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
|
398 |
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 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
|
400 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
|
401 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
|
402 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
|
403 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
|
404 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
|
405 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
|
406 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
|
407 } |
3145 | 408 |
2117 | 409 bool ok (void) const { return ! fail; } |
410 | |
411 // Return current error message for this stream. | |
412 | |
3523 | 413 std::string error (bool clear, int& err_num); |
2117 | 414 |
415 protected: | |
416 | |
3340 | 417 int mode (void) const { return md; } |
2117 | 418 |
3340 | 419 oct_mach_info::float_format float_format (void) const { return flt_fmt; } |
2117 | 420 |
421 // Set current error state and set fail to TRUE. | |
422 | |
3523 | 423 void error (const std::string& msg); |
4468 | 424 void error (const std::string& who, const std::string& msg); |
2117 | 425 |
426 // Clear any error message and set fail to FALSE. | |
427 | |
428 void clear (void); | |
429 | |
4889 | 430 // Clear stream state. |
431 | |
432 void clearerr (void); | |
433 | |
2117 | 434 private: |
435 | |
3340 | 436 // A reference count. |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13983
diff
changeset
|
437 octave_refcount<octave_idx_type> count; |
3340 | 438 |
2117 | 439 // The permission bits for the file. Should be some combination of |
3544 | 440 // std::ios::open_mode bits. |
2117 | 441 int md; |
442 | |
443 // Data format. | |
2317 | 444 oct_mach_info::float_format flt_fmt; |
2117 | 445 |
446 // TRUE if an error has occurred. | |
447 bool fail; | |
448 | |
3340 | 449 // TRUE if this stream is open. |
450 bool open_state; | |
451 | |
2117 | 452 // Should contain error message if fail is TRUE. |
3523 | 453 std::string errmsg; |
2117 | 454 |
455 // Functions that are defined for all input streams (input streams | |
456 // are those that define is). | |
457 | |
5275 | 458 std::string do_gets (octave_idx_type max_len, bool& err, bool strip_newline, |
10313 | 459 const std::string& who /* = "gets" */); |
2117 | 460 |
5275 | 461 std::string getl (octave_idx_type max_len, bool& err, const std::string& who /* = "getl" */); |
462 std::string gets (octave_idx_type max_len, bool& err, const std::string& who /* = "gets" */); | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
463 off_t skipl (off_t count, bool& err, const std::string& who /* = "skipl" */); |
2117 | 464 |
5275 | 465 octave_value do_scanf (scanf_format_list& fmt_list, octave_idx_type nr, octave_idx_type nc, |
10313 | 466 bool one_elt_size_spec, octave_idx_type& count, |
467 const std::string& who /* = "scanf" */); | |
2117 | 468 |
4468 | 469 octave_value scanf (const std::string& fmt, const Array<double>& size, |
10313 | 470 octave_idx_type& count, const std::string& who /* = "scanf" */); |
2117 | 471 |
4468 | 472 bool do_oscanf (const scanf_format_elt *elt, octave_value&, |
10313 | 473 const std::string& who /* = "scanf" */); |
2117 | 474 |
4468 | 475 octave_value_list oscanf (const std::string& fmt, |
10313 | 476 const std::string& who /* = "scanf" */); |
2215 | 477 |
2117 | 478 // Functions that are defined for all output streams (output streams |
479 // are those that define os). | |
480 | |
481 int flush (void); | |
482 | |
4468 | 483 int do_printf (printf_format_list& fmt_list, const octave_value_list& args, |
10313 | 484 const std::string& who /* = "printf" */); |
2117 | 485 |
4468 | 486 int printf (const std::string& fmt, const octave_value_list& args, |
10313 | 487 const std::string& who /* = "printf" */); |
2117 | 488 |
4468 | 489 int puts (const std::string& s, const std::string& who /* = "puts" */); |
2117 | 490 |
491 // We can always do this in terms of seek(), so the derived class | |
492 // only has to provide that. | |
493 | |
4468 | 494 void invalid_operation (const std::string& who, const char *rw); |
2117 | 495 |
496 // No copying! | |
497 | |
498 octave_base_stream (const octave_base_stream&); | |
499 | |
500 octave_base_stream& operator = (const octave_base_stream&); | |
501 }; | |
502 | |
503 class | |
6109 | 504 OCTINTERP_API |
2117 | 505 octave_stream |
506 { | |
507 public: | |
508 | |
3340 | 509 octave_stream (octave_base_stream *bs = 0); |
510 | |
511 ~octave_stream (void); | |
2117 | 512 |
3340 | 513 octave_stream (const octave_stream&); |
514 | |
515 octave_stream& operator = (const octave_stream&); | |
2117 | 516 |
517 int flush (void); | |
518 | |
5275 | 519 std::string getl (octave_idx_type max_len, bool& err, const std::string& who /* = "getl" */); |
4468 | 520 std::string getl (const octave_value& max_len, bool& err, |
10313 | 521 const std::string& who /* = "getl" */); |
2117 | 522 |
5275 | 523 std::string gets (octave_idx_type max_len, bool& err, const std::string& who /* = "gets" */); |
4468 | 524 std::string gets (const octave_value& max_len, bool& err, |
10313 | 525 const std::string& who /* = "gets" */); |
2117 | 526 |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
527 off_t skipl (off_t count, bool& err, const std::string& who /* = "skipl" */); |
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
528 off_t skipl (const octave_value& count, bool& err, const std::string& who /* = "skipl" */); |
9701 | 529 |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
530 int seek (off_t offset, int origin); |
2117 | 531 int seek (const octave_value& offset, const octave_value& origin); |
532 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
533 off_t tell (void); |
2117 | 534 |
535 int rewind (void); | |
536 | |
3340 | 537 bool is_open (void) const; |
538 | |
539 void close (void); | |
540 | |
5275 | 541 octave_value read (const Array<double>& size, octave_idx_type block_size, |
10313 | 542 oct_data_conv::data_type input_type, |
543 oct_data_conv::data_type output_type, | |
544 octave_idx_type skip, oct_mach_info::float_format flt_fmt, | |
545 octave_idx_type& count); | |
2117 | 546 |
5275 | 547 octave_idx_type write (const octave_value& data, octave_idx_type block_size, |
10313 | 548 oct_data_conv::data_type output_type, |
549 octave_idx_type skip, oct_mach_info::float_format flt_fmt); | |
4944 | 550 |
551 template <class T> | |
5275 | 552 octave_idx_type write (const Array<T>&, octave_idx_type block_size, |
10313 | 553 oct_data_conv::data_type output_type, |
554 octave_idx_type skip, oct_mach_info::float_format flt_fmt); | |
2117 | 555 |
4468 | 556 octave_value scanf (const std::string& fmt, const Array<double>& size, |
10313 | 557 octave_idx_type& count, const std::string& who /* = "scanf" */); |
2117 | 558 |
5279 | 559 octave_value scanf (const octave_value& fmt, const Array<double>& size, |
10313 | 560 octave_idx_type& count, const std::string& who /* = "scanf" */); |
5279 | 561 |
4468 | 562 octave_value_list oscanf (const std::string& fmt, |
10313 | 563 const std::string& who /* = "scanf" */); |
2215 | 564 |
5279 | 565 octave_value_list oscanf (const octave_value& fmt, |
10313 | 566 const std::string& who /* = "scanf" */); |
5279 | 567 |
4468 | 568 int printf (const std::string& fmt, const octave_value_list& args, |
10313 | 569 const std::string& who /* = "printf" */); |
2117 | 570 |
5279 | 571 int printf (const octave_value& fmt, const octave_value_list& args, |
10313 | 572 const std::string& who /* = "printf" */); |
5279 | 573 |
4468 | 574 int puts (const std::string& s, const std::string& who /* = "puts" */); |
575 int puts (const octave_value& s, const std::string& who /* = "puts" */); | |
2117 | 576 |
577 bool eof (void) const; | |
578 | |
3523 | 579 std::string error (bool clear, int& err_num); |
2117 | 580 |
3523 | 581 std::string error (bool clear = false) |
2117 | 582 { |
2435 | 583 int err_num; |
584 return error (clear, err_num); | |
2117 | 585 } |
586 | |
4799 | 587 // Set the error message and state. |
588 | |
589 void error (const std::string& msg) | |
590 { | |
591 if (rep) | |
10313 | 592 rep->error (msg); |
4799 | 593 } |
594 | |
595 void error (const char *msg) { error (std::string (msg)); } | |
596 | |
3148 | 597 int file_number (void) { return rep ? rep->file_number () : -1; } |
3145 | 598 |
3340 | 599 bool is_valid (void) const { return (rep != 0); } |
600 | |
2117 | 601 bool ok (void) const { return rep && rep->ok (); } |
602 | |
3145 | 603 operator bool () const { return ok (); } |
2117 | 604 |
3523 | 605 std::string name (void) const; |
2117 | 606 |
3340 | 607 int mode (void) const; |
2117 | 608 |
3340 | 609 oct_mach_info::float_format float_format (void) const; |
2117 | 610 |
3523 | 611 static std::string mode_as_string (int mode); |
2117 | 612 |
6757 | 613 std::istream *input_stream (void) |
614 { | |
615 return rep ? rep->input_stream () : 0; | |
616 } | |
2902 | 617 |
6757 | 618 std::ostream *output_stream (void) |
619 { | |
620 return rep ? rep->output_stream () : 0; | |
621 } | |
14210
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
622 |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
623 std::locale imbue (const std::locale & loc ) |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
624 { |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
625 if (!rep) return std::locale::classic (); |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
626 |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
627 std::istream *is = rep->input_stream (); |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
628 std::ostream *os = rep->output_stream (); |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
629 |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
630 if (os) |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
631 { |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
632 if (is) |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
633 (void) is->imbue (loc); |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
634 return os->imbue (loc); |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
635 } |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
636 return is ? is->imbue (loc) : std::locale::classic (); |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
637 } |
238e499c5fea
locale support implementation in [fs]scanf functions
Pascal Dupuis <Pascal.Dupuis@worldonline.be>
parents:
14138
diff
changeset
|
638 |
4889 | 639 void clearerr (void) { if (rep) rep->clearerr (); } |
640 | |
2117 | 641 private: |
642 | |
643 // The actual representation of this stream. | |
644 octave_base_stream *rep; | |
645 | |
5659 | 646 bool stream_ok (bool clear = true) const |
647 { | |
648 bool retval = true; | |
649 | |
650 if (rep) | |
10313 | 651 { |
652 if (clear) | |
653 rep->clear (); | |
654 } | |
5659 | 655 else |
10313 | 656 retval = false; |
5659 | 657 |
658 return retval; | |
659 } | |
4944 | 660 |
661 void invalid_operation (const std::string& who, const char *rw) | |
662 { | |
663 if (rep) | |
10313 | 664 rep->invalid_operation (who, rw); |
4944 | 665 } |
2117 | 666 }; |
667 | |
668 class | |
6109 | 669 OCTINTERP_API |
2117 | 670 octave_stream_list |
671 { | |
672 protected: | |
673 | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
674 octave_stream_list (void) : list (), lookup_cache (list.end ()) { } |
2117 | 675 |
676 public: | |
677 | |
678 ~octave_stream_list (void) { } | |
679 | |
2926 | 680 static bool instance_ok (void); |
681 | |
6757 | 682 static int insert (octave_stream& os); |
2117 | 683 |
4468 | 684 static octave_stream |
685 lookup (int fid, const std::string& who = std::string ()); | |
686 | |
687 static octave_stream | |
688 lookup (const octave_value& fid, const std::string& who = std::string ()); | |
2117 | 689 |
3523 | 690 static int remove (int fid, const std::string& who = std::string ()); |
3341 | 691 static int remove (const octave_value& fid, |
10313 | 692 const std::string& who = std::string ()); |
2117 | 693 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
694 static void clear (bool flush = true); |
2117 | 695 |
696 static string_vector get_info (int fid); | |
697 static string_vector get_info (const octave_value& fid); | |
698 | |
3523 | 699 static std::string list_open_files (void); |
2117 | 700 |
701 static octave_value open_file_numbers (void); | |
702 | |
2609 | 703 static int get_file_number (const octave_value& fid); |
704 | |
2117 | 705 private: |
706 | |
6757 | 707 typedef std::map<int, octave_stream> ostrl_map; |
2117 | 708 |
6757 | 709 ostrl_map list; |
2117 | 710 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
711 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
|
712 |
2117 | 713 static octave_stream_list *instance; |
714 | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
715 static void cleanup_instance (void) { delete instance; instance = 0; } |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
716 |
6757 | 717 int do_insert (octave_stream& os); |
2117 | 718 |
3523 | 719 octave_stream do_lookup (int fid, const std::string& who = std::string ()) const; |
3341 | 720 octave_stream do_lookup (const octave_value& fid, |
10313 | 721 const std::string& who = std::string ()) const; |
2117 | 722 |
3523 | 723 int do_remove (int fid, const std::string& who = std::string ()); |
724 int do_remove (const octave_value& fid, const std::string& who = std::string ()); | |
2117 | 725 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
726 void do_clear (bool flush = true); |
2117 | 727 |
728 string_vector do_get_info (int fid) const; | |
729 string_vector do_get_info (const octave_value& fid) const; | |
730 | |
3523 | 731 std::string do_list_open_files (void) const; |
2117 | 732 |
733 octave_value do_open_file_numbers (void) const; | |
734 | |
2609 | 735 int do_get_file_number (const octave_value& fid) const; |
2117 | 736 }; |
737 | |
738 #endif |