Mercurial > hg > octave-lyh
annotate libinterp/corefcn/ls-mat-ascii.cc @ 17191:ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
* load-save.cc (get_file_format): call check_gzip_magic before get_file_format
to avoid random LS_MAT_ASCII detections in gzipped files.
* ls-mat-ascii.cc (looks_like_mat_ascii_file): New arg, IS which could also
be a gzipped file, is now opened in the calling function.
* ls-mat-ascii.h (looks_like_mat_ascii_file): New arg, IS.
author | Andreas Weber <andy.weber.aw@gmail.com> |
---|---|
date | Fri, 02 Aug 2013 19:48:34 +0200 |
parents | 68fc671a9339 |
children |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
3 Copyright (C) 1996-2012 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" | |
62 #include "unwind-prot.h" | |
63 #include "utils.h" | |
64 #include "variables.h" | |
65 #include "version.h" | |
66 #include "dMatrix.h" | |
67 | |
68 static std::string | |
69 get_mat_data_input_line (std::istream& is) | |
70 { | |
71 std::string retval; | |
72 | |
73 bool have_data = false; | |
74 | |
75 do | |
76 { | |
77 retval = ""; | |
78 | |
79 char c; | |
80 while (is.get (c)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
81 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
82 if (c == '\n' || c == '\r') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
83 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
84 is.putback (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
85 skip_preceeding_newline (is); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
86 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
87 } |
4634 | 88 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
89 if (c == '%' || c == '#') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
90 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
91 skip_until_newline (is, false); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
92 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
93 } |
4634 | 94 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
95 if (! is.eof ()) |
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 if (! have_data && c != ' ' && c != '\t') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
98 have_data = true; |
4634 | 99 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
100 retval += c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
101 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
102 } |
4634 | 103 } |
104 while (! (have_data || is.eof ())); | |
105 | |
106 return retval; | |
107 } | |
108 | |
109 static void | |
17191
ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
16892
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 bool quiet = false, bool check_numeric = false) |
4634 | 114 { |
115 std::streampos pos = is.tellg (); | |
116 | |
117 int file_line_number = 0; | |
118 | |
119 nr = 0; | |
120 nc = 0; | |
121 | |
122 while (is && ! error_state) | |
123 { | |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
124 octave_quit (); |
4634 | 125 |
126 std::string buf = get_mat_data_input_line (is); | |
127 | |
128 file_line_number++; | |
129 | |
130 size_t beg = buf.find_first_not_of (", \t"); | |
131 | |
132 // If we see a CR as the last character in the buffer, we had a | |
133 // CRLF pair as the line separator. Any other CR in the text | |
134 // will not be considered as whitespace. | |
135 | |
8021 | 136 if (beg != std::string::npos && buf[beg] == '\r' && beg == buf.length () - 1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
137 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
138 // 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
|
139 // same as an empty line. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
140 beg = std::string::npos; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
141 } |
4634 | 142 |
5275 | 143 octave_idx_type tmp_nc = 0; |
4634 | 144 |
8021 | 145 while (beg != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
146 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
147 tmp_nc++; |
4634 | 148 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
149 size_t end = buf.find_first_of (", \t", beg); |
4634 | 150 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
151 if (end != std::string::npos) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
152 { |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
153 if (check_numeric) |
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 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
|
156 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
157 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
|
158 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
159 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
|
160 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
161 if (! quiet) |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
162 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
|
163 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
|
164 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
165 nr = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
166 nc = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
167 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
168 goto done; |
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 } |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
171 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
172 beg = buf.find_first_not_of (", \t", end); |
4634 | 173 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
174 if (beg == std::string::npos || (buf[beg] == '\r' && |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
175 beg == buf.length () - 1)) |
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 // We had a line with trailing spaces and |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
178 // 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
|
179 // not a new colum. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
180 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
181 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
182 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
183 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
184 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
185 } |
4634 | 186 |
187 if (tmp_nc > 0) | |
10315
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 if (nc == 0) |
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 nc = tmp_nc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
192 nr++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
193 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
194 else if (nc == tmp_nc) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
195 nr++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
196 else |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
197 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
198 if (! quiet) |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
199 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
|
200 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
|
201 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
202 nr = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
203 nc = 0; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
204 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
205 goto done; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
206 } |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
207 } |
4634 | 208 } |
209 | |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
210 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
|
211 error ("load: file '%s' seems to be empty!", filename.c_str ()); |
4634 | 212 |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
213 done: |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
214 |
4634 | 215 is.clear (); |
4643 | 216 is.seekg (pos); |
4634 | 217 } |
218 | |
219 // Extract a matrix from a file of numbers only. | |
220 // | |
221 // Comments are not allowed. The file should only have numeric values. | |
222 // | |
223 // Reads the file twice. Once to find the number of rows and columns, | |
224 // and once to extract the matrix. | |
225 // | |
226 // FILENAME is used for error messages. | |
227 // | |
228 // This format provides no way to tag the data as global. | |
229 | |
230 std::string | |
231 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
|
232 octave_value& tc) |
4634 | 233 { |
234 std::string retval; | |
235 | |
236 std::string varname; | |
237 | |
238 size_t pos = filename.rfind ('/'); | |
239 | |
8021 | 240 if (pos != std::string::npos) |
4634 | 241 varname = filename.substr (pos+1); |
242 else | |
243 varname = filename; | |
244 | |
4867 | 245 pos = varname.rfind ('.'); |
4634 | 246 |
8021 | 247 if (pos != std::string::npos) |
4634 | 248 varname = varname.substr (0, pos); |
249 | |
250 size_t len = varname.length (); | |
251 for (size_t i = 0; i < len; i++) | |
252 { | |
253 char c = varname[i]; | |
254 if (! (isalnum (c) || c == '_')) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
255 varname[i] = '_'; |
4634 | 256 } |
257 | |
4867 | 258 if (is_keyword (varname) || ! isalpha (varname[0])) |
4634 | 259 varname.insert (0, "X"); |
260 | |
261 if (valid_identifier (varname)) | |
262 { | |
5275 | 263 octave_idx_type nr = 0; |
264 octave_idx_type nc = 0; | |
4634 | 265 |
266 int total_count = 0; | |
267 | |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
268 get_lines_and_columns (is, nr, nc, filename); |
4634 | 269 |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
270 octave_quit (); |
4634 | 271 |
272 if (! error_state && nr > 0 && nc > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
273 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
274 Matrix tmp (nr, nc); |
4634 | 275 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
276 if (nr < 1 || nc < 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
277 is.clear (std::ios::badbit); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
278 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
279 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
280 double d; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
281 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
|
282 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
283 std::string buf = get_mat_data_input_line (is); |
4634 | 284 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
285 std::istringstream tmp_stream (buf); |
4634 | 286 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
287 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
|
288 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
289 octave_quit (); |
4634 | 290 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
291 d = octave_read_value<double> (tmp_stream); |
4634 | 292 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
293 if (tmp_stream || tmp_stream.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
294 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
295 tmp.elem (i, j) = d; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
296 total_count++; |
4634 | 297 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
298 // Skip whitespace and commas. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
299 char c; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
300 while (1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
301 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
302 tmp_stream >> c; |
4634 | 303 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
304 if (! tmp_stream) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
305 break; |
4634 | 306 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
307 if (! (c == ' ' || c == '\t' || c == ',')) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
308 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
309 tmp_stream.putback (c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
310 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
311 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
312 } |
4634 | 313 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
314 if (tmp_stream.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
315 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
316 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
317 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
318 { |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
319 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
|
320 filename.c_str ()); |
4634 | 321 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
322 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
323 } |
4634 | 324 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
325 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
326 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
327 } |
4634 | 328 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
329 if (is || is.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
330 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
331 // FIXME -- not sure this is best, but it works. |
4634 | 332 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
333 if (is.eof ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
334 is.clear (); |
4634 | 335 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
336 octave_idx_type expected = nr * nc; |
4634 | 337 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
338 if (expected == total_count) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
339 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
340 tc = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
341 retval = varname; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
342 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
343 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
344 error ("load: expected %d elements, found %d", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
345 expected, total_count); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
346 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
347 else |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
348 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
|
349 filename.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
350 } |
4634 | 351 else |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
352 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
|
353 filename.c_str ()); |
4634 | 354 } |
355 else | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
356 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
|
357 filename.c_str ()); |
4634 | 358 |
359 return retval; | |
360 } | |
361 | |
5938 | 362 bool |
363 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
|
364 int precision, bool tabs) |
5938 | 365 { |
366 bool success = true; | |
367 | |
368 if (val.is_complex_type ()) | |
369 warning ("save: omitting imaginary part for ASCII file"); | |
370 | |
371 Matrix m = val.matrix_value (true); | |
372 | |
373 if (error_state) | |
374 { | |
375 success = false; | |
376 | |
377 error_state = 0; | |
378 } | |
379 else | |
5951 | 380 { |
381 long old_precision = os.precision (); | |
382 | |
383 os.precision (precision); | |
5938 | 384 |
5951 | 385 std::ios::fmtflags oflags |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
386 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific)); |
5951 | 387 |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
388 if (tabs) |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
389 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
390 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
|
391 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
392 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
|
393 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
394 // Omit leading tabs. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
395 if (j != 0) os << '\t'; |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
396 octave_write_double (os, m (i, j)); |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
397 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
398 os << "\n"; |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
399 } |
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 else |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
402 os << m; |
5951 | 403 |
404 os.flags (oflags); | |
405 | |
406 os.precision (old_precision); | |
407 } | |
5938 | 408 |
409 return (os && success); | |
410 } | |
16555
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
411 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
412 bool |
17191
ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
16892
diff
changeset
|
413 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
|
414 { |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
415 bool retval = false; |
17191
ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
16892
diff
changeset
|
416 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
|
417 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
|
418 |
17191
ca5103ab0b21
check_gzip_magic before get_file_format (wrong type detection, bug #39652)
Andreas Weber <andy.weber.aw@gmail.com>
parents:
16892
diff
changeset
|
419 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
|
420 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
|
421 |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
422 return retval; |
04fb96f4bea1
allow double-click in file browser to load data files
John W. Eaton <jwe@octave.org>
parents:
15467
diff
changeset
|
423 } |