3 Copyright (C) 1996, 1997, 2003, 2004, 2005, 2006, 2007 John W. Eaton
5 This file is part of Octave.
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.
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
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/>.
23 // Author: John W. Eaton.
38 #include "byte-swap.h"
39 #include "data-conv.h"
41 #include "glob-match.h"
42 #include "lo-mappers.h"
43 #include "mach-info.h"
53 #include "load-save.h"
61 #include "unwind-prot.h"
63 #include "variables.h"
67 #include "ls-oct-ascii.h"
69 // The number of decimal digits to use when writing ascii data.
70 static int Vsave_precision = 16;
72 // Functions for reading ascii data.
74 // Extract a KEYWORD and its value from stream IS, returning the
75 // associated value in a new string.
77 // Input should look something like:
79 // [%#][ \t]*keyword[ \t]*:[ \t]*string-value[ \t]*\n
82 extract_keyword (std::istream& is, const char *keyword, const bool next_only)
89 if (c == '%' || c == '#')
91 std::ostringstream buf;
93 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
94 ; // Skip whitespace and comment characters.
99 while (is.get (c) && isalpha (c))
102 std::string tmp = buf.str ();
103 bool match = (tmp.compare (0, strlen (keyword), keyword) == 0);
107 std::ostringstream value;
108 while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
109 ; // Skip whitespace and the colon.
112 retval = read_until_newline (is, false);
119 while (is.get (c) && c != '\n' && c != '\r')
120 ; // Skip to end of line.
125 int len = retval.length ();
133 if (c == ' ' || c == '\t')
146 // Extract one value (scalar, matrix, string, etc.) from stream IS and
147 // place it in TC, returning the name of the variable. If the value
148 // is tagged as global in the file, return TRUE in GLOBAL.
150 // Each type supplies its own function to load the data, and so this
151 // function is extensible.
153 // FILENAME is used for error messages.
155 // The data is expected to be in the following format:
157 // The input file must have a header followed by some data.
159 // All lines in the header must begin with a `#' character.
161 // The header must contain a list of keyword and value pairs with the
162 // keyword and value separated by a colon.
164 // Keywords must appear in the following order:
170 // Where, for the built in types are:
172 // <name> : a valid identifier
174 // <type> : <typename>
175 // | global <typename>
177 // <typename> : scalar
186 // <info> : <matrix info>
189 // <matrix info> : # rows: <integer>
190 // : # columns: <integer>
192 // <string info> : # elements: <integer>
193 // : # length: <integer> (once before each string)
195 // For backward compatibility the type "string array" is treated as a
196 // "string" type. Also "string" can have a single element with no elements
199 // <string info> : # length: <integer>
201 // Formatted ASCII data follows the header.
228 // FIXME -- this format is fairly rigid, and doesn't allow for
229 // arbitrary comments. Someone should fix that. It does allow arbitrary
232 // Ugh. The signature of the compare method is not standard in older
233 // versions of the GNU libstdc++. Do this instead:
235 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t)
238 read_ascii_data (std::istream& is, const std::string& filename, bool& global,
239 octave_value& tc, octave_idx_type count)
241 // Read name for this entry or break on EOF.
243 std::string name = extract_keyword (is, "name");
248 error ("load: empty name keyword or no data found in file `%s'",
251 return std::string ();
254 if (name == CELL_ELT_TAG)
256 // This is OK -- name won't be used.
258 else if (! valid_identifier (name))
260 error ("load: bogus identifier `%s' found in file `%s'",
261 name.c_str (), filename.c_str ());
262 return std::string ();
265 // Look for type keyword.
267 std::string tag = extract_keyword (is, "type");
272 size_t pos = tag.rfind (' ');
276 global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global");
278 typ = global ? tag.substr (7) : tag;
283 // Special case for backward compatiablity. A small bit of cruft
284 if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array"))
285 tc = octave_value (charMatrix (), true);
287 tc = octave_value_typeinfo::lookup_type (typ);
289 if (! tc.load_ascii (is))
290 error ("load: trouble reading ascii file `%s'", filename.c_str ());
293 error ("load: failed to extract keyword specifying value type");
297 error ("load: reading file %s", filename.c_str ());
298 return std::string ();
304 // Save the data from TC along with the corresponding NAME, and global
305 // flag MARK_AS_GLOBAL on stream OS in the plain text format described
306 // above for load_ascii_data. If NAME is empty, the name: line is not
307 // generated. PRECISION specifies the number of decimal digits to print.
309 // Assumes ranges and strings cannot contain Inf or NaN values.
311 // Returns 1 for success and 0 for failure.
313 // FIXME -- should probably write the help string here too.
316 save_ascii_data (std::ostream& os, const octave_value& val_arg,
317 const std::string& name, bool mark_as_global,
323 os << "# name: " << name << "\n";
325 octave_value val = val_arg;
328 os << "# type: global " << val.type_name () << "\n";
330 os << "# type: " << val.type_name() << "\n";
333 precision = Vsave_precision;
335 long old_precision = os.precision ();
336 os.precision (precision);
338 success = val.save_ascii (os);
340 os.precision (old_precision);
342 return (os && success);
346 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t,
347 const std::string& name)
349 return save_ascii_data (os, t, name, false, 6);
352 // Maybe this should be a static function in tree-plot.cc?
354 // If TC is matrix, save it on stream OS in a format useful for
355 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is
356 // TRUE, assume a parametric 3-dimensional plot will be generated.
359 save_three_d (std::ostream& os, const octave_value& tc, bool parametric)
363 octave_idx_type nr = tc.rows ();
364 octave_idx_type nc = tc.columns ();
366 if (tc.is_real_matrix ())
368 os << "# 3D data...\n"
369 << "# type: matrix\n"
370 << "# total rows: " << nr << "\n"
371 << "# total columns: " << nc << "\n";
373 long old_precision = os.precision ();
378 octave_idx_type extras = nc % 3;
380 warning ("ignoring last %d columns", extras);
382 Matrix tmp = tc.matrix_value ();
385 for (octave_idx_type i = 0; i < nc-extras; i += 3)
387 os << tmp.extract (0, i, nr-1, i+2);
394 Matrix tmp = tc.matrix_value ();
397 for (octave_idx_type i = 0; i < nc; i++)
399 os << tmp.extract (0, i, nr-1, i);
405 os.precision (old_precision);
409 ::error ("for now, I can only save real matrices in 3D format");
413 return (os && ! fail);
416 DEFUN (save_precision, args, nargout,
418 @deftypefn {Built-in Function} {@var{val} =} save_precision ()\n\
419 @deftypefnx {Built-in Function} {@var{old_val} =} save_precision (@var{new_val})\n\
420 Query or set the internal variable that specifies the number of\n\
421 digits to keep when saving data in text format.\n\
424 return SET_INTERNAL_VARIABLE_WITH_LIMITS (save_precision, -1, INT_MAX);
428 ;;; Local Variables: ***