GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ls-mat-ascii.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2017 John W. Eaton
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
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
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
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (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>
34 #include <sstream>
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 "errwarn.h"
52 #include "interpreter.h"
53 #include "lex.h"
54 #include "load-save.h"
55 #include "ls-ascii-helper.h"
56 #include "ls-mat-ascii.h"
57 #include "ovl.h"
58 #include "oct-map.h"
59 #include "ov-cell.h"
60 #include "pager.h"
61 #include "pt-exp.h"
62 #include "sysdep.h"
63 #include "unwind-prot.h"
64 #include "utils.h"
65 #include "variables.h"
66 #include "version.h"
67 #include "dMatrix.h"
68 
69 static std::string
70 get_mat_data_input_line (std::istream& is)
71 {
73 
74  bool have_data = false;
75 
76  do
77  {
78  retval = "";
79 
80  char c;
81  while (is.get (c))
82  {
83  if (c == '\n' || c == '\r')
84  {
85  is.putback (c);
87  break;
88  }
89 
90  if (c == '%' || c == '#')
91  {
92  skip_until_newline (is, false);
93  break;
94  }
95 
96  if (! is.eof ())
97  {
98  if (! have_data && c != ' ' && c != '\t')
99  have_data = true;
100 
101  retval += c;
102  }
103  }
104  }
105  while (! (have_data || is.eof ()));
106 
107  return retval;
108 }
109 
110 static void
111 get_lines_and_columns (std::istream& is,
113  const std::string& filename = "",
114  bool quiet = false, bool check_numeric = false)
115 {
116  std::streampos pos = is.tellg ();
117 
118  int file_line_number = 0;
119 
120  nr = 0;
121  nc = 0;
122 
123  while (is)
124  {
125  octave_quit ();
126 
128 
129  file_line_number++;
130 
131  size_t beg = buf.find_first_not_of (", \t");
132 
133  // If we see a CR as the last character in the buffer, we had a
134  // CRLF pair as the line separator. Any other CR in the text
135  // will not be considered as whitespace.
136 
137  if (beg != std::string::npos && buf[beg] == '\r'
138  && beg == buf.length () - 1)
139  {
140  // We had a blank line ending with a CRLF. Handle it the
141  // same as an empty line.
142  beg = std::string::npos;
143  }
144 
145  octave_idx_type tmp_nc = 0;
146 
147  while (beg != std::string::npos)
148  {
149  tmp_nc++;
150 
151  size_t end = buf.find_first_of (", \t", beg);
152 
153  if (end != std::string::npos)
154  {
155  if (check_numeric)
156  {
157  std::istringstream tmp_stream (buf.substr (beg, end-beg));
158 
159  octave_read_double (tmp_stream);
160 
161  if (tmp_stream.fail ())
162  {
163  if (! quiet)
164  error ("load: %s: non-numeric data found near line %d",
165  filename.c_str (), file_line_number);
166 
167  nr = 0;
168  nc = 0;
169 
170  goto done;
171  }
172  }
173 
174  beg = buf.find_first_not_of (", \t", end);
175 
176  if (beg == std::string::npos
177  || (buf[beg] == '\r' && beg == buf.length () - 1))
178  {
179  // We had a line with trailing spaces and
180  // ending with a CRLF, so this should look like EOL,
181  // not a new colum.
182  break;
183  }
184  }
185  else
186  break;
187  }
188 
189  if (tmp_nc > 0)
190  {
191  if (nc == 0)
192  {
193  nc = tmp_nc;
194  nr++;
195  }
196  else if (nc == tmp_nc)
197  nr++;
198  else
199  {
200  if (! quiet)
201  error ("load: %s: inconsistent number of columns near line %d",
202  filename.c_str (), file_line_number);
203 
204  nr = 0;
205  nc = 0;
206 
207  goto done;
208  }
209  }
210  }
211 
212  if (! quiet && (nr == 0 || nc == 0))
213  error ("load: file '%s' seems to be empty!", filename.c_str ());
214 
215 done:
216 
217  is.clear ();
218  is.seekg (pos);
219 }
220 
221 // Extract a matrix from a file of numbers only.
222 //
223 // Comments are not allowed. The file should only have numeric values.
224 //
225 // Reads the file twice. Once to find the number of rows and columns,
226 // and once to extract the matrix.
227 //
228 // FILENAME is used for error messages.
229 //
230 // This format provides no way to tag the data as global.
231 
234  octave_value& tc)
235 {
236  std::string varname;
237 
238  size_t pos = filename.rfind ('/');
239 
240  if (pos != std::string::npos)
241  varname = filename.substr (pos+1);
242  else
243  varname = filename;
244 
245  pos = varname.rfind ('.');
246 
247  if (pos != std::string::npos)
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 == '_'))
255  varname[i] = '_';
256  }
257 
258  if (octave::is_keyword (varname) || ! isalpha (varname[0]))
259  varname.insert (0, "X");
260 
261  if (! valid_identifier (varname))
262  error ("load: unable to convert filename '%s' to valid identifier",
263  filename.c_str ());
264 
265  octave_idx_type nr = 0;
266  octave_idx_type nc = 0;
267 
268  int total_count = 0;
269 
270  get_lines_and_columns (is, nr, nc, filename);
271 
272  octave_quit ();
273 
274  if (nr <= 0 || nc <= 0)
275  error ("load: unable to extract matrix size from file '%s'",
276  filename.c_str ());
277 
278  Matrix tmp (nr, nc);
279 
280  if (nr < 1 || nc < 1)
281  is.clear (std::ios::badbit);
282  else
283  {
284  double d;
285  for (octave_idx_type i = 0; i < nr; i++)
286  {
288 
289  std::istringstream tmp_stream (buf);
290 
291  for (octave_idx_type j = 0; j < nc; j++)
292  {
293  octave_quit ();
294 
295  d = octave_read_value<double> (tmp_stream);
296 
297  if (! tmp_stream && ! tmp_stream.eof ())
298  error ("load: failed to read matrix from file '%s'",
299  filename.c_str ());
300 
301  tmp.elem (i, j) = d;
302  total_count++;
303 
304  // Skip whitespace and commas.
305  char c;
306  while (1)
307  {
308  tmp_stream >> c;
309 
310  if (! tmp_stream)
311  break;
312 
313  if (! (c == ' ' || c == '\t' || c == ','))
314  {
315  tmp_stream.putback (c);
316  break;
317  }
318  }
319 
320  if (tmp_stream.eof ())
321  break;
322  }
323  }
324  }
325 
326  if (! is && ! is.eof ())
327  error ("load: failed to read matrix from file '%s'",
328  filename.c_str ());
329 
330  // FIXME: not sure this is best, but it works.
331 
332  if (is.eof ())
333  is.clear ();
334 
335  octave_idx_type expected = nr * nc;
336 
337  if (expected != total_count)
338  error ("load: expected %d elements, found %d",
339  expected, total_count);
340 
341  tc = tmp;
342 
343  return varname;
344 }
345 
346 bool
347 save_mat_ascii_data (std::ostream& os, const octave_value& val,
348  int precision, bool tabs)
349 {
350  bool success = true;
351 
352  if (val.is_complex_type ())
353  warning ("save: omitting imaginary part for ASCII file");
354 
355  Matrix m;
356 
357  try
358  {
359  m = val.matrix_value (true);
360  }
361  catch (const octave::execution_exception& e)
362  {
364 
365  success = false;
366  }
367 
368  if (success)
369  {
370  long old_precision = os.precision ();
371 
372  os.precision (precision);
373 
374  std::ios::fmtflags oflags
375  = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific));
376 
377  if (tabs)
378  {
379  for (octave_idx_type i = 0; i < m.rows (); i++)
380  {
381  for (octave_idx_type j = 0; j < m.cols (); j++)
382  {
383  // Omit leading tabs.
384  if (j != 0) os << '\t';
385  octave_write_double (os, m (i, j));
386  }
387  os << "\n";
388  }
389  }
390  else
391  os << m;
392 
393  os.flags (oflags);
394 
395  os.precision (old_precision);
396  }
397 
398  return (os && success);
399 }
400 
401 bool
403 {
404  bool retval = false;
405  octave_idx_type nr = 0;
406  octave_idx_type nc = 0;
407 
408  get_lines_and_columns (is, nr, nc, filename, true, true);
409  retval = (nr != 0 && nc != 0);
410 
411  return retval;
412 }
void octave_write_double(std::ostream &os, double d)
Definition: lo-utils.cc:388
double octave_read_double(std::istream &is)
Definition: lo-utils.h:105
std::string read_mat_ascii_data(std::istream &is, const std::string &filename, octave_value &tc)
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)
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
void skip_until_newline(std::istream &is, bool keep_newline)
bool looks_like_mat_ascii_file(std::istream &is, const std::string &filename)
void error(const char *fmt,...)
Definition: error.cc:570
std::string filename
Definition: urlwrite.cc:340
void skip_preceeding_newline(std::istream &is)
i e
Definition: data.cc:2724
octave_idx_type rows(void) const
Definition: Array.h:401
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
bool save_mat_ascii_data(std::ostream &os, const octave_value &val, int precision, bool tabs)
done
Definition: syscalls.cc:248
bool valid_identifier(const char *s)
Definition: utils.cc:74
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
bool is_complex_type(void) const
Definition: ov.h:670
double tmp
Definition: data.cc:6300
octave_value retval
Definition: data.cc:6294
Definition: dMatrix.h:37
bool is_keyword(const std::string &s)
Definition: lex.cc:4529
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:787
static std::string get_mat_data_input_line(std::istream &is)
Definition: ls-mat-ascii.cc:70
void warning(const char *fmt,...)
Definition: error.cc:788
void recover_from_exception(void)
Definition: interpreter.cc:200
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
octave_idx_type cols(void) const
Definition: Array.h:409
write the output to stdout if nargout is
Definition: load-save.cc:1576
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854