Mercurial > octave-nkf
annotate libinterp/corefcn/oct-stream.cc @ 18518:0bdecd41b2dd stable
correctly size fread result (bug #41648)
* oct-stream.cc (octave_base_stream::read): When reading to EOF, don't
add extra column to the result matrix if the number of elements found
is an exact multiple of the number of rows requested.
Avoid mixed signed/unsigned comparisons.
* io.tst: New tests.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 22 Feb 2014 13:06:18 -0500 |
parents | fdd27f68b011 |
children | f958e8cd6348 aa861a98d84d |
rev | line source |
---|---|
2117 | 1 /* |
2 | |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17463
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 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
3268 | 27 #include <cassert> |
7709
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
28 #include <cctype> |
2215 | 29 #include <cstring> |
30 | |
3503 | 31 #include <iomanip> |
9202
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
32 #include <iostream> |
3559 | 33 #include <fstream> |
5765 | 34 #include <sstream> |
3535 | 35 #include <string> |
2117 | 36 |
4944 | 37 #include <Array.h> |
38 | |
39 #include "byte-swap.h" | |
2117 | 40 #include "lo-ieee.h" |
41 #include "lo-mappers.h" | |
42 #include "lo-utils.h" | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
43 #include "quit.h" |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
44 #include "singleton-cleanup.h" |
2117 | 45 #include "str-vec.h" |
46 | |
47 #include "error.h" | |
7352 | 48 #include "gripes.h" |
3342 | 49 #include "input.h" |
3775 | 50 #include "oct-stdstrm.h" |
2117 | 51 #include "oct-stream.h" |
2877 | 52 #include "oct-obj.h" |
2117 | 53 #include "utils.h" |
54 | |
55 // Possible values for conv_err: | |
56 // | |
57 // 1 : not a real scalar | |
2902 | 58 // 2 : value is NaN |
59 // 3 : value is not an integer | |
2117 | 60 |
61 static int | |
62 convert_to_valid_int (const octave_value& tc, int& conv_err) | |
63 { | |
64 int retval = 0; | |
65 | |
66 conv_err = 0; | |
67 | |
2902 | 68 double dval = tc.double_value (); |
69 | |
70 if (! error_state) | |
2117 | 71 { |
5389 | 72 if (! lo_ieee_isnan (dval)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
73 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
74 int ival = NINT (dval); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
75 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
76 if (ival == dval) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
77 retval = ival; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
78 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
79 conv_err = 3; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
80 } |
2117 | 81 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
82 conv_err = 2; |
2117 | 83 } |
84 else | |
85 conv_err = 1; | |
86 | |
87 return retval; | |
88 } | |
89 | |
90 static int | |
4468 | 91 get_size (double d, const std::string& who) |
2117 | 92 { |
93 int retval = -1; | |
94 | |
5389 | 95 if (! lo_ieee_isnan (d)) |
2117 | 96 { |
97 if (! xisinf (d)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
98 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
99 if (d >= 0.0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
100 retval = NINT (d); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
101 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
102 ::error ("%s: negative value invalid as size specification", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
103 who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
104 } |
2117 | 105 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
106 retval = -1; |
2117 | 107 } |
108 else | |
4468 | 109 ::error ("%s: NaN is invalid as size specification", who.c_str ()); |
2117 | 110 |
111 return retval; | |
112 } | |
113 | |
114 static void | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
115 get_size (const Array<double>& size, octave_idx_type& nr, octave_idx_type& nc, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
116 bool& one_elt_size_spec, const std::string& who) |
2117 | 117 { |
118 nr = -1; | |
119 nc = -1; | |
120 | |
3268 | 121 one_elt_size_spec = false; |
122 | |
2117 | 123 double dnr = -1.0; |
124 double dnc = -1.0; | |
125 | |
5275 | 126 octave_idx_type sz_len = size.length (); |
3810 | 127 |
128 if (sz_len == 1) | |
2601 | 129 { |
3268 | 130 one_elt_size_spec = true; |
131 | |
3810 | 132 dnr = size (0); |
4293 | 133 |
134 dnc = (dnr == 0.0) ? 0.0 : 1.0; | |
2601 | 135 } |
3810 | 136 else if (sz_len == 2) |
2117 | 137 { |
3810 | 138 dnr = size (0); |
139 | |
140 if (! xisinf (dnr)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
141 dnc = size (1); |
3810 | 142 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
143 ::error ("%s: invalid size specification", who.c_str ()); |
2117 | 144 } |
145 else | |
4468 | 146 ::error ("%s: invalid size specification", who.c_str ()); |
2117 | 147 |
148 if (! error_state) | |
149 { | |
4468 | 150 nr = get_size (dnr, who); |
2117 | 151 |
3268 | 152 if (! error_state && dnc >= 0.0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
153 nc = get_size (dnc, who); |
2117 | 154 } |
155 } | |
156 | |
3523 | 157 scanf_format_list::scanf_format_list (const std::string& s) |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
158 : nconv (0), curr_idx (0), list (dim_vector (16, 1)), buf (0) |
2117 | 159 { |
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 num_elts = 0; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
161 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
162 size_t n = s.length (); |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
163 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
164 size_t i = 0; |
2117 | 165 |
2215 | 166 int width = 0; |
2117 | 167 bool discard = false; |
168 char modifier = '\0'; | |
169 char type = '\0'; | |
170 | |
171 bool have_more = true; | |
172 | |
173 while (i < n) | |
174 { | |
175 have_more = true; | |
176 | |
177 if (! buf) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
178 buf = new std::ostringstream (); |
2117 | 179 |
180 if (s[i] == '%') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
181 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
182 // Process percent-escape conversion type. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
183 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
184 process_conversion (s, i, n, width, discard, type, modifier, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
185 num_elts); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
186 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
187 have_more = (buf != 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
188 } |
3483 | 189 else if (isspace (s[i])) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
190 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
191 type = scanf_format_elt::whitespace_conversion; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
192 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
193 width = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
194 discard = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
195 modifier = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
196 *buf << " "; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
197 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
198 while (++i < n && isspace (s[i])) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
199 /* skip whitespace */; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
200 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
201 add_elt_to_list (width, discard, type, modifier, num_elts); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
202 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
203 have_more = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
204 } |
3483 | 205 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
206 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
207 type = scanf_format_elt::literal_conversion; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
208 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
209 width = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
210 discard = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
211 modifier = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
212 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
213 while (i < n && ! isspace (s[i]) && s[i] != '%') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
214 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
215 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
216 add_elt_to_list (width, discard, type, modifier, num_elts); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
217 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
218 have_more = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
219 } |
2117 | 220 |
221 if (nconv < 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
222 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
223 have_more = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
224 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
225 } |
2117 | 226 } |
227 | |
228 if (have_more) | |
2215 | 229 add_elt_to_list (width, discard, type, modifier, num_elts); |
2117 | 230 |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
231 list.resize (dim_vector (num_elts, 1)); |
2117 | 232 |
233 delete buf; | |
234 } | |
235 | |
236 scanf_format_list::~scanf_format_list (void) | |
237 { | |
5275 | 238 octave_idx_type n = list.length (); |
239 | |
240 for (octave_idx_type i = 0; i < n; i++) | |
2117 | 241 { |
3340 | 242 scanf_format_elt *elt = list(i); |
2117 | 243 delete elt; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
244 } |
2117 | 245 } |
246 | |
247 void | |
2215 | 248 scanf_format_list::add_elt_to_list (int width, bool discard, char type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
249 char modifier, octave_idx_type& num_elts, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
250 const std::string& char_class) |
2117 | 251 { |
252 if (buf) | |
253 { | |
5765 | 254 std::string text = buf->str (); |
4051 | 255 |
256 if (! text.empty ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
257 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
258 scanf_format_elt *elt |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
259 = new scanf_format_elt (text.c_str (), width, discard, type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
260 modifier, char_class); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
261 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
262 if (num_elts == list.length ()) |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
263 list.resize (dim_vector (2 * num_elts, 1)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
264 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
265 list(num_elts++) = elt; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
266 } |
2117 | 267 |
268 delete buf; | |
269 buf = 0; | |
270 } | |
271 } | |
272 | |
3535 | 273 static std::string |
3523 | 274 expand_char_class (const std::string& s) |
3483 | 275 { |
3523 | 276 std::string retval; |
3483 | 277 |
278 size_t len = s.length (); | |
279 | |
280 size_t i = 0; | |
281 | |
282 while (i < len) | |
283 { | |
284 unsigned char c = s[i++]; | |
285 | |
286 if (c == '-' && i > 1 && i < len | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
287 && ( static_cast<unsigned char> (s[i-2]) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
288 <= static_cast<unsigned char> (s[i]))) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
289 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
290 // Add all characters from the range except the first (we |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
291 // already added it below). |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
292 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
293 for (c = s[i-2]+1; c < s[i]; c++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
294 retval += c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
295 } |
3483 | 296 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
297 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
298 // Add the character to the class. Only add '-' if it is |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
299 // the last character in the class. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
300 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
301 if (c != '-' || i == len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
302 retval += c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
303 } |
3483 | 304 } |
305 | |
306 return retval; | |
307 } | |
308 | |
2117 | 309 void |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
310 scanf_format_list::process_conversion (const std::string& s, size_t& i, |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
311 size_t n, int& width, bool& discard, |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
312 char& type, char& modifier, |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
313 octave_idx_type& num_elts) |
2117 | 314 { |
2215 | 315 width = 0; |
2117 | 316 discard = false; |
317 modifier = '\0'; | |
318 type = '\0'; | |
319 | |
320 *buf << s[i++]; | |
321 | |
322 bool have_width = false; | |
323 | |
324 while (i < n) | |
325 { | |
326 switch (s[i]) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
327 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
328 case '*': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
329 if (discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
330 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
331 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
332 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
333 discard = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
334 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
335 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
336 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
337 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
338 case '0': case '1': case '2': case '3': case '4': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
339 case '5': case '6': case '7': case '8': case '9': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
340 if (have_width) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
341 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
342 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
343 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
344 char c = s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
345 width = width * 10 + c - '0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
346 have_width = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
347 *buf << c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
348 while (i < n && isdigit (s[i])) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
349 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
350 c = s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
351 width = width * 10 + c - '0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
352 *buf << c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
353 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
354 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
355 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
356 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
357 case 'h': case 'l': case 'L': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
358 if (modifier != '\0') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
359 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
360 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
361 modifier = s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
362 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
363 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
364 case 'd': case 'i': case 'o': case 'u': case 'x': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
365 if (modifier == 'L') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
366 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
367 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
368 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
369 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
370 goto fini; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
371 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
372 case 'e': case 'f': case 'g': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
373 if (modifier == 'h') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
374 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
375 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
376 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
377 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
378 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
379 // No float or long double conversions, thanks. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
380 *buf << 'l'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
381 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
382 goto fini; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
383 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
384 case 'c': case 's': case 'p': case '%': case '[': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
385 if (modifier != '\0') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
386 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
387 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
388 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
389 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
390 goto fini; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
391 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
392 fini: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
393 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
394 if (finish_conversion (s, i, n, width, discard, type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
395 modifier, num_elts) == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
396 return; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
397 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
398 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
399 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
400 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
401 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
402 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
403 } |
2117 | 404 |
405 if (nconv < 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
406 break; |
2117 | 407 } |
408 | |
409 nconv = -1; | |
410 } | |
411 | |
412 int | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
413 scanf_format_list::finish_conversion (const std::string& s, size_t& i, |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
414 size_t n, int& width, bool discard, |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
415 char& type, char modifier, |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
416 octave_idx_type& num_elts) |
2117 | 417 { |
418 int retval = 0; | |
419 | |
3523 | 420 std::string char_class; |
3483 | 421 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
422 size_t beg_idx = std::string::npos; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
423 size_t end_idx = std::string::npos; |
3640 | 424 |
2117 | 425 if (s[i] == '%') |
3640 | 426 { |
427 type = '%'; | |
428 *buf << s[i++]; | |
429 } | |
2117 | 430 else |
431 { | |
432 type = s[i]; | |
433 | |
434 if (s[i] == '[') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
435 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
436 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
437 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
438 if (i < n) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
439 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
440 beg_idx = i; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
441 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
442 if (s[i] == '^') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
443 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
444 type = '^'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
445 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
446 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
447 if (i < n) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
448 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
449 beg_idx = i; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
450 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
451 if (s[i] == ']') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
452 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
453 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
454 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
455 else if (s[i] == ']') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
456 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
457 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
458 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
459 while (i < n && s[i] != ']') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
460 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
461 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
462 if (i < n && s[i] == ']') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
463 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
464 end_idx = i-1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
465 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
466 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
467 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
468 if (s[i-1] != ']') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
469 retval = nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
470 } |
2117 | 471 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
472 *buf << s[i++]; |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
473 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
474 nconv++; |
3640 | 475 } |
476 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
477 if (nconv >= 0) |
3640 | 478 { |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
479 if (beg_idx != std::string::npos && end_idx != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
480 char_class = expand_char_class (s.substr (beg_idx, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
481 end_idx - beg_idx + 1)); |
3640 | 482 |
483 add_elt_to_list (width, discard, type, modifier, num_elts, char_class); | |
2117 | 484 } |
485 | |
486 return retval; | |
487 } | |
488 | |
489 void | |
490 scanf_format_list::printme (void) const | |
491 { | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
492 octave_idx_type n = list.length (); |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
493 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
494 for (octave_idx_type i = 0; i < n; i++) |
2117 | 495 { |
3340 | 496 scanf_format_elt *elt = list(i); |
2117 | 497 |
3531 | 498 std::cerr |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
499 << "width: " << elt->width << "\n" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
500 << "discard: " << elt->discard << "\n" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
501 << "type: "; |
3483 | 502 |
503 if (elt->type == scanf_format_elt::literal_conversion) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
504 std::cerr << "literal text\n"; |
3483 | 505 else if (elt->type == scanf_format_elt::whitespace_conversion) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
506 std::cerr << "whitespace\n"; |
3483 | 507 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
508 std::cerr << elt->type << "\n"; |
3531 | 509 |
510 std::cerr | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
511 << "modifier: " << elt->modifier << "\n" |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
512 << "char_class: '" << undo_string_escapes (elt->char_class) << "'\n" |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
513 << "text: '" << undo_string_escapes (elt->text) << "'\n\n"; |
2117 | 514 } |
515 } | |
516 | |
517 bool | |
518 scanf_format_list::all_character_conversions (void) | |
519 { | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
520 octave_idx_type n = list.length (); |
2117 | 521 |
522 if (n > 0) | |
523 { | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
524 for (octave_idx_type i = 0; i < n; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
525 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
526 scanf_format_elt *elt = list(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
527 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
528 switch (elt->type) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
529 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
530 case 'c': case 's': case '%': case '[': case '^': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
531 case scanf_format_elt::literal_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
532 case scanf_format_elt::whitespace_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
533 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
534 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
535 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
536 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
537 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
538 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
539 } |
2117 | 540 |
541 return true; | |
542 } | |
543 else | |
544 return false; | |
545 } | |
546 | |
547 bool | |
548 scanf_format_list::all_numeric_conversions (void) | |
549 { | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
550 octave_idx_type n = list.length (); |
2117 | 551 |
552 if (n > 0) | |
553 { | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
554 for (octave_idx_type i = 0; i < n; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
555 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
556 scanf_format_elt *elt = list(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
557 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
558 switch (elt->type) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
559 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
560 case 'd': case 'i': case 'o': case 'u': case 'x': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
561 case 'e': case 'f': case 'g': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
562 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
563 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
564 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
565 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
566 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
567 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
568 } |
2117 | 569 |
570 return true; | |
571 } | |
572 else | |
573 return false; | |
574 } | |
575 | |
576 // Ugh again. | |
577 | |
3523 | 578 printf_format_list::printf_format_list (const std::string& s) |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
579 : nconv (0), curr_idx (0), list (dim_vector (16, 1)), buf (0) |
2117 | 580 { |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
581 octave_idx_type num_elts = 0; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
582 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
583 size_t n = s.length (); |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
584 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
585 size_t i = 0; |
2117 | 586 |
587 int args = 0; | |
3643 | 588 std::string flags; |
3640 | 589 int fw = 0; |
590 int prec = 0; | |
2117 | 591 char modifier = '\0'; |
592 char type = '\0'; | |
593 | |
594 bool have_more = true; | |
3640 | 595 bool empty_buf = true; |
2117 | 596 |
4223 | 597 if (n == 0) |
2117 | 598 { |
4223 | 599 printf_format_elt *elt |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
600 = new printf_format_elt ("", args, fw, prec, flags, type, modifier); |
4223 | 601 |
602 list(num_elts++) = elt; | |
603 | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
604 list.resize (dim_vector (num_elts, 1)); |
4223 | 605 } |
606 else | |
607 { | |
608 while (i < n) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
609 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
610 have_more = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
611 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
612 if (! buf) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
613 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
614 buf = new std::ostringstream (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
615 empty_buf = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
616 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
617 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
618 switch (s[i]) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
619 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
620 case '%': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
621 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
622 if (empty_buf) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
623 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
624 process_conversion (s, i, n, args, flags, fw, prec, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
625 type, modifier, num_elts); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
626 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
627 have_more = (buf != 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
628 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
629 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
630 add_elt_to_list (args, flags, fw, prec, type, modifier, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
631 num_elts); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
632 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
633 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
634 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
635 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
636 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
637 args = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
638 flags = ""; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
639 fw = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
640 prec = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
641 modifier = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
642 type = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
643 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
644 empty_buf = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
645 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
646 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
647 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
648 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
649 if (nconv < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
650 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
651 have_more = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
652 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
653 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
654 } |
2117 | 655 |
4223 | 656 if (have_more) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
657 add_elt_to_list (args, flags, fw, prec, type, modifier, num_elts); |
4223 | 658 |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
659 list.resize (dim_vector (num_elts, 1)); |
4223 | 660 |
661 delete buf; | |
2117 | 662 } |
663 } | |
664 | |
665 printf_format_list::~printf_format_list (void) | |
666 { | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
667 octave_idx_type n = list.length (); |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
668 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
669 for (octave_idx_type i = 0; i < n; i++) |
2117 | 670 { |
3340 | 671 printf_format_elt *elt = list(i); |
2117 | 672 delete elt; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
673 } |
2117 | 674 } |
675 | |
676 void | |
3640 | 677 printf_format_list::add_elt_to_list (int args, const std::string& flags, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
678 int fw, int prec, char type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
679 char modifier, octave_idx_type& num_elts) |
2117 | 680 { |
681 if (buf) | |
682 { | |
5765 | 683 std::string text = buf->str (); |
4051 | 684 |
685 if (! text.empty ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
686 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
687 printf_format_elt *elt |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
688 = new printf_format_elt (text.c_str (), args, fw, prec, flags, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
689 type, modifier); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
690 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
691 if (num_elts == list.length ()) |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
692 list.resize (dim_vector (2 * num_elts, 1)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
693 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
694 list(num_elts++) = elt; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
695 } |
2117 | 696 |
697 delete buf; | |
698 buf = 0; | |
699 } | |
700 } | |
701 | |
702 void | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
703 printf_format_list::process_conversion (const std::string& s, size_t& i, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
704 size_t n, int& args, std::string& flags, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
705 int& fw, int& prec, char& modifier, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
706 char& type, octave_idx_type& num_elts) |
2117 | 707 { |
708 args = 0; | |
3640 | 709 flags = ""; |
710 fw = 0; | |
711 prec = 0; | |
2117 | 712 modifier = '\0'; |
713 type = '\0'; | |
714 | |
715 *buf << s[i++]; | |
716 | |
4587 | 717 bool nxt = false; |
2117 | 718 |
719 while (i < n) | |
720 { | |
721 switch (s[i]) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
722 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
723 case '-': case '+': case ' ': case '0': case '#': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
724 flags += s[i]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
725 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
726 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
727 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
728 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
729 nxt = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
730 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
731 } |
2117 | 732 |
4587 | 733 if (nxt) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
734 break; |
2117 | 735 } |
736 | |
737 if (i < n) | |
738 { | |
739 if (s[i] == '*') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
740 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
741 fw = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
742 args++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
743 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
744 } |
2117 | 745 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
746 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
747 if (isdigit (s[i])) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
748 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
749 int nn = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
750 std::string tmp = s.substr (i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
751 sscanf (tmp.c_str (), "%d%n", &fw, &nn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
752 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
753 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
754 while (i < n && isdigit (s[i])) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
755 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
756 } |
2117 | 757 } |
758 | |
759 if (i < n && s[i] == '.') | |
760 { | |
761 *buf << s[i++]; | |
762 | |
763 if (i < n) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
764 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
765 if (s[i] == '*') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
766 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
767 prec = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
768 args++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
769 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
770 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
771 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
772 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
773 if (isdigit (s[i])) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
774 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
775 int nn = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
776 std::string tmp = s.substr (i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
777 sscanf (tmp.c_str (), "%d%n", &prec, &nn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
778 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
779 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
780 while (i < n && isdigit (s[i])) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
781 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
782 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
783 } |
2117 | 784 } |
785 | |
786 if (i < n) | |
787 { | |
788 switch (s[i]) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
789 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
790 case 'h': case 'l': case 'L': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
791 modifier = s[i]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
792 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
793 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
794 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
795 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
796 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
797 } |
2117 | 798 } |
799 | |
800 if (i < n) | |
3640 | 801 finish_conversion (s, i, args, flags, fw, prec, modifier, type, num_elts); |
2117 | 802 else |
803 nconv = -1; | |
804 } | |
805 | |
806 void | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
807 printf_format_list::finish_conversion (const std::string& s, size_t& i, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
808 int args, const std::string& flags, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
809 int fw, int prec, char modifier, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
810 char& type, octave_idx_type& num_elts) |
2117 | 811 { |
812 switch (s[i]) | |
813 { | |
814 case 'd': case 'i': case 'o': case 'x': case 'X': | |
815 case 'u': case 'c': | |
816 if (modifier == 'L') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
817 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
818 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
819 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
820 } |
2117 | 821 goto fini; |
822 | |
823 case 'f': case 'e': case 'E': case 'g': case 'G': | |
824 if (modifier == 'h' || modifier == 'l') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
825 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
826 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
827 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
828 } |
2117 | 829 goto fini; |
830 | |
831 case 's': case 'p': case '%': | |
832 if (modifier != '\0') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
833 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
834 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
835 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
836 } |
2117 | 837 goto fini; |
838 | |
839 fini: | |
840 | |
3640 | 841 type = s[i]; |
842 | |
843 *buf << s[i++]; | |
844 | |
845 if (type != '%' || args != 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
846 nconv++; |
3640 | 847 |
848 if (type != '%') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
849 args++; |
3640 | 850 |
851 add_elt_to_list (args, flags, fw, prec, type, modifier, num_elts); | |
852 | |
2117 | 853 break; |
854 | |
855 default: | |
856 nconv = -1; | |
857 break; | |
858 } | |
859 } | |
860 | |
861 void | |
862 printf_format_list::printme (void) const | |
863 { | |
864 int n = list.length (); | |
865 | |
866 for (int i = 0; i < n; i++) | |
867 { | |
3340 | 868 printf_format_elt *elt = list(i); |
2117 | 869 |
3640 | 870 std::cerr |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
871 << "args: " << elt->args << "\n" |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
872 << "flags: '" << elt->flags << "'\n" |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
873 << "width: " << elt->fw << "\n" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
874 << "prec: " << elt->prec << "\n" |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
875 << "type: '" << elt->type << "'\n" |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
876 << "modifier: '" << elt->modifier << "'\n" |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
877 << "text: '" << undo_string_escapes (elt->text) << "'\n\n"; |
2117 | 878 } |
879 } | |
880 | |
881 void | |
3523 | 882 octave_base_stream::error (const std::string& msg) |
2117 | 883 { |
884 fail = true; | |
885 errmsg = msg; | |
886 } | |
887 | |
888 void | |
4468 | 889 octave_base_stream::error (const std::string& who, const std::string& msg) |
890 { | |
891 fail = true; | |
6296 | 892 errmsg = who + ": " + msg; |
4468 | 893 } |
894 | |
895 void | |
2117 | 896 octave_base_stream::clear (void) |
897 { | |
4889 | 898 fail = false; |
899 errmsg = ""; | |
900 } | |
901 | |
902 void | |
903 octave_base_stream::clearerr (void) | |
904 { | |
4888 | 905 std::istream *is = input_stream (); |
906 std::ostream *os = output_stream (); | |
907 | |
908 if (is) | |
909 is->clear (); | |
910 | |
911 if (os) | |
912 os->clear (); | |
2117 | 913 } |
914 | |
915 // Functions that are defined for all input streams (input streams | |
916 // are those that define is). | |
917 | |
3536 | 918 std::string |
5275 | 919 octave_base_stream::do_gets (octave_idx_type max_len, bool& err, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
920 bool strip_newline, const std::string& who) |
2117 | 921 { |
3523 | 922 std::string retval; |
2117 | 923 |
8773
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
924 if ((interactive || forced_interactive) && file_number () == 0) |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
925 { |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
926 ::error ("%s: unable to read from stdin while running interactively", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
927 who.c_str ()); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
928 |
8773
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
929 return retval; |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
930 } |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
931 |
2117 | 932 err = false; |
933 | |
3523 | 934 std::istream *isp = input_stream (); |
2117 | 935 |
936 if (isp) | |
937 { | |
3523 | 938 std::istream& is = *isp; |
2117 | 939 |
5765 | 940 std::ostringstream buf; |
2117 | 941 |
942 int c = 0; | |
3553 | 943 int char_count = 0; |
6345 | 944 |
945 if (max_len != 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
946 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
947 while (is && (c = is.get ()) != EOF) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
948 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
949 char_count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
950 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
951 // Handle CRLF, CR, or LF as line ending. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
952 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
953 if (c == '\r') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
954 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
955 if (! strip_newline) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
956 buf << static_cast<char> (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
957 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
958 c = is.get (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
959 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
960 if (c != EOF) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
961 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
962 if (c == '\n') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
963 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
964 char_count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
965 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
966 if (! strip_newline) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
967 buf << static_cast<char> (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
968 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
969 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
970 is.putback (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
971 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
972 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
973 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
974 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
975 else if (c == '\n') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
976 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
977 if (! strip_newline) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
978 buf << static_cast<char> (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
979 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
980 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
981 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
982 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
983 buf << static_cast<char> (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
984 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
985 if (max_len > 0 && char_count == max_len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
986 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
987 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
988 } |
6345 | 989 |
990 if (! is.eof () && char_count > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
991 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
992 // GAGME. Matlab seems to check for EOF even if the last |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
993 // character in a file is a newline character. This is NOT |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
994 // what the corresponding C-library functions do. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
995 int disgusting_compatibility_hack = is.get (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
996 if (! is.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
997 is.putback (disgusting_compatibility_hack); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
998 } |
2117 | 999 |
4224 | 1000 if (is.good () || (is.eof () && char_count > 0)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1001 retval = buf.str (); |
4224 | 1002 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1003 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1004 err = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1005 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1006 if (is.eof () && char_count == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1007 error (who, "at end of file"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1008 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1009 error (who, "read error"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1010 } |
2117 | 1011 } |
1012 else | |
1013 { | |
1014 err = true; | |
4468 | 1015 invalid_operation (who, "reading"); |
2117 | 1016 } |
1017 | |
1018 return retval; | |
1019 } | |
1020 | |
3536 | 1021 std::string |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1022 octave_base_stream::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
|
1023 const std::string& who) |
2117 | 1024 { |
4468 | 1025 return do_gets (max_len, err, true, who); |
2117 | 1026 } |
1027 | |
3536 | 1028 std::string |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1029 octave_base_stream::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
|
1030 const std::string& who) |
2117 | 1031 { |
4468 | 1032 return do_gets (max_len, err, false, who); |
2117 | 1033 } |
1034 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
1035 off_t |
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
1036 octave_base_stream::skipl (off_t num, bool& err, const std::string& who) |
9701 | 1037 { |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
1038 off_t cnt = -1; |
9701 | 1039 |
1040 if ((interactive || forced_interactive) && file_number () == 0) | |
1041 { | |
1042 ::error ("%s: unable to read from stdin while running interactively", | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1043 who.c_str ()); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1044 |
9701 | 1045 return count; |
1046 } | |
1047 | |
1048 err = false; | |
1049 | |
1050 std::istream *isp = input_stream (); | |
1051 | |
1052 if (isp) | |
1053 { | |
1054 std::istream& is = *isp; | |
1055 | |
1056 int c = 0, lastc = -1; | |
1057 cnt = 0; | |
1058 | |
1059 while (is && (c = is.get ()) != EOF) | |
1060 { | |
1061 // Handle CRLF, CR, or LF as line ending. | |
1062 | |
1063 if (c == '\r' || (c == '\n' && lastc != '\r')) | |
1064 { | |
1065 if (++cnt == num) | |
1066 break; | |
1067 } | |
1068 | |
1069 lastc = c; | |
1070 } | |
1071 | |
1072 // Maybe eat the following \n if \r was just met. | |
1073 if (c == '\r' && is.peek () == '\n') | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1074 is.get (); |
9701 | 1075 |
1076 if (is.bad ()) | |
1077 { | |
1078 err = true; | |
1079 error (who, "read error"); | |
1080 } | |
1081 | |
1082 if (err) | |
1083 cnt = -1; | |
1084 } | |
1085 else | |
1086 { | |
1087 err = true; | |
1088 invalid_operation (who, "reading"); | |
1089 } | |
1090 | |
1091 return cnt; | |
1092 } | |
1093 | |
3640 | 1094 #define OCTAVE_SCAN(is, fmt, arg) octave_scan (is, fmt, arg) |
3636 | 1095 |
1096 template <class T> | |
1097 std::istream& | |
6767 | 1098 octave_scan_1 (std::istream& is, const scanf_format_elt& fmt, T* valptr) |
3636 | 1099 { |
3779 | 1100 T& ref = *valptr; |
1101 | |
1102 switch (fmt.type) | |
1103 { | |
1104 case 'o': | |
4926 | 1105 is >> std::oct >> ref >> std::dec; |
3779 | 1106 break; |
1107 | |
1108 case 'x': | |
4926 | 1109 is >> std::hex >> ref >> std::dec; |
1110 break; | |
1111 | |
1112 case 'i': | |
1113 { | |
14675
757f729fd41d
skip leading whitespace for scanf %i format
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
1114 int c1 = EOF; |
757f729fd41d
skip leading whitespace for scanf %i format
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
1115 |
757f729fd41d
skip leading whitespace for scanf %i format
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
1116 while (is && (c1 = is.get ()) != EOF && isspace (c1)) |
757f729fd41d
skip leading whitespace for scanf %i format
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
1117 /* skip whitespace */; |
757f729fd41d
skip leading whitespace for scanf %i format
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
1118 |
757f729fd41d
skip leading whitespace for scanf %i format
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
1119 if (c1 != EOF) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1120 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1121 if (c1 == '0') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1122 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1123 int c2 = is.peek (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1124 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1125 if (c2 == 'x' || c2 == 'X') |
7709
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1126 { |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1127 is.ignore (); |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1128 if (std::isxdigit (is.peek ())) |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1129 is >> std::hex >> ref >> std::dec; |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1130 else |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1131 ref = 0; |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1132 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1133 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1134 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1135 if (c2 == '0' || c2 == '1' || c2 == '2' |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1136 || c2 == '3' || c2 == '4' || c2 == '5' |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1137 || c2 == '6' || c2 == '7') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1138 is >> std::oct >> ref >> std::dec; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1139 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1140 ref = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1141 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1142 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1143 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1144 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1145 is.putback (c1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1146 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1147 is >> ref; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1148 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1149 } |
4926 | 1150 } |
3779 | 1151 break; |
1152 | |
1153 default: | |
1154 is >> ref; | |
1155 break; | |
1156 } | |
3639 | 1157 |
3638 | 1158 return is; |
3636 | 1159 } |
1160 | |
6767 | 1161 template <class T> |
1162 std::istream& | |
1163 octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr) | |
1164 { | |
1165 if (fmt.width) | |
1166 { | |
1167 // Limit input to fmt.width characters by reading into a | |
1168 // temporary stringstream buffer. | |
1169 | |
1170 std::string tmp; | |
1171 | |
1172 is.width (fmt.width); | |
1173 is >> tmp; | |
1174 | |
1175 std::istringstream ss (tmp); | |
1176 | |
1177 octave_scan_1 (ss, fmt, valptr); | |
1178 } | |
1179 else | |
1180 octave_scan_1 (is, fmt, valptr); | |
1181 | |
1182 return is; | |
1183 } | |
1184 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1185 // Note that this specialization is only used for reading characters, not |
3779 | 1186 // character strings. See BEGIN_S_CONVERSION for details. |
1187 | |
1188 template<> | |
1189 std::istream& | |
4661 | 1190 octave_scan<> (std::istream& is, const scanf_format_elt& /* fmt */, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1191 char* valptr) |
3779 | 1192 { |
1193 return is >> valptr; | |
1194 } | |
3636 | 1195 |
5403 | 1196 template<> |
5176 | 1197 std::istream& |
5403 | 1198 octave_scan<> (std::istream& is, const scanf_format_elt& fmt, double* valptr) |
5176 | 1199 { |
1200 double& ref = *valptr; | |
1201 | |
1202 switch (fmt.type) | |
1203 { | |
1204 case 'e': | |
1205 case 'f': | |
1206 case 'g': | |
1207 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1208 int c1 = EOF; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1209 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1210 while (is && (c1 = is.get ()) != EOF && isspace (c1)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1211 /* skip whitespace */; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1212 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1213 if (c1 != EOF) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1214 { |
12936
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1215 is.putback (c1); |
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1216 |
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1217 ref = octave_read_value<double> (is); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1218 } |
5176 | 1219 } |
1220 break; | |
1221 | |
1222 default: | |
1223 panic_impossible (); | |
1224 break; | |
1225 } | |
1226 | |
1227 return is; | |
1228 } | |
1229 | |
2572 | 1230 template <class T> |
1231 void | |
3636 | 1232 do_scanf_conv (std::istream& is, const scanf_format_elt& fmt, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1233 T valptr, Matrix& mval, double *data, octave_idx_type& idx, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1234 octave_idx_type& conversion_count, octave_idx_type nr, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1235 octave_idx_type max_size, bool discard) |
2572 | 1236 { |
3640 | 1237 OCTAVE_SCAN (is, fmt, valptr); |
2572 | 1238 |
1239 if (is) | |
1240 { | |
1241 if (idx == max_size && ! discard) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1242 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1243 max_size *= 2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1244 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1245 if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1246 mval.resize (nr, max_size / nr, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1247 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1248 mval.resize (max_size, 1, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1249 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1250 data = mval.fortran_vec (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1251 } |
2572 | 1252 |
1253 if (! discard) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1254 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1255 conversion_count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1256 data[idx++] = *(valptr); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1257 } |
2572 | 1258 } |
1259 } | |
1260 | |
1261 template void | |
3636 | 1262 do_scanf_conv (std::istream&, const scanf_format_elt&, double*, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1263 Matrix&, double*, octave_idx_type&, octave_idx_type&, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1264 octave_idx_type, octave_idx_type, bool); |
2572 | 1265 |
3483 | 1266 #define DO_WHITESPACE_CONVERSION() \ |
1267 do \ | |
1268 { \ | |
1269 int c = EOF; \ | |
1270 \ | |
1271 while (is && (c = is.get ()) != EOF && isspace (c)) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1272 /* skip whitespace */; \ |
3483 | 1273 \ |
1274 if (c != EOF) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1275 is.putback (c); \ |
3483 | 1276 } \ |
1277 while (0) | |
1278 | |
1279 #define DO_LITERAL_CONVERSION() \ | |
1280 do \ | |
1281 { \ | |
1282 int c = EOF; \ | |
1283 \ | |
1284 int n = strlen (fmt); \ | |
1285 int i = 0; \ | |
1286 \ | |
1287 while (i < n && is && (c = is.get ()) != EOF) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1288 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1289 if (c == static_cast<unsigned char> (fmt[i])) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1290 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1291 i++; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1292 continue; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1293 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1294 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1295 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1296 is.putback (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1297 break; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1298 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1299 } \ |
3483 | 1300 \ |
1301 if (i != n) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1302 is.setstate (std::ios::failbit); \ |
3483 | 1303 } \ |
1304 while (0) | |
1305 | |
3640 | 1306 #define DO_PCT_CONVERSION() \ |
1307 do \ | |
1308 { \ | |
1309 int c = is.get (); \ | |
1310 \ | |
1311 if (c != EOF) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1312 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1313 if (c != '%') \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1314 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1315 is.putback (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1316 is.setstate (std::ios::failbit); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1317 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1318 } \ |
3640 | 1319 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1320 is.setstate (std::ios::failbit); \ |
3640 | 1321 } \ |
1322 while (0) | |
1323 | |
3483 | 1324 #define BEGIN_C_CONVERSION() \ |
3538 | 1325 is.unsetf (std::ios::skipws); \ |
3483 | 1326 \ |
1327 int width = elt->width ? elt->width : 1; \ | |
1328 \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1329 std::string tmp (width, '\0'); \ |
3483 | 1330 \ |
1331 int c = EOF; \ | |
1332 int n = 0; \ | |
1333 \ | |
1334 while (is && n < width && (c = is.get ()) != EOF) \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1335 tmp[n++] = static_cast<char> (c); \ |
3483 | 1336 \ |
5266 | 1337 if (n > 0 && c == EOF) \ |
12984
7626f8934466
correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents:
12966
diff
changeset
|
1338 is.clear (); \ |
7626f8934466
correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents:
12966
diff
changeset
|
1339 \ |
7626f8934466
correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents:
12966
diff
changeset
|
1340 tmp.resize (n) |
3483 | 1341 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
1342 // For a '%s' format, skip initial whitespace and then read until the |
5338 | 1343 // next whitespace character or until WIDTH characters have been read. |
3483 | 1344 #define BEGIN_S_CONVERSION() \ |
1345 int width = elt->width; \ | |
1346 \ | |
4051 | 1347 std::string tmp; \ |
3483 | 1348 \ |
1349 do \ | |
1350 { \ | |
1351 if (width) \ | |
5338 | 1352 { \ |
10076 | 1353 tmp = std::string (width, '\0'); \ |
5338 | 1354 \ |
1355 int c = EOF; \ | |
1356 \ | |
1357 int n = 0; \ | |
1358 \ | |
1359 while (is && (c = is.get ()) != EOF) \ | |
1360 { \ | |
1361 if (! isspace (c)) \ | |
1362 { \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1363 tmp[n++] = static_cast<char> (c); \ |
5338 | 1364 break; \ |
1365 } \ | |
1366 } \ | |
4051 | 1367 \ |
5338 | 1368 while (is && n < width && (c = is.get ()) != EOF) \ |
1369 { \ | |
1370 if (isspace (c)) \ | |
1371 { \ | |
1372 is.putback (c); \ | |
1373 break; \ | |
1374 } \ | |
1375 else \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1376 tmp[n++] = static_cast<char> (c); \ |
5338 | 1377 } \ |
3483 | 1378 \ |
5338 | 1379 if (n > 0 && c == EOF) \ |
1380 is.clear (); \ | |
1381 \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1382 tmp.resize (n); \ |
5338 | 1383 } \ |
3483 | 1384 else \ |
5338 | 1385 { \ |
1386 is >> std::ws >> tmp; \ | |
1387 } \ | |
3483 | 1388 } \ |
1389 while (0) | |
1390 | |
1391 // This format must match a nonempty sequence of characters. | |
1392 #define BEGIN_CHAR_CLASS_CONVERSION() \ | |
1393 int width = elt->width; \ | |
1394 \ | |
4051 | 1395 std::string tmp; \ |
3483 | 1396 \ |
1397 do \ | |
1398 { \ | |
7426 | 1399 if (! width) \ |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
1400 width = std::numeric_limits<int>::max (); \ |
7427 | 1401 \ |
7426 | 1402 std::ostringstream buf; \ |
1403 \ | |
1404 std::string char_class = elt->char_class; \ | |
4051 | 1405 \ |
7426 | 1406 int c = EOF; \ |
3483 | 1407 \ |
7426 | 1408 if (elt->type == '[') \ |
1409 { \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1410 int chars_read = 0; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1411 while (is && chars_read++ < width && (c = is.get ()) != EOF \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1412 && char_class.find (c) != std::string::npos) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1413 buf << static_cast<char> (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1414 } \ |
3483 | 1415 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1416 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1417 int chars_read = 0; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1418 while (is && chars_read++ < width && (c = is.get ()) != EOF \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1419 && char_class.find (c) == std::string::npos) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1420 buf << static_cast<char> (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1421 } \ |
3483 | 1422 \ |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
1423 if (width == std::numeric_limits<int>::max () && c != EOF) \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1424 is.putback (c); \ |
3483 | 1425 \ |
7426 | 1426 tmp = buf.str (); \ |
3483 | 1427 \ |
7426 | 1428 if (tmp.empty ()) \ |
1429 is.setstate (std::ios::failbit); \ | |
10969
da355a1a6d44
fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
1430 else if (c == EOF) \ |
da355a1a6d44
fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
1431 is.clear (); \ |
da355a1a6d44
fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
1432 \ |
3483 | 1433 } \ |
1434 while (0) | |
1435 | |
3410 | 1436 #define FINISH_CHARACTER_CONVERSION() \ |
1437 do \ | |
1438 { \ | |
4051 | 1439 width = tmp.length (); \ |
3410 | 1440 \ |
1441 if (is) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1442 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1443 int i = 0; \ |
3410 | 1444 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1445 if (! discard) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1446 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1447 conversion_count++; \ |
3410 | 1448 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1449 while (i < width) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1450 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1451 if (data_index == max_size) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1452 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1453 max_size *= 2; \ |
3410 | 1454 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1455 if (all_char_conv) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1456 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1457 if (one_elt_size_spec) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1458 mval.resize (1, max_size, 0.0); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1459 else if (nr > 0) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1460 mval.resize (nr, max_size / nr, 0.0); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1461 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1462 panic_impossible (); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1463 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1464 else if (nr > 0) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1465 mval.resize (nr, max_size / nr, 0.0); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1466 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1467 mval.resize (max_size, 1, 0.0); \ |
3410 | 1468 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1469 data = mval.fortran_vec (); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1470 } \ |
3410 | 1471 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1472 data[data_index++] = tmp[i++]; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1473 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1474 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1475 } \ |
3410 | 1476 } \ |
1477 while (0) | |
2117 | 1478 |
1479 octave_value | |
1480 octave_base_stream::do_scanf (scanf_format_list& fmt_list, | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1481 octave_idx_type nr, octave_idx_type nc, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1482 bool one_elt_size_spec, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1483 octave_idx_type& conversion_count, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1484 const std::string& who) |
2117 | 1485 { |
8773
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1486 octave_value retval = Matrix (); |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1487 |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1488 if ((interactive || forced_interactive) && file_number () == 0) |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1489 { |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1490 ::error ("%s: unable to read from stdin while running interactively", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1491 who.c_str ()); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1492 |
8773
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1493 return retval; |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1494 } |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1495 |
3268 | 1496 conversion_count = 0; |
1497 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1498 octave_idx_type nconv = fmt_list.num_conversions (); |
3640 | 1499 |
5275 | 1500 octave_idx_type data_index = 0; |
2121 | 1501 |
3268 | 1502 if (nr == 0 || nc == 0) |
1503 { | |
1504 if (one_elt_size_spec) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1505 nc = 0; |
3268 | 1506 |
1507 return Matrix (nr, nc, 0.0); | |
1508 } | |
1509 | |
3523 | 1510 std::istream *isp = input_stream (); |
2117 | 1511 |
1512 bool all_char_conv = fmt_list.all_character_conversions (); | |
1513 | |
1514 Matrix mval; | |
1515 double *data = 0; | |
5275 | 1516 octave_idx_type max_size = 0; |
1517 octave_idx_type max_conv = 0; | |
1518 | |
1519 octave_idx_type final_nr = 0; | |
1520 octave_idx_type final_nc = 0; | |
2117 | 1521 |
3268 | 1522 if (all_char_conv) |
1523 { | |
4420 | 1524 // Any of these could be resized later (if we have %s |
1525 // conversions, we may read more than one element for each | |
1526 // conversion). | |
1527 | |
3268 | 1528 if (one_elt_size_spec) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1529 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1530 max_size = 512; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1531 mval.resize (1, max_size, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1532 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1533 if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1534 max_conv = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1535 } |
4420 | 1536 else if (nr > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1537 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1538 if (nc > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1539 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1540 mval.resize (nr, nc, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1541 max_size = max_conv = nr * nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1542 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1543 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1544 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1545 mval.resize (nr, 32, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1546 max_size = nr * 32; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1547 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1548 } |
4420 | 1549 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1550 panic_impossible (); |
3268 | 1551 } |
1552 else if (nr > 0) | |
2117 | 1553 { |
1554 if (nc > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1555 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1556 // Will not resize later. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1557 mval.resize (nr, nc, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1558 max_size = nr * nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1559 max_conv = max_size; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1560 } |
2117 | 1561 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1562 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1563 // Maybe resize later. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1564 mval.resize (nr, 32, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1565 max_size = nr * 32; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1566 } |
2117 | 1567 } |
1568 else | |
1569 { | |
4420 | 1570 // Maybe resize later. |
2117 | 1571 mval.resize (32, 1, 0.0); |
1572 max_size = 32; | |
1573 } | |
1574 | |
4420 | 1575 data = mval.fortran_vec (); |
1576 | |
2117 | 1577 if (isp) |
1578 { | |
3523 | 1579 std::istream& is = *isp; |
2117 | 1580 |
1581 const scanf_format_elt *elt = fmt_list.first (); | |
1582 | |
3538 | 1583 std::ios::fmtflags flags = is.flags (); |
2213 | 1584 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1585 octave_idx_type trips = 0; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1586 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1587 octave_idx_type num_fmt_elts = fmt_list.length (); |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1588 |
2117 | 1589 for (;;) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1590 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1591 octave_quit (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1592 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1593 if (elt) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1594 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1595 if (! (elt->type == scanf_format_elt::whitespace_conversion |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1596 || elt->type == scanf_format_elt::literal_conversion |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1597 || elt->type == '%') |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1598 && max_conv > 0 && conversion_count == max_conv) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1599 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1600 if (all_char_conv && one_elt_size_spec) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1601 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1602 final_nr = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1603 final_nc = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1604 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1605 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1606 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1607 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1608 final_nc = (data_index - 1) / nr + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1609 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1610 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1611 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1612 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1613 else if (data_index == max_size) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1614 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1615 max_size *= 2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1616 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1617 if (all_char_conv) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1618 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1619 if (one_elt_size_spec) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1620 mval.resize (1, max_size, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1621 else if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1622 mval.resize (nr, max_size / nr, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1623 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1624 panic_impossible (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1625 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1626 else if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1627 mval.resize (nr, max_size / nr, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1628 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1629 mval.resize (max_size, 1, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1630 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1631 data = mval.fortran_vec (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1632 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1633 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1634 const char *fmt = elt->text; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1635 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1636 bool discard = elt->discard; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1637 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1638 switch (elt->type) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1639 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1640 case scanf_format_elt::whitespace_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1641 DO_WHITESPACE_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1642 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1643 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1644 case scanf_format_elt::literal_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1645 DO_LITERAL_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1646 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1647 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1648 case '%': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1649 DO_PCT_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1650 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1651 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1652 case 'd': case 'i': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1653 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1654 switch (elt->modifier) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1655 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1656 case 'h': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1657 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1658 short int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1659 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1660 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1661 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1662 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1663 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1664 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1665 case 'l': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1666 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1667 long int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1668 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1669 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1670 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1671 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1672 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1673 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1674 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1675 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1676 int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1677 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1678 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1679 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1680 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1681 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1682 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1683 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1684 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1685 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1686 case 'o': case 'u': case 'x': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1687 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1688 switch (elt->modifier) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1689 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1690 case 'h': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1691 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1692 unsigned short int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1693 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1694 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1695 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1696 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1697 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1698 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1699 case 'l': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1700 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1701 unsigned long int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1702 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1703 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1704 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1705 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1706 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1707 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1708 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1709 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1710 unsigned int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1711 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1712 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1713 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1714 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1715 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1716 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1717 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1718 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1719 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1720 case 'e': case 'f': case 'g': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1721 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1722 double tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1723 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1724 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1725 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1726 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1727 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1728 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1729 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1730 case 'c': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1731 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1732 BEGIN_C_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1733 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1734 FINISH_CHARACTER_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1735 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1736 is.setf (flags); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1737 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1738 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1739 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1740 case 's': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1741 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1742 BEGIN_S_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1743 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1744 FINISH_CHARACTER_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1745 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1746 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1747 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1748 case '[': case '^': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1749 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1750 BEGIN_CHAR_CLASS_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1751 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1752 FINISH_CHARACTER_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1753 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1754 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1755 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1756 case 'p': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1757 error ("%s: unsupported format specifier", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1758 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1759 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1760 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1761 error ("%s: internal format error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1762 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1763 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1764 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1765 if (! ok ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1766 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1767 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1768 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1769 else if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1770 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1771 if (all_char_conv) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1772 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1773 if (one_elt_size_spec) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1774 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1775 final_nr = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1776 final_nc = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1777 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1778 else if (data_index > nr) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1779 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1780 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1781 final_nc = (data_index - 1) / nr + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1782 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1783 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1784 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1785 final_nr = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1786 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1787 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1788 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1789 else if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1790 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1791 if (data_index > nr) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1792 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1793 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1794 final_nc = (data_index - 1) / nr + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1795 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1796 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1797 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1798 final_nr = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1799 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1800 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1801 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1802 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1803 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1804 final_nr = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1805 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1806 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1807 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1808 // If it looks like we have a matching failure, then |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1809 // reset the failbit in the stream state. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1810 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1811 if (is.rdstate () & std::ios::failbit) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1812 is.clear (is.rdstate () & (~std::ios::failbit)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1813 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1814 // FIXME: is this the right thing to do? |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1815 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1816 if (interactive && name () == "stdin") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1817 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1818 is.clear (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1819 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1820 // Skip to end of line. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1821 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1822 bool err; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1823 do_gets (-1, err, false, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1824 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1825 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1826 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1827 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1828 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1829 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1830 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1831 error ("%s: internal format error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1832 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1833 } |
2117 | 1834 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1835 if (nconv == 0 && ++trips == num_fmt_elts) |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1836 { |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1837 if (all_char_conv && one_elt_size_spec) |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1838 { |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1839 final_nr = 1; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1840 final_nc = data_index; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1841 } |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1842 else |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1843 { |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1844 final_nr = nr; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1845 final_nc = (data_index - 1) / nr + 1; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1846 } |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1847 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1848 break; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1849 } |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1850 else |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1851 elt = fmt_list.next (nconv > 0); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1852 } |
2117 | 1853 } |
1854 | |
1855 if (ok ()) | |
1856 { | |
2121 | 1857 mval.resize (final_nr, final_nc, 0.0); |
2117 | 1858 |
3268 | 1859 retval = mval; |
1860 | |
2117 | 1861 if (all_char_conv) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1862 retval = retval.convert_to_str (false, true); |
2117 | 1863 } |
1864 | |
1865 return retval; | |
1866 } | |
1867 | |
1868 octave_value | |
3810 | 1869 octave_base_stream::scanf (const std::string& fmt, const Array<double>& size, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1870 octave_idx_type& conversion_count, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1871 const std::string& who) |
2117 | 1872 { |
1873 octave_value retval = Matrix (); | |
1874 | |
3559 | 1875 conversion_count = 0; |
2117 | 1876 |
3523 | 1877 std::istream *isp = input_stream (); |
2117 | 1878 |
1879 if (isp) | |
1880 { | |
1881 scanf_format_list fmt_list (fmt); | |
1882 | |
3640 | 1883 if (fmt_list.num_conversions () == -1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1884 ::error ("%s: invalid format specified", who.c_str ()); |
3640 | 1885 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1886 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1887 octave_idx_type nr = -1; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1888 octave_idx_type nc = -1; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1889 |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1890 bool one_elt_size_spec; |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1891 |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1892 get_size (size, nr, nc, one_elt_size_spec, who); |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1893 |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1894 if (! error_state) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1895 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1896 conversion_count, who); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1897 } |
2215 | 1898 } |
1899 else | |
4468 | 1900 invalid_operation (who, "reading"); |
2572 | 1901 |
1902 return retval; | |
1903 } | |
1904 | |
2712 | 1905 bool |
1906 octave_base_stream::do_oscanf (const scanf_format_elt *elt, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1907 octave_value& retval, const std::string& who) |
2572 | 1908 { |
2712 | 1909 bool quit = false; |
2215 | 1910 |
3523 | 1911 std::istream *isp = input_stream (); |
2215 | 1912 |
1913 if (isp) | |
1914 { | |
3523 | 1915 std::istream& is = *isp; |
2215 | 1916 |
3538 | 1917 std::ios::fmtflags flags = is.flags (); |
2215 | 1918 |
1919 if (elt) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1920 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1921 const char *fmt = elt->text; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1922 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1923 bool discard = elt->discard; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1924 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1925 switch (elt->type) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1926 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1927 case scanf_format_elt::whitespace_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1928 DO_WHITESPACE_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1929 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1930 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1931 case scanf_format_elt::literal_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1932 DO_LITERAL_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1933 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1934 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1935 case '%': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1936 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1937 DO_PCT_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1938 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1939 if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1940 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1941 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1942 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1943 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1944 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1945 case 'd': case 'i': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1946 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1947 int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1948 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1949 if (OCTAVE_SCAN (is, *elt, &tmp)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1950 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1951 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1952 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1953 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1954 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1955 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1956 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1957 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1958 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1959 case 'o': case 'u': case 'x': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1960 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1961 long int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1962 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1963 if (OCTAVE_SCAN (is, *elt, &tmp)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1964 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1965 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1966 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1967 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1968 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1969 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1970 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1971 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1972 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1973 case 'e': case 'f': case 'g': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1974 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1975 double tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1976 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1977 if (OCTAVE_SCAN (is, *elt, &tmp)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1978 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1979 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1980 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1981 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1982 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1983 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1984 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1985 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1986 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1987 case 'c': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1988 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1989 BEGIN_C_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1990 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1991 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1992 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1993 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1994 if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1995 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1996 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1997 is.setf (flags); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1998 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1999 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2000 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2001 case 's': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2002 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2003 BEGIN_S_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2004 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2005 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2006 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2007 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2008 if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2009 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2010 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2011 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2012 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2013 case '[': case '^': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2014 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2015 BEGIN_CHAR_CLASS_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2016 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2017 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2018 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2019 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2020 if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2021 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2022 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2023 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2024 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2025 case 'p': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2026 error ("%s: unsupported format specifier", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2027 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2028 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2029 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2030 error ("%s: internal format error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2031 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2032 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2033 } |
2215 | 2034 |
2035 if (ok () && is.fail ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2036 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2037 error ("%s: read error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2038 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2039 // FIXME: is this the right thing to do? |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2040 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2041 if (interactive && name () == "stdin") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2042 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2043 // Skip to end of line. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2044 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2045 bool err; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2046 do_gets (-1, err, false, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2047 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2048 } |
2215 | 2049 } |
2050 | |
2712 | 2051 return quit; |
2215 | 2052 } |
2053 | |
2054 octave_value_list | |
4468 | 2055 octave_base_stream::oscanf (const std::string& fmt, const std::string& who) |
2215 | 2056 { |
2057 octave_value_list retval; | |
2058 | |
3523 | 2059 std::istream *isp = input_stream (); |
2215 | 2060 |
2061 if (isp) | |
2062 { | |
3523 | 2063 std::istream& is = *isp; |
2215 | 2064 |
2065 scanf_format_list fmt_list (fmt); | |
2066 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
2067 octave_idx_type nconv = fmt_list.num_conversions (); |
2215 | 2068 |
3640 | 2069 if (nconv == -1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2070 ::error ("%s: invalid format specified", who.c_str ()); |
3640 | 2071 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2072 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2073 is.clear (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2074 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2075 octave_idx_type len = fmt_list.length (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2076 |
13271
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
2077 retval.resize (nconv+2, Matrix ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2078 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2079 const scanf_format_elt *elt = fmt_list.first (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2080 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2081 int num_values = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2082 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2083 bool quit = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2084 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2085 for (octave_idx_type i = 0; i < len; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2086 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2087 octave_value tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2088 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2089 quit = do_oscanf (elt, tmp, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2090 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2091 if (quit) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2092 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2093 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2094 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2095 if (tmp.is_defined ()) |
14844
5bc9b9cb4362
maint: Use Octave coding conventions for cuddled parenthesis in retval assignments.
Rik <octave@nomad.inbox5.com>
parents:
14675
diff
changeset
|
2096 retval(num_values++) = tmp; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2097 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2098 if (! ok ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2099 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2100 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2101 elt = fmt_list.next (nconv > 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2102 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2103 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2104 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2105 retval(nconv) = num_values; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2106 |
13271
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
2107 int err_num; |
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
2108 retval(nconv+1) = error (false, err_num); |
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
2109 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2110 if (! quit) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2111 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2112 // Pick up any trailing stuff. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2113 if (ok () && len > nconv) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2114 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2115 octave_value tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2116 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2117 elt = fmt_list.next (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2118 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2119 do_oscanf (elt, tmp, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2120 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2121 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2122 } |
2117 | 2123 } |
2124 else | |
4468 | 2125 invalid_operation (who, "reading"); |
2117 | 2126 |
2127 return retval; | |
2128 } | |
2129 | |
2130 // Functions that are defined for all output streams (output streams | |
2131 // are those that define os). | |
2132 | |
2133 int | |
2134 octave_base_stream::flush (void) | |
2135 { | |
2136 int retval = -1; | |
2137 | |
3523 | 2138 std::ostream *os = output_stream (); |
2117 | 2139 |
2140 if (os) | |
2141 { | |
2142 os->flush (); | |
2143 | |
2144 if (os->good ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2145 retval = 0; |
2117 | 2146 } |
2147 else | |
2148 invalid_operation ("fflush", "writing"); | |
2149 | |
2150 return retval; | |
2151 } | |
2152 | |
2153 class | |
2154 printf_value_cache | |
2155 { | |
2156 public: | |
2157 | |
3653 | 2158 enum state { ok, conversion_error }; |
2117 | 2159 |
7352 | 2160 printf_value_cache (const octave_value_list& args, const std::string& who) |
2117 | 2161 : values (args), val_idx (0), elt_idx (0), |
2162 n_vals (values.length ()), n_elts (0), data (0), | |
7352 | 2163 curr_state (ok) |
2164 { | |
2165 for (octave_idx_type i = 0; i < values.length (); i++) | |
2166 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2167 octave_value val = values(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2168 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2169 if (val.is_map () || val.is_cell () || val.is_object ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2170 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2171 gripe_wrong_type_arg (who, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2172 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2173 } |
7352 | 2174 } |
2175 } | |
2117 | 2176 |
2177 ~printf_value_cache (void) { } | |
2178 | |
2179 // Get the current value as a double and advance the internal pointer. | |
2180 double double_value (void); | |
2181 | |
2182 // Get the current value as an int and advance the internal pointer. | |
2183 int int_value (void); | |
2184 | |
2185 // Get the current value as a string and advance the internal pointer. | |
3523 | 2186 std::string string_value (void); |
2117 | 2187 |
3145 | 2188 operator bool () const { return (curr_state == ok); } |
2117 | 2189 |
3653 | 2190 bool exhausted (void) { return (val_idx >= n_vals); } |
2117 | 2191 |
2192 private: | |
2193 | |
2194 const octave_value_list values; | |
2195 int val_idx; | |
2196 int elt_idx; | |
2197 int n_vals; | |
2198 int n_elts; | |
2199 const double *data; | |
4874 | 2200 NDArray curr_val; |
2117 | 2201 state curr_state; |
2202 | |
2203 // Must create value cache with values! | |
2204 | |
2205 printf_value_cache (void); | |
2206 | |
2207 // No copying! | |
2208 | |
2209 printf_value_cache (const printf_value_cache&); | |
2210 | |
2211 printf_value_cache& operator = (const printf_value_cache&); | |
2212 }; | |
2213 | |
2214 double | |
2215 printf_value_cache::double_value (void) | |
2216 { | |
2217 double retval = 0.0; | |
2218 | |
3711 | 2219 if (exhausted ()) |
2220 curr_state = conversion_error; | |
2221 | |
2222 while (! exhausted ()) | |
2117 | 2223 { |
2224 if (! data) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2225 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2226 octave_value tmp_val = values (val_idx); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2227 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2228 // Force string conversion here for compatibility. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2229 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2230 curr_val = tmp_val.array_value (true); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2231 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2232 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2233 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2234 elt_idx = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2235 n_elts = curr_val.length (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2236 data = curr_val.data (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2237 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2238 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2239 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2240 curr_state = conversion_error; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2241 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2242 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2243 } |
2117 | 2244 |
2245 if (elt_idx < n_elts) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2246 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2247 retval = data[elt_idx++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2248 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2249 if (elt_idx >= n_elts) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2250 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2251 elt_idx = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2252 val_idx++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2253 data = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2254 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2255 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2256 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2257 } |
2117 | 2258 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2259 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2260 val_idx++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2261 data = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2262 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2263 if (n_elts == 0 && exhausted ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2264 curr_state = conversion_error; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2265 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2266 continue; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2267 } |
2117 | 2268 } |
2269 | |
2270 return retval; | |
2271 } | |
2272 | |
2273 int | |
2274 printf_value_cache::int_value (void) | |
2275 { | |
2276 int retval = 0; | |
2277 | |
2278 double dval = double_value (); | |
2279 | |
2280 if (! error_state) | |
2281 { | |
2282 if (D_NINT (dval) == dval) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2283 retval = NINT (dval); |
2117 | 2284 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2285 curr_state = conversion_error; |
2117 | 2286 } |
2287 | |
2288 return retval; | |
2289 } | |
2290 | |
3536 | 2291 std::string |
2117 | 2292 printf_value_cache::string_value (void) |
2293 { | |
3523 | 2294 std::string retval; |
2117 | 2295 |
4425 | 2296 if (exhausted ()) |
2297 curr_state = conversion_error; | |
4257 | 2298 else |
2117 | 2299 { |
4425 | 2300 octave_value tval = values (val_idx++); |
2301 | |
2302 if (tval.rows () == 1) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2303 retval = tval.string_value (); |
4425 | 2304 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2305 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2306 // In the name of Matlab compatibility. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2307 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2308 charMatrix chm = tval.char_matrix_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2309 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2310 octave_idx_type nr = chm.rows (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2311 octave_idx_type nc = chm.columns (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2312 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2313 int k = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2314 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2315 retval.resize (nr * nc, '\0'); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2316 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2317 for (octave_idx_type j = 0; j < nc; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2318 for (octave_idx_type i = 0; i < nr; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2319 retval[k++] = chm(i,j); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2320 } |
4425 | 2321 |
2322 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2323 curr_state = conversion_error; |
2117 | 2324 } |
4257 | 2325 |
2117 | 2326 return retval; |
2327 } | |
2328 | |
2329 // Ugh again and again. | |
2330 | |
2572 | 2331 template <class T> |
3620 | 2332 int |
3523 | 2333 do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2334 int sa_2, T arg, const std::string& who) |
2572 | 2335 { |
3620 | 2336 int retval = 0; |
2337 | |
2572 | 2338 switch (nsa) |
2339 { | |
2340 case 2: | |
3640 | 2341 retval = octave_format (os, fmt, sa_1, sa_2, arg); |
2572 | 2342 break; |
2343 | |
2344 case 1: | |
3640 | 2345 retval = octave_format (os, fmt, sa_1, arg); |
2572 | 2346 break; |
2347 | |
2348 case 0: | |
3640 | 2349 retval = octave_format (os, fmt, arg); |
2572 | 2350 break; |
2351 | |
2352 default: | |
4468 | 2353 ::error ("%s: internal error handling format", who.c_str ()); |
2572 | 2354 break; |
2355 } | |
3620 | 2356 |
2357 return retval; | |
2572 | 2358 } |
2359 | |
6492 | 2360 #define DO_DOUBLE_CONV(TQUAL) \ |
2361 do \ | |
2362 { \ | |
7199 | 2363 if (val > std::numeric_limits<TQUAL long>::max () \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2364 || val < std::numeric_limits<TQUAL long>::min ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2365 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2366 std::string tfmt = fmt; \ |
6492 | 2367 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2368 tfmt.replace (tfmt.rfind (elt->type), 1, ".f"); \ |
6492 | 2369 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2370 if (elt->modifier == 'l') \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2371 tfmt.replace (tfmt.rfind (elt->modifier), 1, ""); \ |
7199 | 2372 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2373 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2, \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2374 val, who); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2375 } \ |
6492 | 2376 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2377 retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2378 static_cast<TQUAL long> (val), who); \ |
6492 | 2379 } \ |
2380 while (0) | |
2381 | |
2117 | 2382 int |
2383 octave_base_stream::do_printf (printf_format_list& fmt_list, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2384 const octave_value_list& args, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2385 const std::string& who) |
2117 | 2386 { |
3620 | 2387 int retval = 0; |
2117 | 2388 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
2389 octave_idx_type nconv = fmt_list.num_conversions (); |
3640 | 2390 |
3523 | 2391 std::ostream *osp = output_stream (); |
2117 | 2392 |
2393 if (osp) | |
2394 { | |
3523 | 2395 std::ostream& os = *osp; |
2117 | 2396 |
2397 const printf_format_elt *elt = fmt_list.first (); | |
2398 | |
7352 | 2399 printf_value_cache val_cache (args, who); |
2400 | |
2401 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2402 return retval; |
2117 | 2403 |
2404 for (;;) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2405 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2406 octave_quit (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2407 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2408 if (elt) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2409 { |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
2410 // NSA is the number of 'star' args to convert. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2411 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2412 int nsa = (elt->fw < 0) + (elt->prec < 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2413 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2414 int sa_1 = 0; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2415 int sa_2 = 0; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2416 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2417 if (nsa > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2418 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2419 sa_1 = val_cache.int_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2420 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2421 if (! val_cache) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2422 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2423 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2424 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2425 if (nsa > 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2426 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2427 sa_2 = val_cache.int_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2428 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2429 if (! val_cache) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2430 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2431 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2432 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2433 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2434 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2435 const char *fmt = elt->text; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2436 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2437 if (elt->type == '%') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2438 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2439 os << "%"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2440 retval++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2441 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2442 else if (elt->args == 0 && elt->text) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2443 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2444 os << elt->text; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2445 retval += strlen (elt->text); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2446 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2447 else if (elt->type == 's') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2448 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2449 std::string val = val_cache.string_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2450 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2451 if (val_cache) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2452 retval += do_printf_conv (os, fmt, nsa, sa_1, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2453 sa_2, val.c_str (), who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2454 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2455 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2456 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2457 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2458 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2459 double val = val_cache.double_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2460 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2461 if (val_cache) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2462 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2463 if (lo_ieee_isnan (val) || xisinf (val)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2464 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2465 std::string tfmt = fmt; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2466 std::string::size_type i1, i2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2467 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2468 tfmt.replace ((i1 = tfmt.rfind (elt->type)), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2469 1, 1, 's'); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2470 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2471 if ((i2 = tfmt.rfind ('.')) != std::string::npos |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2472 && i2 < i1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2473 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2474 tfmt.erase (i2, i1-i2); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2475 if (elt->prec < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2476 nsa--; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2477 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2478 |
17292
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2479 const char *tval; |
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2480 if (xisinf (val)) |
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2481 if (elt->flags.find ('+') != std::string::npos) |
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2482 tval = (val < 0 ? "-Inf" : "+Inf"); |
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2483 else |
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2484 tval = (val < 0 ? "-Inf" : "Inf"); |
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2485 else |
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2486 if (elt->flags.find ('+') != std::string::npos) |
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2487 tval = (lo_ieee_is_NA (val) ? "+NA" : "+NaN"); |
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2488 else |
c5073ed27cdc
Handle '+' format modifier in sprintf (bug #39773)
Rik <rik@octave.org>
parents:
16892
diff
changeset
|
2489 tval = (lo_ieee_is_NA (val) ? "NA" : "NaN"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2490 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2491 retval += do_printf_conv (os, tfmt.c_str (), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2492 nsa, sa_1, sa_2, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2493 tval, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2494 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2495 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2496 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2497 char type = elt->type; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2498 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2499 switch (type) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2500 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2501 case 'd': case 'i': case 'c': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2502 DO_DOUBLE_CONV (OCTAVE_EMPTY_CPP_ARG); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2503 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2504 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2505 case 'o': case 'x': case 'X': case 'u': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2506 DO_DOUBLE_CONV (unsigned); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2507 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2508 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2509 case 'f': case 'e': case 'E': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2510 case 'g': case 'G': |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2511 retval += do_printf_conv (os, fmt, nsa, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2512 sa_1, sa_2, val, who); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2513 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2514 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2515 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2516 error ("%s: invalid format specifier", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2517 who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2518 return -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2519 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2520 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2521 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2522 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2523 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2524 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2525 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2526 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2527 if (! os) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2528 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2529 error ("%s: write error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2530 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2531 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2532 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2533 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2534 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2535 ::error ("%s: internal error handling format", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2536 retval = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2537 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2538 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2539 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2540 elt = fmt_list.next (nconv > 0 && ! val_cache.exhausted ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2541 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2542 if (! elt || (val_cache.exhausted () && elt->args > 0)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2543 break; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2544 } |
2117 | 2545 } |
3640 | 2546 else |
4468 | 2547 invalid_operation (who, "writing"); |
2117 | 2548 |
2549 return retval; | |
2550 } | |
2551 | |
2552 int | |
3640 | 2553 octave_base_stream::printf (const std::string& fmt, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2554 const octave_value_list& args, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2555 const std::string& who) |
2117 | 2556 { |
3640 | 2557 int retval = 0; |
2558 | |
2559 printf_format_list fmt_list (fmt); | |
2560 | |
2561 if (fmt_list.num_conversions () == -1) | |
4468 | 2562 ::error ("%s: invalid format specified", who.c_str ()); |
2117 | 2563 else |
4468 | 2564 retval = do_printf (fmt_list, args, who); |
2117 | 2565 |
2566 return retval; | |
2567 } | |
2568 | |
2569 int | |
4468 | 2570 octave_base_stream::puts (const std::string& s, const std::string& who) |
2117 | 2571 { |
2572 int retval = -1; | |
2573 | |
3523 | 2574 std::ostream *osp = output_stream (); |
2117 | 2575 |
2576 if (osp) | |
2577 { | |
3523 | 2578 std::ostream& os = *osp; |
2117 | 2579 |
2580 os << s; | |
2581 | |
2582 if (os) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2583 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2584 // FIXME: why does this seem to be necessary? |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2585 // Without it, output from a loop like |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2586 // |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2587 // for i = 1:100, fputs (stdout, "foo\n"); endfor |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2588 // |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2589 // doesn't seem to go to the pager immediately. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2590 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2591 os.flush (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2592 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2593 if (os) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2594 retval = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2595 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2596 error ("%s: write error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2597 } |
2117 | 2598 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2599 error ("%s: write error", who.c_str ()); |
2117 | 2600 } |
2601 else | |
4468 | 2602 invalid_operation (who, "writing"); |
2117 | 2603 |
2604 return retval; | |
2605 } | |
2606 | |
2607 // Return current error message for this stream. | |
2608 | |
3536 | 2609 std::string |
2435 | 2610 octave_base_stream::error (bool clear_err, int& err_num) |
2117 | 2611 { |
2435 | 2612 err_num = fail ? -1 : 0; |
2117 | 2613 |
3523 | 2614 std::string tmp = errmsg; |
2117 | 2615 |
2616 if (clear_err) | |
2617 clear (); | |
2618 | |
2619 return tmp; | |
2620 } | |
2621 | |
2622 void | |
4468 | 2623 octave_base_stream::invalid_operation (const std::string& who, const char *rw) |
2117 | 2624 { |
4468 | 2625 // Note that this is not ::error () ! |
2626 | |
6297 | 2627 error (who, std::string ("stream not open for ") + rw); |
2117 | 2628 } |
2629 | |
3552 | 2630 octave_stream::octave_stream (octave_base_stream *bs) |
3340 | 2631 : rep (bs) |
2632 { | |
2633 if (rep) | |
2634 rep->count = 1; | |
2635 } | |
2636 | |
2637 octave_stream::~octave_stream (void) | |
2638 { | |
2639 if (rep && --rep->count == 0) | |
2640 delete rep; | |
2641 } | |
2642 | |
2643 octave_stream::octave_stream (const octave_stream& s) | |
2644 : rep (s.rep) | |
2645 { | |
2646 if (rep) | |
2647 rep->count++; | |
2648 } | |
2649 | |
2650 octave_stream& | |
2651 octave_stream::operator = (const octave_stream& s) | |
2652 { | |
2653 if (rep != s.rep) | |
2654 { | |
2655 if (rep && --rep->count == 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2656 delete rep; |
3340 | 2657 |
2658 rep = s.rep; | |
2659 | |
2660 if (rep) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2661 rep->count++; |
3340 | 2662 } |
2663 | |
2664 return *this; | |
2665 } | |
2666 | |
2117 | 2667 int |
2668 octave_stream::flush (void) | |
2669 { | |
2670 int retval = -1; | |
2671 | |
5659 | 2672 if (stream_ok ()) |
2117 | 2673 retval = rep->flush (); |
2674 | |
2675 return retval; | |
2676 } | |
2677 | |
3536 | 2678 std::string |
5275 | 2679 octave_stream::getl (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 2680 { |
3523 | 2681 std::string retval; |
2117 | 2682 |
5659 | 2683 if (stream_ok ()) |
4468 | 2684 retval = rep->getl (max_len, err, who); |
2117 | 2685 |
2686 return retval; | |
2687 } | |
2688 | |
3536 | 2689 std::string |
4468 | 2690 octave_stream::getl (const octave_value& tc_max_len, bool& err, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2691 const std::string& who) |
2117 | 2692 { |
3523 | 2693 std::string retval; |
2117 | 2694 |
2695 err = false; | |
2696 | |
2697 int conv_err = 0; | |
2698 | |
6345 | 2699 int max_len = -1; |
2700 | |
2701 if (tc_max_len.is_defined ()) | |
2117 | 2702 { |
6345 | 2703 max_len = convert_to_valid_int (tc_max_len, conv_err); |
2704 | |
2705 if (conv_err || max_len < 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2706 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2707 err = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2708 ::error ("%s: invalid maximum length specified", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2709 } |
2117 | 2710 } |
6345 | 2711 |
2712 if (! error_state) | |
4468 | 2713 retval = getl (max_len, err, who); |
2117 | 2714 |
2715 return retval; | |
2716 } | |
2717 | |
3536 | 2718 std::string |
5275 | 2719 octave_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 2720 { |
3523 | 2721 std::string retval; |
2117 | 2722 |
5659 | 2723 if (stream_ok ()) |
4468 | 2724 retval = rep->gets (max_len, err, who); |
2117 | 2725 |
2726 return retval; | |
2727 } | |
2728 | |
3536 | 2729 std::string |
4468 | 2730 octave_stream::gets (const octave_value& tc_max_len, bool& err, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2731 const std::string& who) |
2117 | 2732 { |
3523 | 2733 std::string retval; |
2117 | 2734 |
2735 err = false; | |
2736 | |
2737 int conv_err = 0; | |
2738 | |
6345 | 2739 int max_len = -1; |
2740 | |
2741 if (tc_max_len.is_defined ()) | |
2117 | 2742 { |
6345 | 2743 max_len = convert_to_valid_int (tc_max_len, conv_err); |
2744 | |
2745 if (conv_err || max_len < 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2746 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2747 err = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2748 ::error ("%s: invalid maximum length specified", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2749 } |
2117 | 2750 } |
6345 | 2751 |
2752 if (! error_state) | |
4468 | 2753 retval = gets (max_len, err, who); |
2117 | 2754 |
2755 return retval; | |
2756 } | |
2757 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2758 off_t |
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2759 octave_stream::skipl (off_t count, bool& err, const std::string& who) |
9701 | 2760 { |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2761 off_t retval = -1; |
9701 | 2762 |
2763 if (stream_ok ()) | |
2764 retval = rep->skipl (count, err, who); | |
2765 | |
2766 return retval; | |
2767 } | |
2768 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2769 off_t |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2770 octave_stream::skipl (const octave_value& tc_count, bool& err, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2771 const std::string& who) |
9701 | 2772 { |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2773 off_t retval = -1; |
9701 | 2774 |
2775 err = false; | |
2776 | |
2777 int conv_err = 0; | |
2778 | |
2779 int count = 1; | |
2780 | |
2781 if (tc_count.is_defined ()) | |
2782 { | |
2783 if (tc_count.is_scalar_type () && xisinf (tc_count.scalar_value ())) | |
2784 count = -1; | |
2785 else | |
2786 { | |
2787 count = convert_to_valid_int (tc_count, conv_err); | |
2788 | |
2789 if (conv_err || count < 0) | |
2790 { | |
2791 err = true; | |
2792 ::error ("%s: invalid number of lines specified", who.c_str ()); | |
2793 } | |
2794 } | |
2795 } | |
2796 | |
2797 if (! error_state) | |
2798 retval = skipl (count, err, who); | |
2799 | |
2800 return retval; | |
2801 } | |
2802 | |
2117 | 2803 int |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2804 octave_stream::seek (off_t offset, int origin) |
2117 | 2805 { |
5065 | 2806 int status = -1; |
2117 | 2807 |
5659 | 2808 if (stream_ok ()) |
4889 | 2809 { |
2810 clearerr (); | |
2811 | |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2812 // Find current position so we can return to it if needed. |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2813 |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2814 off_t orig_pos = rep->tell (); |
5065 | 2815 |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2816 // Move to end of file. If successful, find the offset of the end. |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2817 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2818 status = rep->seek (0, SEEK_END); |
5065 | 2819 |
2820 if (status == 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2821 { |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2822 off_t eof_pos = rep->tell (); |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2823 |
12943
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2824 if (origin == SEEK_CUR) |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2825 { |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2826 // Move back to original position, otherwise we will be |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2827 // seeking from the end of file which is probably not the |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2828 // original location. |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2829 |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2830 rep->seek (orig_pos, SEEK_SET); |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2831 } |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2832 |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2833 // Attempt to move to desired position; may be outside bounds |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2834 // of existing file. |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2835 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2836 status = rep->seek (offset, origin); |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2837 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2838 if (status == 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2839 { |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2840 // Where are we after moving to desired position? |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2841 |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2842 off_t desired_pos = rep->tell (); |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2843 |
12943
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2844 // I don't think save_pos can be less than zero, but we'll |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2845 // check anyway... |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2846 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2847 if (desired_pos > eof_pos || desired_pos < 0) |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2848 { |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2849 // Seek outside bounds of file. Failure should leave |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2850 // position unchanged. |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2851 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2852 rep->seek (orig_pos, SEEK_SET); |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2853 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2854 status = -1; |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2855 } |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2856 } |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2857 else |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2858 { |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2859 // Seeking to the desired position failed. Move back to |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2860 // original position and return failure status. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2861 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2862 rep->seek (orig_pos, SEEK_SET); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2863 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2864 status = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2865 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2866 } |
4889 | 2867 } |
2117 | 2868 |
5065 | 2869 return status; |
2117 | 2870 } |
2871 | |
2872 int | |
2873 octave_stream::seek (const octave_value& tc_offset, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2874 const octave_value& tc_origin) |
2117 | 2875 { |
2876 int retval = -1; | |
2877 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2878 // FIXME: should we have octave_value methods that handle off_t explicitly? |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2879 octave_int64 val = tc_offset.int64_scalar_value (); |
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2880 off_t xoffset = val.value (); |
4645 | 2881 |
2882 if (! error_state) | |
2117 | 2883 { |
4645 | 2884 int conv_err = 0; |
2885 | |
4797 | 2886 int origin = SEEK_SET; |
2117 | 2887 |
2341 | 2888 if (tc_origin.is_string ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2889 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2890 std::string xorigin = tc_origin.string_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2891 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2892 if (xorigin == "bof") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2893 origin = SEEK_SET; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2894 else if (xorigin == "cof") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2895 origin = SEEK_CUR; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2896 else if (xorigin == "eof") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2897 origin = SEEK_END; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2898 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2899 conv_err = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2900 } |
2341 | 2901 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2902 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2903 int xorigin = convert_to_valid_int (tc_origin, conv_err); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2904 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2905 if (! conv_err) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2906 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2907 if (xorigin == -1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2908 origin = SEEK_SET; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2909 else if (xorigin == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2910 origin = SEEK_CUR; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2911 else if (xorigin == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2912 origin = SEEK_END; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2913 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2914 conv_err = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2915 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2916 } |
2117 | 2917 |
2918 if (! conv_err) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2919 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2920 retval = seek (xoffset, origin); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2921 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2922 if (retval != 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2923 error ("fseek: failed to seek to requested position"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2924 } |
2117 | 2925 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2926 error ("fseek: invalid value for origin"); |
2117 | 2927 } |
2928 else | |
2929 error ("fseek: invalid value for offset"); | |
2930 | |
2931 return retval; | |
2932 } | |
2933 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2934 off_t |
4797 | 2935 octave_stream::tell (void) |
2117 | 2936 { |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2937 off_t retval = -1; |
2117 | 2938 |
5659 | 2939 if (stream_ok ()) |
2117 | 2940 retval = rep->tell (); |
2941 | |
2942 return retval; | |
2943 } | |
2944 | |
2945 int | |
2946 octave_stream::rewind (void) | |
2947 { | |
6296 | 2948 return seek (0, SEEK_SET); |
2117 | 2949 } |
2950 | |
3340 | 2951 bool |
2952 octave_stream::is_open (void) const | |
2953 { | |
2954 bool retval = false; | |
2955 | |
5659 | 2956 if (stream_ok ()) |
3340 | 2957 retval = rep->is_open (); |
2958 | |
2959 return retval; | |
2960 } | |
2961 | |
2962 void | |
2963 octave_stream::close (void) | |
2964 { | |
5659 | 2965 if (stream_ok ()) |
3340 | 2966 rep->close (); |
2967 } | |
2968 | |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2969 template <class SRC_T, class DST_T> |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2970 static octave_value |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2971 convert_and_copy (std::list<void *>& input_buf_list, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2972 octave_idx_type input_buf_elts, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2973 octave_idx_type elts_read, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2974 octave_idx_type nr, octave_idx_type nc, bool swap, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2975 bool do_float_fmt_conv, bool do_NA_conv, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2976 oct_mach_info::float_format from_flt_fmt) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2977 { |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2978 typedef typename DST_T::element_type dst_elt_type; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2979 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2980 DST_T conv (dim_vector (nr, nc)); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2981 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2982 dst_elt_type *conv_data = conv.fortran_vec (); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2983 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2984 octave_idx_type j = 0; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2985 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2986 for (std::list<void *>::const_iterator it = input_buf_list.begin (); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2987 it != input_buf_list.end (); it++) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2988 { |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2989 SRC_T *data = static_cast<SRC_T *> (*it); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2990 |
17463 | 2991 if (swap || do_float_fmt_conv) |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
2992 { |
17463 | 2993 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read; |
2994 i++, j++) | |
2995 { | |
2996 if (swap) | |
2997 swap_bytes<sizeof (SRC_T)> (&data[i]); | |
2998 else if (do_float_fmt_conv) | |
2999 do_float_format_conversion (&data[i], sizeof (SRC_T), | |
3000 1, from_flt_fmt, | |
3001 oct_mach_info::float_format ()); | |
3002 | |
3003 dst_elt_type tmp (data[i]); | |
3004 | |
3005 if (do_NA_conv && __lo_ieee_is_old_NA (tmp)) | |
3006 tmp = __lo_ieee_replace_old_NA (tmp); | |
3007 | |
3008 conv_data[j] = tmp; | |
3009 } | |
3010 } | |
3011 else | |
3012 { | |
3013 if (do_NA_conv) | |
3014 { | |
3015 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read; | |
3016 i++, j++) | |
3017 { | |
3018 dst_elt_type tmp (data[i]); | |
3019 | |
3020 if (__lo_ieee_is_old_NA (tmp)) | |
3021 tmp = __lo_ieee_replace_old_NA (tmp); | |
3022 | |
3023 conv_data[j] = tmp; | |
3024 } | |
3025 } | |
3026 else | |
3027 { | |
3028 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read; | |
3029 i++, j++) | |
3030 conv_data[j] = data[i]; | |
3031 } | |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3032 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3033 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3034 delete [] data; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3035 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3036 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3037 input_buf_list.clear (); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3038 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3039 for (octave_idx_type i = elts_read; i < nr * nc; i++) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3040 conv_data[i] = dst_elt_type (0); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3041 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3042 return conv; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3043 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3044 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3045 typedef octave_value (*conv_fptr) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3046 (std::list<void *>& input_buf_list, octave_idx_type input_buf_elts, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3047 octave_idx_type elts_read, octave_idx_type nr, octave_idx_type nc, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3048 bool swap, bool do_float_fmt_conv, bool do_NA_conv, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3049 oct_mach_info::float_format from_flt_fmt); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3050 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3051 #define TABLE_ELT(T, U, V, W) \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3052 conv_fptr_table[oct_data_conv::T][oct_data_conv::U] = convert_and_copy<V, W> |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3053 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3054 #define FILL_TABLE_ROW(T, V) \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3055 TABLE_ELT (T, dt_int8, V, int8NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3056 TABLE_ELT (T, dt_uint8, V, uint8NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3057 TABLE_ELT (T, dt_int16, V, int16NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3058 TABLE_ELT (T, dt_uint16, V, uint16NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3059 TABLE_ELT (T, dt_int32, V, int32NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3060 TABLE_ELT (T, dt_uint32, V, uint32NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3061 TABLE_ELT (T, dt_int64, V, int64NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3062 TABLE_ELT (T, dt_uint64, V, uint64NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3063 TABLE_ELT (T, dt_single, V, FloatNDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3064 TABLE_ELT (T, dt_double, V, NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3065 TABLE_ELT (T, dt_char, V, charNDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3066 TABLE_ELT (T, dt_schar, V, charNDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3067 TABLE_ELT (T, dt_uchar, V, charNDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3068 TABLE_ELT (T, dt_logical, V, boolNDArray); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3069 |
2117 | 3070 octave_value |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3071 octave_stream::finalize_read (std::list<void *>& input_buf_list, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3072 octave_idx_type input_buf_elts, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3073 octave_idx_type elts_read, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3074 octave_idx_type nr, octave_idx_type nc, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3075 oct_data_conv::data_type input_type, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3076 oct_data_conv::data_type output_type, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3077 oct_mach_info::float_format ffmt) |
2117 | 3078 { |
3079 octave_value retval; | |
3080 | |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3081 static bool initialized = false; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3082 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3083 // Table function pointers for return types x read types. |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3084 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3085 static conv_fptr conv_fptr_table[oct_data_conv::dt_unknown][14]; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3086 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3087 if (! initialized) |
4944 | 3088 { |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3089 for (int i = 0; i < oct_data_conv::dt_unknown; i++) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3090 for (int j = 0; j < 14; j++) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3091 conv_fptr_table[i][j] = 0; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3092 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3093 FILL_TABLE_ROW (dt_int8, int8_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3094 FILL_TABLE_ROW (dt_uint8, uint8_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3095 FILL_TABLE_ROW (dt_int16, int16_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3096 FILL_TABLE_ROW (dt_uint16, uint16_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3097 FILL_TABLE_ROW (dt_int32, int32_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3098 FILL_TABLE_ROW (dt_uint32, uint32_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3099 FILL_TABLE_ROW (dt_int64, int64_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3100 FILL_TABLE_ROW (dt_uint64, uint64_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3101 FILL_TABLE_ROW (dt_single, float); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3102 FILL_TABLE_ROW (dt_double, double); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3103 FILL_TABLE_ROW (dt_char, char); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3104 FILL_TABLE_ROW (dt_schar, signed char); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3105 FILL_TABLE_ROW (dt_uchar, unsigned char); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3106 FILL_TABLE_ROW (dt_logical, bool); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3107 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3108 initialized = true; |
4944 | 3109 } |
3110 | |
3111 bool swap = false; | |
3112 | |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3113 if (ffmt == oct_mach_info::flt_fmt_unknown) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3114 ffmt = float_format (); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3115 |
4944 | 3116 if (oct_mach_info::words_big_endian ()) |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3117 swap = (ffmt == oct_mach_info::flt_fmt_ieee_little_endian); |
4944 | 3118 else |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3119 swap = (ffmt == oct_mach_info::flt_fmt_ieee_big_endian); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3120 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3121 bool do_float_fmt_conv = ((input_type == oct_data_conv::dt_double |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3122 || input_type == oct_data_conv::dt_single) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3123 && ffmt != float_format ()); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3124 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3125 bool do_NA_conv = (output_type == oct_data_conv::dt_double); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3126 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3127 switch (output_type) |
4944 | 3128 { |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3129 case oct_data_conv::dt_int8: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3130 case oct_data_conv::dt_uint8: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3131 case oct_data_conv::dt_int16: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3132 case oct_data_conv::dt_uint16: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3133 case oct_data_conv::dt_int32: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3134 case oct_data_conv::dt_uint32: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3135 case oct_data_conv::dt_int64: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3136 case oct_data_conv::dt_uint64: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3137 case oct_data_conv::dt_single: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3138 case oct_data_conv::dt_double: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3139 case oct_data_conv::dt_char: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3140 case oct_data_conv::dt_schar: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3141 case oct_data_conv::dt_uchar: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3142 case oct_data_conv::dt_logical: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3143 { |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3144 conv_fptr fptr = conv_fptr_table[input_type][output_type]; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3145 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3146 retval = fptr (input_buf_list, input_buf_elts, elts_read, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3147 nr, nc, swap, do_float_fmt_conv, do_NA_conv, ffmt); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3148 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3149 break; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3150 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3151 default: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3152 retval = false; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3153 (*current_liboctave_error_handler) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3154 ("read: invalid type specification"); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3155 break; |
4944 | 3156 } |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3157 |
4944 | 3158 |
3159 return retval; | |
3160 } | |
3161 | |
3162 octave_value | |
5275 | 3163 octave_stream::read (const Array<double>& size, octave_idx_type block_size, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3164 oct_data_conv::data_type input_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3165 oct_data_conv::data_type output_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3166 octave_idx_type skip, oct_mach_info::float_format ffmt, |
18209
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3167 octave_idx_type& count) |
4944 | 3168 { |
3169 octave_value retval; | |
3170 | |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3171 octave_idx_type nr = -1; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3172 octave_idx_type nc = -1; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3173 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3174 bool one_elt_size_spec = false; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3175 |
5659 | 3176 if (stream_ok ()) |
4944 | 3177 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3178 // FIXME: we may eventually want to make this extensible. |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3179 |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3180 // FIXME: we need a better way to ensure that this |
4944 | 3181 // numbering stays consistent with the order of the elements in the |
3182 // data_type enum in the oct_data_conv class. | |
3183 | |
18209
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3184 // Expose this in a future version? |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3185 octave_idx_type char_count = 0; |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3186 |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3187 count = 0; |
4944 | 3188 |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3189 get_size (size, nr, nc, one_elt_size_spec, "fread"); |
4944 | 3190 |
3191 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3192 { |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3193 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3194 octave_idx_type elts_to_read |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3195 = std::numeric_limits<octave_idx_type>::max (); |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3196 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3197 if (one_elt_size_spec) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3198 { |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3199 // If NR == 0, Matlab returns [](0x0). |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3200 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3201 // If NR > 0, the result will be a column vector with the given |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3202 // number of rows. |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3203 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3204 // If NR < 0, then we have Inf and the result will be a column |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3205 // vector but we have to wait to see how big NR will be. |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3206 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3207 if (nr == 0) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3208 nr = nc = 0; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3209 else |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3210 nc = 1; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3211 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3212 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3213 { |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3214 // Matlab returns [] even if there are two elements in the size |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3215 // specification and one is nonzero. |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3216 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3217 // If NC < 0 we have [NR, Inf] and we'll wait to decide how big NC |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3218 // should be. |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3219 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3220 if (nr == 0 || nc == 0) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3221 nr = nc = 0; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3222 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3223 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3224 // FIXME: ensure that this does not overflow. |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3225 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3226 elts_to_read = nr * nc; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3227 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3228 bool read_to_eof = elts_to_read < 0; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3229 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3230 octave_idx_type input_buf_elts = -1; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3231 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3232 if (skip == 0) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3233 { |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3234 if (read_to_eof) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3235 input_buf_elts = 1024 * 1024; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3236 else |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3237 input_buf_elts = elts_to_read; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3238 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3239 else |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3240 input_buf_elts = block_size; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3241 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3242 octave_idx_type input_elt_size |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3243 = oct_data_conv::data_type_size (input_type); |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3244 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3245 octave_idx_type input_buf_size = input_buf_elts * input_elt_size; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3246 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3247 assert (input_buf_size >= 0); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3248 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3249 // Must also work and return correct type object |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3250 // for 0 elements to read. |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3251 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3252 std::istream *isp = input_stream (); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3253 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3254 if (isp) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3255 { |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3256 std::istream& is = *isp; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3257 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3258 std::list <void *> input_buf_list; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3259 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3260 while (is && ! is.eof () |
18209
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3261 && (read_to_eof || count < elts_to_read)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3262 { |
18512
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3263 if (! read_to_eof) |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3264 { |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3265 octave_idx_type remaining_elts = elts_to_read - count; |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3266 |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3267 if (remaining_elts < input_buf_elts) |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3268 input_buf_size = remaining_elts * input_elt_size; |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3269 } |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3270 |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3271 char *input_buf = new char [input_buf_size]; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3272 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3273 is.read (input_buf, input_buf_size); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3274 |
18209
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3275 size_t gcount = is.gcount (); |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3276 |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3277 char_count += gcount; |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3278 |
18518
0bdecd41b2dd
correctly size fread result (bug #41648)
John W. Eaton <jwe@octave.org>
parents:
18512
diff
changeset
|
3279 octave_idx_type nel = gcount / input_elt_size; |
18512
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3280 |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3281 count += nel; |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3282 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3283 input_buf_list.push_back (input_buf); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3284 |
18512
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3285 if (is && skip != 0 && nel == block_size) |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3286 { |
18512
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3287 // Seek to skip. If skip would move past EOF, |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3288 // position at EOF. |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3289 |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3290 off_t orig_pos = tell (); |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3291 |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3292 seek (0, SEEK_END); |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3293 |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3294 off_t eof_pos = tell (); |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3295 |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3296 // Is it possible for this to fail to return us to |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3297 // the original position? |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3298 seek (orig_pos, SEEK_SET); |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3299 |
18518
0bdecd41b2dd
correctly size fread result (bug #41648)
John W. Eaton <jwe@octave.org>
parents:
18512
diff
changeset
|
3300 off_t remaining = eof_pos - orig_pos; |
18512
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3301 |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3302 if (remaining < skip) |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3303 seek (0, SEEK_END); |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3304 else |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3305 seek (skip, SEEK_CUR); |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3306 |
fdd27f68b011
handle fread skip parameter correctly (bug #41648)
Rik <rik@octave.org> and John W. Eaton <jwe@octave.org>
parents:
18209
diff
changeset
|
3307 if (! is) |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3308 break; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3309 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3310 } |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3311 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3312 if (read_to_eof) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3313 { |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3314 if (nc < 0) |
18518
0bdecd41b2dd
correctly size fread result (bug #41648)
John W. Eaton <jwe@octave.org>
parents:
18512
diff
changeset
|
3315 { |
0bdecd41b2dd
correctly size fread result (bug #41648)
John W. Eaton <jwe@octave.org>
parents:
18512
diff
changeset
|
3316 nc = count / nr; |
0bdecd41b2dd
correctly size fread result (bug #41648)
John W. Eaton <jwe@octave.org>
parents:
18512
diff
changeset
|
3317 |
0bdecd41b2dd
correctly size fread result (bug #41648)
John W. Eaton <jwe@octave.org>
parents:
18512
diff
changeset
|
3318 if (count % nr != 0) |
0bdecd41b2dd
correctly size fread result (bug #41648)
John W. Eaton <jwe@octave.org>
parents:
18512
diff
changeset
|
3319 nc ++; |
0bdecd41b2dd
correctly size fread result (bug #41648)
John W. Eaton <jwe@octave.org>
parents:
18512
diff
changeset
|
3320 } |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3321 else |
18209
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3322 nr = count; |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3323 } |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3324 else if (count == 0) |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3325 { |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3326 nr = 0; |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3327 nc = 0; |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3328 } |
18209
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3329 else if (count != nr * nc) |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3330 { |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3331 if (count % nr != 0) |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3332 nc = count / nr + 1; |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3333 |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3334 if (count < nr) |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3335 nr = count; |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3336 } |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3337 |
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3338 retval = finalize_read (input_buf_list, input_buf_elts, count, |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3339 nr, nc, input_type, output_type, ffmt); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3340 } |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3341 else |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3342 error ("fread: invalid input stream"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3343 } |
4944 | 3344 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3345 invalid_operation ("fread", "reading"); |
4944 | 3346 } |
2117 | 3347 |
3348 return retval; | |
3349 } | |
3350 | |
5275 | 3351 octave_idx_type |
3352 octave_stream::write (const octave_value& data, octave_idx_type block_size, | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3353 oct_data_conv::data_type output_type, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3354 octave_idx_type skip, oct_mach_info::float_format flt_fmt) |
2117 | 3355 { |
5275 | 3356 octave_idx_type retval = -1; |
2117 | 3357 |
5659 | 3358 if (stream_ok ()) |
4944 | 3359 { |
3360 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3361 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3362 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3363 flt_fmt = float_format (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3364 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3365 octave_idx_type status = data.write (*this, block_size, output_type, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3366 skip, flt_fmt); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3367 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3368 if (status < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3369 error ("fwrite: write error"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3370 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3371 retval = status; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3372 } |
4944 | 3373 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3374 invalid_operation ("fwrite", "writing"); |
4944 | 3375 } |
2117 | 3376 |
3377 return retval; | |
3378 } | |
3379 | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3380 template <class T, class V> |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3381 static void |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3382 convert_ints (const T *data, void *conv_data, octave_idx_type n_elts, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3383 bool swap) |
4944 | 3384 { |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3385 typedef typename V::val_type val_type; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3386 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3387 val_type *vt_data = static_cast <val_type *> (conv_data); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3388 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3389 for (octave_idx_type i = 0; i < n_elts; i++) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3390 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3391 V val (data[i]); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3392 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3393 vt_data[i] = val.value (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3394 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3395 if (swap) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3396 swap_bytes<sizeof (val_type)> (&vt_data[i]); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3397 } |
4944 | 3398 } |
3399 | |
3400 template <class T> | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3401 static bool |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3402 convert_data (const T *data, void *conv_data, octave_idx_type n_elts, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3403 oct_data_conv::data_type output_type, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3404 oct_mach_info::float_format flt_fmt) |
4944 | 3405 { |
3406 bool retval = true; | |
3407 | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3408 bool swap |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3409 = ((oct_mach_info::words_big_endian () |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3410 && flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3411 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3412 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3413 bool do_float_conversion = flt_fmt != oct_mach_info::float_format (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3414 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3415 // We use octave_intN classes here instead of converting directly to |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3416 // intN_t so that we get integer saturation semantics. |
4944 | 3417 |
3418 switch (output_type) | |
3419 { | |
3420 case oct_data_conv::dt_char: | |
3421 case oct_data_conv::dt_schar: | |
3422 case oct_data_conv::dt_int8: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3423 convert_ints<T, octave_int8> (data, conv_data, n_elts, swap); |
4944 | 3424 break; |
3425 | |
3426 case oct_data_conv::dt_uchar: | |
3427 case oct_data_conv::dt_uint8: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3428 convert_ints<T, octave_uint8> (data, conv_data, n_elts, swap); |
4944 | 3429 break; |
3430 | |
3431 case oct_data_conv::dt_int16: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3432 convert_ints<T, octave_int16> (data, conv_data, n_elts, swap); |
4944 | 3433 break; |
3434 | |
3435 case oct_data_conv::dt_uint16: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3436 convert_ints<T, octave_uint16> (data, conv_data, n_elts, swap); |
4944 | 3437 break; |
3438 | |
3439 case oct_data_conv::dt_int32: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3440 convert_ints<T, octave_int32> (data, conv_data, n_elts, swap); |
4944 | 3441 break; |
3442 | |
3443 case oct_data_conv::dt_uint32: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3444 convert_ints<T, octave_uint32> (data, conv_data, n_elts, swap); |
4944 | 3445 break; |
3446 | |
3447 case oct_data_conv::dt_int64: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3448 convert_ints<T, octave_int64> (data, conv_data, n_elts, swap); |
4944 | 3449 break; |
3450 | |
3451 case oct_data_conv::dt_uint64: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3452 convert_ints<T, octave_uint64> (data, conv_data, n_elts, swap); |
4944 | 3453 break; |
3454 | |
3455 case oct_data_conv::dt_single: | |
3456 { | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3457 float *vt_data = static_cast <float *> (conv_data); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3458 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3459 for (octave_idx_type i = 0; i < n_elts; i++) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3460 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3461 vt_data[i] = data[i]; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3462 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3463 if (do_float_conversion) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3464 do_float_format_conversion (&vt_data[i], 1, flt_fmt); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3465 } |
4944 | 3466 } |
3467 break; | |
3468 | |
3469 case oct_data_conv::dt_double: | |
3470 { | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3471 double *vt_data = static_cast <double *> (conv_data); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3472 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3473 for (octave_idx_type i = 0; i < n_elts; i++) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3474 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3475 vt_data[i] = data[i]; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3476 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3477 if (do_float_conversion) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3478 do_double_format_conversion (&vt_data[i], 1, flt_fmt); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3479 } |
4944 | 3480 } |
3481 break; | |
3482 | |
3483 default: | |
3484 retval = false; | |
3485 (*current_liboctave_error_handler) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3486 ("write: invalid type specification"); |
4944 | 3487 break; |
3488 } | |
3489 | |
3490 return retval; | |
3491 } | |
3492 | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3493 bool |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3494 octave_stream::write_bytes (const void *data, size_t nbytes) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3495 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3496 bool status = false; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3497 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3498 std::ostream *osp = output_stream (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3499 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3500 if (osp) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3501 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3502 std::ostream& os = *osp; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3503 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3504 if (os) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3505 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3506 os.write (static_cast<const char *> (data), nbytes); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3507 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3508 if (os) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3509 status = true; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3510 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3511 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3512 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3513 return status; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3514 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3515 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3516 bool |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3517 octave_stream::skip_bytes (size_t skip) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3518 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3519 bool status = false; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3520 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3521 std::ostream *osp = output_stream (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3522 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3523 if (osp) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3524 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3525 std::ostream& os = *osp; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3526 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3527 // Seek to skip when inside bounds of existing file. |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3528 // Otherwise, write NUL to skip. |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3529 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3530 off_t orig_pos = tell (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3531 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3532 seek (0, SEEK_END); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3533 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3534 off_t eof_pos = tell (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3535 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3536 // Is it possible for this to fail to return us to the |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3537 // original position? |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3538 seek (orig_pos, SEEK_SET); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3539 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3540 size_t remaining = eof_pos - orig_pos; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3541 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3542 if (remaining < skip) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3543 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3544 seek (0, SEEK_END); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3545 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3546 // FIXME: probably should try to write larger blocks... |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3547 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3548 unsigned char zero = 0; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3549 for (size_t j = 0; j < skip - remaining; j++) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3550 os.write (reinterpret_cast<const char *> (&zero), 1); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3551 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3552 else |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3553 seek (skip, SEEK_CUR); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3554 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3555 if (os) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3556 status = true; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3557 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3558 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3559 return status; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3560 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3561 |
4944 | 3562 template <class T> |
5275 | 3563 octave_idx_type |
3564 octave_stream::write (const Array<T>& data, octave_idx_type block_size, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3565 oct_data_conv::data_type output_type, |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3566 octave_idx_type skip, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3567 oct_mach_info::float_format flt_fmt) |
4944 | 3568 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3569 bool swap = ((oct_mach_info::words_big_endian () |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3570 && flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3571 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3572 |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3573 bool do_data_conversion = (swap || ! is_equivalent_type<T> (output_type) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3574 || flt_fmt != oct_mach_info::float_format ()); |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3575 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3576 octave_idx_type nel = data.numel (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3577 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3578 octave_idx_type chunk_size; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3579 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3580 if (skip != 0) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3581 chunk_size = block_size; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3582 else if (do_data_conversion) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3583 chunk_size = 1024 * 1024; |
4944 | 3584 else |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3585 chunk_size = nel; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3586 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3587 octave_idx_type i = 0; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3588 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3589 const T *pdata = data.data (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3590 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3591 while (i < nel) |
4944 | 3592 { |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3593 if (skip != 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3594 { |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3595 if (! skip_bytes (skip)) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3596 return -1; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3597 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3598 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3599 octave_idx_type remaining_nel = nel - i; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3600 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3601 if (chunk_size > remaining_nel) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3602 chunk_size = remaining_nel; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3603 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3604 bool status = false; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3605 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3606 if (do_data_conversion) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3607 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3608 size_t output_size |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3609 = chunk_size * oct_data_conv::data_type_size (output_type); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3610 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3611 OCTAVE_LOCAL_BUFFER (unsigned char, conv_data, output_size); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3612 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3613 status = convert_data (&pdata[i], conv_data, chunk_size, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3614 output_type, flt_fmt); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3615 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3616 if (status) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3617 status = write_bytes (conv_data, output_size); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3618 } |
4944 | 3619 else |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3620 status = write_bytes (pdata, sizeof (T) * chunk_size); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3621 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3622 if (! status) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3623 return -1; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3624 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3625 i += chunk_size; |
4944 | 3626 } |
3627 | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3628 return nel; |
4944 | 3629 } |
3630 | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3631 #define INSTANTIATE_WRITE(T) \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3632 template \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3633 octave_idx_type \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3634 octave_stream::write (const Array<T>& data, octave_idx_type block_size, \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3635 oct_data_conv::data_type output_type, \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3636 octave_idx_type skip, \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3637 oct_mach_info::float_format flt_fmt) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3638 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3639 INSTANTIATE_WRITE (octave_int8); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3640 INSTANTIATE_WRITE (octave_uint8); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3641 INSTANTIATE_WRITE (octave_int16); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3642 INSTANTIATE_WRITE (octave_uint16); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3643 INSTANTIATE_WRITE (octave_int32); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3644 INSTANTIATE_WRITE (octave_uint32); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3645 INSTANTIATE_WRITE (octave_int64); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3646 INSTANTIATE_WRITE (octave_uint64); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3647 INSTANTIATE_WRITE (int8_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3648 INSTANTIATE_WRITE (uint8_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3649 INSTANTIATE_WRITE (int16_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3650 INSTANTIATE_WRITE (uint16_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3651 INSTANTIATE_WRITE (int32_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3652 INSTANTIATE_WRITE (uint32_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3653 INSTANTIATE_WRITE (int64_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3654 INSTANTIATE_WRITE (uint64_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3655 INSTANTIATE_WRITE (bool); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3656 INSTANTIATE_WRITE (char); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3657 INSTANTIATE_WRITE (float); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3658 INSTANTIATE_WRITE (double); |
4944 | 3659 |
2117 | 3660 octave_value |
3810 | 3661 octave_stream::scanf (const std::string& fmt, const Array<double>& size, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3662 octave_idx_type& count, const std::string& who) |
2117 | 3663 { |
3664 octave_value retval; | |
3665 | |
5659 | 3666 if (stream_ok ()) |
4468 | 3667 retval = rep->scanf (fmt, size, count, who); |
2117 | 3668 |
3669 return retval; | |
3670 } | |
3671 | |
5279 | 3672 octave_value |
3673 octave_stream::scanf (const octave_value& fmt, const Array<double>& size, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3674 octave_idx_type& count, const std::string& who) |
5279 | 3675 { |
3676 octave_value retval = Matrix (); | |
3677 | |
3678 if (fmt.is_string ()) | |
3679 { | |
3680 std::string sfmt = fmt.string_value (); | |
3681 | |
3682 if (fmt.is_sq_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3683 sfmt = do_string_escapes (sfmt); |
5279 | 3684 |
3685 retval = scanf (sfmt, size, count, who); | |
3686 } | |
3687 else | |
3688 { | |
3689 // Note that this is not ::error () ! | |
3690 | |
3691 error (who + ": format must be a string"); | |
3692 } | |
3693 | |
3694 return retval; | |
3695 } | |
3696 | |
2215 | 3697 octave_value_list |
4468 | 3698 octave_stream::oscanf (const std::string& fmt, const std::string& who) |
2215 | 3699 { |
3700 octave_value_list retval; | |
3701 | |
5659 | 3702 if (stream_ok ()) |
4468 | 3703 retval = rep->oscanf (fmt, who); |
2215 | 3704 |
3705 return retval; | |
3706 } | |
3707 | |
5279 | 3708 octave_value_list |
3709 octave_stream::oscanf (const octave_value& fmt, const std::string& who) | |
3710 { | |
3711 octave_value_list retval; | |
3712 | |
3713 if (fmt.is_string ()) | |
3714 { | |
3715 std::string sfmt = fmt.string_value (); | |
3716 | |
3717 if (fmt.is_sq_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3718 sfmt = do_string_escapes (sfmt); |
5279 | 3719 |
3720 retval = oscanf (sfmt, who); | |
3721 } | |
3722 else | |
3723 { | |
3724 // Note that this is not ::error () ! | |
3725 | |
3726 error (who + ": format must be a string"); | |
3727 } | |
3728 | |
3729 return retval; | |
3730 } | |
3731 | |
2117 | 3732 int |
4468 | 3733 octave_stream::printf (const std::string& fmt, const octave_value_list& args, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3734 const std::string& who) |
2117 | 3735 { |
3736 int retval = -1; | |
3737 | |
5659 | 3738 if (stream_ok ()) |
4468 | 3739 retval = rep->printf (fmt, args, who); |
2117 | 3740 |
3741 return retval; | |
3742 } | |
3743 | |
3744 int | |
5279 | 3745 octave_stream::printf (const octave_value& fmt, const octave_value_list& args, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3746 const std::string& who) |
5279 | 3747 { |
3748 int retval = 0; | |
3749 | |
3750 if (fmt.is_string ()) | |
3751 { | |
3752 std::string sfmt = fmt.string_value (); | |
3753 | |
3754 if (fmt.is_sq_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3755 sfmt = do_string_escapes (sfmt); |
5279 | 3756 |
3757 retval = printf (sfmt, args, who); | |
3758 } | |
3759 else | |
3760 { | |
3761 // Note that this is not ::error () ! | |
3762 | |
3763 error (who + ": format must be a string"); | |
3764 } | |
3765 | |
3766 return retval; | |
3767 } | |
3768 | |
3769 int | |
4468 | 3770 octave_stream::puts (const std::string& s, const std::string& who) |
2117 | 3771 { |
3772 int retval = -1; | |
3773 | |
5659 | 3774 if (stream_ok ()) |
4468 | 3775 retval = rep->puts (s, who); |
2117 | 3776 |
3777 return retval; | |
3778 } | |
3779 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3780 // FIXME: maybe this should work for string arrays too. |
2117 | 3781 |
3782 int | |
4468 | 3783 octave_stream::puts (const octave_value& tc_s, const std::string& who) |
2117 | 3784 { |
3785 int retval = -1; | |
3786 | |
3787 if (tc_s.is_string ()) | |
3788 { | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
3789 std::string s = tc_s.string_value (); |
5279 | 3790 retval = puts (s, who); |
2117 | 3791 } |
3792 else | |
4468 | 3793 { |
3794 // Note that this is not ::error () ! | |
3795 | |
3796 error (who + ": argument must be a string"); | |
3797 } | |
2117 | 3798 |
3799 return retval; | |
3800 } | |
3801 | |
3802 bool | |
3803 octave_stream::eof (void) const | |
3804 { | |
3805 int retval = -1; | |
3806 | |
5659 | 3807 if (stream_ok ()) |
2117 | 3808 retval = rep->eof (); |
3809 | |
3810 return retval; | |
3811 } | |
3812 | |
3536 | 3813 std::string |
2435 | 3814 octave_stream::error (bool clear, int& err_num) |
2117 | 3815 { |
5649 | 3816 std::string retval = "invalid stream object"; |
3817 | |
5659 | 3818 if (stream_ok (false)) |
2435 | 3819 retval = rep->error (clear, err_num); |
2117 | 3820 |
3821 return retval; | |
3822 } | |
3823 | |
3536 | 3824 std::string |
3340 | 3825 octave_stream::name (void) const |
2117 | 3826 { |
3523 | 3827 std::string retval; |
2117 | 3828 |
5659 | 3829 if (stream_ok ()) |
2117 | 3830 retval = rep->name (); |
3831 | |
3832 return retval; | |
3833 } | |
3834 | |
3835 int | |
3340 | 3836 octave_stream::mode (void) const |
2117 | 3837 { |
3838 int retval = 0; | |
3839 | |
5659 | 3840 if (stream_ok ()) |
2117 | 3841 retval = rep->mode (); |
3842 | |
3843 return retval; | |
3844 } | |
3845 | |
2317 | 3846 oct_mach_info::float_format |
3340 | 3847 octave_stream::float_format (void) const |
2117 | 3848 { |
4574 | 3849 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
2317 | 3850 |
5659 | 3851 if (stream_ok ()) |
2317 | 3852 retval = rep->float_format (); |
2117 | 3853 |
3854 return retval; | |
3855 } | |
3856 | |
3536 | 3857 std::string |
2117 | 3858 octave_stream::mode_as_string (int mode) |
3859 { | |
3523 | 3860 std::string retval = "???"; |
3775 | 3861 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode); |
3862 | |
3863 if (in_mode == std::ios::in) | |
3864 retval = "r"; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
3865 else if (in_mode == std::ios::out |
4078 | 3866 || in_mode == (std::ios::out | std::ios::trunc)) |
3775 | 3867 retval = "w"; |
4078 | 3868 else if (in_mode == (std::ios::out | std::ios::app)) |
3775 | 3869 retval = "a"; |
4078 | 3870 else if (in_mode == (std::ios::in | std::ios::out)) |
3775 | 3871 retval = "r+"; |
4078 | 3872 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc)) |
3775 | 3873 retval = "w+"; |
4078 | 3874 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate)) |
3775 | 3875 retval = "a+"; |
4078 | 3876 else if (in_mode == (std::ios::in | std::ios::binary)) |
3775 | 3877 retval = "rb"; |
4078 | 3878 else if (in_mode == (std::ios::out | std::ios::binary) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3879 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary)) |
3775 | 3880 retval = "wb"; |
4078 | 3881 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary)) |
3775 | 3882 retval = "ab"; |
4078 | 3883 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary)) |
3775 | 3884 retval = "r+b"; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
3885 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3886 | std::ios::binary)) |
3775 | 3887 retval = "w+b"; |
4078 | 3888 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3889 | std::ios::binary)) |
3775 | 3890 retval = "a+b"; |
2117 | 3891 |
3892 return retval; | |
3893 } | |
3894 | |
3895 octave_stream_list *octave_stream_list::instance = 0; | |
3896 | |
2926 | 3897 bool |
3898 octave_stream_list::instance_ok (void) | |
3899 { | |
3900 bool retval = true; | |
3901 | |
3902 if (! instance) | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3903 { |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3904 instance = new octave_stream_list (); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3905 |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3906 if (instance) |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3907 singleton_cleanup_list::add (cleanup_instance); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3908 } |
2926 | 3909 |
3910 if (! instance) | |
3911 { | |
3912 ::error ("unable to create stream list object!"); | |
3913 | |
3914 retval = false; | |
3915 } | |
3916 | |
3917 return retval; | |
3918 } | |
3919 | |
5353 | 3920 int |
6757 | 3921 octave_stream_list::insert (octave_stream& os) |
2926 | 3922 { |
5353 | 3923 return (instance_ok ()) ? instance->do_insert (os) : -1; |
2926 | 3924 } |
3925 | |
3340 | 3926 octave_stream |
3523 | 3927 octave_stream_list::lookup (int fid, const std::string& who) |
2926 | 3928 { |
3341 | 3929 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926 | 3930 } |
3931 | |
3340 | 3932 octave_stream |
3523 | 3933 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926 | 3934 { |
3341 | 3935 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926 | 3936 } |
3937 | |
3938 int | |
3523 | 3939 octave_stream_list::remove (int fid, const std::string& who) |
2926 | 3940 { |
3341 | 3941 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926 | 3942 } |
3943 | |
3944 int | |
3523 | 3945 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926 | 3946 { |
3341 | 3947 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926 | 3948 } |
3949 | |
3950 void | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
3951 octave_stream_list::clear (bool flush) |
2926 | 3952 { |
3953 if (instance) | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
3954 instance->do_clear (flush); |
2926 | 3955 } |
3956 | |
3957 string_vector | |
3958 octave_stream_list::get_info (int fid) | |
3959 { | |
3960 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); | |
3961 } | |
3962 | |
3963 string_vector | |
3964 octave_stream_list::get_info (const octave_value& fid) | |
3965 { | |
3966 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); | |
3967 } | |
3968 | |
3536 | 3969 std::string |
2926 | 3970 octave_stream_list::list_open_files (void) |
3971 { | |
3523 | 3972 return (instance_ok ()) ? instance->do_list_open_files () : std::string (); |
2926 | 3973 } |
3974 | |
3975 octave_value | |
3976 octave_stream_list::open_file_numbers (void) | |
3977 { | |
3978 return (instance_ok ()) | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3979 ? instance->do_open_file_numbers () : octave_value (); |
2926 | 3980 } |
3981 | |
3982 int | |
3983 octave_stream_list::get_file_number (const octave_value& fid) | |
3984 { | |
3985 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; | |
3986 } | |
3987 | |
5353 | 3988 int |
6757 | 3989 octave_stream_list::do_insert (octave_stream& os) |
2117 | 3990 { |
6757 | 3991 // Insert item with key corresponding to file-descriptor. |
3992 | |
3993 int stream_number; | |
3994 | |
3995 if ((stream_number = os.file_number ()) == -1) | |
3996 return stream_number; | |
3997 | |
3998 // Should we test for "(list.find (stream_number) != list.end ()) && | |
3999 // list[stream_number].is_open ()" and respond with "error | |
4000 // ("internal error: ...")"? It should not happen except for some | |
4001 // bug or if the user has opened a stream with an interpreted | |
4002 // command, but closed it directly with a system call in an | |
4003 // oct-file; then the kernel knows the fd is free, but Octave does | |
4004 // not know. If it happens, it should not do harm here to simply | |
4005 // overwrite this entry, although the wrong entry might have done | |
4006 // harm before. | |
4007 | |
4008 if (list.size () < list.max_size ()) | |
4009 list[stream_number] = os; | |
4010 else | |
2117 | 4011 { |
6757 | 4012 stream_number = -1; |
4013 error ("could not create file id"); | |
3340 | 4014 } |
2117 | 4015 |
5353 | 4016 return stream_number; |
6757 | 4017 |
2117 | 4018 } |
4019 | |
3341 | 4020 static void |
3523 | 4021 gripe_invalid_file_id (int fid, const std::string& who) |
3341 | 4022 { |
4023 if (who.empty ()) | |
4024 ::error ("invalid stream number = %d", fid); | |
4025 else | |
4026 ::error ("%s: invalid stream number = %d", who.c_str (), fid); | |
4027 } | |
4028 | |
3340 | 4029 octave_stream |
3523 | 4030 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117 | 4031 { |
3340 | 4032 octave_stream retval; |
2117 | 4033 |
6757 | 4034 if (fid >= 0) |
4035 { | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4036 if (lookup_cache != list.end () && lookup_cache->first == fid) |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4037 retval = lookup_cache->second; |
6757 | 4038 else |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4039 { |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4040 ostrl_map::const_iterator iter = list.find (fid); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4041 |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4042 if (iter != list.end ()) |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4043 { |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4044 retval = iter->second; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4045 lookup_cache = iter; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4046 } |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4047 else |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4048 gripe_invalid_file_id (fid, who); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4049 } |
6757 | 4050 } |
3341 | 4051 else |
4052 gripe_invalid_file_id (fid, who); | |
2117 | 4053 |
4054 return retval; | |
4055 } | |
4056 | |
3340 | 4057 octave_stream |
3341 | 4058 octave_stream_list::do_lookup (const octave_value& fid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4059 const std::string& who) const |
2117 | 4060 { |
3340 | 4061 octave_stream retval; |
2117 | 4062 |
4063 int i = get_file_number (fid); | |
4064 | |
4065 if (! error_state) | |
3341 | 4066 retval = do_lookup (i, who); |
2117 | 4067 |
4068 return retval; | |
4069 } | |
4070 | |
4071 int | |
3523 | 4072 octave_stream_list::do_remove (int fid, const std::string& who) |
2117 | 4073 { |
4074 int retval = -1; | |
4075 | |
3531 | 4076 // Can't remove stdin (std::cin), stdout (std::cout), or stderr |
4077 // (std::cerr). | |
2117 | 4078 |
6757 | 4079 if (fid > 2) |
2117 | 4080 { |
6757 | 4081 ostrl_map::iterator iter = list.find (fid); |
4082 | |
4083 if (iter != list.end ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4084 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4085 octave_stream os = iter->second; |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4086 list.erase (iter); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4087 lookup_cache = list.end (); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4088 |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4089 // FIXME: is this check redundant? |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4090 if (os.is_valid ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4091 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4092 os.close (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4093 retval = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4094 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4095 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4096 gripe_invalid_file_id (fid, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4097 } |
3341 | 4098 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4099 gripe_invalid_file_id (fid, who); |
2117 | 4100 } |
3341 | 4101 else |
4102 gripe_invalid_file_id (fid, who); | |
2117 | 4103 |
4104 return retval; | |
4105 } | |
4106 | |
4107 int | |
3523 | 4108 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117 | 4109 { |
4110 int retval = -1; | |
4111 | |
6054 | 4112 if (fid.is_string () && fid.string_value () == "all") |
4113 { | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4114 do_clear (false); |
6054 | 4115 |
4116 retval = 0; | |
4117 } | |
4118 else | |
4119 { | |
4120 int i = get_file_number (fid); | |
4121 | |
4122 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4123 retval = do_remove (i, who); |
6054 | 4124 } |
2117 | 4125 |
4126 return retval; | |
4127 } | |
4128 | |
4129 void | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4130 octave_stream_list::do_clear (bool flush) |
2117 | 4131 { |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4132 if (flush) |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4133 { |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4134 // Do flush stdout and stderr. |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4135 |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4136 list[0].flush (); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4137 list[1].flush (); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4138 } |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4139 |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4140 octave_stream saved_os[3]; |
2117 | 4141 // But don't delete them or stdin. |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4142 for (ostrl_map::iterator iter = list.begin (); iter != list.end (); iter++) |
6757 | 4143 { |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4144 int fid = iter->first; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4145 octave_stream os = iter->second; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4146 if (fid < 3) |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4147 saved_os[fid] = os; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4148 else if (os.is_valid ()) |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4149 os.close (); |
6757 | 4150 } |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4151 list.clear (); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4152 for (int fid = 0; fid < 3; fid++) list[fid] = saved_os[fid]; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4153 lookup_cache = list.end (); |
2117 | 4154 } |
4155 | |
4156 string_vector | |
4157 octave_stream_list::do_get_info (int fid) const | |
4158 { | |
4159 string_vector retval; | |
4160 | |
3340 | 4161 octave_stream os = do_lookup (fid); |
2117 | 4162 |
3341 | 4163 if (os.is_valid ()) |
2117 | 4164 { |
4165 retval.resize (3); | |
4166 | |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
13983
diff
changeset
|
4167 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); |
3340 | 4168 retval(1) = octave_stream::mode_as_string (os.mode ()); |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
13983
diff
changeset
|
4169 retval(0) = os.name (); |
2117 | 4170 } |
4171 else | |
3341 | 4172 ::error ("invalid file id = %d", fid); |
2117 | 4173 |
4174 return retval; | |
4175 } | |
4176 | |
4177 string_vector | |
4178 octave_stream_list::do_get_info (const octave_value& fid) const | |
4179 { | |
4180 string_vector retval; | |
4181 | |
4182 int conv_err = 0; | |
4183 | |
4184 int int_fid = convert_to_valid_int (fid, conv_err); | |
4185 | |
4186 if (! conv_err) | |
4187 retval = do_get_info (int_fid); | |
4188 else | |
2915 | 4189 ::error ("file id must be a file object or integer value"); |
2117 | 4190 |
4191 return retval; | |
4192 } | |
4193 | |
3536 | 4194 std::string |
2117 | 4195 octave_stream_list::do_list_open_files (void) const |
4196 { | |
3523 | 4197 std::string retval; |
2117 | 4198 |
5765 | 4199 std::ostringstream buf; |
2117 | 4200 |
4201 buf << "\n" | |
4202 << " number mode arch name\n" | |
4203 << " ------ ---- ---- ----\n"; | |
4204 | |
6757 | 4205 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4206 { |
6757 | 4207 octave_stream os = p->second; |
2117 | 4208 |
4326 | 4209 buf << " " |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4210 << std::setiosflags (std::ios::right) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4211 << std::setw (4) << p->first << " " |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4212 << std::setiosflags (std::ios::left) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4213 << std::setw (3) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4214 << octave_stream::mode_as_string (os.mode ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4215 << " " |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4216 << std::setw (9) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4217 << oct_mach_info::float_format_as_string (os.float_format ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4218 << " " |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4219 << os.name () << "\n"; |
2117 | 4220 } |
4221 | |
5765 | 4222 buf << "\n"; |
4223 | |
4224 retval = buf.str (); | |
2117 | 4225 |
4226 return retval; | |
4227 } | |
4228 | |
4229 octave_value | |
4230 octave_stream_list::do_open_file_numbers (void) const | |
4231 { | |
6757 | 4232 Matrix retval (1, list.size (), 0.0); |
2117 | 4233 |
4234 int num_open = 0; | |
4235 | |
6757 | 4236 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4237 { |
6757 | 4238 // Skip stdin, stdout, and stderr. |
4239 | |
4240 if (p->first > 2 && p->second) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4241 retval(0,num_open++) = p->first; |
2117 | 4242 } |
4243 | |
4244 retval.resize ((num_open > 0), num_open); | |
4245 | |
4246 return retval; | |
4247 } | |
4248 | |
4249 int | |
2609 | 4250 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117 | 4251 { |
4252 int retval = -1; | |
4253 | |
4254 if (fid.is_string ()) | |
4255 { | |
3523 | 4256 std::string nm = fid.string_value (); |
2117 | 4257 |
6757 | 4258 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4259 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4260 // stdin (std::cin), stdout (std::cout), and stderr (std::cerr) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4261 // are unnamed. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4262 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4263 if (p->first > 2) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4264 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4265 octave_stream os = p->second; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4266 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4267 if (os && os.name () == nm) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4268 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4269 retval = p->first; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4270 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4271 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4272 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4273 } |
2117 | 4274 } |
4275 else | |
4276 { | |
4277 int conv_err = 0; | |
4278 | |
4279 int int_fid = convert_to_valid_int (fid, conv_err); | |
4280 | |
4281 if (conv_err) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4282 ::error ("file id must be a file object, std::string, or integer value"); |
2117 | 4283 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4284 retval = int_fid; |
2117 | 4285 } |
4286 | |
4287 return retval; | |
4288 } |