Mercurial > octave-nkf
annotate src/oct-stream.cc @ 14022:de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
* balance.cc, getgrent.cc, getpwent.cc, spparms.cc, sqrtm.cc, urlwrite.cc,
data.cc, dirfns.cc, graphics.cc, oct-stream.cc, syscalls.cc:
Return retval(1) before retval(0) to avoid re-sizing call on retval.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Fri, 09 Dec 2011 11:17:13 -0800 |
parents | 7dd7cccf0757 |
children | 72c96de7a403 |
rev | line source |
---|---|
2117 | 1 /* |
2 | |
11523 | 3 Copyright (C) 1996-2011 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" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
511 << "char_class: `" << undo_string_escapes (elt->char_class) << "'\n" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
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" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
870 << "flags: `" << elt->flags << "'\n" |
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" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
873 << "type: `" << elt->type << "'\n" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
874 << "modifier: `" << elt->modifier << "'\n" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
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 | |
9701 | 1031 long |
1032 octave_base_stream::skipl (long num, bool& err, const std::string& who) | |
1033 { | |
1034 long cnt = -1; | |
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 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1110 int c1 = is.get (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1111 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1112 if (! is.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1113 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1114 if (c1 == '0') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1115 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1116 int c2 = is.peek (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1117 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1118 if (c2 == 'x' || c2 == 'X') |
7709
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1119 { |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1120 is.ignore (); |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1121 if (std::isxdigit (is.peek ())) |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1122 is >> std::hex >> ref >> std::dec; |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1123 else |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1124 ref = 0; |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1125 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1126 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1127 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1128 if (c2 == '0' || c2 == '1' || c2 == '2' |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1129 || c2 == '3' || c2 == '4' || c2 == '5' |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1130 || c2 == '6' || c2 == '7') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1131 is >> std::oct >> ref >> std::dec; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1132 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1133 ref = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1134 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1135 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1136 else |
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 is.putback (c1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1139 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1140 is >> ref; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1141 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1142 } |
4926 | 1143 } |
3779 | 1144 break; |
1145 | |
1146 default: | |
1147 is >> ref; | |
1148 break; | |
1149 } | |
3639 | 1150 |
3638 | 1151 return is; |
3636 | 1152 } |
1153 | |
6767 | 1154 template <class T> |
1155 std::istream& | |
1156 octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr) | |
1157 { | |
1158 if (fmt.width) | |
1159 { | |
1160 // Limit input to fmt.width characters by reading into a | |
1161 // temporary stringstream buffer. | |
1162 | |
1163 std::string tmp; | |
1164 | |
1165 is.width (fmt.width); | |
1166 is >> tmp; | |
1167 | |
1168 std::istringstream ss (tmp); | |
1169 | |
1170 octave_scan_1 (ss, fmt, valptr); | |
1171 } | |
1172 else | |
1173 octave_scan_1 (is, fmt, valptr); | |
1174 | |
1175 return is; | |
1176 } | |
1177 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1178 // Note that this specialization is only used for reading characters, not |
3779 | 1179 // character strings. See BEGIN_S_CONVERSION for details. |
1180 | |
1181 template<> | |
1182 std::istream& | |
4661 | 1183 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
|
1184 char* valptr) |
3779 | 1185 { |
1186 return is >> valptr; | |
1187 } | |
3636 | 1188 |
4595 | 1189 template std::istream& |
1190 octave_scan (std::istream&, const scanf_format_elt&, int*); | |
1191 | |
1192 template std::istream& | |
1193 octave_scan (std::istream&, const scanf_format_elt&, long int*); | |
1194 | |
1195 template std::istream& | |
1196 octave_scan (std::istream&, const scanf_format_elt&, short int*); | |
1197 | |
1198 template std::istream& | |
1199 octave_scan (std::istream&, const scanf_format_elt&, unsigned int*); | |
1200 | |
1201 template std::istream& | |
1202 octave_scan (std::istream&, const scanf_format_elt&, unsigned long int*); | |
1203 | |
1204 template std::istream& | |
1205 octave_scan (std::istream&, const scanf_format_elt&, unsigned short int*); | |
1206 | |
1207 #if 0 | |
1208 template std::istream& | |
1209 octave_scan (std::istream&, const scanf_format_elt&, float*); | |
1210 #endif | |
1211 | |
5403 | 1212 template<> |
5176 | 1213 std::istream& |
5403 | 1214 octave_scan<> (std::istream& is, const scanf_format_elt& fmt, double* valptr) |
5176 | 1215 { |
1216 double& ref = *valptr; | |
1217 | |
1218 switch (fmt.type) | |
1219 { | |
1220 case 'e': | |
1221 case 'f': | |
1222 case 'g': | |
1223 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1224 int c1 = EOF; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1225 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1226 while (is && (c1 = is.get ()) != EOF && isspace (c1)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1227 /* skip whitespace */; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1228 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1229 if (c1 != EOF) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1230 { |
12936
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1231 is.putback (c1); |
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1232 |
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1233 ref = octave_read_value<double> (is); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1234 } |
5176 | 1235 } |
1236 break; | |
1237 | |
1238 default: | |
1239 panic_impossible (); | |
1240 break; | |
1241 } | |
1242 | |
1243 return is; | |
1244 } | |
1245 | |
2572 | 1246 template <class T> |
1247 void | |
3636 | 1248 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
|
1249 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
|
1250 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
|
1251 bool discard) |
2572 | 1252 { |
3640 | 1253 OCTAVE_SCAN (is, fmt, valptr); |
2572 | 1254 |
1255 if (is) | |
1256 { | |
1257 if (idx == max_size && ! discard) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1258 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1259 max_size *= 2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1260 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1261 if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1262 mval.resize (nr, max_size / nr, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1263 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1264 mval.resize (max_size, 1, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1265 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1266 data = mval.fortran_vec (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1267 } |
2572 | 1268 |
1269 if (! discard) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1270 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1271 conversion_count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1272 data[idx++] = *(valptr); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1273 } |
2572 | 1274 } |
1275 } | |
1276 | |
1277 template void | |
3878 | 1278 do_scanf_conv (std::istream&, const scanf_format_elt&, int*, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1279 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2572 | 1280 |
3233 | 1281 template void |
3636 | 1282 do_scanf_conv (std::istream&, const scanf_format_elt&, long int*, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1283 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3233 | 1284 |
1285 template void | |
3636 | 1286 do_scanf_conv (std::istream&, const scanf_format_elt&, short int*, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1287 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3233 | 1288 |
3878 | 1289 template void |
1290 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned int*, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1291 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878 | 1292 |
1293 template void | |
1294 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned long int*, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1295 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878 | 1296 |
1297 template void | |
1298 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned short int*, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1299 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878 | 1300 |
2600 | 1301 #if 0 |
2572 | 1302 template void |
3636 | 1303 do_scanf_conv (std::istream&, const scanf_format_elt&, float*, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1304 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2600 | 1305 #endif |
2572 | 1306 |
1307 template void | |
3636 | 1308 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
|
1309 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2572 | 1310 |
3483 | 1311 #define DO_WHITESPACE_CONVERSION() \ |
1312 do \ | |
1313 { \ | |
1314 int c = EOF; \ | |
1315 \ | |
1316 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
|
1317 /* skip whitespace */; \ |
3483 | 1318 \ |
1319 if (c != EOF) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1320 is.putback (c); \ |
3483 | 1321 } \ |
1322 while (0) | |
1323 | |
1324 #define DO_LITERAL_CONVERSION() \ | |
1325 do \ | |
1326 { \ | |
1327 int c = EOF; \ | |
1328 \ | |
1329 int n = strlen (fmt); \ | |
1330 int i = 0; \ | |
1331 \ | |
1332 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
|
1333 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1334 if (c == static_cast<unsigned char> (fmt[i])) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1335 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1336 i++; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1337 continue; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1338 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1339 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1340 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1341 is.putback (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1342 break; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1343 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1344 } \ |
3483 | 1345 \ |
1346 if (i != n) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1347 is.setstate (std::ios::failbit); \ |
3483 | 1348 } \ |
1349 while (0) | |
1350 | |
3640 | 1351 #define DO_PCT_CONVERSION() \ |
1352 do \ | |
1353 { \ | |
1354 int c = is.get (); \ | |
1355 \ | |
1356 if (c != EOF) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1357 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1358 if (c != '%') \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1359 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1360 is.putback (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1361 is.setstate (std::ios::failbit); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1362 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1363 } \ |
3640 | 1364 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1365 is.setstate (std::ios::failbit); \ |
3640 | 1366 } \ |
1367 while (0) | |
1368 | |
3483 | 1369 #define BEGIN_C_CONVERSION() \ |
3538 | 1370 is.unsetf (std::ios::skipws); \ |
3483 | 1371 \ |
1372 int width = elt->width ? elt->width : 1; \ | |
1373 \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1374 std::string tmp (width, '\0'); \ |
3483 | 1375 \ |
1376 int c = EOF; \ | |
1377 int n = 0; \ | |
1378 \ | |
1379 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
|
1380 tmp[n++] = static_cast<char> (c); \ |
3483 | 1381 \ |
5266 | 1382 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
|
1383 is.clear (); \ |
7626f8934466
correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents:
12966
diff
changeset
|
1384 \ |
7626f8934466
correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents:
12966
diff
changeset
|
1385 tmp.resize (n) |
3483 | 1386 |
1387 // For a `%s' format, skip initial whitespace and then read until the | |
5338 | 1388 // next whitespace character or until WIDTH characters have been read. |
3483 | 1389 #define BEGIN_S_CONVERSION() \ |
1390 int width = elt->width; \ | |
1391 \ | |
4051 | 1392 std::string tmp; \ |
3483 | 1393 \ |
1394 do \ | |
1395 { \ | |
1396 if (width) \ | |
5338 | 1397 { \ |
10076 | 1398 tmp = std::string (width, '\0'); \ |
5338 | 1399 \ |
1400 int c = EOF; \ | |
1401 \ | |
1402 int n = 0; \ | |
1403 \ | |
1404 while (is && (c = is.get ()) != EOF) \ | |
1405 { \ | |
1406 if (! isspace (c)) \ | |
1407 { \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1408 tmp[n++] = static_cast<char> (c); \ |
5338 | 1409 break; \ |
1410 } \ | |
1411 } \ | |
4051 | 1412 \ |
5338 | 1413 while (is && n < width && (c = is.get ()) != EOF) \ |
1414 { \ | |
1415 if (isspace (c)) \ | |
1416 { \ | |
1417 is.putback (c); \ | |
1418 break; \ | |
1419 } \ | |
1420 else \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1421 tmp[n++] = static_cast<char> (c); \ |
5338 | 1422 } \ |
3483 | 1423 \ |
5338 | 1424 if (n > 0 && c == EOF) \ |
1425 is.clear (); \ | |
1426 \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1427 tmp.resize (n); \ |
5338 | 1428 } \ |
3483 | 1429 else \ |
5338 | 1430 { \ |
1431 is >> std::ws >> tmp; \ | |
1432 } \ | |
3483 | 1433 } \ |
1434 while (0) | |
1435 | |
1436 // This format must match a nonempty sequence of characters. | |
1437 #define BEGIN_CHAR_CLASS_CONVERSION() \ | |
1438 int width = elt->width; \ | |
1439 \ | |
4051 | 1440 std::string tmp; \ |
3483 | 1441 \ |
1442 do \ | |
1443 { \ | |
7426 | 1444 if (! width) \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1445 width = INT_MAX; \ |
7427 | 1446 \ |
7426 | 1447 std::ostringstream buf; \ |
1448 \ | |
1449 std::string char_class = elt->char_class; \ | |
4051 | 1450 \ |
7426 | 1451 int c = EOF; \ |
3483 | 1452 \ |
7426 | 1453 if (elt->type == '[') \ |
1454 { \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1455 int chars_read = 0; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1456 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
|
1457 && char_class.find (c) != std::string::npos) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1458 buf << static_cast<char> (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1459 } \ |
3483 | 1460 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1461 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1462 int chars_read = 0; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1463 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
|
1464 && char_class.find (c) == std::string::npos) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1465 buf << static_cast<char> (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1466 } \ |
3483 | 1467 \ |
7426 | 1468 if (width == INT_MAX && c != EOF) \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1469 is.putback (c); \ |
3483 | 1470 \ |
7426 | 1471 tmp = buf.str (); \ |
3483 | 1472 \ |
7426 | 1473 if (tmp.empty ()) \ |
1474 is.setstate (std::ios::failbit); \ | |
10969
da355a1a6d44
fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
1475 else if (c == EOF) \ |
da355a1a6d44
fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
1476 is.clear (); \ |
da355a1a6d44
fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
1477 \ |
3483 | 1478 } \ |
1479 while (0) | |
1480 | |
3410 | 1481 #define FINISH_CHARACTER_CONVERSION() \ |
1482 do \ | |
1483 { \ | |
4051 | 1484 width = tmp.length (); \ |
3410 | 1485 \ |
1486 if (is) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1487 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1488 int i = 0; \ |
3410 | 1489 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1490 if (! discard) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1491 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1492 conversion_count++; \ |
3410 | 1493 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1494 while (i < width) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1495 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1496 if (data_index == max_size) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1497 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1498 max_size *= 2; \ |
3410 | 1499 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1500 if (all_char_conv) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1501 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1502 if (one_elt_size_spec) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1503 mval.resize (1, max_size, 0.0); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1504 else if (nr > 0) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1505 mval.resize (nr, max_size / nr, 0.0); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1506 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1507 panic_impossible (); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1508 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1509 else if (nr > 0) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1510 mval.resize (nr, max_size / nr, 0.0); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1511 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1512 mval.resize (max_size, 1, 0.0); \ |
3410 | 1513 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1514 data = mval.fortran_vec (); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1515 } \ |
3410 | 1516 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1517 data[data_index++] = tmp[i++]; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1518 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1519 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1520 } \ |
3410 | 1521 } \ |
1522 while (0) | |
2117 | 1523 |
1524 octave_value | |
1525 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
|
1526 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
|
1527 octave_idx_type& conversion_count, const std::string& who) |
2117 | 1528 { |
8773
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1529 octave_value retval = Matrix (); |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1530 |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1531 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
|
1532 { |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1533 ::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
|
1534 who.c_str ()); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1535 |
8773
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1536 return retval; |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1537 } |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1538 |
3268 | 1539 conversion_count = 0; |
1540 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1541 octave_idx_type nconv = fmt_list.num_conversions (); |
3640 | 1542 |
5275 | 1543 octave_idx_type data_index = 0; |
2121 | 1544 |
3268 | 1545 if (nr == 0 || nc == 0) |
1546 { | |
1547 if (one_elt_size_spec) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1548 nc = 0; |
3268 | 1549 |
1550 return Matrix (nr, nc, 0.0); | |
1551 } | |
1552 | |
3523 | 1553 std::istream *isp = input_stream (); |
2117 | 1554 |
1555 bool all_char_conv = fmt_list.all_character_conversions (); | |
1556 | |
1557 Matrix mval; | |
1558 double *data = 0; | |
5275 | 1559 octave_idx_type max_size = 0; |
1560 octave_idx_type max_conv = 0; | |
1561 | |
1562 octave_idx_type final_nr = 0; | |
1563 octave_idx_type final_nc = 0; | |
2117 | 1564 |
3268 | 1565 if (all_char_conv) |
1566 { | |
4420 | 1567 // Any of these could be resized later (if we have %s |
1568 // conversions, we may read more than one element for each | |
1569 // conversion). | |
1570 | |
3268 | 1571 if (one_elt_size_spec) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1572 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1573 max_size = 512; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1574 mval.resize (1, max_size, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1575 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1576 if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1577 max_conv = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1578 } |
4420 | 1579 else if (nr > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1580 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1581 if (nc > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1582 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1583 mval.resize (nr, nc, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1584 max_size = max_conv = nr * nc; |
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 else |
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 mval.resize (nr, 32, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1589 max_size = nr * 32; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1590 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1591 } |
4420 | 1592 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1593 panic_impossible (); |
3268 | 1594 } |
1595 else if (nr > 0) | |
2117 | 1596 { |
1597 if (nc > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1598 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1599 // Will not resize later. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1600 mval.resize (nr, nc, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1601 max_size = nr * nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1602 max_conv = max_size; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1603 } |
2117 | 1604 else |
10315
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 // Maybe resize later. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1607 mval.resize (nr, 32, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1608 max_size = nr * 32; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1609 } |
2117 | 1610 } |
1611 else | |
1612 { | |
4420 | 1613 // Maybe resize later. |
2117 | 1614 mval.resize (32, 1, 0.0); |
1615 max_size = 32; | |
1616 } | |
1617 | |
4420 | 1618 data = mval.fortran_vec (); |
1619 | |
2117 | 1620 if (isp) |
1621 { | |
3523 | 1622 std::istream& is = *isp; |
2117 | 1623 |
1624 const scanf_format_elt *elt = fmt_list.first (); | |
1625 | |
3538 | 1626 std::ios::fmtflags flags = is.flags (); |
2213 | 1627 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1628 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
|
1629 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1630 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
|
1631 |
2117 | 1632 for (;;) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1633 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1634 octave_quit (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1635 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1636 if (elt) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1637 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1638 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
|
1639 || 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
|
1640 || elt->type == '%') |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1641 && max_conv > 0 && conversion_count == max_conv) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1642 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1643 if (all_char_conv && one_elt_size_spec) |
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 final_nr = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1646 final_nc = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1647 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1648 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1649 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1650 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1651 final_nc = (data_index - 1) / nr + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1652 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1653 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1654 break; |
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 else if (data_index == max_size) |
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 max_size *= 2; |
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 if (all_char_conv) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1661 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1662 if (one_elt_size_spec) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1663 mval.resize (1, max_size, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1664 else if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1665 mval.resize (nr, max_size / nr, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1666 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1667 panic_impossible (); |
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 else if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1670 mval.resize (nr, max_size / nr, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1671 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1672 mval.resize (max_size, 1, 0.0); |
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 data = mval.fortran_vec (); |
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 const char *fmt = elt->text; |
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 bool discard = elt->discard; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1680 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1681 switch (elt->type) |
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 scanf_format_elt::whitespace_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1684 DO_WHITESPACE_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1685 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1686 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1687 case scanf_format_elt::literal_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1688 DO_LITERAL_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1689 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1690 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1691 case '%': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1692 DO_PCT_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1693 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1694 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1695 case 'd': case 'i': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1696 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1697 switch (elt->modifier) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1698 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1699 case 'h': |
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 short int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1702 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1703 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1704 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1705 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1706 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1707 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1708 case 'l': |
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 long int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1711 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1712 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1713 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1714 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1715 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1716 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1717 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1718 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1719 int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1720 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1721 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1722 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1723 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1724 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1725 } |
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 break; |
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 case 'o': case 'u': case 'x': |
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 switch (elt->modifier) |
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 'h': |
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 unsigned short int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1736 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1737 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1738 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1739 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1740 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1741 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1742 case 'l': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1743 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1744 unsigned long int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1745 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1746 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1747 nr, max_size, discard); |
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 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1750 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1751 default: |
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 unsigned int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1754 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1755 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1756 nr, max_size, discard); |
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 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1759 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1760 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1761 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1762 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1763 case 'e': case 'f': case 'g': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1764 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1765 double tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1766 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1767 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1768 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1769 nr, max_size, discard); |
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 break; |
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 case 'c': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1774 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1775 BEGIN_C_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1776 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1777 FINISH_CHARACTER_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1778 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1779 is.setf (flags); |
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 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1782 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1783 case 's': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1784 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1785 BEGIN_S_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1786 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1787 FINISH_CHARACTER_CONVERSION (); |
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 break; |
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 case '[': case '^': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1792 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1793 BEGIN_CHAR_CLASS_CONVERSION (); |
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 FINISH_CHARACTER_CONVERSION (); |
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 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1798 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1799 case 'p': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1800 error ("%s: unsupported format specifier", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1801 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1802 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1803 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1804 error ("%s: internal format error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1805 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1806 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1807 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1808 if (! ok ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1809 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1810 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1811 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1812 else if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1813 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1814 if (all_char_conv) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1815 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1816 if (one_elt_size_spec) |
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 final_nr = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1819 final_nc = data_index; |
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 else if (data_index > nr) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1822 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1823 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1824 final_nc = (data_index - 1) / nr + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1825 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1826 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1827 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1828 final_nr = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1829 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1830 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1831 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1832 else if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1833 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1834 if (data_index > nr) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1835 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1836 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1837 final_nc = (data_index - 1) / nr + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1838 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1839 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1840 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1841 final_nr = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1842 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1843 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1844 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1845 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1846 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1847 final_nr = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1848 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1849 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1850 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1851 // 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
|
1852 // reset the failbit in the stream state. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1853 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1854 if (is.rdstate () & std::ios::failbit) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1855 is.clear (is.rdstate () & (~std::ios::failbit)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1856 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1857 // FIXME -- is this the right thing to do? |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1858 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1859 if (interactive && name () == "stdin") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1860 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1861 is.clear (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1862 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1863 // Skip to end of line. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1864 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1865 bool err; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1866 do_gets (-1, err, false, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1867 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1868 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1869 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1870 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1871 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1872 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1873 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1874 error ("%s: internal format error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1875 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1876 } |
2117 | 1877 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1878 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
|
1879 { |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1880 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
|
1881 { |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1882 final_nr = 1; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1883 final_nc = data_index; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1884 } |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1885 else |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1886 { |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1887 final_nr = nr; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1888 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
|
1889 } |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1890 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1891 break; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1892 } |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1893 else |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1894 elt = fmt_list.next (nconv > 0); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1895 } |
2117 | 1896 } |
1897 | |
1898 if (ok ()) | |
1899 { | |
2121 | 1900 mval.resize (final_nr, final_nc, 0.0); |
2117 | 1901 |
3268 | 1902 retval = mval; |
1903 | |
2117 | 1904 if (all_char_conv) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1905 retval = retval.convert_to_str (false, true); |
2117 | 1906 } |
1907 | |
1908 return retval; | |
1909 } | |
1910 | |
1911 octave_value | |
3810 | 1912 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
|
1913 octave_idx_type& conversion_count, const std::string& who) |
2117 | 1914 { |
1915 octave_value retval = Matrix (); | |
1916 | |
3559 | 1917 conversion_count = 0; |
2117 | 1918 |
3523 | 1919 std::istream *isp = input_stream (); |
2117 | 1920 |
1921 if (isp) | |
1922 { | |
1923 scanf_format_list fmt_list (fmt); | |
1924 | |
3640 | 1925 if (fmt_list.num_conversions () == -1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1926 ::error ("%s: invalid format specified", who.c_str ()); |
3640 | 1927 else |
10315
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 octave_idx_type nr = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1930 octave_idx_type nc = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1931 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1932 bool one_elt_size_spec; |
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 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
|
1935 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1936 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1937 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
|
1938 conversion_count, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1939 } |
2215 | 1940 } |
1941 else | |
4468 | 1942 invalid_operation (who, "reading"); |
2572 | 1943 |
1944 return retval; | |
1945 } | |
1946 | |
2712 | 1947 bool |
1948 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
|
1949 octave_value& retval, const std::string& who) |
2572 | 1950 { |
2712 | 1951 bool quit = false; |
2215 | 1952 |
3523 | 1953 std::istream *isp = input_stream (); |
2215 | 1954 |
1955 if (isp) | |
1956 { | |
3523 | 1957 std::istream& is = *isp; |
2215 | 1958 |
3538 | 1959 std::ios::fmtflags flags = is.flags (); |
2215 | 1960 |
1961 if (elt) | |
10315
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 const char *fmt = elt->text; |
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 bool discard = elt->discard; |
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 switch (elt->type) |
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 case scanf_format_elt::whitespace_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1970 DO_WHITESPACE_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1971 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1972 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1973 case scanf_format_elt::literal_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1974 DO_LITERAL_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1975 break; |
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 case '%': |
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 DO_PCT_CONVERSION (); |
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 if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1982 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1983 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1984 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1985 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1986 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1987 case 'd': case 'i': |
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 int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1990 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1991 if (OCTAVE_SCAN (is, *elt, &tmp)) |
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 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1994 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1995 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1996 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1997 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1998 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1999 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2000 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2001 case 'o': case 'u': case 'x': |
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 long int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2004 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2005 if (OCTAVE_SCAN (is, *elt, &tmp)) |
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 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2008 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2009 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2010 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2011 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2012 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2013 break; |
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 case 'e': case 'f': case 'g': |
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 double tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2018 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2019 if (OCTAVE_SCAN (is, *elt, &tmp)) |
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 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2022 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2023 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2024 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2025 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2026 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2027 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2028 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2029 case 'c': |
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 BEGIN_C_CONVERSION (); |
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 (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2034 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2035 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2036 if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2037 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2038 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2039 is.setf (flags); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2040 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2041 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2042 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2043 case 's': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2044 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2045 BEGIN_S_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2046 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2047 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2048 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2049 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2050 if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2051 quit = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2052 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2053 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2054 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2055 case '[': case '^': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2056 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2057 BEGIN_CHAR_CLASS_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2058 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2059 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2060 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2061 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2062 if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2063 quit = true; |
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 break; |
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 case 'p': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2068 error ("%s: unsupported format specifier", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2069 break; |
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 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2072 error ("%s: internal format error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2073 break; |
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 } |
2215 | 2076 |
2077 if (ok () && is.fail ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2078 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2079 error ("%s: read error", who.c_str ()); |
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 // FIXME -- is this the right thing to do? |
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 (interactive && name () == "stdin") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2084 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2085 // Skip to end of line. |
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 bool err; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2088 do_gets (-1, err, false, who); |
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 } |
2215 | 2091 } |
2092 | |
2712 | 2093 return quit; |
2215 | 2094 } |
2095 | |
2096 octave_value_list | |
4468 | 2097 octave_base_stream::oscanf (const std::string& fmt, const std::string& who) |
2215 | 2098 { |
2099 octave_value_list retval; | |
2100 | |
3523 | 2101 std::istream *isp = input_stream (); |
2215 | 2102 |
2103 if (isp) | |
2104 { | |
3523 | 2105 std::istream& is = *isp; |
2215 | 2106 |
2107 scanf_format_list fmt_list (fmt); | |
2108 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
2109 octave_idx_type nconv = fmt_list.num_conversions (); |
2215 | 2110 |
3640 | 2111 if (nconv == -1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2112 ::error ("%s: invalid format specified", who.c_str ()); |
3640 | 2113 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2114 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2115 is.clear (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2116 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2117 octave_idx_type len = fmt_list.length (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2118 |
13271
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
2119 retval.resize (nconv+2, Matrix ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2120 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2121 const scanf_format_elt *elt = fmt_list.first (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2122 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2123 int num_values = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2124 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2125 bool quit = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2126 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2127 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
|
2128 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2129 octave_value tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2130 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2131 quit = do_oscanf (elt, tmp, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2132 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2133 if (quit) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2134 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2135 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2136 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2137 if (tmp.is_defined ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2138 retval (num_values++) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2139 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2140 if (! ok ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2141 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2142 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2143 elt = fmt_list.next (nconv > 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2144 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2145 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2146 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2147 retval(nconv) = num_values; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2148 |
13271
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
2149 int err_num; |
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
2150 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
|
2151 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2152 if (! quit) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2153 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2154 // Pick up any trailing stuff. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2155 if (ok () && len > nconv) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2156 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2157 octave_value tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2158 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2159 elt = fmt_list.next (); |
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 do_oscanf (elt, tmp, who); |
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 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2164 } |
2117 | 2165 } |
2166 else | |
4468 | 2167 invalid_operation (who, "reading"); |
2117 | 2168 |
2169 return retval; | |
2170 } | |
2171 | |
2172 // Functions that are defined for all output streams (output streams | |
2173 // are those that define os). | |
2174 | |
2175 int | |
2176 octave_base_stream::flush (void) | |
2177 { | |
2178 int retval = -1; | |
2179 | |
3523 | 2180 std::ostream *os = output_stream (); |
2117 | 2181 |
2182 if (os) | |
2183 { | |
2184 os->flush (); | |
2185 | |
2186 if (os->good ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2187 retval = 0; |
2117 | 2188 } |
2189 else | |
2190 invalid_operation ("fflush", "writing"); | |
2191 | |
2192 return retval; | |
2193 } | |
2194 | |
2195 class | |
2196 printf_value_cache | |
2197 { | |
2198 public: | |
2199 | |
3653 | 2200 enum state { ok, conversion_error }; |
2117 | 2201 |
7352 | 2202 printf_value_cache (const octave_value_list& args, const std::string& who) |
2117 | 2203 : values (args), val_idx (0), elt_idx (0), |
2204 n_vals (values.length ()), n_elts (0), data (0), | |
7352 | 2205 curr_state (ok) |
2206 { | |
2207 for (octave_idx_type i = 0; i < values.length (); i++) | |
2208 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2209 octave_value val = values(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2210 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2211 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
|
2212 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2213 gripe_wrong_type_arg (who, val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2214 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2215 } |
7352 | 2216 } |
2217 } | |
2117 | 2218 |
2219 ~printf_value_cache (void) { } | |
2220 | |
2221 // Get the current value as a double and advance the internal pointer. | |
2222 double double_value (void); | |
2223 | |
2224 // Get the current value as an int and advance the internal pointer. | |
2225 int int_value (void); | |
2226 | |
2227 // Get the current value as a string and advance the internal pointer. | |
3523 | 2228 std::string string_value (void); |
2117 | 2229 |
3145 | 2230 operator bool () const { return (curr_state == ok); } |
2117 | 2231 |
3653 | 2232 bool exhausted (void) { return (val_idx >= n_vals); } |
2117 | 2233 |
2234 private: | |
2235 | |
2236 const octave_value_list values; | |
2237 int val_idx; | |
2238 int elt_idx; | |
2239 int n_vals; | |
2240 int n_elts; | |
2241 const double *data; | |
4874 | 2242 NDArray curr_val; |
2117 | 2243 state curr_state; |
2244 | |
2245 // Must create value cache with values! | |
2246 | |
2247 printf_value_cache (void); | |
2248 | |
2249 // No copying! | |
2250 | |
2251 printf_value_cache (const printf_value_cache&); | |
2252 | |
2253 printf_value_cache& operator = (const printf_value_cache&); | |
2254 }; | |
2255 | |
2256 double | |
2257 printf_value_cache::double_value (void) | |
2258 { | |
2259 double retval = 0.0; | |
2260 | |
3711 | 2261 if (exhausted ()) |
2262 curr_state = conversion_error; | |
2263 | |
2264 while (! exhausted ()) | |
2117 | 2265 { |
2266 if (! data) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2267 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2268 octave_value tmp_val = values (val_idx); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2269 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2270 // Force string conversion here for compatibility. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2271 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2272 curr_val = tmp_val.array_value (true); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2273 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2274 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2275 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2276 elt_idx = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2277 n_elts = curr_val.length (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2278 data = curr_val.data (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2279 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2280 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2281 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2282 curr_state = conversion_error; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2283 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2284 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2285 } |
2117 | 2286 |
2287 if (elt_idx < n_elts) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2288 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2289 retval = data[elt_idx++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2290 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2291 if (elt_idx >= n_elts) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2292 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2293 elt_idx = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2294 val_idx++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2295 data = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2296 } |
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 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2299 } |
2117 | 2300 else |
10315
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 val_idx++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2303 data = 0; |
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 if (n_elts == 0 && exhausted ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2306 curr_state = conversion_error; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2307 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2308 continue; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2309 } |
2117 | 2310 } |
2311 | |
2312 return retval; | |
2313 } | |
2314 | |
2315 int | |
2316 printf_value_cache::int_value (void) | |
2317 { | |
2318 int retval = 0; | |
2319 | |
2320 double dval = double_value (); | |
2321 | |
2322 if (! error_state) | |
2323 { | |
2324 if (D_NINT (dval) == dval) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2325 retval = NINT (dval); |
2117 | 2326 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2327 curr_state = conversion_error; |
2117 | 2328 } |
2329 | |
2330 return retval; | |
2331 } | |
2332 | |
3536 | 2333 std::string |
2117 | 2334 printf_value_cache::string_value (void) |
2335 { | |
3523 | 2336 std::string retval; |
2117 | 2337 |
4425 | 2338 if (exhausted ()) |
2339 curr_state = conversion_error; | |
4257 | 2340 else |
2117 | 2341 { |
4425 | 2342 octave_value tval = values (val_idx++); |
2343 | |
2344 if (tval.rows () == 1) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2345 retval = tval.string_value (); |
4425 | 2346 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2347 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2348 // In the name of Matlab compatibility. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2349 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2350 charMatrix chm = tval.char_matrix_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2351 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2352 octave_idx_type nr = chm.rows (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2353 octave_idx_type nc = chm.columns (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2354 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2355 int k = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2356 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2357 retval.resize (nr * nc, '\0'); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2358 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2359 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
|
2360 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
|
2361 retval[k++] = chm(i,j); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2362 } |
4425 | 2363 |
2364 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2365 curr_state = conversion_error; |
2117 | 2366 } |
4257 | 2367 |
2117 | 2368 return retval; |
2369 } | |
2370 | |
2371 // Ugh again and again. | |
2372 | |
2572 | 2373 template <class T> |
3620 | 2374 int |
3523 | 2375 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
|
2376 int sa_2, T arg, const std::string& who) |
2572 | 2377 { |
3620 | 2378 int retval = 0; |
2379 | |
2572 | 2380 switch (nsa) |
2381 { | |
2382 case 2: | |
3640 | 2383 retval = octave_format (os, fmt, sa_1, sa_2, arg); |
2572 | 2384 break; |
2385 | |
2386 case 1: | |
3640 | 2387 retval = octave_format (os, fmt, sa_1, arg); |
2572 | 2388 break; |
2389 | |
2390 case 0: | |
3640 | 2391 retval = octave_format (os, fmt, arg); |
2572 | 2392 break; |
2393 | |
2394 default: | |
4468 | 2395 ::error ("%s: internal error handling format", who.c_str ()); |
2572 | 2396 break; |
2397 } | |
3620 | 2398 |
2399 return retval; | |
2572 | 2400 } |
2401 | |
3620 | 2402 template int |
4468 | 2403 do_printf_conv (std::ostream&, const char*, int, int, int, int, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2404 const std::string&); |
4468 | 2405 |
2406 template int | |
2407 do_printf_conv (std::ostream&, const char*, int, int, int, long, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2408 const std::string&); |
4468 | 2409 |
3878 | 2410 template int |
4468 | 2411 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned int, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2412 const std::string&); |
4468 | 2413 |
2414 template int | |
2415 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned long, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2416 const std::string&); |
4468 | 2417 |
2418 template int | |
2419 do_printf_conv (std::ostream&, const char*, int, int, int, double, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2420 const std::string&); |
4468 | 2421 |
2422 template int | |
2423 do_printf_conv (std::ostream&, const char*, int, int, int, const char*, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2424 const std::string&); |
2117 | 2425 |
6492 | 2426 #define DO_DOUBLE_CONV(TQUAL) \ |
2427 do \ | |
2428 { \ | |
7199 | 2429 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
|
2430 || val < std::numeric_limits<TQUAL long>::min ()) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2431 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2432 std::string tfmt = fmt; \ |
6492 | 2433 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2434 tfmt.replace (tfmt.rfind (elt->type), 1, ".f"); \ |
6492 | 2435 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2436 if (elt->modifier == 'l') \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2437 tfmt.replace (tfmt.rfind (elt->modifier), 1, ""); \ |
7199 | 2438 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2439 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
|
2440 val, who); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2441 } \ |
6492 | 2442 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2443 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
|
2444 static_cast<TQUAL long> (val), who); \ |
6492 | 2445 } \ |
2446 while (0) | |
2447 | |
2117 | 2448 int |
2449 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
|
2450 const octave_value_list& args, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2451 const std::string& who) |
2117 | 2452 { |
3620 | 2453 int retval = 0; |
2117 | 2454 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
2455 octave_idx_type nconv = fmt_list.num_conversions (); |
3640 | 2456 |
3523 | 2457 std::ostream *osp = output_stream (); |
2117 | 2458 |
2459 if (osp) | |
2460 { | |
3523 | 2461 std::ostream& os = *osp; |
2117 | 2462 |
2463 const printf_format_elt *elt = fmt_list.first (); | |
2464 | |
7352 | 2465 printf_value_cache val_cache (args, who); |
2466 | |
2467 if (error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2468 return retval; |
2117 | 2469 |
2470 for (;;) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2471 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2472 octave_quit (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2473 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2474 if (elt) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2475 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2476 // NSA is the number of `star' args to convert. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2477 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2478 int nsa = (elt->fw < 0) + (elt->prec < 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2479 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2480 int sa_1 = 0; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2481 int sa_2 = 0; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2482 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2483 if (nsa > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2484 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2485 sa_1 = val_cache.int_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2486 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2487 if (! val_cache) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2488 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2489 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2490 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2491 if (nsa > 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2492 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2493 sa_2 = val_cache.int_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2494 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2495 if (! val_cache) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2496 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2497 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2498 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2499 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2500 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2501 const char *fmt = elt->text; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2502 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2503 if (elt->type == '%') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2504 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2505 os << "%"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2506 retval++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2507 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2508 else if (elt->args == 0 && elt->text) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2509 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2510 os << elt->text; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2511 retval += strlen (elt->text); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2512 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2513 else if (elt->type == 's') |
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 std::string val = val_cache.string_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2516 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2517 if (val_cache) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2518 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
|
2519 sa_2, val.c_str (), who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2520 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2521 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2522 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2523 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2524 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2525 double val = val_cache.double_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2526 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2527 if (val_cache) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2528 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2529 if (lo_ieee_isnan (val) || xisinf (val)) |
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 std::string tfmt = fmt; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2532 std::string::size_type i1, i2; |
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 tfmt.replace ((i1 = tfmt.rfind (elt->type)), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2535 1, 1, 's'); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2536 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2537 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
|
2538 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2539 tfmt.erase (i2, i1-i2); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2540 if (elt->prec < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2541 nsa--; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2542 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2543 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2544 const char *tval = xisinf (val) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2545 ? (val < 0 ? "-Inf" : "Inf") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2546 : (lo_ieee_is_NA (val) ? "NA" : "NaN"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2547 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2548 retval += do_printf_conv (os, tfmt.c_str (), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2549 nsa, sa_1, sa_2, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2550 tval, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2551 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2552 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2553 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2554 char type = elt->type; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2555 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2556 switch (type) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2557 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2558 case 'd': case 'i': case 'c': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2559 DO_DOUBLE_CONV (OCTAVE_EMPTY_CPP_ARG); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2560 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2561 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2562 case 'o': case 'x': case 'X': case 'u': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2563 DO_DOUBLE_CONV (unsigned); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2564 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2565 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2566 case 'f': case 'e': case 'E': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2567 case 'g': case 'G': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2568 retval |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2569 += 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
|
2570 val, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2571 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2572 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2573 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2574 error ("%s: invalid format specifier", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2575 who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2576 return -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2577 break; |
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 } |
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 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2582 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2583 } |
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 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2587 error ("%s: write error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2588 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2589 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2590 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2591 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2592 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2593 ::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
|
2594 retval = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2595 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2596 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2597 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2598 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
|
2599 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2600 if (! elt || (val_cache.exhausted () && elt->args > 0)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2601 break; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2602 } |
2117 | 2603 } |
3640 | 2604 else |
4468 | 2605 invalid_operation (who, "writing"); |
2117 | 2606 |
2607 return retval; | |
2608 } | |
2609 | |
2610 int | |
3640 | 2611 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
|
2612 const octave_value_list& args, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2613 const std::string& who) |
2117 | 2614 { |
3640 | 2615 int retval = 0; |
2616 | |
2617 printf_format_list fmt_list (fmt); | |
2618 | |
2619 if (fmt_list.num_conversions () == -1) | |
4468 | 2620 ::error ("%s: invalid format specified", who.c_str ()); |
2117 | 2621 else |
4468 | 2622 retval = do_printf (fmt_list, args, who); |
2117 | 2623 |
2624 return retval; | |
2625 } | |
2626 | |
2627 int | |
4468 | 2628 octave_base_stream::puts (const std::string& s, const std::string& who) |
2117 | 2629 { |
2630 int retval = -1; | |
2631 | |
3523 | 2632 std::ostream *osp = output_stream (); |
2117 | 2633 |
2634 if (osp) | |
2635 { | |
3523 | 2636 std::ostream& os = *osp; |
2117 | 2637 |
2638 os << s; | |
2639 | |
2640 if (os) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2641 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2642 // FIXME -- why does this seem to be necessary? |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2643 // Without it, output from a loop like |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2644 // |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2645 // 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
|
2646 // |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2647 // 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
|
2648 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2649 os.flush (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2650 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2651 if (os) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2652 retval = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2653 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2654 error ("%s: write error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2655 } |
2117 | 2656 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2657 error ("%s: write error", who.c_str ()); |
2117 | 2658 } |
2659 else | |
4468 | 2660 invalid_operation (who, "writing"); |
2117 | 2661 |
2662 return retval; | |
2663 } | |
2664 | |
2665 // Return current error message for this stream. | |
2666 | |
3536 | 2667 std::string |
2435 | 2668 octave_base_stream::error (bool clear_err, int& err_num) |
2117 | 2669 { |
2435 | 2670 err_num = fail ? -1 : 0; |
2117 | 2671 |
3523 | 2672 std::string tmp = errmsg; |
2117 | 2673 |
2674 if (clear_err) | |
2675 clear (); | |
2676 | |
2677 return tmp; | |
2678 } | |
2679 | |
2680 void | |
4468 | 2681 octave_base_stream::invalid_operation (const std::string& who, const char *rw) |
2117 | 2682 { |
4468 | 2683 // Note that this is not ::error () ! |
2684 | |
6297 | 2685 error (who, std::string ("stream not open for ") + rw); |
2117 | 2686 } |
2687 | |
3552 | 2688 octave_stream::octave_stream (octave_base_stream *bs) |
3340 | 2689 : rep (bs) |
2690 { | |
2691 if (rep) | |
2692 rep->count = 1; | |
2693 } | |
2694 | |
2695 octave_stream::~octave_stream (void) | |
2696 { | |
2697 if (rep && --rep->count == 0) | |
2698 delete rep; | |
2699 } | |
2700 | |
2701 octave_stream::octave_stream (const octave_stream& s) | |
2702 : rep (s.rep) | |
2703 { | |
2704 if (rep) | |
2705 rep->count++; | |
2706 } | |
2707 | |
2708 octave_stream& | |
2709 octave_stream::operator = (const octave_stream& s) | |
2710 { | |
2711 if (rep != s.rep) | |
2712 { | |
2713 if (rep && --rep->count == 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2714 delete rep; |
3340 | 2715 |
2716 rep = s.rep; | |
2717 | |
2718 if (rep) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2719 rep->count++; |
3340 | 2720 } |
2721 | |
2722 return *this; | |
2723 } | |
2724 | |
2117 | 2725 int |
2726 octave_stream::flush (void) | |
2727 { | |
2728 int retval = -1; | |
2729 | |
5659 | 2730 if (stream_ok ()) |
2117 | 2731 retval = rep->flush (); |
2732 | |
2733 return retval; | |
2734 } | |
2735 | |
3536 | 2736 std::string |
5275 | 2737 octave_stream::getl (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 2738 { |
3523 | 2739 std::string retval; |
2117 | 2740 |
5659 | 2741 if (stream_ok ()) |
4468 | 2742 retval = rep->getl (max_len, err, who); |
2117 | 2743 |
2744 return retval; | |
2745 } | |
2746 | |
3536 | 2747 std::string |
4468 | 2748 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
|
2749 const std::string& who) |
2117 | 2750 { |
3523 | 2751 std::string retval; |
2117 | 2752 |
2753 err = false; | |
2754 | |
2755 int conv_err = 0; | |
2756 | |
6345 | 2757 int max_len = -1; |
2758 | |
2759 if (tc_max_len.is_defined ()) | |
2117 | 2760 { |
6345 | 2761 max_len = convert_to_valid_int (tc_max_len, conv_err); |
2762 | |
2763 if (conv_err || max_len < 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2764 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2765 err = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2766 ::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
|
2767 } |
2117 | 2768 } |
6345 | 2769 |
2770 if (! error_state) | |
4468 | 2771 retval = getl (max_len, err, who); |
2117 | 2772 |
2773 return retval; | |
2774 } | |
2775 | |
3536 | 2776 std::string |
5275 | 2777 octave_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 2778 { |
3523 | 2779 std::string retval; |
2117 | 2780 |
5659 | 2781 if (stream_ok ()) |
4468 | 2782 retval = rep->gets (max_len, err, who); |
2117 | 2783 |
2784 return retval; | |
2785 } | |
2786 | |
3536 | 2787 std::string |
4468 | 2788 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
|
2789 const std::string& who) |
2117 | 2790 { |
3523 | 2791 std::string retval; |
2117 | 2792 |
2793 err = false; | |
2794 | |
2795 int conv_err = 0; | |
2796 | |
6345 | 2797 int max_len = -1; |
2798 | |
2799 if (tc_max_len.is_defined ()) | |
2117 | 2800 { |
6345 | 2801 max_len = convert_to_valid_int (tc_max_len, conv_err); |
2802 | |
2803 if (conv_err || max_len < 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2804 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2805 err = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2806 ::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
|
2807 } |
2117 | 2808 } |
6345 | 2809 |
2810 if (! error_state) | |
4468 | 2811 retval = gets (max_len, err, who); |
2117 | 2812 |
2813 return retval; | |
2814 } | |
2815 | |
9701 | 2816 long |
2817 octave_stream::skipl (long count, bool& err, const std::string& who) | |
2818 { | |
2819 long retval = -1; | |
2820 | |
2821 if (stream_ok ()) | |
2822 retval = rep->skipl (count, err, who); | |
2823 | |
2824 return retval; | |
2825 } | |
2826 | |
2827 long | |
2828 octave_stream::skipl (const octave_value& tc_count, bool& err, const std::string& who) | |
2829 { | |
2830 long retval = -1; | |
2831 | |
2832 err = false; | |
2833 | |
2834 int conv_err = 0; | |
2835 | |
2836 int count = 1; | |
2837 | |
2838 if (tc_count.is_defined ()) | |
2839 { | |
2840 if (tc_count.is_scalar_type () && xisinf (tc_count.scalar_value ())) | |
2841 count = -1; | |
2842 else | |
2843 { | |
2844 count = convert_to_valid_int (tc_count, conv_err); | |
2845 | |
2846 if (conv_err || count < 0) | |
2847 { | |
2848 err = true; | |
2849 ::error ("%s: invalid number of lines specified", who.c_str ()); | |
2850 } | |
2851 } | |
2852 } | |
2853 | |
2854 if (! error_state) | |
2855 retval = skipl (count, err, who); | |
2856 | |
2857 return retval; | |
2858 } | |
2859 | |
2117 | 2860 int |
4797 | 2861 octave_stream::seek (long offset, int origin) |
2117 | 2862 { |
5065 | 2863 int status = -1; |
2117 | 2864 |
5659 | 2865 if (stream_ok ()) |
4889 | 2866 { |
2867 clearerr (); | |
2868 | |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2869 // 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
|
2870 |
5065 | 2871 long orig_pos = rep->tell (); |
2872 | |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2873 // 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
|
2874 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2875 status = rep->seek (0, SEEK_END); |
5065 | 2876 |
2877 if (status == 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2878 { |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2879 long eof_pos = rep->tell (); |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2880 |
12943
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2881 if (origin == SEEK_CUR) |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2882 { |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2883 // 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
|
2884 // 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
|
2885 // original location. |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2886 |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2887 rep->seek (orig_pos, SEEK_SET); |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2888 } |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2889 |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2890 // 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
|
2891 // of existing file. |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2892 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2893 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
|
2894 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2895 if (status == 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2896 { |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2897 // 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
|
2898 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2899 long desired_pos = rep->tell (); |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2900 |
12943
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2901 // 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
|
2902 // check anyway... |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2903 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2904 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
|
2905 { |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2906 // 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
|
2907 // position unchanged. |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2908 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2909 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
|
2910 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2911 status = -1; |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2912 } |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2913 } |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2914 else |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2915 { |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2916 // 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
|
2917 // original position and return failure status. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2918 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2919 rep->seek (orig_pos, SEEK_SET); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2920 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2921 status = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2922 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2923 } |
4889 | 2924 } |
2117 | 2925 |
5065 | 2926 return status; |
2117 | 2927 } |
2928 | |
2929 int | |
2930 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
|
2931 const octave_value& tc_origin) |
2117 | 2932 { |
2933 int retval = -1; | |
2934 | |
4797 | 2935 long xoffset = tc_offset.long_value (true); |
4645 | 2936 |
2937 if (! error_state) | |
2117 | 2938 { |
4645 | 2939 int conv_err = 0; |
2940 | |
4797 | 2941 int origin = SEEK_SET; |
2117 | 2942 |
2341 | 2943 if (tc_origin.is_string ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2944 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2945 std::string xorigin = tc_origin.string_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2946 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2947 if (xorigin == "bof") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2948 origin = SEEK_SET; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2949 else if (xorigin == "cof") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2950 origin = SEEK_CUR; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2951 else if (xorigin == "eof") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2952 origin = SEEK_END; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2953 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2954 conv_err = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2955 } |
2341 | 2956 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2957 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2958 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
|
2959 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2960 if (! conv_err) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2961 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2962 if (xorigin == -1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2963 origin = SEEK_SET; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2964 else if (xorigin == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2965 origin = SEEK_CUR; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2966 else if (xorigin == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2967 origin = SEEK_END; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2968 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2969 conv_err = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2970 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2971 } |
2117 | 2972 |
2973 if (! conv_err) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2974 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2975 retval = seek (xoffset, origin); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2976 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2977 if (retval != 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2978 error ("fseek: failed to seek to requested position"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2979 } |
2117 | 2980 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2981 error ("fseek: invalid value for origin"); |
2117 | 2982 } |
2983 else | |
2984 error ("fseek: invalid value for offset"); | |
2985 | |
2986 return retval; | |
2987 } | |
2988 | |
4797 | 2989 long |
2990 octave_stream::tell (void) | |
2117 | 2991 { |
4797 | 2992 long retval = -1; |
2117 | 2993 |
5659 | 2994 if (stream_ok ()) |
2117 | 2995 retval = rep->tell (); |
2996 | |
2997 return retval; | |
2998 } | |
2999 | |
3000 int | |
3001 octave_stream::rewind (void) | |
3002 { | |
6296 | 3003 return seek (0, SEEK_SET); |
2117 | 3004 } |
3005 | |
3340 | 3006 bool |
3007 octave_stream::is_open (void) const | |
3008 { | |
3009 bool retval = false; | |
3010 | |
5659 | 3011 if (stream_ok ()) |
3340 | 3012 retval = rep->is_open (); |
3013 | |
3014 return retval; | |
3015 } | |
3016 | |
3017 void | |
3018 octave_stream::close (void) | |
3019 { | |
5659 | 3020 if (stream_ok ()) |
3340 | 3021 rep->close (); |
3022 } | |
3023 | |
4944 | 3024 template <class RET_T, class READ_T> |
2117 | 3025 octave_value |
5275 | 3026 do_read (octave_stream& strm, octave_idx_type nr, octave_idx_type nc, octave_idx_type block_size, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3027 octave_idx_type skip, bool do_float_fmt_conv, bool do_NA_conv, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3028 oct_mach_info::float_format from_flt_fmt, octave_idx_type& count) |
2117 | 3029 { |
3030 octave_value retval; | |
3031 | |
4944 | 3032 RET_T nda; |
3033 | |
3034 count = 0; | |
3035 | |
9941
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
9701
diff
changeset
|
3036 typedef typename RET_T::element_type ELMT; |
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
9701
diff
changeset
|
3037 ELMT elt_zero = ELMT (); |
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
9701
diff
changeset
|
3038 |
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
9701
diff
changeset
|
3039 ELMT *dat = 0; |
4944 | 3040 |
5275 | 3041 octave_idx_type max_size = 0; |
3042 | |
3043 octave_idx_type final_nr = 0; | |
3044 octave_idx_type final_nc = 1; | |
4944 | 3045 |
3046 if (nr > 0) | |
3047 { | |
3048 if (nc > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3049 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3050 nda.resize (dim_vector (nr, nc), elt_zero); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3051 dat = nda.fortran_vec (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3052 max_size = nr * nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3053 } |
4944 | 3054 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3055 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3056 nda.resize (dim_vector (nr, 32), elt_zero); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3057 dat = nda.fortran_vec (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3058 max_size = nr * 32; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3059 } |
4944 | 3060 } |
3061 else | |
3062 { | |
3063 nda.resize (dim_vector (32, 1), elt_zero); | |
3064 dat = nda.fortran_vec (); | |
3065 max_size = 32; | |
3066 } | |
3067 | |
5775 | 3068 // FIXME -- byte order for Cray? |
4944 | 3069 |
3070 bool swap = false; | |
3071 | |
3072 if (oct_mach_info::words_big_endian ()) | |
3073 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3074 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3075 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g); |
4944 | 3076 else |
3077 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); | |
3078 | |
3079 union | |
3080 { | |
9685 | 3081 char buf[sizeof (typename strip_template_param<octave_int, READ_T>::type)]; |
3082 typename strip_template_param<octave_int, READ_T>::type val; | |
4944 | 3083 } u; |
3084 | |
3085 std::istream *isp = strm.input_stream (); | |
3086 | |
3087 if (isp) | |
3088 { | |
3089 std::istream& is = *isp; | |
3090 | |
5275 | 3091 octave_idx_type elts_read = 0; |
4944 | 3092 |
3093 for (;;) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3094 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3095 // FIXME -- maybe there should be a special case for |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3096 // skip == 0. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3097 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3098 if (is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3099 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3100 if (nr > 0 && nc > 0 && count == max_size) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3101 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3102 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3103 final_nc = nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3104 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3105 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3106 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3107 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3108 is.read (u.buf, sizeof (typename strip_template_param<octave_int, READ_T>::type)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3109 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3110 // We only swap bytes for integer types. For float |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3111 // types, the format conversion will also handle byte |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3112 // swapping. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3113 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3114 if (swap) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3115 swap_bytes<sizeof (typename strip_template_param<octave_int, READ_T>::type)> (u.buf); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3116 else if (do_float_fmt_conv) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3117 do_float_format_conversion |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3118 (u.buf, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3119 sizeof (typename strip_template_param<octave_int, READ_T>::type), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3120 1, from_flt_fmt, oct_mach_info::float_format ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3121 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3122 typename RET_T::element_type tmp |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3123 = static_cast <typename RET_T::element_type> (u.val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3124 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3125 if (is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3126 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3127 if (count == max_size) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3128 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3129 max_size *= 2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3130 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3131 if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3132 nda.resize (dim_vector (nr, max_size / nr), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3133 elt_zero); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3134 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3135 nda.resize (dim_vector (max_size, 1), elt_zero); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3136 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3137 dat = nda.fortran_vec (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3138 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3139 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3140 if (do_NA_conv && __lo_ieee_is_old_NA (tmp)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3141 tmp = __lo_ieee_replace_old_NA (tmp); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3142 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3143 dat[count++] = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3144 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3145 elts_read++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3146 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3147 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3148 int seek_status = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3149 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3150 if (skip != 0 && elts_read == block_size) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3151 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3152 seek_status = strm.seek (skip, SEEK_CUR); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3153 elts_read = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3154 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3155 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3156 if (is.eof () || seek_status < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3157 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3158 if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3159 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3160 if (count > nr) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3161 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3162 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3163 final_nc = (count - 1) / nr + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3164 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3165 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3166 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3167 final_nr = count; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3168 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3169 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3170 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3171 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3172 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3173 final_nr = count; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3174 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3175 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3176 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3177 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3178 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3179 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3180 else if (is.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3181 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3182 } |
4944 | 3183 } |
3184 | |
5030 | 3185 nda.resize (dim_vector (final_nr, final_nc), elt_zero); |
3186 | |
3187 retval = nda; | |
4944 | 3188 |
3189 return retval; | |
3190 } | |
3191 | |
3192 #define DO_READ_VAL_TEMPLATE(RET_T, READ_T) \ | |
3193 template octave_value \ | |
7995 | 3194 do_read<RET_T, READ_T> (octave_stream&, octave_idx_type, octave_idx_type, octave_idx_type, octave_idx_type, bool, bool, \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3195 oct_mach_info::float_format, octave_idx_type&) |
4944 | 3196 |
5775 | 3197 // FIXME -- should we only have float if it is a different |
4944 | 3198 // size from double? |
3199 | |
3200 #define INSTANTIATE_DO_READ(VAL_T) \ | |
3201 DO_READ_VAL_TEMPLATE (VAL_T, octave_int8); \ | |
3202 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint8); \ | |
3203 DO_READ_VAL_TEMPLATE (VAL_T, octave_int16); \ | |
3204 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint16); \ | |
3205 DO_READ_VAL_TEMPLATE (VAL_T, octave_int32); \ | |
3206 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint32); \ | |
3207 DO_READ_VAL_TEMPLATE (VAL_T, octave_int64); \ | |
3208 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint64); \ | |
3209 DO_READ_VAL_TEMPLATE (VAL_T, float); \ | |
3210 DO_READ_VAL_TEMPLATE (VAL_T, double); \ | |
3211 DO_READ_VAL_TEMPLATE (VAL_T, char); \ | |
3212 DO_READ_VAL_TEMPLATE (VAL_T, signed char); \ | |
3213 DO_READ_VAL_TEMPLATE (VAL_T, unsigned char) | |
3214 | |
4970 | 3215 INSTANTIATE_DO_READ (int8NDArray); |
3216 INSTANTIATE_DO_READ (uint8NDArray); | |
3217 INSTANTIATE_DO_READ (int16NDArray); | |
3218 INSTANTIATE_DO_READ (uint16NDArray); | |
3219 INSTANTIATE_DO_READ (int32NDArray); | |
3220 INSTANTIATE_DO_READ (uint32NDArray); | |
3221 INSTANTIATE_DO_READ (int64NDArray); | |
3222 INSTANTIATE_DO_READ (uint64NDArray); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3223 INSTANTIATE_DO_READ (FloatNDArray); |
4970 | 3224 INSTANTIATE_DO_READ (NDArray); |
3225 INSTANTIATE_DO_READ (charNDArray); | |
5780 | 3226 INSTANTIATE_DO_READ (boolNDArray); |
4944 | 3227 |
7995 | 3228 typedef octave_value (*read_fptr) (octave_stream&, octave_idx_type, octave_idx_type, octave_idx_type, octave_idx_type, bool, bool, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3229 oct_mach_info::float_format ffmt, octave_idx_type&); |
4944 | 3230 |
3231 #define FILL_TABLE_ROW(R, VAL_T) \ | |
9202
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3232 read_fptr_table[R][oct_data_conv::dt_int8] = do_read<VAL_T, octave_int8>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3233 read_fptr_table[R][oct_data_conv::dt_uint8] = do_read<VAL_T, octave_uint8>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3234 read_fptr_table[R][oct_data_conv::dt_int16] = do_read<VAL_T, octave_int16>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3235 read_fptr_table[R][oct_data_conv::dt_uint16] = do_read<VAL_T, octave_uint16>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3236 read_fptr_table[R][oct_data_conv::dt_int32] = do_read<VAL_T, octave_int32>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3237 read_fptr_table[R][oct_data_conv::dt_uint32] = do_read<VAL_T, octave_uint32>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3238 read_fptr_table[R][oct_data_conv::dt_int64] = do_read<VAL_T, octave_int64>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3239 read_fptr_table[R][oct_data_conv::dt_uint64] = do_read<VAL_T, octave_uint64>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3240 read_fptr_table[R][oct_data_conv::dt_single] = do_read<VAL_T, float>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3241 read_fptr_table[R][oct_data_conv::dt_double] = do_read<VAL_T, double>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3242 read_fptr_table[R][oct_data_conv::dt_char] = do_read<VAL_T, char>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3243 read_fptr_table[R][oct_data_conv::dt_schar] = do_read<VAL_T, signed char>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3244 read_fptr_table[R][oct_data_conv::dt_uchar] = do_read<VAL_T, unsigned char>; \ |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3245 read_fptr_table[R][oct_data_conv::dt_logical] = do_read<VAL_T, unsigned char> |
4944 | 3246 |
3247 octave_value | |
5275 | 3248 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
|
3249 oct_data_conv::data_type input_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3250 oct_data_conv::data_type output_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3251 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
|
3252 octave_idx_type& char_count) |
4944 | 3253 { |
3254 static bool initialized = false; | |
3255 | |
3256 // Table function pointers for return types x read types. | |
3257 | |
9202
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3258 static read_fptr read_fptr_table[oct_data_conv::dt_unknown][14]; |
4944 | 3259 |
3260 if (! initialized) | |
3261 { | |
9202
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3262 for (int i = 0; i < oct_data_conv::dt_unknown; i++) |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3263 for (int j = 0; j < 14; j++) |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3264 read_fptr_table[i][j] = 0; |
4b2147b25e8d
clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
9017
diff
changeset
|
3265 |
4944 | 3266 FILL_TABLE_ROW (oct_data_conv::dt_int8, int8NDArray); |
3267 FILL_TABLE_ROW (oct_data_conv::dt_uint8, uint8NDArray); | |
3268 FILL_TABLE_ROW (oct_data_conv::dt_int16, int16NDArray); | |
3269 FILL_TABLE_ROW (oct_data_conv::dt_uint16, uint16NDArray); | |
3270 FILL_TABLE_ROW (oct_data_conv::dt_int32, int32NDArray); | |
3271 FILL_TABLE_ROW (oct_data_conv::dt_uint32, uint32NDArray); | |
3272 FILL_TABLE_ROW (oct_data_conv::dt_int64, int64NDArray); | |
3273 FILL_TABLE_ROW (oct_data_conv::dt_uint64, uint64NDArray); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3274 FILL_TABLE_ROW (oct_data_conv::dt_single, FloatNDArray); |
4944 | 3275 FILL_TABLE_ROW (oct_data_conv::dt_double, NDArray); |
3276 FILL_TABLE_ROW (oct_data_conv::dt_char, charNDArray); | |
3277 FILL_TABLE_ROW (oct_data_conv::dt_schar, charNDArray); | |
3278 FILL_TABLE_ROW (oct_data_conv::dt_uchar, charNDArray); | |
4970 | 3279 FILL_TABLE_ROW (oct_data_conv::dt_logical, boolNDArray); |
4944 | 3280 |
3281 initialized = true; | |
3282 } | |
3283 | |
3284 octave_value retval; | |
3285 | |
5659 | 3286 if (stream_ok ()) |
4944 | 3287 { |
5775 | 3288 // FIXME -- we may eventually want to make this extensible. |
3289 | |
3290 // FIXME -- we need a better way to ensure that this | |
4944 | 3291 // numbering stays consistent with the order of the elements in the |
3292 // data_type enum in the oct_data_conv class. | |
3293 | |
3294 char_count = 0; | |
3295 | |
5275 | 3296 octave_idx_type nr = -1; |
3297 octave_idx_type nc = -1; | |
4944 | 3298 |
3299 bool ignore; | |
3300 | |
3301 get_size (size, nr, nc, ignore, "fread"); | |
3302 | |
3303 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3304 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3305 if (nr == 0 || nc == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3306 retval = Matrix (nr, nc); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3307 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3308 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3309 if (ffmt == oct_mach_info::flt_fmt_unknown) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3310 ffmt = float_format (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3311 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3312 read_fptr fcn = read_fptr_table[output_type][input_type]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3313 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3314 bool do_float_fmt_conv = ((input_type == oct_data_conv::dt_double |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3315 || input_type == oct_data_conv::dt_single) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3316 && ffmt != float_format ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3317 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3318 bool do_NA_conv = (output_type == oct_data_conv::dt_double); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3319 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3320 if (fcn) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3321 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3322 retval = (*fcn) (*this, nr, nc, block_size, skip, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3323 do_float_fmt_conv, do_NA_conv, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3324 ffmt, char_count); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3325 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3326 // FIXME -- kluge! |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3327 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3328 if (! error_state |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3329 && (output_type == oct_data_conv::dt_char |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3330 || output_type == oct_data_conv::dt_schar |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3331 || output_type == oct_data_conv::dt_uchar)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3332 retval = retval.char_matrix_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3333 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3334 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3335 error ("fread: unable to read and convert requested types"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3336 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3337 } |
4944 | 3338 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3339 invalid_operation ("fread", "reading"); |
4944 | 3340 } |
2117 | 3341 |
3342 return retval; | |
3343 } | |
3344 | |
5275 | 3345 octave_idx_type |
3346 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
|
3347 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
|
3348 oct_mach_info::float_format flt_fmt) |
2117 | 3349 { |
5275 | 3350 octave_idx_type retval = -1; |
2117 | 3351 |
5659 | 3352 if (stream_ok ()) |
4944 | 3353 { |
3354 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3355 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3356 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
|
3357 flt_fmt = float_format (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3358 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3359 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
|
3360 skip, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3361 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3362 if (status < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3363 error ("fwrite: write error"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3364 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3365 retval = status; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3366 } |
4944 | 3367 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3368 invalid_operation ("fwrite", "writing"); |
4944 | 3369 } |
2117 | 3370 |
3371 return retval; | |
3372 } | |
3373 | |
4944 | 3374 template <class T> |
3375 void | |
3376 write_int (std::ostream& os, bool swap, const T& val) | |
3377 { | |
9685 | 3378 typename T::val_type tmp = val.value (); |
4944 | 3379 |
3380 if (swap) | |
9685 | 3381 swap_bytes<sizeof (typename T::val_type)> (&tmp); |
4944 | 3382 |
3383 os.write (reinterpret_cast<const char *> (&tmp), | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3384 sizeof (typename T::val_type)); |
4944 | 3385 } |
3386 | |
3387 template void write_int (std::ostream&, bool, const octave_int8&); | |
3388 template void write_int (std::ostream&, bool, const octave_uint8&); | |
3389 template void write_int (std::ostream&, bool, const octave_int16&); | |
3390 template void write_int (std::ostream&, bool, const octave_uint16&); | |
3391 template void write_int (std::ostream&, bool, const octave_int32&); | |
3392 template void write_int (std::ostream&, bool, const octave_uint32&); | |
3393 template void write_int (std::ostream&, bool, const octave_int64&); | |
3394 template void write_int (std::ostream&, bool, const octave_uint64&); | |
3395 | |
3396 template <class T> | |
3397 static inline bool | |
3398 do_write (std::ostream& os, const T& val, oct_data_conv::data_type output_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3399 oct_mach_info::float_format flt_fmt, bool swap, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3400 bool do_float_conversion) |
4944 | 3401 { |
3402 bool retval = true; | |
3403 | |
3404 // For compatibility, Octave converts to the output type, then | |
3405 // writes. This means that truncation happens on the conversion. | |
3406 // For example, the following program prints 0: | |
3407 // | |
3408 // x = int8 (-1) | |
3409 // f = fopen ("foo.dat", "w"); | |
3410 // fwrite (f, x, "unsigned char"); | |
3411 // fclose (f); | |
3412 // f = fopen ("foo.dat", "r"); | |
3413 // y = fread (f, 1, "unsigned char"); | |
3414 // printf ("%d\n", y); | |
3415 | |
3416 switch (output_type) | |
3417 { | |
3418 case oct_data_conv::dt_char: | |
3419 case oct_data_conv::dt_schar: | |
3420 case oct_data_conv::dt_int8: | |
3421 write_int (os, swap, octave_int8 (val)); | |
3422 break; | |
3423 | |
3424 case oct_data_conv::dt_uchar: | |
3425 case oct_data_conv::dt_uint8: | |
3426 write_int (os, swap, octave_uint8 (val)); | |
3427 break; | |
3428 | |
3429 case oct_data_conv::dt_int16: | |
3430 write_int (os, swap, octave_int16 (val)); | |
3431 break; | |
3432 | |
3433 case oct_data_conv::dt_uint16: | |
3434 write_int (os, swap, octave_uint16 (val)); | |
3435 break; | |
3436 | |
3437 case oct_data_conv::dt_int32: | |
3438 write_int (os, swap, octave_int32 (val)); | |
3439 break; | |
3440 | |
3441 case oct_data_conv::dt_uint32: | |
3442 write_int (os, swap, octave_uint32 (val)); | |
3443 break; | |
3444 | |
3445 case oct_data_conv::dt_int64: | |
3446 write_int (os, swap, octave_int64 (val)); | |
3447 break; | |
3448 | |
3449 case oct_data_conv::dt_uint64: | |
3450 write_int (os, swap, octave_uint64 (val)); | |
3451 break; | |
3452 | |
3453 case oct_data_conv::dt_single: | |
3454 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3455 float f = static_cast<float> (val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3456 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3457 if (do_float_conversion) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3458 do_float_format_conversion (&f, 1, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3459 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3460 os.write (reinterpret_cast<const char *> (&f), sizeof (float)); |
4944 | 3461 } |
3462 break; | |
3463 | |
3464 case oct_data_conv::dt_double: | |
3465 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3466 double d = static_cast<double> (val); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3467 if (do_float_conversion) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3468 do_double_format_conversion (&d, 1, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3469 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3470 os.write (reinterpret_cast<const char *> (&d), sizeof (double)); |
4944 | 3471 } |
3472 break; | |
3473 | |
3474 default: | |
3475 retval = false; | |
3476 (*current_liboctave_error_handler) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3477 ("write: invalid type specification"); |
4944 | 3478 break; |
3479 } | |
3480 | |
3481 return retval; | |
3482 } | |
3483 | |
5887 | 3484 template bool |
3485 do_write (std::ostream&, const octave_int8&, oct_data_conv::data_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3486 oct_mach_info::float_format, bool, bool); |
5887 | 3487 |
3488 template bool | |
3489 do_write (std::ostream&, const octave_uint8&, oct_data_conv::data_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3490 oct_mach_info::float_format, bool, bool); |
5887 | 3491 |
3492 template bool | |
3493 do_write (std::ostream&, const octave_int16&, oct_data_conv::data_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3494 oct_mach_info::float_format, bool, bool); |
5887 | 3495 |
3496 template bool | |
3497 do_write (std::ostream&, const octave_uint16&, oct_data_conv::data_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3498 oct_mach_info::float_format, bool, bool); |
5887 | 3499 |
3500 template bool | |
3501 do_write (std::ostream&, const octave_int32&, oct_data_conv::data_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3502 oct_mach_info::float_format, bool, bool); |
5887 | 3503 |
3504 template bool | |
3505 do_write (std::ostream&, const octave_uint32&, oct_data_conv::data_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3506 oct_mach_info::float_format, bool, bool); |
5887 | 3507 |
3508 template bool | |
3509 do_write (std::ostream&, const octave_int64&, oct_data_conv::data_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3510 oct_mach_info::float_format, bool, bool); |
5887 | 3511 |
3512 template bool | |
3513 do_write (std::ostream&, const octave_uint64&, oct_data_conv::data_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3514 oct_mach_info::float_format, bool, bool); |
5887 | 3515 |
4944 | 3516 template <class T> |
5275 | 3517 octave_idx_type |
3518 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
|
3519 oct_data_conv::data_type output_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3520 octave_idx_type skip, oct_mach_info::float_format flt_fmt) |
4944 | 3521 { |
5275 | 3522 octave_idx_type retval = -1; |
4944 | 3523 |
3524 bool status = true; | |
3525 | |
5275 | 3526 octave_idx_type count = 0; |
4944 | 3527 |
3528 const T *d = data.data (); | |
3529 | |
5275 | 3530 octave_idx_type n = data.length (); |
4944 | 3531 |
3532 oct_mach_info::float_format native_flt_fmt | |
3533 = oct_mach_info::float_format (); | |
3534 | |
3535 bool do_float_conversion = (flt_fmt != native_flt_fmt); | |
3536 | |
5775 | 3537 // FIXME -- byte order for Cray? |
4944 | 3538 |
3539 bool swap = false; | |
3540 | |
3541 if (oct_mach_info::words_big_endian ()) | |
3542 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3543 || flt_fmt == oct_mach_info::flt_fmt_vax_g |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3544 || flt_fmt == oct_mach_info::flt_fmt_vax_g); |
4944 | 3545 else |
3546 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); | |
3547 | |
5275 | 3548 for (octave_idx_type i = 0; i < n; i++) |
4944 | 3549 { |
3550 std::ostream *osp = output_stream (); | |
3551 | |
3552 if (osp) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3553 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3554 std::ostream& os = *osp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3555 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3556 if (skip != 0 && (i % block_size) == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3557 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3558 // Seek to skip when inside bounds of existing file. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3559 // Otherwise, write NUL to skip. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3560 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3561 long orig_pos = tell (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3562 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3563 seek (0, SEEK_END); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3564 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3565 long eof_pos = tell (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3566 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3567 // Is it possible for this to fail to return us to the |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3568 // original position? |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3569 seek (orig_pos, SEEK_SET); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3570 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3571 long remaining = eof_pos - orig_pos; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3572 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3573 if (remaining < skip) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3574 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3575 seek (0, SEEK_END); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3576 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3577 // FIXME -- probably should try to write larger |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3578 // blocks... |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3579 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3580 unsigned char zero = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3581 for (octave_idx_type j = 0; j < skip - remaining; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3582 os.write (reinterpret_cast<const char *> (&zero), 1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3583 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3584 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3585 seek (skip, SEEK_CUR); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3586 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3587 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3588 if (os) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3589 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3590 status = do_write (os, d[i], output_type, flt_fmt, swap, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3591 do_float_conversion); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3592 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3593 if (os && status) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3594 count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3595 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3596 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3597 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3598 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3599 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3600 status = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3601 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3602 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3603 } |
4944 | 3604 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3605 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3606 status = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3607 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3608 } |
4944 | 3609 } |
3610 | |
3611 if (status) | |
3612 retval = count; | |
3613 | |
3614 return retval; | |
3615 } | |
3616 | |
5275 | 3617 template octave_idx_type |
3618 octave_stream::write (const Array<char>&, octave_idx_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3619 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3620 octave_idx_type, oct_mach_info::float_format); |
5275 | 3621 |
3622 template octave_idx_type | |
3623 octave_stream::write (const Array<bool>&, octave_idx_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3624 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3625 octave_idx_type, oct_mach_info::float_format); |
5275 | 3626 |
3627 template octave_idx_type | |
3628 octave_stream::write (const Array<double>&, octave_idx_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3629 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3630 octave_idx_type, oct_mach_info::float_format); |
5275 | 3631 |
3632 template octave_idx_type | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3633 octave_stream::write (const Array<float>&, octave_idx_type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3634 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3635 octave_idx_type, oct_mach_info::float_format); |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3636 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3637 template octave_idx_type |
5275 | 3638 octave_stream::write (const Array<octave_int8>&, octave_idx_type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3639 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3640 octave_idx_type, oct_mach_info::float_format); |
5275 | 3641 |
3642 template octave_idx_type | |
3643 octave_stream::write (const Array<octave_uint8>&, octave_idx_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3644 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3645 octave_idx_type, oct_mach_info::float_format); |
5275 | 3646 |
3647 template octave_idx_type | |
3648 octave_stream::write (const Array<octave_int16>&, octave_idx_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3649 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3650 octave_idx_type, oct_mach_info::float_format); |
5275 | 3651 |
3652 template octave_idx_type | |
3653 octave_stream::write (const Array<octave_uint16>&, octave_idx_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3654 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3655 octave_idx_type, oct_mach_info::float_format); |
5275 | 3656 |
3657 template octave_idx_type | |
3658 octave_stream::write (const Array<octave_int32>&, octave_idx_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3659 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3660 octave_idx_type, oct_mach_info::float_format); |
5275 | 3661 |
3662 template octave_idx_type | |
3663 octave_stream::write (const Array<octave_uint32>&, octave_idx_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3664 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3665 octave_idx_type, oct_mach_info::float_format); |
5275 | 3666 |
3667 template octave_idx_type | |
3668 octave_stream::write (const Array<octave_int64>&, octave_idx_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3669 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3670 octave_idx_type, oct_mach_info::float_format); |
5275 | 3671 |
3672 template octave_idx_type | |
3673 octave_stream::write (const Array<octave_uint64>&, octave_idx_type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3674 oct_data_conv::data_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3675 octave_idx_type, oct_mach_info::float_format); |
4944 | 3676 |
2117 | 3677 octave_value |
3810 | 3678 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
|
3679 octave_idx_type& count, const std::string& who) |
2117 | 3680 { |
3681 octave_value retval; | |
3682 | |
5659 | 3683 if (stream_ok ()) |
4468 | 3684 retval = rep->scanf (fmt, size, count, who); |
2117 | 3685 |
3686 return retval; | |
3687 } | |
3688 | |
5279 | 3689 octave_value |
3690 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
|
3691 octave_idx_type& count, const std::string& who) |
5279 | 3692 { |
3693 octave_value retval = Matrix (); | |
3694 | |
3695 if (fmt.is_string ()) | |
3696 { | |
3697 std::string sfmt = fmt.string_value (); | |
3698 | |
3699 if (fmt.is_sq_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3700 sfmt = do_string_escapes (sfmt); |
5279 | 3701 |
3702 retval = scanf (sfmt, size, count, who); | |
3703 } | |
3704 else | |
3705 { | |
3706 // Note that this is not ::error () ! | |
3707 | |
3708 error (who + ": format must be a string"); | |
3709 } | |
3710 | |
3711 return retval; | |
3712 } | |
3713 | |
2215 | 3714 octave_value_list |
4468 | 3715 octave_stream::oscanf (const std::string& fmt, const std::string& who) |
2215 | 3716 { |
3717 octave_value_list retval; | |
3718 | |
5659 | 3719 if (stream_ok ()) |
4468 | 3720 retval = rep->oscanf (fmt, who); |
2215 | 3721 |
3722 return retval; | |
3723 } | |
3724 | |
5279 | 3725 octave_value_list |
3726 octave_stream::oscanf (const octave_value& fmt, const std::string& who) | |
3727 { | |
3728 octave_value_list retval; | |
3729 | |
3730 if (fmt.is_string ()) | |
3731 { | |
3732 std::string sfmt = fmt.string_value (); | |
3733 | |
3734 if (fmt.is_sq_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3735 sfmt = do_string_escapes (sfmt); |
5279 | 3736 |
3737 retval = oscanf (sfmt, who); | |
3738 } | |
3739 else | |
3740 { | |
3741 // Note that this is not ::error () ! | |
3742 | |
3743 error (who + ": format must be a string"); | |
3744 } | |
3745 | |
3746 return retval; | |
3747 } | |
3748 | |
2117 | 3749 int |
4468 | 3750 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
|
3751 const std::string& who) |
2117 | 3752 { |
3753 int retval = -1; | |
3754 | |
5659 | 3755 if (stream_ok ()) |
4468 | 3756 retval = rep->printf (fmt, args, who); |
2117 | 3757 |
3758 return retval; | |
3759 } | |
3760 | |
3761 int | |
5279 | 3762 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
|
3763 const std::string& who) |
5279 | 3764 { |
3765 int retval = 0; | |
3766 | |
3767 if (fmt.is_string ()) | |
3768 { | |
3769 std::string sfmt = fmt.string_value (); | |
3770 | |
3771 if (fmt.is_sq_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3772 sfmt = do_string_escapes (sfmt); |
5279 | 3773 |
3774 retval = printf (sfmt, args, who); | |
3775 } | |
3776 else | |
3777 { | |
3778 // Note that this is not ::error () ! | |
3779 | |
3780 error (who + ": format must be a string"); | |
3781 } | |
3782 | |
3783 return retval; | |
3784 } | |
3785 | |
3786 int | |
4468 | 3787 octave_stream::puts (const std::string& s, const std::string& who) |
2117 | 3788 { |
3789 int retval = -1; | |
3790 | |
5659 | 3791 if (stream_ok ()) |
4468 | 3792 retval = rep->puts (s, who); |
2117 | 3793 |
3794 return retval; | |
3795 } | |
3796 | |
5775 | 3797 // FIXME -- maybe this should work for string arrays too. |
2117 | 3798 |
3799 int | |
4468 | 3800 octave_stream::puts (const octave_value& tc_s, const std::string& who) |
2117 | 3801 { |
3802 int retval = -1; | |
3803 | |
3804 if (tc_s.is_string ()) | |
3805 { | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
3806 std::string s = tc_s.string_value (); |
5279 | 3807 retval = puts (s, who); |
2117 | 3808 } |
3809 else | |
4468 | 3810 { |
3811 // Note that this is not ::error () ! | |
3812 | |
3813 error (who + ": argument must be a string"); | |
3814 } | |
2117 | 3815 |
3816 return retval; | |
3817 } | |
3818 | |
3819 bool | |
3820 octave_stream::eof (void) const | |
3821 { | |
3822 int retval = -1; | |
3823 | |
5659 | 3824 if (stream_ok ()) |
2117 | 3825 retval = rep->eof (); |
3826 | |
3827 return retval; | |
3828 } | |
3829 | |
3536 | 3830 std::string |
2435 | 3831 octave_stream::error (bool clear, int& err_num) |
2117 | 3832 { |
5649 | 3833 std::string retval = "invalid stream object"; |
3834 | |
5659 | 3835 if (stream_ok (false)) |
2435 | 3836 retval = rep->error (clear, err_num); |
2117 | 3837 |
3838 return retval; | |
3839 } | |
3840 | |
3536 | 3841 std::string |
3340 | 3842 octave_stream::name (void) const |
2117 | 3843 { |
3523 | 3844 std::string retval; |
2117 | 3845 |
5659 | 3846 if (stream_ok ()) |
2117 | 3847 retval = rep->name (); |
3848 | |
3849 return retval; | |
3850 } | |
3851 | |
3852 int | |
3340 | 3853 octave_stream::mode (void) const |
2117 | 3854 { |
3855 int retval = 0; | |
3856 | |
5659 | 3857 if (stream_ok ()) |
2117 | 3858 retval = rep->mode (); |
3859 | |
3860 return retval; | |
3861 } | |
3862 | |
2317 | 3863 oct_mach_info::float_format |
3340 | 3864 octave_stream::float_format (void) const |
2117 | 3865 { |
4574 | 3866 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
2317 | 3867 |
5659 | 3868 if (stream_ok ()) |
2317 | 3869 retval = rep->float_format (); |
2117 | 3870 |
3871 return retval; | |
3872 } | |
3873 | |
3536 | 3874 std::string |
2117 | 3875 octave_stream::mode_as_string (int mode) |
3876 { | |
3523 | 3877 std::string retval = "???"; |
3775 | 3878 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode); |
3879 | |
3880 if (in_mode == std::ios::in) | |
3881 retval = "r"; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
3882 else if (in_mode == std::ios::out |
4078 | 3883 || in_mode == (std::ios::out | std::ios::trunc)) |
3775 | 3884 retval = "w"; |
4078 | 3885 else if (in_mode == (std::ios::out | std::ios::app)) |
3775 | 3886 retval = "a"; |
4078 | 3887 else if (in_mode == (std::ios::in | std::ios::out)) |
3775 | 3888 retval = "r+"; |
4078 | 3889 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc)) |
3775 | 3890 retval = "w+"; |
4078 | 3891 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate)) |
3775 | 3892 retval = "a+"; |
4078 | 3893 else if (in_mode == (std::ios::in | std::ios::binary)) |
3775 | 3894 retval = "rb"; |
4078 | 3895 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
|
3896 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary)) |
3775 | 3897 retval = "wb"; |
4078 | 3898 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary)) |
3775 | 3899 retval = "ab"; |
4078 | 3900 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary)) |
3775 | 3901 retval = "r+b"; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
3902 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
|
3903 | std::ios::binary)) |
3775 | 3904 retval = "w+b"; |
4078 | 3905 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
|
3906 | std::ios::binary)) |
3775 | 3907 retval = "a+b"; |
2117 | 3908 |
3909 return retval; | |
3910 } | |
3911 | |
3912 octave_stream_list *octave_stream_list::instance = 0; | |
3913 | |
2926 | 3914 bool |
3915 octave_stream_list::instance_ok (void) | |
3916 { | |
3917 bool retval = true; | |
3918 | |
3919 if (! instance) | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3920 { |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3921 instance = new octave_stream_list (); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3922 |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3923 if (instance) |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
3924 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
|
3925 } |
2926 | 3926 |
3927 if (! instance) | |
3928 { | |
3929 ::error ("unable to create stream list object!"); | |
3930 | |
3931 retval = false; | |
3932 } | |
3933 | |
3934 return retval; | |
3935 } | |
3936 | |
5353 | 3937 int |
6757 | 3938 octave_stream_list::insert (octave_stream& os) |
2926 | 3939 { |
5353 | 3940 return (instance_ok ()) ? instance->do_insert (os) : -1; |
2926 | 3941 } |
3942 | |
3340 | 3943 octave_stream |
3523 | 3944 octave_stream_list::lookup (int fid, const std::string& who) |
2926 | 3945 { |
3341 | 3946 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926 | 3947 } |
3948 | |
3340 | 3949 octave_stream |
3523 | 3950 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926 | 3951 { |
3341 | 3952 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926 | 3953 } |
3954 | |
3955 int | |
3523 | 3956 octave_stream_list::remove (int fid, const std::string& who) |
2926 | 3957 { |
3341 | 3958 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926 | 3959 } |
3960 | |
3961 int | |
3523 | 3962 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926 | 3963 { |
3341 | 3964 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926 | 3965 } |
3966 | |
3967 void | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
3968 octave_stream_list::clear (bool flush) |
2926 | 3969 { |
3970 if (instance) | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
3971 instance->do_clear (flush); |
2926 | 3972 } |
3973 | |
3974 string_vector | |
3975 octave_stream_list::get_info (int fid) | |
3976 { | |
3977 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); | |
3978 } | |
3979 | |
3980 string_vector | |
3981 octave_stream_list::get_info (const octave_value& fid) | |
3982 { | |
3983 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); | |
3984 } | |
3985 | |
3536 | 3986 std::string |
2926 | 3987 octave_stream_list::list_open_files (void) |
3988 { | |
3523 | 3989 return (instance_ok ()) ? instance->do_list_open_files () : std::string (); |
2926 | 3990 } |
3991 | |
3992 octave_value | |
3993 octave_stream_list::open_file_numbers (void) | |
3994 { | |
3995 return (instance_ok ()) | |
3996 ? instance->do_open_file_numbers () : octave_value (); | |
3997 } | |
3998 | |
3999 int | |
4000 octave_stream_list::get_file_number (const octave_value& fid) | |
4001 { | |
4002 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; | |
4003 } | |
4004 | |
5353 | 4005 int |
6757 | 4006 octave_stream_list::do_insert (octave_stream& os) |
2117 | 4007 { |
6757 | 4008 // Insert item with key corresponding to file-descriptor. |
4009 | |
4010 int stream_number; | |
4011 | |
4012 if ((stream_number = os.file_number ()) == -1) | |
4013 return stream_number; | |
4014 | |
4015 // Should we test for "(list.find (stream_number) != list.end ()) && | |
4016 // list[stream_number].is_open ()" and respond with "error | |
4017 // ("internal error: ...")"? It should not happen except for some | |
4018 // bug or if the user has opened a stream with an interpreted | |
4019 // command, but closed it directly with a system call in an | |
4020 // oct-file; then the kernel knows the fd is free, but Octave does | |
4021 // not know. If it happens, it should not do harm here to simply | |
4022 // overwrite this entry, although the wrong entry might have done | |
4023 // harm before. | |
4024 | |
4025 if (list.size () < list.max_size ()) | |
4026 list[stream_number] = os; | |
4027 else | |
2117 | 4028 { |
6757 | 4029 stream_number = -1; |
4030 error ("could not create file id"); | |
3340 | 4031 } |
2117 | 4032 |
5353 | 4033 return stream_number; |
6757 | 4034 |
2117 | 4035 } |
4036 | |
3341 | 4037 static void |
3523 | 4038 gripe_invalid_file_id (int fid, const std::string& who) |
3341 | 4039 { |
4040 if (who.empty ()) | |
4041 ::error ("invalid stream number = %d", fid); | |
4042 else | |
4043 ::error ("%s: invalid stream number = %d", who.c_str (), fid); | |
4044 } | |
4045 | |
3340 | 4046 octave_stream |
3523 | 4047 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117 | 4048 { |
3340 | 4049 octave_stream retval; |
2117 | 4050 |
6757 | 4051 if (fid >= 0) |
4052 { | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4053 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
|
4054 retval = lookup_cache->second; |
6757 | 4055 else |
8902
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 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
|
4058 |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4059 if (iter != list.end ()) |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4060 { |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4061 retval = iter->second; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4062 lookup_cache = iter; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4063 } |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4064 else |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4065 gripe_invalid_file_id (fid, who); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4066 } |
6757 | 4067 } |
3341 | 4068 else |
4069 gripe_invalid_file_id (fid, who); | |
2117 | 4070 |
4071 return retval; | |
4072 } | |
4073 | |
3340 | 4074 octave_stream |
3341 | 4075 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
|
4076 const std::string& who) const |
2117 | 4077 { |
3340 | 4078 octave_stream retval; |
2117 | 4079 |
4080 int i = get_file_number (fid); | |
4081 | |
4082 if (! error_state) | |
3341 | 4083 retval = do_lookup (i, who); |
2117 | 4084 |
4085 return retval; | |
4086 } | |
4087 | |
4088 int | |
3523 | 4089 octave_stream_list::do_remove (int fid, const std::string& who) |
2117 | 4090 { |
4091 int retval = -1; | |
4092 | |
3531 | 4093 // Can't remove stdin (std::cin), stdout (std::cout), or stderr |
4094 // (std::cerr). | |
2117 | 4095 |
6757 | 4096 if (fid > 2) |
2117 | 4097 { |
6757 | 4098 ostrl_map::iterator iter = list.find (fid); |
4099 | |
4100 if (iter != list.end ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4101 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4102 octave_stream os = iter->second; |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4103 list.erase (iter); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4104 lookup_cache = list.end (); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4105 |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4106 // FIXME: is this check redundant? |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4107 if (os.is_valid ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4108 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4109 os.close (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4110 retval = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4111 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4112 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4113 gripe_invalid_file_id (fid, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4114 } |
3341 | 4115 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4116 gripe_invalid_file_id (fid, who); |
2117 | 4117 } |
3341 | 4118 else |
4119 gripe_invalid_file_id (fid, who); | |
2117 | 4120 |
4121 return retval; | |
4122 } | |
4123 | |
4124 int | |
3523 | 4125 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117 | 4126 { |
4127 int retval = -1; | |
4128 | |
6054 | 4129 if (fid.is_string () && fid.string_value () == "all") |
4130 { | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4131 do_clear (false); |
6054 | 4132 |
4133 retval = 0; | |
4134 } | |
4135 else | |
4136 { | |
4137 int i = get_file_number (fid); | |
4138 | |
4139 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4140 retval = do_remove (i, who); |
6054 | 4141 } |
2117 | 4142 |
4143 return retval; | |
4144 } | |
4145 | |
4146 void | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4147 octave_stream_list::do_clear (bool flush) |
2117 | 4148 { |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4149 if (flush) |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4150 { |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4151 // Do flush stdout and stderr. |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4152 |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4153 list[0].flush (); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4154 list[1].flush (); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4155 } |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4156 |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4157 octave_stream saved_os[3]; |
2117 | 4158 // 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
|
4159 for (ostrl_map::iterator iter = list.begin (); iter != list.end (); iter++) |
6757 | 4160 { |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4161 int fid = iter->first; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4162 octave_stream os = iter->second; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4163 if (fid < 3) |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4164 saved_os[fid] = os; |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4165 else if (os.is_valid ()) |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4166 os.close (); |
6757 | 4167 } |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4168 list.clear (); |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4169 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
|
4170 lookup_cache = list.end (); |
2117 | 4171 } |
4172 | |
4173 string_vector | |
4174 octave_stream_list::do_get_info (int fid) const | |
4175 { | |
4176 string_vector retval; | |
4177 | |
3340 | 4178 octave_stream os = do_lookup (fid); |
2117 | 4179 |
3341 | 4180 if (os.is_valid ()) |
2117 | 4181 { |
4182 retval.resize (3); | |
4183 | |
14022
de90542b7afc
Return retval(1) before retval(0) to avoid re-sizing call on retval.
Rik <octave@nomad.inbox5.com>
parents:
13983
diff
changeset
|
4184 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); |
3340 | 4185 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
|
4186 retval(0) = os.name (); |
2117 | 4187 } |
4188 else | |
3341 | 4189 ::error ("invalid file id = %d", fid); |
2117 | 4190 |
4191 return retval; | |
4192 } | |
4193 | |
4194 string_vector | |
4195 octave_stream_list::do_get_info (const octave_value& fid) const | |
4196 { | |
4197 string_vector retval; | |
4198 | |
4199 int conv_err = 0; | |
4200 | |
4201 int int_fid = convert_to_valid_int (fid, conv_err); | |
4202 | |
4203 if (! conv_err) | |
4204 retval = do_get_info (int_fid); | |
4205 else | |
2915 | 4206 ::error ("file id must be a file object or integer value"); |
2117 | 4207 |
4208 return retval; | |
4209 } | |
4210 | |
3536 | 4211 std::string |
2117 | 4212 octave_stream_list::do_list_open_files (void) const |
4213 { | |
3523 | 4214 std::string retval; |
2117 | 4215 |
5765 | 4216 std::ostringstream buf; |
2117 | 4217 |
4218 buf << "\n" | |
4219 << " number mode arch name\n" | |
4220 << " ------ ---- ---- ----\n"; | |
4221 | |
6757 | 4222 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4223 { |
6757 | 4224 octave_stream os = p->second; |
2117 | 4225 |
4326 | 4226 buf << " " |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4227 << std::setiosflags (std::ios::right) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4228 << std::setw (4) << p->first << " " |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4229 << std::setiosflags (std::ios::left) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4230 << std::setw (3) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4231 << octave_stream::mode_as_string (os.mode ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4232 << " " |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4233 << std::setw (9) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4234 << 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
|
4235 << " " |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4236 << os.name () << "\n"; |
2117 | 4237 } |
4238 | |
5765 | 4239 buf << "\n"; |
4240 | |
4241 retval = buf.str (); | |
2117 | 4242 |
4243 return retval; | |
4244 } | |
4245 | |
4246 octave_value | |
4247 octave_stream_list::do_open_file_numbers (void) const | |
4248 { | |
6757 | 4249 Matrix retval (1, list.size (), 0.0); |
2117 | 4250 |
4251 int num_open = 0; | |
4252 | |
6757 | 4253 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4254 { |
6757 | 4255 // Skip stdin, stdout, and stderr. |
4256 | |
4257 if (p->first > 2 && p->second) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4258 retval(0,num_open++) = p->first; |
2117 | 4259 } |
4260 | |
4261 retval.resize ((num_open > 0), num_open); | |
4262 | |
4263 return retval; | |
4264 } | |
4265 | |
4266 int | |
2609 | 4267 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117 | 4268 { |
4269 int retval = -1; | |
4270 | |
4271 if (fid.is_string ()) | |
4272 { | |
3523 | 4273 std::string nm = fid.string_value (); |
2117 | 4274 |
6757 | 4275 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
|
4276 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4277 // 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
|
4278 // are unnamed. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4279 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4280 if (p->first > 2) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4281 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4282 octave_stream os = p->second; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4283 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4284 if (os && os.name () == nm) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4285 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4286 retval = p->first; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4287 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4288 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4289 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4290 } |
2117 | 4291 } |
4292 else | |
4293 { | |
4294 int conv_err = 0; | |
4295 | |
4296 int int_fid = convert_to_valid_int (fid, conv_err); | |
4297 | |
4298 if (conv_err) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4299 ::error ("file id must be a file object, std::string, or integer value"); |
2117 | 4300 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4301 retval = int_fid; |
2117 | 4302 } |
4303 | |
4304 return retval; | |
4305 } |