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