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