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