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> |
|
36 #include <string> |
|
37 |
|
38 #include "byte-swap.h" |
|
39 #include "data-conv.h" |
|
40 #include "file-ops.h" |
|
41 #include "glob-match.h" |
|
42 #include "lo-mappers.h" |
|
43 #include "lo-sstream.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. |
|
71 static int Vsave_precision; |
|
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 { |
|
123 OSSTREAM buf; |
|
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 |
|
134 buf << OSSTREAM_ENDS; |
|
135 const char *tmp = OSSTREAM_C_STR (buf); |
|
136 OSSTREAM_FREEZE (buf); |
|
137 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
138 |
|
139 if (match) |
|
140 { |
|
141 OSSTREAM value; |
|
142 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
143 ; // Skip whitespace and the colon. |
|
144 |
|
145 if (c != '\n') |
|
146 { |
|
147 value << c; |
|
148 while (is.get (c) && c != '\n') |
|
149 value << c; |
|
150 } |
|
151 value << OSSTREAM_ENDS; |
|
152 retval = OSSTREAM_STR (value); |
|
153 OSSTREAM_FREEZE (value); |
|
154 break; |
|
155 } |
4687
|
156 else if (next_only) |
|
157 break; |
4634
|
158 } |
|
159 } |
|
160 |
|
161 int len = retval.length (); |
|
162 |
|
163 if (len > 0) |
|
164 { |
|
165 while (len) |
|
166 { |
|
167 c = retval[len-1]; |
|
168 |
|
169 if (c == ' ' || c == '\t') |
|
170 len--; |
|
171 else |
|
172 { |
|
173 retval.resize (len); |
|
174 break; |
|
175 } |
|
176 } |
|
177 } |
|
178 |
|
179 return retval; |
|
180 } |
|
181 |
|
182 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
183 // place it in TC, returning the name of the variable. If the value |
|
184 // is tagged as global in the file, return TRUE in GLOBAL. |
|
185 // |
4687
|
186 // Each type supplies its own function to load the data, and so this |
|
187 // function is extensible. |
|
188 // |
4634
|
189 // FILENAME is used for error messages. |
|
190 // |
|
191 // The data is expected to be in the following format: |
|
192 // |
|
193 // The input file must have a header followed by some data. |
|
194 // |
|
195 // All lines in the header must begin with a `#' character. |
|
196 // |
|
197 // The header must contain a list of keyword and value pairs with the |
|
198 // keyword and value separated by a colon. |
|
199 // |
|
200 // Keywords must appear in the following order: |
|
201 // |
|
202 // # name: <name> |
|
203 // # type: <type> |
|
204 // # <info> |
|
205 // |
4687
|
206 // Where, for the built in types are: |
4634
|
207 // |
|
208 // <name> : a valid identifier |
|
209 // |
|
210 // <type> : <typename> |
|
211 // | global <typename> |
|
212 // |
|
213 // <typename> : scalar |
|
214 // | complex scalar |
|
215 // | matrix |
|
216 // | complex matrix |
4687
|
217 // | bool |
|
218 // | bool matrix |
4634
|
219 // | string |
|
220 // | range |
|
221 // |
|
222 // <info> : <matrix info> |
|
223 // | <string info> |
|
224 // |
|
225 // <matrix info> : # rows: <integer> |
|
226 // : # columns: <integer> |
|
227 // |
4687
|
228 // <string info> : # elements: <integer> |
|
229 // : # length: <integer> (once before each string) |
4634
|
230 // |
4687
|
231 // For backward compatibility the type "string array" is treated as a |
|
232 // "string" type. Also "string" can have a single element with no elements |
|
233 // line such that |
|
234 // |
|
235 // <string info> : # length: <integer> |
4634
|
236 // |
|
237 // Formatted ASCII data follows the header. |
|
238 // |
|
239 // Example: |
|
240 // |
|
241 // # name: foo |
|
242 // # type: matrix |
|
243 // # rows: 2 |
|
244 // # columns: 2 |
|
245 // 2 4 |
|
246 // 1 3 |
|
247 // |
|
248 // Example: |
|
249 // |
|
250 // # name: foo |
4687
|
251 // # type: string |
4634
|
252 // # elements: 5 |
|
253 // # length: 4 |
|
254 // this |
|
255 // # length: 2 |
|
256 // is |
|
257 // # length: 1 |
|
258 // a |
|
259 // # length: 6 |
|
260 // string |
|
261 // # length: 5 |
|
262 // array |
|
263 // |
|
264 // XXX FIXME XXX -- this format is fairly rigid, and doesn't allow for |
4687
|
265 // arbitrary comments. Someone should fix that. It does allow arbitrary |
|
266 // types however. |
4634
|
267 |
|
268 // Ugh. The signature of the compare method is not standard in older |
|
269 // versions of the GNU libstdc++. Do this instead: |
|
270 |
|
271 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t) |
|
272 |
|
273 std::string |
|
274 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
|
275 octave_value& tc, int count) |
|
276 { |
|
277 // Read name for this entry or break on EOF. |
|
278 |
|
279 std::string name = extract_keyword (is, "name"); |
|
280 |
|
281 if (name.empty ()) |
|
282 { |
|
283 if (count == 0) |
|
284 error ("load: empty name keyword or no data found in file `%s'", |
|
285 filename.c_str ()); |
|
286 |
|
287 return std::string (); |
|
288 } |
|
289 |
|
290 if (name == CELL_ELT_TAG) |
|
291 { |
|
292 // This is OK -- name won't be used. |
|
293 } |
|
294 else if (! valid_identifier (name)) |
|
295 { |
|
296 error ("load: bogus identifier `%s' found in file `%s'", |
|
297 name.c_str (), filename.c_str ()); |
|
298 return std::string (); |
|
299 } |
|
300 |
|
301 // Look for type keyword. |
|
302 |
|
303 std::string tag = extract_keyword (is, "type"); |
|
304 |
|
305 if (! tag.empty ()) |
|
306 { |
|
307 std::string typ; |
|
308 size_t pos = tag.rfind (' '); |
|
309 |
|
310 if (pos != NPOS) |
|
311 { |
|
312 global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global"); |
|
313 |
|
314 typ = global ? tag.substr (7) : tag; |
|
315 } |
|
316 else |
|
317 typ = tag; |
|
318 |
4687
|
319 // Special case for backward compatiablity. A small bit of cruft |
|
320 if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array")) |
|
321 tc = octave_value (charMatrix (), true); |
|
322 else |
|
323 tc = octave_value_typeinfo::lookup_type (typ); |
4634
|
324 |
4988
|
325 if (! tc.load_ascii (is)) |
|
326 error ("load: trouble reading ascii file `%s'", filename.c_str ()); |
4634
|
327 } |
|
328 else |
|
329 error ("load: failed to extract keyword specifying value type"); |
|
330 |
|
331 if (error_state) |
|
332 { |
|
333 error ("load: reading file %s", filename.c_str ()); |
|
334 return std::string (); |
|
335 } |
|
336 |
|
337 return name; |
|
338 } |
|
339 |
|
340 // Save the data from TC along with the corresponding NAME, and global |
|
341 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
|
342 // above for load_ascii_data. If NAME is empty, the name: line is not |
|
343 // generated. PRECISION specifies the number of decimal digits to print. |
|
344 // If STRIP_NAN_AND_INF is TRUE, rows containing NaNs are deleted, |
|
345 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
346 // but not so big that gnuplot can't handle it when trying to compute |
|
347 // axis ranges, etc.). |
|
348 // |
|
349 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
350 // |
|
351 // Returns 1 for success and 0 for failure. |
|
352 |
|
353 // XXX FIXME XXX -- should probably write the help string here too. |
|
354 |
|
355 bool |
|
356 save_ascii_data (std::ostream& os, const octave_value& val_arg, |
|
357 const std::string& name, bool& infnan_warned, |
|
358 bool strip_nan_and_inf, bool mark_as_global, |
|
359 int precision) |
|
360 { |
|
361 bool success = true; |
|
362 |
|
363 if (! precision) |
|
364 precision = Vsave_precision; |
|
365 |
|
366 if (! name.empty ()) |
|
367 os << "# name: " << name << "\n"; |
|
368 |
|
369 long old_precision = os.precision (); |
|
370 os.precision (precision); |
|
371 |
|
372 octave_value val = val_arg; |
|
373 |
4687
|
374 if (mark_as_global) |
|
375 os << "# type: global " << val.type_name () << "\n"; |
|
376 else |
|
377 os << "# type: " << val.type_name() << "\n"; |
4634
|
378 |
4687
|
379 success = val . save_ascii(os, infnan_warned, strip_nan_and_inf); |
4634
|
380 |
|
381 os.precision (old_precision); |
|
382 |
|
383 return (os && success); |
|
384 } |
|
385 |
|
386 bool |
|
387 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
|
388 const std::string& name) |
|
389 { |
|
390 bool infnan_warned = true; |
|
391 |
|
392 return save_ascii_data (os, t, name, infnan_warned, true, false, 0); |
|
393 } |
|
394 |
|
395 // Maybe this should be a static function in tree-plot.cc? |
|
396 |
|
397 // If TC is matrix, save it on stream OS in a format useful for |
|
398 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
399 // TRUE, assume a parametric 3-dimensional plot will be generated. |
|
400 |
|
401 bool |
|
402 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) |
|
403 { |
|
404 bool fail = false; |
|
405 |
5275
|
406 octave_idx_type nr = tc.rows (); |
|
407 octave_idx_type nc = tc.columns (); |
4634
|
408 |
|
409 if (tc.is_real_matrix ()) |
|
410 { |
|
411 os << "# 3D data...\n" |
|
412 << "# type: matrix\n" |
|
413 << "# total rows: " << nr << "\n" |
|
414 << "# total columns: " << nc << "\n"; |
|
415 |
|
416 if (parametric) |
|
417 { |
5275
|
418 octave_idx_type extras = nc % 3; |
4634
|
419 if (extras) |
|
420 warning ("ignoring last %d columns", extras); |
|
421 |
|
422 Matrix tmp = tc.matrix_value (); |
|
423 tmp = strip_infnan (tmp); |
|
424 nr = tmp.rows (); |
|
425 |
5275
|
426 for (octave_idx_type i = 0; i < nc-extras; i += 3) |
4634
|
427 { |
|
428 os << tmp.extract (0, i, nr-1, i+2); |
|
429 if (i+3 < nc-extras) |
|
430 os << "\n"; |
|
431 } |
|
432 } |
|
433 else |
|
434 { |
|
435 Matrix tmp = tc.matrix_value (); |
|
436 tmp = strip_infnan (tmp); |
|
437 nr = tmp.rows (); |
|
438 |
5275
|
439 for (octave_idx_type i = 0; i < nc; i++) |
4634
|
440 { |
|
441 os << tmp.extract (0, i, nr-1, i); |
|
442 if (i+1 < nc) |
|
443 os << "\n"; |
|
444 } |
|
445 } |
|
446 } |
|
447 else |
|
448 { |
|
449 ::error ("for now, I can only save real matrices in 3D format"); |
|
450 fail = true; |
|
451 } |
|
452 |
|
453 return (os && ! fail); |
|
454 } |
|
455 |
|
456 static int |
|
457 save_precision (void) |
|
458 { |
|
459 double val; |
|
460 if (builtin_real_scalar_variable ("save_precision", val) |
|
461 && ! xisnan (val)) |
|
462 { |
|
463 int ival = NINT (val); |
|
464 if (ival >= 0 && ival == val) |
|
465 { |
|
466 Vsave_precision = ival; |
|
467 return 0; |
|
468 } |
|
469 } |
|
470 gripe_invalid_value_specified ("save_precision"); |
|
471 return -1; |
|
472 } |
|
473 |
|
474 void |
|
475 symbols_of_ls_oct_ascii (void) |
|
476 { |
|
477 DEFVAR (save_precision, 15.0, save_precision, |
|
478 "-*- texinfo -*-\n\ |
|
479 @defvr {Built-in Variable} save_precision\n\ |
|
480 This variable specifies the number of digits to keep when saving data in\n\ |
|
481 text format. The default value is 17.\n\ |
|
482 @end defvr"); |
|
483 } |
|
484 |
|
485 /* |
|
486 ;;; Local Variables: *** |
|
487 ;;; mode: C++ *** |
|
488 ;;; End: *** |
|
489 */ |
|
490 |