4634
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4634
|
21 |
|
22 */ |
|
23 |
|
24 // Author: John W. Eaton. |
|
25 |
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include <config.h> |
|
28 #endif |
|
29 |
|
30 #include <cstring> |
|
31 #include <cctype> |
|
32 |
|
33 #include <fstream> |
|
34 #include <iomanip> |
|
35 #include <iostream> |
5765
|
36 #include <sstream> |
4634
|
37 #include <string> |
|
38 |
|
39 #include "byte-swap.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 #include "str-vec.h" |
|
49 |
|
50 #include "Cell.h" |
|
51 #include "defun.h" |
|
52 #include "error.h" |
|
53 #include "gripes.h" |
|
54 #include "load-save.h" |
|
55 #include "oct-obj.h" |
|
56 #include "oct-map.h" |
|
57 #include "ov-cell.h" |
|
58 #include "pager.h" |
|
59 #include "pt-exp.h" |
|
60 #include "symtab.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 #include "ls-oct-ascii.h" |
|
69 |
|
70 // The number of decimal digits to use when writing ascii data. |
5951
|
71 static int Vsave_precision = 16; |
4634
|
72 |
|
73 // Functions for reading ascii data. |
|
74 |
|
75 static Matrix |
|
76 strip_infnan (const Matrix& m) |
|
77 { |
5275
|
78 octave_idx_type nr = m.rows (); |
|
79 octave_idx_type nc = m.columns (); |
4634
|
80 |
|
81 Matrix retval (nr, nc); |
|
82 |
5275
|
83 octave_idx_type k = 0; |
|
84 for (octave_idx_type i = 0; i < nr; i++) |
4634
|
85 { |
5275
|
86 for (octave_idx_type j = 0; j < nc; j++) |
4634
|
87 { |
|
88 double d = m (i, j); |
|
89 if (xisnan (d)) |
|
90 goto next_row; |
|
91 else |
|
92 retval (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
93 } |
|
94 k++; |
|
95 |
|
96 next_row: |
|
97 continue; |
|
98 } |
|
99 |
|
100 if (k > 0) |
|
101 retval.resize (k, nc); |
|
102 |
|
103 return retval; |
|
104 } |
|
105 |
|
106 // Extract a KEYWORD and its value from stream IS, returning the |
|
107 // associated value in a new string. |
|
108 // |
|
109 // Input should look something like: |
|
110 // |
|
111 // [%#][ \t]*keyword[ \t]*:[ \t]*string-value[ \t]*\n |
|
112 |
|
113 std::string |
4687
|
114 extract_keyword (std::istream& is, const char *keyword, const bool next_only) |
4634
|
115 { |
|
116 std::string retval; |
|
117 |
|
118 char c; |
|
119 while (is.get (c)) |
|
120 { |
|
121 if (c == '%' || c == '#') |
|
122 { |
5765
|
123 std::ostringstream buf; |
4634
|
124 |
|
125 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
126 ; // Skip whitespace and comment characters. |
|
127 |
|
128 if (isalpha (c)) |
|
129 buf << c; |
|
130 |
|
131 while (is.get (c) && isalpha (c)) |
|
132 buf << c; |
|
133 |
5765
|
134 std::string tmp = buf.str (); |
5679
|
135 bool match = (tmp.compare (0, strlen (keyword), keyword) == 0); |
4634
|
136 |
|
137 if (match) |
|
138 { |
5765
|
139 std::ostringstream value; |
4634
|
140 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
141 ; // Skip whitespace and the colon. |
|
142 |
|
143 if (c != '\n') |
|
144 { |
|
145 value << c; |
|
146 while (is.get (c) && c != '\n') |
|
147 value << c; |
|
148 } |
5765
|
149 |
|
150 retval = value.str (); |
4634
|
151 break; |
|
152 } |
4687
|
153 else if (next_only) |
|
154 break; |
4634
|
155 } |
|
156 } |
|
157 |
|
158 int len = retval.length (); |
|
159 |
|
160 if (len > 0) |
|
161 { |
|
162 while (len) |
|
163 { |
|
164 c = retval[len-1]; |
|
165 |
|
166 if (c == ' ' || c == '\t') |
|
167 len--; |
|
168 else |
|
169 { |
|
170 retval.resize (len); |
|
171 break; |
|
172 } |
|
173 } |
|
174 } |
|
175 |
|
176 return retval; |
|
177 } |
|
178 |
|
179 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
180 // place it in TC, returning the name of the variable. If the value |
|
181 // is tagged as global in the file, return TRUE in GLOBAL. |
|
182 // |
4687
|
183 // Each type supplies its own function to load the data, and so this |
|
184 // function is extensible. |
|
185 // |
4634
|
186 // FILENAME is used for error messages. |
|
187 // |
|
188 // The data is expected to be in the following format: |
|
189 // |
|
190 // The input file must have a header followed by some data. |
|
191 // |
|
192 // All lines in the header must begin with a `#' character. |
|
193 // |
|
194 // The header must contain a list of keyword and value pairs with the |
|
195 // keyword and value separated by a colon. |
|
196 // |
|
197 // Keywords must appear in the following order: |
|
198 // |
|
199 // # name: <name> |
|
200 // # type: <type> |
|
201 // # <info> |
|
202 // |
4687
|
203 // Where, for the built in types are: |
4634
|
204 // |
|
205 // <name> : a valid identifier |
|
206 // |
|
207 // <type> : <typename> |
|
208 // | global <typename> |
|
209 // |
|
210 // <typename> : scalar |
|
211 // | complex scalar |
|
212 // | matrix |
|
213 // | complex matrix |
4687
|
214 // | bool |
|
215 // | bool matrix |
4634
|
216 // | string |
|
217 // | range |
|
218 // |
|
219 // <info> : <matrix info> |
|
220 // | <string info> |
|
221 // |
|
222 // <matrix info> : # rows: <integer> |
|
223 // : # columns: <integer> |
|
224 // |
4687
|
225 // <string info> : # elements: <integer> |
|
226 // : # length: <integer> (once before each string) |
4634
|
227 // |
4687
|
228 // For backward compatibility the type "string array" is treated as a |
|
229 // "string" type. Also "string" can have a single element with no elements |
|
230 // line such that |
|
231 // |
|
232 // <string info> : # length: <integer> |
4634
|
233 // |
|
234 // Formatted ASCII data follows the header. |
|
235 // |
|
236 // Example: |
|
237 // |
|
238 // # name: foo |
|
239 // # type: matrix |
|
240 // # rows: 2 |
|
241 // # columns: 2 |
|
242 // 2 4 |
|
243 // 1 3 |
|
244 // |
|
245 // Example: |
|
246 // |
|
247 // # name: foo |
4687
|
248 // # type: string |
4634
|
249 // # elements: 5 |
|
250 // # length: 4 |
|
251 // this |
|
252 // # length: 2 |
|
253 // is |
|
254 // # length: 1 |
|
255 // a |
|
256 // # length: 6 |
|
257 // string |
|
258 // # length: 5 |
|
259 // array |
|
260 // |
5775
|
261 // FIXME -- this format is fairly rigid, and doesn't allow for |
4687
|
262 // arbitrary comments. Someone should fix that. It does allow arbitrary |
|
263 // types however. |
4634
|
264 |
|
265 // Ugh. The signature of the compare method is not standard in older |
|
266 // versions of the GNU libstdc++. Do this instead: |
|
267 |
|
268 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t) |
|
269 |
|
270 std::string |
|
271 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
5754
|
272 octave_value& tc, octave_idx_type count) |
4634
|
273 { |
|
274 // Read name for this entry or break on EOF. |
|
275 |
|
276 std::string name = extract_keyword (is, "name"); |
|
277 |
|
278 if (name.empty ()) |
|
279 { |
|
280 if (count == 0) |
|
281 error ("load: empty name keyword or no data found in file `%s'", |
|
282 filename.c_str ()); |
|
283 |
|
284 return std::string (); |
|
285 } |
|
286 |
|
287 if (name == CELL_ELT_TAG) |
|
288 { |
|
289 // This is OK -- name won't be used. |
|
290 } |
|
291 else if (! valid_identifier (name)) |
|
292 { |
|
293 error ("load: bogus identifier `%s' found in file `%s'", |
|
294 name.c_str (), filename.c_str ()); |
|
295 return std::string (); |
|
296 } |
|
297 |
|
298 // Look for type keyword. |
|
299 |
|
300 std::string tag = extract_keyword (is, "type"); |
|
301 |
|
302 if (! tag.empty ()) |
|
303 { |
|
304 std::string typ; |
|
305 size_t pos = tag.rfind (' '); |
|
306 |
|
307 if (pos != NPOS) |
|
308 { |
|
309 global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global"); |
|
310 |
|
311 typ = global ? tag.substr (7) : tag; |
|
312 } |
|
313 else |
|
314 typ = tag; |
|
315 |
4687
|
316 // Special case for backward compatiablity. A small bit of cruft |
|
317 if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array")) |
|
318 tc = octave_value (charMatrix (), true); |
|
319 else |
|
320 tc = octave_value_typeinfo::lookup_type (typ); |
4634
|
321 |
4988
|
322 if (! tc.load_ascii (is)) |
|
323 error ("load: trouble reading ascii file `%s'", filename.c_str ()); |
4634
|
324 } |
|
325 else |
|
326 error ("load: failed to extract keyword specifying value type"); |
|
327 |
|
328 if (error_state) |
|
329 { |
|
330 error ("load: reading file %s", filename.c_str ()); |
|
331 return std::string (); |
|
332 } |
|
333 |
|
334 return name; |
|
335 } |
|
336 |
|
337 // Save the data from TC along with the corresponding NAME, and global |
|
338 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
|
339 // above for load_ascii_data. If NAME is empty, the name: line is not |
|
340 // generated. PRECISION specifies the number of decimal digits to print. |
|
341 // If STRIP_NAN_AND_INF is TRUE, rows containing NaNs are deleted, |
|
342 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
343 // but not so big that gnuplot can't handle it when trying to compute |
|
344 // axis ranges, etc.). |
|
345 // |
|
346 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
347 // |
|
348 // Returns 1 for success and 0 for failure. |
|
349 |
5775
|
350 // FIXME -- should probably write the help string here too. |
4634
|
351 |
|
352 bool |
|
353 save_ascii_data (std::ostream& os, const octave_value& val_arg, |
|
354 const std::string& name, bool& infnan_warned, |
|
355 bool strip_nan_and_inf, bool mark_as_global, |
|
356 int precision) |
|
357 { |
|
358 bool success = true; |
|
359 |
|
360 if (! name.empty ()) |
|
361 os << "# name: " << name << "\n"; |
|
362 |
|
363 octave_value val = val_arg; |
|
364 |
4687
|
365 if (mark_as_global) |
|
366 os << "# type: global " << val.type_name () << "\n"; |
|
367 else |
|
368 os << "# type: " << val.type_name() << "\n"; |
4634
|
369 |
5951
|
370 if (! precision) |
|
371 precision = Vsave_precision; |
|
372 |
|
373 long old_precision = os.precision (); |
|
374 os.precision (precision); |
|
375 |
|
376 success = val . save_ascii (os, infnan_warned, strip_nan_and_inf); |
4634
|
377 |
|
378 os.precision (old_precision); |
|
379 |
|
380 return (os && success); |
|
381 } |
|
382 |
|
383 bool |
|
384 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
|
385 const std::string& name) |
|
386 { |
|
387 bool infnan_warned = true; |
|
388 |
5576
|
389 return save_ascii_data (os, t, name, infnan_warned, false, false, 0); |
4634
|
390 } |
|
391 |
|
392 // Maybe this should be a static function in tree-plot.cc? |
|
393 |
|
394 // If TC is matrix, save it on stream OS in a format useful for |
|
395 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
396 // TRUE, assume a parametric 3-dimensional plot will be generated. |
|
397 |
|
398 bool |
|
399 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) |
|
400 { |
|
401 bool fail = false; |
|
402 |
5275
|
403 octave_idx_type nr = tc.rows (); |
|
404 octave_idx_type nc = tc.columns (); |
4634
|
405 |
|
406 if (tc.is_real_matrix ()) |
|
407 { |
|
408 os << "# 3D data...\n" |
|
409 << "# type: matrix\n" |
|
410 << "# total rows: " << nr << "\n" |
|
411 << "# total columns: " << nc << "\n"; |
|
412 |
|
413 if (parametric) |
|
414 { |
5275
|
415 octave_idx_type extras = nc % 3; |
4634
|
416 if (extras) |
|
417 warning ("ignoring last %d columns", extras); |
|
418 |
|
419 Matrix tmp = tc.matrix_value (); |
|
420 nr = tmp.rows (); |
|
421 |
5275
|
422 for (octave_idx_type i = 0; i < nc-extras; i += 3) |
4634
|
423 { |
|
424 os << tmp.extract (0, i, nr-1, i+2); |
|
425 if (i+3 < nc-extras) |
|
426 os << "\n"; |
|
427 } |
|
428 } |
|
429 else |
|
430 { |
|
431 Matrix tmp = tc.matrix_value (); |
|
432 nr = tmp.rows (); |
|
433 |
5275
|
434 for (octave_idx_type i = 0; i < nc; i++) |
4634
|
435 { |
|
436 os << tmp.extract (0, i, nr-1, i); |
|
437 if (i+1 < nc) |
|
438 os << "\n"; |
|
439 } |
|
440 } |
|
441 } |
|
442 else |
|
443 { |
|
444 ::error ("for now, I can only save real matrices in 3D format"); |
|
445 fail = true; |
|
446 } |
|
447 |
|
448 return (os && ! fail); |
|
449 } |
|
450 |
5794
|
451 DEFUN (save_precision, args, nargout, |
|
452 "-*- texinfo -*-\n\ |
|
453 @deftypefn {Built-in Function} {@var{val} =} save_precision ()\n\ |
|
454 @deftypefnx {Built-in Function} {@var{old_val} =} save_precision (@var{new_val})\n\ |
|
455 Query or set the internal variable that specifies the number of\n\ |
|
456 digits to keep when saving data in text format.\n\ |
|
457 @end deftypefn") |
4634
|
458 { |
5794
|
459 return SET_INTERNAL_VARIABLE_WITH_LIMITS (save_precision, -1, INT_MAX); |
4634
|
460 } |
|
461 |
|
462 /* |
|
463 ;;; Local Variables: *** |
|
464 ;;; mode: C++ *** |
|
465 ;;; End: *** |
|
466 */ |
|
467 |