Mercurial > octave
annotate libinterp/corefcn/ls-mat-ascii.cc @ 26376:00f796120a6d stable
maint: Update copyright dates in all source files.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 02 Jan 2019 16:32:43 -0500 |
parents | 7f6a50f73625 |
children | c67501d55541 |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
26164
diff
changeset
|
3 Copyright (C) 1996-2019 John W. Eaton |
4634 | 4 |
5 This file is part of Octave. | |
6 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23987
diff
changeset
|
7 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23987
diff
changeset
|
9 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
10 (at your option) any later version. |
4634 | 11 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
15 GNU General Public License for more details. |
4634 | 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 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23987
diff
changeset
|
19 <https://www.gnu.org/licenses/>. |
4634 | 20 |
21 */ | |
22 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
23 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
24 # include "config.h" |
4634 | 25 #endif |
26 | |
27 #include <cctype> | |
28 | |
29 #include <iomanip> | |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25392
diff
changeset
|
30 #include <istream> |
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25392
diff
changeset
|
31 #include <ostream> |
5765 | 32 #include <sstream> |
4634 | 33 #include <string> |
34 | |
35 #include "byte-swap.h" | |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
36 #include "dMatrix.h" |
4634 | 37 #include "data-conv.h" |
38 #include "file-ops.h" | |
39 #include "glob-match.h" | |
40 #include "lo-mappers.h" | |
41 #include "mach-info.h" | |
42 #include "oct-env.h" | |
43 #include "oct-time.h" | |
44 #include "quit.h" | |
45 | |
46 #include "Cell.h" | |
47 #include "defun.h" | |
48 #include "error.h" | |
22094
9203833cab7d
move new interpreter class to separate file
John W. Eaton <jwe@octave.org>
parents:
21724
diff
changeset
|
49 #include "interpreter.h" |
4867 | 50 #include "lex.h" |
4634 | 51 #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
|
52 #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
|
53 #include "ls-mat-ascii.h" |
4634 | 54 #include "oct-map.h" |
55 #include "ov-cell.h" | |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
56 #include "ov.h" |
4634 | 57 #include "pager.h" |
58 #include "sysdep.h" | |
59 #include "utils.h" | |
60 #include "variables.h" | |
61 #include "version.h" | |
62 | |
63 static std::string | |
64 get_mat_data_input_line (std::istream& is) | |
65 { | |
66 std::string retval; | |
67 | |
68 bool have_data = false; | |
69 | |
70 do | |
71 { | |
72 retval = ""; | |
73 | |
74 char c; | |
75 while (is.get (c)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
76 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
77 if (c == '\n' || c == '\r') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
78 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
79 is.putback (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
80 skip_preceeding_newline (is); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
81 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
82 } |
4634 | 83 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
84 if (c == '%' || c == '#') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
85 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
86 skip_until_newline (is, false); |
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 (! is.eof ()) |
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 if (! have_data && c != ' ' && c != '\t') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
93 have_data = true; |
4634 | 94 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
95 retval += c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
96 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
97 } |
4634 | 98 } |
99 while (! (have_data || is.eof ())); | |
100 | |
101 return retval; | |
102 } | |
103 | |
104 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
|
105 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
|
106 octave_idx_type& nr, octave_idx_type& nc, |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20981
diff
changeset
|
107 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
|
108 bool quiet = false, bool check_numeric = false) |
4634 | 109 { |
110 std::streampos pos = is.tellg (); | |
111 | |
112 int file_line_number = 0; | |
113 | |
114 nr = 0; | |
115 nc = 0; | |
116 | |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20535
diff
changeset
|
117 while (is) |
4634 | 118 { |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
119 octave_quit (); |
4634 | 120 |
121 std::string buf = get_mat_data_input_line (is); | |
122 | |
123 file_line_number++; | |
124 | |
125 size_t beg = buf.find_first_not_of (", \t"); | |
126 | |
127 // If we see a CR as the last character in the buffer, we had a | |
128 // CRLF pair as the line separator. Any other CR in the text | |
129 // will not be considered as whitespace. | |
130 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
131 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
|
132 && beg == buf.length () - 1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
133 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
134 // 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
|
135 // same as an empty line. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
136 beg = std::string::npos; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
137 } |
4634 | 138 |
5275 | 139 octave_idx_type tmp_nc = 0; |
4634 | 140 |
8021 | 141 while (beg != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
142 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
143 tmp_nc++; |
4634 | 144 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
145 size_t end = buf.find_first_of (", \t", beg); |
4634 | 146 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
147 if (end != std::string::npos) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
148 { |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
149 if (check_numeric) |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
150 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
151 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
|
152 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
153 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
|
154 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
155 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
|
156 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
157 if (! quiet) |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
158 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
|
159 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
|
160 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
161 nr = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
162 nc = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
163 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
164 goto done; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
165 } |
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 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
168 beg = buf.find_first_not_of (", \t", end); |
4634 | 169 |
19864
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
170 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
|
171 || (buf[beg] == '\r' && beg == buf.length () - 1)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
172 { |
23812
057a894914df
Use C++11 string fcns back() and pop_back() to simplify code.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
173 // We had a line with trailing spaces and ending with a CRLF, |
057a894914df
Use C++11 string fcns back() and pop_back() to simplify code.
Rik <rik@octave.org>
parents:
23581
diff
changeset
|
174 // so this should look like EOL, not a new column. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
175 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
176 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
177 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
178 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
179 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
180 } |
4634 | 181 |
182 if (tmp_nc > 0) | |
10315
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 if (nc == 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
185 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
186 nc = tmp_nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
187 nr++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
188 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
189 else if (nc == tmp_nc) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
190 nr++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
191 else |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
192 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
193 if (! quiet) |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
194 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
|
195 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
|
196 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
197 nr = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
198 nc = 0; |
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 goto done; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
201 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
202 } |
4634 | 203 } |
204 | |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
205 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
|
206 error ("load: file '%s' seems to be empty!", filename.c_str ()); |
4634 | 207 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
208 done: |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
209 |
4634 | 210 is.clear (); |
4643 | 211 is.seekg (pos); |
4634 | 212 } |
213 | |
214 // Extract a matrix from a file of numbers only. | |
215 // | |
216 // Comments are not allowed. The file should only have numeric values. | |
217 // | |
218 // Reads the file twice. Once to find the number of rows and columns, | |
219 // and once to extract the matrix. | |
220 // | |
221 // FILENAME is used for error messages. | |
222 // | |
223 // This format provides no way to tag the data as global. | |
224 | |
225 std::string | |
226 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
|
227 octave_value& tc) |
4634 | 228 { |
229 std::string varname; | |
230 | |
231 size_t pos = filename.rfind ('/'); | |
232 | |
8021 | 233 if (pos != std::string::npos) |
4634 | 234 varname = filename.substr (pos+1); |
235 else | |
236 varname = filename; | |
237 | |
4867 | 238 pos = varname.rfind ('.'); |
4634 | 239 |
8021 | 240 if (pos != std::string::npos) |
4634 | 241 varname = varname.substr (0, pos); |
242 | |
243 size_t len = varname.length (); | |
244 for (size_t i = 0; i < len; i++) | |
245 { | |
246 char c = varname[i]; | |
247 if (! (isalnum (c) || c == '_')) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
248 varname[i] = '_'; |
4634 | 249 } |
250 | |
26059
da2bbcf1fbcd
Deprecate C++ function is_keyword in favor of iskeyword for readability.
Rik <rik@octave.org>
parents:
25646
diff
changeset
|
251 if (octave::iskeyword (varname) || ! isalpha (varname[0])) |
4634 | 252 varname.insert (0, "X"); |
253 | |
25646
4d565baa475e
move libinterp/utils functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25438
diff
changeset
|
254 if (! octave::valid_identifier (varname)) |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20956
diff
changeset
|
255 error ("load: unable to convert filename '%s' to valid identifier", |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20956
diff
changeset
|
256 filename.c_str ()); |
4634 | 257 |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20956
diff
changeset
|
258 octave_idx_type nr = 0; |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20956
diff
changeset
|
259 octave_idx_type nc = 0; |
4634 | 260 |
26164
7f6a50f73625
Silence compiler warnings about format identifier for octave_idx_type (bug #55046).
Markus Mützel <markus.muetzel@gmx.de>
parents:
26059
diff
changeset
|
261 octave_idx_type total_count = 0; |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20956
diff
changeset
|
262 |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20956
diff
changeset
|
263 get_lines_and_columns (is, nr, nc, filename); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20956
diff
changeset
|
264 |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20956
diff
changeset
|
265 octave_quit (); |
4634 | 266 |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
267 if (nr <= 0 || nc <= 0) |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20956
diff
changeset
|
268 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
|
269 filename.c_str ()); |
4634 | 270 |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
271 Matrix tmp (nr, nc); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
272 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
273 if (nr < 1 || nc < 1) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
274 is.clear (std::ios::badbit); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
275 else |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
276 { |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
277 double d; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
278 for (octave_idx_type i = 0; i < nr; i++) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
279 { |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
280 std::string buf = get_mat_data_input_line (is); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
281 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
282 std::istringstream tmp_stream (buf); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
283 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
284 for (octave_idx_type j = 0; j < nc; j++) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
285 { |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
286 octave_quit (); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
287 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
288 d = octave_read_value<double> (tmp_stream); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
289 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
290 if (! tmp_stream && ! tmp_stream.eof ()) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
291 error ("load: failed to read matrix from file '%s'", |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
292 filename.c_str ()); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
293 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
294 tmp.elem (i, j) = d; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
295 total_count++; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
296 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
297 // Skip whitespace and commas. |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
298 char c; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
299 while (1) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
300 { |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
301 tmp_stream >> c; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
302 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
303 if (! tmp_stream) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
304 break; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
305 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
306 if (! (c == ' ' || c == '\t' || c == ',')) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
307 { |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
308 tmp_stream.putback (c); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
309 break; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
310 } |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
311 } |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
312 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
313 if (tmp_stream.eof ()) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
314 break; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
315 } |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
316 } |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
317 } |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
318 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
319 if (! is && ! is.eof ()) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
320 error ("load: failed to read matrix from file '%s'", |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
321 filename.c_str ()); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
322 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
323 // FIXME: not sure this is best, but it works. |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
324 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
325 if (is.eof ()) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
326 is.clear (); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
327 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
328 octave_idx_type expected = nr * nc; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
329 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
330 if (expected != total_count) |
26164
7f6a50f73625
Silence compiler warnings about format identifier for octave_idx_type (bug #55046).
Markus Mützel <markus.muetzel@gmx.de>
parents:
26059
diff
changeset
|
331 error ("load: expected %" OCTAVE_IDX_TYPE_FORMAT " elements, found " |
7f6a50f73625
Silence compiler warnings about format identifier for octave_idx_type (bug #55046).
Markus Mützel <markus.muetzel@gmx.de>
parents:
26059
diff
changeset
|
332 "%" OCTAVE_IDX_TYPE_FORMAT, expected, total_count); |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
333 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
334 tc = tmp; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
335 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
336 return varname; |
4634 | 337 } |
338 | |
5938 | 339 bool |
340 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
|
341 int precision, bool tabs) |
5938 | 342 { |
343 bool success = true; | |
344 | |
23581
c3075ae020e1
maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents:
23455
diff
changeset
|
345 if (val.iscomplex ()) |
5938 | 346 warning ("save: omitting imaginary part for ASCII file"); |
347 | |
23985
aa127fce67e4
doc: Document extra save options for -ascii format (bug #51898).
Rik <rik@octave.org>
parents:
23812
diff
changeset
|
348 if (val.ndims () > 2) |
23987
4d5018136313
Skip writing N-D matrix to -ascii format, rather than calling error().
Rik <rik@octave.org>
parents:
23985
diff
changeset
|
349 { |
4d5018136313
Skip writing N-D matrix to -ascii format, rather than calling error().
Rik <rik@octave.org>
parents:
23985
diff
changeset
|
350 warning ("save: skipping variable which is not a 2-D matrix"); |
4d5018136313
Skip writing N-D matrix to -ascii format, rather than calling error().
Rik <rik@octave.org>
parents:
23985
diff
changeset
|
351 return true; |
4d5018136313
Skip writing N-D matrix to -ascii format, rather than calling error().
Rik <rik@octave.org>
parents:
23985
diff
changeset
|
352 } |
23985
aa127fce67e4
doc: Document extra save options for -ascii format (bug #51898).
Rik <rik@octave.org>
parents:
23812
diff
changeset
|
353 |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
354 Matrix m; |
5938 | 355 |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
356 try |
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
357 { |
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
358 m = val.matrix_value (true); |
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
359 } |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
360 catch (const octave::execution_exception& e) |
5938 | 361 { |
23110
af48d8be62b3
move recover_from_exception and atexit functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
23084
diff
changeset
|
362 octave::interpreter::recover_from_exception (); |
20756
200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
363 |
5938 | 364 success = false; |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
365 } |
5938 | 366 |
20535
b70cc4bd8109
begin removal of global error_state variable
John W. Eaton <jwe@octave.org>
parents:
19864
diff
changeset
|
367 if (success) |
5951 | 368 { |
369 long old_precision = os.precision (); | |
370 | |
371 os.precision (precision); | |
5938 | 372 |
5951 | 373 std::ios::fmtflags oflags |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
374 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific)); |
5951 | 375 |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
376 if (tabs) |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
377 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
378 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
|
379 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
380 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
|
381 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
382 // Omit leading tabs. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
383 if (j != 0) os << '\t'; |
23985
aa127fce67e4
doc: Document extra save options for -ascii format (bug #51898).
Rik <rik@octave.org>
parents:
23812
diff
changeset
|
384 octave_write_double (os, m(i, j)); |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
385 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
386 os << "\n"; |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
387 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
388 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
389 else |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
390 os << m; |
5951 | 391 |
23985
aa127fce67e4
doc: Document extra save options for -ascii format (bug #51898).
Rik <rik@octave.org>
parents:
23812
diff
changeset
|
392 // Restore format |
5951 | 393 os.flags (oflags); |
394 os.precision (old_precision); | |
395 } | |
5938 | 396 |
397 return (os && success); | |
398 } | |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
399 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
400 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
|
401 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
|
402 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
403 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
|
404 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
|
405 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
|
406 |
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
|
407 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
|
408 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
|
409 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
410 return retval; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
411 } |