Mercurial > octave
annotate libinterp/corefcn/ls-mat-ascii.cc @ 20756:200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
* gripes.h, gripes.cc (gripe_user_supplied_eval,
gripe_wrong_type_arg): New overloaded functions that accept
octave_execution_exception objects.
* error.h, error.cc (error, verror, error_1, usage_1,
debug_or_throw_exception): New overloaded functions that accept
* octave_execution_exception objects.
* __qp__.cc, cellfun.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc,
file-io.cc, graphics.cc, graphics.in.h, input.cc, load-path.cc,
ls-mat-ascii.cc, lsode.cc, mex.cc, oct-handle.h, oct-map.cc,
oct-stream.cc, quad.cc, rand.cc, toplev.cc, utils.cc, variables.cc,
__eigs__.cc, ov-base.cc, ov-class.cc, ov-fcn-handle.cc,
ov-oncleanup.cc, ov.cc, octave.cc, oct-parse.in.yy: Propagate
octave_execution_exception objects through try/catch blocks.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 25 Nov 2015 15:27:48 -0500 |
parents | f90c8372b7ba |
children | 48b2ad5ee801 |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3 Copyright (C) 1996-2015 John W. Eaton |
4634 | 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. | |
4634 | 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/>. | |
4634 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <cfloat> | |
28 #include <cstring> | |
29 #include <cctype> | |
30 | |
31 #include <fstream> | |
32 #include <iomanip> | |
33 #include <iostream> | |
5765 | 34 #include <sstream> |
4634 | 35 #include <string> |
36 | |
37 #include "byte-swap.h" | |
38 #include "data-conv.h" | |
39 #include "file-ops.h" | |
40 #include "glob-match.h" | |
41 #include "lo-mappers.h" | |
42 #include "mach-info.h" | |
43 #include "oct-env.h" | |
44 #include "oct-time.h" | |
45 #include "quit.h" | |
46 #include "str-vec.h" | |
47 | |
48 #include "Cell.h" | |
49 #include "defun.h" | |
50 #include "error.h" | |
51 #include "gripes.h" | |
4867 | 52 #include "lex.h" |
4634 | 53 #include "load-save.h" |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
54 #include "ls-ascii-helper.h" |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
55 #include "ls-mat-ascii.h" |
4634 | 56 #include "oct-obj.h" |
57 #include "oct-map.h" | |
58 #include "ov-cell.h" | |
59 #include "pager.h" | |
60 #include "pt-exp.h" | |
61 #include "sysdep.h" | |
20756
200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
62 #include "toplev.h" |
4634 | 63 #include "unwind-prot.h" |
64 #include "utils.h" | |
65 #include "variables.h" | |
66 #include "version.h" | |
67 #include "dMatrix.h" | |
68 | |
69 static std::string | |
70 get_mat_data_input_line (std::istream& is) | |
71 { | |
72 std::string retval; | |
73 | |
74 bool have_data = false; | |
75 | |
76 do | |
77 { | |
78 retval = ""; | |
79 | |
80 char c; | |
81 while (is.get (c)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
82 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
83 if (c == '\n' || c == '\r') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
84 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
85 is.putback (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
86 skip_preceeding_newline (is); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
87 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
88 } |
4634 | 89 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
90 if (c == '%' || c == '#') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
91 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
92 skip_until_newline (is, false); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
93 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
94 } |
4634 | 95 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
96 if (! is.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
97 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
98 if (! have_data && c != ' ' && c != '\t') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
99 have_data = true; |
4634 | 100 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
101 retval += c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
102 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
103 } |
4634 | 104 } |
105 while (! (have_data || is.eof ())); | |
106 | |
107 return retval; | |
108 } | |
109 | |
110 static void | |
17183
ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
16892
diff
changeset
|
111 get_lines_and_columns (std::istream& is, |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
112 octave_idx_type& nr, octave_idx_type& nc, |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
113 const std::string& filename = std::string (), |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
114 bool quiet = false, bool check_numeric = false) |
4634 | 115 { |
116 std::streampos pos = is.tellg (); | |
117 | |
118 int file_line_number = 0; | |
119 | |
120 nr = 0; | |
121 nc = 0; | |
122 | |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20535
diff
changeset
|
123 while (is) |
4634 | 124 { |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
125 octave_quit (); |
4634 | 126 |
127 std::string buf = get_mat_data_input_line (is); | |
128 | |
129 file_line_number++; | |
130 | |
131 size_t beg = buf.find_first_not_of (", \t"); | |
132 | |
133 // If we see a CR as the last character in the buffer, we had a | |
134 // CRLF pair as the line separator. Any other CR in the text | |
135 // will not be considered as whitespace. | |
136 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
137 if (beg != std::string::npos && buf[beg] == '\r' |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
138 && beg == buf.length () - 1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
139 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
140 // We had a blank line ending with a CRLF. Handle it the |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
141 // same as an empty line. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
142 beg = std::string::npos; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
143 } |
4634 | 144 |
5275 | 145 octave_idx_type tmp_nc = 0; |
4634 | 146 |
8021 | 147 while (beg != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
148 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
149 tmp_nc++; |
4634 | 150 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
151 size_t end = buf.find_first_of (", \t", beg); |
4634 | 152 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
153 if (end != std::string::npos) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
154 { |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
155 if (check_numeric) |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
156 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
157 std::istringstream tmp_stream (buf.substr (beg, end-beg)); |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
158 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
159 octave_read_double (tmp_stream); |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
160 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
161 if (tmp_stream.fail ()) |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
162 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
163 if (! quiet) |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
164 error ("load: %s: non-numeric data found near line %d", |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
165 filename.c_str (), file_line_number); |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
166 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
167 nr = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
168 nc = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
169 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
170 goto done; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
171 } |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
172 } |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
173 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
174 beg = buf.find_first_not_of (", \t", end); |
4634 | 175 |
19864
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
176 if (beg == std::string::npos |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
177 || (buf[beg] == '\r' && beg == buf.length () - 1)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
178 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
179 // We had a line with trailing spaces and |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
180 // ending with a CRLF, so this should look like EOL, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
181 // not a new colum. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
182 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
183 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
184 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
185 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
186 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
187 } |
4634 | 188 |
189 if (tmp_nc > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
190 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
191 if (nc == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
192 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
193 nc = tmp_nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
194 nr++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
195 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
196 else if (nc == tmp_nc) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
197 nr++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
198 else |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
199 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
200 if (! quiet) |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
201 error ("load: %s: inconsistent number of columns near line %d", |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
202 filename.c_str (), file_line_number); |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
203 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
204 nr = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
205 nc = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
206 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
207 goto done; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
208 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
209 } |
4634 | 210 } |
211 | |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
212 if (! quiet && (nr == 0 || nc == 0)) |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
213 error ("load: file '%s' seems to be empty!", filename.c_str ()); |
4634 | 214 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
215 done: |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
216 |
4634 | 217 is.clear (); |
4643 | 218 is.seekg (pos); |
4634 | 219 } |
220 | |
221 // Extract a matrix from a file of numbers only. | |
222 // | |
223 // Comments are not allowed. The file should only have numeric values. | |
224 // | |
225 // Reads the file twice. Once to find the number of rows and columns, | |
226 // and once to extract the matrix. | |
227 // | |
228 // FILENAME is used for error messages. | |
229 // | |
230 // This format provides no way to tag the data as global. | |
231 | |
232 std::string | |
233 read_mat_ascii_data (std::istream& is, const std::string& filename, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
234 octave_value& tc) |
4634 | 235 { |
236 std::string retval; | |
237 | |
238 std::string varname; | |
239 | |
240 size_t pos = filename.rfind ('/'); | |
241 | |
8021 | 242 if (pos != std::string::npos) |
4634 | 243 varname = filename.substr (pos+1); |
244 else | |
245 varname = filename; | |
246 | |
4867 | 247 pos = varname.rfind ('.'); |
4634 | 248 |
8021 | 249 if (pos != std::string::npos) |
4634 | 250 varname = varname.substr (0, pos); |
251 | |
252 size_t len = varname.length (); | |
253 for (size_t i = 0; i < len; i++) | |
254 { | |
255 char c = varname[i]; | |
256 if (! (isalnum (c) || c == '_')) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
257 varname[i] = '_'; |
4634 | 258 } |
259 | |
4867 | 260 if (is_keyword (varname) || ! isalpha (varname[0])) |
4634 | 261 varname.insert (0, "X"); |
262 | |
263 if (valid_identifier (varname)) | |
264 { | |
5275 | 265 octave_idx_type nr = 0; |
266 octave_idx_type nc = 0; | |
4634 | 267 |
268 int total_count = 0; | |
269 | |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
270 get_lines_and_columns (is, nr, nc, filename); |
4634 | 271 |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
272 octave_quit (); |
4634 | 273 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20535
diff
changeset
|
274 if (nr > 0 && nc > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
275 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
276 Matrix tmp (nr, nc); |
4634 | 277 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
278 if (nr < 1 || nc < 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
279 is.clear (std::ios::badbit); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
280 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
281 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
282 double d; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
283 for (octave_idx_type i = 0; i < nr; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
284 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
285 std::string buf = get_mat_data_input_line (is); |
4634 | 286 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
287 std::istringstream tmp_stream (buf); |
4634 | 288 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
289 for (octave_idx_type j = 0; j < nc; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
290 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
291 octave_quit (); |
4634 | 292 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
293 d = octave_read_value<double> (tmp_stream); |
4634 | 294 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
295 if (tmp_stream || tmp_stream.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
296 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
297 tmp.elem (i, j) = d; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
298 total_count++; |
4634 | 299 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
300 // Skip whitespace and commas. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
301 char c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
302 while (1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
303 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
304 tmp_stream >> c; |
4634 | 305 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
306 if (! tmp_stream) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
307 break; |
4634 | 308 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
309 if (! (c == ' ' || c == '\t' || c == ',')) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
310 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
311 tmp_stream.putback (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
312 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
313 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
314 } |
4634 | 315 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
316 if (tmp_stream.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
317 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
318 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
319 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
320 { |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
321 error ("load: failed to read matrix from file '%s'", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
322 filename.c_str ()); |
4634 | 323 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
324 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
325 } |
4634 | 326 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
327 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
328 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
329 } |
4634 | 330 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
331 if (is || is.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
332 { |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
333 // FIXME: not sure this is best, but it works. |
4634 | 334 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
335 if (is.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
336 is.clear (); |
4634 | 337 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
338 octave_idx_type expected = nr * nc; |
4634 | 339 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
340 if (expected == total_count) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
341 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
342 tc = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
343 retval = varname; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
344 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
345 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
346 error ("load: expected %d elements, found %d", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
347 expected, total_count); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
348 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
349 else |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
350 error ("load: failed to read matrix from file '%s'", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
351 filename.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
352 } |
4634 | 353 else |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
354 error ("load: unable to extract matrix size from file '%s'", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
355 filename.c_str ()); |
4634 | 356 } |
357 else | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
358 error ("load: unable to convert filename '%s' to valid identifier", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
359 filename.c_str ()); |
4634 | 360 |
361 return retval; | |
362 } | |
363 | |
5938 | 364 bool |
365 save_mat_ascii_data (std::ostream& os, const octave_value& val, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
366 int precision, bool tabs) |
5938 | 367 { |
368 bool success = true; | |
369 | |
370 if (val.is_complex_type ()) | |
371 warning ("save: omitting imaginary part for ASCII file"); | |
372 | |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
373 Matrix m; |
5938 | 374 |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
375 try |
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
376 { |
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
377 m = val.matrix_value (true); |
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
378 } |
20756
200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
379 catch (const octave_execution_exception& e) |
5938 | 380 { |
20756
200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
381 recover_from_exception (); |
200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
382 |
5938 | 383 success = false; |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
384 } |
5938 | 385 |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
386 if (success) |
5951 | 387 { |
388 long old_precision = os.precision (); | |
389 | |
390 os.precision (precision); | |
5938 | 391 |
5951 | 392 std::ios::fmtflags oflags |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
393 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific)); |
5951 | 394 |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
395 if (tabs) |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
396 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
397 for (octave_idx_type i = 0; i < m.rows (); i++) |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
398 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
399 for (octave_idx_type j = 0; j < m.cols (); j++) |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
400 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
401 // Omit leading tabs. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
402 if (j != 0) os << '\t'; |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
403 octave_write_double (os, m (i, j)); |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
404 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
405 os << "\n"; |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
406 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
407 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
408 else |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
409 os << m; |
5951 | 410 |
411 os.flags (oflags); | |
412 | |
413 os.precision (old_precision); | |
414 } | |
5938 | 415 |
416 return (os && success); | |
417 } | |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
418 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
419 bool |
17183
ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
16892
diff
changeset
|
420 looks_like_mat_ascii_file (std::istream& is, const std::string& filename) |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
421 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
422 bool retval = false; |
17183
ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
16892
diff
changeset
|
423 octave_idx_type nr = 0; |
ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
16892
diff
changeset
|
424 octave_idx_type nc = 0; |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
425 |
17183
ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
16892
diff
changeset
|
426 get_lines_and_columns (is, nr, nc, filename, true, true); |
ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
16892
diff
changeset
|
427 retval = (nr != 0 && nc != 0); |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
428 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
429 return retval; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
430 } |