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