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