604
|
1 // load-save.cc -*- C++ -*- |
|
2 /* |
|
3 |
1884
|
4 Copyright (C) 1996 John W. Eaton |
604
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
604
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
604
|
26 #endif |
|
27 |
1343
|
28 #include <cfloat> |
|
29 #include <climits> |
|
30 #include <cstring> |
|
31 #include <cctype> |
|
32 |
1728
|
33 #include <string> |
|
34 |
604
|
35 #include <iostream.h> |
|
36 #include <fstream.h> |
|
37 #include <strstream.h> |
|
38 |
1961
|
39 #include "byte-swap.h" |
|
40 #include "data-conv.h" |
|
41 #include "float-fmt.h" |
1792
|
42 #include "oct-glob.h" |
1755
|
43 #include "str-vec.h" |
|
44 |
1352
|
45 #include "defun.h" |
604
|
46 #include "error.h" |
777
|
47 #include "gripes.h" |
1352
|
48 #include "help.h" |
|
49 #include "load-save.h" |
1742
|
50 #include "mappers.h" |
1750
|
51 #include "oct-obj.h" |
1352
|
52 #include "pager.h" |
1750
|
53 #include "pt-exp.h" |
1352
|
54 #include "symtab.h" |
|
55 #include "sysdep.h" |
|
56 #include "unwind-prot.h" |
|
57 #include "user-prefs.h" |
604
|
58 #include "utils.h" |
|
59 |
872
|
60 // Used when converting Inf to something that gnuplot can read. |
|
61 |
|
62 #ifndef OCT_RBV |
|
63 #define OCT_RBV DBL_MAX / 100.0 |
|
64 #endif |
|
65 |
604
|
66 enum load_save_format |
|
67 { |
|
68 LS_ASCII, |
|
69 LS_BINARY, |
|
70 LS_MAT_BINARY, |
|
71 LS_UNKNOWN, |
|
72 }; |
|
73 |
|
74 // Return nonzero if S is a valid identifier. |
|
75 |
|
76 static int |
|
77 valid_identifier (char *s) |
|
78 { |
|
79 if (! s || ! (isalnum (*s) || *s == '_')) |
|
80 return 0; |
|
81 |
|
82 while (*++s != '\0') |
|
83 if (! (isalnum (*s) || *s == '_')) |
|
84 return 0; |
|
85 |
|
86 return 1; |
|
87 } |
|
88 |
|
89 // Return nonzero if any element of M is not an integer. Also extract |
|
90 // the largest and smallest values and return them in MAX_VAL and MIN_VAL. |
|
91 |
|
92 static int |
|
93 all_parts_int (const Matrix& m, double& max_val, double& min_val) |
|
94 { |
|
95 int nr = m.rows (); |
|
96 int nc = m.columns (); |
|
97 |
|
98 if (nr > 0 && nc > 0) |
|
99 { |
|
100 max_val = m.elem (0, 0); |
|
101 min_val = m.elem (0, 0); |
|
102 } |
|
103 else |
|
104 return 0; |
|
105 |
|
106 for (int j = 0; j < nc; j++) |
|
107 for (int i = 0; i < nr; i++) |
|
108 { |
|
109 double val = m.elem (i, j); |
|
110 |
|
111 if (val > max_val) |
|
112 max_val = val; |
|
113 |
|
114 if (val < min_val) |
|
115 min_val = val; |
|
116 |
|
117 if (D_NINT (val) != val) |
|
118 return 0; |
|
119 } |
|
120 return 1; |
|
121 } |
|
122 |
|
123 // Return nonzero if any element of CM has a non-integer real or |
|
124 // imaginary part. Also extract the largest and smallest (real or |
|
125 // imaginary) values and return them in MAX_VAL and MIN_VAL. |
|
126 |
|
127 static int |
|
128 all_parts_int (const ComplexMatrix& m, double& max_val, double& min_val) |
|
129 { |
|
130 int nr = m.rows (); |
|
131 int nc = m.columns (); |
|
132 |
|
133 if (nr > 0 && nc > 0) |
|
134 { |
|
135 Complex val = m.elem (0, 0); |
|
136 |
|
137 double r_val = real (val); |
|
138 double i_val = imag (val); |
|
139 |
|
140 max_val = r_val; |
|
141 min_val = r_val; |
|
142 |
|
143 if (i_val > max_val) |
|
144 max_val = i_val; |
|
145 |
|
146 if (i_val < max_val) |
|
147 min_val = i_val; |
|
148 } |
|
149 else |
|
150 return 0; |
|
151 |
|
152 for (int j = 0; j < nc; j++) |
|
153 for (int i = 0; i < nr; i++) |
|
154 { |
|
155 Complex val = m.elem (i, j); |
|
156 |
|
157 double r_val = real (val); |
|
158 double i_val = imag (val); |
|
159 |
|
160 if (r_val > max_val) |
|
161 max_val = r_val; |
|
162 |
|
163 if (i_val > max_val) |
|
164 max_val = i_val; |
|
165 |
|
166 if (r_val < min_val) |
|
167 min_val = r_val; |
|
168 |
|
169 if (i_val < min_val) |
|
170 min_val = i_val; |
|
171 |
|
172 if (D_NINT (r_val) != r_val || D_NINT (i_val) != i_val) |
|
173 return 0; |
|
174 } |
|
175 return 1; |
|
176 } |
|
177 |
630
|
178 static int |
|
179 too_large_for_float (const Matrix& m) |
|
180 { |
|
181 int nr = m.rows (); |
|
182 int nc = m.columns (); |
|
183 |
|
184 for (int j = 0; j < nc; j++) |
|
185 for (int i = 0; i < nr; i++) |
|
186 { |
1312
|
187 double val = m.elem (i, j); |
|
188 |
|
189 if (val > FLT_MAX || val < FLT_MIN) |
630
|
190 return 1; |
|
191 } |
|
192 |
|
193 return 0; |
|
194 } |
|
195 |
|
196 static int |
|
197 too_large_for_float (const ComplexMatrix& m) |
|
198 { |
|
199 int nr = m.rows (); |
|
200 int nc = m.columns (); |
|
201 |
|
202 for (int j = 0; j < nc; j++) |
|
203 for (int i = 0; i < nr; i++) |
|
204 { |
|
205 Complex val = m.elem (i, j); |
|
206 |
|
207 double r_val = real (val); |
|
208 double i_val = imag (val); |
|
209 |
|
210 if (r_val > FLT_MAX |
|
211 || i_val > FLT_MAX |
|
212 || r_val < FLT_MIN |
|
213 || i_val < FLT_MIN) |
|
214 return 1; |
|
215 } |
|
216 |
|
217 return 0; |
|
218 } |
|
219 |
|
220 // XXX FIXME XXX -- shouldn't this be implemented in terms of other |
|
221 // functions that are already available? |
604
|
222 |
|
223 // Install a variable with name NAME and the value specified TC in the |
|
224 // symbol table. If FORCE is nonzero, replace any existing definition |
|
225 // for NAME. If GLOBAL is nonzero, make the variable global. |
|
226 // |
|
227 // Assumes TC is defined. |
|
228 |
|
229 static void |
|
230 install_loaded_variable (int force, char *name, const tree_constant& tc, |
|
231 int global, char *doc) |
|
232 { |
1358
|
233 // Is there already a symbol by this name? If so, what is it? |
604
|
234 |
|
235 symbol_record *lsr = curr_sym_tab->lookup (name, 0, 0); |
|
236 |
|
237 int is_undefined = 1; |
|
238 int is_variable = 0; |
|
239 int is_function = 0; |
|
240 int is_global = 0; |
|
241 |
|
242 if (lsr) |
|
243 { |
|
244 is_undefined = ! lsr->is_defined (); |
|
245 is_variable = lsr->is_variable (); |
|
246 is_function = lsr->is_function (); |
|
247 is_global = lsr->is_linked_to_global (); |
|
248 } |
|
249 |
|
250 symbol_record *sr = 0; |
|
251 |
|
252 if (global) |
|
253 { |
|
254 if (is_global || is_undefined) |
|
255 { |
|
256 if (force || is_undefined) |
|
257 { |
|
258 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
259 link_to_global_variable (lsr); |
|
260 sr = lsr; |
|
261 } |
|
262 else |
|
263 { |
|
264 warning ("load: global variable name `%s' exists.", name); |
|
265 warning ("use `load -force' to overwrite"); |
|
266 } |
|
267 } |
|
268 else if (is_function) |
|
269 { |
|
270 if (force) |
|
271 { |
|
272 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
273 link_to_global_variable (lsr); |
|
274 sr = lsr; |
|
275 } |
|
276 else |
|
277 { |
|
278 warning ("load: `%s' is currently a function in this scope", name); |
|
279 warning ("`load -force' will load variable and hide function"); |
|
280 } |
|
281 } |
|
282 else if (is_variable) |
|
283 { |
|
284 if (force) |
|
285 { |
|
286 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
287 link_to_global_variable (lsr); |
|
288 sr = lsr; |
|
289 } |
|
290 else |
|
291 { |
|
292 warning ("load: local variable name `%s' exists.", name); |
|
293 warning ("use `load -force' to overwrite"); |
|
294 } |
|
295 } |
|
296 else |
774
|
297 error ("load: unable to load data for unknown symbol type"); |
604
|
298 } |
|
299 else |
|
300 { |
|
301 if (is_global) |
|
302 { |
|
303 if (force || is_undefined) |
|
304 { |
|
305 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
306 link_to_global_variable (lsr); |
|
307 sr = lsr; |
|
308 } |
|
309 else |
|
310 { |
|
311 warning ("load: global variable name `%s' exists.", name); |
|
312 warning ("use `load -force' to overwrite"); |
|
313 } |
|
314 } |
|
315 else if (is_function) |
|
316 { |
|
317 if (force) |
|
318 { |
|
319 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
320 link_to_global_variable (lsr); |
|
321 sr = lsr; |
|
322 } |
|
323 else |
|
324 { |
|
325 warning ("load: `%s' is currently a function in this scope", name); |
|
326 warning ("`load -force' will load variable and hide function"); |
|
327 } |
|
328 } |
|
329 else if (is_variable || is_undefined) |
|
330 { |
|
331 if (force || is_undefined) |
|
332 { |
|
333 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
334 sr = lsr; |
|
335 } |
|
336 else |
|
337 { |
|
338 warning ("load: local variable name `%s' exists.", name); |
|
339 warning ("use `load -force' to overwrite"); |
|
340 } |
|
341 } |
|
342 else |
774
|
343 error ("load: unable to load data for unknown symbol type"); |
604
|
344 } |
|
345 |
|
346 if (sr) |
|
347 { |
|
348 tree_constant *tmp_tc = new tree_constant (tc); |
|
349 sr->define (tmp_tc); |
|
350 if (doc) |
|
351 sr->document (doc); |
|
352 return; |
|
353 } |
|
354 else |
|
355 error ("load: unable to load variable `%s'", name); |
|
356 |
|
357 return; |
|
358 } |
|
359 |
|
360 // Functions for reading ascii data. |
|
361 |
|
362 // Skip white space and comments on stream IS. |
|
363 |
|
364 static void |
|
365 skip_comments (istream& is) |
|
366 { |
|
367 char c = '\0'; |
|
368 while (is.get (c)) |
|
369 { |
|
370 if (c == ' ' || c == '\t' || c == '\n') |
|
371 ; // Skip whitespace on way to beginning of next line. |
|
372 else |
|
373 break; |
|
374 } |
|
375 |
|
376 for (;;) |
|
377 { |
|
378 if (is && c == '#') |
|
379 while (is.get (c) && c != '\n') |
|
380 ; // Skip to beginning of next line, ignoring everything. |
|
381 else |
|
382 break; |
|
383 } |
|
384 } |
|
385 |
|
386 // Extract a KEYWORD and its value from stream IS, returning the |
|
387 // associated value in a new string. |
|
388 // |
|
389 // Input should look something like: |
|
390 // |
918
|
391 // #[ \t]*keyword[ \t]*:[ \t]*string-value[ \t]*\n |
604
|
392 |
|
393 static char * |
|
394 extract_keyword (istream& is, char *keyword) |
|
395 { |
|
396 ostrstream buf; |
|
397 |
|
398 char *retval = 0; |
|
399 |
|
400 char c; |
|
401 while (is.get (c)) |
|
402 { |
|
403 if (c == '#') |
|
404 { |
|
405 while (is.get (c) && (c == ' ' || c == '\t' || c == '#')) |
|
406 ; // Skip whitespace and comment characters. |
|
407 |
|
408 if (isalpha (c)) |
|
409 buf << c; |
|
410 |
|
411 while (is.get (c) && isalpha (c)) |
|
412 buf << c; |
|
413 |
|
414 buf << ends; |
|
415 char *tmp = buf.str (); |
|
416 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
417 delete [] tmp; |
|
418 |
|
419 if (match) |
|
420 { |
|
421 ostrstream value; |
|
422 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
423 ; // Skip whitespace and the colon. |
|
424 |
|
425 if (c != '\n') |
|
426 { |
|
427 value << c; |
|
428 while (is.get (c) && c != '\n') |
|
429 value << c; |
|
430 } |
|
431 value << ends; |
|
432 retval = value.str (); |
|
433 break; |
|
434 } |
|
435 } |
|
436 } |
918
|
437 |
|
438 if (retval) |
|
439 { |
|
440 int len = strlen (retval); |
|
441 if (len > 0) |
|
442 { |
|
443 char *ptr = retval + len - 1; |
|
444 while (*ptr == ' ' || *ptr == '\t') |
|
445 ptr--; |
|
446 *(ptr+1) = '\0'; |
|
447 } |
|
448 } |
|
449 |
604
|
450 return retval; |
|
451 } |
|
452 |
|
453 // Match KEYWORD on stream IS, placing the associated value in VALUE, |
|
454 // returning 1 if successful and 0 otherwise. |
|
455 // |
|
456 // Input should look something like: |
|
457 // |
918
|
458 // [ \t]*keyword[ \t]*int-value.*\n |
604
|
459 |
|
460 static int |
|
461 extract_keyword (istream& is, char *keyword, int& value) |
|
462 { |
|
463 ostrstream buf; |
|
464 |
|
465 int status = 0; |
|
466 value = 0; |
|
467 |
|
468 char c; |
|
469 while (is.get (c)) |
|
470 { |
|
471 if (c == '#') |
|
472 { |
|
473 while (is.get (c) && (c == ' ' || c == '\t' || c == '#')) |
|
474 ; // Skip whitespace and comment characters. |
|
475 |
|
476 if (isalpha (c)) |
|
477 buf << c; |
|
478 |
|
479 while (is.get (c) && isalpha (c)) |
|
480 buf << c; |
|
481 |
|
482 buf << ends; |
|
483 char *tmp = buf.str (); |
|
484 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
485 delete [] tmp; |
|
486 |
|
487 if (match) |
|
488 { |
|
489 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
490 ; // Skip whitespace and the colon. |
|
491 |
|
492 is.putback (c); |
|
493 if (c != '\n') |
|
494 is >> value; |
|
495 if (is) |
|
496 status = 1; |
|
497 while (is.get (c) && c != '\n') |
|
498 ; // Skip to beginning of next line; |
|
499 break; |
|
500 } |
|
501 } |
|
502 } |
|
503 return status; |
|
504 } |
|
505 |
|
506 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
507 // place it in TC, returning the name of the variable. If the value |
|
508 // is tagged as global in the file, return nonzero in GLOBAL. |
|
509 // |
|
510 // FILENAME is used for error messages. |
|
511 // |
|
512 // The data is expected to be in the following format: |
|
513 // |
|
514 // The input file must have a header followed by some data. |
|
515 // |
|
516 // All lines in the header must begin with a `#' character. |
|
517 // |
|
518 // The header must contain a list of keyword and value pairs with the |
|
519 // keyword and value separated by a colon. |
|
520 // |
|
521 // Keywords must appear in the following order: |
|
522 // |
|
523 // # name: <name> |
|
524 // # type: <type> |
|
525 // # <info> |
|
526 // |
|
527 // Where: |
|
528 // |
|
529 // <name> : a valid identifier |
|
530 // |
|
531 // <type> : <typename> |
|
532 // | global <typename> |
|
533 // |
|
534 // <typename> : scalar |
|
535 // | complex scalar |
|
536 // | matrix |
|
537 // | complex matrix |
|
538 // | string |
|
539 // | range |
1427
|
540 // | string array |
604
|
541 // |
|
542 // <info> : <matrix info> |
|
543 // | <string info> |
1427
|
544 // | <string array info> |
604
|
545 // |
|
546 // <matrix info> : # rows: <integer> |
1427
|
547 // : # columns: <integer> |
604
|
548 // |
1427
|
549 // <string info> : # length: <integer> |
|
550 // |
|
551 // <string array info> : # elements: <integer> |
|
552 // : # length: <integer> (once before each string) |
604
|
553 // |
|
554 // Formatted ASCII data follows the header. |
|
555 // |
|
556 // Example: |
|
557 // |
|
558 // # name: foo |
|
559 // # type: matrix |
|
560 // # rows: 2 |
|
561 // # columns: 2 |
|
562 // 2 4 |
|
563 // 1 3 |
|
564 // |
1427
|
565 // Example: |
|
566 // |
|
567 // # name: foo |
|
568 // # type: string array |
|
569 // # elements: 5 |
|
570 // # length: 4 |
|
571 // this |
|
572 // # length: 2 |
|
573 // is |
|
574 // # length: 1 |
|
575 // a |
|
576 // # length: 6 |
|
577 // string |
|
578 // # length: 5 |
|
579 // array |
|
580 // |
604
|
581 // XXX FIXME XXX -- this format is fairly rigid, and doesn't allow for |
|
582 // arbitrary comments, etc. Someone should fix that. |
|
583 |
|
584 static char * |
1755
|
585 read_ascii_data (istream& is, const string& filename, int& global, |
604
|
586 tree_constant& tc) |
|
587 { |
1358
|
588 // Read name for this entry or break on EOF. |
604
|
589 |
|
590 char *name = extract_keyword (is, "name"); |
|
591 |
|
592 if (! name) |
|
593 return 0; |
|
594 |
|
595 if (! *name) |
|
596 { |
1755
|
597 error ("load: empty name keyword found in file `%s'", |
|
598 filename.c_str ()); |
604
|
599 delete [] name; |
|
600 return 0; |
|
601 } |
|
602 |
|
603 |
|
604 if (! valid_identifier (name)) |
|
605 { |
1755
|
606 error ("load: bogus identifier `%s' found in file `%s'", name, |
|
607 filename.c_str ()); |
604
|
608 delete [] name; |
|
609 return 0; |
|
610 } |
|
611 |
1358
|
612 // Look for type keyword. |
604
|
613 |
|
614 char *tag = extract_keyword (is, "type"); |
|
615 |
|
616 if (tag && *tag) |
|
617 { |
|
618 char *ptr = strchr (tag, ' '); |
|
619 if (ptr) |
|
620 { |
|
621 *ptr = '\0'; |
|
622 global = (strncmp (tag, "global", 6) == 0); |
|
623 *ptr = ' '; |
|
624 if (global) |
|
625 ptr++; |
|
626 else |
|
627 ptr = tag; |
|
628 } |
|
629 else |
|
630 ptr = tag; |
|
631 |
|
632 if (strncmp (ptr, "scalar", 6) == 0) |
|
633 { |
|
634 double tmp; |
|
635 is >> tmp; |
|
636 if (is) |
|
637 tc = tmp; |
|
638 else |
|
639 error ("load: failed to load scalar constant"); |
|
640 } |
|
641 else if (strncmp (ptr, "matrix", 6) == 0) |
|
642 { |
|
643 int nr = 0, nc = 0; |
|
644 |
1275
|
645 if (extract_keyword (is, "rows", nr) && nr >= 0 |
|
646 && extract_keyword (is, "columns", nc) && nc >= 0) |
604
|
647 { |
1275
|
648 if (nr > 0 && nc > 0) |
|
649 { |
|
650 Matrix tmp (nr, nc); |
|
651 is >> tmp; |
|
652 if (is) |
|
653 tc = tmp; |
|
654 else |
|
655 error ("load: failed to load matrix constant"); |
|
656 } |
|
657 else if (nr == 0 || nc == 0) |
|
658 tc = Matrix (nr, nc); |
604
|
659 else |
1275
|
660 panic_impossible (); |
604
|
661 } |
|
662 else |
|
663 error ("load: failed to extract number of rows and columns"); |
|
664 } |
|
665 else if (strncmp (ptr, "complex scalar", 14) == 0) |
|
666 { |
|
667 Complex tmp; |
|
668 is >> tmp; |
|
669 if (is) |
|
670 tc = tmp; |
|
671 else |
|
672 error ("load: failed to load complex scalar constant"); |
|
673 } |
|
674 else if (strncmp (ptr, "complex matrix", 14) == 0) |
|
675 { |
|
676 int nr = 0, nc = 0; |
|
677 |
|
678 if (extract_keyword (is, "rows", nr) && nr > 0 |
|
679 && extract_keyword (is, "columns", nc) && nc > 0) |
|
680 { |
|
681 ComplexMatrix tmp (nr, nc); |
|
682 is >> tmp; |
|
683 if (is) |
|
684 tc = tmp; |
|
685 else |
|
686 error ("load: failed to load complex matrix constant"); |
|
687 } |
|
688 else |
|
689 error ("load: failed to extract number of rows and columns"); |
|
690 } |
1427
|
691 else if (strncmp (ptr, "string array", 12) == 0) |
|
692 { |
|
693 int elements; |
|
694 if (extract_keyword (is, "elements", elements) && elements > 0) |
|
695 { |
1572
|
696 // XXX FIXME XXX -- need to be able to get max length |
|
697 // before doing anything. |
|
698 |
|
699 charMatrix chm (elements, 0); |
|
700 int max_len = 0; |
1427
|
701 for (int i = 0; i < elements; i++) |
|
702 { |
|
703 int len; |
|
704 if (extract_keyword (is, "length", len) && len > 0) |
|
705 { |
|
706 char *tmp = new char [len]; |
|
707 if (! is.read (tmp, len)) |
|
708 { |
|
709 error ("load: failed to load string constant"); |
|
710 break; |
|
711 } |
|
712 else |
1572
|
713 { |
|
714 if (len > max_len) |
|
715 { |
|
716 max_len = len; |
|
717 chm.resize (elements, max_len, 0); |
|
718 } |
|
719 chm.insert (tmp, i, 0); |
|
720 } |
1427
|
721 delete [] tmp; |
|
722 } |
|
723 else |
|
724 error ("load: failed to extract string length for element %d", i+1); |
|
725 } |
|
726 |
|
727 if (! error_state) |
1572
|
728 tc = chm; |
1427
|
729 } |
|
730 else |
|
731 error ("load: failed to extract number of string elements"); |
|
732 } |
604
|
733 else if (strncmp (ptr, "string", 6) == 0) |
|
734 { |
|
735 int len; |
|
736 if (extract_keyword (is, "length", len) && len > 0) |
|
737 { |
|
738 char *tmp = new char [len+1]; |
|
739 is.get (tmp, len+1, EOF); |
|
740 if (is) |
|
741 tc = tmp; |
|
742 else |
|
743 error ("load: failed to load string constant"); |
|
744 } |
|
745 else |
|
746 error ("load: failed to extract string length"); |
|
747 } |
|
748 else if (strncmp (ptr, "range", 5) == 0) |
|
749 { |
1358
|
750 // # base, limit, range comment added by save(). |
|
751 |
604
|
752 skip_comments (is); |
|
753 Range tmp; |
|
754 is >> tmp; |
|
755 if (is) |
|
756 tc = tmp; |
|
757 else |
|
758 error ("load: failed to load range constant"); |
|
759 } |
|
760 else |
|
761 error ("load: unknown constant type `%s'", tag); |
|
762 } |
|
763 else |
|
764 error ("load: failed to extract keyword specifying value type"); |
|
765 |
|
766 delete [] tag; |
|
767 |
|
768 if (error_state) |
|
769 { |
1755
|
770 error ("load: reading file %s", filename.c_str ()); |
604
|
771 return 0; |
|
772 } |
|
773 |
|
774 return name; |
|
775 } |
|
776 |
|
777 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
778 // place it in TC, returning the name of the variable. If the value |
|
779 // is tagged as global in the file, return nonzero in GLOBAL. If SWAP |
|
780 // is nonzero, swap bytes after reading. |
|
781 // |
|
782 // The data is expected to be in the following format: |
|
783 // |
867
|
784 // Header (one per file): |
|
785 // ===================== |
604
|
786 // |
867
|
787 // object type bytes |
|
788 // ------ ---- ----- |
|
789 // magic number string 10 |
604
|
790 // |
867
|
791 // float format integer 1 |
|
792 // |
604
|
793 // |
867
|
794 // Data (one set for each item): |
|
795 // ============================ |
604
|
796 // |
867
|
797 // object type bytes |
|
798 // ------ ---- ----- |
|
799 // name_length integer 4 |
604
|
800 // |
867
|
801 // name string name_length |
604
|
802 // |
867
|
803 // doc_length integer 4 |
|
804 // |
|
805 // doc string doc_length |
604
|
806 // |
867
|
807 // global flag integer 1 |
604
|
808 // |
867
|
809 // data type integer 1 |
604
|
810 // |
867
|
811 // data (one of): |
|
812 // |
|
813 // scalar: |
|
814 // data real 8 |
604
|
815 // |
867
|
816 // complex scalar: |
|
817 // data complex 16 |
|
818 // |
|
819 // matrix: |
|
820 // rows integer 4 |
|
821 // columns integer 4 |
|
822 // data real r*c*8 |
604
|
823 // |
867
|
824 // complex matrix: |
|
825 // rows integer 4 |
|
826 // columns integer 4 |
|
827 // data complex r*c*16 |
604
|
828 // |
867
|
829 // string: |
|
830 // length int 4 |
|
831 // data string length |
604
|
832 // |
867
|
833 // range: |
|
834 // base real 8 |
|
835 // limit real 8 |
|
836 // increment real 8 |
604
|
837 // |
1427
|
838 // string array |
|
839 // elements int 4 |
|
840 // |
|
841 // for each element: |
|
842 // length int 4 |
|
843 // data string length |
|
844 // |
604
|
845 // FILENAME is used for error messages. |
|
846 |
|
847 static char * |
|
848 read_binary_data (istream& is, int swap, floating_point_format fmt, |
1755
|
849 const string& filename, int& global, |
604
|
850 tree_constant& tc, char *&doc) |
|
851 { |
|
852 char tmp = 0; |
|
853 |
|
854 FOUR_BYTE_INT name_len = 0, doc_len = 0; |
|
855 char *name = 0; |
|
856 |
|
857 doc = 0; |
|
858 |
1358
|
859 // We expect to fail here, at the beginning of a record, so not |
|
860 // being able to read another name should not result in an error. |
867
|
861 |
604
|
862 is.read (&name_len, 4); |
|
863 if (! is) |
867
|
864 return 0; |
604
|
865 if (swap) |
|
866 swap_4_bytes ((char *) &name_len); |
|
867 |
|
868 name = new char [name_len+1]; |
|
869 name[name_len] = '\0'; |
|
870 if (! is.read (name, name_len)) |
|
871 goto data_read_error; |
|
872 |
|
873 is.read (&doc_len, 4); |
|
874 if (! is) |
|
875 goto data_read_error; |
|
876 if (swap) |
|
877 swap_4_bytes ((char *) &doc_len); |
|
878 |
|
879 doc = new char [doc_len+1]; |
|
880 doc[doc_len] = '\0'; |
|
881 if (! is.read (doc, doc_len)) |
|
882 goto data_read_error; |
|
883 |
|
884 if (! is.read (&tmp, 1)) |
|
885 goto data_read_error; |
|
886 global = tmp ? 1 : 0; |
|
887 |
|
888 tmp = 0; |
|
889 if (! is.read (&tmp, 1)) |
|
890 goto data_read_error; |
|
891 |
|
892 switch (tmp) |
|
893 { |
|
894 case 1: |
|
895 { |
630
|
896 if (! is.read (&tmp, 1)) |
604
|
897 goto data_read_error; |
630
|
898 double dtmp; |
|
899 read_doubles (is, &dtmp, (save_type) tmp, 1, swap, fmt); |
774
|
900 if (error_state || ! is) |
630
|
901 goto data_read_error; |
604
|
902 tc = dtmp; |
|
903 } |
|
904 break; |
|
905 |
|
906 case 2: |
|
907 { |
|
908 FOUR_BYTE_INT nr, nc; |
|
909 if (! is.read (&nr, 4)) |
|
910 goto data_read_error; |
|
911 if (swap) |
|
912 swap_4_bytes ((char *) &nr); |
|
913 if (! is.read (&nc, 4)) |
|
914 goto data_read_error; |
|
915 if (swap) |
|
916 swap_4_bytes ((char *) &nc); |
|
917 if (! is.read (&tmp, 1)) |
|
918 goto data_read_error; |
|
919 Matrix m (nr, nc); |
|
920 double *re = m.fortran_vec (); |
|
921 int len = nr * nc; |
|
922 read_doubles (is, re, (save_type) tmp, len, swap, fmt); |
774
|
923 if (error_state || ! is) |
604
|
924 goto data_read_error; |
|
925 tc = m; |
|
926 } |
|
927 break; |
|
928 |
|
929 case 3: |
|
930 { |
630
|
931 if (! is.read (&tmp, 1)) |
604
|
932 goto data_read_error; |
630
|
933 Complex ctmp; |
|
934 read_doubles (is, (double *) &ctmp, (save_type) tmp, 2, swap, fmt); |
774
|
935 if (error_state || ! is) |
630
|
936 goto data_read_error; |
604
|
937 tc = ctmp; |
|
938 } |
|
939 break; |
|
940 |
|
941 case 4: |
|
942 { |
|
943 FOUR_BYTE_INT nr, nc; |
|
944 if (! is.read (&nr, 4)) |
|
945 goto data_read_error; |
|
946 if (swap) |
|
947 swap_4_bytes ((char *) &nr); |
|
948 if (! is.read (&nc, 4)) |
|
949 goto data_read_error; |
|
950 if (swap) |
|
951 swap_4_bytes ((char *) &nc); |
|
952 if (! is.read (&tmp, 1)) |
|
953 goto data_read_error; |
|
954 ComplexMatrix m (nr, nc); |
|
955 Complex *im = m.fortran_vec (); |
|
956 int len = nr * nc; |
630
|
957 read_doubles (is, (double *) im, (save_type) tmp, 2*len, |
|
958 swap, fmt); |
774
|
959 if (error_state || ! is) |
604
|
960 goto data_read_error; |
|
961 tc = m; |
|
962 } |
|
963 break; |
|
964 |
|
965 case 5: |
|
966 { |
1427
|
967 FOUR_BYTE_INT len; |
604
|
968 if (! is.read (&len, 4)) |
|
969 goto data_read_error; |
|
970 if (swap) |
|
971 swap_4_bytes ((char *) &len); |
|
972 char *s = new char [len+1]; |
|
973 if (! is.read (s, len)) |
|
974 { |
|
975 delete [] s; |
|
976 goto data_read_error; |
|
977 } |
|
978 s[len] = '\0'; |
|
979 tc = s; |
|
980 } |
|
981 break; |
|
982 |
|
983 case 6: |
|
984 { |
630
|
985 if (! is.read (&tmp, 1)) |
|
986 goto data_read_error; |
604
|
987 double bas, lim, inc; |
|
988 if (! is.read (&bas, 8)) |
|
989 goto data_read_error; |
|
990 if (swap) |
|
991 swap_8_bytes ((char *) &bas); |
|
992 if (! is.read (&lim, 8)) |
|
993 goto data_read_error; |
|
994 if (swap) |
|
995 swap_8_bytes ((char *) &lim); |
|
996 if (! is.read (&inc, 8)) |
|
997 goto data_read_error; |
|
998 if (swap) |
|
999 swap_8_bytes ((char *) &inc); |
|
1000 Range r (bas, lim, inc); |
|
1001 tc = r; |
|
1002 } |
|
1003 break; |
|
1004 |
1427
|
1005 case 7: |
|
1006 { |
|
1007 FOUR_BYTE_INT elements; |
|
1008 if (! is.read (&elements, 4)) |
|
1009 goto data_read_error; |
|
1010 if (swap) |
|
1011 swap_4_bytes ((char *) &elements); |
1572
|
1012 charMatrix chm (elements, 0); |
|
1013 int max_len = 0; |
1427
|
1014 for (int i = 0; i < elements; i++) |
|
1015 { |
|
1016 FOUR_BYTE_INT len; |
|
1017 if (! is.read (&len, 4)) |
|
1018 goto data_read_error; |
|
1019 if (swap) |
|
1020 swap_4_bytes ((char *) &len); |
|
1021 char *tmp = new char [len]; |
|
1022 if (! is.read (tmp, len)) |
|
1023 { |
|
1024 delete [] tmp; |
|
1025 goto data_read_error; |
|
1026 } |
1572
|
1027 if (len > max_len) |
|
1028 { |
|
1029 max_len = len; |
|
1030 chm.resize (elements, max_len, 0); |
|
1031 } |
|
1032 chm.insert (tmp, i, 0); |
1427
|
1033 delete [] tmp; |
|
1034 } |
1572
|
1035 tc = chm; |
1427
|
1036 } |
|
1037 break; |
|
1038 |
604
|
1039 default: |
|
1040 data_read_error: |
1755
|
1041 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
604
|
1042 delete [] name; |
|
1043 name = 0; |
|
1044 break; |
|
1045 } |
|
1046 |
|
1047 return name; |
|
1048 } |
|
1049 |
|
1050 // Read LEN elements of data from IS in the format specified by |
|
1051 // PRECISION, placing the result in DATA. If SWAP is nonzero, swap |
|
1052 // the bytes of each element before copying to DATA. FLT_FMT |
|
1053 // specifies the format of the data if we are reading floating point |
|
1054 // numbers. |
|
1055 |
|
1056 static void |
|
1057 read_mat_binary_data (istream& is, double *data, int precision, |
|
1058 int len, int swap, floating_point_format flt_fmt) |
|
1059 { |
|
1060 switch (precision) |
|
1061 { |
|
1062 case 0: |
630
|
1063 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); |
604
|
1064 break; |
|
1065 |
|
1066 case 1: |
630
|
1067 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); |
604
|
1068 break; |
|
1069 |
|
1070 case 2: |
|
1071 read_doubles (is, data, LS_INT, len, swap, flt_fmt); |
|
1072 break; |
|
1073 |
|
1074 case 3: |
|
1075 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); |
|
1076 break; |
|
1077 |
|
1078 case 4: |
|
1079 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); |
|
1080 break; |
|
1081 |
|
1082 case 5: |
|
1083 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); |
|
1084 break; |
|
1085 |
|
1086 default: |
|
1087 break; |
|
1088 } |
|
1089 } |
|
1090 |
|
1091 static int |
|
1092 read_mat_file_header (istream& is, int& swap, FOUR_BYTE_INT& mopt, |
|
1093 FOUR_BYTE_INT& nr, FOUR_BYTE_INT& nc, |
|
1094 FOUR_BYTE_INT& imag, FOUR_BYTE_INT& len, |
|
1095 int quiet = 0) |
|
1096 { |
671
|
1097 swap = 0; |
|
1098 |
1358
|
1099 // We expect to fail here, at the beginning of a record, so not |
|
1100 // being able to read another mopt value should not result in an |
|
1101 // error. |
911
|
1102 |
604
|
1103 is.read (&mopt, 4); |
|
1104 if (! is) |
911
|
1105 return 1; |
604
|
1106 |
|
1107 if (! is.read (&nr, 4)) |
|
1108 goto data_read_error; |
|
1109 |
|
1110 if (! is.read (&nc, 4)) |
|
1111 goto data_read_error; |
|
1112 |
|
1113 if (! is.read (&imag, 4)) |
|
1114 goto data_read_error; |
|
1115 |
|
1116 if (! is.read (&len, 4)) |
|
1117 goto data_read_error; |
|
1118 |
|
1119 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
1120 // bigger than we expect, so we swap bytes. |
|
1121 // |
|
1122 // If mopt is zero, it means the file was written on a little endian |
|
1123 // machine, and we only need to swap if we are running on a big endian |
|
1124 // machine. |
|
1125 // |
|
1126 // Gag me. |
|
1127 |
1415
|
1128 if (octave_words_big_endian && mopt == 0) |
604
|
1129 swap = 1; |
|
1130 |
1358
|
1131 // mopt is signed, therefore byte swap may result in negative value. |
911
|
1132 |
|
1133 if (mopt > 9999 || mopt < 0) |
604
|
1134 swap = 1; |
|
1135 |
|
1136 if (swap) |
|
1137 { |
|
1138 swap_4_bytes ((char *) &mopt); |
|
1139 swap_4_bytes ((char *) &nr); |
|
1140 swap_4_bytes ((char *) &nc); |
|
1141 swap_4_bytes ((char *) &imag); |
|
1142 swap_4_bytes ((char *) &len); |
|
1143 } |
|
1144 |
911
|
1145 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) |
604
|
1146 { |
|
1147 if (! quiet) |
|
1148 error ("load: can't read binary file"); |
|
1149 return -1; |
|
1150 } |
|
1151 |
|
1152 return 0; |
|
1153 |
|
1154 data_read_error: |
|
1155 return -1; |
|
1156 } |
|
1157 |
617
|
1158 // We don't just use a cast here, because we need to be able to detect |
|
1159 // possible errors. |
|
1160 |
|
1161 static floating_point_format |
|
1162 get_floating_point_format (int mach) |
|
1163 { |
1226
|
1164 floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; |
619
|
1165 |
617
|
1166 switch (mach) |
|
1167 { |
|
1168 case 0: |
1226
|
1169 flt_fmt = OCTAVE_IEEE_LITTLE; |
617
|
1170 break; |
|
1171 |
|
1172 case 1: |
1226
|
1173 flt_fmt = OCTAVE_IEEE_BIG; |
617
|
1174 break; |
|
1175 |
|
1176 case 2: |
1226
|
1177 flt_fmt = OCTAVE_VAX_D; |
617
|
1178 break; |
|
1179 |
|
1180 case 3: |
1226
|
1181 flt_fmt = OCTAVE_VAX_G; |
617
|
1182 break; |
|
1183 |
|
1184 case 4: |
1226
|
1185 flt_fmt = OCTAVE_CRAY; |
617
|
1186 break; |
|
1187 |
|
1188 default: |
1226
|
1189 flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; |
617
|
1190 break; |
|
1191 } |
619
|
1192 |
|
1193 return flt_fmt; |
617
|
1194 } |
619
|
1195 |
604
|
1196 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
1197 // place it in TC, returning the name of the variable. |
|
1198 // |
|
1199 // The data is expected to be in Matlab's .mat format, though not all |
|
1200 // the features of that format are supported. |
|
1201 // |
|
1202 // FILENAME is used for error messages. |
|
1203 // |
|
1204 // This format provides no way to tag the data as global. |
|
1205 |
|
1206 static char * |
1755
|
1207 read_mat_binary_data (istream& is, const string& filename, |
604
|
1208 tree_constant& tc) |
|
1209 { |
1358
|
1210 // These are initialized here instead of closer to where they are |
|
1211 // first used to avoid errors from gcc about goto crossing |
|
1212 // initialization of variable. |
604
|
1213 |
|
1214 Matrix re; |
1226
|
1215 floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; |
604
|
1216 char *name = 0; |
|
1217 int swap = 0, type = 0, prec = 0, mach = 0, dlen = 0; |
|
1218 |
|
1219 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
1220 |
|
1221 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
1222 if (err) |
|
1223 { |
|
1224 if (err < 0) |
|
1225 goto data_read_error; |
|
1226 else |
|
1227 return 0; |
|
1228 } |
|
1229 |
|
1230 type = mopt % 10; // Full, sparse, etc. |
|
1231 mopt /= 10; // Eliminate first digit. |
|
1232 prec = mopt % 10; // double, float, int, etc. |
|
1233 mopt /= 100; // Skip unused third digit too. |
|
1234 mach = mopt % 10; // IEEE, VAX, etc. |
|
1235 |
617
|
1236 flt_fmt = get_floating_point_format (mach); |
1226
|
1237 if (flt_fmt == OCTAVE_UNKNOWN_FLT_FMT) |
604
|
1238 { |
|
1239 error ("load: unrecognized binary format!"); |
|
1240 return 0; |
|
1241 } |
|
1242 |
|
1243 if (type != 0 && type != 1) |
|
1244 { |
|
1245 error ("load: can't read sparse matrices"); |
|
1246 return 0; |
|
1247 } |
|
1248 |
|
1249 if (imag && type == 1) |
|
1250 { |
|
1251 error ("load: encountered complex matrix with string flag set!"); |
|
1252 return 0; |
|
1253 } |
|
1254 |
|
1255 name = new char [len]; |
|
1256 if (! is.read (name, len)) |
|
1257 goto data_read_error; |
|
1258 |
|
1259 dlen = nr * nc; |
|
1260 if (dlen < 0) |
|
1261 goto data_read_error; |
|
1262 |
|
1263 re.resize (nr, nc); |
|
1264 |
|
1265 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1266 |
|
1267 if (! is || error_state) |
|
1268 { |
|
1269 error ("load: reading matrix data for `%s'", name); |
|
1270 goto data_read_error; |
|
1271 } |
|
1272 |
|
1273 if (imag) |
|
1274 { |
|
1275 Matrix im (nr, nc); |
|
1276 |
|
1277 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1278 |
|
1279 if (! is || error_state) |
|
1280 { |
|
1281 error ("load: reading imaginary matrix data for `%s'", name); |
|
1282 goto data_read_error; |
|
1283 } |
|
1284 |
|
1285 ComplexMatrix ctmp (nr, nc); |
|
1286 |
|
1287 for (int j = 0; j < nc; j++) |
|
1288 for (int i = 0; i < nr; i++) |
|
1289 ctmp.elem (i, j) = Complex (re.elem (i, j), im.elem (i, j)); |
|
1290 |
|
1291 tc = ctmp; |
|
1292 } |
|
1293 else |
|
1294 tc = re; |
|
1295 |
1427
|
1296 if (type == 1) |
604
|
1297 tc = tc.convert_to_str (); |
|
1298 |
|
1299 return name; |
|
1300 |
|
1301 data_read_error: |
1755
|
1302 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
604
|
1303 delete [] name; |
|
1304 return 0; |
|
1305 } |
|
1306 |
|
1307 // Return nonzero if NAME matches one of the given globbing PATTERNS. |
|
1308 |
|
1309 static int |
1755
|
1310 matches_patterns (const string_vector& patterns, int pat_idx, |
1792
|
1311 int num_pat, const string& name) |
604
|
1312 { |
1755
|
1313 for (int i = pat_idx; i < num_pat; i++) |
604
|
1314 { |
1792
|
1315 glob_match pattern (patterns[i]); |
|
1316 if (pattern.match (name)) |
604
|
1317 return 1; |
|
1318 } |
|
1319 return 0; |
|
1320 } |
|
1321 |
|
1322 static int |
|
1323 read_binary_file_header (istream& is, int& swap, |
630
|
1324 floating_point_format& flt_fmt, int quiet = 0) |
604
|
1325 { |
|
1326 int magic_len = 10; |
|
1327 char magic [magic_len+1]; |
|
1328 is.read (magic, magic_len); |
|
1329 magic[magic_len] = '\0'; |
|
1330 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
1415
|
1331 swap = octave_words_big_endian; |
604
|
1332 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
1415
|
1333 swap = ! octave_words_big_endian; |
604
|
1334 else |
|
1335 { |
|
1336 if (! quiet) |
|
1337 error ("load: can't read binary file"); |
|
1338 return -1; |
|
1339 } |
|
1340 |
|
1341 char tmp = 0; |
|
1342 is.read (&tmp, 1); |
|
1343 |
617
|
1344 flt_fmt = get_floating_point_format (tmp); |
1226
|
1345 if (flt_fmt == OCTAVE_UNKNOWN_FLT_FMT) |
604
|
1346 { |
|
1347 if (! quiet) |
|
1348 error ("load: unrecognized binary format!"); |
|
1349 return -1; |
|
1350 } |
|
1351 |
|
1352 return 0; |
|
1353 } |
|
1354 |
|
1355 static load_save_format |
1750
|
1356 get_file_format (const string& fname, const string& orig_fname) |
604
|
1357 { |
|
1358 load_save_format retval = LS_UNKNOWN; |
|
1359 |
1750
|
1360 ifstream file (fname.c_str ()); |
604
|
1361 |
|
1362 if (! file) |
|
1363 { |
1750
|
1364 error ("load: couldn't open input file `%s'", orig_fname.c_str ()); |
604
|
1365 return retval; |
|
1366 } |
|
1367 |
|
1368 int swap; |
1226
|
1369 floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; |
604
|
1370 |
|
1371 if (read_binary_file_header (file, swap, flt_fmt, 1) == 0) |
|
1372 retval = LS_BINARY; |
|
1373 else |
|
1374 { |
|
1375 file.seekg (0, ios::beg); |
|
1376 |
|
1377 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
1180
|
1378 |
|
1379 int err = read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1); |
|
1380 |
|
1381 if (! err) |
604
|
1382 retval = LS_MAT_BINARY; |
|
1383 else |
|
1384 { |
|
1385 file.seekg (0, ios::beg); |
|
1386 |
|
1387 char *tmp = extract_keyword (file, "name"); |
1180
|
1388 |
604
|
1389 if (tmp) |
1180
|
1390 { |
|
1391 retval = LS_ASCII; |
|
1392 delete [] tmp; |
|
1393 } |
604
|
1394 } |
|
1395 } |
|
1396 |
|
1397 file.close (); |
|
1398 |
|
1399 if (retval == LS_UNKNOWN) |
1750
|
1400 error ("load: unable to determine file format for `%s'", |
|
1401 orig_fname.c_str ()); |
604
|
1402 |
|
1403 return retval; |
|
1404 } |
|
1405 |
863
|
1406 static Octave_object |
1755
|
1407 do_load (istream& stream, const string& orig_fname, int force, |
863
|
1408 load_save_format format, floating_point_format flt_fmt, |
1755
|
1409 int list_only, int swap, int verbose, const string_vector& argv, |
|
1410 int argv_idx, int argc, int nargout) |
604
|
1411 { |
|
1412 Octave_object retval; |
|
1413 |
621
|
1414 ostrstream output_buf; |
604
|
1415 int count = 0; |
|
1416 for (;;) |
|
1417 { |
|
1418 int global = 0; |
|
1419 tree_constant tc; |
|
1420 |
|
1421 char *name = 0; |
|
1422 char *doc = 0; |
|
1423 |
|
1424 switch (format) |
|
1425 { |
|
1426 case LS_ASCII: |
|
1427 name = read_ascii_data (stream, orig_fname, global, tc); |
|
1428 break; |
|
1429 |
|
1430 case LS_BINARY: |
|
1431 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
1432 global, tc, doc); |
|
1433 break; |
|
1434 |
|
1435 case LS_MAT_BINARY: |
|
1436 name = read_mat_binary_data (stream, orig_fname, tc); |
|
1437 break; |
|
1438 |
|
1439 default: |
775
|
1440 gripe_unrecognized_data_fmt ("load"); |
604
|
1441 break; |
|
1442 } |
|
1443 |
867
|
1444 if (error_state || stream.eof () || ! name) |
604
|
1445 { |
867
|
1446 delete [] name; |
|
1447 delete [] doc; |
604
|
1448 break; |
|
1449 } |
|
1450 else if (! error_state && name) |
|
1451 { |
|
1452 if (tc.is_defined ()) |
|
1453 { |
1755
|
1454 if (argv_idx == argc |
|
1455 || matches_patterns (argv, argv_idx, argc, name)) |
604
|
1456 { |
|
1457 count++; |
621
|
1458 if (list_only) |
|
1459 { |
|
1460 if (verbose) |
|
1461 { |
|
1462 if (count == 1) |
|
1463 output_buf |
|
1464 << "type rows cols name\n" |
|
1465 << "==== ==== ==== ====\n"; |
|
1466 |
|
1467 output_buf.form ("%-16s", tc.type_as_string ()); |
|
1468 output_buf.form ("%7d", tc.rows ()); |
|
1469 output_buf.form ("%7d", tc.columns ()); |
|
1470 output_buf << " "; |
|
1471 } |
|
1472 output_buf << name << "\n"; |
|
1473 } |
|
1474 else |
|
1475 { |
|
1476 install_loaded_variable (force, name, tc, global, doc); |
|
1477 } |
604
|
1478 } |
|
1479 } |
|
1480 else |
|
1481 error ("load: unable to load variable `%s'", name); |
|
1482 } |
|
1483 else |
|
1484 { |
|
1485 if (count == 0) |
|
1486 error ("load: are you sure `%s' is an Octave data file?", |
1755
|
1487 orig_fname.c_str ()); |
604
|
1488 |
867
|
1489 delete [] name; |
|
1490 delete [] doc; |
604
|
1491 break; |
|
1492 } |
|
1493 |
|
1494 delete [] name; |
|
1495 delete [] doc; |
|
1496 } |
|
1497 |
621
|
1498 if (list_only && count) |
|
1499 { |
|
1500 if (nargout > 0) |
|
1501 { |
|
1502 output_buf << ends; |
|
1503 char *msg = output_buf.str (); |
|
1504 retval = msg; |
|
1505 delete [] msg; |
|
1506 } |
|
1507 else |
|
1508 maybe_page_output (output_buf); |
|
1509 } |
|
1510 |
863
|
1511 return retval; |
|
1512 } |
|
1513 |
1957
|
1514 DEFUN_TEXT (load, args, nargout, |
910
|
1515 "load [-force] [-ascii] [-binary] [-mat-binary] file [pattern ...]\n\ |
863
|
1516 \n\ |
|
1517 Load variables from a file.\n\ |
|
1518 \n\ |
|
1519 If no argument is supplied to select a format, load tries to read the |
|
1520 named file as an Octave binary, then as a .mat file, and then as an |
|
1521 Octave text file.\n\ |
|
1522 \n\ |
|
1523 If the option -force is given, variables with the same names as those |
|
1524 found in the file will be replaced with the values read from the file.") |
|
1525 { |
|
1526 Octave_object retval; |
|
1527 |
1755
|
1528 int argc = args.length () + 1; |
|
1529 |
|
1530 string_vector argv = make_argv (args, "load"); |
|
1531 |
|
1532 if (error_state) |
|
1533 return retval; |
863
|
1534 |
|
1535 int force = 0; |
|
1536 |
1358
|
1537 // It isn't necessary to have the default load format stored in a |
|
1538 // user preference variable since we can determine the type of file |
|
1539 // as we are reading. |
863
|
1540 |
|
1541 load_save_format format = LS_UNKNOWN; |
|
1542 |
|
1543 int list_only = 0; |
|
1544 int verbose = 0; |
|
1545 |
1755
|
1546 int i; |
|
1547 for (i = 1; i < argc; i++) |
863
|
1548 { |
1755
|
1549 if (argv[i] == "-force" || argv[i] == "-f") |
863
|
1550 { |
|
1551 force++; |
|
1552 } |
1755
|
1553 else if (argv[i] == "-list" || argv[i] == "-l") |
863
|
1554 { |
|
1555 list_only = 1; |
|
1556 } |
1755
|
1557 else if (argv[i] == "-verbose" || argv[i] == "-v") |
863
|
1558 { |
|
1559 verbose = 1; |
|
1560 } |
1755
|
1561 else if (argv[i] == "-ascii" || argv[i] == "-a") |
863
|
1562 { |
|
1563 format = LS_ASCII; |
|
1564 } |
1755
|
1565 else if (argv[i] == "-binary" || argv[i] == "-b") |
863
|
1566 { |
|
1567 format = LS_BINARY; |
|
1568 } |
1755
|
1569 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
863
|
1570 { |
|
1571 format = LS_MAT_BINARY; |
|
1572 } |
|
1573 else |
|
1574 break; |
|
1575 } |
|
1576 |
1755
|
1577 if (i == argc) |
863
|
1578 { |
|
1579 error ("load: you must specify a single file to read"); |
|
1580 return retval; |
|
1581 } |
|
1582 |
1755
|
1583 string orig_fname = argv[i]; |
863
|
1584 |
1226
|
1585 floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; |
863
|
1586 |
|
1587 int swap = 0; |
|
1588 |
1755
|
1589 if (argv[i] == "-") |
863
|
1590 { |
1755
|
1591 i++; |
863
|
1592 |
|
1593 if (format != LS_UNKNOWN) |
|
1594 { |
1358
|
1595 // XXX FIXME XXX -- if we have already seen EOF on a |
|
1596 // previous call, how do we fix up the state of cin so that |
|
1597 // we can get additional input? I'm afraid that we can't |
|
1598 // fix this using cin only. |
863
|
1599 |
|
1600 retval = do_load (cin, orig_fname, force, format, flt_fmt, |
1755
|
1601 list_only, swap, verbose, argv, i, argc, |
863
|
1602 nargout); |
|
1603 } |
|
1604 else |
|
1605 error ("load: must specify file format if reading from stdin"); |
|
1606 } |
|
1607 else |
|
1608 { |
1755
|
1609 string fname = oct_tilde_expand (argv[i]); |
863
|
1610 |
|
1611 if (format == LS_UNKNOWN) |
|
1612 format = get_file_format (fname, orig_fname); |
|
1613 |
|
1614 if (format != LS_UNKNOWN) |
|
1615 { |
1755
|
1616 i++; |
863
|
1617 |
|
1618 unsigned mode = ios::in; |
|
1619 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
1620 mode |= ios::bin; |
|
1621 |
1750
|
1622 ifstream file (fname.c_str (), mode); |
863
|
1623 |
|
1624 if (file) |
|
1625 { |
|
1626 if (format == LS_BINARY) |
|
1627 { |
|
1628 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
1629 { |
|
1630 file.close (); |
|
1631 return retval; |
|
1632 } |
|
1633 } |
|
1634 |
|
1635 retval = do_load (file, orig_fname, force, format, |
|
1636 flt_fmt, list_only, swap, verbose, |
1755
|
1637 argv, i, argc, nargout); |
863
|
1638 |
|
1639 file.close (); |
|
1640 } |
|
1641 else |
1755
|
1642 error ("load: couldn't open input file `%s'", |
|
1643 orig_fname.c_str ()); |
863
|
1644 } |
|
1645 } |
604
|
1646 |
|
1647 return retval; |
|
1648 } |
|
1649 |
|
1650 // Return nonzero if PATTERN has any special globbing chars in it. |
|
1651 |
|
1652 static int |
1755
|
1653 glob_pattern_p (const string& pattern) |
604
|
1654 { |
|
1655 int open = 0; |
|
1656 |
1755
|
1657 int len = pattern.length (); |
|
1658 |
|
1659 for (int i = 0; i < len; i++) |
604
|
1660 { |
1755
|
1661 char c = pattern[i]; |
|
1662 |
604
|
1663 switch (c) |
|
1664 { |
|
1665 case '?': |
|
1666 case '*': |
|
1667 return 1; |
|
1668 |
|
1669 case '[': // Only accept an open brace if there is a close |
|
1670 open++; // brace to match it. Bracket expressions must be |
|
1671 continue; // complete, according to Posix.2 |
|
1672 |
|
1673 case ']': |
|
1674 if (open) |
|
1675 return 1; |
|
1676 continue; |
|
1677 |
|
1678 case '\\': |
1755
|
1679 if (i == len - 1) |
604
|
1680 return 0; |
|
1681 |
|
1682 default: |
|
1683 continue; |
|
1684 } |
|
1685 } |
|
1686 |
|
1687 return 0; |
|
1688 } |
|
1689 |
618
|
1690 // MAX_VAL and MIN_VAL are assumed to have integral values even though |
|
1691 // they are stored in doubles. |
|
1692 |
604
|
1693 static save_type |
|
1694 get_save_type (double max_val, double min_val) |
|
1695 { |
|
1696 save_type st = LS_DOUBLE; |
|
1697 |
|
1698 if (max_val < 256 && min_val > -1) |
|
1699 st = LS_U_CHAR; |
|
1700 else if (max_val < 65536 && min_val > -1) |
|
1701 st = LS_U_SHORT; |
618
|
1702 else if (max_val < 4294967295 && min_val > -1) |
|
1703 st = LS_U_INT; |
|
1704 else if (max_val < 128 && min_val >= -128) |
|
1705 st = LS_CHAR; |
604
|
1706 else if (max_val < 32768 && min_val >= -32768) |
|
1707 st = LS_SHORT; |
|
1708 else if (max_val < 2147483648 && min_val > -2147483648) |
|
1709 st = LS_INT; |
|
1710 |
|
1711 return st; |
|
1712 } |
|
1713 |
|
1714 // Save the data from TC along with the corresponding NAME, help |
|
1715 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
1427
|
1716 // binary format described above for read_binary_data. |
604
|
1717 |
|
1718 static int |
1755
|
1719 save_binary_data (ostream& os, const tree_constant& tc, |
|
1720 const string& name, const string& doc, |
|
1721 int mark_as_global, int save_as_floats) |
604
|
1722 { |
620
|
1723 int fail = 0; |
|
1724 |
1755
|
1725 FOUR_BYTE_INT name_len = name.length (); |
604
|
1726 |
|
1727 os.write (&name_len, 4); |
1755
|
1728 os << name; |
|
1729 |
|
1730 FOUR_BYTE_INT doc_len = doc.length (); |
604
|
1731 |
|
1732 os.write (&doc_len, 4); |
1755
|
1733 os << doc; |
604
|
1734 |
|
1735 char tmp; |
|
1736 |
|
1737 tmp = mark_as_global; |
|
1738 os.write (&tmp, 1); |
|
1739 |
620
|
1740 if (tc.is_real_scalar ()) |
604
|
1741 { |
|
1742 tmp = 1; |
|
1743 os.write (&tmp, 1); |
630
|
1744 tmp = (char) LS_DOUBLE; |
|
1745 os.write (&tmp, 1); |
604
|
1746 double tmp = tc.double_value (); |
|
1747 os.write (&tmp, 8); |
|
1748 } |
620
|
1749 else if (tc.is_real_matrix ()) |
604
|
1750 { |
|
1751 tmp = 2; |
|
1752 os.write (&tmp, 1); |
|
1753 Matrix m = tc.matrix_value (); |
|
1754 FOUR_BYTE_INT nr = m.rows (); |
|
1755 FOUR_BYTE_INT nc = m.columns (); |
|
1756 os.write (&nr, 4); |
|
1757 os.write (&nc, 4); |
|
1758 int len = nr * nc; |
|
1759 save_type st = LS_DOUBLE; |
630
|
1760 if (save_as_floats) |
|
1761 { |
|
1762 if (too_large_for_float (m)) |
|
1763 { |
|
1764 warning ("save: some values too large to save as floats --"); |
|
1765 warning ("save: saving as doubles instead"); |
|
1766 } |
|
1767 else |
|
1768 st = LS_FLOAT; |
|
1769 } |
|
1770 else if (len > 8192) // XXX FIXME XXX -- make this configurable. |
604
|
1771 { |
|
1772 double max_val, min_val; |
|
1773 if (all_parts_int (m, max_val, min_val)) |
|
1774 st = get_save_type (max_val, min_val); |
|
1775 } |
630
|
1776 const double *mtmp = m.data (); |
604
|
1777 write_doubles (os, mtmp, st, len); |
|
1778 } |
|
1779 else if (tc.is_complex_scalar ()) |
|
1780 { |
|
1781 tmp = 3; |
|
1782 os.write (&tmp, 1); |
630
|
1783 tmp = (char) LS_DOUBLE; |
|
1784 os.write (&tmp, 1); |
604
|
1785 Complex tmp = tc.complex_value (); |
|
1786 os.write (&tmp, 16); |
|
1787 } |
|
1788 else if (tc.is_complex_matrix ()) |
|
1789 { |
|
1790 tmp = 4; |
|
1791 os.write (&tmp, 1); |
|
1792 ComplexMatrix m = tc.complex_matrix_value (); |
|
1793 FOUR_BYTE_INT nr = m.rows (); |
|
1794 FOUR_BYTE_INT nc = m.columns (); |
|
1795 os.write (&nr, 4); |
|
1796 os.write (&nc, 4); |
|
1797 int len = nr * nc; |
|
1798 save_type st = LS_DOUBLE; |
630
|
1799 if (save_as_floats) |
|
1800 { |
|
1801 if (too_large_for_float (m)) |
|
1802 { |
|
1803 warning ("save: some values too large to save as floats --"); |
|
1804 warning ("save: saving as doubles instead"); |
|
1805 } |
|
1806 else |
|
1807 st = LS_FLOAT; |
|
1808 } |
|
1809 else if (len > 4096) // XXX FIXME XXX -- make this configurable. |
604
|
1810 { |
|
1811 double max_val, min_val; |
|
1812 if (all_parts_int (m, max_val, min_val)) |
|
1813 st = get_save_type (max_val, min_val); |
|
1814 } |
630
|
1815 const Complex *mtmp = m.data (); |
|
1816 write_doubles (os, (const double *) mtmp, st, 2*len); |
604
|
1817 } |
|
1818 else if (tc.is_string ()) |
|
1819 { |
1427
|
1820 tmp = 7; |
604
|
1821 os.write (&tmp, 1); |
1427
|
1822 FOUR_BYTE_INT nr = tc.rows (); |
|
1823 os.write (&nr, 4); |
1572
|
1824 charMatrix chm = tc.all_strings (); |
1427
|
1825 for (int i = 0; i < nr; i++) |
|
1826 { |
1572
|
1827 FOUR_BYTE_INT len = chm.cols (); |
1427
|
1828 os.write (&len, 4); |
1728
|
1829 string tstr = chm.row_as_string (i); |
|
1830 const char *tmp = tstr.data (); |
1427
|
1831 os.write (tmp, len); |
|
1832 } |
604
|
1833 } |
|
1834 else if (tc.is_range ()) |
|
1835 { |
|
1836 tmp = 6; |
|
1837 os.write (&tmp, 1); |
630
|
1838 tmp = (char) LS_DOUBLE; |
|
1839 os.write (&tmp, 1); |
604
|
1840 Range r = tc.range_value (); |
|
1841 double bas = r.base (); |
|
1842 double lim = r.limit (); |
|
1843 double inc = r.inc (); |
|
1844 os.write (&bas, 8); |
|
1845 os.write (&lim, 8); |
|
1846 os.write (&inc, 8); |
|
1847 } |
|
1848 else |
620
|
1849 { |
|
1850 gripe_wrong_type_arg ("save", tc); |
|
1851 fail = 1; |
|
1852 } |
604
|
1853 |
620
|
1854 return (os && ! fail); |
604
|
1855 } |
|
1856 |
667
|
1857 // Save the data from TC along with the corresponding NAME on stream OS |
|
1858 // in the MatLab binary format. |
|
1859 |
|
1860 static int |
1755
|
1861 save_mat_binary_data (ostream& os, const tree_constant& tc, |
|
1862 const string& name) |
667
|
1863 { |
|
1864 int fail = 0; |
|
1865 |
|
1866 FOUR_BYTE_INT mopt = 0; |
|
1867 |
|
1868 mopt += tc.is_string () ? 1 : 0; |
1226
|
1869 mopt += 1000 * get_floating_point_format (native_float_format); |
667
|
1870 |
|
1871 os.write (&mopt, 4); |
|
1872 |
|
1873 FOUR_BYTE_INT nr = tc.rows (); |
|
1874 os.write (&nr, 4); |
|
1875 |
|
1876 FOUR_BYTE_INT nc = tc.columns (); |
|
1877 os.write (&nc, 4); |
|
1878 |
|
1879 int len = nr * nc; |
|
1880 |
|
1881 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; |
|
1882 os.write (&imag, 4); |
|
1883 |
1755
|
1884 FOUR_BYTE_INT name_len = name.length (); |
667
|
1885 |
|
1886 os.write (&name_len, 4); |
1755
|
1887 os << name; |
667
|
1888 |
|
1889 if (tc.is_real_scalar ()) |
|
1890 { |
|
1891 double tmp = tc.double_value (); |
|
1892 os.write (&tmp, 8); |
|
1893 } |
911
|
1894 else if (tc.is_real_matrix ()) |
667
|
1895 { |
|
1896 Matrix m = tc.matrix_value (); |
|
1897 os.write (m.data (), 8 * len); |
|
1898 } |
|
1899 else if (tc.is_complex_scalar ()) |
|
1900 { |
|
1901 Complex tmp = tc.complex_value (); |
|
1902 os.write (&tmp, 16); |
|
1903 } |
|
1904 else if (tc.is_complex_matrix ()) |
|
1905 { |
|
1906 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
1907 Matrix m = ::real(m_cmplx); |
|
1908 os.write (m.data (), 8 * len); |
|
1909 m = ::imag(m_cmplx); |
|
1910 os.write (m.data (), 8 * len); |
|
1911 } |
|
1912 else if (tc.is_string ()) |
|
1913 { |
|
1914 begin_unwind_frame ("save_mat_binary_data"); |
|
1915 unwind_protect_int (user_pref.implicit_str_to_num_ok); |
|
1916 user_pref.implicit_str_to_num_ok = 1; |
|
1917 Matrix m = tc.matrix_value (); |
|
1918 os.write (m.data (), 8 * len); |
|
1919 run_unwind_frame ("save_mat_binary_data"); |
|
1920 } |
911
|
1921 else if (tc.is_range ()) |
|
1922 { |
|
1923 Range r = tc.range_value (); |
|
1924 double base = r.base (); |
|
1925 double inc = r.inc (); |
|
1926 int nel = r.nelem (); |
|
1927 for (int i = 0; i < nel; i++) |
|
1928 { |
|
1929 double x = base + i * inc; |
|
1930 os.write (&x, 8); |
|
1931 } |
|
1932 } |
667
|
1933 else |
|
1934 { |
|
1935 gripe_wrong_type_arg ("save", tc); |
|
1936 fail = 1; |
|
1937 } |
|
1938 |
|
1939 return (os && ! fail); |
|
1940 } |
|
1941 |
620
|
1942 static void |
|
1943 ascii_save_type (ostream& os, char *type, int mark_as_global) |
|
1944 { |
|
1945 if (mark_as_global) |
|
1946 os << "# type: global "; |
|
1947 else |
|
1948 os << "# type: "; |
|
1949 |
|
1950 os << type << "\n"; |
|
1951 } |
|
1952 |
872
|
1953 static Matrix |
|
1954 strip_infnan (const Matrix& m) |
|
1955 { |
|
1956 int nr = m.rows (); |
|
1957 int nc = m.columns (); |
|
1958 |
|
1959 Matrix retval (nr, nc); |
|
1960 |
|
1961 int k = 0; |
|
1962 for (int i = 0; i < nr; i++) |
|
1963 { |
|
1964 for (int j = 0; j < nc; j++) |
|
1965 { |
|
1966 double d = m.elem (i, j); |
|
1967 if (xisnan (d)) |
|
1968 goto next_row; |
|
1969 else |
|
1970 retval.elem (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
1971 } |
|
1972 k++; |
|
1973 |
|
1974 next_row: |
|
1975 continue; |
|
1976 } |
|
1977 |
|
1978 if (k > 0) |
|
1979 retval.resize (k, nc); |
|
1980 |
|
1981 return retval; |
|
1982 } |
|
1983 |
|
1984 static ComplexMatrix |
|
1985 strip_infnan (const ComplexMatrix& m) |
|
1986 { |
|
1987 int nr = m.rows (); |
|
1988 int nc = m.columns (); |
|
1989 |
|
1990 ComplexMatrix retval (nr, nc); |
|
1991 |
|
1992 int k = 0; |
|
1993 for (int i = 0; i < nr; i++) |
|
1994 { |
|
1995 for (int j = 0; j < nc; j++) |
|
1996 { |
|
1997 Complex c = m.elem (i, j); |
|
1998 if (xisnan (c)) |
|
1999 goto next_row; |
|
2000 else |
|
2001 { |
|
2002 double re = real (c); |
|
2003 double im = imag (c); |
|
2004 |
|
2005 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
2006 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
2007 |
|
2008 retval.elem (k, j) = Complex (re, im); |
|
2009 } |
|
2010 } |
|
2011 k++; |
|
2012 |
|
2013 next_row: |
|
2014 continue; |
|
2015 } |
|
2016 |
|
2017 if (k > 0) |
|
2018 retval.resize (k, nc); |
|
2019 |
|
2020 return retval; |
|
2021 } |
|
2022 |
620
|
2023 // Save the data from TC along with the corresponding NAME, and global |
604
|
2024 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
1755
|
2025 // above for load_ascii_data. If NAME is empty, the name: line is not |
604
|
2026 // generated. PRECISION specifies the number of decimal digits to print. |
872
|
2027 // If STRIP_NAN_AND_INF is nonzero, rows containing NaNs are deleted, |
|
2028 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
2029 // but not so big that gnuplot can't handle it when trying to compute |
|
2030 // axis ranges, etc.). |
|
2031 // |
|
2032 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
2033 // |
|
2034 // Returns 1 for success and 0 for failure. |
604
|
2035 |
|
2036 // XXX FIXME XXX -- should probably write the help string here too. |
|
2037 |
|
2038 int |
620
|
2039 save_ascii_data (ostream& os, const tree_constant& tc, |
1755
|
2040 const string& name, int strip_nan_and_inf, |
872
|
2041 int mark_as_global, int precision) |
604
|
2042 { |
872
|
2043 int success = 1; |
620
|
2044 |
604
|
2045 if (! precision) |
|
2046 precision = user_pref.save_precision; |
|
2047 |
1755
|
2048 if (! name.empty ()) |
604
|
2049 os << "# name: " << name << "\n"; |
|
2050 |
|
2051 long old_precision = os.precision (); |
|
2052 os.precision (precision); |
|
2053 |
620
|
2054 if (tc.is_real_scalar ()) |
|
2055 { |
|
2056 ascii_save_type (os, "scalar", mark_as_global); |
872
|
2057 |
|
2058 double d = tc.double_value (); |
|
2059 if (strip_nan_and_inf) |
|
2060 { |
|
2061 if (xisnan (d)) |
|
2062 { |
|
2063 error ("only value to plot is NaN"); |
|
2064 success = 0; |
|
2065 } |
|
2066 else |
|
2067 { |
|
2068 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
2069 os << d << "\n"; |
|
2070 } |
|
2071 } |
|
2072 else |
|
2073 os << d << "\n"; |
620
|
2074 } |
|
2075 else if (tc.is_real_matrix ()) |
|
2076 { |
|
2077 ascii_save_type (os, "matrix", mark_as_global); |
|
2078 os << "# rows: " << tc.rows () << "\n" |
872
|
2079 << "# columns: " << tc.columns () << "\n"; |
|
2080 |
|
2081 Matrix tmp = tc.matrix_value (); |
|
2082 if (strip_nan_and_inf) |
|
2083 tmp = strip_infnan (tmp); |
|
2084 |
|
2085 os << tmp; |
620
|
2086 } |
|
2087 else if (tc.is_complex_scalar ()) |
|
2088 { |
|
2089 ascii_save_type (os, "complex scalar", mark_as_global); |
872
|
2090 |
|
2091 Complex c = tc.complex_value (); |
|
2092 if (strip_nan_and_inf) |
|
2093 { |
|
2094 if (xisnan (c)) |
|
2095 { |
|
2096 error ("only value to plot is NaN"); |
|
2097 success = 0; |
|
2098 } |
|
2099 else |
|
2100 { |
|
2101 double re = real (c); |
|
2102 double im = imag (c); |
|
2103 |
|
2104 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
2105 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
2106 |
|
2107 c = Complex (re, im); |
|
2108 |
|
2109 os << c << "\n"; |
|
2110 } |
|
2111 } |
|
2112 else |
|
2113 os << c << "\n"; |
620
|
2114 } |
|
2115 else if (tc.is_complex_matrix ()) |
604
|
2116 { |
620
|
2117 ascii_save_type (os, "complex matrix", mark_as_global); |
|
2118 os << "# rows: " << tc.rows () << "\n" |
875
|
2119 << "# columns: " << tc.columns () << "\n"; |
|
2120 |
|
2121 ComplexMatrix tmp = tc.complex_matrix_value (); |
872
|
2122 if (strip_nan_and_inf) |
|
2123 tmp = strip_infnan (tmp); |
|
2124 |
|
2125 os << tmp; |
620
|
2126 } |
|
2127 else if (tc.is_string ()) |
|
2128 { |
1427
|
2129 ascii_save_type (os, "string array", mark_as_global); |
1572
|
2130 charMatrix chm = tc.all_strings (); |
|
2131 int elements = chm.rows (); |
1427
|
2132 os << "# elements: " << elements << "\n"; |
|
2133 for (int i = 0; i < elements; i++) |
|
2134 { |
1572
|
2135 int len = chm.cols (); |
1427
|
2136 os << "# length: " << len << "\n"; |
1728
|
2137 string tstr = chm.row_as_string (i); |
1742
|
2138 const char *tmp = tstr.data (); |
1572
|
2139 os.write (tmp, len); |
1427
|
2140 os << "\n"; |
|
2141 } |
620
|
2142 } |
872
|
2143 else if (tc.is_range ()) |
620
|
2144 { |
|
2145 ascii_save_type (os, "range", mark_as_global); |
|
2146 Range tmp = tc.range_value (); |
|
2147 os << "# base, limit, increment\n" |
|
2148 << tmp.base () << " " |
|
2149 << tmp.limit () << " " |
|
2150 << tmp.inc () << "\n"; |
|
2151 } |
|
2152 else |
|
2153 { |
|
2154 gripe_wrong_type_arg ("save", tc); |
872
|
2155 success = 0; |
604
|
2156 } |
|
2157 |
|
2158 os.precision (old_precision); |
|
2159 |
872
|
2160 return (os && success); |
604
|
2161 } |
|
2162 |
|
2163 // Save the info from sr on stream os in the format specified by fmt. |
|
2164 |
|
2165 static void |
630
|
2166 do_save (ostream& os, symbol_record *sr, load_save_format fmt, |
|
2167 int save_as_floats) |
604
|
2168 { |
|
2169 if (! sr->is_variable ()) |
|
2170 { |
|
2171 error ("save: can only save variables, not functions"); |
|
2172 return; |
|
2173 } |
|
2174 |
1755
|
2175 string name = sr->name (); |
|
2176 string help = sr->help (); |
604
|
2177 int global = sr->is_linked_to_global (); |
|
2178 tree_constant tc = *((tree_constant *) sr->def ()); |
|
2179 |
1755
|
2180 if (tc.is_undefined ()) |
604
|
2181 return; |
|
2182 |
|
2183 switch (fmt) |
|
2184 { |
|
2185 case LS_ASCII: |
872
|
2186 save_ascii_data (os, tc, name, 0, global); |
604
|
2187 break; |
|
2188 |
|
2189 case LS_BINARY: |
630
|
2190 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
2191 break; |
|
2192 |
667
|
2193 case LS_MAT_BINARY: |
|
2194 save_mat_binary_data (os, tc, name); |
|
2195 break; |
|
2196 |
604
|
2197 default: |
775
|
2198 gripe_unrecognized_data_fmt ("save"); |
604
|
2199 break; |
|
2200 } |
|
2201 } |
|
2202 |
|
2203 // Save variables with names matching PATTERN on stream OS in the |
|
2204 // format specified by FMT. If SAVE_BUILTINS is nonzero, also save |
|
2205 // builtin variables with names that match PATTERN. |
|
2206 |
|
2207 static int |
1755
|
2208 save_vars (ostream& os, const string& pattern, int save_builtins, |
630
|
2209 load_save_format fmt, int save_as_floats) |
604
|
2210 { |
|
2211 int count; |
|
2212 |
|
2213 symbol_record **vars = curr_sym_tab->glob |
|
2214 (count, pattern, symbol_def::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2215 |
|
2216 int saved = count; |
|
2217 |
|
2218 int i; |
|
2219 |
|
2220 for (i = 0; i < count; i++) |
620
|
2221 { |
630
|
2222 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2223 |
|
2224 if (error_state) |
|
2225 break; |
|
2226 } |
604
|
2227 |
|
2228 delete [] vars; |
|
2229 |
620
|
2230 if (! error_state && save_builtins) |
604
|
2231 { |
|
2232 symbol_record **vars = global_sym_tab->glob |
|
2233 (count, pattern, symbol_def::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2234 |
|
2235 saved += count; |
|
2236 |
|
2237 for (i = 0; i < count; i++) |
620
|
2238 { |
630
|
2239 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2240 |
|
2241 if (error_state) |
|
2242 break; |
|
2243 } |
604
|
2244 |
|
2245 delete [] vars; |
|
2246 } |
|
2247 |
|
2248 return saved; |
|
2249 } |
|
2250 |
|
2251 static load_save_format |
|
2252 get_default_save_format (void) |
|
2253 { |
|
2254 load_save_format retval = LS_ASCII; |
|
2255 |
1755
|
2256 string fmt = user_pref.default_save_format; |
|
2257 |
|
2258 if (fmt == "binary") |
604
|
2259 retval = LS_BINARY; |
1755
|
2260 else if (fmt == "mat-binary" || fmt =="mat_binary") |
911
|
2261 retval = LS_MAT_BINARY; |
604
|
2262 |
|
2263 return retval; |
|
2264 } |
|
2265 |
863
|
2266 static void |
|
2267 write_binary_header (ostream& stream, load_save_format format) |
|
2268 { |
|
2269 if (format == LS_BINARY) |
|
2270 { |
1415
|
2271 stream << (octave_words_big_endian ? "Octave-1-B" : "Octave-1-L"); |
863
|
2272 |
1226
|
2273 char tmp = (char) native_float_format; |
863
|
2274 stream.write (&tmp, 1); |
|
2275 } |
|
2276 } |
|
2277 |
|
2278 static void |
1755
|
2279 save_vars (const string_vector& argv, int argv_idx, int argc, |
|
2280 ostream& os, int save_builtins, load_save_format fmt, |
|
2281 int save_as_floats) |
863
|
2282 { |
|
2283 write_binary_header (os, fmt); |
|
2284 |
1755
|
2285 if (argv_idx == argc) |
863
|
2286 { |
|
2287 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
|
2288 } |
|
2289 else |
|
2290 { |
1755
|
2291 for (int i = argv_idx; i < argc; i++) |
863
|
2292 { |
1755
|
2293 if (! save_vars (os, argv[i], save_builtins, fmt, save_as_floats)) |
863
|
2294 { |
1755
|
2295 warning ("save: no such variable `%s'", argv[i].c_str ()); |
863
|
2296 } |
|
2297 } |
|
2298 } |
|
2299 } |
|
2300 |
1380
|
2301 void |
|
2302 save_user_variables (void) |
|
2303 { |
|
2304 // XXX FIXME XXX -- should choose better file name? |
|
2305 |
|
2306 const char *fname = "octave-core"; |
|
2307 |
|
2308 message (0, "attempting to save variables to `%s'...", fname); |
|
2309 |
|
2310 load_save_format format = get_default_save_format (); |
|
2311 |
|
2312 unsigned mode = ios::out|ios::trunc; |
|
2313 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2314 mode |= ios::bin; |
|
2315 |
|
2316 ofstream file (fname, mode); |
|
2317 |
|
2318 if (file) |
|
2319 { |
1755
|
2320 save_vars (string_vector (), 0, 0, file, 0, format, 0); |
1380
|
2321 message (0, "save to `%s' complete", fname); |
|
2322 } |
|
2323 else |
|
2324 warning ("unable to open `%s' for writing...", fname); |
|
2325 } |
|
2326 |
1957
|
2327 DEFUN_TEXT (save, args, , |
910
|
2328 "save [-ascii] [-binary] [-float-binary] [-mat-binary] \n\ |
667
|
2329 [-save-builtins] file [pattern ...]\n\ |
604
|
2330 \n\ |
|
2331 save variables in a file") |
|
2332 { |
|
2333 Octave_object retval; |
|
2334 |
1755
|
2335 int argc = args.length () + 1; |
|
2336 |
|
2337 string_vector argv = make_argv (args, "save"); |
|
2338 |
|
2339 if (error_state) |
|
2340 return retval; |
604
|
2341 |
1358
|
2342 // Here is where we would get the default save format if it were |
|
2343 // stored in a user preference variable. |
604
|
2344 |
|
2345 int save_builtins = 0; |
|
2346 |
630
|
2347 int save_as_floats = 0; |
|
2348 |
604
|
2349 load_save_format format = get_default_save_format (); |
|
2350 |
1755
|
2351 int i; |
|
2352 for (i = 1; i < argc; i++) |
604
|
2353 { |
1755
|
2354 if (argv[i] == "-ascii" || argv[i] == "-a") |
604
|
2355 { |
|
2356 format = LS_ASCII; |
|
2357 } |
1755
|
2358 else if (argv[i] == "-binary" || argv[i] == "-b") |
604
|
2359 { |
|
2360 format = LS_BINARY; |
|
2361 } |
1755
|
2362 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
667
|
2363 { |
|
2364 format = LS_MAT_BINARY; |
|
2365 } |
1755
|
2366 else if (argv[i] == "-float-binary" || argv[i] == "-f") |
630
|
2367 { |
|
2368 format = LS_BINARY; |
|
2369 save_as_floats = 1; |
|
2370 } |
1755
|
2371 else if (argv[i] == "-save-builtins") |
604
|
2372 { |
|
2373 save_builtins = 1; |
|
2374 } |
|
2375 else |
|
2376 break; |
|
2377 } |
|
2378 |
|
2379 if (argc < 1) |
|
2380 { |
|
2381 print_usage ("save"); |
|
2382 return retval; |
|
2383 } |
|
2384 |
630
|
2385 if (save_as_floats && format == LS_ASCII) |
|
2386 { |
|
2387 error ("save: cannot specify both -ascii and -float-binary"); |
|
2388 return retval; |
|
2389 } |
|
2390 |
1755
|
2391 if (argv[i] == "-") |
604
|
2392 { |
1755
|
2393 i++; |
863
|
2394 |
1358
|
2395 // XXX FIXME XXX -- should things intended for the screen end up |
|
2396 // in a tree_constant (string)? |
863
|
2397 |
1043
|
2398 ostrstream buf; |
|
2399 |
1755
|
2400 save_vars (argv, i, argc, buf, save_builtins, format, |
863
|
2401 save_as_floats); |
1043
|
2402 |
|
2403 maybe_page_output (buf); |
604
|
2404 } |
1755
|
2405 |
|
2406 // Guard against things like `save a*', which are probably mistakes... |
|
2407 |
|
2408 else if (i == argc - 1 && glob_pattern_p (argv[i])) |
|
2409 { |
|
2410 print_usage ("save"); |
604
|
2411 return retval; |
|
2412 } |
|
2413 else |
|
2414 { |
1755
|
2415 string fname = oct_tilde_expand (argv[i]); |
|
2416 |
|
2417 i++; |
604
|
2418 |
911
|
2419 unsigned mode = ios::out|ios::trunc; |
604
|
2420 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2421 mode |= ios::bin; |
|
2422 |
1750
|
2423 ofstream file (fname.c_str (), mode); |
863
|
2424 |
|
2425 if (file) |
|
2426 { |
1755
|
2427 save_vars (argv, i, argc, file, save_builtins, format, |
863
|
2428 save_as_floats); |
|
2429 } |
|
2430 else |
604
|
2431 { |
1755
|
2432 error ("save: couldn't open output file `%s'", fname.c_str ()); |
604
|
2433 return retval; |
|
2434 } |
|
2435 } |
|
2436 |
|
2437 return retval; |
|
2438 } |
|
2439 |
|
2440 // Maybe this should be a static function in tree-plot.cc? |
|
2441 |
620
|
2442 // If TC is matrix, save it on stream OS in a format useful for |
604
|
2443 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
2444 // nonzero, assume a parametric 3-dimensional plot will be generated. |
|
2445 |
|
2446 int |
620
|
2447 save_three_d (ostream& os, const tree_constant& tc, int parametric) |
604
|
2448 { |
620
|
2449 int fail = 0; |
604
|
2450 |
620
|
2451 int nr = tc.rows (); |
|
2452 int nc = tc.columns (); |
|
2453 |
|
2454 if (tc.is_real_matrix ()) |
604
|
2455 { |
|
2456 os << "# 3D data...\n" |
|
2457 << "# type: matrix\n" |
|
2458 << "# total rows: " << nr << "\n" |
|
2459 << "# total columns: " << nc << "\n"; |
|
2460 |
|
2461 if (parametric) |
|
2462 { |
|
2463 int extras = nc % 3; |
|
2464 if (extras) |
|
2465 warning ("ignoring last %d columns", extras); |
|
2466 |
620
|
2467 Matrix tmp = tc.matrix_value (); |
872
|
2468 tmp = strip_infnan (tmp); |
|
2469 nr = tmp.rows (); |
|
2470 |
604
|
2471 for (int i = 0; i < nc-extras; i += 3) |
|
2472 { |
|
2473 os << tmp.extract (0, i, nr-1, i+2); |
|
2474 if (i+3 < nc-extras) |
|
2475 os << "\n"; |
|
2476 } |
|
2477 } |
|
2478 else |
|
2479 { |
620
|
2480 Matrix tmp = tc.matrix_value (); |
872
|
2481 tmp = strip_infnan (tmp); |
|
2482 nr = tmp.rows (); |
|
2483 |
604
|
2484 for (int i = 0; i < nc; i++) |
|
2485 { |
|
2486 os << tmp.extract (0, i, nr-1, i); |
|
2487 if (i+1 < nc) |
|
2488 os << "\n"; |
|
2489 } |
|
2490 } |
620
|
2491 } |
|
2492 else |
|
2493 { |
604
|
2494 ::error ("for now, I can only save real matrices in 3D format"); |
620
|
2495 fail = 1; |
604
|
2496 } |
620
|
2497 |
|
2498 return (os && ! fail); |
604
|
2499 } |
|
2500 |
|
2501 /* |
|
2502 ;;; Local Variables: *** |
|
2503 ;;; mode: C++ *** |
|
2504 ;;; page-delimiter: "^/\\*" *** |
|
2505 ;;; End: *** |
|
2506 */ |