Mercurial > octave-nkf
annotate libinterp/corefcn/oct-stream.h @ 17416:6690dba6078a
improve efficiency of fwrite
* oct-stream.h, oct-stream.cc (write_int, do_write): Delete.
(convert_ints, convert_data, octave_stream::write_bytes
octave_stream::skip_bytes): New functions.
(octave_stream::write): Improve efficiency by writing data in larger
chunks.
* oct-stream.cc: Delete unnecessary template instantiations.
* data-conv.h, data-conv.cc (is_equivalent_type): New template.
Provide specializations for core Octave data types.
(oct_data_conv::data_type_size): New function.
(oct_data_conv::data_type_to_string): Correct spelling of unsigned.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 13 Sep 2013 23:31:11 -0400 |
parents | 68fc671a9339 |
children | 669ad11f282d |
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" |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
40 #include "oct-locbuf.h" |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13983
diff
changeset
|
41 #include "oct-refcount.h" |
2117 | 42 |
3640 | 43 class |
6109 | 44 OCTINTERP_API |
2117 | 45 scanf_format_elt |
46 { | |
3640 | 47 public: |
48 | |
3483 | 49 enum special_conversion |
50 { | |
51 whitespace_conversion = 1, | |
52 literal_conversion = 2 | |
53 }; | |
54 | |
2215 | 55 scanf_format_elt (const char *txt = 0, int w = 0, bool d = false, |
10313 | 56 char typ = '\0', char mod = '\0', |
57 const std::string& ch_class = std::string ()) | |
3640 | 58 : text (strsave (txt)), width (w), discard (d), type (typ), |
59 modifier (mod), char_class (ch_class) { } | |
60 | |
61 scanf_format_elt (const scanf_format_elt& e) | |
62 : text (strsave (e.text)), width (e.width), discard (e.discard), | |
63 type (e.type), modifier (e.modifier), char_class (e.char_class) { } | |
2117 | 64 |
3640 | 65 scanf_format_elt& operator = (const scanf_format_elt& e) |
66 { | |
67 if (this != &e) | |
10313 | 68 { |
69 text = strsave (e.text); | |
70 width = e.width; | |
71 discard = e.discard; | |
72 type = e.type; | |
73 modifier = e.modifier; | |
74 char_class = e.char_class; | |
75 } | |
2117 | 76 |
3640 | 77 return *this; |
78 } | |
79 | |
80 ~scanf_format_elt (void) { delete [] text; } | |
81 | |
82 // The C-style format string. | |
2117 | 83 const char *text; |
3640 | 84 |
85 // The maximum field width. | |
2215 | 86 int width; |
3640 | 87 |
88 // TRUE if we are not storing the result of this conversion. | |
2117 | 89 bool discard; |
3640 | 90 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
91 // 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
|
92 // 'c', 's', 'p', '%', or '['. |
2117 | 93 char type; |
3640 | 94 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
95 // A length modifier -- 'h', 'l', or 'L'. |
2117 | 96 char modifier; |
3640 | 97 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
98 // The class of characters in a '[' format. |
3523 | 99 std::string char_class; |
2117 | 100 }; |
101 | |
102 class | |
6109 | 103 OCTINTERP_API |
2117 | 104 scanf_format_list |
105 { | |
106 public: | |
107 | |
3523 | 108 scanf_format_list (const std::string& fmt = std::string ()); |
2117 | 109 |
110 ~scanf_format_list (void); | |
111 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
112 octave_idx_type num_conversions (void) { return nconv; } |
2117 | 113 |
2215 | 114 // The length can be different than the number of conversions. |
115 // For example, "x %d y %d z" has 2 conversions but the length of | |
116 // the list is 3 because of the characters that appear after the | |
117 // last conversion. | |
118 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
119 octave_idx_type length (void) { return list.length (); } |
2215 | 120 |
2117 | 121 const scanf_format_elt *first (void) |
122 { | |
123 curr_idx = 0; | |
124 return current (); | |
125 } | |
126 | |
127 const scanf_format_elt *current (void) const | |
128 { return list.length () > 0 ? list.elem (curr_idx) : 0; } | |
129 | |
3640 | 130 const scanf_format_elt *next (bool cycle = true) |
2117 | 131 { |
132 curr_idx++; | |
3640 | 133 |
2117 | 134 if (curr_idx >= list.length ()) |
10313 | 135 { |
136 if (cycle) | |
137 curr_idx = 0; | |
138 else | |
139 return 0; | |
140 } | |
2117 | 141 return current (); |
142 } | |
143 | |
144 void printme (void) const; | |
145 | |
146 bool ok (void) const { return (nconv >= 0); } | |
147 | |
3145 | 148 operator bool () const { return ok (); } |
2117 | 149 |
150 bool all_character_conversions (void); | |
151 | |
152 bool all_numeric_conversions (void); | |
153 | |
154 private: | |
155 | |
3642 | 156 // Number of conversions specified by this format string, or -1 if |
2117 | 157 // 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
|
158 octave_idx_type nconv; |
2117 | 159 |
160 // 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
|
161 octave_idx_type curr_idx; |
2117 | 162 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
163 // FIXME -- maybe LIST should be a std::list object? |
2117 | 164 // List of format elements. |
165 Array<scanf_format_elt*> list; | |
166 | |
167 // Temporary buffer. | |
5765 | 168 std::ostringstream *buf; |
2117 | 169 |
2215 | 170 void add_elt_to_list (int width, bool discard, char type, char modifier, |
10313 | 171 octave_idx_type& num_elts, |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
172 const std::string& char_class = std::string ()); |
2117 | 173 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
174 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
|
175 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
|
176 char& modifier, octave_idx_type& num_elts); |
2117 | 177 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
178 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
|
179 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
|
180 char modifier, octave_idx_type& num_elts); |
2117 | 181 // No copying! |
182 | |
183 scanf_format_list (const scanf_format_list&); | |
184 | |
185 scanf_format_list& operator = (const scanf_format_list&); | |
186 }; | |
187 | |
3640 | 188 class |
2117 | 189 printf_format_elt |
190 { | |
3640 | 191 public: |
192 | |
193 printf_format_elt (const char *txt = 0, int n = 0, int w = 0, | |
10313 | 194 int p = 0, const std::string& f = std::string (), |
195 char typ = '\0', char mod = '\0') | |
3640 | 196 : text (strsave (txt)), args (n), fw (w), prec (p), flags (f), |
197 type (typ), modifier (mod) { } | |
198 | |
199 printf_format_elt (const printf_format_elt& e) | |
200 : text (strsave (e.text)), args (e.args), fw (e.fw), prec (e.prec), | |
201 flags (e.flags), type (e.type), modifier (e.modifier) { } | |
202 | |
203 printf_format_elt& operator = (const printf_format_elt& e) | |
204 { | |
205 if (this != &e) | |
10313 | 206 { |
207 text = strsave (e.text); | |
208 args = e.args; | |
209 fw = e.fw; | |
210 prec = e.prec; | |
211 flags = e.flags; | |
212 type = e.type; | |
213 modifier = e.modifier; | |
214 } | |
2117 | 215 |
3640 | 216 return *this; |
217 } | |
2117 | 218 |
3640 | 219 ~printf_format_elt (void) { delete [] text; } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
220 |
3640 | 221 // The C-style format string. |
2117 | 222 const char *text; |
3640 | 223 |
224 // How many args do we expect to consume? | |
2117 | 225 int args; |
3640 | 226 |
227 // Field width. | |
228 int fw; | |
229 | |
230 // Precision. | |
231 int prec; | |
232 | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
233 // Flags -- '-', '+', ' ', '0', or '#'. |
3642 | 234 std::string flags; |
3640 | 235 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
236 // 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
|
237 // 'f', 'e', 'E', 'g', 'G', 'p', or '%' |
2117 | 238 char type; |
3640 | 239 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
240 // A length modifier -- 'h', 'l', or 'L'. |
2117 | 241 char modifier; |
242 }; | |
243 | |
244 class | |
6109 | 245 OCTINTERP_API |
2117 | 246 printf_format_list |
247 { | |
248 public: | |
249 | |
3523 | 250 printf_format_list (const std::string& fmt = std::string ()); |
2117 | 251 |
252 ~printf_format_list (void); | |
253 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
254 octave_idx_type num_conversions (void) { return nconv; } |
2117 | 255 |
256 const printf_format_elt *first (void) | |
257 { | |
258 curr_idx = 0; | |
259 return current (); | |
260 } | |
261 | |
262 const printf_format_elt *current (void) const | |
263 { return list.length () > 0 ? list.elem (curr_idx) : 0; } | |
264 | |
3640 | 265 const printf_format_elt *next (bool cycle = true) |
2117 | 266 { |
267 curr_idx++; | |
3640 | 268 |
2117 | 269 if (curr_idx >= list.length ()) |
10313 | 270 { |
271 if (cycle) | |
272 curr_idx = 0; | |
273 else | |
274 return 0; | |
275 } | |
3640 | 276 |
2117 | 277 return current (); |
278 } | |
279 | |
3640 | 280 bool last_elt_p (void) { return (curr_idx + 1 == list.length ()); } |
281 | |
2117 | 282 void printme (void) const; |
283 | |
284 bool ok (void) const { return (nconv >= 0); } | |
285 | |
3145 | 286 operator bool () const { return ok (); } |
2117 | 287 |
288 private: | |
289 | |
3642 | 290 // Number of conversions specified by this format string, or -1 if |
2117 | 291 // 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
|
292 octave_idx_type nconv; |
2117 | 293 |
294 // 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
|
295 octave_idx_type curr_idx; |
2117 | 296 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
297 // FIXME -- maybe LIST should be a std::list object? |
2117 | 298 // List of format elements. |
299 Array<printf_format_elt*> list; | |
300 | |
301 // Temporary buffer. | |
5765 | 302 std::ostringstream *buf; |
2117 | 303 |
3640 | 304 void add_elt_to_list (int args, const std::string& flags, int fw, |
10313 | 305 int prec, char type, char modifier, |
306 octave_idx_type& num_elts); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
307 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
308 void process_conversion (const std::string& s, size_t& i, size_t n, |
10313 | 309 int& args, std::string& flags, int& fw, |
310 int& prec, char& modifier, char& type, | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
311 octave_idx_type& num_elts); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11584
diff
changeset
|
312 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
313 void finish_conversion (const std::string& s, size_t& i, int args, |
10313 | 314 const std::string& flags, int fw, int prec, |
315 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
|
316 octave_idx_type& num_elts); |
2117 | 317 |
318 // No copying! | |
319 | |
320 printf_format_list (const printf_format_list&); | |
321 | |
322 printf_format_list& operator = (const printf_format_list&); | |
323 }; | |
324 | |
325 // Provide an interface for Octave streams. | |
326 | |
327 class | |
6109 | 328 OCTINTERP_API |
2117 | 329 octave_base_stream |
330 { | |
331 friend class octave_stream; | |
332 | |
333 public: | |
334 | |
3544 | 335 octave_base_stream (std::ios::openmode arg_md = std::ios::in|std::ios::out, |
10313 | 336 oct_mach_info::float_format ff |
337 = 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
|
338 : 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
|
339 errmsg () |
3340 | 340 { } |
2117 | 341 |
342 virtual ~octave_base_stream (void) { } | |
343 | |
344 // The remaining functions are not specific to input or output only, | |
345 // and must be provided by the derived classes. | |
346 | |
347 // Position a stream at OFFSET relative to ORIGIN. | |
348 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
349 virtual int seek (off_t offset, int origin) = 0; |
2117 | 350 |
351 // Return current stream position. | |
352 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
353 virtual off_t tell (void) = 0; |
2117 | 354 |
3340 | 355 // Return TRUE if EOF has been reached on this stream. |
2117 | 356 |
357 virtual bool eof (void) const = 0; | |
358 | |
359 // The name of the file. | |
360 | |
3523 | 361 virtual std::string name (void) const = 0; |
2117 | 362 |
363 // If the derived class provides this function and it returns a | |
364 // pointer to a valid istream, scanf(), read(), getl(), and gets() | |
365 // will automatically work for this stream. | |
366 | |
3523 | 367 virtual std::istream *input_stream (void) { return 0; } |
2117 | 368 |
369 // If the derived class provides this function and it returns a | |
370 // pointer to a valid ostream, flush(), write(), and printf() will | |
371 // automatically work for this stream. | |
372 | |
3523 | 373 virtual std::ostream *output_stream (void) { return 0; } |
2117 | 374 |
3340 | 375 // Return TRUE if this stream is open. |
376 | |
377 bool is_open (void) const { return open_state; } | |
378 | |
3652 | 379 virtual void do_close (void) { } |
380 | |
381 void close (void) | |
382 { | |
383 if (is_open ()) | |
10313 | 384 { |
385 open_state = false; | |
386 do_close (); | |
387 } | |
3652 | 388 } |
3340 | 389 |
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
|
390 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
|
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 // 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
|
393 |
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 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
|
395 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
|
396 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
|
397 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
|
398 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
|
399 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
|
400 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
|
401 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
|
402 } |
3145 | 403 |
2117 | 404 bool ok (void) const { return ! fail; } |
405 | |
406 // Return current error message for this stream. | |
407 | |
3523 | 408 std::string error (bool clear, int& err_num); |
2117 | 409 |
410 protected: | |
411 | |
3340 | 412 int mode (void) const { return md; } |
2117 | 413 |
3340 | 414 oct_mach_info::float_format float_format (void) const { return flt_fmt; } |
2117 | 415 |
416 // Set current error state and set fail to TRUE. | |
417 | |
3523 | 418 void error (const std::string& msg); |
4468 | 419 void error (const std::string& who, const std::string& msg); |
2117 | 420 |
421 // Clear any error message and set fail to FALSE. | |
422 | |
423 void clear (void); | |
424 | |
4889 | 425 // Clear stream state. |
426 | |
427 void clearerr (void); | |
428 | |
2117 | 429 private: |
430 | |
3340 | 431 // A reference count. |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13983
diff
changeset
|
432 octave_refcount<octave_idx_type> count; |
3340 | 433 |
2117 | 434 // The permission bits for the file. Should be some combination of |
3544 | 435 // std::ios::open_mode bits. |
2117 | 436 int md; |
437 | |
438 // Data format. | |
2317 | 439 oct_mach_info::float_format flt_fmt; |
2117 | 440 |
441 // TRUE if an error has occurred. | |
442 bool fail; | |
443 | |
3340 | 444 // TRUE if this stream is open. |
445 bool open_state; | |
446 | |
2117 | 447 // Should contain error message if fail is TRUE. |
3523 | 448 std::string errmsg; |
2117 | 449 |
450 // Functions that are defined for all input streams (input streams | |
451 // are those that define is). | |
452 | |
5275 | 453 std::string do_gets (octave_idx_type max_len, bool& err, bool strip_newline, |
10313 | 454 const std::string& who /* = "gets" */); |
2117 | 455 |
5275 | 456 std::string getl (octave_idx_type max_len, bool& err, const std::string& who /* = "getl" */); |
457 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
|
458 off_t skipl (off_t count, bool& err, const std::string& who /* = "skipl" */); |
2117 | 459 |
5275 | 460 octave_value do_scanf (scanf_format_list& fmt_list, octave_idx_type nr, octave_idx_type nc, |
10313 | 461 bool one_elt_size_spec, octave_idx_type& count, |
462 const std::string& who /* = "scanf" */); | |
2117 | 463 |
4468 | 464 octave_value scanf (const std::string& fmt, const Array<double>& size, |
10313 | 465 octave_idx_type& count, const std::string& who /* = "scanf" */); |
2117 | 466 |
4468 | 467 bool do_oscanf (const scanf_format_elt *elt, octave_value&, |
10313 | 468 const std::string& who /* = "scanf" */); |
2117 | 469 |
4468 | 470 octave_value_list oscanf (const std::string& fmt, |
10313 | 471 const std::string& who /* = "scanf" */); |
2215 | 472 |
2117 | 473 // Functions that are defined for all output streams (output streams |
474 // are those that define os). | |
475 | |
476 int flush (void); | |
477 | |
4468 | 478 int do_printf (printf_format_list& fmt_list, const octave_value_list& args, |
10313 | 479 const std::string& who /* = "printf" */); |
2117 | 480 |
4468 | 481 int printf (const std::string& fmt, const octave_value_list& args, |
10313 | 482 const std::string& who /* = "printf" */); |
2117 | 483 |
4468 | 484 int puts (const std::string& s, const std::string& who /* = "puts" */); |
2117 | 485 |
486 // We can always do this in terms of seek(), so the derived class | |
487 // only has to provide that. | |
488 | |
4468 | 489 void invalid_operation (const std::string& who, const char *rw); |
2117 | 490 |
491 // No copying! | |
492 | |
493 octave_base_stream (const octave_base_stream&); | |
494 | |
495 octave_base_stream& operator = (const octave_base_stream&); | |
496 }; | |
497 | |
498 class | |
6109 | 499 OCTINTERP_API |
2117 | 500 octave_stream |
501 { | |
502 public: | |
503 | |
3340 | 504 octave_stream (octave_base_stream *bs = 0); |
505 | |
506 ~octave_stream (void); | |
2117 | 507 |
3340 | 508 octave_stream (const octave_stream&); |
509 | |
510 octave_stream& operator = (const octave_stream&); | |
2117 | 511 |
512 int flush (void); | |
513 | |
5275 | 514 std::string getl (octave_idx_type max_len, bool& err, const std::string& who /* = "getl" */); |
4468 | 515 std::string getl (const octave_value& max_len, bool& err, |
10313 | 516 const std::string& who /* = "getl" */); |
2117 | 517 |
5275 | 518 std::string gets (octave_idx_type max_len, bool& err, const std::string& who /* = "gets" */); |
4468 | 519 std::string gets (const octave_value& max_len, bool& err, |
10313 | 520 const std::string& who /* = "gets" */); |
2117 | 521 |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
522 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
|
523 off_t skipl (const octave_value& count, bool& err, const std::string& who /* = "skipl" */); |
9701 | 524 |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
525 int seek (off_t offset, int origin); |
2117 | 526 int seek (const octave_value& offset, const octave_value& origin); |
527 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
528 off_t tell (void); |
2117 | 529 |
530 int rewind (void); | |
531 | |
3340 | 532 bool is_open (void) const; |
533 | |
534 void close (void); | |
535 | |
5275 | 536 octave_value read (const Array<double>& size, octave_idx_type block_size, |
10313 | 537 oct_data_conv::data_type input_type, |
538 oct_data_conv::data_type output_type, | |
539 octave_idx_type skip, oct_mach_info::float_format flt_fmt, | |
540 octave_idx_type& count); | |
2117 | 541 |
5275 | 542 octave_idx_type write (const octave_value& data, octave_idx_type block_size, |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
543 oct_data_conv::data_type output_type, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
544 octave_idx_type skip, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
545 oct_mach_info::float_format flt_fmt); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
546 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
547 bool write_bytes (const void *data, size_t n_elts); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
548 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
549 bool skip_bytes (size_t n_elts); |
4944 | 550 |
551 template <class T> | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
552 octave_idx_type write (const Array<T>& data, octave_idx_type block_size, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
553 oct_data_conv::data_type output_type, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
554 octave_idx_type skip, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
16892
diff
changeset
|
555 oct_mach_info::float_format flt_fmt); |
2117 | 556 |
4468 | 557 octave_value scanf (const std::string& fmt, const Array<double>& size, |
10313 | 558 octave_idx_type& count, const std::string& who /* = "scanf" */); |
2117 | 559 |
5279 | 560 octave_value scanf (const octave_value& fmt, const Array<double>& size, |
10313 | 561 octave_idx_type& count, const std::string& who /* = "scanf" */); |
5279 | 562 |
4468 | 563 octave_value_list oscanf (const std::string& fmt, |
10313 | 564 const std::string& who /* = "scanf" */); |
2215 | 565 |
5279 | 566 octave_value_list oscanf (const octave_value& fmt, |
10313 | 567 const std::string& who /* = "scanf" */); |
5279 | 568 |
4468 | 569 int printf (const std::string& fmt, const octave_value_list& args, |
10313 | 570 const std::string& who /* = "printf" */); |
2117 | 571 |
5279 | 572 int printf (const octave_value& fmt, const octave_value_list& args, |
10313 | 573 const std::string& who /* = "printf" */); |
5279 | 574 |
4468 | 575 int puts (const std::string& s, const std::string& who /* = "puts" */); |
576 int puts (const octave_value& s, const std::string& who /* = "puts" */); | |
2117 | 577 |
578 bool eof (void) const; | |
579 | |
3523 | 580 std::string error (bool clear, int& err_num); |
2117 | 581 |
3523 | 582 std::string error (bool clear = false) |
2117 | 583 { |
2435 | 584 int err_num; |
585 return error (clear, err_num); | |
2117 | 586 } |
587 | |
4799 | 588 // Set the error message and state. |
589 | |
590 void error (const std::string& msg) | |
591 { | |
592 if (rep) | |
10313 | 593 rep->error (msg); |
4799 | 594 } |
595 | |
596 void error (const char *msg) { error (std::string (msg)); } | |
597 | |
3148 | 598 int file_number (void) { return rep ? rep->file_number () : -1; } |
3145 | 599 |
3340 | 600 bool is_valid (void) const { return (rep != 0); } |
601 | |
2117 | 602 bool ok (void) const { return rep && rep->ok (); } |
603 | |
3145 | 604 operator bool () const { return ok (); } |
2117 | 605 |
3523 | 606 std::string name (void) const; |
2117 | 607 |
3340 | 608 int mode (void) const; |
2117 | 609 |
3340 | 610 oct_mach_info::float_format float_format (void) const; |
2117 | 611 |
3523 | 612 static std::string mode_as_string (int mode); |
2117 | 613 |
6757 | 614 std::istream *input_stream (void) |
615 { | |
616 return rep ? rep->input_stream () : 0; | |
617 } | |
2902 | 618 |
6757 | 619 std::ostream *output_stream (void) |
620 { | |
621 return rep ? rep->output_stream () : 0; | |
622 } | |
16099
4b6c44096862
Backout changeset 238e499c5fea (locale support in scanf)
Rik <rik@octave.org>
parents:
16011
diff
changeset
|
623 |
4889 | 624 void clearerr (void) { if (rep) rep->clearerr (); } |
625 | |
2117 | 626 private: |
627 | |
628 // The actual representation of this stream. | |
629 octave_base_stream *rep; | |
630 | |
5659 | 631 bool stream_ok (bool clear = true) const |
632 { | |
633 bool retval = true; | |
634 | |
635 if (rep) | |
10313 | 636 { |
637 if (clear) | |
638 rep->clear (); | |
639 } | |
5659 | 640 else |
10313 | 641 retval = false; |
5659 | 642 |
643 return retval; | |
644 } | |
4944 | 645 |
646 void invalid_operation (const std::string& who, const char *rw) | |
647 { | |
648 if (rep) | |
10313 | 649 rep->invalid_operation (who, rw); |
4944 | 650 } |
2117 | 651 }; |
652 | |
653 class | |
6109 | 654 OCTINTERP_API |
2117 | 655 octave_stream_list |
656 { | |
657 protected: | |
658 | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
659 octave_stream_list (void) : list (), lookup_cache (list.end ()) { } |
2117 | 660 |
661 public: | |
662 | |
663 ~octave_stream_list (void) { } | |
664 | |
2926 | 665 static bool instance_ok (void); |
666 | |
6757 | 667 static int insert (octave_stream& os); |
2117 | 668 |
4468 | 669 static octave_stream |
670 lookup (int fid, const std::string& who = std::string ()); | |
671 | |
672 static octave_stream | |
673 lookup (const octave_value& fid, const std::string& who = std::string ()); | |
2117 | 674 |
3523 | 675 static int remove (int fid, const std::string& who = std::string ()); |
3341 | 676 static int remove (const octave_value& fid, |
10313 | 677 const std::string& who = std::string ()); |
2117 | 678 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
679 static void clear (bool flush = true); |
2117 | 680 |
681 static string_vector get_info (int fid); | |
682 static string_vector get_info (const octave_value& fid); | |
683 | |
3523 | 684 static std::string list_open_files (void); |
2117 | 685 |
686 static octave_value open_file_numbers (void); | |
687 | |
2609 | 688 static int get_file_number (const octave_value& fid); |
689 | |
2117 | 690 private: |
691 | |
6757 | 692 typedef std::map<int, octave_stream> ostrl_map; |
2117 | 693 |
6757 | 694 ostrl_map list; |
2117 | 695 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
696 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
|
697 |
2117 | 698 static octave_stream_list *instance; |
699 | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
700 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
|
701 |
6757 | 702 int do_insert (octave_stream& os); |
2117 | 703 |
3523 | 704 octave_stream do_lookup (int fid, const std::string& who = std::string ()) const; |
3341 | 705 octave_stream do_lookup (const octave_value& fid, |
10313 | 706 const std::string& who = std::string ()) const; |
2117 | 707 |
3523 | 708 int do_remove (int fid, const std::string& who = std::string ()); |
709 int do_remove (const octave_value& fid, const std::string& who = std::string ()); | |
2117 | 710 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
711 void do_clear (bool flush = true); |
2117 | 712 |
713 string_vector do_get_info (int fid) const; | |
714 string_vector do_get_info (const octave_value& fid) const; | |
715 | |
3523 | 716 std::string do_list_open_files (void) const; |
2117 | 717 |
718 octave_value do_open_file_numbers (void) const; | |
719 | |
2609 | 720 int do_get_file_number (const octave_value& fid) const; |
2117 | 721 }; |
722 | |
723 #endif |