1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29
30 #include <cctype>
31
32 #include <iomanip>
33 #include <istream>
34 #include <ostream>
35 #include <sstream>
36 #include <string>
37
38 #include "byte-swap.h"
39 #include "dMatrix.h"
40 #include "data-conv.h"
41 #include "file-ops.h"
42 #include "glob-match.h"
43 #include "lo-mappers.h"
44 #include "mach-info.h"
45 #include "oct-env.h"
46 #include "oct-time.h"
47 #include "quit.h"
48
49 #include "Cell.h"
50 #include "defun.h"
51 #include "error.h"
52 #include "interpreter-private.h"
53 #include "interpreter.h"
54 #include "lex.h"
55 #include "load-save.h"
56 #include "ls-ascii-helper.h"
57 #include "ls-mat-ascii.h"
58 #include "oct-map.h"
59 #include "ov-cell.h"
60 #include "ov.h"
61 #include "pager.h"
62 #include "sysdep.h"
63 #include "utils.h"
64 #include "variables.h"
65 #include "version.h"
66
67 static std::string
get_mat_data_input_line(std::istream & is)68 get_mat_data_input_line (std::istream& is)
69 {
70 std::string retval;
71
72 bool have_data = false;
73
74 do
75 {
76 retval = "";
77
78 char c;
79 while (is.get (c))
80 {
81 if (c == '\n' || c == '\r')
82 {
83 is.putback (c);
84 skip_preceeding_newline (is);
85 break;
86 }
87
88 if (c == '%' || c == '#')
89 {
90 skip_until_newline (is, false);
91 break;
92 }
93
94 if (! is.eof ())
95 {
96 if (! have_data && c != ' ' && c != '\t')
97 have_data = true;
98
99 retval += c;
100 }
101 }
102 }
103 while (! (have_data || is.eof () || is.fail ()));
104
105 return retval;
106 }
107
108 static void
get_lines_and_columns(std::istream & is,octave_idx_type & nr,octave_idx_type & nc,const std::string & filename="",bool quiet=false,bool check_numeric=false)109 get_lines_and_columns (std::istream& is,
110 octave_idx_type& nr, octave_idx_type& nc,
111 const std::string& filename = "",
112 bool quiet = false, bool check_numeric = false)
113 {
114 std::streampos pos = is.tellg ();
115
116 int file_line_number = 0;
117
118 nr = 0;
119 nc = 0;
120
121 while (is)
122 {
123 octave_quit ();
124
125 std::string buf = get_mat_data_input_line (is);
126
127 file_line_number++;
128
129 std::size_t beg = buf.find_first_not_of (", \t");
130
131 // If we see a CR as the last character in the buffer, we had a
132 // CRLF pair as the line separator. Any other CR in the text
133 // will not be considered as whitespace.
134
135 if (beg != std::string::npos && buf[beg] == '\r'
136 && beg == buf.length () - 1)
137 {
138 // We had a blank line ending with a CRLF. Handle it the
139 // same as an empty line.
140 beg = std::string::npos;
141 }
142
143 octave_idx_type tmp_nc = 0;
144
145 while (beg != std::string::npos)
146 {
147 tmp_nc++;
148
149 std::size_t end = buf.find_first_of (", \t", beg);
150
151 if (end != std::string::npos)
152 {
153 if (check_numeric)
154 {
155 std::istringstream tmp_stream (buf.substr (beg, end-beg));
156
157 octave_read_double (tmp_stream);
158
159 if (tmp_stream.fail ())
160 {
161 if (! quiet)
162 error ("load: %s: non-numeric data found near line %d",
163 filename.c_str (), file_line_number);
164
165 nr = 0;
166 nc = 0;
167
168 goto done;
169 }
170 }
171
172 beg = buf.find_first_not_of (", \t", end);
173
174 if (beg == std::string::npos
175 || (buf[beg] == '\r' && beg == buf.length () - 1))
176 {
177 // We had a line with trailing spaces and ending with a CRLF,
178 // so this should look like EOL, not a new column.
179 break;
180 }
181 }
182 else
183 break;
184 }
185
186 if (tmp_nc > 0)
187 {
188 if (nc == 0)
189 {
190 nc = tmp_nc;
191 nr++;
192 }
193 else if (nc == tmp_nc)
194 nr++;
195 else
196 {
197 if (! quiet)
198 error ("load: %s: inconsistent number of columns near line %d",
199 filename.c_str (), file_line_number);
200
201 nr = 0;
202 nc = 0;
203
204 goto done;
205 }
206 }
207 }
208
209 if (! quiet && (nr == 0 || nc == 0))
210 error ("load: file '%s' seems to be empty!", filename.c_str ());
211
212 done:
213
214 is.clear ();
215 is.seekg (pos);
216 }
217
218 // Extract a matrix from a file of numbers only.
219 //
220 // Comments are not allowed. The file should only have numeric values.
221 //
222 // Reads the file twice. Once to find the number of rows and columns,
223 // and once to extract the matrix.
224 //
225 // FILENAME is used for error messages.
226 //
227 // This format provides no way to tag the data as global.
228
229 std::string
read_mat_ascii_data(std::istream & is,const std::string & filename,octave_value & tc)230 read_mat_ascii_data (std::istream& is, const std::string& filename,
231 octave_value& tc)
232 {
233 std::string varname;
234
235 std::size_t pos = filename.rfind ('/');
236
237 if (pos != std::string::npos)
238 varname = filename.substr (pos+1);
239 else
240 varname = filename;
241
242 pos = varname.rfind ('.');
243
244 if (pos != std::string::npos)
245 varname = varname.substr (0, pos);
246
247 std::size_t len = varname.length ();
248 for (std::size_t i = 0; i < len; i++)
249 {
250 char c = varname[i];
251 if (! (isalnum (c) || c == '_'))
252 varname[i] = '_';
253 }
254
255 if (octave::iskeyword (varname) || ! isalpha (varname[0]))
256 varname.insert (0, "X");
257
258 if (! octave::valid_identifier (varname))
259 error ("load: unable to convert filename '%s' to valid identifier",
260 filename.c_str ());
261
262 octave_idx_type nr = 0;
263 octave_idx_type nc = 0;
264
265 octave_idx_type total_count = 0;
266
267 get_lines_and_columns (is, nr, nc, filename);
268
269 octave_quit ();
270
271 if (nr <= 0 || nc <= 0)
272 error ("load: unable to extract matrix size from file '%s'",
273 filename.c_str ());
274
275 Matrix tmp (nr, nc);
276
277 double d;
278 for (octave_idx_type i = 0; i < nr; i++)
279 {
280 std::string buf = get_mat_data_input_line (is);
281
282 std::istringstream tmp_stream (buf);
283
284 for (octave_idx_type j = 0; j < nc; j++)
285 {
286 octave_quit ();
287
288 d = octave_read_value<double> (tmp_stream);
289
290 if (! tmp_stream && ! tmp_stream.eof ())
291 error ("load: failed to read matrix from file '%s'",
292 filename.c_str ());
293
294 tmp.elem (i, j) = d;
295 total_count++;
296
297 // Skip whitespace and commas.
298 char c;
299 while (1)
300 {
301 tmp_stream >> c;
302
303 if (! tmp_stream)
304 break;
305
306 if (! (c == ' ' || c == '\t' || c == ','))
307 {
308 tmp_stream.putback (c);
309 break;
310 }
311 }
312
313 if (tmp_stream.eof ())
314 break;
315 }
316 }
317
318 if (! is && ! is.eof ())
319 error ("load: failed to read matrix from file '%s'", filename.c_str ());
320
321 // FIXME: not sure this is best, but it works.
322 if (is.eof ())
323 is.clear ();
324
325 octave_idx_type expected = nr * nc;
326
327 if (expected != total_count)
328 error ("load: expected %" OCTAVE_IDX_TYPE_FORMAT " elements, found "
329 "%" OCTAVE_IDX_TYPE_FORMAT, expected, total_count);
330
331 tc = tmp;
332
333 return varname;
334 }
335
336 bool
save_mat_ascii_data(std::ostream & os,const octave_value & val,int precision,bool tabs)337 save_mat_ascii_data (std::ostream& os, const octave_value& val,
338 int precision, bool tabs)
339 {
340 bool success = true;
341
342 if (val.iscomplex ())
343 warning ("save: omitting imaginary part for ASCII file");
344
345 if (val.ndims () > 2)
346 {
347 warning ("save: skipping variable which is not a 2-D matrix");
348 return true;
349 }
350
351 Matrix m;
352
353 try
354 {
355 m = val.matrix_value (true);
356 }
357 catch (const octave::execution_exception& e)
358 {
359 octave::interpreter& interp
360 = octave::__get_interpreter__ ("save_mat_ascii_data");
361
362 interp.recover_from_exception ();
363
364 success = false;
365 }
366
367 if (success)
368 {
369 long old_precision = os.precision ();
370
371 os.precision (precision);
372
373 std::ios::fmtflags oflags
374 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific));
375
376 if (tabs)
377 {
378 for (octave_idx_type i = 0; i < m.rows (); i++)
379 {
380 for (octave_idx_type j = 0; j < m.cols (); j++)
381 {
382 // Omit leading tabs.
383 if (j != 0) os << '\t';
384 octave_write_double (os, m(i, j));
385 }
386 os << "\n";
387 }
388 }
389 else
390 os << m;
391
392 // Restore format
393 os.flags (oflags);
394 os.precision (old_precision);
395 }
396
397 return (os && success);
398 }
399
400 bool
looks_like_mat_ascii_file(std::istream & is,const std::string & filename)401 looks_like_mat_ascii_file (std::istream& is, const std::string& filename)
402 {
403 bool retval = false;
404 octave_idx_type nr = 0;
405 octave_idx_type nc = 0;
406
407 get_lines_and_columns (is, nr, nc, filename, true, true);
408 retval = (nr != 0 && nc != 0);
409
410 return retval;
411 }
412