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