Mercurial > octave
annotate libinterp/corefcn/oct-stream.cc @ 21475:4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
* oct-stream.h, oct-stream.cc (class scanf_format_list,
class_printf_format_list): Use std::deque instead of Array to hold
format elements.
* libinterp/template-inst/Array-os.cc: Don't instantiate Array objects
of pointers to scanf_format_elt or printf_format_elt objects.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 18 Mar 2016 12:11:15 -0400 |
parents | 06d15e4e611a |
children | 4fc04d04dd9c |
rev | line source |
---|---|
2117 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19346
diff
changeset
|
3 Copyright (C) 1996-2015 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 | |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21297
diff
changeset
|
24 # include "config.h" |
2117 | 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 |
19269
65554f5847ac
don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents:
19123
diff
changeset
|
37 #include "Array.h" |
4944 | 38 #include "byte-swap.h" |
2117 | 39 #include "lo-ieee.h" |
40 #include "lo-mappers.h" | |
41 #include "lo-utils.h" | |
19269
65554f5847ac
don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents:
19123
diff
changeset
|
42 #include "oct-locbuf.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" | |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21040
diff
changeset
|
48 #include "errwarn.h" |
3342 | 49 #include "input.h" |
3775 | 50 #include "oct-stdstrm.h" |
2117 | 51 #include "oct-stream.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20938
diff
changeset
|
52 #include "ovl.h" |
20756
200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents:
20747
diff
changeset
|
53 #include "toplev.h" |
2117 | 54 #include "utils.h" |
55 | |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
56 // Programming Note: There are two very different error functions used |
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
57 // in the stream code. When invoked with "error (...)" the member |
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
58 // function from octave_stream or octave_base_stream is called. This |
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
59 // function sets the error state on the stream AND returns control to |
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
60 // the caller. The caller must then return a value at the end of the |
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
61 // function. When invoked with "::error (...)" the exception-based |
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
62 // error function from error.h is used. This function will throw an |
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
63 // exception and not return control to the caller. BE CAREFUL and |
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
64 // invoke the correct error function! |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
65 |
2117 | 66 // Possible values for conv_err: |
67 // | |
68 // 1 : not a real scalar | |
2902 | 69 // 2 : value is NaN |
70 // 3 : value is not an integer | |
2117 | 71 |
72 static int | |
73 convert_to_valid_int (const octave_value& tc, int& conv_err) | |
74 { | |
20747
ee2743bd07a8
eliminate various compiler warnings
John W. Eaton <jwe@octave.org>
parents:
20741
diff
changeset
|
75 conv_err = 0; |
ee2743bd07a8
eliminate various compiler warnings
John W. Eaton <jwe@octave.org>
parents:
20741
diff
changeset
|
76 |
2117 | 77 int retval = 0; |
78 | |
20747
ee2743bd07a8
eliminate various compiler warnings
John W. Eaton <jwe@octave.org>
parents:
20741
diff
changeset
|
79 double dval = 0.0; |
20741
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
80 |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
81 try |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
82 { |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
83 dval = tc.double_value (); |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
84 } |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
85 catch (const octave_execution_exception&) |
2117 | 86 { |
20756
200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents:
20747
diff
changeset
|
87 recover_from_exception (); |
200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents:
20747
diff
changeset
|
88 |
20741
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
89 conv_err = 1; |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
90 } |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
91 |
20747
ee2743bd07a8
eliminate various compiler warnings
John W. Eaton <jwe@octave.org>
parents:
20741
diff
changeset
|
92 if (! conv_err) |
2117 | 93 { |
5389 | 94 if (! lo_ieee_isnan (dval)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
95 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
96 int ival = NINT (dval); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
97 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
98 if (ival == dval) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
99 retval = ival; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
100 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
101 conv_err = 3; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
102 } |
2117 | 103 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
104 conv_err = 2; |
2117 | 105 } |
106 | |
107 return retval; | |
108 } | |
109 | |
110 static int | |
4468 | 111 get_size (double d, const std::string& who) |
2117 | 112 { |
113 int retval = -1; | |
114 | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
115 if (lo_ieee_isnan (d)) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
116 ::error ("%s: NaN is invalid as size specification", who.c_str ()); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
117 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
118 if (xisinf (d)) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
119 retval = -1; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
120 else |
2117 | 121 { |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
122 if (d < 0.0) |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
123 ::error ("%s: negative value invalid as size specification", |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
124 who.c_str ()); |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
125 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
126 retval = NINT (d); |
2117 | 127 } |
128 | |
129 return retval; | |
130 } | |
131 | |
132 static void | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
133 get_size (const Array<double>& size, octave_idx_type& nr, octave_idx_type& nc, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
134 bool& one_elt_size_spec, const std::string& who) |
2117 | 135 { |
136 nr = -1; | |
137 nc = -1; | |
138 | |
3268 | 139 one_elt_size_spec = false; |
140 | |
2117 | 141 double dnr = -1.0; |
142 double dnc = -1.0; | |
143 | |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20230
diff
changeset
|
144 octave_idx_type sz_len = size.numel (); |
3810 | 145 |
146 if (sz_len == 1) | |
2601 | 147 { |
3268 | 148 one_elt_size_spec = true; |
149 | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
150 dnr = size(0); |
4293 | 151 |
152 dnc = (dnr == 0.0) ? 0.0 : 1.0; | |
2601 | 153 } |
3810 | 154 else if (sz_len == 2) |
2117 | 155 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
156 dnr = size(0); |
3810 | 157 |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
158 if (xisinf (dnr)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
159 ::error ("%s: invalid size specification", who.c_str ()); |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
160 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
161 dnc = size(1); |
2117 | 162 } |
163 else | |
4468 | 164 ::error ("%s: invalid size specification", who.c_str ()); |
2117 | 165 |
20558
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
166 nr = get_size (dnr, who); |
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
167 |
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
168 if (dnc >= 0.0) |
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
169 nc = get_size (dnc, who); |
2117 | 170 } |
171 | |
3523 | 172 scanf_format_list::scanf_format_list (const std::string& s) |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
173 : nconv (0), curr_idx (0), fmt_elts (), buf (0) |
2117 | 174 { |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
175 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
|
176 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
177 size_t i = 0; |
2117 | 178 |
2215 | 179 int width = 0; |
2117 | 180 bool discard = false; |
181 char modifier = '\0'; | |
182 char type = '\0'; | |
183 | |
184 bool have_more = true; | |
185 | |
186 while (i < n) | |
187 { | |
188 have_more = true; | |
189 | |
190 if (! buf) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
191 buf = new std::ostringstream (); |
2117 | 192 |
193 if (s[i] == '%') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
194 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
195 // Process percent-escape conversion type. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
196 |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
197 process_conversion (s, i, n, width, discard, type, modifier); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
198 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
199 have_more = (buf != 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
200 } |
3483 | 201 else if (isspace (s[i])) |
10315
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 type = scanf_format_elt::whitespace_conversion; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
204 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
205 width = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
206 discard = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
207 modifier = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
208 *buf << " "; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
209 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
210 while (++i < n && isspace (s[i])) |
20946 | 211 ; // skip whitespace |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
212 |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
213 add_elt_to_list (width, discard, type, modifier); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
214 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
215 have_more = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
216 } |
3483 | 217 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
218 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
219 type = scanf_format_elt::literal_conversion; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
220 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
221 width = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
222 discard = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
223 modifier = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
224 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
225 while (i < n && ! isspace (s[i]) && s[i] != '%') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
226 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
227 |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
228 add_elt_to_list (width, discard, type, modifier); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
229 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
230 have_more = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
231 } |
2117 | 232 |
233 if (nconv < 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
234 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
235 have_more = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
236 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
237 } |
2117 | 238 } |
239 | |
240 if (have_more) | |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
241 add_elt_to_list (width, discard, type, modifier); |
2117 | 242 |
243 delete buf; | |
244 } | |
245 | |
246 scanf_format_list::~scanf_format_list (void) | |
247 { | |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
248 size_t n = fmt_elts.size (); |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
249 |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
250 for (size_t i = 0; i < n; i++) |
2117 | 251 { |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
252 scanf_format_elt *elt = fmt_elts[i]; |
2117 | 253 delete elt; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
254 } |
2117 | 255 } |
256 | |
257 void | |
2215 | 258 scanf_format_list::add_elt_to_list (int width, bool discard, char type, |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
259 char modifier, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
260 const std::string& char_class) |
2117 | 261 { |
262 if (buf) | |
263 { | |
5765 | 264 std::string text = buf->str (); |
4051 | 265 |
266 if (! text.empty ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
267 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
268 scanf_format_elt *elt |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
269 = 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
|
270 modifier, char_class); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
271 |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
272 fmt_elts.push_back (elt); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
273 } |
2117 | 274 |
275 delete buf; | |
276 buf = 0; | |
277 } | |
278 } | |
279 | |
3535 | 280 static std::string |
3523 | 281 expand_char_class (const std::string& s) |
3483 | 282 { |
3523 | 283 std::string retval; |
3483 | 284 |
285 size_t len = s.length (); | |
286 | |
287 size_t i = 0; | |
288 | |
289 while (i < len) | |
290 { | |
291 unsigned char c = s[i++]; | |
292 | |
293 if (c == '-' && i > 1 && i < len | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
294 && ( static_cast<unsigned char> (s[i-2]) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
295 <= static_cast<unsigned char> (s[i]))) |
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 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
|
298 // already added it below). |
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 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
|
301 retval += c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
302 } |
3483 | 303 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
304 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
305 // 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
|
306 // the last character in the class. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
307 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
308 if (c != '-' || i == len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
309 retval += c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
310 } |
3483 | 311 } |
312 | |
313 return retval; | |
314 } | |
315 | |
2117 | 316 void |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
317 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
|
318 size_t n, int& width, bool& discard, |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
319 char& type, char& modifier) |
2117 | 320 { |
2215 | 321 width = 0; |
2117 | 322 discard = false; |
323 modifier = '\0'; | |
324 type = '\0'; | |
325 | |
326 *buf << s[i++]; | |
327 | |
328 bool have_width = false; | |
329 | |
330 while (i < n) | |
331 { | |
332 switch (s[i]) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
333 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
334 case '*': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
335 if (discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
336 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
337 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
338 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
339 discard = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
340 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
341 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
342 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
343 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
344 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
|
345 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
|
346 if (have_width) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
347 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
348 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
349 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
350 char c = s[i++]; |
20230
e914b5399c67
Use in-place operators in C++ code where possible.
Rik <rik@octave.org>
parents:
19864
diff
changeset
|
351 width = 10 * width + c - '0'; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
352 have_width = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
353 *buf << c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
354 while (i < n && isdigit (s[i])) |
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 c = s[i++]; |
20230
e914b5399c67
Use in-place operators in C++ code where possible.
Rik <rik@octave.org>
parents:
19864
diff
changeset
|
357 width = 10 * width + c - '0'; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
358 *buf << c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
359 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
360 } |
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 'h': case 'l': case 'L': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
364 if (modifier != '\0') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
365 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
366 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
367 modifier = s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
368 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
369 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
370 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
|
371 if (modifier == 'L') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
372 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
373 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
374 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
375 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
376 goto fini; |
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 case 'e': case 'f': case 'g': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
379 if (modifier == 'h') |
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 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
382 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
383 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
384 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
385 // No float or long double conversions, thanks. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
386 *buf << 'l'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
387 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
388 goto fini; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
389 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
390 case 'c': case 's': case 'p': case '%': case '[': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
391 if (modifier != '\0') |
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 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
394 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
395 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
396 goto fini; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
397 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
398 fini: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
399 { |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
400 if (finish_conversion (s, i, n, width, discard, |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
401 type, modifier) == 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
402 return; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
403 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
404 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
405 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
406 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
407 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
408 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
409 } |
2117 | 410 |
411 if (nconv < 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
412 break; |
2117 | 413 } |
414 | |
415 nconv = -1; | |
416 } | |
417 | |
418 int | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
419 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
|
420 size_t n, int& width, bool discard, |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
421 char& type, char modifier) |
2117 | 422 { |
423 int retval = 0; | |
424 | |
3523 | 425 std::string char_class; |
3483 | 426 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
427 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
|
428 size_t end_idx = std::string::npos; |
3640 | 429 |
2117 | 430 if (s[i] == '%') |
3640 | 431 { |
432 type = '%'; | |
433 *buf << s[i++]; | |
434 } | |
2117 | 435 else |
436 { | |
437 type = s[i]; | |
438 | |
439 if (s[i] == '[') | |
10315
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 *buf << 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 if (i < n) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
444 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
445 beg_idx = i; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
446 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
447 if (s[i] == '^') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
448 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
449 type = '^'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
450 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
451 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
452 if (i < n) |
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 beg_idx = i; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
455 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
456 if (s[i] == ']') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
457 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
458 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
459 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
460 else if (s[i] == ']') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
461 *buf << 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 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
464 while (i < n && s[i] != ']') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
465 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
466 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
467 if (i < n && s[i] == ']') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
468 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
469 end_idx = i-1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
470 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
471 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
472 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
473 if (s[i-1] != ']') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
474 retval = nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
475 } |
2117 | 476 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
477 *buf << s[i++]; |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
478 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
479 nconv++; |
3640 | 480 } |
481 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
482 if (nconv >= 0) |
3640 | 483 { |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
484 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
|
485 char_class = expand_char_class (s.substr (beg_idx, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
486 end_idx - beg_idx + 1)); |
3640 | 487 |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
488 add_elt_to_list (width, discard, type, modifier, char_class); |
2117 | 489 } |
490 | |
491 return retval; | |
492 } | |
493 | |
494 void | |
495 scanf_format_list::printme (void) const | |
496 { | |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
497 size_t n = fmt_elts.size (); |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
498 |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
499 for (size_t i = 0; i < n; i++) |
2117 | 500 { |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
501 scanf_format_elt *elt = fmt_elts[i]; |
2117 | 502 |
3531 | 503 std::cerr |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
504 << "width: " << elt->width << "\n" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
505 << "discard: " << elt->discard << "\n" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
506 << "type: "; |
3483 | 507 |
508 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
|
509 std::cerr << "literal text\n"; |
3483 | 510 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
|
511 std::cerr << "whitespace\n"; |
3483 | 512 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
513 std::cerr << elt->type << "\n"; |
3531 | 514 |
515 std::cerr | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
516 << "modifier: " << elt->modifier << "\n" |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
517 << "char_class: '" << undo_string_escapes (elt->char_class) << "'\n" |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
518 << "text: '" << undo_string_escapes (elt->text) << "'\n\n"; |
2117 | 519 } |
520 } | |
521 | |
522 bool | |
523 scanf_format_list::all_character_conversions (void) | |
524 { | |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
525 size_t n = fmt_elts.size (); |
2117 | 526 |
527 if (n > 0) | |
528 { | |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
529 for (size_t i = 0; i < n; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
530 { |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
531 scanf_format_elt *elt = fmt_elts[i]; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
532 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
533 switch (elt->type) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
534 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
535 case 'c': case 's': case '%': case '[': case '^': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
536 case scanf_format_elt::literal_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
537 case scanf_format_elt::whitespace_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
538 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
539 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
540 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
541 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
542 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
543 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
544 } |
2117 | 545 |
546 return true; | |
547 } | |
548 else | |
549 return false; | |
550 } | |
551 | |
552 bool | |
553 scanf_format_list::all_numeric_conversions (void) | |
554 { | |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
555 size_t n = fmt_elts.size (); |
2117 | 556 |
557 if (n > 0) | |
558 { | |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
559 for (size_t i = 0; i < n; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
560 { |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
561 scanf_format_elt *elt = fmt_elts[i]; |
10315
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 switch (elt->type) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
564 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
565 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
|
566 case 'e': case 'f': case 'g': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
567 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
568 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
569 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
570 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
571 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
572 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
573 } |
2117 | 574 |
575 return true; | |
576 } | |
577 else | |
578 return false; | |
579 } | |
580 | |
581 // Ugh again. | |
582 | |
3523 | 583 printf_format_list::printf_format_list (const std::string& s) |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
584 : nconv (0), curr_idx (0), fmt_elts (), buf (0) |
2117 | 585 { |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
586 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
|
587 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
588 size_t i = 0; |
2117 | 589 |
590 int args = 0; | |
3643 | 591 std::string flags; |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
592 int fw = -1; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
593 int prec = -1; |
2117 | 594 char modifier = '\0'; |
595 char type = '\0'; | |
596 | |
597 bool have_more = true; | |
3640 | 598 bool empty_buf = true; |
2117 | 599 |
4223 | 600 if (n == 0) |
2117 | 601 { |
4223 | 602 printf_format_elt *elt |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
603 = new printf_format_elt ("", args, fw, prec, flags, type, modifier); |
4223 | 604 |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
605 fmt_elts.push_back (elt); |
4223 | 606 } |
607 else | |
608 { | |
609 while (i < n) | |
10315
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 have_more = true; |
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 if (! buf) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
614 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
615 buf = new std::ostringstream (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
616 empty_buf = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
617 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
618 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
619 switch (s[i]) |
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 case '%': |
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 if (empty_buf) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
624 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
625 process_conversion (s, i, n, args, flags, fw, prec, |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
626 type, modifier); |
10315
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 have_more = (buf != 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
629 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
630 else |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
631 add_elt_to_list (args, flags, fw, prec, type, modifier); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
632 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
633 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
634 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
635 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
636 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
637 args = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
638 flags = ""; |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
639 fw = -1; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
640 prec = -1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
641 modifier = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
642 type = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
643 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
644 empty_buf = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
645 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
646 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
647 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
648 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
649 if (nconv < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
650 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
651 have_more = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
652 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
653 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
654 } |
2117 | 655 |
4223 | 656 if (have_more) |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
657 add_elt_to_list (args, flags, fw, prec, type, modifier); |
4223 | 658 |
659 delete buf; | |
2117 | 660 } |
661 } | |
662 | |
663 printf_format_list::~printf_format_list (void) | |
664 { | |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
665 size_t n = fmt_elts.size (); |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
666 |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
667 for (size_t i = 0; i < n; i++) |
2117 | 668 { |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
669 printf_format_elt *elt = fmt_elts[i]; |
2117 | 670 delete elt; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
671 } |
2117 | 672 } |
673 | |
674 void | |
3640 | 675 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
|
676 int fw, int prec, char type, |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
677 char modifier) |
2117 | 678 { |
679 if (buf) | |
680 { | |
5765 | 681 std::string text = buf->str (); |
4051 | 682 |
683 if (! text.empty ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
684 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
685 printf_format_elt *elt |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
686 = 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
|
687 type, modifier); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
688 |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
689 fmt_elts.push_back (elt); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
690 } |
2117 | 691 |
692 delete buf; | |
693 buf = 0; | |
694 } | |
695 } | |
696 | |
697 void | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
698 printf_format_list::process_conversion (const std::string& s, size_t& i, |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
699 size_t n, int& args, |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
700 std::string& flags, int& fw, |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
701 int& prec, char& modifier, |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
702 char& type) |
2117 | 703 { |
704 args = 0; | |
3640 | 705 flags = ""; |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
706 fw = -1; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
707 prec = -1; |
2117 | 708 modifier = '\0'; |
709 type = '\0'; | |
710 | |
711 *buf << s[i++]; | |
712 | |
4587 | 713 bool nxt = false; |
2117 | 714 |
715 while (i < n) | |
716 { | |
717 switch (s[i]) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
718 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
719 case '-': case '+': case ' ': case '0': case '#': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
720 flags += s[i]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
721 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
722 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
723 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
724 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
725 nxt = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
726 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
727 } |
2117 | 728 |
4587 | 729 if (nxt) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
730 break; |
2117 | 731 } |
732 | |
733 if (i < n) | |
734 { | |
735 if (s[i] == '*') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
736 { |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
737 fw = -2; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
738 args++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
739 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
740 } |
2117 | 741 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
742 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
743 if (isdigit (s[i])) |
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 int nn = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
746 std::string tmp = s.substr (i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
747 sscanf (tmp.c_str (), "%d%n", &fw, &nn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
748 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
749 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
750 while (i < n && isdigit (s[i])) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
751 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
752 } |
2117 | 753 } |
754 | |
755 if (i < n && s[i] == '.') | |
756 { | |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
757 // nothing before the . means 0. |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
758 if (fw == -1) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
759 fw = 0; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
760 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
761 // . followed by nothing is 0. |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
762 prec = 0; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
763 |
2117 | 764 *buf << s[i++]; |
765 | |
766 if (i < n) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
767 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
768 if (s[i] == '*') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
769 { |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
770 prec = -2; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
771 args++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
772 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
773 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
774 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
775 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
776 if (isdigit (s[i])) |
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 int nn = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
779 std::string tmp = s.substr (i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
780 sscanf (tmp.c_str (), "%d%n", &prec, &nn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
781 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
782 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
783 while (i < n && isdigit (s[i])) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
784 *buf << s[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
785 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
786 } |
2117 | 787 } |
788 | |
789 if (i < n) | |
790 { | |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
791 // Accept and record modifier, but don't place it in the format |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
792 // item text. All integer conversions are handled as 64-bit |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
793 // integers. |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
794 |
2117 | 795 switch (s[i]) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
796 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
797 case 'h': case 'l': case 'L': |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
798 modifier = s[i++]; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
799 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
800 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
801 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
802 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
803 } |
2117 | 804 } |
805 | |
806 if (i < n) | |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
807 finish_conversion (s, i, args, flags, fw, prec, modifier, type); |
2117 | 808 else |
809 nconv = -1; | |
810 } | |
811 | |
812 void | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
813 printf_format_list::finish_conversion (const std::string& s, size_t& i, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
814 int args, const std::string& flags, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
815 int fw, int prec, char modifier, |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
816 char& type) |
2117 | 817 { |
818 switch (s[i]) | |
819 { | |
820 case 'd': case 'i': case 'o': case 'x': case 'X': | |
821 case 'u': case 'c': | |
822 if (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 'f': case 'e': case 'E': case 'g': case 'G': | |
830 if (modifier == 'h' || modifier == 'l') | |
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 case 's': case 'p': case '%': | |
838 if (modifier != '\0') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
839 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
840 nconv = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
841 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
842 } |
2117 | 843 goto fini; |
844 | |
845 fini: | |
846 | |
3640 | 847 type = s[i]; |
848 | |
849 *buf << s[i++]; | |
850 | |
851 if (type != '%' || args != 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
852 nconv++; |
3640 | 853 |
854 if (type != '%') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
855 args++; |
3640 | 856 |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
857 add_elt_to_list (args, flags, fw, prec, type, modifier); |
3640 | 858 |
2117 | 859 break; |
860 | |
861 default: | |
862 nconv = -1; | |
863 break; | |
864 } | |
865 } | |
866 | |
867 void | |
868 printf_format_list::printme (void) const | |
869 { | |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
870 size_t n = fmt_elts.size (); |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
871 |
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
872 for (size_t i = 0; i < n; i++) |
2117 | 873 { |
21475
4f3e63d75f33
use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents:
21354
diff
changeset
|
874 printf_format_elt *elt = fmt_elts[i]; |
2117 | 875 |
3640 | 876 std::cerr |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
877 << "args: " << elt->args << "\n" |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
878 << "flags: '" << elt->flags << "'\n" |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
879 << "width: " << elt->fw << "\n" |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
880 << "prec: " << elt->prec << "\n" |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
881 << "type: '" << elt->type << "'\n" |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
882 << "modifier: '" << elt->modifier << "'\n" |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
883 << "text: '" << undo_string_escapes (elt->text) << "'\n\n"; |
2117 | 884 } |
885 } | |
886 | |
887 void | |
3523 | 888 octave_base_stream::error (const std::string& msg) |
2117 | 889 { |
890 fail = true; | |
891 errmsg = msg; | |
892 } | |
893 | |
894 void | |
4468 | 895 octave_base_stream::error (const std::string& who, const std::string& msg) |
896 { | |
897 fail = true; | |
6296 | 898 errmsg = who + ": " + msg; |
4468 | 899 } |
900 | |
901 void | |
2117 | 902 octave_base_stream::clear (void) |
903 { | |
4889 | 904 fail = false; |
905 errmsg = ""; | |
906 } | |
907 | |
908 void | |
909 octave_base_stream::clearerr (void) | |
910 { | |
4888 | 911 std::istream *is = input_stream (); |
912 std::ostream *os = output_stream (); | |
913 | |
914 if (is) | |
915 is->clear (); | |
916 | |
917 if (os) | |
918 os->clear (); | |
2117 | 919 } |
920 | |
921 // Functions that are defined for all input streams (input streams | |
922 // are those that define is). | |
923 | |
3536 | 924 std::string |
5275 | 925 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
|
926 bool strip_newline, const std::string& who) |
2117 | 927 { |
19849
1c9ed5b4c73d
input.h: change meaning of interactive and forced_interactive global variables.
Carnë Draug <carandraug@octave.org>
parents:
19739
diff
changeset
|
928 if (interactive && file_number () == 0) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20768
diff
changeset
|
929 ::error ("%s: unable to read from stdin while running interactively", |
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20768
diff
changeset
|
930 who.c_str ()); |
8773
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
931 |
3523 | 932 std::string retval; |
2117 | 933 |
934 err = false; | |
935 | |
3523 | 936 std::istream *isp = input_stream (); |
2117 | 937 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
938 if (! isp) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
939 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
940 err = true; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
941 invalid_operation (who, "reading"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
942 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
943 else |
2117 | 944 { |
3523 | 945 std::istream& is = *isp; |
2117 | 946 |
5765 | 947 std::ostringstream buf; |
2117 | 948 |
949 int c = 0; | |
3553 | 950 int char_count = 0; |
6345 | 951 |
952 if (max_len != 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
953 { |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
954 while (is && (c = is.get ()) != std::istream::traits_type::eof ()) |
10315
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 char_count++; |
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 // Handle CRLF, CR, or LF as line ending. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
959 if (c == '\r') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
960 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
961 if (! strip_newline) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
962 buf << static_cast<char> (c); |
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 c = is.get (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
965 |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
966 if (c != std::istream::traits_type::eof ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
967 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
968 if (c == '\n') |
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 char_count++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
971 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
972 if (! strip_newline) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
973 buf << static_cast<char> (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
974 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
975 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
976 is.putback (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 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
979 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
980 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
981 else if (c == '\n') |
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 (! strip_newline) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
984 buf << static_cast<char> (c); |
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 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
987 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
988 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
989 buf << static_cast<char> (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
990 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
991 if (max_len > 0 && char_count == max_len) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
992 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
993 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
994 } |
6345 | 995 |
996 if (! is.eof () && char_count > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
997 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
998 // GAGME. Matlab seems to check for EOF even if the last character |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
999 // in a file is a newline character. This is NOT what the |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1000 // corresponding C-library functions do. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1001 int disgusting_compatibility_hack = is.get (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1002 if (! is.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1003 is.putback (disgusting_compatibility_hack); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1004 } |
2117 | 1005 |
4224 | 1006 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
|
1007 retval = buf.str (); |
4224 | 1008 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1009 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1010 err = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1011 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1012 if (is.eof () && char_count == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1013 error (who, "at end of file"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1014 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1015 error (who, "read error"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1016 } |
2117 | 1017 } |
1018 | |
1019 return retval; | |
1020 } | |
1021 | |
3536 | 1022 std::string |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1023 octave_base_stream::getl (octave_idx_type max_len, bool& err, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1024 const std::string& who) |
2117 | 1025 { |
4468 | 1026 return do_gets (max_len, err, true, who); |
2117 | 1027 } |
1028 | |
3536 | 1029 std::string |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1030 octave_base_stream::gets (octave_idx_type max_len, bool& err, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1031 const std::string& who) |
2117 | 1032 { |
4468 | 1033 return do_gets (max_len, err, false, who); |
2117 | 1034 } |
1035 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
1036 off_t |
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
1037 octave_base_stream::skipl (off_t num, bool& err, const std::string& who) |
9701 | 1038 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1039 if (interactive && file_number () == 0) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1040 ::error ("%s: unable to read from stdin while running interactively", |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1041 who.c_str ()); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1042 |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
1043 off_t cnt = -1; |
9701 | 1044 |
1045 err = false; | |
1046 | |
1047 std::istream *isp = input_stream (); | |
1048 | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1049 if (! isp) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1050 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1051 err = true; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1052 invalid_operation (who, "reading"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1053 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1054 else |
9701 | 1055 { |
1056 std::istream& is = *isp; | |
1057 | |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
1058 int c = 0; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
1059 int lastc = -1; |
9701 | 1060 cnt = 0; |
1061 | |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1062 while (is && (c = is.get ()) != std::istream::traits_type::eof ()) |
9701 | 1063 { |
1064 // Handle CRLF, CR, or LF as line ending. | |
1065 if (c == '\r' || (c == '\n' && lastc != '\r')) | |
1066 { | |
1067 if (++cnt == num) | |
1068 break; | |
1069 } | |
1070 | |
1071 lastc = c; | |
1072 } | |
1073 | |
1074 // Maybe eat the following \n if \r was just met. | |
1075 if (c == '\r' && is.peek () == '\n') | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1076 is.get (); |
9701 | 1077 |
1078 if (is.bad ()) | |
1079 { | |
1080 err = true; | |
1081 error (who, "read error"); | |
1082 } | |
1083 | |
1084 if (err) | |
1085 cnt = -1; | |
1086 } | |
1087 | |
1088 return cnt; | |
1089 } | |
1090 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
1091 template <typename T> |
3636 | 1092 std::istream& |
6767 | 1093 octave_scan_1 (std::istream& is, const scanf_format_elt& fmt, T* valptr) |
3636 | 1094 { |
3779 | 1095 T& ref = *valptr; |
1096 | |
1097 switch (fmt.type) | |
1098 { | |
1099 case 'o': | |
4926 | 1100 is >> std::oct >> ref >> std::dec; |
3779 | 1101 break; |
1102 | |
1103 case 'x': | |
4926 | 1104 is >> std::hex >> ref >> std::dec; |
1105 break; | |
1106 | |
1107 case 'i': | |
1108 { | |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1109 int c1 = std::istream::traits_type::eof (); |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1110 |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1111 while (is && (c1 = is.get ()) != std::istream::traits_type::eof () |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1112 && isspace (c1)) |
20946 | 1113 ; // skip whitespace |
14675
757f729fd41d
skip leading whitespace for scanf %i format
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
1114 |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1115 if (c1 != std::istream::traits_type::eof ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1116 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1117 if (c1 == '0') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1118 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1119 int c2 = is.peek (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1120 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1121 if (c2 == 'x' || c2 == 'X') |
7709
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1122 { |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1123 is.ignore (); |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1124 if (std::isxdigit (is.peek ())) |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1125 is >> std::hex >> ref >> std::dec; |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1126 else |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1127 ref = 0; |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1128 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1129 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1130 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1131 if (c2 == '0' || c2 == '1' || c2 == '2' |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1132 || c2 == '3' || c2 == '4' || c2 == '5' |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1133 || c2 == '6' || c2 == '7') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1134 is >> std::oct >> ref >> std::dec; |
21114
47908b818865
oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents:
21102
diff
changeset
|
1135 else if (c2 == '8' || c2 == '9') |
47908b818865
oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents:
21102
diff
changeset
|
1136 { |
47908b818865
oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents:
21102
diff
changeset
|
1137 // FIXME: Would like to set error state on octave stream. |
47908b818865
oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents:
21102
diff
changeset
|
1138 // See bug #46493. But only std::istream is input to fcn |
47908b818865
oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents:
21102
diff
changeset
|
1139 // error ("internal failure to match octal format"); |
47908b818865
oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents:
21102
diff
changeset
|
1140 ref = 0; |
47908b818865
oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents:
21102
diff
changeset
|
1141 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1142 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1143 ref = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1144 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1145 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1146 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1147 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1148 is.putback (c1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1149 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1150 is >> ref; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1151 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1152 } |
4926 | 1153 } |
3779 | 1154 break; |
1155 | |
1156 default: | |
1157 is >> ref; | |
1158 break; | |
1159 } | |
3639 | 1160 |
3638 | 1161 return is; |
3636 | 1162 } |
1163 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
1164 template <typename T> |
6767 | 1165 std::istream& |
1166 octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr) | |
1167 { | |
1168 if (fmt.width) | |
1169 { | |
1170 // Limit input to fmt.width characters by reading into a | |
1171 // temporary stringstream buffer. | |
1172 std::string tmp; | |
1173 | |
1174 is.width (fmt.width); | |
1175 is >> tmp; | |
1176 | |
1177 std::istringstream ss (tmp); | |
1178 | |
1179 octave_scan_1 (ss, fmt, valptr); | |
1180 } | |
1181 else | |
1182 octave_scan_1 (is, fmt, valptr); | |
1183 | |
1184 return is; | |
1185 } | |
1186 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
1187 // Note that this specialization is only used for reading characters, not |
3779 | 1188 // character strings. See BEGIN_S_CONVERSION for details. |
1189 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
1190 template <> |
3779 | 1191 std::istream& |
4661 | 1192 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
|
1193 char* valptr) |
3779 | 1194 { |
1195 return is >> valptr; | |
1196 } | |
3636 | 1197 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
1198 template <> |
5176 | 1199 std::istream& |
5403 | 1200 octave_scan<> (std::istream& is, const scanf_format_elt& fmt, double* valptr) |
5176 | 1201 { |
1202 double& ref = *valptr; | |
1203 | |
1204 switch (fmt.type) | |
1205 { | |
1206 case 'e': | |
1207 case 'f': | |
1208 case 'g': | |
1209 { | |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1210 int c1 = std::istream::traits_type::eof (); |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1211 |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1212 while (is && (c1 = is.get ()) != std::istream::traits_type::eof () |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1213 && isspace (c1)) |
20946 | 1214 ; // skip whitespace |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1215 |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1216 if (c1 != std::istream::traits_type::eof ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1217 { |
12936
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1218 is.putback (c1); |
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1219 |
b74cb659e757
accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
1220 ref = octave_read_value<double> (is); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1221 } |
5176 | 1222 } |
1223 break; | |
1224 | |
1225 default: | |
1226 panic_impossible (); | |
1227 break; | |
1228 } | |
1229 | |
1230 return is; | |
1231 } | |
1232 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
1233 template <typename T> |
2572 | 1234 void |
3636 | 1235 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
|
1236 T valptr, Matrix& mval, double *data, octave_idx_type& idx, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1237 octave_idx_type& conversion_count, octave_idx_type nr, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1238 octave_idx_type max_size, bool discard) |
2572 | 1239 { |
21147
95feb42d7a97
* oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents:
21139
diff
changeset
|
1240 octave_scan (is, fmt, valptr); |
2572 | 1241 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1242 if (! is) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1243 return; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1244 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1245 if (idx == max_size && ! discard) |
2572 | 1246 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1247 max_size *= 2; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1248 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1249 if (nr > 0) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1250 mval.resize (nr, max_size / nr, 0.0); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1251 else |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1252 mval.resize (max_size, 1, 0.0); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1253 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1254 data = mval.fortran_vec (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1255 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1256 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1257 if (! discard) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1258 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1259 conversion_count++; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1260 data[idx++] = *(valptr); |
2572 | 1261 } |
1262 } | |
1263 | |
1264 template void | |
3636 | 1265 do_scanf_conv (std::istream&, const scanf_format_elt&, double*, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1266 Matrix&, double*, octave_idx_type&, octave_idx_type&, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1267 octave_idx_type, octave_idx_type, bool); |
2572 | 1268 |
3483 | 1269 #define DO_WHITESPACE_CONVERSION() \ |
1270 do \ | |
1271 { \ | |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1272 int c = std::istream::traits_type::eof (); \ |
3483 | 1273 \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1274 while (is && (c = is.get ()) != std::istream::traits_type::eof () \ |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1275 && isspace (c)) \ |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1276 { /* skip whitespace */ } \ |
3483 | 1277 \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1278 if (c != std::istream::traits_type::eof ()) \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1279 is.putback (c); \ |
3483 | 1280 } \ |
1281 while (0) | |
1282 | |
1283 #define DO_LITERAL_CONVERSION() \ | |
1284 do \ | |
1285 { \ | |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1286 int c = std::istream::traits_type::eof (); \ |
3483 | 1287 \ |
1288 int n = strlen (fmt); \ | |
1289 int i = 0; \ | |
1290 \ | |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1291 while (i < n && is && (c = is.get ()) != std::istream::traits_type::eof ()) \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1292 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1293 if (c == static_cast<unsigned char> (fmt[i])) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1294 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1295 i++; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1296 continue; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1297 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1298 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1299 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1300 is.putback (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1301 break; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1302 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1303 } \ |
3483 | 1304 \ |
1305 if (i != n) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1306 is.setstate (std::ios::failbit); \ |
3483 | 1307 } \ |
1308 while (0) | |
1309 | |
3640 | 1310 #define DO_PCT_CONVERSION() \ |
1311 do \ | |
1312 { \ | |
1313 int c = is.get (); \ | |
1314 \ | |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1315 if (c != std::istream::traits_type::eof ()) \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1316 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1317 if (c != '%') \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1318 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1319 is.putback (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1320 is.setstate (std::ios::failbit); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1321 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1322 } \ |
3640 | 1323 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1324 is.setstate (std::ios::failbit); \ |
3640 | 1325 } \ |
1326 while (0) | |
1327 | |
3483 | 1328 #define BEGIN_C_CONVERSION() \ |
3538 | 1329 is.unsetf (std::ios::skipws); \ |
3483 | 1330 \ |
1331 int width = elt->width ? elt->width : 1; \ | |
1332 \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1333 std::string tmp (width, '\0'); \ |
3483 | 1334 \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1335 int c = std::istream::traits_type::eof (); \ |
3483 | 1336 int n = 0; \ |
1337 \ | |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1338 while (is && n < width && (c = is.get ()) != std::istream::traits_type::eof ()) \ |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1339 tmp[n++] = static_cast<char> (c); \ |
3483 | 1340 \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1341 if (n > 0 && c == std::istream::traits_type::eof ()) \ |
12984
7626f8934466
correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents:
12966
diff
changeset
|
1342 is.clear (); \ |
7626f8934466
correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents:
12966
diff
changeset
|
1343 \ |
7626f8934466
correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents:
12966
diff
changeset
|
1344 tmp.resize (n) |
3483 | 1345 |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
1346 // For a '%s' format, skip initial whitespace and then read until the |
5338 | 1347 // next whitespace character or until WIDTH characters have been read. |
3483 | 1348 #define BEGIN_S_CONVERSION() \ |
1349 int width = elt->width; \ | |
1350 \ | |
4051 | 1351 std::string tmp; \ |
3483 | 1352 \ |
1353 do \ | |
1354 { \ | |
1355 if (width) \ | |
5338 | 1356 { \ |
10076 | 1357 tmp = std::string (width, '\0'); \ |
5338 | 1358 \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1359 int c = std::istream::traits_type::eof (); \ |
5338 | 1360 \ |
1361 int n = 0; \ | |
1362 \ | |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1363 while (is && (c = is.get ()) != std::istream::traits_type::eof ()) \ |
5338 | 1364 { \ |
1365 if (! isspace (c)) \ | |
1366 { \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1367 tmp[n++] = static_cast<char> (c); \ |
5338 | 1368 break; \ |
1369 } \ | |
1370 } \ | |
4051 | 1371 \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1372 while (is && n < width \ |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1373 && (c = is.get ()) != std::istream::traits_type::eof ()) \ |
5338 | 1374 { \ |
1375 if (isspace (c)) \ | |
1376 { \ | |
1377 is.putback (c); \ | |
1378 break; \ | |
1379 } \ | |
1380 else \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1381 tmp[n++] = static_cast<char> (c); \ |
5338 | 1382 } \ |
3483 | 1383 \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1384 if (n > 0 && c == std::istream::traits_type::eof ()) \ |
5338 | 1385 is.clear (); \ |
1386 \ | |
9952
7cd2e1b372e5
allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents:
9941
diff
changeset
|
1387 tmp.resize (n); \ |
5338 | 1388 } \ |
3483 | 1389 else \ |
5338 | 1390 { \ |
1391 is >> std::ws >> tmp; \ | |
1392 } \ | |
3483 | 1393 } \ |
1394 while (0) | |
1395 | |
1396 // This format must match a nonempty sequence of characters. | |
1397 #define BEGIN_CHAR_CLASS_CONVERSION() \ | |
1398 int width = elt->width; \ | |
1399 \ | |
4051 | 1400 std::string tmp; \ |
3483 | 1401 \ |
1402 do \ | |
1403 { \ | |
7426 | 1404 if (! width) \ |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
1405 width = std::numeric_limits<int>::max (); \ |
7427 | 1406 \ |
7426 | 1407 std::ostringstream buf; \ |
1408 \ | |
1409 std::string char_class = elt->char_class; \ | |
4051 | 1410 \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1411 int c = std::istream::traits_type::eof (); \ |
3483 | 1412 \ |
7426 | 1413 if (elt->type == '[') \ |
1414 { \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1415 int chars_read = 0; \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1416 while (is && chars_read++ < width \ |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1417 && (c = is.get ()) != std::istream::traits_type::eof () \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1418 && char_class.find (c) != std::string::npos) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1419 buf << static_cast<char> (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1420 } \ |
3483 | 1421 else \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1422 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1423 int chars_read = 0; \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1424 while (is && chars_read++ < width \ |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1425 && (c = is.get ()) != std::istream::traits_type::eof () \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1426 && char_class.find (c) == std::string::npos) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1427 buf << static_cast<char> (c); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1428 } \ |
3483 | 1429 \ |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1430 if (width == std::numeric_limits<int>::max () \ |
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1431 && c != std::istream::traits_type::eof ()) \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1432 is.putback (c); \ |
3483 | 1433 \ |
7426 | 1434 tmp = buf.str (); \ |
3483 | 1435 \ |
7426 | 1436 if (tmp.empty ()) \ |
1437 is.setstate (std::ios::failbit); \ | |
21184
3db899f1d54c
use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents:
21147
diff
changeset
|
1438 else if (c == std::istream::traits_type::eof ()) \ |
10969
da355a1a6d44
fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
1439 is.clear (); \ |
da355a1a6d44
fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents:
10350
diff
changeset
|
1440 \ |
3483 | 1441 } \ |
1442 while (0) | |
1443 | |
3410 | 1444 #define FINISH_CHARACTER_CONVERSION() \ |
1445 do \ | |
1446 { \ | |
4051 | 1447 width = tmp.length (); \ |
3410 | 1448 \ |
1449 if (is) \ | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1450 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1451 int i = 0; \ |
3410 | 1452 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1453 if (! discard) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1454 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1455 conversion_count++; \ |
3410 | 1456 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1457 while (i < width) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1458 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1459 if (data_index == max_size) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1460 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1461 max_size *= 2; \ |
3410 | 1462 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1463 if (all_char_conv) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1464 { \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1465 if (one_elt_size_spec) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1466 mval.resize (1, max_size, 0.0); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1467 else if (nr > 0) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1468 mval.resize (nr, max_size / nr, 0.0); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1469 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1470 panic_impossible (); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1471 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1472 else if (nr > 0) \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1473 mval.resize (nr, max_size / nr, 0.0); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1474 else \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1475 mval.resize (max_size, 1, 0.0); \ |
3410 | 1476 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1477 data = mval.fortran_vec (); \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1478 } \ |
3410 | 1479 \ |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1480 data[data_index++] = tmp[i++]; \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1481 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1482 } \ |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1483 } \ |
3410 | 1484 } \ |
1485 while (0) | |
2117 | 1486 |
1487 octave_value | |
1488 octave_base_stream::do_scanf (scanf_format_list& fmt_list, | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1489 octave_idx_type nr, octave_idx_type nc, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1490 bool one_elt_size_spec, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1491 octave_idx_type& conversion_count, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1492 const std::string& who) |
2117 | 1493 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1494 if (interactive && file_number () == 0) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1495 ::error ("%s: unable to read from stdin while running interactively", |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1496 who.c_str ()); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1497 |
8773
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1498 octave_value retval = Matrix (); |
9e3111d203c0
disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents:
8739
diff
changeset
|
1499 |
3268 | 1500 conversion_count = 0; |
1501 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1502 octave_idx_type nconv = fmt_list.num_conversions (); |
3640 | 1503 |
5275 | 1504 octave_idx_type data_index = 0; |
2121 | 1505 |
3268 | 1506 if (nr == 0 || nc == 0) |
1507 { | |
1508 if (one_elt_size_spec) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1509 nc = 0; |
3268 | 1510 |
1511 return Matrix (nr, nc, 0.0); | |
1512 } | |
1513 | |
3523 | 1514 std::istream *isp = input_stream (); |
2117 | 1515 |
1516 bool all_char_conv = fmt_list.all_character_conversions (); | |
1517 | |
1518 Matrix mval; | |
1519 double *data = 0; | |
5275 | 1520 octave_idx_type max_size = 0; |
1521 octave_idx_type max_conv = 0; | |
1522 | |
1523 octave_idx_type final_nr = 0; | |
1524 octave_idx_type final_nc = 0; | |
2117 | 1525 |
3268 | 1526 if (all_char_conv) |
1527 { | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1528 // Any of these could be resized later (if we have %s conversions, |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1529 // we may read more than one element for each conversion). |
3268 | 1530 if (one_elt_size_spec) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1531 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1532 max_size = 512; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1533 mval.resize (1, max_size, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1534 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1535 if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1536 max_conv = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1537 } |
4420 | 1538 else if (nr > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1539 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1540 if (nc > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1541 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1542 mval.resize (nr, nc, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1543 max_size = max_conv = nr * nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1544 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1545 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1546 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1547 mval.resize (nr, 32, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1548 max_size = nr * 32; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1549 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1550 } |
4420 | 1551 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1552 panic_impossible (); |
3268 | 1553 } |
1554 else if (nr > 0) | |
2117 | 1555 { |
1556 if (nc > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1557 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1558 // Will not resize later. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1559 mval.resize (nr, nc, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1560 max_size = nr * nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1561 max_conv = max_size; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1562 } |
2117 | 1563 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1564 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1565 // Maybe resize later. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1566 mval.resize (nr, 32, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1567 max_size = nr * 32; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1568 } |
2117 | 1569 } |
1570 else | |
1571 { | |
4420 | 1572 // Maybe resize later. |
2117 | 1573 mval.resize (32, 1, 0.0); |
1574 max_size = 32; | |
1575 } | |
1576 | |
4420 | 1577 data = mval.fortran_vec (); |
1578 | |
2117 | 1579 if (isp) |
1580 { | |
3523 | 1581 std::istream& is = *isp; |
2117 | 1582 |
1583 const scanf_format_elt *elt = fmt_list.first (); | |
1584 | |
3538 | 1585 std::ios::fmtflags flags = is.flags (); |
2213 | 1586 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1587 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
|
1588 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1589 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
|
1590 |
2117 | 1591 for (;;) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1592 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1593 octave_quit (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1594 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1595 if (elt) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1596 { |
20465
44eb1102f8a8
don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents:
20432
diff
changeset
|
1597 if (elt->type == scanf_format_elt::null |
44eb1102f8a8
don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents:
20432
diff
changeset
|
1598 || (! (elt->type == scanf_format_elt::whitespace_conversion |
44eb1102f8a8
don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents:
20432
diff
changeset
|
1599 || elt->type == scanf_format_elt::literal_conversion |
44eb1102f8a8
don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents:
20432
diff
changeset
|
1600 || elt->type == '%') |
44eb1102f8a8
don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents:
20432
diff
changeset
|
1601 && max_conv > 0 && conversion_count == max_conv)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1602 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1603 // We are done, either because we have reached the end of the |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1604 // format string and are not cycling through the format again |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1605 // or because we've converted all the values that have been |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1606 // requested and the next format element is a conversion. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1607 // Determine final array size and exit. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1608 if (all_char_conv && one_elt_size_spec) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1609 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1610 final_nr = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1611 final_nc = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1612 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1613 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1614 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1615 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1616 final_nc = (data_index - 1) / nr + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1617 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1618 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1619 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1620 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1621 else if (data_index == max_size) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1622 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1623 max_size *= 2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1624 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1625 if (all_char_conv) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1626 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1627 if (one_elt_size_spec) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1628 mval.resize (1, max_size, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1629 else if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1630 mval.resize (nr, max_size / nr, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1631 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1632 panic_impossible (); |
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 else if (nr > 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1635 mval.resize (nr, max_size / nr, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1636 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1637 mval.resize (max_size, 1, 0.0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1638 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1639 data = mval.fortran_vec (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1640 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1641 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1642 const char *fmt = elt->text; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1643 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1644 bool discard = elt->discard; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1645 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1646 switch (elt->type) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1647 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1648 case scanf_format_elt::whitespace_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1649 DO_WHITESPACE_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1650 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1651 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1652 case scanf_format_elt::literal_conversion: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1653 DO_LITERAL_CONVERSION (); |
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 case '%': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1657 DO_PCT_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1658 break; |
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 case 'd': case 'i': |
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 switch (elt->modifier) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1663 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1664 case 'h': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1665 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1666 short int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1667 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1668 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1669 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1670 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1671 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1672 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1673 case 'l': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1674 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1675 long int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1676 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1677 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1678 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1679 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1680 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1681 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1682 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1683 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1684 int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1685 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1686 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1687 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1688 } |
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 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1692 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1693 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1694 case 'o': case 'u': case 'x': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1695 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1696 switch (elt->modifier) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1697 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1698 case 'h': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1699 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1700 unsigned short int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1701 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1702 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1703 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1704 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1705 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1706 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1707 case 'l': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1708 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1709 unsigned long int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1710 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1711 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1712 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1713 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1714 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1715 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1716 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1717 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1718 unsigned int tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1719 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1720 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1721 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1722 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1723 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1724 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1725 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1726 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1727 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1728 case 'e': case 'f': case 'g': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1729 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1730 double tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1731 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1732 do_scanf_conv (is, *elt, &tmp, mval, data, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1733 data_index, conversion_count, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1734 nr, max_size, discard); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1735 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1736 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1737 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1738 case 'c': |
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 BEGIN_C_CONVERSION (); |
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 FINISH_CHARACTER_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1743 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1744 is.setf (flags); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1745 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1746 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1747 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1748 case 's': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1749 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1750 BEGIN_S_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1751 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1752 FINISH_CHARACTER_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1753 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1754 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1755 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1756 case '[': case '^': |
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 BEGIN_CHAR_CLASS_CONVERSION (); |
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 FINISH_CHARACTER_CONVERSION (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1761 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1762 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1763 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1764 case 'p': |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1765 error ("%s: unsupported format specifier", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1766 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1767 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1768 default: |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1769 error ("%s: internal format error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1770 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1771 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1772 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1773 if (! ok ()) |
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 break; |
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 else if (! is) |
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 if (all_char_conv) |
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 if (one_elt_size_spec) |
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 final_nr = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1784 final_nc = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1785 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1786 else if (data_index > nr) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1787 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1788 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1789 final_nc = (data_index - 1) / nr + 1; |
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 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1792 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1793 final_nr = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1794 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1795 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1796 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1797 else if (nr > 0) |
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 if (data_index > nr) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1800 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1801 final_nr = nr; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1802 final_nc = (data_index - 1) / nr + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1803 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1804 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1805 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1806 final_nr = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1807 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1808 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1809 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1810 else |
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 final_nr = data_index; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1813 final_nc = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1814 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1815 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1816 // 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
|
1817 // reset the failbit in the stream state. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1818 if (is.rdstate () & std::ios::failbit) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1819 is.clear (is.rdstate () & (~std::ios::failbit)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1820 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1821 // FIXME: is this the right thing to do? |
19849
1c9ed5b4c73d
input.h: change meaning of interactive and forced_interactive global variables.
Carnë Draug <carandraug@octave.org>
parents:
19739
diff
changeset
|
1822 if (interactive && ! forced_interactive && name () == "stdin") |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1823 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1824 is.clear (); |
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 // Skip to end of line. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1827 bool err; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1828 do_gets (-1, err, false, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1829 } |
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 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1832 } |
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 else |
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 error ("%s: internal format error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1837 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1838 } |
2117 | 1839 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1840 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
|
1841 { |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1842 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
|
1843 { |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1844 final_nr = 1; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1845 final_nc = data_index; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1846 } |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1847 else |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1848 { |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1849 final_nr = nr; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1850 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
|
1851 } |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1852 |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1853 break; |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1854 } |
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
1855 else |
20465
44eb1102f8a8
don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents:
20432
diff
changeset
|
1856 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1857 // Cycle through the format list more than once if we have some |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1858 // conversions to make and we haven't reached the limit on the |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1859 // number of values to convert (possibly because there is no |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1860 // specified limit). |
20465
44eb1102f8a8
don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents:
20432
diff
changeset
|
1861 elt = fmt_list.next (nconv > 0 |
44eb1102f8a8
don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents:
20432
diff
changeset
|
1862 && (max_conv == 0 |
44eb1102f8a8
don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents:
20432
diff
changeset
|
1863 || conversion_count < max_conv)); |
44eb1102f8a8
don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents:
20432
diff
changeset
|
1864 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1865 } |
2117 | 1866 } |
1867 | |
1868 if (ok ()) | |
1869 { | |
2121 | 1870 mval.resize (final_nr, final_nc, 0.0); |
2117 | 1871 |
3268 | 1872 retval = mval; |
1873 | |
2117 | 1874 if (all_char_conv) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1875 retval = retval.convert_to_str (false, true); |
2117 | 1876 } |
1877 | |
1878 return retval; | |
1879 } | |
1880 | |
1881 octave_value | |
3810 | 1882 octave_base_stream::scanf (const std::string& fmt, const Array<double>& size, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1883 octave_idx_type& conversion_count, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1884 const std::string& who) |
2117 | 1885 { |
1886 octave_value retval = Matrix (); | |
1887 | |
3559 | 1888 conversion_count = 0; |
2117 | 1889 |
3523 | 1890 std::istream *isp = input_stream (); |
2117 | 1891 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1892 if (! isp) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1893 invalid_operation (who, "reading"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1894 else |
2117 | 1895 { |
1896 scanf_format_list fmt_list (fmt); | |
1897 | |
3640 | 1898 if (fmt_list.num_conversions () == -1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1899 ::error ("%s: invalid format specified", who.c_str ()); |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1900 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1901 octave_idx_type nr = -1; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1902 octave_idx_type nc = -1; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1903 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1904 bool one_elt_size_spec; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1905 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1906 get_size (size, nr, nc, one_elt_size_spec, who); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1907 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1908 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec, |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1909 conversion_count, who); |
2215 | 1910 } |
2572 | 1911 |
1912 return retval; | |
1913 } | |
1914 | |
2712 | 1915 bool |
1916 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
|
1917 octave_value& retval, const std::string& who) |
2572 | 1918 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1919 std::istream *isp = input_stream (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1920 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1921 if (! isp) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1922 return false; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1923 |
2712 | 1924 bool quit = false; |
2215 | 1925 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1926 std::istream& is = *isp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1927 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1928 std::ios::fmtflags flags = is.flags (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1929 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1930 if (elt) |
2215 | 1931 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1932 const char *fmt = elt->text; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1933 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1934 bool discard = elt->discard; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1935 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1936 switch (elt->type) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1937 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1938 case scanf_format_elt::whitespace_conversion: |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1939 DO_WHITESPACE_CONVERSION (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1940 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1941 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1942 case scanf_format_elt::literal_conversion: |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1943 DO_LITERAL_CONVERSION (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1944 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1945 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1946 case '%': |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1947 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1948 DO_PCT_CONVERSION (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1949 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1950 if (! is) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1951 quit = true; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1952 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1953 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1954 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1955 case 'd': case 'i': |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1956 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1957 int tmp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1958 |
21147
95feb42d7a97
* oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents:
21139
diff
changeset
|
1959 if (octave_scan (is, *elt, &tmp)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1960 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1961 if (! discard) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1962 retval = tmp; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1963 } |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1964 else |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1965 quit = true; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1966 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1967 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1968 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1969 case 'o': case 'u': case 'x': |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1970 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1971 long int tmp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1972 |
21147
95feb42d7a97
* oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents:
21139
diff
changeset
|
1973 if (octave_scan (is, *elt, &tmp)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1974 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1975 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1976 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1977 } |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1978 else |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1979 quit = true; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1980 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1981 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1982 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1983 case 'e': case 'f': case 'g': |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1984 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1985 double tmp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1986 |
21147
95feb42d7a97
* oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents:
21139
diff
changeset
|
1987 if (octave_scan (is, *elt, &tmp)) |
10315
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 if (! discard) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1990 retval = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
1991 } |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1992 else |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1993 quit = true; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1994 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1995 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1996 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1997 case 'c': |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1998 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
1999 BEGIN_C_CONVERSION (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2000 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2001 if (! discard) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2002 retval = tmp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2003 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2004 if (! is) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2005 quit = true; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2006 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2007 is.setf (flags); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2008 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2009 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2010 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2011 case 's': |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2012 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2013 BEGIN_S_CONVERSION (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2014 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2015 if (! discard) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2016 retval = tmp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2017 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2018 if (! is) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2019 quit = true; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2020 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2021 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2022 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2023 case '[': |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2024 case '^': |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2025 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2026 BEGIN_CHAR_CLASS_CONVERSION (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2027 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2028 if (! discard) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2029 retval = tmp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2030 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2031 if (! is) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2032 quit = true; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2033 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2034 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2035 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2036 case 'p': |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2037 error ("%s: unsupported format specifier", who.c_str ()); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2038 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2039 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2040 default: |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2041 error ("%s: internal format error", who.c_str ()); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2042 break; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2043 } |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2044 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2045 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2046 if (ok () && is.fail ()) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2047 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2048 error ("%s: read error", who.c_str ()); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2049 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2050 // FIXME: is this the right thing to do? |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2051 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2052 if (interactive && ! forced_interactive && name () == "stdin") |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2053 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2054 // Skip to end of line. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2055 bool err; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2056 do_gets (-1, err, false, who); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2057 } |
2215 | 2058 } |
2059 | |
2712 | 2060 return quit; |
2215 | 2061 } |
2062 | |
2063 octave_value_list | |
4468 | 2064 octave_base_stream::oscanf (const std::string& fmt, const std::string& who) |
2215 | 2065 { |
2066 octave_value_list retval; | |
2067 | |
3523 | 2068 std::istream *isp = input_stream (); |
2215 | 2069 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2070 if (! isp) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2071 invalid_operation (who, "reading"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2072 else |
2215 | 2073 { |
3523 | 2074 std::istream& is = *isp; |
2215 | 2075 |
2076 scanf_format_list fmt_list (fmt); | |
2077 | |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
2078 octave_idx_type nconv = fmt_list.num_conversions (); |
2215 | 2079 |
3640 | 2080 if (nconv == -1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2081 ::error ("%s: invalid format specified", who.c_str ()); |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2082 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2083 is.clear (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2084 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2085 octave_idx_type len = fmt_list.length (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2086 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2087 retval.resize (nconv+2, Matrix ()); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2088 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2089 const scanf_format_elt *elt = fmt_list.first (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2090 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2091 int num_values = 0; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2092 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2093 bool quit = false; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2094 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2095 for (octave_idx_type i = 0; i < len; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2096 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2097 octave_value tmp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2098 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2099 quit = do_oscanf (elt, tmp, who); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2100 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2101 if (quit) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2102 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2103 else |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2104 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2105 if (tmp.is_defined ()) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2106 retval(num_values++) = tmp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2107 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2108 if (! ok ()) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2109 break; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2110 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2111 elt = fmt_list.next (nconv > 0); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2112 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2113 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2114 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2115 retval(nconv) = num_values; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2116 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2117 int err_num; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2118 retval(nconv+1) = error (false, err_num); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2119 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2120 if (! quit) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2121 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2122 // Pick up any trailing stuff. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2123 if (ok () && len > nconv) |
10315
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 octave_value tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2126 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2127 elt = fmt_list.next (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2128 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2129 do_oscanf (elt, tmp, who); |
10315
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 } |
2117 | 2132 } |
2133 | |
2134 return retval; | |
2135 } | |
2136 | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2137 // Functions that are defined for all output streams |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2138 // (output streams are those that define os). |
2117 | 2139 |
2140 int | |
2141 octave_base_stream::flush (void) | |
2142 { | |
2143 int retval = -1; | |
2144 | |
3523 | 2145 std::ostream *os = output_stream (); |
2117 | 2146 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2147 if (! os) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2148 invalid_operation ("fflush", "writing"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2149 else |
2117 | 2150 { |
2151 os->flush (); | |
2152 | |
2153 if (os->good ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2154 retval = 0; |
2117 | 2155 } |
2156 | |
2157 return retval; | |
2158 } | |
2159 | |
2160 class | |
2161 printf_value_cache | |
2162 { | |
2163 public: | |
2164 | |
3653 | 2165 enum state { ok, conversion_error }; |
2117 | 2166 |
7352 | 2167 printf_value_cache (const octave_value_list& args, const std::string& who) |
2117 | 2168 : values (args), val_idx (0), elt_idx (0), |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2169 n_vals (values.length ()), n_elts (0), have_data (false), |
7352 | 2170 curr_state (ok) |
2171 { | |
2172 for (octave_idx_type i = 0; i < values.length (); i++) | |
2173 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2174 octave_value val = values(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2175 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2176 if (val.is_map () || val.is_cell () || val.is_object ()) |
21129
228b65504557
maint: Eliminate useless statements after err_XXX.
Rik <rik@octave.org>
parents:
21114
diff
changeset
|
2177 err_wrong_type_arg (who, val); |
7352 | 2178 } |
2179 } | |
2117 | 2180 |
2181 ~printf_value_cache (void) { } | |
2182 | |
2183 // Get the current value as a double and advance the internal pointer. | |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2184 octave_value get_next_value (char type = 0); |
2117 | 2185 |
2186 // Get the current value as an int and advance the internal pointer. | |
2187 int int_value (void); | |
2188 | |
3145 | 2189 operator bool () const { return (curr_state == ok); } |
2117 | 2190 |
3653 | 2191 bool exhausted (void) { return (val_idx >= n_vals); } |
2117 | 2192 |
2193 private: | |
2194 | |
2195 const octave_value_list values; | |
2196 int val_idx; | |
2197 int elt_idx; | |
2198 int n_vals; | |
2199 int n_elts; | |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2200 bool have_data; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2201 octave_value curr_val; |
2117 | 2202 state curr_state; |
2203 | |
2204 // Must create value cache with values! | |
2205 | |
2206 printf_value_cache (void); | |
2207 | |
2208 // No copying! | |
2209 | |
2210 printf_value_cache (const printf_value_cache&); | |
2211 | |
2212 printf_value_cache& operator = (const printf_value_cache&); | |
2213 }; | |
2214 | |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2215 octave_value |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2216 printf_value_cache::get_next_value (char type) |
2117 | 2217 { |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2218 octave_value retval; |
2117 | 2219 |
3711 | 2220 if (exhausted ()) |
2221 curr_state = conversion_error; | |
2222 | |
2223 while (! exhausted ()) | |
2117 | 2224 { |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2225 if (! have_data) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2226 { |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2227 curr_val = values (val_idx); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2228 |
20741
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
2229 elt_idx = 0; |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
2230 n_elts = curr_val.numel (); |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
2231 have_data = true; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2232 } |
2117 | 2233 |
2234 if (elt_idx < n_elts) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2235 { |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2236 if (type == 's') |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2237 { |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2238 if (curr_val.is_string ()) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2239 { |
21296
596e19e7e571
fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents:
21235
diff
changeset
|
2240 dim_vector dv (1, curr_val.numel ()); |
596e19e7e571
fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents:
21235
diff
changeset
|
2241 octave_value tmp = curr_val.reshape (dv); |
596e19e7e571
fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents:
21235
diff
changeset
|
2242 |
596e19e7e571
fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents:
21235
diff
changeset
|
2243 std::string sval = tmp.string_value (); |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2244 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2245 retval = sval.substr (elt_idx); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2246 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2247 // We've consumed the rest of the value. |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2248 elt_idx = n_elts; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2249 } |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2250 else |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2251 { |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2252 // Convert to character string while values are |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2253 // integers in the range [0 : char max] |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2254 const NDArray val = curr_val.array_value (); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2255 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2256 octave_idx_type idx = elt_idx; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2257 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2258 for (; idx < n_elts; idx++) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2259 { |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2260 double dval = val(idx); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2261 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2262 if (D_NINT (dval) != dval || dval < 0 || dval > 255) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2263 break; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2264 } |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2265 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2266 octave_idx_type n = idx - elt_idx; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2267 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2268 if (n > 0) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2269 { |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2270 std::string sval (n, '\0'); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2271 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2272 for (octave_idx_type i = 0; i < n; i++) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2273 sval[i] = val(elt_idx++); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2274 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2275 retval = sval; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2276 } |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2277 else |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2278 retval = curr_val.fast_elem_extract (elt_idx++); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2279 } |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2280 } |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2281 else |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2282 { |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2283 retval = curr_val.fast_elem_extract (elt_idx++); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2284 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2285 if (type == 'c' && ! retval.is_string ()) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2286 { |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2287 double dval = retval.double_value (); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2288 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2289 if (D_NINT (dval) == dval && dval >= 0 && dval < 256) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2290 retval = static_cast<char> (dval); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2291 } |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2292 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2293 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2294 if (elt_idx >= n_elts) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2295 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2296 elt_idx = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2297 val_idx++; |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2298 have_data = false; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2299 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2300 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2301 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2302 } |
2117 | 2303 else |
10315
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 val_idx++; |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2306 have_data = false; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2307 |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2308 if (n_elts == 0) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2309 { |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2310 if (elt_idx == 0 && (type == 's' || type == 'c')) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2311 { |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2312 retval = ""; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2313 break; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2314 } |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2315 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2316 if (exhausted ()) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2317 curr_state = conversion_error; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2318 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2319 } |
2117 | 2320 } |
2321 | |
2322 return retval; | |
2323 } | |
2324 | |
2325 int | |
2326 printf_value_cache::int_value (void) | |
2327 { | |
2328 int retval = 0; | |
2329 | |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2330 octave_value val = get_next_value (); |
2117 | 2331 |
20558
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
2332 double dval = val.double_value (true); |
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
2333 |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
2334 if (D_NINT (dval) == dval) |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
2335 retval = NINT (dval); |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
2336 else |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
2337 curr_state = conversion_error; |
2117 | 2338 |
2339 return retval; | |
2340 } | |
2341 | |
2342 // Ugh again and again. | |
2343 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
2344 template <typename T> |
3620 | 2345 int |
3523 | 2346 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
|
2347 int sa_2, T arg, const std::string& who) |
2572 | 2348 { |
3620 | 2349 int retval = 0; |
2350 | |
2572 | 2351 switch (nsa) |
2352 { | |
2353 case 2: | |
3640 | 2354 retval = octave_format (os, fmt, sa_1, sa_2, arg); |
2572 | 2355 break; |
2356 | |
2357 case 1: | |
3640 | 2358 retval = octave_format (os, fmt, sa_1, arg); |
2572 | 2359 break; |
2360 | |
2361 case 0: | |
3640 | 2362 retval = octave_format (os, fmt, arg); |
2572 | 2363 break; |
2364 | |
2365 default: | |
4468 | 2366 ::error ("%s: internal error handling format", who.c_str ()); |
2572 | 2367 break; |
2368 } | |
3620 | 2369 |
2370 return retval; | |
2572 | 2371 } |
2372 | |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2373 static size_t |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2374 do_printf_string (std::ostream& os, const printf_format_elt *elt, |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2375 int nsa, int sa_1, int sa_2, const std::string& arg, |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2376 const std::string& who) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2377 { |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2378 if (nsa > 2) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20768
diff
changeset
|
2379 ::error ("%s: internal error handling format", who.c_str ()); |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2380 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2381 std::string flags = elt->flags; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2382 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2383 bool left = flags.find ('-') != std::string::npos; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2384 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2385 size_t len = arg.length (); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2386 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2387 size_t fw = nsa > 0 ? sa_1 : (elt->fw == -1 ? len : elt->fw); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2388 size_t prec = nsa > 1 ? sa_2 : (elt->prec == -1 ? len : elt->prec); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2389 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2390 os << std::setw (fw) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2391 << (left ? std::left : std::right) |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2392 << (prec < len ? arg.substr (0, prec) : arg); |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2393 |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2394 return len > fw ? len : fw; |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2395 } |
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2396 |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2397 static bool |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2398 is_nan_or_inf (const octave_value& val) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2399 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2400 octave_value ov_isnan = val.isnan (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2401 octave_value ov_isinf = val.isinf (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2402 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2403 return (ov_isnan.is_true () || ov_isinf.is_true ()); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2404 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2405 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2406 static bool |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2407 ok_for_signed_int_conv (const octave_value& val) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2408 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2409 uint64_t limit = std::numeric_limits<int64_t>::max (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2410 |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2411 if (val.is_string ()) |
20252
7fa170cc14fe
Return correct hex value for printf when used with string inputs (bug #45263).
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
2412 return true; |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2413 else if (val.is_integer_type ()) |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2414 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2415 if (val.is_uint64_type ()) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2416 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2417 octave_uint64 ival = val.uint64_scalar_value (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2418 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2419 if (ival.value () <= limit) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2420 return true; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2421 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2422 else |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2423 return true; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2424 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2425 else |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2426 { |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2427 double dval = val.double_value (true); |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2428 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2429 if (dval == xround (dval) && dval <= limit) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2430 return true; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2431 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2432 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2433 return false; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2434 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2435 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2436 static bool |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2437 ok_for_unsigned_int_conv (const octave_value& val) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2438 { |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2439 if (val.is_string ()) |
20252
7fa170cc14fe
Return correct hex value for printf when used with string inputs (bug #45263).
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
2440 return true; |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2441 else if (val.is_integer_type ()) |
18653
7d0014bb9e4e
also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents:
18650
diff
changeset
|
2442 { |
7d0014bb9e4e
also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents:
18650
diff
changeset
|
2443 // Easier than dispatching here... |
7d0014bb9e4e
also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents:
18650
diff
changeset
|
2444 |
7d0014bb9e4e
also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents:
18650
diff
changeset
|
2445 octave_value ov_is_ge_zero |
7d0014bb9e4e
also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents:
18650
diff
changeset
|
2446 = do_binary_op (octave_value::op_ge, val, octave_value (0.0)); |
7d0014bb9e4e
also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents:
18650
diff
changeset
|
2447 |
7d0014bb9e4e
also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents:
18650
diff
changeset
|
2448 return ov_is_ge_zero.is_true (); |
7d0014bb9e4e
also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents:
18650
diff
changeset
|
2449 } |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2450 else |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2451 { |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2452 double dval = val.double_value (true); |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2453 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2454 uint64_t limit = std::numeric_limits<uint64_t>::max (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2455 |
18653
7d0014bb9e4e
also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents:
18650
diff
changeset
|
2456 if (dval == xround (dval) && dval >= 0 && dval <= limit) |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2457 return true; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2458 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2459 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2460 return false; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2461 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2462 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2463 static std::string |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2464 switch_to_g_format (const printf_format_elt *elt) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2465 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2466 std::string tfmt = elt->text; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2467 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2468 tfmt.replace (tfmt.rfind (elt->type), 1, "g"); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2469 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2470 return tfmt; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2471 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2472 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2473 int |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2474 octave_base_stream::do_numeric_printf_conv (std::ostream& os, |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2475 const printf_format_elt *elt, |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2476 int nsa, int sa_1, int sa_2, |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2477 const octave_value& val, |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2478 const std::string& who) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2479 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2480 int retval = 0; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2481 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2482 const char *fmt = elt->text; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2483 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2484 if (is_nan_or_inf (val)) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2485 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2486 double dval = val.double_value (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2487 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2488 std::string tfmt = fmt; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2489 std::string::size_type i1, i2; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2490 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2491 tfmt.replace ((i1 = tfmt.rfind (elt->type)), 1, 1, 's'); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2492 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2493 if ((i2 = tfmt.rfind ('.')) != std::string::npos && i2 < i1) |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2494 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2495 tfmt.erase (i2, i1-i2); |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2496 if (elt->prec == -2) |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2497 nsa--; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2498 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2499 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2500 const char *tval; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2501 if (lo_ieee_isinf (dval)) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2502 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2503 if (elt->flags.find ('+') != std::string::npos) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2504 tval = (dval < 0 ? "-Inf" : "+Inf"); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2505 else |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2506 tval = (dval < 0 ? "-Inf" : "Inf"); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2507 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2508 else |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2509 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2510 if (elt->flags.find ('+') != std::string::npos) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2511 tval = (lo_ieee_is_NA (dval) ? "+NA" : "+NaN"); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2512 else |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2513 tval = (lo_ieee_is_NA (dval) ? "NA" : "NaN"); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2514 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2515 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2516 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2, tval, who); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2517 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2518 else |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2519 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2520 static std::string llmod |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2521 = sizeof (long) == sizeof (int64_t) ? "l" : "ll"; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2522 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2523 char type = elt->type; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2524 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2525 switch (type) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2526 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2527 case 'd': case 'i': case 'c': |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2528 if (ok_for_signed_int_conv (val)) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2529 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2530 octave_int64 tval = val.int64_scalar_value (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2531 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2532 // Insert "long" modifier. |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2533 std::string tfmt = fmt; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2534 tfmt.replace (tfmt.rfind (type), 1, llmod + type); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2535 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2536 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2, |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2537 tval.value (), who); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2538 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2539 else |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2540 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2541 std::string tfmt = switch_to_g_format (elt); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2542 |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2543 double dval = val.double_value (true); |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2544 |
20558
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
2545 retval += do_printf_conv (os, tfmt.c_str (), nsa, |
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
2546 sa_1, sa_2, dval, who); |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2547 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2548 break; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2549 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2550 case 'o': case 'x': case 'X': case 'u': |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2551 if (ok_for_unsigned_int_conv (val)) |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2552 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2553 octave_uint64 tval = val.uint64_scalar_value (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2554 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2555 // Insert "long" modifier. |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2556 std::string tfmt = fmt; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2557 tfmt.replace (tfmt.rfind (type), 1, llmod + type); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2558 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2559 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2, |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2560 tval.value (), who); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2561 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2562 else |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2563 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2564 std::string tfmt = switch_to_g_format (elt); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2565 |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2566 double dval = val.double_value (true); |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2567 |
20558
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
2568 retval += do_printf_conv (os, tfmt.c_str (), nsa, |
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
2569 sa_1, sa_2, dval, who); |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2570 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2571 break; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2572 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2573 case 'f': case 'e': case 'E': |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2574 case 'g': case 'G': |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2575 { |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2576 double dval = val.double_value (true); |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2577 |
20558
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
2578 retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, dval, who); |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2579 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2580 break; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2581 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2582 default: |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
2583 // Note: error is member fcn from octave_base_stream, not ::error. |
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
2584 // This error does not halt execution so "return ..." must exist. |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20768
diff
changeset
|
2585 error ("%s: invalid format specifier", who.c_str ()); |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2586 return -1; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2587 break; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2588 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2589 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2590 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2591 return retval; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2592 } |
6492 | 2593 |
2117 | 2594 int |
2595 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
|
2596 const octave_value_list& args, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2597 const std::string& who) |
2117 | 2598 { |
3620 | 2599 int retval = 0; |
2117 | 2600 |
10187
a44d15813a39
don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
2601 octave_idx_type nconv = fmt_list.num_conversions (); |
3640 | 2602 |
3523 | 2603 std::ostream *osp = output_stream (); |
2117 | 2604 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2605 if (! osp) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2606 invalid_operation (who, "writing"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2607 else |
2117 | 2608 { |
3523 | 2609 std::ostream& os = *osp; |
2117 | 2610 |
2611 const printf_format_elt *elt = fmt_list.first (); | |
2612 | |
7352 | 2613 printf_value_cache val_cache (args, who); |
2614 | |
2117 | 2615 for (;;) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2616 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2617 octave_quit (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2618 |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2619 if (! elt) |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2620 ::error ("%s: internal error handling format", who.c_str ()); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2621 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2622 // NSA is the number of 'star' args to convert. |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2623 int nsa = (elt->fw == -2) + (elt->prec == -2); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2624 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2625 int sa_1 = 0; |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2626 int sa_2 = 0; |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2627 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2628 if (nsa > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2629 { |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2630 sa_1 = val_cache.int_value (); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2631 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2632 if (! val_cache) |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2633 break; |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2634 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2635 { |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2636 if (nsa > 1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2637 { |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2638 sa_2 = val_cache.int_value (); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2639 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2640 if (! val_cache) |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2641 break; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2642 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2643 } |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2644 } |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2645 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2646 if (elt->type == '%') |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2647 { |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2648 os << "%"; |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2649 retval++; |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2650 } |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2651 else if (elt->args == 0 && elt->text) |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2652 { |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2653 os << elt->text; |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2654 retval += strlen (elt->text); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2655 } |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2656 else if (elt->type == 's' || elt->type == 'c') |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2657 { |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2658 octave_value val = val_cache.get_next_value (elt->type); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2659 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2660 if (val_cache) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2661 { |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2662 if (val.is_string ()) |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2663 { |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2664 std::string sval = val.string_value (); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2665 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2666 retval += do_printf_string (os, elt, nsa, sa_1, |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2667 sa_2, sval, who); |
19729
17a7e9f26e50
improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
2668 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2669 else |
18650
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2670 retval += do_numeric_printf_conv (os, elt, nsa, sa_1, |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18620
diff
changeset
|
2671 sa_2, val, who); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2672 } |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2673 else |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2674 break; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2675 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2676 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2677 { |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2678 octave_value val = val_cache.get_next_value (); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2679 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2680 if (val_cache) |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2681 retval += do_numeric_printf_conv (os, elt, nsa, sa_1, |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2682 sa_2, val, who); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2683 else |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2684 break; |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2685 } |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2686 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2687 if (! os) |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2688 { |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
2689 error ("%s: write error", who.c_str ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2690 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2691 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2692 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2693 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
|
2694 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2695 if (! elt || (val_cache.exhausted () && elt->args > 0)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2696 break; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
2697 } |
2117 | 2698 } |
2699 | |
2700 return retval; | |
2701 } | |
2702 | |
2703 int | |
3640 | 2704 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
|
2705 const octave_value_list& args, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2706 const std::string& who) |
2117 | 2707 { |
3640 | 2708 printf_format_list fmt_list (fmt); |
2709 | |
2710 if (fmt_list.num_conversions () == -1) | |
4468 | 2711 ::error ("%s: invalid format specified", who.c_str ()); |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2712 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2713 return do_printf (fmt_list, args, who); |
2117 | 2714 } |
2715 | |
2716 int | |
4468 | 2717 octave_base_stream::puts (const std::string& s, const std::string& who) |
2117 | 2718 { |
2719 int retval = -1; | |
2720 | |
3523 | 2721 std::ostream *osp = output_stream (); |
2117 | 2722 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2723 if (! osp) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2724 invalid_operation (who, "writing"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2725 else |
2117 | 2726 { |
3523 | 2727 std::ostream& os = *osp; |
2117 | 2728 |
2729 os << s; | |
2730 | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2731 if (! os) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2732 error ("%s: write error", who.c_str ()); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2733 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2734 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2735 // FIXME: why does this seem to be necessary? |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2736 // Without it, output from a loop like |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2737 // |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2738 // 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
|
2739 // |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2740 // 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
|
2741 os.flush (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2742 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2743 if (os) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2744 retval = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2745 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2746 error ("%s: write error", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2747 } |
2117 | 2748 } |
2749 | |
2750 return retval; | |
2751 } | |
2752 | |
2753 // Return current error message for this stream. | |
2754 | |
3536 | 2755 std::string |
2435 | 2756 octave_base_stream::error (bool clear_err, int& err_num) |
2117 | 2757 { |
2435 | 2758 err_num = fail ? -1 : 0; |
2117 | 2759 |
3523 | 2760 std::string tmp = errmsg; |
2117 | 2761 |
2762 if (clear_err) | |
2763 clear (); | |
2764 | |
2765 return tmp; | |
2766 } | |
2767 | |
2768 void | |
4468 | 2769 octave_base_stream::invalid_operation (const std::string& who, const char *rw) |
2117 | 2770 { |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
2771 // Note: This calls the member fcn error, not ::error from error.h. |
6297 | 2772 error (who, std::string ("stream not open for ") + rw); |
2117 | 2773 } |
2774 | |
3552 | 2775 octave_stream::octave_stream (octave_base_stream *bs) |
3340 | 2776 : rep (bs) |
2777 { | |
2778 if (rep) | |
2779 rep->count = 1; | |
2780 } | |
2781 | |
2782 octave_stream::~octave_stream (void) | |
2783 { | |
2784 if (rep && --rep->count == 0) | |
2785 delete rep; | |
2786 } | |
2787 | |
2788 octave_stream::octave_stream (const octave_stream& s) | |
2789 : rep (s.rep) | |
2790 { | |
2791 if (rep) | |
2792 rep->count++; | |
2793 } | |
2794 | |
2795 octave_stream& | |
2796 octave_stream::operator = (const octave_stream& s) | |
2797 { | |
2798 if (rep != s.rep) | |
2799 { | |
2800 if (rep && --rep->count == 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2801 delete rep; |
3340 | 2802 |
2803 rep = s.rep; | |
2804 | |
2805 if (rep) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2806 rep->count++; |
3340 | 2807 } |
2808 | |
2809 return *this; | |
2810 } | |
2811 | |
2117 | 2812 int |
2813 octave_stream::flush (void) | |
2814 { | |
2815 int retval = -1; | |
2816 | |
5659 | 2817 if (stream_ok ()) |
2117 | 2818 retval = rep->flush (); |
2819 | |
2820 return retval; | |
2821 } | |
2822 | |
3536 | 2823 std::string |
5275 | 2824 octave_stream::getl (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 2825 { |
3523 | 2826 std::string retval; |
2117 | 2827 |
5659 | 2828 if (stream_ok ()) |
4468 | 2829 retval = rep->getl (max_len, err, who); |
2117 | 2830 |
2831 return retval; | |
2832 } | |
2833 | |
3536 | 2834 std::string |
4468 | 2835 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
|
2836 const std::string& who) |
2117 | 2837 { |
2838 err = false; | |
2839 | |
2840 int conv_err = 0; | |
2841 | |
6345 | 2842 int max_len = -1; |
2843 | |
2844 if (tc_max_len.is_defined ()) | |
2117 | 2845 { |
6345 | 2846 max_len = convert_to_valid_int (tc_max_len, conv_err); |
2847 | |
2848 if (conv_err || max_len < 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2849 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2850 err = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2851 ::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
|
2852 } |
2117 | 2853 } |
6345 | 2854 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2855 return getl (max_len, err, who); |
2117 | 2856 } |
2857 | |
3536 | 2858 std::string |
5275 | 2859 octave_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 2860 { |
3523 | 2861 std::string retval; |
2117 | 2862 |
5659 | 2863 if (stream_ok ()) |
4468 | 2864 retval = rep->gets (max_len, err, who); |
2117 | 2865 |
2866 return retval; | |
2867 } | |
2868 | |
3536 | 2869 std::string |
4468 | 2870 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
|
2871 const std::string& who) |
2117 | 2872 { |
2873 err = false; | |
2874 | |
2875 int conv_err = 0; | |
2876 | |
6345 | 2877 int max_len = -1; |
2878 | |
2879 if (tc_max_len.is_defined ()) | |
2117 | 2880 { |
6345 | 2881 max_len = convert_to_valid_int (tc_max_len, conv_err); |
2882 | |
2883 if (conv_err || max_len < 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2884 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2885 err = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2886 ::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
|
2887 } |
2117 | 2888 } |
6345 | 2889 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2890 return gets (max_len, err, who); |
2117 | 2891 } |
2892 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2893 off_t |
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2894 octave_stream::skipl (off_t count, bool& err, const std::string& who) |
9701 | 2895 { |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2896 off_t retval = -1; |
9701 | 2897 |
2898 if (stream_ok ()) | |
2899 retval = rep->skipl (count, err, who); | |
2900 | |
2901 return retval; | |
2902 } | |
2903 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2904 off_t |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2905 octave_stream::skipl (const octave_value& tc_count, bool& err, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
2906 const std::string& who) |
9701 | 2907 { |
2908 err = false; | |
2909 | |
2910 int conv_err = 0; | |
2911 | |
2912 int count = 1; | |
2913 | |
2914 if (tc_count.is_defined ()) | |
2915 { | |
2916 if (tc_count.is_scalar_type () && xisinf (tc_count.scalar_value ())) | |
2917 count = -1; | |
2918 else | |
2919 { | |
2920 count = convert_to_valid_int (tc_count, conv_err); | |
2921 | |
2922 if (conv_err || count < 0) | |
2923 { | |
2924 err = true; | |
2925 ::error ("%s: invalid number of lines specified", who.c_str ()); | |
2926 } | |
2927 } | |
2928 } | |
2929 | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2930 return skipl (count, err, who); |
9701 | 2931 } |
2932 | |
2117 | 2933 int |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2934 octave_stream::seek (off_t offset, int origin) |
2117 | 2935 { |
5065 | 2936 int status = -1; |
2117 | 2937 |
5659 | 2938 if (stream_ok ()) |
4889 | 2939 { |
2940 clearerr (); | |
2941 | |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2942 // Find current position so we can return to it if needed. |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2943 off_t orig_pos = rep->tell (); |
5065 | 2944 |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2945 // 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
|
2946 status = rep->seek (0, SEEK_END); |
5065 | 2947 |
2948 if (status == 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2949 { |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2950 off_t eof_pos = rep->tell (); |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2951 |
12943
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2952 if (origin == SEEK_CUR) |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2953 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2954 // Move back to original position, otherwise we will be seeking |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2955 // from the end of file which is probably not the original |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2956 // location. |
12943
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2957 rep->seek (orig_pos, SEEK_SET); |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2958 } |
8372d50de75a
improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents:
12936
diff
changeset
|
2959 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2960 // Attempt to move to desired position; may be outside bounds of |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2961 // existing file. |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2962 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
|
2963 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2964 if (status == 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2965 { |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2966 // Where are we after moving to desired position? |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
2967 off_t desired_pos = rep->tell (); |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2968 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2969 // I don't think save_pos can be less than zero, |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2970 // but we'll check anyway... |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2971 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
|
2972 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2973 // Seek outside bounds of file. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2974 // Failure should leave position unchanged. |
12957
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2975 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
|
2976 |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2977 status = -1; |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2978 } |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2979 } |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2980 else |
fb69561e5901
maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
2981 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2982 // Seeking to the desired position failed. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
2983 // Move back to original position and return failure status. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2984 rep->seek (orig_pos, SEEK_SET); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2985 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2986 status = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2987 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
2988 } |
4889 | 2989 } |
2117 | 2990 |
5065 | 2991 return status; |
2117 | 2992 } |
2993 | |
2994 int | |
2995 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
|
2996 const octave_value& tc_origin) |
2117 | 2997 { |
2998 int retval = -1; | |
2999 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3000 // FIXME: should we have octave_value methods that handle off_t explicitly? |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3001 octave_int64 val = tc_offset.xint64_scalar_value ("fseek: invalid value for offset"); |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
3002 off_t xoffset = val.value (); |
4645 | 3003 |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3004 int conv_err = 0; |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3005 |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3006 int origin = SEEK_SET; |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3007 |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3008 if (tc_origin.is_string ()) |
2117 | 3009 { |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3010 std::string xorigin = tc_origin.string_value ("fseek: invalid value for origin"); |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3011 |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3012 if (xorigin == "bof") |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3013 origin = SEEK_SET; |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3014 else if (xorigin == "cof") |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3015 origin = SEEK_CUR; |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3016 else if (xorigin == "eof") |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3017 origin = SEEK_END; |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3018 else |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3019 conv_err = -1; |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3020 } |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3021 else |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3022 { |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3023 int xorigin = convert_to_valid_int (tc_origin, conv_err); |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3024 |
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3025 if (! conv_err) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3026 { |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3027 if (xorigin == -1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3028 origin = SEEK_SET; |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3029 else if (xorigin == 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3030 origin = SEEK_CUR; |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3031 else if (xorigin == 1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3032 origin = SEEK_END; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3033 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3034 conv_err = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3035 } |
2117 | 3036 } |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20650
diff
changeset
|
3037 |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
3038 if (conv_err) |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
3039 ::error ("fseek: invalid value for origin"); |
2117 | 3040 |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
3041 retval = seek (xoffset, origin); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
3042 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
3043 if (retval != 0) |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
3044 // Note: error is member fcn from octave_stream, not ::error. |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
3045 error ("fseek: failed to seek to requested position"); |
2117 | 3046 |
3047 return retval; | |
3048 } | |
3049 | |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
3050 off_t |
4797 | 3051 octave_stream::tell (void) |
2117 | 3052 { |
16011
8122286c69a9
initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
3053 off_t retval = -1; |
2117 | 3054 |
5659 | 3055 if (stream_ok ()) |
2117 | 3056 retval = rep->tell (); |
3057 | |
3058 return retval; | |
3059 } | |
3060 | |
3061 int | |
3062 octave_stream::rewind (void) | |
3063 { | |
6296 | 3064 return seek (0, SEEK_SET); |
2117 | 3065 } |
3066 | |
3340 | 3067 bool |
3068 octave_stream::is_open (void) const | |
3069 { | |
3070 bool retval = false; | |
3071 | |
5659 | 3072 if (stream_ok ()) |
3340 | 3073 retval = rep->is_open (); |
3074 | |
3075 return retval; | |
3076 } | |
3077 | |
3078 void | |
3079 octave_stream::close (void) | |
3080 { | |
5659 | 3081 if (stream_ok ()) |
3340 | 3082 rep->close (); |
3083 } | |
3084 | |
21335
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3085 // FIXME: maybe these should be defined in lo-ieee.h? |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3086 |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3087 template <typename T> |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3088 static inline bool |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3089 is_old_NA (T) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3090 { |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3091 return false; |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3092 } |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3093 |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3094 template <> |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3095 inline bool |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3096 is_old_NA<double> (double val) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3097 { |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3098 return __lo_ieee_is_old_NA (val); |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3099 } |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3100 |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3101 template <typename T> |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3102 static inline T |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3103 replace_old_NA (T val) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3104 { |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3105 return val; |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3106 } |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3107 |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3108 template <> |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3109 inline double |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3110 replace_old_NA<double> (double val) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3111 { |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3112 return __lo_ieee_replace_old_NA (val); |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3113 } |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3114 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
3115 template <typename SRC_T, typename DST_T> |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3116 static octave_value |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3117 convert_and_copy (std::list<void *>& input_buf_list, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3118 octave_idx_type input_buf_elts, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3119 octave_idx_type elts_read, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3120 octave_idx_type nr, octave_idx_type nc, bool swap, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3121 bool do_float_fmt_conv, bool do_NA_conv, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3122 oct_mach_info::float_format from_flt_fmt) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3123 { |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3124 typedef typename DST_T::element_type dst_elt_type; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3125 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3126 DST_T conv (dim_vector (nr, nc)); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3127 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3128 dst_elt_type *conv_data = conv.fortran_vec (); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3129 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3130 octave_idx_type j = 0; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3131 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3132 for (std::list<void *>::const_iterator it = input_buf_list.begin (); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3133 it != input_buf_list.end (); it++) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3134 { |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3135 SRC_T *data = static_cast<SRC_T *> (*it); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3136 |
17463 | 3137 if (swap || do_float_fmt_conv) |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3138 { |
21335
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3139 if (do_NA_conv) |
17463 | 3140 { |
21335
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3141 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read; |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3142 i++, j++) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3143 { |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3144 if (swap) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3145 swap_bytes<sizeof (SRC_T)> (&data[i]); |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3146 else if (do_float_fmt_conv) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3147 do_float_format_conversion (&data[i], sizeof (SRC_T), |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3148 1, from_flt_fmt, |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3149 oct_mach_info::native_float_format ()); |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3150 |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3151 dst_elt_type tmp (data[i]); |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3152 |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3153 if (is_old_NA (tmp)) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3154 tmp = replace_old_NA (tmp); |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3155 |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3156 conv_data[j] = tmp; |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3157 } |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3158 } |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3159 else |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3160 { |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3161 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read; |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3162 i++, j++) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3163 { |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3164 if (swap) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3165 swap_bytes<sizeof (SRC_T)> (&data[i]); |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3166 else if (do_float_fmt_conv) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3167 do_float_format_conversion (&data[i], sizeof (SRC_T), |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3168 1, from_flt_fmt, |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3169 oct_mach_info::native_float_format ()); |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3170 |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3171 conv_data[j] = data[i]; |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3172 } |
17463 | 3173 } |
3174 } | |
3175 else | |
3176 { | |
3177 if (do_NA_conv) | |
3178 { | |
3179 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read; | |
3180 i++, j++) | |
3181 { | |
3182 dst_elt_type tmp (data[i]); | |
3183 | |
21335
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3184 if (is_old_NA (tmp)) |
e078d2208d86
avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
3185 tmp = replace_old_NA (tmp); |
17463 | 3186 |
3187 conv_data[j] = tmp; | |
3188 } | |
3189 } | |
3190 else | |
3191 { | |
3192 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read; | |
3193 i++, j++) | |
3194 conv_data[j] = data[i]; | |
3195 } | |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3196 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3197 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3198 delete [] data; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3199 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3200 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3201 input_buf_list.clear (); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3202 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3203 for (octave_idx_type i = elts_read; i < nr * nc; i++) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3204 conv_data[i] = dst_elt_type (0); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3205 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3206 return conv; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3207 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3208 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3209 typedef octave_value (*conv_fptr) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3210 (std::list<void *>& input_buf_list, octave_idx_type input_buf_elts, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3211 octave_idx_type elts_read, octave_idx_type nr, octave_idx_type nc, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3212 bool swap, bool do_float_fmt_conv, bool do_NA_conv, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3213 oct_mach_info::float_format from_flt_fmt); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3214 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3215 #define TABLE_ELT(T, U, V, W) \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3216 conv_fptr_table[oct_data_conv::T][oct_data_conv::U] = convert_and_copy<V, W> |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3217 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3218 #define FILL_TABLE_ROW(T, V) \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3219 TABLE_ELT (T, dt_int8, V, int8NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3220 TABLE_ELT (T, dt_uint8, V, uint8NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3221 TABLE_ELT (T, dt_int16, V, int16NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3222 TABLE_ELT (T, dt_uint16, V, uint16NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3223 TABLE_ELT (T, dt_int32, V, int32NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3224 TABLE_ELT (T, dt_uint32, V, uint32NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3225 TABLE_ELT (T, dt_int64, V, int64NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3226 TABLE_ELT (T, dt_uint64, V, uint64NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3227 TABLE_ELT (T, dt_single, V, FloatNDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3228 TABLE_ELT (T, dt_double, V, NDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3229 TABLE_ELT (T, dt_char, V, charNDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3230 TABLE_ELT (T, dt_schar, V, charNDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3231 TABLE_ELT (T, dt_uchar, V, charNDArray); \ |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3232 TABLE_ELT (T, dt_logical, V, boolNDArray); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3233 |
2117 | 3234 octave_value |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3235 octave_stream::finalize_read (std::list<void *>& input_buf_list, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3236 octave_idx_type input_buf_elts, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3237 octave_idx_type elts_read, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3238 octave_idx_type nr, octave_idx_type nc, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3239 oct_data_conv::data_type input_type, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3240 oct_data_conv::data_type output_type, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3241 oct_mach_info::float_format ffmt) |
2117 | 3242 { |
3243 octave_value retval; | |
3244 | |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3245 static bool initialized = false; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3246 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3247 // Table function pointers for return types x read types. |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3248 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3249 static conv_fptr conv_fptr_table[oct_data_conv::dt_unknown][14]; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3250 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3251 if (! initialized) |
4944 | 3252 { |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3253 for (int i = 0; i < oct_data_conv::dt_unknown; i++) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3254 for (int j = 0; j < 14; j++) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3255 conv_fptr_table[i][j] = 0; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3256 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3257 FILL_TABLE_ROW (dt_int8, int8_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3258 FILL_TABLE_ROW (dt_uint8, uint8_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3259 FILL_TABLE_ROW (dt_int16, int16_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3260 FILL_TABLE_ROW (dt_uint16, uint16_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3261 FILL_TABLE_ROW (dt_int32, int32_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3262 FILL_TABLE_ROW (dt_uint32, uint32_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3263 FILL_TABLE_ROW (dt_int64, int64_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3264 FILL_TABLE_ROW (dt_uint64, uint64_t); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3265 FILL_TABLE_ROW (dt_single, float); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3266 FILL_TABLE_ROW (dt_double, double); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3267 FILL_TABLE_ROW (dt_char, char); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3268 FILL_TABLE_ROW (dt_schar, signed char); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3269 FILL_TABLE_ROW (dt_uchar, unsigned char); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3270 FILL_TABLE_ROW (dt_logical, bool); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3271 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3272 initialized = true; |
4944 | 3273 } |
3274 | |
3275 bool swap = false; | |
3276 | |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3277 if (ffmt == oct_mach_info::flt_fmt_unknown) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3278 ffmt = float_format (); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3279 |
4944 | 3280 if (oct_mach_info::words_big_endian ()) |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3281 swap = (ffmt == oct_mach_info::flt_fmt_ieee_little_endian); |
4944 | 3282 else |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3283 swap = (ffmt == oct_mach_info::flt_fmt_ieee_big_endian); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3284 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3285 bool do_float_fmt_conv = ((input_type == oct_data_conv::dt_double |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3286 || input_type == oct_data_conv::dt_single) |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3287 && ffmt != float_format ()); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3288 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3289 bool do_NA_conv = (output_type == oct_data_conv::dt_double); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3290 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3291 switch (output_type) |
4944 | 3292 { |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3293 case oct_data_conv::dt_int8: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3294 case oct_data_conv::dt_uint8: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3295 case oct_data_conv::dt_int16: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3296 case oct_data_conv::dt_uint16: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3297 case oct_data_conv::dt_int32: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3298 case oct_data_conv::dt_uint32: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3299 case oct_data_conv::dt_int64: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3300 case oct_data_conv::dt_uint64: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3301 case oct_data_conv::dt_single: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3302 case oct_data_conv::dt_double: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3303 case oct_data_conv::dt_char: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3304 case oct_data_conv::dt_schar: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3305 case oct_data_conv::dt_uchar: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3306 case oct_data_conv::dt_logical: |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3307 { |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3308 conv_fptr fptr = conv_fptr_table[input_type][output_type]; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3309 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3310 retval = fptr (input_buf_list, input_buf_elts, elts_read, |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3311 nr, nc, swap, do_float_fmt_conv, do_NA_conv, ffmt); |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3312 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3313 break; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3314 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3315 default: |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
3316 ::error ("read: invalid type specification"); |
4944 | 3317 } |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3318 |
4944 | 3319 return retval; |
3320 } | |
3321 | |
3322 octave_value | |
5275 | 3323 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
|
3324 oct_data_conv::data_type input_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3325 oct_data_conv::data_type output_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3326 octave_idx_type skip, oct_mach_info::float_format ffmt, |
18210
a0abcf377ec5
return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3327 octave_idx_type& count) |
4944 | 3328 { |
3329 octave_value retval; | |
3330 | |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3331 octave_idx_type nr = -1; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3332 octave_idx_type nc = -1; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3333 |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3334 bool one_elt_size_spec = false; |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3335 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3336 if (! stream_ok ()) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3337 return retval; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3338 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3339 // FIXME: we may eventually want to make this extensible. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3340 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3341 // FIXME: we need a better way to ensure that this |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3342 // numbering stays consistent with the order of the elements in the |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3343 // data_type enum in the oct_data_conv class. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3344 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3345 // Expose this in a future version? |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3346 octave_idx_type char_count = 0; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3347 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3348 count = 0; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3349 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3350 try |
4944 | 3351 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3352 get_size (size, nr, nc, one_elt_size_spec, "fread"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3353 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3354 catch (const octave_execution_exception&) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3355 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3356 invalid_operation ("fread", "reading"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3357 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3358 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3359 octave_idx_type elts_to_read; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3360 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3361 if (one_elt_size_spec) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3362 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3363 // If NR == 0, Matlab returns [](0x0). |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3364 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3365 // If NR > 0, the result will be a column vector with the given |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3366 // number of rows. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3367 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3368 // If NR < 0, then we have Inf and the result will be a column |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3369 // vector but we have to wait to see how big NR will be. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3370 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3371 if (nr == 0) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3372 nr = nc = 0; |
20741
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3373 else |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3374 nc = 1; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3375 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3376 else |
4944 | 3377 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3378 // Matlab returns [] even if there are two elements in the size |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3379 // specification and one is nonzero. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3380 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3381 // If NC < 0 we have [NR, Inf] and we'll wait to decide how big NC |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3382 // should be. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3383 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3384 if (nr == 0 || nc == 0) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3385 nr = nc = 0; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3386 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3387 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3388 // FIXME: Ensure that this does not overflow. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3389 // Maybe try comparing nr * nc computed in double with |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3390 // std::numeric_limits<octave_idx_type>::max (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3391 elts_to_read = nr * nc; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3392 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3393 bool read_to_eof = elts_to_read < 0; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3394 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3395 octave_idx_type input_buf_elts = -1; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3396 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3397 if (skip == 0) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3398 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3399 if (read_to_eof) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3400 input_buf_elts = 1024 * 1024; |
4944 | 3401 else |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3402 input_buf_elts = elts_to_read; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3403 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3404 else |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3405 input_buf_elts = block_size; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3406 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3407 octave_idx_type input_elt_size |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3408 = oct_data_conv::data_type_size (input_type); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3409 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3410 octave_idx_type input_buf_size = input_buf_elts * input_elt_size; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3411 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3412 assert (input_buf_size >= 0); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3413 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3414 // Must also work and return correct type object |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3415 // for 0 elements to read. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3416 std::istream *isp = input_stream (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3417 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3418 if (! isp) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3419 error ("fread: invalid input stream"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3420 else |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3421 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3422 std::istream& is = *isp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3423 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3424 std::list <void *> input_buf_list; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3425 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3426 while (is && ! is.eof () |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3427 && (read_to_eof || count < elts_to_read)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3428 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3429 if (! read_to_eof) |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3430 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3431 octave_idx_type remaining_elts = elts_to_read - count; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3432 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3433 if (remaining_elts < input_buf_elts) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3434 input_buf_size = remaining_elts * input_elt_size; |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3435 } |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3436 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3437 char *input_buf = new char [input_buf_size]; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3438 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3439 is.read (input_buf, input_buf_size); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3440 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3441 size_t gcount = is.gcount (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3442 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3443 char_count += gcount; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3444 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3445 octave_idx_type nel = gcount / input_elt_size; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3446 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3447 count += nel; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3448 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3449 input_buf_list.push_back (input_buf); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3450 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3451 if (is && skip != 0 && nel == block_size) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3452 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3453 // Seek to skip. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3454 // If skip would move past EOF, position at EOF. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3455 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3456 off_t orig_pos = tell (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3457 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3458 seek (0, SEEK_END); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3459 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3460 off_t eof_pos = tell (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3461 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3462 // Is it possible for this to fail to return us to |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3463 // the original position? |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3464 seek (orig_pos, SEEK_SET); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3465 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3466 off_t remaining = eof_pos - orig_pos; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3467 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3468 if (remaining < skip) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3469 seek (0, SEEK_END); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3470 else |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3471 seek (skip, SEEK_CUR); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3472 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3473 if (! is) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3474 break; |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3475 } |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3476 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3477 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3478 if (read_to_eof) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3479 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3480 if (nc < 0) |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3481 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3482 nc = count / nr; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3483 |
20741
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3484 if (count % nr != 0) |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3485 nc++; |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3486 } |
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3487 else |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3488 nr = count; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3489 } |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3490 else if (count == 0) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3491 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3492 nr = 0; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3493 nc = 0; |
20741
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3494 } |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3495 else if (count != nr * nc) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3496 { |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3497 if (count % nr != 0) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3498 nc = count / nr + 1; |
17453
669ad11f282d
improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents:
17416
diff
changeset
|
3499 else |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3500 nc = count / nr; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3501 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3502 if (count < nr) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3503 nr = count; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3504 } |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3505 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3506 retval = finalize_read (input_buf_list, input_buf_elts, count, |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3507 nr, nc, input_type, output_type, ffmt); |
4944 | 3508 } |
2117 | 3509 |
3510 return retval; | |
3511 } | |
3512 | |
5275 | 3513 octave_idx_type |
3514 octave_stream::write (const octave_value& data, octave_idx_type block_size, | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3515 oct_data_conv::data_type output_type, |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3516 octave_idx_type skip, oct_mach_info::float_format flt_fmt) |
2117 | 3517 { |
5275 | 3518 octave_idx_type retval = -1; |
2117 | 3519 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3520 if (! stream_ok ()) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3521 invalid_operation ("fwrite", "writing"); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3522 else |
4944 | 3523 { |
20741
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3524 if (flt_fmt == oct_mach_info::flt_fmt_unknown) |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3525 flt_fmt = float_format (); |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3526 |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3527 octave_idx_type status = data.write (*this, block_size, output_type, |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3528 skip, flt_fmt); |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3529 |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3530 if (status < 0) |
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3531 error ("fwrite: write error"); |
4944 | 3532 else |
20741
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20714
diff
changeset
|
3533 retval = status; |
4944 | 3534 } |
2117 | 3535 |
3536 return retval; | |
3537 } | |
3538 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
3539 template <typename T, typename V> |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3540 static void |
18616
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3541 convert_chars (const void *data, void *conv_data, octave_idx_type n_elts) |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3542 { |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3543 const T *tt_data = static_cast<const T *> (data); |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3544 |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3545 V *vt_data = static_cast<V *> (conv_data); |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3546 |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3547 for (octave_idx_type i = 0; i < n_elts; i++) |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3548 vt_data[i] = tt_data[i]; |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3549 } |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3550 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
3551 template <typename T, typename V> |
18616
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3552 static void |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3553 convert_ints (const T *data, void *conv_data, octave_idx_type n_elts, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3554 bool swap) |
4944 | 3555 { |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3556 typedef typename V::val_type val_type; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3557 |
19739
3fa35defe495
Adjust spacing of static_cast<> calls to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19729
diff
changeset
|
3558 val_type *vt_data = static_cast<val_type *> (conv_data); |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3559 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3560 for (octave_idx_type i = 0; i < n_elts; i++) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3561 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3562 // Yes, we want saturation semantics when converting to an integer type. |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3563 V val (data[i]); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3564 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3565 vt_data[i] = val.value (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3566 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3567 if (swap) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3568 swap_bytes<sizeof (val_type)> (&vt_data[i]); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3569 } |
4944 | 3570 } |
3571 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
3572 template <typename T> |
18616
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3573 class ultimate_element_type |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3574 { |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3575 public: |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3576 typedef T type; |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3577 }; |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3578 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
3579 template <typename T> |
18616
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3580 class ultimate_element_type<octave_int<T> > |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3581 { |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3582 public: |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3583 typedef T type; |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3584 }; |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3585 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
3586 template <typename T> |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3587 static bool |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3588 convert_data (const T *data, void *conv_data, octave_idx_type n_elts, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3589 oct_data_conv::data_type output_type, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3590 oct_mach_info::float_format flt_fmt) |
4944 | 3591 { |
3592 bool retval = true; | |
3593 | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3594 bool swap |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3595 = ((oct_mach_info::words_big_endian () |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3596 && flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3597 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3598 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3599 bool do_float_conversion = flt_fmt != oct_mach_info::float_format (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3600 |
18616
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3601 typedef typename ultimate_element_type<T>::type ult_elt_type; |
4944 | 3602 |
3603 switch (output_type) | |
3604 { | |
3605 case oct_data_conv::dt_char: | |
18616
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3606 convert_chars<ult_elt_type, char> (data, conv_data, n_elts); |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3607 break; |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3608 |
4944 | 3609 case oct_data_conv::dt_schar: |
18616
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3610 convert_chars<ult_elt_type, signed char> (data, conv_data, n_elts); |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3611 break; |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3612 |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3613 case oct_data_conv::dt_uchar: |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3614 convert_chars<ult_elt_type, unsigned char> (data, conv_data, n_elts); |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3615 break; |
aa861a98d84d
fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents:
18518
diff
changeset
|
3616 |
4944 | 3617 case oct_data_conv::dt_int8: |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3618 convert_ints<T, octave_int8> (data, conv_data, n_elts, swap); |
4944 | 3619 break; |
3620 | |
3621 case oct_data_conv::dt_uint8: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3622 convert_ints<T, octave_uint8> (data, conv_data, n_elts, swap); |
4944 | 3623 break; |
3624 | |
3625 case oct_data_conv::dt_int16: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3626 convert_ints<T, octave_int16> (data, conv_data, n_elts, swap); |
4944 | 3627 break; |
3628 | |
3629 case oct_data_conv::dt_uint16: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3630 convert_ints<T, octave_uint16> (data, conv_data, n_elts, swap); |
4944 | 3631 break; |
3632 | |
3633 case oct_data_conv::dt_int32: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3634 convert_ints<T, octave_int32> (data, conv_data, n_elts, swap); |
4944 | 3635 break; |
3636 | |
3637 case oct_data_conv::dt_uint32: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3638 convert_ints<T, octave_uint32> (data, conv_data, n_elts, swap); |
4944 | 3639 break; |
3640 | |
3641 case oct_data_conv::dt_int64: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3642 convert_ints<T, octave_int64> (data, conv_data, n_elts, swap); |
4944 | 3643 break; |
3644 | |
3645 case oct_data_conv::dt_uint64: | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3646 convert_ints<T, octave_uint64> (data, conv_data, n_elts, swap); |
4944 | 3647 break; |
3648 | |
3649 case oct_data_conv::dt_single: | |
3650 { | |
19739
3fa35defe495
Adjust spacing of static_cast<> calls to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19729
diff
changeset
|
3651 float *vt_data = static_cast<float *> (conv_data); |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3652 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3653 for (octave_idx_type i = 0; i < n_elts; i++) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3654 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3655 vt_data[i] = data[i]; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3656 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3657 if (do_float_conversion) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3658 do_float_format_conversion (&vt_data[i], 1, flt_fmt); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3659 } |
4944 | 3660 } |
3661 break; | |
3662 | |
3663 case oct_data_conv::dt_double: | |
3664 { | |
19739
3fa35defe495
Adjust spacing of static_cast<> calls to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19729
diff
changeset
|
3665 double *vt_data = static_cast<double *> (conv_data); |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3666 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3667 for (octave_idx_type i = 0; i < n_elts; i++) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3668 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3669 vt_data[i] = data[i]; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3670 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3671 if (do_float_conversion) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3672 do_double_format_conversion (&vt_data[i], 1, flt_fmt); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3673 } |
4944 | 3674 } |
3675 break; | |
3676 | |
3677 default: | |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
3678 ::error ("write: invalid type specification"); |
4944 | 3679 } |
3680 | |
3681 return retval; | |
3682 } | |
3683 | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3684 bool |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3685 octave_stream::write_bytes (const void *data, size_t nbytes) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3686 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3687 bool status = false; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3688 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3689 std::ostream *osp = output_stream (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3690 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3691 if (osp) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3692 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3693 std::ostream& os = *osp; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3694 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3695 if (os) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3696 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3697 os.write (static_cast<const char *> (data), nbytes); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3698 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3699 if (os) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3700 status = true; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3701 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3702 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3703 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3704 return status; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3705 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3706 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3707 bool |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3708 octave_stream::skip_bytes (size_t skip) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3709 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3710 bool status = false; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3711 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3712 std::ostream *osp = output_stream (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3713 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3714 if (! osp) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3715 return false; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3716 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3717 std::ostream& os = *osp; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3718 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3719 // Seek to skip when inside bounds of existing file. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3720 // Otherwise, write NUL to skip. |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3721 off_t orig_pos = tell (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3722 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3723 seek (0, SEEK_END); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3724 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3725 off_t eof_pos = tell (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3726 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3727 // Is it possible for this to fail to return us to the original position? |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3728 seek (orig_pos, SEEK_SET); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3729 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3730 size_t remaining = eof_pos - orig_pos; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3731 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3732 if (remaining < skip) |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3733 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3734 seek (0, SEEK_END); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3735 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3736 // FIXME: probably should try to write larger blocks... |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3737 unsigned char zero = 0; |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3738 for (size_t j = 0; j < skip - remaining; j++) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3739 os.write (reinterpret_cast<const char *> (&zero), 1); |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3740 } |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3741 else |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3742 seek (skip, SEEK_CUR); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3743 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3744 if (os) |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
3745 status = true; |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3746 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3747 return status; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3748 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3749 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21129
diff
changeset
|
3750 template <typename T> |
5275 | 3751 octave_idx_type |
3752 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
|
3753 oct_data_conv::data_type output_type, |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3754 octave_idx_type skip, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3755 oct_mach_info::float_format flt_fmt) |
4944 | 3756 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3757 bool swap = ((oct_mach_info::words_big_endian () |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3758 && flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3759 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3760 |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3761 bool do_data_conversion = (swap || ! is_equivalent_type<T> (output_type) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3762 || flt_fmt != oct_mach_info::float_format ()); |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3763 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3764 octave_idx_type nel = data.numel (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3765 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3766 octave_idx_type chunk_size; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3767 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3768 if (skip != 0) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3769 chunk_size = block_size; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3770 else if (do_data_conversion) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3771 chunk_size = 1024 * 1024; |
4944 | 3772 else |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3773 chunk_size = nel; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3774 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3775 octave_idx_type i = 0; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3776 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3777 const T *pdata = data.data (); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3778 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3779 while (i < nel) |
4944 | 3780 { |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3781 if (skip != 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3782 { |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3783 if (! skip_bytes (skip)) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3784 return -1; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3785 } |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3786 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3787 octave_idx_type remaining_nel = nel - i; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3788 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3789 if (chunk_size > remaining_nel) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3790 chunk_size = remaining_nel; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3791 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3792 bool status = false; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3793 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3794 if (do_data_conversion) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3795 { |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3796 size_t output_size |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3797 = chunk_size * oct_data_conv::data_type_size (output_type); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3798 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3799 OCTAVE_LOCAL_BUFFER (unsigned char, conv_data, output_size); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3800 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3801 status = convert_data (&pdata[i], conv_data, chunk_size, |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3802 output_type, flt_fmt); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3803 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3804 if (status) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3805 status = write_bytes (conv_data, output_size); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3806 } |
4944 | 3807 else |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3808 status = write_bytes (pdata, sizeof (T) * chunk_size); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3809 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3810 if (! status) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3811 return -1; |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3812 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3813 i += chunk_size; |
4944 | 3814 } |
3815 | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3816 return nel; |
4944 | 3817 } |
3818 | |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3819 #define INSTANTIATE_WRITE(T) \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3820 template \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3821 octave_idx_type \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3822 octave_stream::write (const Array<T>& data, octave_idx_type block_size, \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3823 oct_data_conv::data_type output_type, \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3824 octave_idx_type skip, \ |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3825 oct_mach_info::float_format flt_fmt) |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3826 |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3827 INSTANTIATE_WRITE (octave_int8); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3828 INSTANTIATE_WRITE (octave_uint8); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3829 INSTANTIATE_WRITE (octave_int16); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3830 INSTANTIATE_WRITE (octave_uint16); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3831 INSTANTIATE_WRITE (octave_int32); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3832 INSTANTIATE_WRITE (octave_uint32); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3833 INSTANTIATE_WRITE (octave_int64); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3834 INSTANTIATE_WRITE (octave_uint64); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3835 INSTANTIATE_WRITE (int8_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3836 INSTANTIATE_WRITE (uint8_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3837 INSTANTIATE_WRITE (int16_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3838 INSTANTIATE_WRITE (uint16_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3839 INSTANTIATE_WRITE (int32_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3840 INSTANTIATE_WRITE (uint32_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3841 INSTANTIATE_WRITE (int64_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3842 INSTANTIATE_WRITE (uint64_t); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3843 INSTANTIATE_WRITE (bool); |
21354
06d15e4e611a
move public data type macros to octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21335
diff
changeset
|
3844 #if defined (OCTAVE_HAVE_OVERLOAD_CHAR_INT8_TYPES) |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3845 INSTANTIATE_WRITE (char); |
21235
b48d65c5df5b
Disable char/int8_t function overloads where not permitted (bug #45411)
Mike Miller <mtmiller@octave.org>
parents:
20252
diff
changeset
|
3846 #endif |
17416
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3847 INSTANTIATE_WRITE (float); |
6690dba6078a
improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents:
17415
diff
changeset
|
3848 INSTANTIATE_WRITE (double); |
4944 | 3849 |
2117 | 3850 octave_value |
3810 | 3851 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
|
3852 octave_idx_type& count, const std::string& who) |
2117 | 3853 { |
3854 octave_value retval; | |
3855 | |
5659 | 3856 if (stream_ok ()) |
4468 | 3857 retval = rep->scanf (fmt, size, count, who); |
2117 | 3858 |
3859 return retval; | |
3860 } | |
3861 | |
5279 | 3862 octave_value |
3863 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
|
3864 octave_idx_type& count, const std::string& who) |
5279 | 3865 { |
3866 octave_value retval = Matrix (); | |
3867 | |
3868 if (fmt.is_string ()) | |
3869 { | |
3870 std::string sfmt = fmt.string_value (); | |
3871 | |
3872 if (fmt.is_sq_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3873 sfmt = do_string_escapes (sfmt); |
5279 | 3874 |
3875 retval = scanf (sfmt, size, count, who); | |
3876 } | |
3877 else | |
3878 { | |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
3879 // Note: error is member fcn from octave_stream, not ::error. |
5279 | 3880 error (who + ": format must be a string"); |
3881 } | |
3882 | |
3883 return retval; | |
3884 } | |
3885 | |
2215 | 3886 octave_value_list |
4468 | 3887 octave_stream::oscanf (const std::string& fmt, const std::string& who) |
2215 | 3888 { |
3889 octave_value_list retval; | |
3890 | |
5659 | 3891 if (stream_ok ()) |
4468 | 3892 retval = rep->oscanf (fmt, who); |
2215 | 3893 |
3894 return retval; | |
3895 } | |
3896 | |
5279 | 3897 octave_value_list |
3898 octave_stream::oscanf (const octave_value& fmt, const std::string& who) | |
3899 { | |
3900 octave_value_list retval; | |
3901 | |
3902 if (fmt.is_string ()) | |
3903 { | |
3904 std::string sfmt = fmt.string_value (); | |
3905 | |
3906 if (fmt.is_sq_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3907 sfmt = do_string_escapes (sfmt); |
5279 | 3908 |
3909 retval = oscanf (sfmt, who); | |
3910 } | |
3911 else | |
3912 { | |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
3913 // Note: error is member fcn from octave_stream, not ::error. |
5279 | 3914 error (who + ": format must be a string"); |
3915 } | |
3916 | |
3917 return retval; | |
3918 } | |
3919 | |
2117 | 3920 int |
4468 | 3921 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
|
3922 const std::string& who) |
2117 | 3923 { |
3924 int retval = -1; | |
3925 | |
5659 | 3926 if (stream_ok ()) |
4468 | 3927 retval = rep->printf (fmt, args, who); |
2117 | 3928 |
3929 return retval; | |
3930 } | |
3931 | |
3932 int | |
5279 | 3933 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
|
3934 const std::string& who) |
5279 | 3935 { |
3936 int retval = 0; | |
3937 | |
3938 if (fmt.is_string ()) | |
3939 { | |
3940 std::string sfmt = fmt.string_value (); | |
3941 | |
3942 if (fmt.is_sq_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
3943 sfmt = do_string_escapes (sfmt); |
5279 | 3944 |
3945 retval = printf (sfmt, args, who); | |
3946 } | |
3947 else | |
3948 { | |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
3949 // Note: error is member fcn from octave_stream, not ::error. |
5279 | 3950 error (who + ": format must be a string"); |
3951 } | |
3952 | |
3953 return retval; | |
3954 } | |
3955 | |
3956 int | |
4468 | 3957 octave_stream::puts (const std::string& s, const std::string& who) |
2117 | 3958 { |
3959 int retval = -1; | |
3960 | |
5659 | 3961 if (stream_ok ()) |
4468 | 3962 retval = rep->puts (s, who); |
2117 | 3963 |
3964 return retval; | |
3965 } | |
3966 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
3967 // FIXME: maybe this should work for string arrays too. |
2117 | 3968 |
3969 int | |
4468 | 3970 octave_stream::puts (const octave_value& tc_s, const std::string& who) |
2117 | 3971 { |
3972 int retval = -1; | |
3973 | |
3974 if (tc_s.is_string ()) | |
3975 { | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
3976 std::string s = tc_s.string_value (); |
5279 | 3977 retval = puts (s, who); |
2117 | 3978 } |
3979 else | |
4468 | 3980 { |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
3981 // Note: error is member fcn from octave_stream, not ::error. |
4468 | 3982 error (who + ": argument must be a string"); |
3983 } | |
2117 | 3984 |
3985 return retval; | |
3986 } | |
3987 | |
3988 bool | |
3989 octave_stream::eof (void) const | |
3990 { | |
3991 int retval = -1; | |
3992 | |
5659 | 3993 if (stream_ok ()) |
2117 | 3994 retval = rep->eof (); |
3995 | |
3996 return retval; | |
3997 } | |
3998 | |
3536 | 3999 std::string |
2435 | 4000 octave_stream::error (bool clear, int& err_num) |
2117 | 4001 { |
5649 | 4002 std::string retval = "invalid stream object"; |
4003 | |
5659 | 4004 if (stream_ok (false)) |
2435 | 4005 retval = rep->error (clear, err_num); |
2117 | 4006 |
4007 return retval; | |
4008 } | |
4009 | |
3536 | 4010 std::string |
3340 | 4011 octave_stream::name (void) const |
2117 | 4012 { |
3523 | 4013 std::string retval; |
2117 | 4014 |
5659 | 4015 if (stream_ok ()) |
2117 | 4016 retval = rep->name (); |
4017 | |
4018 return retval; | |
4019 } | |
4020 | |
4021 int | |
3340 | 4022 octave_stream::mode (void) const |
2117 | 4023 { |
4024 int retval = 0; | |
4025 | |
5659 | 4026 if (stream_ok ()) |
2117 | 4027 retval = rep->mode (); |
4028 | |
4029 return retval; | |
4030 } | |
4031 | |
2317 | 4032 oct_mach_info::float_format |
3340 | 4033 octave_stream::float_format (void) const |
2117 | 4034 { |
4574 | 4035 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
2317 | 4036 |
5659 | 4037 if (stream_ok ()) |
2317 | 4038 retval = rep->float_format (); |
2117 | 4039 |
4040 return retval; | |
4041 } | |
4042 | |
3536 | 4043 std::string |
2117 | 4044 octave_stream::mode_as_string (int mode) |
4045 { | |
3523 | 4046 std::string retval = "???"; |
3775 | 4047 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode); |
4048 | |
4049 if (in_mode == std::ios::in) | |
4050 retval = "r"; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
4051 else if (in_mode == std::ios::out |
4078 | 4052 || in_mode == (std::ios::out | std::ios::trunc)) |
3775 | 4053 retval = "w"; |
4078 | 4054 else if (in_mode == (std::ios::out | std::ios::app)) |
3775 | 4055 retval = "a"; |
4078 | 4056 else if (in_mode == (std::ios::in | std::ios::out)) |
3775 | 4057 retval = "r+"; |
4078 | 4058 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc)) |
3775 | 4059 retval = "w+"; |
4078 | 4060 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate)) |
3775 | 4061 retval = "a+"; |
4078 | 4062 else if (in_mode == (std::ios::in | std::ios::binary)) |
3775 | 4063 retval = "rb"; |
4078 | 4064 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
|
4065 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary)) |
3775 | 4066 retval = "wb"; |
4078 | 4067 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary)) |
3775 | 4068 retval = "ab"; |
4078 | 4069 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary)) |
3775 | 4070 retval = "r+b"; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
4071 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
|
4072 | std::ios::binary)) |
3775 | 4073 retval = "w+b"; |
4078 | 4074 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
|
4075 | std::ios::binary)) |
3775 | 4076 retval = "a+b"; |
2117 | 4077 |
4078 return retval; | |
4079 } | |
4080 | |
4081 octave_stream_list *octave_stream_list::instance = 0; | |
4082 | |
2926 | 4083 bool |
4084 octave_stream_list::instance_ok (void) | |
4085 { | |
4086 bool retval = true; | |
4087 | |
4088 if (! instance) | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
4089 { |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
4090 instance = new octave_stream_list (); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
4091 |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
4092 if (instance) |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
13271
diff
changeset
|
4093 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
|
4094 } |
2926 | 4095 |
4096 if (! instance) | |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20768
diff
changeset
|
4097 ::error ("unable to create stream list object!"); |
2926 | 4098 |
4099 return retval; | |
4100 } | |
4101 | |
5353 | 4102 int |
6757 | 4103 octave_stream_list::insert (octave_stream& os) |
2926 | 4104 { |
5353 | 4105 return (instance_ok ()) ? instance->do_insert (os) : -1; |
2926 | 4106 } |
4107 | |
3340 | 4108 octave_stream |
3523 | 4109 octave_stream_list::lookup (int fid, const std::string& who) |
2926 | 4110 { |
3341 | 4111 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926 | 4112 } |
4113 | |
3340 | 4114 octave_stream |
3523 | 4115 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926 | 4116 { |
3341 | 4117 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926 | 4118 } |
4119 | |
4120 int | |
3523 | 4121 octave_stream_list::remove (int fid, const std::string& who) |
2926 | 4122 { |
3341 | 4123 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926 | 4124 } |
4125 | |
4126 int | |
3523 | 4127 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926 | 4128 { |
3341 | 4129 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926 | 4130 } |
4131 | |
4132 void | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4133 octave_stream_list::clear (bool flush) |
2926 | 4134 { |
4135 if (instance) | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4136 instance->do_clear (flush); |
2926 | 4137 } |
4138 | |
4139 string_vector | |
4140 octave_stream_list::get_info (int fid) | |
4141 { | |
4142 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); | |
4143 } | |
4144 | |
4145 string_vector | |
4146 octave_stream_list::get_info (const octave_value& fid) | |
4147 { | |
4148 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); | |
4149 } | |
4150 | |
3536 | 4151 std::string |
2926 | 4152 octave_stream_list::list_open_files (void) |
4153 { | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20982
diff
changeset
|
4154 return (instance_ok ()) ? instance->do_list_open_files () : ""; |
2926 | 4155 } |
4156 | |
4157 octave_value | |
4158 octave_stream_list::open_file_numbers (void) | |
4159 { | |
4160 return (instance_ok ()) | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
4161 ? instance->do_open_file_numbers () : octave_value (); |
2926 | 4162 } |
4163 | |
4164 int | |
4165 octave_stream_list::get_file_number (const octave_value& fid) | |
4166 { | |
4167 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; | |
4168 } | |
4169 | |
5353 | 4170 int |
6757 | 4171 octave_stream_list::do_insert (octave_stream& os) |
2117 | 4172 { |
6757 | 4173 // Insert item with key corresponding to file-descriptor. |
4174 | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4175 int stream_number = os.file_number (); |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4176 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4177 if (stream_number == -1) |
6757 | 4178 return stream_number; |
4179 | |
19864
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4180 // Should we test for |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4181 // |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4182 // (list.find (stream_number) != list.end () |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4183 // && list[stream_number].is_open ()) |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4184 // |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4185 // and respond with "error ("internal error: ...")"? It should not |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4186 // happen except for some bug or if the user has opened a stream with |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4187 // an interpreted command, but closed it directly with a system call |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4188 // in an oct-file; then the kernel knows the fd is free, but Octave |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4189 // does not know. If it happens, it should not do harm here to simply |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4190 // overwrite this entry, although the wrong entry might have done harm |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19861
diff
changeset
|
4191 // before. |
6757 | 4192 |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4193 if (list.size () >= list.max_size ()) |
20938
aac911d8847b
choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents:
20931
diff
changeset
|
4194 ::error ("could not create file id"); |
2117 | 4195 |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4196 list[stream_number] = os; |
2117 | 4197 |
5353 | 4198 return stream_number; |
2117 | 4199 } |
4200 | |
21040
3e7cfee5f786
maint: Rename attributes GCC_ATTR_XXX to OCTAVE_XXX.
Rik <rik@octave.org>
parents:
21029
diff
changeset
|
4201 OCTAVE_NORETURN static |
21029
e3b3bb522d62
maint: Move GCC_ attributes to start of declaration for future compatibility.
Rik <rik@octave.org>
parents:
21017
diff
changeset
|
4202 void |
21102
dfcb9d74b253
Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
4203 err_invalid_file_id (int fid, const std::string& who) |
3341 | 4204 { |
4205 if (who.empty ()) | |
4206 ::error ("invalid stream number = %d", fid); | |
4207 else | |
4208 ::error ("%s: invalid stream number = %d", who.c_str (), fid); | |
4209 } | |
4210 | |
3340 | 4211 octave_stream |
3523 | 4212 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117 | 4213 { |
3340 | 4214 octave_stream retval; |
2117 | 4215 |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4216 if (fid < 0) |
21102
dfcb9d74b253
Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
4217 err_invalid_file_id (fid, who); |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4218 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4219 if (lookup_cache != list.end () && lookup_cache->first == fid) |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4220 retval = lookup_cache->second; |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4221 else |
6757 | 4222 { |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4223 ostrl_map::const_iterator iter = list.find (fid); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4224 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4225 if (iter == list.end ()) |
21102
dfcb9d74b253
Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
4226 err_invalid_file_id (fid, who); |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4227 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4228 retval = iter->second; |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4229 lookup_cache = iter; |
6757 | 4230 } |
2117 | 4231 |
4232 return retval; | |
4233 } | |
4234 | |
3340 | 4235 octave_stream |
3341 | 4236 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
|
4237 const std::string& who) const |
2117 | 4238 { |
4239 int i = get_file_number (fid); | |
4240 | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4241 return do_lookup (i, who); |
2117 | 4242 } |
4243 | |
4244 int | |
3523 | 4245 octave_stream_list::do_remove (int fid, const std::string& who) |
2117 | 4246 { |
20764
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4247 // Can't remove stdin (std::cin), stdout (std::cout), or stderr (std::cerr). |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4248 if (fid < 3) |
21102
dfcb9d74b253
Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
4249 err_invalid_file_id (fid, who); |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4250 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4251 ostrl_map::iterator iter = list.find (fid); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4252 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4253 if (iter == list.end ()) |
21102
dfcb9d74b253
Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
4254 err_invalid_file_id (fid, who); |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4255 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4256 octave_stream os = iter->second; |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4257 list.erase (iter); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4258 lookup_cache = list.end (); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4259 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4260 // FIXME: is this check redundant? |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4261 if (! os.is_valid ()) |
21102
dfcb9d74b253
Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
4262 err_invalid_file_id (fid, who); |
20982
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4263 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4264 os.close (); |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4265 |
d27f66b4b8e6
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20946
diff
changeset
|
4266 return 0; |
2117 | 4267 } |
4268 | |
4269 int | |
3523 | 4270 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117 | 4271 { |
4272 int retval = -1; | |
4273 | |
6054 | 4274 if (fid.is_string () && fid.string_value () == "all") |
4275 { | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4276 do_clear (false); |
6054 | 4277 |
4278 retval = 0; | |
4279 } | |
4280 else | |
4281 { | |
4282 int i = get_file_number (fid); | |
4283 | |
20558
1a0a433c8263
eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20465
diff
changeset
|
4284 retval = do_remove (i, who); |
6054 | 4285 } |
2117 | 4286 |
4287 return retval; | |
4288 } | |
4289 | |
4290 void | |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4291 octave_stream_list::do_clear (bool flush) |
2117 | 4292 { |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4293 if (flush) |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4294 { |
20764
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4295 // Flush stdout and stderr. |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4296 list[1].flush (); |
20764
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4297 list[2].flush (); |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4298 } |
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4299 |
20764
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4300 for (ostrl_map::iterator iter = list.begin (); iter != list.end (); ) |
6757 | 4301 { |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4302 int fid = iter->first; |
20764
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4303 if (fid < 3) // Don't delete stdin, stdout, stderr |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4304 { |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4305 iter++; |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4306 continue; |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4307 } |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4308 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4309 octave_stream os = iter->second; |
20768
7655b399abff
eliminate trailing whitespace in source files we maintain
John W. Eaton <jwe@octave.org>
parents:
20764
diff
changeset
|
4310 |
7655b399abff
eliminate trailing whitespace in source files we maintain
John W. Eaton <jwe@octave.org>
parents:
20764
diff
changeset
|
4311 std::string name = os.name (); |
20764
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4312 std::transform (name.begin (), name.end (), name.begin (), tolower); |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4313 |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4314 // FIXME: This test for gnuplot is hardly foolproof. |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4315 if (name.find ("gnuplot") != std::string::npos) |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4316 { |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4317 // Don't close down pipes to gnuplot |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4318 iter++; |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4319 continue; |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4320 } |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4321 |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4322 // Normal file handle. Close and delete from list. |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4323 if (os.is_valid ()) |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4324 os.close (); |
20764
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4325 |
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4326 list.erase (iter++); |
6757 | 4327 } |
20764
0ad449bc8848
Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents:
20756
diff
changeset
|
4328 |
8902
5d5db7a347c6
erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
8773
diff
changeset
|
4329 lookup_cache = list.end (); |
2117 | 4330 } |
4331 | |
4332 string_vector | |
4333 octave_stream_list::do_get_info (int fid) const | |
4334 { | |
3340 | 4335 octave_stream os = do_lookup (fid); |
2117 | 4336 |
20892 | 4337 if (! os.is_valid ()) |
3341 | 4338 ::error ("invalid file id = %d", fid); |
2117 | 4339 |
20892 | 4340 string_vector retval (3); |
4341 | |
4342 retval(0) = os.name (); | |
4343 retval(1) = octave_stream::mode_as_string (os.mode ()); | |
4344 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); | |
4345 | |
2117 | 4346 return retval; |
4347 } | |
4348 | |
4349 string_vector | |
4350 octave_stream_list::do_get_info (const octave_value& fid) const | |
4351 { | |
4352 int conv_err = 0; | |
4353 | |
4354 int int_fid = convert_to_valid_int (fid, conv_err); | |
4355 | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4356 if (conv_err) |
2915 | 4357 ::error ("file id must be a file object or integer value"); |
2117 | 4358 |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4359 return do_get_info (int_fid); |
2117 | 4360 } |
4361 | |
3536 | 4362 std::string |
2117 | 4363 octave_stream_list::do_list_open_files (void) const |
4364 { | |
5765 | 4365 std::ostringstream buf; |
2117 | 4366 |
4367 buf << "\n" | |
4368 << " number mode arch name\n" | |
4369 << " ------ ---- ---- ----\n"; | |
4370 | |
6757 | 4371 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4372 { |
6757 | 4373 octave_stream os = p->second; |
2117 | 4374 |
4326 | 4375 buf << " " |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4376 << std::setiosflags (std::ios::right) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4377 << std::setw (4) << p->first << " " |
19123 | 4378 // reset necessary in addition to setiosflags since this is one stmt. |
4379 << std::resetiosflags (std::ios::adjustfield) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4380 << std::setiosflags (std::ios::left) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4381 << std::setw (3) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4382 << octave_stream::mode_as_string (os.mode ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4383 << " " |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4384 << std::setw (9) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4385 << 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
|
4386 << " " |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4387 << os.name () << "\n"; |
2117 | 4388 } |
4389 | |
5765 | 4390 buf << "\n"; |
4391 | |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4392 return buf.str (); |
2117 | 4393 } |
4394 | |
4395 octave_value | |
4396 octave_stream_list::do_open_file_numbers (void) const | |
4397 { | |
6757 | 4398 Matrix retval (1, list.size (), 0.0); |
2117 | 4399 |
4400 int num_open = 0; | |
4401 | |
6757 | 4402 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4403 { |
6757 | 4404 // Skip stdin, stdout, and stderr. |
4405 if (p->first > 2 && p->second) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4406 retval(0,num_open++) = p->first; |
2117 | 4407 } |
4408 | |
4409 retval.resize ((num_open > 0), num_open); | |
4410 | |
4411 return retval; | |
4412 } | |
4413 | |
4414 int | |
2609 | 4415 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117 | 4416 { |
4417 int retval = -1; | |
4418 | |
4419 if (fid.is_string ()) | |
4420 { | |
3523 | 4421 std::string nm = fid.string_value (); |
2117 | 4422 |
6757 | 4423 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
|
4424 { |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4425 // stdin, stdout, and stderr are unnamed. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4426 if (p->first > 2) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4427 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4428 octave_stream os = p->second; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4429 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4430 if (os && os.name () == nm) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4431 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4432 retval = p->first; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4433 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4434 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4435 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4436 } |
2117 | 4437 } |
4438 else | |
4439 { | |
4440 int conv_err = 0; | |
4441 | |
4442 int int_fid = convert_to_valid_int (fid, conv_err); | |
4443 | |
4444 if (conv_err) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10293
diff
changeset
|
4445 ::error ("file id must be a file object, std::string, or integer value"); |
20931
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4446 |
69dcb58b9ada
Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents:
20917
diff
changeset
|
4447 retval = int_fid; |
2117 | 4448 } |
4449 | |
4450 return retval; | |
4451 } |