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