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 |
4634
|
302 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
303 // place it in TC, returning the name of the variable. If the value |
|
304 // is tagged as global in the file, return TRUE in GLOBAL. |
|
305 // |
4687
|
306 // Each type supplies its own function to load the data, and so this |
|
307 // function is extensible. |
|
308 // |
4634
|
309 // FILENAME is used for error messages. |
|
310 // |
|
311 // The data is expected to be in the following format: |
|
312 // |
|
313 // The input file must have a header followed by some data. |
|
314 // |
|
315 // All lines in the header must begin with a `#' character. |
|
316 // |
|
317 // The header must contain a list of keyword and value pairs with the |
|
318 // keyword and value separated by a colon. |
|
319 // |
|
320 // Keywords must appear in the following order: |
|
321 // |
|
322 // # name: <name> |
|
323 // # type: <type> |
|
324 // # <info> |
|
325 // |
4687
|
326 // Where, for the built in types are: |
4634
|
327 // |
|
328 // <name> : a valid identifier |
|
329 // |
|
330 // <type> : <typename> |
|
331 // | global <typename> |
|
332 // |
|
333 // <typename> : scalar |
|
334 // | complex scalar |
|
335 // | matrix |
|
336 // | complex matrix |
4687
|
337 // | bool |
|
338 // | bool matrix |
4634
|
339 // | string |
|
340 // | range |
|
341 // |
|
342 // <info> : <matrix info> |
|
343 // | <string info> |
|
344 // |
|
345 // <matrix info> : # rows: <integer> |
|
346 // : # columns: <integer> |
|
347 // |
4687
|
348 // <string info> : # elements: <integer> |
|
349 // : # length: <integer> (once before each string) |
4634
|
350 // |
4687
|
351 // For backward compatibility the type "string array" is treated as a |
|
352 // "string" type. Also "string" can have a single element with no elements |
|
353 // line such that |
|
354 // |
|
355 // <string info> : # length: <integer> |
4634
|
356 // |
|
357 // Formatted ASCII data follows the header. |
|
358 // |
|
359 // Example: |
|
360 // |
|
361 // # name: foo |
|
362 // # type: matrix |
|
363 // # rows: 2 |
|
364 // # columns: 2 |
|
365 // 2 4 |
|
366 // 1 3 |
|
367 // |
|
368 // Example: |
|
369 // |
|
370 // # name: foo |
4687
|
371 // # type: string |
4634
|
372 // # elements: 5 |
|
373 // # length: 4 |
|
374 // this |
|
375 // # length: 2 |
|
376 // is |
|
377 // # length: 1 |
|
378 // a |
|
379 // # length: 6 |
|
380 // string |
|
381 // # length: 5 |
|
382 // array |
|
383 // |
|
384 // XXX FIXME XXX -- this format is fairly rigid, and doesn't allow for |
4687
|
385 // arbitrary comments. Someone should fix that. It does allow arbitrary |
|
386 // types however. |
4634
|
387 |
|
388 // Ugh. The signature of the compare method is not standard in older |
|
389 // versions of the GNU libstdc++. Do this instead: |
|
390 |
|
391 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t) |
|
392 |
|
393 std::string |
|
394 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
|
395 octave_value& tc, int count) |
|
396 { |
|
397 // Read name for this entry or break on EOF. |
|
398 |
|
399 std::string name = extract_keyword (is, "name"); |
|
400 |
|
401 if (name.empty ()) |
|
402 { |
|
403 if (count == 0) |
|
404 error ("load: empty name keyword or no data found in file `%s'", |
|
405 filename.c_str ()); |
|
406 |
|
407 return std::string (); |
|
408 } |
|
409 |
|
410 if (name == CELL_ELT_TAG) |
|
411 { |
|
412 // This is OK -- name won't be used. |
|
413 } |
|
414 else if (! valid_identifier (name)) |
|
415 { |
|
416 error ("load: bogus identifier `%s' found in file `%s'", |
|
417 name.c_str (), filename.c_str ()); |
|
418 return std::string (); |
|
419 } |
|
420 |
|
421 // Look for type keyword. |
|
422 |
|
423 std::string tag = extract_keyword (is, "type"); |
|
424 |
|
425 if (! tag.empty ()) |
|
426 { |
|
427 std::string typ; |
|
428 size_t pos = tag.rfind (' '); |
|
429 |
|
430 if (pos != NPOS) |
|
431 { |
|
432 global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global"); |
|
433 |
|
434 typ = global ? tag.substr (7) : tag; |
|
435 } |
|
436 else |
|
437 typ = tag; |
|
438 |
4687
|
439 // Special case for backward compatiablity. A small bit of cruft |
|
440 if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array")) |
|
441 tc = octave_value (charMatrix (), true); |
|
442 else |
|
443 tc = octave_value_typeinfo::lookup_type (typ); |
4634
|
444 |
4988
|
445 if (! tc.load_ascii (is)) |
|
446 error ("load: trouble reading ascii file `%s'", filename.c_str ()); |
4634
|
447 } |
|
448 else |
|
449 error ("load: failed to extract keyword specifying value type"); |
|
450 |
|
451 if (error_state) |
|
452 { |
|
453 error ("load: reading file %s", filename.c_str ()); |
|
454 return std::string (); |
|
455 } |
|
456 |
|
457 return name; |
|
458 } |
|
459 |
|
460 // Save the data from TC along with the corresponding NAME, and global |
|
461 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
|
462 // above for load_ascii_data. If NAME is empty, the name: line is not |
|
463 // generated. PRECISION specifies the number of decimal digits to print. |
|
464 // If STRIP_NAN_AND_INF is TRUE, rows containing NaNs are deleted, |
|
465 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
466 // but not so big that gnuplot can't handle it when trying to compute |
|
467 // axis ranges, etc.). |
|
468 // |
|
469 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
470 // |
|
471 // Returns 1 for success and 0 for failure. |
|
472 |
|
473 // XXX FIXME XXX -- should probably write the help string here too. |
|
474 |
|
475 bool |
|
476 save_ascii_data (std::ostream& os, const octave_value& val_arg, |
|
477 const std::string& name, bool& infnan_warned, |
|
478 bool strip_nan_and_inf, bool mark_as_global, |
|
479 int precision) |
|
480 { |
|
481 bool success = true; |
|
482 |
|
483 if (! precision) |
|
484 precision = Vsave_precision; |
|
485 |
|
486 if (! name.empty ()) |
|
487 os << "# name: " << name << "\n"; |
|
488 |
|
489 long old_precision = os.precision (); |
|
490 os.precision (precision); |
|
491 |
|
492 octave_value val = val_arg; |
|
493 |
4687
|
494 if (mark_as_global) |
|
495 os << "# type: global " << val.type_name () << "\n"; |
|
496 else |
|
497 os << "# type: " << val.type_name() << "\n"; |
4634
|
498 |
4687
|
499 success = val . save_ascii(os, infnan_warned, strip_nan_and_inf); |
4634
|
500 |
|
501 os.precision (old_precision); |
|
502 |
|
503 return (os && success); |
|
504 } |
|
505 |
|
506 bool |
|
507 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
|
508 const std::string& name) |
|
509 { |
|
510 bool infnan_warned = true; |
|
511 |
|
512 return save_ascii_data (os, t, name, infnan_warned, true, false, 0); |
|
513 } |
|
514 |
|
515 // Maybe this should be a static function in tree-plot.cc? |
|
516 |
|
517 // If TC is matrix, save it on stream OS in a format useful for |
|
518 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
519 // TRUE, assume a parametric 3-dimensional plot will be generated. |
|
520 |
|
521 bool |
|
522 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) |
|
523 { |
|
524 bool fail = false; |
|
525 |
|
526 int nr = tc.rows (); |
|
527 int nc = tc.columns (); |
|
528 |
|
529 if (tc.is_real_matrix ()) |
|
530 { |
|
531 os << "# 3D data...\n" |
|
532 << "# type: matrix\n" |
|
533 << "# total rows: " << nr << "\n" |
|
534 << "# total columns: " << nc << "\n"; |
|
535 |
|
536 if (parametric) |
|
537 { |
|
538 int extras = nc % 3; |
|
539 if (extras) |
|
540 warning ("ignoring last %d columns", extras); |
|
541 |
|
542 Matrix tmp = tc.matrix_value (); |
|
543 tmp = strip_infnan (tmp); |
|
544 nr = tmp.rows (); |
|
545 |
|
546 for (int i = 0; i < nc-extras; i += 3) |
|
547 { |
|
548 os << tmp.extract (0, i, nr-1, i+2); |
|
549 if (i+3 < nc-extras) |
|
550 os << "\n"; |
|
551 } |
|
552 } |
|
553 else |
|
554 { |
|
555 Matrix tmp = tc.matrix_value (); |
|
556 tmp = strip_infnan (tmp); |
|
557 nr = tmp.rows (); |
|
558 |
|
559 for (int i = 0; i < nc; i++) |
|
560 { |
|
561 os << tmp.extract (0, i, nr-1, i); |
|
562 if (i+1 < nc) |
|
563 os << "\n"; |
|
564 } |
|
565 } |
|
566 } |
|
567 else |
|
568 { |
|
569 ::error ("for now, I can only save real matrices in 3D format"); |
|
570 fail = true; |
|
571 } |
|
572 |
|
573 return (os && ! fail); |
|
574 } |
|
575 |
|
576 static int |
|
577 save_precision (void) |
|
578 { |
|
579 double val; |
|
580 if (builtin_real_scalar_variable ("save_precision", val) |
|
581 && ! xisnan (val)) |
|
582 { |
|
583 int ival = NINT (val); |
|
584 if (ival >= 0 && ival == val) |
|
585 { |
|
586 Vsave_precision = ival; |
|
587 return 0; |
|
588 } |
|
589 } |
|
590 gripe_invalid_value_specified ("save_precision"); |
|
591 return -1; |
|
592 } |
|
593 |
|
594 void |
|
595 symbols_of_ls_oct_ascii (void) |
|
596 { |
|
597 DEFVAR (save_precision, 15.0, save_precision, |
|
598 "-*- texinfo -*-\n\ |
|
599 @defvr {Built-in Variable} save_precision\n\ |
|
600 This variable specifies the number of digits to keep when saving data in\n\ |
|
601 text format. The default value is 17.\n\ |
|
602 @end defvr"); |
|
603 } |
|
604 |
|
605 /* |
|
606 ;;; Local Variables: *** |
|
607 ;;; mode: C++ *** |
|
608 ;;; End: *** |
|
609 */ |
|
610 |