GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ls-mat-ascii.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 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
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);
85  break;
86  }
87 
88  if (c == '%' || c == '#')
89  {
90  octave::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
109 get_lines_and_columns (std::istream& is,
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_value<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
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
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&)
358  {
359  octave::interpreter& interp = octave::__get_interpreter__ ();
360 
361  interp.recover_from_exception ();
362 
363  success = false;
364  }
365 
366  if (success)
367  {
368  long old_precision = os.precision ();
369 
370  os.precision (precision);
371 
372  std::ios::fmtflags oflags
373  = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific));
374 
375  if (tabs)
376  {
377  for (octave_idx_type i = 0; i < m.rows (); i++)
378  {
379  for (octave_idx_type j = 0; j < m.cols (); j++)
380  {
381  // Omit leading tabs.
382  if (j != 0) os << '\t';
383  octave::write_value<double> (os, m(i, j));
384  }
385  os << "\n";
386  }
387  }
388  else
389  os << m;
390 
391  // Restore format
392  os.flags (oflags);
393  os.precision (old_precision);
394  }
395 
396  return (os && success);
397 }
398 
399 bool
400 looks_like_mat_ascii_file (std::istream& is, const std::string& filename)
401 {
402  bool retval = false;
403  octave_idx_type nr = 0;
404  octave_idx_type nc = 0;
405 
406  get_lines_and_columns (is, nr, nc, filename, true, true);
407  retval = (nr != 0 && nc != 0);
408 
409  return retval;
410 }
T & elem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:562
Definition: dMatrix.h:42
int ndims() const
Definition: ov.h:551
bool iscomplex() const
Definition: ov.h:741
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:853
void warning(const char *fmt,...)
Definition: error.cc:1063
void() error(const char *fmt,...)
Definition: error.cc:988
interpreter & __get_interpreter__()
bool iskeyword(const std::string &s)
Definition: lex.cc:1335
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
void skip_until_newline(std::istream &is, bool keep_newline)
void skip_preceeding_newline(std::istream &is)
bool save_mat_ascii_data(std::ostream &os, const octave_value &val, int precision, bool tabs)
std::string read_mat_ascii_data(std::istream &is, const std::string &filename, octave_value &tc)
bool looks_like_mat_ascii_file(std::istream &is, const std::string &filename)
T octave_idx_type m
Definition: mx-inlines.cc:781
bool valid_identifier(const char *s)
Definition: utils.cc:79
F77_RET_T len
Definition: xerbla.cc:61