GNU Octave  4.0.0
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-2015 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 #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>
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 "gripes.h"
52 #include "lex.h"
53 #include "load-save.h"
54 #include "ls-ascii-helper.h"
55 #include "ls-mat-ascii.h"
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))
81  {
82  if (c == '\n' || c == '\r')
83  {
84  is.putback (c);
86  break;
87  }
88 
89  if (c == '%' || c == '#')
90  {
91  skip_until_newline (is, false);
92  break;
93  }
94 
95  if (! is.eof ())
96  {
97  if (! have_data && c != ' ' && c != '\t')
98  have_data = true;
99 
100  retval += c;
101  }
102  }
103  }
104  while (! (have_data || is.eof ()));
105 
106  return retval;
107 }
108 
109 static void
110 get_lines_and_columns (std::istream& is,
112  const std::string& filename = std::string (),
113  bool quiet = false, bool check_numeric = false)
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  {
124  octave_quit ();
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 
136  if (beg != std::string::npos && buf[beg] == '\r'
137  && beg == buf.length () - 1)
138  {
139  // We had a blank line ending with a CRLF. Handle it the
140  // same as an empty line.
141  beg = std::string::npos;
142  }
143 
144  octave_idx_type tmp_nc = 0;
145 
146  while (beg != std::string::npos)
147  {
148  tmp_nc++;
149 
150  size_t end = buf.find_first_of (", \t", beg);
151 
152  if (end != std::string::npos)
153  {
154  if (check_numeric)
155  {
156  std::istringstream tmp_stream (buf.substr (beg, end-beg));
157 
158  octave_read_double (tmp_stream);
159 
160  if (tmp_stream.fail ())
161  {
162  if (! quiet)
163  error ("load: %s: non-numeric data found near line %d",
164  filename.c_str (), file_line_number);
165 
166  nr = 0;
167  nc = 0;
168 
169  goto done;
170  }
171  }
172 
173  beg = buf.find_first_not_of (", \t", end);
174 
175  if (beg == std::string::npos
176  || (buf[beg] == '\r' && beg == buf.length () - 1))
177  {
178  // We had a line with trailing spaces and
179  // ending with a CRLF, so this should look like EOL,
180  // not a new colum.
181  break;
182  }
183  }
184  else
185  break;
186  }
187 
188  if (tmp_nc > 0)
189  {
190  if (nc == 0)
191  {
192  nc = tmp_nc;
193  nr++;
194  }
195  else if (nc == tmp_nc)
196  nr++;
197  else
198  {
199  if (! quiet)
200  error ("load: %s: inconsistent number of columns near line %d",
201  filename.c_str (), file_line_number);
202 
203  nr = 0;
204  nc = 0;
205 
206  goto done;
207  }
208  }
209  }
210 
211  if (! quiet && (nr == 0 || nc == 0))
212  error ("load: file '%s' seems to be empty!", filename.c_str ());
213 
214 done:
215 
216  is.clear ();
217  is.seekg (pos);
218 }
219 
220 // Extract a matrix from a file of numbers only.
221 //
222 // Comments are not allowed. The file should only have numeric values.
223 //
224 // Reads the file twice. Once to find the number of rows and columns,
225 // and once to extract the matrix.
226 //
227 // FILENAME is used for error messages.
228 //
229 // This format provides no way to tag the data as global.
230 
231 std::string
232 read_mat_ascii_data (std::istream& is, const std::string& filename,
233  octave_value& tc)
234 {
235  std::string retval;
236 
237  std::string varname;
238 
239  size_t pos = filename.rfind ('/');
240 
241  if (pos != std::string::npos)
242  varname = filename.substr (pos+1);
243  else
244  varname = filename;
245 
246  pos = varname.rfind ('.');
247 
248  if (pos != std::string::npos)
249  varname = varname.substr (0, pos);
250 
251  size_t len = varname.length ();
252  for (size_t i = 0; i < len; i++)
253  {
254  char c = varname[i];
255  if (! (isalnum (c) || c == '_'))
256  varname[i] = '_';
257  }
258 
259  if (is_keyword (varname) || ! isalpha (varname[0]))
260  varname.insert (0, "X");
261 
262  if (valid_identifier (varname))
263  {
264  octave_idx_type nr = 0;
265  octave_idx_type nc = 0;
266 
267  int total_count = 0;
268 
269  get_lines_and_columns (is, nr, nc, filename);
270 
271  octave_quit ();
272 
273  if (! error_state && nr > 0 && nc > 0)
274  {
275  Matrix tmp (nr, nc);
276 
277  if (nr < 1 || nc < 1)
278  is.clear (std::ios::badbit);
279  else
280  {
281  double d;
282  for (octave_idx_type i = 0; i < nr; i++)
283  {
284  std::string buf = get_mat_data_input_line (is);
285 
286  std::istringstream tmp_stream (buf);
287 
288  for (octave_idx_type j = 0; j < nc; j++)
289  {
290  octave_quit ();
291 
292  d = octave_read_value<double> (tmp_stream);
293 
294  if (tmp_stream || tmp_stream.eof ())
295  {
296  tmp.elem (i, j) = d;
297  total_count++;
298 
299  // Skip whitespace and commas.
300  char c;
301  while (1)
302  {
303  tmp_stream >> c;
304 
305  if (! tmp_stream)
306  break;
307 
308  if (! (c == ' ' || c == '\t' || c == ','))
309  {
310  tmp_stream.putback (c);
311  break;
312  }
313  }
314 
315  if (tmp_stream.eof ())
316  break;
317  }
318  else
319  {
320  error ("load: failed to read matrix from file '%s'",
321  filename.c_str ());
322 
323  return retval;
324  }
325 
326  }
327  }
328  }
329 
330  if (is || is.eof ())
331  {
332  // FIXME: not sure this is best, but it works.
333 
334  if (is.eof ())
335  is.clear ();
336 
337  octave_idx_type expected = nr * nc;
338 
339  if (expected == total_count)
340  {
341  tc = tmp;
342  retval = varname;
343  }
344  else
345  error ("load: expected %d elements, found %d",
346  expected, total_count);
347  }
348  else
349  error ("load: failed to read matrix from file '%s'",
350  filename.c_str ());
351  }
352  else
353  error ("load: unable to extract matrix size from file '%s'",
354  filename.c_str ());
355  }
356  else
357  error ("load: unable to convert filename '%s' to valid identifier",
358  filename.c_str ());
359 
360  return retval;
361 }
362 
363 bool
364 save_mat_ascii_data (std::ostream& os, const octave_value& val,
365  int precision, bool tabs)
366 {
367  bool success = true;
368 
369  if (val.is_complex_type ())
370  warning ("save: omitting imaginary part for ASCII file");
371 
372  Matrix m = val.matrix_value (true);
373 
374  if (error_state)
375  {
376  success = false;
377 
378  error_state = 0;
379  }
380  else
381  {
382  long old_precision = os.precision ();
383 
384  os.precision (precision);
385 
386  std::ios::fmtflags oflags
387  = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific));
388 
389  if (tabs)
390  {
391  for (octave_idx_type i = 0; i < m.rows (); i++)
392  {
393  for (octave_idx_type j = 0; j < m.cols (); j++)
394  {
395  // Omit leading tabs.
396  if (j != 0) os << '\t';
397  octave_write_double (os, m (i, j));
398  }
399  os << "\n";
400  }
401  }
402  else
403  os << m;
404 
405  os.flags (oflags);
406 
407  os.precision (old_precision);
408  }
409 
410  return (os && success);
411 }
412 
413 bool
414 looks_like_mat_ascii_file (std::istream& is, const std::string& filename)
415 {
416  bool retval = false;
417  octave_idx_type nr = 0;
418  octave_idx_type nc = 0;
419 
420  get_lines_and_columns (is, nr, nc, filename, true, true);
421  retval = (nr != 0 && nc != 0);
422 
423  return retval;
424 }
void octave_write_double(std::ostream &os, double d)
Definition: lo-utils.cc:386
double octave_read_double(std::istream &is)
Definition: lo-utils.h:103
std::string read_mat_ascii_data(std::istream &is, const std::string &filename, octave_value &tc)
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:476
void skip_preceeding_newline(std::istream &is)
T & elem(octave_idx_type n)
Definition: Array.h:380
bool is_keyword(const std::string &s)
Definition: lex.cc:4522
octave_idx_type rows(void) const
Definition: Array.h:313
F77_RET_T const double const double double * d
bool save_mat_ascii_data(std::ostream &os, const octave_value &val, int precision, bool tabs)
bool valid_identifier(const char *s)
Definition: utils.cc:77
int error_state
Definition: error.cc:101
bool is_complex_type(void) const
Definition: ov.h:654
Definition: dMatrix.h:35
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:773
static std::string get_mat_data_input_line(std::istream &is)
Definition: ls-mat-ascii.cc:69
void warning(const char *fmt,...)
Definition: error.cc:681
octave_idx_type cols(void) const
Definition: Array.h:321
static void get_lines_and_columns(std::istream &is, octave_idx_type &nr, octave_idx_type &nc, const std::string &filename=std::string(), bool quiet=false, bool check_numeric=false)