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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 // Author: John W. Eaton. |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
|
26 #include <config.h> |
|
27 #endif |
|
28 |
|
29 #include <cfloat> |
|
30 #include <cstring> |
|
31 #include <cctype> |
|
32 |
|
33 #include <fstream> |
|
34 #include <iomanip> |
|
35 #include <iostream> |
|
36 #include <memory> |
|
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 "lo-sstream.h" |
|
45 #include "mach-info.h" |
|
46 #include "oct-env.h" |
|
47 #include "oct-time.h" |
|
48 #include "quit.h" |
|
49 #include "str-vec.h" |
|
50 |
|
51 #include "Cell.h" |
|
52 #include "defun.h" |
|
53 #include "error.h" |
|
54 #include "gripes.h" |
|
55 #include "load-save.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 "symtab.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 #include "ls-oct-ascii.h" |
|
70 |
|
71 // The number of decimal digits to use when writing ascii data. |
|
72 static int Vsave_precision; |
|
73 |
|
74 #define CELL_ELT_TAG "<cell-element>" |
|
75 |
|
76 // Used when converting Inf to something that gnuplot can read. |
|
77 |
|
78 #ifndef OCT_RBV |
|
79 #define OCT_RBV DBL_MAX / 100.0 |
|
80 #endif |
|
81 |
|
82 // Functions for reading ascii data. |
|
83 |
|
84 static void |
|
85 ascii_save_type (std::ostream& os, const char *type, bool mark_as_global) |
|
86 { |
|
87 if (mark_as_global) |
|
88 os << "# type: global "; |
|
89 else |
|
90 os << "# type: "; |
|
91 |
|
92 os << type << "\n"; |
|
93 } |
|
94 |
|
95 static Matrix |
|
96 strip_infnan (const Matrix& m) |
|
97 { |
|
98 int nr = m.rows (); |
|
99 int nc = m.columns (); |
|
100 |
|
101 Matrix retval (nr, nc); |
|
102 |
|
103 int k = 0; |
|
104 for (int i = 0; i < nr; i++) |
|
105 { |
|
106 for (int j = 0; j < nc; j++) |
|
107 { |
|
108 double d = m (i, j); |
|
109 if (xisnan (d)) |
|
110 goto next_row; |
|
111 else |
|
112 retval (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
113 } |
|
114 k++; |
|
115 |
|
116 next_row: |
|
117 continue; |
|
118 } |
|
119 |
|
120 if (k > 0) |
|
121 retval.resize (k, nc); |
|
122 |
|
123 return retval; |
|
124 } |
|
125 |
|
126 static ComplexMatrix |
|
127 strip_infnan (const ComplexMatrix& m) |
|
128 { |
|
129 int nr = m.rows (); |
|
130 int nc = m.columns (); |
|
131 |
|
132 ComplexMatrix retval (nr, nc); |
|
133 |
|
134 int k = 0; |
|
135 for (int i = 0; i < nr; i++) |
|
136 { |
|
137 for (int j = 0; j < nc; j++) |
|
138 { |
|
139 Complex c = m (i, j); |
|
140 if (xisnan (c)) |
|
141 goto next_row; |
|
142 else |
|
143 { |
|
144 double re = real (c); |
|
145 double im = imag (c); |
|
146 |
|
147 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
148 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
149 |
|
150 retval (k, j) = Complex (re, im); |
|
151 } |
|
152 } |
|
153 k++; |
|
154 |
|
155 next_row: |
|
156 continue; |
|
157 } |
|
158 |
|
159 if (k > 0) |
|
160 retval.resize (k, nc); |
|
161 |
|
162 return retval; |
|
163 } |
|
164 |
|
165 // Skip white space and comments on stream IS. |
|
166 |
|
167 static void |
|
168 skip_comments (std::istream& is) |
|
169 { |
|
170 char c = '\0'; |
|
171 while (is.get (c)) |
|
172 { |
|
173 if (c == ' ' || c == '\t' || c == '\n') |
|
174 ; // Skip whitespace on way to beginning of next line. |
|
175 else |
|
176 break; |
|
177 } |
|
178 |
|
179 for (;;) |
|
180 { |
|
181 if (is && (c == '%' || c == '#')) |
|
182 while (is.get (c) && c != '\n') |
|
183 ; // Skip to beginning of next line, ignoring everything. |
|
184 else |
|
185 break; |
|
186 } |
|
187 } |
|
188 |
|
189 // Extract a KEYWORD and its value from stream IS, returning the |
|
190 // associated value in a new string. |
|
191 // |
|
192 // Input should look something like: |
|
193 // |
|
194 // [%#][ \t]*keyword[ \t]*:[ \t]*string-value[ \t]*\n |
|
195 |
|
196 std::string |
|
197 extract_keyword (std::istream& is, const char *keyword) |
|
198 { |
|
199 std::string retval; |
|
200 |
|
201 char c; |
|
202 while (is.get (c)) |
|
203 { |
|
204 if (c == '%' || c == '#') |
|
205 { |
|
206 OSSTREAM buf; |
|
207 |
|
208 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
209 ; // Skip whitespace and comment characters. |
|
210 |
|
211 if (isalpha (c)) |
|
212 buf << c; |
|
213 |
|
214 while (is.get (c) && isalpha (c)) |
|
215 buf << c; |
|
216 |
|
217 buf << OSSTREAM_ENDS; |
|
218 const char *tmp = OSSTREAM_C_STR (buf); |
|
219 OSSTREAM_FREEZE (buf); |
|
220 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
221 |
|
222 if (match) |
|
223 { |
|
224 OSSTREAM value; |
|
225 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
226 ; // Skip whitespace and the colon. |
|
227 |
|
228 if (c != '\n') |
|
229 { |
|
230 value << c; |
|
231 while (is.get (c) && c != '\n') |
|
232 value << c; |
|
233 } |
|
234 value << OSSTREAM_ENDS; |
|
235 retval = OSSTREAM_STR (value); |
|
236 OSSTREAM_FREEZE (value); |
|
237 break; |
|
238 } |
|
239 } |
|
240 } |
|
241 |
|
242 int len = retval.length (); |
|
243 |
|
244 if (len > 0) |
|
245 { |
|
246 while (len) |
|
247 { |
|
248 c = retval[len-1]; |
|
249 |
|
250 if (c == ' ' || c == '\t') |
|
251 len--; |
|
252 else |
|
253 { |
|
254 retval.resize (len); |
|
255 break; |
|
256 } |
|
257 } |
|
258 } |
|
259 |
|
260 return retval; |
|
261 } |
|
262 |
|
263 // Match KEYWORD on stream IS, placing the associated value in VALUE, |
|
264 // returning TRUE if successful and FALSE otherwise. |
|
265 // |
|
266 // Input should look something like: |
|
267 // |
|
268 // [%#][ \t]*keyword[ \t]*int-value.*\n |
|
269 |
|
270 static bool |
|
271 extract_keyword (std::istream& is, const char *keyword, int& value) |
|
272 { |
|
273 bool status = false; |
|
274 value = 0; |
|
275 |
|
276 char c; |
|
277 while (is.get (c)) |
|
278 { |
|
279 if (c == '%' || c == '#') |
|
280 { |
|
281 OSSTREAM buf; |
|
282 |
|
283 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
284 ; // Skip whitespace and comment characters. |
|
285 |
|
286 if (isalpha (c)) |
|
287 buf << c; |
|
288 |
|
289 while (is.get (c) && isalpha (c)) |
|
290 buf << c; |
|
291 |
|
292 buf << OSSTREAM_ENDS; |
|
293 const char *tmp = OSSTREAM_C_STR (buf); |
|
294 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
295 OSSTREAM_FREEZE (buf); |
|
296 |
|
297 if (match) |
|
298 { |
|
299 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
300 ; // Skip whitespace and the colon. |
|
301 |
|
302 is.putback (c); |
|
303 if (c != '\n') |
|
304 is >> value; |
|
305 if (is) |
|
306 status = true; |
|
307 while (is.get (c) && c != '\n') |
|
308 ; // Skip to beginning of next line; |
|
309 break; |
|
310 } |
|
311 } |
|
312 } |
|
313 return status; |
|
314 } |
|
315 |
|
316 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
317 // place it in TC, returning the name of the variable. If the value |
|
318 // is tagged as global in the file, return TRUE in GLOBAL. |
|
319 // |
|
320 // FILENAME is used for error messages. |
|
321 // |
|
322 // The data is expected to be in the following format: |
|
323 // |
|
324 // The input file must have a header followed by some data. |
|
325 // |
|
326 // All lines in the header must begin with a `#' character. |
|
327 // |
|
328 // The header must contain a list of keyword and value pairs with the |
|
329 // keyword and value separated by a colon. |
|
330 // |
|
331 // Keywords must appear in the following order: |
|
332 // |
|
333 // # name: <name> |
|
334 // # type: <type> |
|
335 // # <info> |
|
336 // |
|
337 // Where: |
|
338 // |
|
339 // <name> : a valid identifier |
|
340 // |
|
341 // <type> : <typename> |
|
342 // | global <typename> |
|
343 // |
|
344 // <typename> : scalar |
|
345 // | complex scalar |
|
346 // | matrix |
|
347 // | complex matrix |
|
348 // | string |
|
349 // | range |
|
350 // | string array |
|
351 // |
|
352 // <info> : <matrix info> |
|
353 // | <string info> |
|
354 // | <string array info> |
|
355 // |
|
356 // <matrix info> : # rows: <integer> |
|
357 // : # columns: <integer> |
|
358 // |
|
359 // <string info> : # length: <integer> |
|
360 // |
|
361 // <string array info> : # elements: <integer> |
|
362 // : # length: <integer> (once before each string) |
|
363 // |
|
364 // Formatted ASCII data follows the header. |
|
365 // |
|
366 // Example: |
|
367 // |
|
368 // # name: foo |
|
369 // # type: matrix |
|
370 // # rows: 2 |
|
371 // # columns: 2 |
|
372 // 2 4 |
|
373 // 1 3 |
|
374 // |
|
375 // Example: |
|
376 // |
|
377 // # name: foo |
|
378 // # type: string array |
|
379 // # elements: 5 |
|
380 // # length: 4 |
|
381 // this |
|
382 // # length: 2 |
|
383 // is |
|
384 // # length: 1 |
|
385 // a |
|
386 // # length: 6 |
|
387 // string |
|
388 // # length: 5 |
|
389 // array |
|
390 // |
|
391 // XXX FIXME XXX -- this format is fairly rigid, and doesn't allow for |
|
392 // arbitrary comments, etc. Someone should fix that. |
|
393 |
|
394 // Ugh. The signature of the compare method is not standard in older |
|
395 // versions of the GNU libstdc++. Do this instead: |
|
396 |
|
397 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t) |
|
398 |
|
399 std::string |
|
400 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
|
401 octave_value& tc, int count) |
|
402 { |
|
403 // Read name for this entry or break on EOF. |
|
404 |
|
405 std::string name = extract_keyword (is, "name"); |
|
406 |
|
407 if (name.empty ()) |
|
408 { |
|
409 if (count == 0) |
|
410 error ("load: empty name keyword or no data found in file `%s'", |
|
411 filename.c_str ()); |
|
412 |
|
413 return std::string (); |
|
414 } |
|
415 |
|
416 if (name == CELL_ELT_TAG) |
|
417 { |
|
418 // This is OK -- name won't be used. |
|
419 } |
|
420 else if (! valid_identifier (name)) |
|
421 { |
|
422 error ("load: bogus identifier `%s' found in file `%s'", |
|
423 name.c_str (), filename.c_str ()); |
|
424 return std::string (); |
|
425 } |
|
426 |
|
427 // Look for type keyword. |
|
428 |
|
429 std::string tag = extract_keyword (is, "type"); |
|
430 |
|
431 if (! tag.empty ()) |
|
432 { |
|
433 std::string typ; |
|
434 size_t pos = tag.rfind (' '); |
|
435 |
|
436 if (pos != NPOS) |
|
437 { |
|
438 global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global"); |
|
439 |
|
440 typ = global ? tag.substr (7) : tag; |
|
441 } |
|
442 else |
|
443 typ = tag; |
|
444 |
|
445 if (SUBSTRING_COMPARE_EQ (typ, 0, 6, "scalar")) |
|
446 { |
|
447 double tmp = octave_read_double (is); |
|
448 if (is) |
|
449 tc = tmp; |
|
450 else |
|
451 error ("load: failed to load scalar constant"); |
|
452 } |
|
453 else if (SUBSTRING_COMPARE_EQ (typ, 0, 6, "matrix")) |
|
454 { |
|
455 int nr = 0; |
|
456 int nc = 0; |
|
457 |
|
458 if (extract_keyword (is, "rows", nr) && nr >= 0 |
|
459 && extract_keyword (is, "columns", nc) && nc >= 0) |
|
460 { |
|
461 if (nr > 0 && nc > 0) |
|
462 { |
|
463 Matrix tmp (nr, nc); |
|
464 is >> tmp; |
|
465 if (is) |
|
466 tc = tmp; |
|
467 else |
|
468 error ("load: failed to load matrix constant"); |
|
469 } |
|
470 else if (nr == 0 || nc == 0) |
|
471 tc = Matrix (nr, nc); |
|
472 else |
|
473 panic_impossible (); |
|
474 } |
|
475 else |
|
476 error ("load: failed to extract number of rows and columns"); |
|
477 } |
|
478 else if (SUBSTRING_COMPARE_EQ (typ, 0, 4, "cell")) |
|
479 { |
|
480 int nr = 0; |
|
481 int nc = 0; |
|
482 |
|
483 if (extract_keyword (is, "rows", nr) && nr >= 0 |
|
484 && extract_keyword (is, "columns", nc) && nc >= 0) |
|
485 { |
|
486 if (nr > 0 && nc > 0) |
|
487 { |
|
488 Cell tmp (nr, nc); |
|
489 |
|
490 for (int j = 0; j < nc; j++) |
|
491 { |
|
492 for (int i = 0; i < nr; i++) |
|
493 { |
|
494 octave_value t2; |
|
495 |
|
496 // recurse to read cell elements |
|
497 std::string nm |
|
498 = read_ascii_data (is, filename, global, t2, count); |
|
499 |
|
500 if (nm == CELL_ELT_TAG) |
|
501 { |
|
502 if (is) |
|
503 tmp.elem (i, j) = t2; |
|
504 } |
|
505 else |
|
506 { |
|
507 error ("load: cell array element had unexpected name"); |
|
508 goto cell_read_error; |
|
509 } |
|
510 } |
|
511 } |
|
512 |
|
513 cell_read_error: |
|
514 |
|
515 if (is) |
|
516 tc = tmp; |
|
517 else |
|
518 error ("load: failed to load cell element"); |
|
519 } |
|
520 else if (nr == 0 || nc == 0) |
|
521 tc = Cell (nr, nc); |
|
522 else |
|
523 panic_impossible (); |
|
524 } |
|
525 else |
|
526 error ("load: failed to extract number of rows and columns for cell array"); |
|
527 } |
|
528 else if (SUBSTRING_COMPARE_EQ (typ, 0, 14, "complex scalar")) |
|
529 { |
|
530 Complex tmp = octave_read_complex (is); |
|
531 if (is) |
|
532 tc = tmp; |
|
533 else |
|
534 error ("load: failed to load complex scalar constant"); |
|
535 } |
|
536 else if (SUBSTRING_COMPARE_EQ (typ, 0, 14, "complex matrix")) |
|
537 { |
|
538 int nr = 0; |
|
539 int nc = 0; |
|
540 |
|
541 if (extract_keyword (is, "rows", nr) && nr > 0 |
|
542 && extract_keyword (is, "columns", nc) && nc > 0) |
|
543 { |
|
544 ComplexMatrix tmp (nr, nc); |
|
545 is >> tmp; |
|
546 if (is) |
|
547 tc = tmp; |
|
548 else |
|
549 error ("load: failed to load complex matrix constant"); |
|
550 } |
|
551 else |
|
552 error ("load: failed to extract number of rows and columns"); |
|
553 } |
|
554 else if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array")) |
|
555 { |
|
556 int elements; |
|
557 if (extract_keyword (is, "elements", elements) && elements >= 0) |
|
558 { |
|
559 // XXX FIXME XXX -- need to be able to get max length |
|
560 // before doing anything. |
|
561 |
|
562 charMatrix chm (elements, 0); |
|
563 int max_len = 0; |
|
564 for (int i = 0; i < elements; i++) |
|
565 { |
|
566 int len; |
|
567 if (extract_keyword (is, "length", len) && len >= 0) |
|
568 { |
|
569 OCTAVE_LOCAL_BUFFER (char, tmp, len+1); |
|
570 |
|
571 if (len > 0 && ! is.read (X_CAST (char *, tmp), len)) |
|
572 { |
|
573 error ("load: failed to load string constant"); |
|
574 break; |
|
575 } |
|
576 else |
|
577 { |
|
578 tmp [len] = '\0'; |
|
579 if (len > max_len) |
|
580 { |
|
581 max_len = len; |
|
582 chm.resize (elements, max_len, 0); |
|
583 } |
|
584 chm.insert (tmp, i, 0); |
|
585 } |
|
586 } |
|
587 else |
|
588 error ("load: failed to extract string length for element %d", i+1); |
|
589 } |
|
590 |
|
591 if (! error_state) |
|
592 tc = octave_value (chm, true); |
|
593 } |
|
594 else |
|
595 error ("load: failed to extract number of string elements"); |
|
596 } |
|
597 else if (SUBSTRING_COMPARE_EQ (typ, 0, 6, "string")) |
|
598 { |
|
599 int len; |
|
600 if (extract_keyword (is, "length", len) && len >= 0) |
|
601 { |
|
602 OCTAVE_LOCAL_BUFFER (char, tmp, len+1); |
|
603 |
|
604 if (len > 0 && ! is.read (X_CAST (char *, tmp), len)) |
|
605 { |
|
606 error ("load: failed to load string constant"); |
|
607 } |
|
608 else |
|
609 { |
|
610 tmp [len] = '\0'; |
|
611 |
|
612 if (is) |
|
613 tc = tmp; |
|
614 else |
|
615 error ("load: failed to load string constant"); |
|
616 } |
|
617 } |
|
618 else |
|
619 error ("load: failed to extract string length"); |
|
620 } |
|
621 else if (SUBSTRING_COMPARE_EQ (typ, 0, 5, "range")) |
|
622 { |
|
623 // # base, limit, range comment added by save (). |
|
624 |
|
625 skip_comments (is); |
|
626 Range tmp; |
|
627 is >> tmp; |
|
628 if (is) |
|
629 tc = tmp; |
|
630 else |
|
631 error ("load: failed to load range constant"); |
|
632 } |
|
633 else |
|
634 error ("load: unknown constant type `%s'", tag.c_str ()); |
|
635 } |
|
636 else |
|
637 error ("load: failed to extract keyword specifying value type"); |
|
638 |
|
639 if (error_state) |
|
640 { |
|
641 error ("load: reading file %s", filename.c_str ()); |
|
642 return std::string (); |
|
643 } |
|
644 |
|
645 return name; |
|
646 } |
|
647 |
|
648 // Save the data from TC along with the corresponding NAME, and global |
|
649 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
|
650 // above for load_ascii_data. If NAME is empty, the name: line is not |
|
651 // generated. PRECISION specifies the number of decimal digits to print. |
|
652 // If STRIP_NAN_AND_INF is TRUE, rows containing NaNs are deleted, |
|
653 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
654 // but not so big that gnuplot can't handle it when trying to compute |
|
655 // axis ranges, etc.). |
|
656 // |
|
657 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
658 // |
|
659 // Returns 1 for success and 0 for failure. |
|
660 |
|
661 // XXX FIXME XXX -- should probably write the help string here too. |
|
662 |
|
663 bool |
|
664 save_ascii_data (std::ostream& os, const octave_value& val_arg, |
|
665 const std::string& name, bool& infnan_warned, |
|
666 bool strip_nan_and_inf, bool mark_as_global, |
|
667 int precision) |
|
668 { |
|
669 bool success = true; |
|
670 |
|
671 if (! precision) |
|
672 precision = Vsave_precision; |
|
673 |
|
674 if (! name.empty ()) |
|
675 os << "# name: " << name << "\n"; |
|
676 |
|
677 long old_precision = os.precision (); |
|
678 os.precision (precision); |
|
679 |
|
680 octave_value val = val_arg; |
|
681 |
|
682 if (val.is_range ()) |
|
683 { |
|
684 Range r = val.range_value (); |
|
685 double base = r.base (); |
|
686 double limit = r.limit (); |
|
687 double inc = r.inc (); |
|
688 if (! (NINT (base) == base |
|
689 && NINT (limit) == limit |
|
690 && NINT (inc) == inc)) |
|
691 val = val.matrix_value (); |
|
692 } |
|
693 |
|
694 if (val.is_string ()) |
|
695 { |
|
696 ascii_save_type (os, "string array", mark_as_global); |
|
697 charMatrix chm = val.char_matrix_value (); |
|
698 int elements = chm.rows (); |
|
699 os << "# elements: " << elements << "\n"; |
|
700 for (int i = 0; i < elements; i++) |
|
701 { |
|
702 unsigned len = chm.cols (); |
|
703 os << "# length: " << len << "\n"; |
|
704 std::string tstr = chm.row_as_string (i, false, true); |
|
705 const char *tmp = tstr.data (); |
|
706 if (tstr.length () > len) |
|
707 panic_impossible (); |
|
708 os.write (X_CAST (char *, tmp), len); |
|
709 os << "\n"; |
|
710 } |
|
711 } |
|
712 else if (val.is_range ()) |
|
713 { |
|
714 ascii_save_type (os, "range", mark_as_global); |
|
715 Range tmp = val.range_value (); |
|
716 os << "# base, limit, increment\n"; |
|
717 octave_write_double (os, tmp.base ()); |
|
718 os << " "; |
|
719 octave_write_double (os, tmp.limit ()); |
|
720 os << " "; |
|
721 octave_write_double (os, tmp.inc ()); |
|
722 os << "\n"; |
|
723 } |
|
724 else if (val.is_real_scalar ()) |
|
725 { |
|
726 ascii_save_type (os, "scalar", mark_as_global); |
|
727 |
|
728 double d = val.double_value (); |
|
729 |
|
730 if (strip_nan_and_inf) |
|
731 { |
|
732 if (xisnan (d)) |
|
733 { |
|
734 error ("only value to plot is NaN"); |
|
735 success = false; |
|
736 } |
|
737 else |
|
738 { |
|
739 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
740 octave_write_double (os, d); |
|
741 os << "\n"; |
|
742 } |
|
743 } |
|
744 else |
|
745 { |
|
746 if (! infnan_warned && (xisnan (d) || xisinf (d))) |
|
747 { |
|
748 warning ("save: Inf or NaN values may not be reloadable"); |
|
749 infnan_warned = true; |
|
750 } |
|
751 |
|
752 octave_write_double (os, d); |
|
753 os << "\n"; |
|
754 } |
|
755 } |
|
756 else if (val.is_real_matrix ()) |
|
757 { |
|
758 ascii_save_type (os, "matrix", mark_as_global); |
|
759 |
|
760 os << "# rows: " << val.rows () << "\n" |
|
761 << "# columns: " << val.columns () << "\n"; |
|
762 |
|
763 Matrix tmp = val.matrix_value (); |
|
764 |
|
765 if (strip_nan_and_inf) |
|
766 tmp = strip_infnan (tmp); |
|
767 else if (! infnan_warned && tmp.any_element_is_inf_or_nan ()) |
|
768 { |
|
769 warning ("save: Inf or NaN values may not be reloadable"); |
|
770 infnan_warned = true; |
|
771 } |
|
772 |
|
773 os << tmp; |
|
774 } |
|
775 else if (val.is_cell ()) |
|
776 { |
|
777 ascii_save_type (os, "cell", mark_as_global); |
|
778 |
|
779 os << "# rows: " << val.rows () << "\n" |
|
780 << "# columns: " << val.columns () << "\n"; |
|
781 |
|
782 Cell tmp = val.cell_value (); |
|
783 |
|
784 for (int j = 0; j < tmp.cols (); j++) |
|
785 { |
|
786 for (int i = 0; i < tmp.rows (); i++) |
|
787 { |
|
788 octave_value o_val = tmp.elem (i, j); |
|
789 |
|
790 // Recurse to print sub-value. |
|
791 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, |
|
792 infnan_warned, strip_nan_and_inf, |
|
793 mark_as_global, 0); |
|
794 |
|
795 if (! b) |
|
796 return os; |
|
797 } |
|
798 |
|
799 os << "\n"; |
|
800 } |
|
801 } |
|
802 else if (val.is_complex_scalar ()) |
|
803 { |
|
804 ascii_save_type (os, "complex scalar", mark_as_global); |
|
805 |
|
806 Complex c = val.complex_value (); |
|
807 |
|
808 if (strip_nan_and_inf) |
|
809 { |
|
810 if (xisnan (c)) |
|
811 { |
|
812 error ("only value to plot is NaN"); |
|
813 success = false; |
|
814 } |
|
815 else |
|
816 { |
|
817 double re = real (c); |
|
818 double im = imag (c); |
|
819 |
|
820 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
821 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
822 |
|
823 c = Complex (re, im); |
|
824 |
|
825 octave_write_complex (os, c); |
|
826 os << "\n"; |
|
827 } |
|
828 } |
|
829 else |
|
830 { |
|
831 if (! infnan_warned && (xisnan (c) || xisinf (c))) |
|
832 { |
|
833 warning ("save: Inf or NaN values may not be reloadable"); |
|
834 infnan_warned = true; |
|
835 } |
|
836 |
|
837 octave_write_complex (os, c); |
|
838 os << "\n"; |
|
839 } |
|
840 } |
|
841 else if (val.is_complex_matrix ()) |
|
842 { |
|
843 ascii_save_type (os, "complex matrix", mark_as_global); |
|
844 |
|
845 os << "# rows: " << val.rows () << "\n" |
|
846 << "# columns: " << val.columns () << "\n"; |
|
847 |
|
848 ComplexMatrix tmp = val.complex_matrix_value (); |
|
849 |
|
850 if (strip_nan_and_inf) |
|
851 tmp = strip_infnan (tmp); |
|
852 else if (! infnan_warned && tmp.any_element_is_inf_or_nan ()) |
|
853 { |
|
854 warning ("save: Inf or NaN values may not be reloadable"); |
|
855 infnan_warned = true; |
|
856 } |
|
857 |
|
858 os << tmp; |
|
859 } |
|
860 else |
|
861 gripe_wrong_type_arg ("save", val, false); |
|
862 |
|
863 os.precision (old_precision); |
|
864 |
|
865 return (os && success); |
|
866 } |
|
867 |
|
868 bool |
|
869 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
|
870 const std::string& name) |
|
871 { |
|
872 bool infnan_warned = true; |
|
873 |
|
874 return save_ascii_data (os, t, name, infnan_warned, true, false, 0); |
|
875 } |
|
876 |
|
877 // Maybe this should be a static function in tree-plot.cc? |
|
878 |
|
879 // If TC is matrix, save it on stream OS in a format useful for |
|
880 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
881 // TRUE, assume a parametric 3-dimensional plot will be generated. |
|
882 |
|
883 bool |
|
884 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) |
|
885 { |
|
886 bool fail = false; |
|
887 |
|
888 int nr = tc.rows (); |
|
889 int nc = tc.columns (); |
|
890 |
|
891 if (tc.is_real_matrix ()) |
|
892 { |
|
893 os << "# 3D data...\n" |
|
894 << "# type: matrix\n" |
|
895 << "# total rows: " << nr << "\n" |
|
896 << "# total columns: " << nc << "\n"; |
|
897 |
|
898 if (parametric) |
|
899 { |
|
900 int extras = nc % 3; |
|
901 if (extras) |
|
902 warning ("ignoring last %d columns", extras); |
|
903 |
|
904 Matrix tmp = tc.matrix_value (); |
|
905 tmp = strip_infnan (tmp); |
|
906 nr = tmp.rows (); |
|
907 |
|
908 for (int i = 0; i < nc-extras; i += 3) |
|
909 { |
|
910 os << tmp.extract (0, i, nr-1, i+2); |
|
911 if (i+3 < nc-extras) |
|
912 os << "\n"; |
|
913 } |
|
914 } |
|
915 else |
|
916 { |
|
917 Matrix tmp = tc.matrix_value (); |
|
918 tmp = strip_infnan (tmp); |
|
919 nr = tmp.rows (); |
|
920 |
|
921 for (int i = 0; i < nc; i++) |
|
922 { |
|
923 os << tmp.extract (0, i, nr-1, i); |
|
924 if (i+1 < nc) |
|
925 os << "\n"; |
|
926 } |
|
927 } |
|
928 } |
|
929 else |
|
930 { |
|
931 ::error ("for now, I can only save real matrices in 3D format"); |
|
932 fail = true; |
|
933 } |
|
934 |
|
935 return (os && ! fail); |
|
936 } |
|
937 |
|
938 static int |
|
939 save_precision (void) |
|
940 { |
|
941 double val; |
|
942 if (builtin_real_scalar_variable ("save_precision", val) |
|
943 && ! xisnan (val)) |
|
944 { |
|
945 int ival = NINT (val); |
|
946 if (ival >= 0 && ival == val) |
|
947 { |
|
948 Vsave_precision = ival; |
|
949 return 0; |
|
950 } |
|
951 } |
|
952 gripe_invalid_value_specified ("save_precision"); |
|
953 return -1; |
|
954 } |
|
955 |
|
956 void |
|
957 symbols_of_ls_oct_ascii (void) |
|
958 { |
|
959 DEFVAR (save_precision, 15.0, save_precision, |
|
960 "-*- texinfo -*-\n\ |
|
961 @defvr {Built-in Variable} save_precision\n\ |
|
962 This variable specifies the number of digits to keep when saving data in\n\ |
|
963 text format. The default value is 17.\n\ |
|
964 @end defvr"); |
|
965 } |
|
966 |
|
967 /* |
|
968 ;;; Local Variables: *** |
|
969 ;;; mode: C++ *** |
|
970 ;;; End: *** |
|
971 */ |
|
972 |