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-oct-text.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 // Author: John W. Eaton.
24 
25 #if defined (HAVE_CONFIG_H)
26 # include "config.h"
27 #endif
28 
29 #include <cstring>
30 #include <cctype>
31 
32 #include <fstream>
33 #include <iomanip>
34 #include <iostream>
35 #include <sstream>
36 #include <string>
37 
38 #include "byte-swap.h"
39 #include "data-conv.h"
40 #include "file-ops.h"
41 #include "glob-match.h"
42 #include "lo-mappers.h"
43 #include "mach-info.h"
44 #include "oct-env.h"
45 #include "oct-time.h"
46 #include "quit.h"
47 #include "str-vec.h"
48 
49 #include "Cell.h"
50 #include "defun.h"
51 #include "error.h"
52 #include "errwarn.h"
53 #include "load-save.h"
54 #include "ls-ascii-helper.h"
55 #include "ls-oct-text.h"
56 #include "ovl.h"
57 #include "oct-map.h"
58 #include "ov-cell.h"
59 #include "pager.h"
60 #include "pt-exp.h"
61 #include "unwind-prot.h"
62 #include "utils.h"
63 #include "variables.h"
64 #include "version.h"
65 #include "dMatrix.h"
66 
67 // The number of decimal digits to use when writing ascii data.
68 static int Vsave_precision = 16;
69 
70 // Functions for reading octave format text data.
71 
72 // Extract a KEYWORD and its value from stream IS, returning the
73 // associated value in a new string.
74 //
75 // Input should look something like:
76 //
77 // [%#][ \t]*keyword[ \t]*:[ \t]*string-value[ \t]*\n
78 
80 extract_keyword (std::istream& is, const char *keyword, const bool next_only)
81 {
83 
84  int ch = is.peek ();
85  if (next_only && ch != '%' && ch != '#')
86  return retval;
87 
88  char c;
89  while (is.get (c))
90  {
91  if (c == '%' || c == '#')
92  {
93  std::ostringstream buf;
94 
95  while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
96  ; // Skip whitespace and comment characters.
97 
98  if (isalpha (c))
99  buf << c;
100 
101  while (is.get (c) && isalpha (c))
102  buf << c;
103 
104  std::string tmp = buf.str ();
105  bool match = (tmp.substr (0, strlen (keyword)) == keyword);
106 
107  if (match)
108  {
109  std::ostringstream value;
110  while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
111  ; // Skip whitespace and the colon.
112 
113  is.putback (c);
114  retval = read_until_newline (is, false);
115  break;
116  }
117  else if (next_only)
118  break;
119  else
120  skip_until_newline (is, false);
121  }
122  }
123 
124  int len = retval.length ();
125 
126  if (len > 0)
127  {
128  while (len)
129  {
130  c = retval[len-1];
131 
132  if (c == ' ' || c == '\t')
133  len--;
134  else
135  {
136  retval.resize (len);
137  break;
138  }
139  }
140  }
141 
142  return retval;
143 }
144 
145 // Extract one value (scalar, matrix, string, etc.) from stream IS and
146 // place it in TC, returning the name of the variable. If the value
147 // is tagged as global in the file, return TRUE in GLOBAL.
148 //
149 // Each type supplies its own function to load the data, and so this
150 // function is extensible.
151 //
152 // FILENAME is used for error messages.
153 //
154 // The data is expected to be in the following format:
155 //
156 // The input file must have a header followed by some data.
157 //
158 // All lines in the header must begin with a '#' character.
159 //
160 // The header must contain a list of keyword and value pairs with the
161 // keyword and value separated by a colon.
162 //
163 // Keywords must appear in the following order:
164 //
165 // # name: <name>
166 // # type: <type>
167 // # <info>
168 //
169 // Where, for the built in types are:
170 //
171 // <name> : a valid identifier
172 //
173 // <type> : <typename>
174 // | global <typename>
175 //
176 // <typename> : scalar
177 // | complex scalar
178 // | matrix
179 // | complex matrix
180 // | bool
181 // | bool matrix
182 // | string
183 // | range
184 //
185 // <info> : <matrix info>
186 // | <string info>
187 //
188 // <matrix info> : # rows: <integer>
189 // : # columns: <integer>
190 //
191 // <string info> : # elements: <integer>
192 // : # length: <integer> (once before each string)
193 //
194 // For backward compatibility the type "string array" is treated as a
195 // "string" type. Also "string" can have a single element with no elements
196 // line such that
197 //
198 // <string info> : # length: <integer>
199 //
200 // Formatted ASCII data follows the header.
201 //
202 // Example:
203 //
204 // # name: foo
205 // # type: matrix
206 // # rows: 2
207 // # columns: 2
208 // 2 4
209 // 1 3
210 //
211 // Example:
212 //
213 // # name: foo
214 // # type: string
215 // # elements: 5
216 // # length: 4
217 // this
218 // # length: 2
219 // is
220 // # length: 1
221 // a
222 // # length: 6
223 // string
224 // # length: 5
225 // array
226 //
227 // FIXME: This format is fairly rigid, and doesn't allow for arbitrary comments.
228 // Someone should fix that. It does allow arbitrary types however.
229 
230 // Ugh. The signature of the compare method is not standard in older
231 // versions of the GNU libstdc++. Do this instead:
232 
233 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t)
234 
236 read_text_data (std::istream& is, const std::string& filename, bool& global,
237  octave_value& tc, octave_idx_type count)
238 {
239  // Read name for this entry or break on EOF.
240 
241  std::string name = extract_keyword (is, "name");
242 
243  if (name.empty ())
244  {
245  if (count == 0)
246  error ("load: empty name keyword or no data found in file '%s'",
247  filename.c_str ());
248 
249  return "";
250  }
251 
252  if (! (name == ".nargin." || name == ".nargout."
253  || name == CELL_ELT_TAG || valid_identifier (name)))
254  error ("load: invalid identifier '%s' found in file '%s'",
255  name.c_str (), filename.c_str ());
256 
257  // Look for type keyword.
258 
259  std::string tag = extract_keyword (is, "type");
260 
261  if (tag.empty ())
262  error ("load: failed to extract keyword specifying value type");
263 
264  std::string typ;
265  size_t pos = tag.rfind (' ');
266 
267  if (pos != std::string::npos)
268  {
269  global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global");
270 
271  typ = global ? tag.substr (7) : tag;
272  }
273  else
274  typ = tag;
275 
276  // Special case for backward compatibility. A small bit of cruft
277  if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array"))
278  tc = charMatrix ();
279  else
281 
282  if (! tc.load_ascii (is))
283  error ("load: trouble reading ascii file '%s'", filename.c_str ());
284 
285  return name;
286 }
287 
288 // Save the data from TC along with the corresponding NAME, and global
289 // flag MARK_AS_GLOBAL on stream OS in the plain text format described
290 // above for load_text_data. If NAME is empty, the name: line is not
291 // generated. PRECISION specifies the number of decimal digits to print.
292 //
293 // Assumes ranges and strings cannot contain Inf or NaN values.
294 //
295 // Returns 1 for success and 0 for failure.
296 
297 // FIXME: should probably write the help string here too.
298 
299 bool
300 save_text_data (std::ostream& os, const octave_value& val_arg,
301  const std::string& name, bool mark_as_global,
302  int precision)
303 {
304  bool success = true;
305 
306  if (! name.empty ())
307  os << "# name: " << name << "\n";
308 
309  octave_value val = val_arg;
310 
311  if (mark_as_global)
312  os << "# type: global " << val.type_name () << "\n";
313  else
314  os << "# type: " << val.type_name () << "\n";
315 
316  if (! precision)
317  precision = Vsave_precision;
318 
319  long old_precision = os.precision ();
320  os.precision (precision);
321 
322  success = val.save_ascii (os);
323 
324  // Insert an extra pair of newline characters after the data so that
325  // multiple data elements may be handled separately by gnuplot (see
326  // the description of the index qualifier for the plot command in the
327  // gnuplot documentation).
328  os << "\n\n";
329 
330  os.precision (old_precision);
331 
332  return (os && success);
333 }
334 
335 bool
336 save_text_data_for_plotting (std::ostream& os, const octave_value& t,
337  const std::string& name)
338 {
339  return save_text_data (os, t, name, false, 6);
340 }
341 
342 // Maybe this should be a static function in tree-plot.cc?
343 
344 // If TC is matrix, save it on stream OS in a format useful for
345 // making a 3-D plot with gnuplot. If PARAMETRIC is TRUE,
346 // assume a parametric 3-D plot will be generated.
347 
348 bool
349 save_three_d (std::ostream& os, const octave_value& tc, bool parametric)
350 {
351  bool fail = false;
352 
353  octave_idx_type nr = tc.rows ();
354  octave_idx_type nc = tc.columns ();
355 
356  if (! tc.is_real_matrix ())
357  error ("for now, I can only save real matrices in 3-D format");
358 
359  os << "# 3-D data...\n"
360  << "# type: matrix\n"
361  << "# total rows: " << nr << "\n"
362  << "# total columns: " << nc << "\n";
363 
364  long old_precision = os.precision ();
365  os.precision (6);
366 
367  if (parametric)
368  {
369  octave_idx_type extras = nc % 3;
370  if (extras)
371  warning ("ignoring last %d columns", extras);
372 
373  Matrix tmp = tc.matrix_value ();
374  nr = tmp.rows ();
375 
376  for (octave_idx_type i = 0; i < nc-extras; i += 3)
377  {
378  os << tmp.extract (0, i, nr-1, i+2);
379  if (i+3 < nc-extras)
380  os << "\n";
381  }
382  }
383  else
384  {
385  Matrix tmp = tc.matrix_value ();
386  nr = tmp.rows ();
387 
388  for (octave_idx_type i = 0; i < nc; i++)
389  {
390  os << tmp.extract (0, i, nr-1, i);
391  if (i+1 < nc)
392  os << "\n";
393  }
394  }
395 
396  os.precision (old_precision);
397 
398  return (os && ! fail);
399 }
400 
401 DEFUN (save_precision, args, nargout,
402  doc: /* -*- texinfo -*-
403 @deftypefn {} {@var{val} =} save_precision ()
404 @deftypefnx {} {@var{old_val} =} save_precision (@var{new_val})
405 @deftypefnx {} {} save_precision (@var{new_val}, "local")
406 Query or set the internal variable that specifies the number of digits to
407 keep when saving data in text format.
408 
409 When called from inside a function with the @qcode{"local"} option, the
410 variable is changed locally for the function and any subroutines it calls.
411 The original variable value is restored when exiting the function.
412 @end deftypefn */)
413 {
414  return SET_INTERNAL_VARIABLE_WITH_LIMITS (save_precision, -1,
416 }
std::string read_until_newline(std::istream &is, bool keep_newline)
#define SET_INTERNAL_VARIABLE_WITH_LIMITS(NM, MINVAL, MAXVAL)
Definition: variables.h:132
bool save_text_data_for_plotting(std::ostream &os, const octave_value &t, const std::string &name)
Definition: ls-oct-text.cc:336
octave_idx_type rows(void) const
Definition: ov.h:489
Matrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
Definition: dMatrix.cc:398
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)
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
void error(const char *fmt,...)
Definition: error.cc:570
std::string filename
Definition: urlwrite.cc:340
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:935
static int Vsave_precision
Definition: ls-oct-text.cc:68
octave_idx_type rows(void) const
Definition: Array.h:401
#define SUBSTRING_COMPARE_EQ(s, pos, n, t)
Definition: ls-oct-text.cc:233
JNIEnv void * args
Definition: ov-java.cc:67
static octave_value lookup_type(const std::string &nm)
Definition: ov-typeinfo.h:104
octave_idx_type columns(void) const
Definition: ov.h:491
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function xample nargout(@histc)
Definition: ov-usr-fcn.cc:935
bool save_text_data(std::ostream &os, const octave_value &val_arg, const std::string &name, bool mark_as_global, int precision)
Definition: ls-oct-text.cc:300
bool valid_identifier(const char *s)
Definition: utils.cc:74
std::string extract_keyword(std::istream &is, const char *keyword, const bool next_only)
Definition: ls-oct-text.cc:80
double tmp
Definition: data.cc:6300
octave_value retval
Definition: data.cc:6294
Definition: dMatrix.h:37
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
T::size_type strlen(const typename T::value_type *str)
Definition: oct-string.cc:75
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:787
bool save_ascii(std::ostream &os)
Definition: ov.h:1277
void warning(const char *fmt,...)
Definition: error.cc:788
std::string type_name(void) const
Definition: ov.h:1232
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:228
#define CELL_ELT_TAG
Definition: ls-oct-text.h:38
bool load_ascii(std::istream &is)
Definition: ov.h:1279
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the IEEE symbol zero divided by nd tex zero divided by nd ifnottex and any operation involving another NaN value(5+NaN).Note that NaN always compares not equal to NaN(NaN!
std::string read_text_data(std::istream &is, const std::string &filename, bool &global, octave_value &tc, octave_idx_type count)
Definition: ls-oct-text.cc:236
bool save_three_d(std::ostream &os, const octave_value &tc, bool parametric)
Definition: ls-oct-text.cc:349
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
bool is_real_matrix(void) const
Definition: ov.h:554