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