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 <cstring> |
|
30 #include <cctype> |
|
31 |
|
32 #include <fstream> |
|
33 #include <iomanip> |
|
34 #include <iostream> |
|
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 "lo-sstream.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 "gripes.h" |
|
53 #include "load-save.h" |
|
54 #include "oct-obj.h" |
|
55 #include "oct-map.h" |
|
56 #include "ov-cell.h" |
|
57 #include "pager.h" |
|
58 #include "pt-exp.h" |
|
59 #include "symtab.h" |
|
60 #include "sysdep.h" |
|
61 #include "unwind-prot.h" |
|
62 #include "utils.h" |
|
63 #include "variables.h" |
|
64 #include "version.h" |
|
65 #include "dMatrix.h" |
|
66 |
|
67 #include "ls-oct-ascii.h" |
|
68 |
|
69 // The number of decimal digits to use when writing ascii data. |
|
70 static int Vsave_precision; |
|
71 |
|
72 // Functions for reading ascii data. |
|
73 |
|
74 static Matrix |
|
75 strip_infnan (const Matrix& m) |
|
76 { |
|
77 int nr = m.rows (); |
|
78 int nc = m.columns (); |
|
79 |
|
80 Matrix retval (nr, nc); |
|
81 |
|
82 int k = 0; |
|
83 for (int i = 0; i < nr; i++) |
|
84 { |
|
85 for (int j = 0; j < nc; j++) |
|
86 { |
|
87 double d = m (i, j); |
|
88 if (xisnan (d)) |
|
89 goto next_row; |
|
90 else |
|
91 retval (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
92 } |
|
93 k++; |
|
94 |
|
95 next_row: |
|
96 continue; |
|
97 } |
|
98 |
|
99 if (k > 0) |
|
100 retval.resize (k, nc); |
|
101 |
|
102 return retval; |
|
103 } |
|
104 |
|
105 // Extract a KEYWORD and its value from stream IS, returning the |
|
106 // associated value in a new string. |
|
107 // |
|
108 // Input should look something like: |
|
109 // |
|
110 // [%#][ \t]*keyword[ \t]*:[ \t]*string-value[ \t]*\n |
|
111 |
|
112 std::string |
4687
|
113 extract_keyword (std::istream& is, const char *keyword, const bool next_only) |
4634
|
114 { |
|
115 std::string retval; |
|
116 |
|
117 char c; |
|
118 while (is.get (c)) |
|
119 { |
|
120 if (c == '%' || c == '#') |
|
121 { |
|
122 OSSTREAM buf; |
|
123 |
|
124 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
125 ; // Skip whitespace and comment characters. |
|
126 |
|
127 if (isalpha (c)) |
|
128 buf << c; |
|
129 |
|
130 while (is.get (c) && isalpha (c)) |
|
131 buf << c; |
|
132 |
|
133 buf << OSSTREAM_ENDS; |
|
134 const char *tmp = OSSTREAM_C_STR (buf); |
|
135 OSSTREAM_FREEZE (buf); |
|
136 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
137 |
|
138 if (match) |
|
139 { |
|
140 OSSTREAM value; |
|
141 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
142 ; // Skip whitespace and the colon. |
|
143 |
|
144 if (c != '\n') |
|
145 { |
|
146 value << c; |
|
147 while (is.get (c) && c != '\n') |
|
148 value << c; |
|
149 } |
|
150 value << OSSTREAM_ENDS; |
|
151 retval = OSSTREAM_STR (value); |
|
152 OSSTREAM_FREEZE (value); |
|
153 break; |
|
154 } |
4687
|
155 else if (next_only) |
|
156 break; |
4634
|
157 } |
|
158 } |
|
159 |
|
160 int len = retval.length (); |
|
161 |
|
162 if (len > 0) |
|
163 { |
|
164 while (len) |
|
165 { |
|
166 c = retval[len-1]; |
|
167 |
|
168 if (c == ' ' || c == '\t') |
|
169 len--; |
|
170 else |
|
171 { |
|
172 retval.resize (len); |
|
173 break; |
|
174 } |
|
175 } |
|
176 } |
|
177 |
|
178 return retval; |
|
179 } |
|
180 |
|
181 // Match KEYWORD on stream IS, placing the associated value in VALUE, |
|
182 // returning TRUE if successful and FALSE otherwise. |
|
183 // |
|
184 // Input should look something like: |
|
185 // |
|
186 // [%#][ \t]*keyword[ \t]*int-value.*\n |
|
187 |
4687
|
188 bool |
|
189 extract_keyword (std::istream& is, const char *keyword, int& value, |
|
190 const bool next_only) |
4634
|
191 { |
|
192 bool status = false; |
|
193 value = 0; |
|
194 |
|
195 char c; |
|
196 while (is.get (c)) |
|
197 { |
|
198 if (c == '%' || c == '#') |
|
199 { |
|
200 OSSTREAM buf; |
|
201 |
|
202 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
203 ; // Skip whitespace and comment characters. |
|
204 |
|
205 if (isalpha (c)) |
|
206 buf << c; |
|
207 |
|
208 while (is.get (c) && isalpha (c)) |
|
209 buf << c; |
|
210 |
|
211 buf << OSSTREAM_ENDS; |
|
212 const char *tmp = OSSTREAM_C_STR (buf); |
|
213 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
214 OSSTREAM_FREEZE (buf); |
|
215 |
|
216 if (match) |
|
217 { |
|
218 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
219 ; // Skip whitespace and the colon. |
|
220 |
|
221 is.putback (c); |
|
222 if (c != '\n') |
|
223 is >> value; |
|
224 if (is) |
|
225 status = true; |
|
226 while (is.get (c) && c != '\n') |
|
227 ; // Skip to beginning of next line; |
|
228 break; |
|
229 } |
4687
|
230 else if (next_only) |
|
231 break; |
4634
|
232 } |
|
233 } |
|
234 return status; |
|
235 } |
|
236 |
5099
|
237 // Match one of the elements in KEYWORDS on stream IS, placing the |
|
238 // matched keyword in KW and the associated value in VALUE, |
|
239 // returning TRUE if successful and FALSE otherwise. |
|
240 // |
|
241 // Input should look something like: |
|
242 // |
|
243 // [%#][ \t]*keyword[ \t]*int-value.*\n |
|
244 |
|
245 bool |
|
246 extract_keyword (std::istream& is, const string_vector& keywords, |
|
247 std::string& kw, int& value, const bool next_only) |
|
248 { |
|
249 bool status = false; |
|
250 kw = ""; |
|
251 value = 0; |
|
252 |
|
253 char c; |
|
254 while (is.get (c)) |
|
255 { |
|
256 if (c == '%' || c == '#') |
|
257 { |
|
258 OSSTREAM buf; |
|
259 |
|
260 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
261 ; // Skip whitespace and comment characters. |
|
262 |
|
263 if (isalpha (c)) |
|
264 buf << c; |
|
265 |
|
266 while (is.get (c) && isalpha (c)) |
|
267 buf << c; |
|
268 |
|
269 buf << OSSTREAM_ENDS; |
|
270 std::string tmp = OSSTREAM_STR (buf); |
|
271 OSSTREAM_FREEZE (buf); |
|
272 |
|
273 for (int i = 0; i < keywords.length (); i++) |
|
274 { |
|
275 int match = (tmp == keywords[i]); |
|
276 |
|
277 if (match) |
|
278 { |
|
279 kw = keywords[i]; |
|
280 |
|
281 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
282 ; // Skip whitespace and the colon. |
|
283 |
|
284 is.putback (c); |
|
285 if (c != '\n') |
|
286 is >> value; |
|
287 if (is) |
|
288 status = true; |
|
289 while (is.get (c) && c != '\n') |
|
290 ; // Skip to beginning of next line; |
|
291 return status; |
|
292 } |
|
293 } |
|
294 |
|
295 if (next_only) |
|
296 break; |
|
297 } |
|
298 } |
|
299 return status; |
|
300 } |
|
301 |
5100
|
302 // Match one of the elements in KEYWORDS on stream IS, placing the |
|
303 // matched keyword in KW and the associated value in VALUE, |
|
304 // returning TRUE if successful and FALSE otherwise. |
|
305 // |
|
306 // Input should look something like: |
|
307 // |
|
308 // [%#][ \t]*keyword[ \t]*int-value.*\n |
|
309 |
|
310 bool |
|
311 extract_keyword (std::istream& is, const string_vector& keywords, |
|
312 std::string& kw, int& value, const bool next_only) |
|
313 { |
|
314 bool status = false; |
|
315 kw = ""; |
|
316 value = 0; |
|
317 |
|
318 char c; |
|
319 while (is.get (c)) |
|
320 { |
|
321 if (c == '%' || c == '#') |
|
322 { |
|
323 OSSTREAM buf; |
|
324 |
|
325 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
326 ; // Skip whitespace and comment characters. |
|
327 |
|
328 if (isalpha (c)) |
|
329 buf << c; |
|
330 |
|
331 while (is.get (c) && isalpha (c)) |
|
332 buf << c; |
|
333 |
|
334 buf << OSSTREAM_ENDS; |
|
335 std::string tmp = OSSTREAM_STR (buf); |
|
336 OSSTREAM_FREEZE (buf); |
|
337 |
|
338 for (int i = 0; i < keywords.length (); i++) |
|
339 { |
|
340 int match = (tmp == keywords[i]); |
|
341 |
|
342 if (match) |
|
343 { |
|
344 kw = keywords[i]; |
|
345 |
|
346 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
347 ; // Skip whitespace and the colon. |
|
348 |
|
349 is.putback (c); |
|
350 if (c != '\n') |
|
351 is >> value; |
|
352 if (is) |
|
353 status = true; |
|
354 while (is.get (c) && c != '\n') |
|
355 ; // Skip to beginning of next line; |
|
356 return status; |
|
357 } |
|
358 } |
|
359 |
|
360 if (next_only) |
|
361 break; |
|
362 } |
|
363 } |
|
364 return status; |
|
365 } |
|
366 |
4634
|
367 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
368 // place it in TC, returning the name of the variable. If the value |
|
369 // is tagged as global in the file, return TRUE in GLOBAL. |
|
370 // |
4687
|
371 // Each type supplies its own function to load the data, and so this |
|
372 // function is extensible. |
|
373 // |
4634
|
374 // FILENAME is used for error messages. |
|
375 // |
|
376 // The data is expected to be in the following format: |
|
377 // |
|
378 // The input file must have a header followed by some data. |
|
379 // |
|
380 // All lines in the header must begin with a `#' character. |
|
381 // |
|
382 // The header must contain a list of keyword and value pairs with the |
|
383 // keyword and value separated by a colon. |
|
384 // |
|
385 // Keywords must appear in the following order: |
|
386 // |
|
387 // # name: <name> |
|
388 // # type: <type> |
|
389 // # <info> |
|
390 // |
4687
|
391 // Where, for the built in types are: |
4634
|
392 // |
|
393 // <name> : a valid identifier |
|
394 // |
|
395 // <type> : <typename> |
|
396 // | global <typename> |
|
397 // |
|
398 // <typename> : scalar |
|
399 // | complex scalar |
|
400 // | matrix |
|
401 // | complex matrix |
4687
|
402 // | bool |
|
403 // | bool matrix |
4634
|
404 // | string |
|
405 // | range |
|
406 // |
|
407 // <info> : <matrix info> |
|
408 // | <string info> |
|
409 // |
|
410 // <matrix info> : # rows: <integer> |
|
411 // : # columns: <integer> |
|
412 // |
4687
|
413 // <string info> : # elements: <integer> |
|
414 // : # length: <integer> (once before each string) |
4634
|
415 // |
4687
|
416 // For backward compatibility the type "string array" is treated as a |
|
417 // "string" type. Also "string" can have a single element with no elements |
|
418 // line such that |
|
419 // |
|
420 // <string info> : # length: <integer> |
4634
|
421 // |
|
422 // Formatted ASCII data follows the header. |
|
423 // |
|
424 // Example: |
|
425 // |
|
426 // # name: foo |
|
427 // # type: matrix |
|
428 // # rows: 2 |
|
429 // # columns: 2 |
|
430 // 2 4 |
|
431 // 1 3 |
|
432 // |
|
433 // Example: |
|
434 // |
|
435 // # name: foo |
4687
|
436 // # type: string |
4634
|
437 // # elements: 5 |
|
438 // # length: 4 |
|
439 // this |
|
440 // # length: 2 |
|
441 // is |
|
442 // # length: 1 |
|
443 // a |
|
444 // # length: 6 |
|
445 // string |
|
446 // # length: 5 |
|
447 // array |
|
448 // |
|
449 // XXX FIXME XXX -- this format is fairly rigid, and doesn't allow for |
4687
|
450 // arbitrary comments. Someone should fix that. It does allow arbitrary |
|
451 // types however. |
4634
|
452 |
|
453 // Ugh. The signature of the compare method is not standard in older |
|
454 // versions of the GNU libstdc++. Do this instead: |
|
455 |
|
456 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t) |
|
457 |
|
458 std::string |
|
459 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
|
460 octave_value& tc, int count) |
|
461 { |
|
462 // Read name for this entry or break on EOF. |
|
463 |
|
464 std::string name = extract_keyword (is, "name"); |
|
465 |
|
466 if (name.empty ()) |
|
467 { |
|
468 if (count == 0) |
|
469 error ("load: empty name keyword or no data found in file `%s'", |
|
470 filename.c_str ()); |
|
471 |
|
472 return std::string (); |
|
473 } |
|
474 |
|
475 if (name == CELL_ELT_TAG) |
|
476 { |
|
477 // This is OK -- name won't be used. |
|
478 } |
|
479 else if (! valid_identifier (name)) |
|
480 { |
|
481 error ("load: bogus identifier `%s' found in file `%s'", |
|
482 name.c_str (), filename.c_str ()); |
|
483 return std::string (); |
|
484 } |
|
485 |
|
486 // Look for type keyword. |
|
487 |
|
488 std::string tag = extract_keyword (is, "type"); |
|
489 |
|
490 if (! tag.empty ()) |
|
491 { |
|
492 std::string typ; |
|
493 size_t pos = tag.rfind (' '); |
|
494 |
|
495 if (pos != NPOS) |
|
496 { |
|
497 global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global"); |
|
498 |
|
499 typ = global ? tag.substr (7) : tag; |
|
500 } |
|
501 else |
|
502 typ = tag; |
|
503 |
4687
|
504 // Special case for backward compatiablity. A small bit of cruft |
|
505 if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array")) |
|
506 tc = octave_value (charMatrix (), true); |
|
507 else |
|
508 tc = octave_value_typeinfo::lookup_type (typ); |
4634
|
509 |
4988
|
510 if (! tc.load_ascii (is)) |
|
511 error ("load: trouble reading ascii file `%s'", filename.c_str ()); |
4634
|
512 } |
|
513 else |
|
514 error ("load: failed to extract keyword specifying value type"); |
|
515 |
|
516 if (error_state) |
|
517 { |
|
518 error ("load: reading file %s", filename.c_str ()); |
|
519 return std::string (); |
|
520 } |
|
521 |
|
522 return name; |
|
523 } |
|
524 |
|
525 // Save the data from TC along with the corresponding NAME, and global |
|
526 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
|
527 // above for load_ascii_data. If NAME is empty, the name: line is not |
|
528 // generated. PRECISION specifies the number of decimal digits to print. |
|
529 // If STRIP_NAN_AND_INF is TRUE, rows containing NaNs are deleted, |
|
530 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
531 // but not so big that gnuplot can't handle it when trying to compute |
|
532 // axis ranges, etc.). |
|
533 // |
|
534 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
535 // |
|
536 // Returns 1 for success and 0 for failure. |
|
537 |
|
538 // XXX FIXME XXX -- should probably write the help string here too. |
|
539 |
|
540 bool |
|
541 save_ascii_data (std::ostream& os, const octave_value& val_arg, |
|
542 const std::string& name, bool& infnan_warned, |
|
543 bool strip_nan_and_inf, bool mark_as_global, |
|
544 int precision) |
|
545 { |
|
546 bool success = true; |
|
547 |
|
548 if (! precision) |
|
549 precision = Vsave_precision; |
|
550 |
|
551 if (! name.empty ()) |
|
552 os << "# name: " << name << "\n"; |
|
553 |
|
554 long old_precision = os.precision (); |
|
555 os.precision (precision); |
|
556 |
|
557 octave_value val = val_arg; |
|
558 |
4687
|
559 if (mark_as_global) |
|
560 os << "# type: global " << val.type_name () << "\n"; |
|
561 else |
|
562 os << "# type: " << val.type_name() << "\n"; |
4634
|
563 |
4687
|
564 success = val . save_ascii(os, infnan_warned, strip_nan_and_inf); |
4634
|
565 |
|
566 os.precision (old_precision); |
|
567 |
|
568 return (os && success); |
|
569 } |
|
570 |
|
571 bool |
|
572 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
|
573 const std::string& name) |
|
574 { |
|
575 bool infnan_warned = true; |
|
576 |
|
577 return save_ascii_data (os, t, name, infnan_warned, true, false, 0); |
|
578 } |
|
579 |
|
580 // Maybe this should be a static function in tree-plot.cc? |
|
581 |
|
582 // If TC is matrix, save it on stream OS in a format useful for |
|
583 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
584 // TRUE, assume a parametric 3-dimensional plot will be generated. |
|
585 |
|
586 bool |
|
587 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) |
|
588 { |
|
589 bool fail = false; |
|
590 |
|
591 int nr = tc.rows (); |
|
592 int nc = tc.columns (); |
|
593 |
|
594 if (tc.is_real_matrix ()) |
|
595 { |
|
596 os << "# 3D data...\n" |
|
597 << "# type: matrix\n" |
|
598 << "# total rows: " << nr << "\n" |
|
599 << "# total columns: " << nc << "\n"; |
|
600 |
|
601 if (parametric) |
|
602 { |
|
603 int extras = nc % 3; |
|
604 if (extras) |
|
605 warning ("ignoring last %d columns", extras); |
|
606 |
|
607 Matrix tmp = tc.matrix_value (); |
|
608 tmp = strip_infnan (tmp); |
|
609 nr = tmp.rows (); |
|
610 |
|
611 for (int i = 0; i < nc-extras; i += 3) |
|
612 { |
|
613 os << tmp.extract (0, i, nr-1, i+2); |
|
614 if (i+3 < nc-extras) |
|
615 os << "\n"; |
|
616 } |
|
617 } |
|
618 else |
|
619 { |
|
620 Matrix tmp = tc.matrix_value (); |
|
621 tmp = strip_infnan (tmp); |
|
622 nr = tmp.rows (); |
|
623 |
|
624 for (int i = 0; i < nc; i++) |
|
625 { |
|
626 os << tmp.extract (0, i, nr-1, i); |
|
627 if (i+1 < nc) |
|
628 os << "\n"; |
|
629 } |
|
630 } |
|
631 } |
|
632 else |
|
633 { |
|
634 ::error ("for now, I can only save real matrices in 3D format"); |
|
635 fail = true; |
|
636 } |
|
637 |
|
638 return (os && ! fail); |
|
639 } |
|
640 |
|
641 static int |
|
642 save_precision (void) |
|
643 { |
|
644 double val; |
|
645 if (builtin_real_scalar_variable ("save_precision", val) |
|
646 && ! xisnan (val)) |
|
647 { |
|
648 int ival = NINT (val); |
|
649 if (ival >= 0 && ival == val) |
|
650 { |
|
651 Vsave_precision = ival; |
|
652 return 0; |
|
653 } |
|
654 } |
|
655 gripe_invalid_value_specified ("save_precision"); |
|
656 return -1; |
|
657 } |
|
658 |
|
659 void |
|
660 symbols_of_ls_oct_ascii (void) |
|
661 { |
|
662 DEFVAR (save_precision, 15.0, save_precision, |
|
663 "-*- texinfo -*-\n\ |
|
664 @defvr {Built-in Variable} save_precision\n\ |
|
665 This variable specifies the number of digits to keep when saving data in\n\ |
|
666 text format. The default value is 17.\n\ |
|
667 @end defvr"); |
|
668 } |
|
669 |
|
670 /* |
|
671 ;;; Local Variables: *** |
|
672 ;;; mode: C++ *** |
|
673 ;;; End: *** |
|
674 */ |
|
675 |