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" |
2318
|
39 #include "mach-info.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" |
2371
|
52 #include "pt-fvc.h" |
1352
|
53 #include "symtab.h" |
|
54 #include "sysdep.h" |
|
55 #include "unwind-prot.h" |
604
|
56 #include "utils.h" |
2371
|
57 #include "variables.h" |
604
|
58 |
2194
|
59 // The default output format. May be one of "binary", "text", or |
|
60 // "mat-binary". |
|
61 static string Vdefault_save_format; |
|
62 |
|
63 // The number of decimal digits to use when writing ascii data. |
|
64 static int Vsave_precision; |
|
65 |
872
|
66 // Used when converting Inf to something that gnuplot can read. |
|
67 |
|
68 #ifndef OCT_RBV |
|
69 #define OCT_RBV DBL_MAX / 100.0 |
|
70 #endif |
|
71 |
604
|
72 enum load_save_format |
|
73 { |
|
74 LS_ASCII, |
|
75 LS_BINARY, |
|
76 LS_MAT_BINARY, |
|
77 LS_UNKNOWN, |
|
78 }; |
|
79 |
630
|
80 // XXX FIXME XXX -- shouldn't this be implemented in terms of other |
|
81 // functions that are already available? |
604
|
82 |
|
83 // Install a variable with name NAME and the value specified TC in the |
|
84 // symbol table. If FORCE is nonzero, replace any existing definition |
|
85 // for NAME. If GLOBAL is nonzero, make the variable global. |
|
86 // |
|
87 // Assumes TC is defined. |
|
88 |
|
89 static void |
2371
|
90 install_loaded_variable (int force, char *name, const octave_value& val, |
604
|
91 int global, char *doc) |
|
92 { |
1358
|
93 // Is there already a symbol by this name? If so, what is it? |
604
|
94 |
|
95 symbol_record *lsr = curr_sym_tab->lookup (name, 0, 0); |
|
96 |
|
97 int is_undefined = 1; |
|
98 int is_variable = 0; |
|
99 int is_function = 0; |
|
100 int is_global = 0; |
|
101 |
|
102 if (lsr) |
|
103 { |
|
104 is_undefined = ! lsr->is_defined (); |
|
105 is_variable = lsr->is_variable (); |
|
106 is_function = lsr->is_function (); |
|
107 is_global = lsr->is_linked_to_global (); |
|
108 } |
|
109 |
|
110 symbol_record *sr = 0; |
|
111 |
|
112 if (global) |
|
113 { |
|
114 if (is_global || is_undefined) |
|
115 { |
|
116 if (force || is_undefined) |
|
117 { |
|
118 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
119 link_to_global_variable (lsr); |
|
120 sr = lsr; |
|
121 } |
|
122 else |
|
123 { |
|
124 warning ("load: global variable name `%s' exists.", name); |
|
125 warning ("use `load -force' to overwrite"); |
|
126 } |
|
127 } |
|
128 else if (is_function) |
|
129 { |
|
130 if (force) |
|
131 { |
|
132 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
133 link_to_global_variable (lsr); |
|
134 sr = lsr; |
|
135 } |
|
136 else |
|
137 { |
|
138 warning ("load: `%s' is currently a function in this scope", name); |
|
139 warning ("`load -force' will load variable and hide function"); |
|
140 } |
|
141 } |
|
142 else if (is_variable) |
|
143 { |
|
144 if (force) |
|
145 { |
|
146 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
147 link_to_global_variable (lsr); |
|
148 sr = lsr; |
|
149 } |
|
150 else |
|
151 { |
|
152 warning ("load: local variable name `%s' exists.", name); |
|
153 warning ("use `load -force' to overwrite"); |
|
154 } |
|
155 } |
|
156 else |
774
|
157 error ("load: unable to load data for unknown symbol type"); |
604
|
158 } |
|
159 else |
|
160 { |
|
161 if (is_global) |
|
162 { |
|
163 if (force || is_undefined) |
|
164 { |
|
165 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
166 link_to_global_variable (lsr); |
|
167 sr = lsr; |
|
168 } |
|
169 else |
|
170 { |
|
171 warning ("load: global variable name `%s' exists.", name); |
|
172 warning ("use `load -force' to overwrite"); |
|
173 } |
|
174 } |
|
175 else if (is_function) |
|
176 { |
|
177 if (force) |
|
178 { |
|
179 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
180 link_to_global_variable (lsr); |
|
181 sr = lsr; |
|
182 } |
|
183 else |
|
184 { |
|
185 warning ("load: `%s' is currently a function in this scope", name); |
|
186 warning ("`load -force' will load variable and hide function"); |
|
187 } |
|
188 } |
|
189 else if (is_variable || is_undefined) |
|
190 { |
|
191 if (force || is_undefined) |
|
192 { |
|
193 lsr = curr_sym_tab->lookup (name, 1, 0); |
|
194 sr = lsr; |
|
195 } |
|
196 else |
|
197 { |
|
198 warning ("load: local variable name `%s' exists.", name); |
|
199 warning ("use `load -force' to overwrite"); |
|
200 } |
|
201 } |
|
202 else |
774
|
203 error ("load: unable to load data for unknown symbol type"); |
604
|
204 } |
|
205 |
|
206 if (sr) |
|
207 { |
2371
|
208 sr->define (val); |
604
|
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 * |
2318
|
707 read_binary_data (istream& is, int swap, |
|
708 oct_mach_info::float_format fmt, |
1755
|
709 const string& filename, int& global, |
2086
|
710 octave_value& tc, char *&doc) |
604
|
711 { |
|
712 char tmp = 0; |
|
713 |
|
714 FOUR_BYTE_INT name_len = 0, doc_len = 0; |
|
715 char *name = 0; |
|
716 |
|
717 doc = 0; |
|
718 |
1358
|
719 // We expect to fail here, at the beginning of a record, so not |
|
720 // being able to read another name should not result in an error. |
867
|
721 |
604
|
722 is.read (&name_len, 4); |
|
723 if (! is) |
867
|
724 return 0; |
604
|
725 if (swap) |
|
726 swap_4_bytes ((char *) &name_len); |
|
727 |
|
728 name = new char [name_len+1]; |
|
729 name[name_len] = '\0'; |
|
730 if (! is.read (name, name_len)) |
|
731 goto data_read_error; |
|
732 |
|
733 is.read (&doc_len, 4); |
|
734 if (! is) |
|
735 goto data_read_error; |
|
736 if (swap) |
|
737 swap_4_bytes ((char *) &doc_len); |
|
738 |
|
739 doc = new char [doc_len+1]; |
|
740 doc[doc_len] = '\0'; |
|
741 if (! is.read (doc, doc_len)) |
|
742 goto data_read_error; |
|
743 |
|
744 if (! is.read (&tmp, 1)) |
|
745 goto data_read_error; |
|
746 global = tmp ? 1 : 0; |
|
747 |
|
748 tmp = 0; |
|
749 if (! is.read (&tmp, 1)) |
|
750 goto data_read_error; |
|
751 |
|
752 switch (tmp) |
|
753 { |
|
754 case 1: |
|
755 { |
630
|
756 if (! is.read (&tmp, 1)) |
604
|
757 goto data_read_error; |
630
|
758 double dtmp; |
|
759 read_doubles (is, &dtmp, (save_type) tmp, 1, swap, fmt); |
774
|
760 if (error_state || ! is) |
630
|
761 goto data_read_error; |
604
|
762 tc = dtmp; |
|
763 } |
|
764 break; |
|
765 |
|
766 case 2: |
|
767 { |
|
768 FOUR_BYTE_INT nr, nc; |
|
769 if (! is.read (&nr, 4)) |
|
770 goto data_read_error; |
|
771 if (swap) |
|
772 swap_4_bytes ((char *) &nr); |
|
773 if (! is.read (&nc, 4)) |
|
774 goto data_read_error; |
|
775 if (swap) |
|
776 swap_4_bytes ((char *) &nc); |
|
777 if (! is.read (&tmp, 1)) |
|
778 goto data_read_error; |
|
779 Matrix m (nr, nc); |
|
780 double *re = m.fortran_vec (); |
|
781 int len = nr * nc; |
|
782 read_doubles (is, re, (save_type) tmp, len, swap, fmt); |
774
|
783 if (error_state || ! is) |
604
|
784 goto data_read_error; |
|
785 tc = m; |
|
786 } |
|
787 break; |
|
788 |
|
789 case 3: |
|
790 { |
630
|
791 if (! is.read (&tmp, 1)) |
604
|
792 goto data_read_error; |
630
|
793 Complex ctmp; |
|
794 read_doubles (is, (double *) &ctmp, (save_type) tmp, 2, swap, fmt); |
774
|
795 if (error_state || ! is) |
630
|
796 goto data_read_error; |
604
|
797 tc = ctmp; |
|
798 } |
|
799 break; |
|
800 |
|
801 case 4: |
|
802 { |
|
803 FOUR_BYTE_INT nr, nc; |
|
804 if (! is.read (&nr, 4)) |
|
805 goto data_read_error; |
|
806 if (swap) |
|
807 swap_4_bytes ((char *) &nr); |
|
808 if (! is.read (&nc, 4)) |
|
809 goto data_read_error; |
|
810 if (swap) |
|
811 swap_4_bytes ((char *) &nc); |
|
812 if (! is.read (&tmp, 1)) |
|
813 goto data_read_error; |
|
814 ComplexMatrix m (nr, nc); |
|
815 Complex *im = m.fortran_vec (); |
|
816 int len = nr * nc; |
630
|
817 read_doubles (is, (double *) im, (save_type) tmp, 2*len, |
|
818 swap, fmt); |
774
|
819 if (error_state || ! is) |
604
|
820 goto data_read_error; |
|
821 tc = m; |
|
822 } |
|
823 break; |
|
824 |
|
825 case 5: |
|
826 { |
1427
|
827 FOUR_BYTE_INT len; |
604
|
828 if (! is.read (&len, 4)) |
|
829 goto data_read_error; |
|
830 if (swap) |
|
831 swap_4_bytes ((char *) &len); |
|
832 char *s = new char [len+1]; |
|
833 if (! is.read (s, len)) |
|
834 { |
|
835 delete [] s; |
|
836 goto data_read_error; |
|
837 } |
|
838 s[len] = '\0'; |
|
839 tc = s; |
|
840 } |
|
841 break; |
|
842 |
|
843 case 6: |
|
844 { |
630
|
845 if (! is.read (&tmp, 1)) |
|
846 goto data_read_error; |
604
|
847 double bas, lim, inc; |
|
848 if (! is.read (&bas, 8)) |
|
849 goto data_read_error; |
|
850 if (swap) |
|
851 swap_8_bytes ((char *) &bas); |
|
852 if (! is.read (&lim, 8)) |
|
853 goto data_read_error; |
|
854 if (swap) |
|
855 swap_8_bytes ((char *) &lim); |
|
856 if (! is.read (&inc, 8)) |
|
857 goto data_read_error; |
|
858 if (swap) |
|
859 swap_8_bytes ((char *) &inc); |
|
860 Range r (bas, lim, inc); |
|
861 tc = r; |
|
862 } |
|
863 break; |
|
864 |
1427
|
865 case 7: |
|
866 { |
|
867 FOUR_BYTE_INT elements; |
|
868 if (! is.read (&elements, 4)) |
|
869 goto data_read_error; |
|
870 if (swap) |
|
871 swap_4_bytes ((char *) &elements); |
1572
|
872 charMatrix chm (elements, 0); |
|
873 int max_len = 0; |
1427
|
874 for (int i = 0; i < elements; i++) |
|
875 { |
|
876 FOUR_BYTE_INT len; |
|
877 if (! is.read (&len, 4)) |
|
878 goto data_read_error; |
|
879 if (swap) |
|
880 swap_4_bytes ((char *) &len); |
|
881 char *tmp = new char [len]; |
|
882 if (! is.read (tmp, len)) |
|
883 { |
|
884 delete [] tmp; |
|
885 goto data_read_error; |
|
886 } |
1572
|
887 if (len > max_len) |
|
888 { |
|
889 max_len = len; |
|
890 chm.resize (elements, max_len, 0); |
|
891 } |
|
892 chm.insert (tmp, i, 0); |
1427
|
893 delete [] tmp; |
|
894 } |
1572
|
895 tc = chm; |
1427
|
896 } |
|
897 break; |
|
898 |
604
|
899 default: |
|
900 data_read_error: |
1755
|
901 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
604
|
902 delete [] name; |
|
903 name = 0; |
|
904 break; |
|
905 } |
|
906 |
|
907 return name; |
|
908 } |
|
909 |
|
910 // Read LEN elements of data from IS in the format specified by |
|
911 // PRECISION, placing the result in DATA. If SWAP is nonzero, swap |
|
912 // the bytes of each element before copying to DATA. FLT_FMT |
|
913 // specifies the format of the data if we are reading floating point |
|
914 // numbers. |
|
915 |
|
916 static void |
|
917 read_mat_binary_data (istream& is, double *data, int precision, |
2318
|
918 int len, int swap, |
|
919 oct_mach_info::float_format flt_fmt) |
604
|
920 { |
|
921 switch (precision) |
|
922 { |
|
923 case 0: |
630
|
924 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); |
604
|
925 break; |
|
926 |
|
927 case 1: |
630
|
928 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); |
604
|
929 break; |
|
930 |
|
931 case 2: |
|
932 read_doubles (is, data, LS_INT, len, swap, flt_fmt); |
|
933 break; |
|
934 |
|
935 case 3: |
|
936 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); |
|
937 break; |
|
938 |
|
939 case 4: |
|
940 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); |
|
941 break; |
|
942 |
|
943 case 5: |
|
944 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); |
|
945 break; |
|
946 |
|
947 default: |
|
948 break; |
|
949 } |
|
950 } |
|
951 |
|
952 static int |
|
953 read_mat_file_header (istream& is, int& swap, FOUR_BYTE_INT& mopt, |
|
954 FOUR_BYTE_INT& nr, FOUR_BYTE_INT& nc, |
|
955 FOUR_BYTE_INT& imag, FOUR_BYTE_INT& len, |
|
956 int quiet = 0) |
|
957 { |
671
|
958 swap = 0; |
|
959 |
1358
|
960 // We expect to fail here, at the beginning of a record, so not |
|
961 // being able to read another mopt value should not result in an |
|
962 // error. |
911
|
963 |
604
|
964 is.read (&mopt, 4); |
|
965 if (! is) |
911
|
966 return 1; |
604
|
967 |
|
968 if (! is.read (&nr, 4)) |
|
969 goto data_read_error; |
|
970 |
|
971 if (! is.read (&nc, 4)) |
|
972 goto data_read_error; |
|
973 |
|
974 if (! is.read (&imag, 4)) |
|
975 goto data_read_error; |
|
976 |
|
977 if (! is.read (&len, 4)) |
|
978 goto data_read_error; |
|
979 |
|
980 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
981 // bigger than we expect, so we swap bytes. |
|
982 // |
|
983 // If mopt is zero, it means the file was written on a little endian |
|
984 // machine, and we only need to swap if we are running on a big endian |
|
985 // machine. |
|
986 // |
|
987 // Gag me. |
|
988 |
2318
|
989 if (oct_mach_info::words_big_endian () && mopt == 0) |
604
|
990 swap = 1; |
|
991 |
1358
|
992 // mopt is signed, therefore byte swap may result in negative value. |
911
|
993 |
|
994 if (mopt > 9999 || mopt < 0) |
604
|
995 swap = 1; |
|
996 |
|
997 if (swap) |
|
998 { |
|
999 swap_4_bytes ((char *) &mopt); |
|
1000 swap_4_bytes ((char *) &nr); |
|
1001 swap_4_bytes ((char *) &nc); |
|
1002 swap_4_bytes ((char *) &imag); |
|
1003 swap_4_bytes ((char *) &len); |
|
1004 } |
|
1005 |
911
|
1006 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) |
604
|
1007 { |
|
1008 if (! quiet) |
|
1009 error ("load: can't read binary file"); |
|
1010 return -1; |
|
1011 } |
|
1012 |
|
1013 return 0; |
|
1014 |
|
1015 data_read_error: |
|
1016 return -1; |
|
1017 } |
|
1018 |
617
|
1019 // We don't just use a cast here, because we need to be able to detect |
|
1020 // possible errors. |
|
1021 |
2318
|
1022 static oct_mach_info::float_format |
|
1023 mopt_digit_to_float_format (int mach) |
617
|
1024 { |
2318
|
1025 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
619
|
1026 |
617
|
1027 switch (mach) |
|
1028 { |
|
1029 case 0: |
2318
|
1030 flt_fmt = oct_mach_info::ieee_little_endian; |
617
|
1031 break; |
|
1032 |
|
1033 case 1: |
2318
|
1034 flt_fmt = oct_mach_info::ieee_big_endian; |
617
|
1035 break; |
|
1036 |
|
1037 case 2: |
2318
|
1038 flt_fmt = oct_mach_info::vax_d; |
617
|
1039 break; |
|
1040 |
|
1041 case 3: |
2318
|
1042 flt_fmt = oct_mach_info::vax_g; |
617
|
1043 break; |
|
1044 |
|
1045 case 4: |
2318
|
1046 flt_fmt = oct_mach_info::cray; |
617
|
1047 break; |
|
1048 |
|
1049 default: |
2318
|
1050 flt_fmt = oct_mach_info::unknown; |
617
|
1051 break; |
|
1052 } |
619
|
1053 |
|
1054 return flt_fmt; |
617
|
1055 } |
619
|
1056 |
2318
|
1057 static int |
|
1058 float_format_to_mopt_digit (oct_mach_info::float_format flt_fmt) |
|
1059 { |
|
1060 int retval = -1; |
|
1061 |
|
1062 switch (flt_fmt) |
|
1063 { |
|
1064 case oct_mach_info::ieee_little_endian: |
|
1065 retval = 0; |
|
1066 break; |
|
1067 |
|
1068 case oct_mach_info::ieee_big_endian: |
|
1069 retval = 1; |
|
1070 break; |
|
1071 |
|
1072 case oct_mach_info::vax_d: |
|
1073 retval = 2; |
|
1074 break; |
|
1075 |
|
1076 case oct_mach_info::vax_g: |
|
1077 retval = 3; |
|
1078 break; |
|
1079 |
|
1080 case oct_mach_info::cray: |
|
1081 retval = 4; |
|
1082 break; |
|
1083 |
|
1084 default: |
|
1085 break; |
|
1086 } |
|
1087 |
|
1088 return retval; |
|
1089 } |
|
1090 |
604
|
1091 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
1092 // place it in TC, returning the name of the variable. |
|
1093 // |
|
1094 // The data is expected to be in Matlab's .mat format, though not all |
|
1095 // the features of that format are supported. |
|
1096 // |
|
1097 // FILENAME is used for error messages. |
|
1098 // |
|
1099 // This format provides no way to tag the data as global. |
|
1100 |
|
1101 static char * |
1755
|
1102 read_mat_binary_data (istream& is, const string& filename, |
2086
|
1103 octave_value& tc) |
604
|
1104 { |
1358
|
1105 // These are initialized here instead of closer to where they are |
|
1106 // first used to avoid errors from gcc about goto crossing |
|
1107 // initialization of variable. |
604
|
1108 |
|
1109 Matrix re; |
2318
|
1110 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
604
|
1111 char *name = 0; |
|
1112 int swap = 0, type = 0, prec = 0, mach = 0, dlen = 0; |
|
1113 |
|
1114 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
1115 |
|
1116 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
1117 if (err) |
|
1118 { |
|
1119 if (err < 0) |
|
1120 goto data_read_error; |
|
1121 else |
|
1122 return 0; |
|
1123 } |
|
1124 |
|
1125 type = mopt % 10; // Full, sparse, etc. |
|
1126 mopt /= 10; // Eliminate first digit. |
|
1127 prec = mopt % 10; // double, float, int, etc. |
|
1128 mopt /= 100; // Skip unused third digit too. |
|
1129 mach = mopt % 10; // IEEE, VAX, etc. |
|
1130 |
2318
|
1131 flt_fmt = mopt_digit_to_float_format (mach); |
|
1132 |
|
1133 if (flt_fmt == oct_mach_info::unknown) |
604
|
1134 { |
|
1135 error ("load: unrecognized binary format!"); |
|
1136 return 0; |
|
1137 } |
|
1138 |
|
1139 if (type != 0 && type != 1) |
|
1140 { |
|
1141 error ("load: can't read sparse matrices"); |
|
1142 return 0; |
|
1143 } |
|
1144 |
|
1145 if (imag && type == 1) |
|
1146 { |
|
1147 error ("load: encountered complex matrix with string flag set!"); |
|
1148 return 0; |
|
1149 } |
|
1150 |
2345
|
1151 // LEN includes the terminating character, and the file is also |
2436
|
1152 // supposed to include it, but apparently not all files do. Either |
|
1153 // way, I think this should work. |
2345
|
1154 |
2436
|
1155 name = new char [len+1]; |
604
|
1156 if (! is.read (name, len)) |
|
1157 goto data_read_error; |
2436
|
1158 name[len] = '\0'; |
604
|
1159 |
|
1160 dlen = nr * nc; |
|
1161 if (dlen < 0) |
|
1162 goto data_read_error; |
|
1163 |
|
1164 re.resize (nr, nc); |
|
1165 |
|
1166 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1167 |
|
1168 if (! is || error_state) |
|
1169 { |
|
1170 error ("load: reading matrix data for `%s'", name); |
|
1171 goto data_read_error; |
|
1172 } |
|
1173 |
|
1174 if (imag) |
|
1175 { |
|
1176 Matrix im (nr, nc); |
|
1177 |
|
1178 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1179 |
|
1180 if (! is || error_state) |
|
1181 { |
|
1182 error ("load: reading imaginary matrix data for `%s'", name); |
|
1183 goto data_read_error; |
|
1184 } |
|
1185 |
|
1186 ComplexMatrix ctmp (nr, nc); |
|
1187 |
|
1188 for (int j = 0; j < nc; j++) |
|
1189 for (int i = 0; i < nr; i++) |
2305
|
1190 ctmp (i, j) = Complex (re (i, j), im (i, j)); |
604
|
1191 |
|
1192 tc = ctmp; |
|
1193 } |
|
1194 else |
|
1195 tc = re; |
|
1196 |
1427
|
1197 if (type == 1) |
604
|
1198 tc = tc.convert_to_str (); |
|
1199 |
|
1200 return name; |
|
1201 |
|
1202 data_read_error: |
1755
|
1203 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
604
|
1204 delete [] name; |
|
1205 return 0; |
|
1206 } |
|
1207 |
|
1208 // Return nonzero if NAME matches one of the given globbing PATTERNS. |
|
1209 |
|
1210 static int |
1755
|
1211 matches_patterns (const string_vector& patterns, int pat_idx, |
1792
|
1212 int num_pat, const string& name) |
604
|
1213 { |
1755
|
1214 for (int i = pat_idx; i < num_pat; i++) |
604
|
1215 { |
1792
|
1216 glob_match pattern (patterns[i]); |
|
1217 if (pattern.match (name)) |
604
|
1218 return 1; |
|
1219 } |
|
1220 return 0; |
|
1221 } |
|
1222 |
|
1223 static int |
|
1224 read_binary_file_header (istream& is, int& swap, |
2318
|
1225 oct_mach_info::float_format& flt_fmt, |
|
1226 int quiet = 0) |
604
|
1227 { |
|
1228 int magic_len = 10; |
|
1229 char magic [magic_len+1]; |
|
1230 is.read (magic, magic_len); |
|
1231 magic[magic_len] = '\0'; |
|
1232 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
2318
|
1233 swap = oct_mach_info::words_big_endian (); |
604
|
1234 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
2318
|
1235 swap = ! oct_mach_info::words_big_endian (); |
604
|
1236 else |
|
1237 { |
|
1238 if (! quiet) |
|
1239 error ("load: can't read binary file"); |
|
1240 return -1; |
|
1241 } |
|
1242 |
|
1243 char tmp = 0; |
|
1244 is.read (&tmp, 1); |
|
1245 |
2318
|
1246 flt_fmt = mopt_digit_to_float_format (tmp); |
|
1247 |
|
1248 if (flt_fmt == oct_mach_info::unknown) |
604
|
1249 { |
|
1250 if (! quiet) |
|
1251 error ("load: unrecognized binary format!"); |
|
1252 return -1; |
|
1253 } |
|
1254 |
|
1255 return 0; |
|
1256 } |
|
1257 |
|
1258 static load_save_format |
1750
|
1259 get_file_format (const string& fname, const string& orig_fname) |
604
|
1260 { |
|
1261 load_save_format retval = LS_UNKNOWN; |
|
1262 |
1750
|
1263 ifstream file (fname.c_str ()); |
604
|
1264 |
|
1265 if (! file) |
|
1266 { |
1750
|
1267 error ("load: couldn't open input file `%s'", orig_fname.c_str ()); |
604
|
1268 return retval; |
|
1269 } |
|
1270 |
|
1271 int swap; |
2318
|
1272 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
604
|
1273 |
|
1274 if (read_binary_file_header (file, swap, flt_fmt, 1) == 0) |
|
1275 retval = LS_BINARY; |
|
1276 else |
|
1277 { |
|
1278 file.seekg (0, ios::beg); |
|
1279 |
|
1280 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
1180
|
1281 |
|
1282 int err = read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1); |
|
1283 |
|
1284 if (! err) |
604
|
1285 retval = LS_MAT_BINARY; |
|
1286 else |
|
1287 { |
|
1288 file.seekg (0, ios::beg); |
|
1289 |
|
1290 char *tmp = extract_keyword (file, "name"); |
1180
|
1291 |
604
|
1292 if (tmp) |
1180
|
1293 { |
|
1294 retval = LS_ASCII; |
|
1295 delete [] tmp; |
|
1296 } |
604
|
1297 } |
|
1298 } |
|
1299 |
|
1300 file.close (); |
|
1301 |
|
1302 if (retval == LS_UNKNOWN) |
1750
|
1303 error ("load: unable to determine file format for `%s'", |
|
1304 orig_fname.c_str ()); |
604
|
1305 |
|
1306 return retval; |
|
1307 } |
|
1308 |
2086
|
1309 static octave_value_list |
1755
|
1310 do_load (istream& stream, const string& orig_fname, int force, |
2318
|
1311 load_save_format format, oct_mach_info::float_format flt_fmt, |
1755
|
1312 int list_only, int swap, int verbose, const string_vector& argv, |
|
1313 int argv_idx, int argc, int nargout) |
604
|
1314 { |
2086
|
1315 octave_value_list retval; |
604
|
1316 |
621
|
1317 ostrstream output_buf; |
604
|
1318 int count = 0; |
|
1319 for (;;) |
|
1320 { |
|
1321 int global = 0; |
2086
|
1322 octave_value tc; |
604
|
1323 |
|
1324 char *name = 0; |
|
1325 char *doc = 0; |
|
1326 |
|
1327 switch (format) |
|
1328 { |
|
1329 case LS_ASCII: |
|
1330 name = read_ascii_data (stream, orig_fname, global, tc); |
|
1331 break; |
|
1332 |
|
1333 case LS_BINARY: |
|
1334 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
1335 global, tc, doc); |
|
1336 break; |
|
1337 |
|
1338 case LS_MAT_BINARY: |
|
1339 name = read_mat_binary_data (stream, orig_fname, tc); |
|
1340 break; |
|
1341 |
|
1342 default: |
775
|
1343 gripe_unrecognized_data_fmt ("load"); |
604
|
1344 break; |
|
1345 } |
|
1346 |
867
|
1347 if (error_state || stream.eof () || ! name) |
604
|
1348 { |
867
|
1349 delete [] name; |
|
1350 delete [] doc; |
604
|
1351 break; |
|
1352 } |
|
1353 else if (! error_state && name) |
|
1354 { |
|
1355 if (tc.is_defined ()) |
|
1356 { |
1755
|
1357 if (argv_idx == argc |
|
1358 || matches_patterns (argv, argv_idx, argc, name)) |
604
|
1359 { |
|
1360 count++; |
621
|
1361 if (list_only) |
|
1362 { |
|
1363 if (verbose) |
|
1364 { |
|
1365 if (count == 1) |
|
1366 output_buf |
|
1367 << "type rows cols name\n" |
|
1368 << "==== ==== ==== ====\n"; |
|
1369 |
2371
|
1370 string type = tc.type_name (); |
|
1371 output_buf.form ("%-16s", type.c_str ()); |
621
|
1372 output_buf.form ("%7d", tc.rows ()); |
|
1373 output_buf.form ("%7d", tc.columns ()); |
|
1374 output_buf << " "; |
|
1375 } |
|
1376 output_buf << name << "\n"; |
|
1377 } |
|
1378 else |
|
1379 { |
|
1380 install_loaded_variable (force, name, tc, global, doc); |
|
1381 } |
604
|
1382 } |
|
1383 } |
|
1384 else |
|
1385 error ("load: unable to load variable `%s'", name); |
|
1386 } |
|
1387 else |
|
1388 { |
|
1389 if (count == 0) |
|
1390 error ("load: are you sure `%s' is an Octave data file?", |
1755
|
1391 orig_fname.c_str ()); |
604
|
1392 |
867
|
1393 delete [] name; |
|
1394 delete [] doc; |
604
|
1395 break; |
|
1396 } |
|
1397 |
|
1398 delete [] name; |
|
1399 delete [] doc; |
|
1400 } |
|
1401 |
621
|
1402 if (list_only && count) |
|
1403 { |
2095
|
1404 output_buf << ends; |
|
1405 |
|
1406 char *msg = output_buf.str (); |
|
1407 |
621
|
1408 if (nargout > 0) |
2095
|
1409 retval = msg; |
621
|
1410 else |
2095
|
1411 octave_stdout << msg; |
|
1412 |
|
1413 delete [] msg; |
621
|
1414 } |
|
1415 |
863
|
1416 return retval; |
|
1417 } |
|
1418 |
1957
|
1419 DEFUN_TEXT (load, args, nargout, |
910
|
1420 "load [-force] [-ascii] [-binary] [-mat-binary] file [pattern ...]\n\ |
863
|
1421 \n\ |
|
1422 Load variables from a file.\n\ |
|
1423 \n\ |
|
1424 If no argument is supplied to select a format, load tries to read the |
|
1425 named file as an Octave binary, then as a .mat file, and then as an |
|
1426 Octave text file.\n\ |
|
1427 \n\ |
|
1428 If the option -force is given, variables with the same names as those |
|
1429 found in the file will be replaced with the values read from the file.") |
|
1430 { |
2086
|
1431 octave_value_list retval; |
863
|
1432 |
1755
|
1433 int argc = args.length () + 1; |
|
1434 |
1968
|
1435 string_vector argv = args.make_argv ("load"); |
1755
|
1436 |
|
1437 if (error_state) |
|
1438 return retval; |
863
|
1439 |
|
1440 int force = 0; |
|
1441 |
1358
|
1442 // It isn't necessary to have the default load format stored in a |
|
1443 // user preference variable since we can determine the type of file |
|
1444 // as we are reading. |
863
|
1445 |
|
1446 load_save_format format = LS_UNKNOWN; |
|
1447 |
|
1448 int list_only = 0; |
|
1449 int verbose = 0; |
|
1450 |
1755
|
1451 int i; |
|
1452 for (i = 1; i < argc; i++) |
863
|
1453 { |
1755
|
1454 if (argv[i] == "-force" || argv[i] == "-f") |
863
|
1455 { |
|
1456 force++; |
|
1457 } |
1755
|
1458 else if (argv[i] == "-list" || argv[i] == "-l") |
863
|
1459 { |
|
1460 list_only = 1; |
|
1461 } |
1755
|
1462 else if (argv[i] == "-verbose" || argv[i] == "-v") |
863
|
1463 { |
|
1464 verbose = 1; |
|
1465 } |
1755
|
1466 else if (argv[i] == "-ascii" || argv[i] == "-a") |
863
|
1467 { |
|
1468 format = LS_ASCII; |
|
1469 } |
1755
|
1470 else if (argv[i] == "-binary" || argv[i] == "-b") |
863
|
1471 { |
|
1472 format = LS_BINARY; |
|
1473 } |
1755
|
1474 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
863
|
1475 { |
|
1476 format = LS_MAT_BINARY; |
|
1477 } |
|
1478 else |
|
1479 break; |
|
1480 } |
|
1481 |
1755
|
1482 if (i == argc) |
863
|
1483 { |
2057
|
1484 print_usage ("load"); |
863
|
1485 return retval; |
|
1486 } |
|
1487 |
1755
|
1488 string orig_fname = argv[i]; |
863
|
1489 |
2318
|
1490 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
863
|
1491 |
|
1492 int swap = 0; |
|
1493 |
1755
|
1494 if (argv[i] == "-") |
863
|
1495 { |
1755
|
1496 i++; |
863
|
1497 |
|
1498 if (format != LS_UNKNOWN) |
|
1499 { |
1358
|
1500 // XXX FIXME XXX -- if we have already seen EOF on a |
|
1501 // previous call, how do we fix up the state of cin so that |
|
1502 // we can get additional input? I'm afraid that we can't |
|
1503 // fix this using cin only. |
863
|
1504 |
|
1505 retval = do_load (cin, orig_fname, force, format, flt_fmt, |
1755
|
1506 list_only, swap, verbose, argv, i, argc, |
863
|
1507 nargout); |
|
1508 } |
|
1509 else |
|
1510 error ("load: must specify file format if reading from stdin"); |
|
1511 } |
|
1512 else |
|
1513 { |
1755
|
1514 string fname = oct_tilde_expand (argv[i]); |
863
|
1515 |
|
1516 if (format == LS_UNKNOWN) |
|
1517 format = get_file_format (fname, orig_fname); |
|
1518 |
|
1519 if (format != LS_UNKNOWN) |
|
1520 { |
1755
|
1521 i++; |
863
|
1522 |
|
1523 unsigned mode = ios::in; |
|
1524 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
1525 mode |= ios::bin; |
|
1526 |
1750
|
1527 ifstream file (fname.c_str (), mode); |
863
|
1528 |
|
1529 if (file) |
|
1530 { |
|
1531 if (format == LS_BINARY) |
|
1532 { |
|
1533 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
1534 { |
|
1535 file.close (); |
|
1536 return retval; |
|
1537 } |
|
1538 } |
|
1539 |
|
1540 retval = do_load (file, orig_fname, force, format, |
|
1541 flt_fmt, list_only, swap, verbose, |
1755
|
1542 argv, i, argc, nargout); |
863
|
1543 |
|
1544 file.close (); |
|
1545 } |
|
1546 else |
1755
|
1547 error ("load: couldn't open input file `%s'", |
|
1548 orig_fname.c_str ()); |
863
|
1549 } |
|
1550 } |
604
|
1551 |
|
1552 return retval; |
|
1553 } |
|
1554 |
|
1555 // Return nonzero if PATTERN has any special globbing chars in it. |
|
1556 |
|
1557 static int |
1755
|
1558 glob_pattern_p (const string& pattern) |
604
|
1559 { |
|
1560 int open = 0; |
|
1561 |
1755
|
1562 int len = pattern.length (); |
|
1563 |
|
1564 for (int i = 0; i < len; i++) |
604
|
1565 { |
1755
|
1566 char c = pattern[i]; |
|
1567 |
604
|
1568 switch (c) |
|
1569 { |
|
1570 case '?': |
|
1571 case '*': |
|
1572 return 1; |
|
1573 |
|
1574 case '[': // Only accept an open brace if there is a close |
|
1575 open++; // brace to match it. Bracket expressions must be |
|
1576 continue; // complete, according to Posix.2 |
|
1577 |
|
1578 case ']': |
|
1579 if (open) |
|
1580 return 1; |
|
1581 continue; |
|
1582 |
|
1583 case '\\': |
1755
|
1584 if (i == len - 1) |
604
|
1585 return 0; |
|
1586 |
|
1587 default: |
|
1588 continue; |
|
1589 } |
|
1590 } |
|
1591 |
|
1592 return 0; |
|
1593 } |
|
1594 |
618
|
1595 // MAX_VAL and MIN_VAL are assumed to have integral values even though |
|
1596 // they are stored in doubles. |
|
1597 |
604
|
1598 static save_type |
|
1599 get_save_type (double max_val, double min_val) |
|
1600 { |
|
1601 save_type st = LS_DOUBLE; |
|
1602 |
|
1603 if (max_val < 256 && min_val > -1) |
|
1604 st = LS_U_CHAR; |
|
1605 else if (max_val < 65536 && min_val > -1) |
|
1606 st = LS_U_SHORT; |
618
|
1607 else if (max_val < 4294967295 && min_val > -1) |
|
1608 st = LS_U_INT; |
|
1609 else if (max_val < 128 && min_val >= -128) |
|
1610 st = LS_CHAR; |
604
|
1611 else if (max_val < 32768 && min_val >= -32768) |
|
1612 st = LS_SHORT; |
|
1613 else if (max_val < 2147483648 && min_val > -2147483648) |
|
1614 st = LS_INT; |
|
1615 |
|
1616 return st; |
|
1617 } |
|
1618 |
|
1619 // Save the data from TC along with the corresponding NAME, help |
|
1620 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
1427
|
1621 // binary format described above for read_binary_data. |
604
|
1622 |
|
1623 static int |
2086
|
1624 save_binary_data (ostream& os, const octave_value& tc, |
1755
|
1625 const string& name, const string& doc, |
|
1626 int mark_as_global, int save_as_floats) |
604
|
1627 { |
620
|
1628 int fail = 0; |
|
1629 |
1755
|
1630 FOUR_BYTE_INT name_len = name.length (); |
604
|
1631 |
|
1632 os.write (&name_len, 4); |
1755
|
1633 os << name; |
|
1634 |
|
1635 FOUR_BYTE_INT doc_len = doc.length (); |
604
|
1636 |
|
1637 os.write (&doc_len, 4); |
1755
|
1638 os << doc; |
604
|
1639 |
|
1640 char tmp; |
|
1641 |
|
1642 tmp = mark_as_global; |
|
1643 os.write (&tmp, 1); |
|
1644 |
620
|
1645 if (tc.is_real_scalar ()) |
604
|
1646 { |
|
1647 tmp = 1; |
|
1648 os.write (&tmp, 1); |
630
|
1649 tmp = (char) LS_DOUBLE; |
|
1650 os.write (&tmp, 1); |
604
|
1651 double tmp = tc.double_value (); |
|
1652 os.write (&tmp, 8); |
|
1653 } |
620
|
1654 else if (tc.is_real_matrix ()) |
604
|
1655 { |
|
1656 tmp = 2; |
|
1657 os.write (&tmp, 1); |
|
1658 Matrix m = tc.matrix_value (); |
|
1659 FOUR_BYTE_INT nr = m.rows (); |
|
1660 FOUR_BYTE_INT nc = m.columns (); |
|
1661 os.write (&nr, 4); |
|
1662 os.write (&nc, 4); |
|
1663 int len = nr * nc; |
|
1664 save_type st = LS_DOUBLE; |
630
|
1665 if (save_as_floats) |
|
1666 { |
1963
|
1667 if (m.too_large_for_float ()) |
630
|
1668 { |
|
1669 warning ("save: some values too large to save as floats --"); |
|
1670 warning ("save: saving as doubles instead"); |
|
1671 } |
|
1672 else |
|
1673 st = LS_FLOAT; |
|
1674 } |
|
1675 else if (len > 8192) // XXX FIXME XXX -- make this configurable. |
604
|
1676 { |
|
1677 double max_val, min_val; |
1963
|
1678 if (m.all_integers (max_val, min_val)) |
604
|
1679 st = get_save_type (max_val, min_val); |
|
1680 } |
630
|
1681 const double *mtmp = m.data (); |
604
|
1682 write_doubles (os, mtmp, st, len); |
|
1683 } |
|
1684 else if (tc.is_complex_scalar ()) |
|
1685 { |
|
1686 tmp = 3; |
|
1687 os.write (&tmp, 1); |
630
|
1688 tmp = (char) LS_DOUBLE; |
|
1689 os.write (&tmp, 1); |
604
|
1690 Complex tmp = tc.complex_value (); |
|
1691 os.write (&tmp, 16); |
|
1692 } |
|
1693 else if (tc.is_complex_matrix ()) |
|
1694 { |
|
1695 tmp = 4; |
|
1696 os.write (&tmp, 1); |
|
1697 ComplexMatrix m = tc.complex_matrix_value (); |
|
1698 FOUR_BYTE_INT nr = m.rows (); |
|
1699 FOUR_BYTE_INT nc = m.columns (); |
|
1700 os.write (&nr, 4); |
|
1701 os.write (&nc, 4); |
|
1702 int len = nr * nc; |
|
1703 save_type st = LS_DOUBLE; |
630
|
1704 if (save_as_floats) |
|
1705 { |
1963
|
1706 if (m.too_large_for_float ()) |
630
|
1707 { |
|
1708 warning ("save: some values too large to save as floats --"); |
|
1709 warning ("save: saving as doubles instead"); |
|
1710 } |
|
1711 else |
|
1712 st = LS_FLOAT; |
|
1713 } |
|
1714 else if (len > 4096) // XXX FIXME XXX -- make this configurable. |
604
|
1715 { |
|
1716 double max_val, min_val; |
1963
|
1717 if (m.all_integers (max_val, min_val)) |
604
|
1718 st = get_save_type (max_val, min_val); |
|
1719 } |
630
|
1720 const Complex *mtmp = m.data (); |
|
1721 write_doubles (os, (const double *) mtmp, st, 2*len); |
604
|
1722 } |
|
1723 else if (tc.is_string ()) |
|
1724 { |
1427
|
1725 tmp = 7; |
604
|
1726 os.write (&tmp, 1); |
1427
|
1727 FOUR_BYTE_INT nr = tc.rows (); |
|
1728 os.write (&nr, 4); |
1572
|
1729 charMatrix chm = tc.all_strings (); |
1427
|
1730 for (int i = 0; i < nr; i++) |
|
1731 { |
1572
|
1732 FOUR_BYTE_INT len = chm.cols (); |
1427
|
1733 os.write (&len, 4); |
1728
|
1734 string tstr = chm.row_as_string (i); |
|
1735 const char *tmp = tstr.data (); |
1427
|
1736 os.write (tmp, len); |
|
1737 } |
604
|
1738 } |
|
1739 else if (tc.is_range ()) |
|
1740 { |
|
1741 tmp = 6; |
|
1742 os.write (&tmp, 1); |
630
|
1743 tmp = (char) LS_DOUBLE; |
|
1744 os.write (&tmp, 1); |
604
|
1745 Range r = tc.range_value (); |
|
1746 double bas = r.base (); |
|
1747 double lim = r.limit (); |
|
1748 double inc = r.inc (); |
|
1749 os.write (&bas, 8); |
|
1750 os.write (&lim, 8); |
|
1751 os.write (&inc, 8); |
|
1752 } |
|
1753 else |
620
|
1754 { |
|
1755 gripe_wrong_type_arg ("save", tc); |
|
1756 fail = 1; |
|
1757 } |
604
|
1758 |
620
|
1759 return (os && ! fail); |
604
|
1760 } |
|
1761 |
667
|
1762 // Save the data from TC along with the corresponding NAME on stream OS |
|
1763 // in the MatLab binary format. |
|
1764 |
|
1765 static int |
2086
|
1766 save_mat_binary_data (ostream& os, const octave_value& tc, |
1755
|
1767 const string& name) |
667
|
1768 { |
|
1769 int fail = 0; |
|
1770 |
|
1771 FOUR_BYTE_INT mopt = 0; |
|
1772 |
|
1773 mopt += tc.is_string () ? 1 : 0; |
2318
|
1774 |
|
1775 oct_mach_info::float_format flt_fmt = |
|
1776 oct_mach_info::native_float_format ();; |
|
1777 |
|
1778 mopt += 1000 * float_format_to_mopt_digit (flt_fmt); |
667
|
1779 |
|
1780 os.write (&mopt, 4); |
|
1781 |
|
1782 FOUR_BYTE_INT nr = tc.rows (); |
|
1783 os.write (&nr, 4); |
|
1784 |
|
1785 FOUR_BYTE_INT nc = tc.columns (); |
|
1786 os.write (&nc, 4); |
|
1787 |
|
1788 int len = nr * nc; |
|
1789 |
|
1790 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; |
|
1791 os.write (&imag, 4); |
|
1792 |
2345
|
1793 // LEN includes the terminating character, and the file is also |
|
1794 // supposed to include it. |
|
1795 |
|
1796 FOUR_BYTE_INT name_len = name.length () + 1; |
667
|
1797 |
|
1798 os.write (&name_len, 4); |
2345
|
1799 os << name << '\0'; |
667
|
1800 |
|
1801 if (tc.is_real_scalar ()) |
|
1802 { |
|
1803 double tmp = tc.double_value (); |
|
1804 os.write (&tmp, 8); |
|
1805 } |
911
|
1806 else if (tc.is_real_matrix ()) |
667
|
1807 { |
|
1808 Matrix m = tc.matrix_value (); |
|
1809 os.write (m.data (), 8 * len); |
|
1810 } |
|
1811 else if (tc.is_complex_scalar ()) |
|
1812 { |
|
1813 Complex tmp = tc.complex_value (); |
|
1814 os.write (&tmp, 16); |
|
1815 } |
|
1816 else if (tc.is_complex_matrix ()) |
|
1817 { |
|
1818 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
1819 Matrix m = ::real(m_cmplx); |
|
1820 os.write (m.data (), 8 * len); |
|
1821 m = ::imag(m_cmplx); |
|
1822 os.write (m.data (), 8 * len); |
|
1823 } |
|
1824 else if (tc.is_string ()) |
|
1825 { |
|
1826 begin_unwind_frame ("save_mat_binary_data"); |
2181
|
1827 unwind_protect_int (Vimplicit_str_to_num_ok); |
|
1828 Vimplicit_str_to_num_ok = 1; |
667
|
1829 Matrix m = tc.matrix_value (); |
|
1830 os.write (m.data (), 8 * len); |
|
1831 run_unwind_frame ("save_mat_binary_data"); |
|
1832 } |
911
|
1833 else if (tc.is_range ()) |
|
1834 { |
|
1835 Range r = tc.range_value (); |
|
1836 double base = r.base (); |
|
1837 double inc = r.inc (); |
|
1838 int nel = r.nelem (); |
|
1839 for (int i = 0; i < nel; i++) |
|
1840 { |
|
1841 double x = base + i * inc; |
|
1842 os.write (&x, 8); |
|
1843 } |
|
1844 } |
667
|
1845 else |
|
1846 { |
|
1847 gripe_wrong_type_arg ("save", tc); |
|
1848 fail = 1; |
|
1849 } |
|
1850 |
|
1851 return (os && ! fail); |
|
1852 } |
|
1853 |
620
|
1854 static void |
|
1855 ascii_save_type (ostream& os, char *type, int mark_as_global) |
|
1856 { |
|
1857 if (mark_as_global) |
|
1858 os << "# type: global "; |
|
1859 else |
|
1860 os << "# type: "; |
|
1861 |
|
1862 os << type << "\n"; |
|
1863 } |
|
1864 |
872
|
1865 static Matrix |
|
1866 strip_infnan (const Matrix& m) |
|
1867 { |
|
1868 int nr = m.rows (); |
|
1869 int nc = m.columns (); |
|
1870 |
|
1871 Matrix retval (nr, nc); |
|
1872 |
|
1873 int k = 0; |
|
1874 for (int i = 0; i < nr; i++) |
|
1875 { |
|
1876 for (int j = 0; j < nc; j++) |
|
1877 { |
2305
|
1878 double d = m (i, j); |
872
|
1879 if (xisnan (d)) |
|
1880 goto next_row; |
|
1881 else |
2305
|
1882 retval (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
872
|
1883 } |
|
1884 k++; |
|
1885 |
|
1886 next_row: |
|
1887 continue; |
|
1888 } |
|
1889 |
|
1890 if (k > 0) |
|
1891 retval.resize (k, nc); |
|
1892 |
|
1893 return retval; |
|
1894 } |
|
1895 |
|
1896 static ComplexMatrix |
|
1897 strip_infnan (const ComplexMatrix& m) |
|
1898 { |
|
1899 int nr = m.rows (); |
|
1900 int nc = m.columns (); |
|
1901 |
|
1902 ComplexMatrix retval (nr, nc); |
|
1903 |
|
1904 int k = 0; |
|
1905 for (int i = 0; i < nr; i++) |
|
1906 { |
|
1907 for (int j = 0; j < nc; j++) |
|
1908 { |
2305
|
1909 Complex c = m (i, j); |
872
|
1910 if (xisnan (c)) |
|
1911 goto next_row; |
|
1912 else |
|
1913 { |
|
1914 double re = real (c); |
|
1915 double im = imag (c); |
|
1916 |
|
1917 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
1918 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
1919 |
2305
|
1920 retval (k, j) = Complex (re, im); |
872
|
1921 } |
|
1922 } |
|
1923 k++; |
|
1924 |
|
1925 next_row: |
|
1926 continue; |
|
1927 } |
|
1928 |
|
1929 if (k > 0) |
|
1930 retval.resize (k, nc); |
|
1931 |
|
1932 return retval; |
|
1933 } |
|
1934 |
620
|
1935 // Save the data from TC along with the corresponding NAME, and global |
604
|
1936 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
1755
|
1937 // above for load_ascii_data. If NAME is empty, the name: line is not |
604
|
1938 // generated. PRECISION specifies the number of decimal digits to print. |
872
|
1939 // If STRIP_NAN_AND_INF is nonzero, rows containing NaNs are deleted, |
|
1940 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
1941 // but not so big that gnuplot can't handle it when trying to compute |
|
1942 // axis ranges, etc.). |
|
1943 // |
|
1944 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
1945 // |
|
1946 // Returns 1 for success and 0 for failure. |
604
|
1947 |
|
1948 // XXX FIXME XXX -- should probably write the help string here too. |
|
1949 |
|
1950 int |
2086
|
1951 save_ascii_data (ostream& os, const octave_value& tc, |
1755
|
1952 const string& name, int strip_nan_and_inf, |
872
|
1953 int mark_as_global, int precision) |
604
|
1954 { |
872
|
1955 int success = 1; |
620
|
1956 |
604
|
1957 if (! precision) |
2194
|
1958 precision = Vsave_precision; |
604
|
1959 |
1755
|
1960 if (! name.empty ()) |
604
|
1961 os << "# name: " << name << "\n"; |
|
1962 |
|
1963 long old_precision = os.precision (); |
|
1964 os.precision (precision); |
|
1965 |
620
|
1966 if (tc.is_real_scalar ()) |
|
1967 { |
|
1968 ascii_save_type (os, "scalar", mark_as_global); |
872
|
1969 |
|
1970 double d = tc.double_value (); |
|
1971 if (strip_nan_and_inf) |
|
1972 { |
|
1973 if (xisnan (d)) |
|
1974 { |
|
1975 error ("only value to plot is NaN"); |
|
1976 success = 0; |
|
1977 } |
|
1978 else |
|
1979 { |
|
1980 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
1981 os << d << "\n"; |
|
1982 } |
|
1983 } |
|
1984 else |
|
1985 os << d << "\n"; |
620
|
1986 } |
|
1987 else if (tc.is_real_matrix ()) |
|
1988 { |
|
1989 ascii_save_type (os, "matrix", mark_as_global); |
|
1990 os << "# rows: " << tc.rows () << "\n" |
872
|
1991 << "# columns: " << tc.columns () << "\n"; |
|
1992 |
|
1993 Matrix tmp = tc.matrix_value (); |
|
1994 if (strip_nan_and_inf) |
|
1995 tmp = strip_infnan (tmp); |
|
1996 |
|
1997 os << tmp; |
620
|
1998 } |
|
1999 else if (tc.is_complex_scalar ()) |
|
2000 { |
|
2001 ascii_save_type (os, "complex scalar", mark_as_global); |
872
|
2002 |
|
2003 Complex c = tc.complex_value (); |
|
2004 if (strip_nan_and_inf) |
|
2005 { |
|
2006 if (xisnan (c)) |
|
2007 { |
|
2008 error ("only value to plot is NaN"); |
|
2009 success = 0; |
|
2010 } |
|
2011 else |
|
2012 { |
|
2013 double re = real (c); |
|
2014 double im = imag (c); |
|
2015 |
|
2016 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
2017 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
2018 |
|
2019 c = Complex (re, im); |
|
2020 |
|
2021 os << c << "\n"; |
|
2022 } |
|
2023 } |
|
2024 else |
|
2025 os << c << "\n"; |
620
|
2026 } |
|
2027 else if (tc.is_complex_matrix ()) |
604
|
2028 { |
620
|
2029 ascii_save_type (os, "complex matrix", mark_as_global); |
|
2030 os << "# rows: " << tc.rows () << "\n" |
875
|
2031 << "# columns: " << tc.columns () << "\n"; |
|
2032 |
|
2033 ComplexMatrix tmp = tc.complex_matrix_value (); |
872
|
2034 if (strip_nan_and_inf) |
|
2035 tmp = strip_infnan (tmp); |
|
2036 |
|
2037 os << tmp; |
620
|
2038 } |
|
2039 else if (tc.is_string ()) |
|
2040 { |
1427
|
2041 ascii_save_type (os, "string array", mark_as_global); |
1572
|
2042 charMatrix chm = tc.all_strings (); |
|
2043 int elements = chm.rows (); |
1427
|
2044 os << "# elements: " << elements << "\n"; |
|
2045 for (int i = 0; i < elements; i++) |
|
2046 { |
1572
|
2047 int len = chm.cols (); |
1427
|
2048 os << "# length: " << len << "\n"; |
1728
|
2049 string tstr = chm.row_as_string (i); |
1742
|
2050 const char *tmp = tstr.data (); |
1572
|
2051 os.write (tmp, len); |
1427
|
2052 os << "\n"; |
|
2053 } |
620
|
2054 } |
872
|
2055 else if (tc.is_range ()) |
620
|
2056 { |
|
2057 ascii_save_type (os, "range", mark_as_global); |
|
2058 Range tmp = tc.range_value (); |
|
2059 os << "# base, limit, increment\n" |
|
2060 << tmp.base () << " " |
|
2061 << tmp.limit () << " " |
|
2062 << tmp.inc () << "\n"; |
|
2063 } |
|
2064 else |
|
2065 { |
|
2066 gripe_wrong_type_arg ("save", tc); |
872
|
2067 success = 0; |
604
|
2068 } |
|
2069 |
|
2070 os.precision (old_precision); |
|
2071 |
872
|
2072 return (os && success); |
604
|
2073 } |
|
2074 |
|
2075 // Save the info from sr on stream os in the format specified by fmt. |
|
2076 |
|
2077 static void |
630
|
2078 do_save (ostream& os, symbol_record *sr, load_save_format fmt, |
|
2079 int save_as_floats) |
604
|
2080 { |
|
2081 if (! sr->is_variable ()) |
|
2082 { |
|
2083 error ("save: can only save variables, not functions"); |
|
2084 return; |
|
2085 } |
|
2086 |
1755
|
2087 string name = sr->name (); |
|
2088 string help = sr->help (); |
604
|
2089 int global = sr->is_linked_to_global (); |
2371
|
2090 tree_fvc *tmp = sr->def (); |
|
2091 octave_value tc = tmp->eval (0); |
604
|
2092 |
1755
|
2093 if (tc.is_undefined ()) |
604
|
2094 return; |
|
2095 |
|
2096 switch (fmt) |
|
2097 { |
|
2098 case LS_ASCII: |
872
|
2099 save_ascii_data (os, tc, name, 0, global); |
604
|
2100 break; |
|
2101 |
|
2102 case LS_BINARY: |
630
|
2103 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
2104 break; |
|
2105 |
667
|
2106 case LS_MAT_BINARY: |
|
2107 save_mat_binary_data (os, tc, name); |
|
2108 break; |
|
2109 |
604
|
2110 default: |
775
|
2111 gripe_unrecognized_data_fmt ("save"); |
604
|
2112 break; |
|
2113 } |
|
2114 } |
|
2115 |
|
2116 // Save variables with names matching PATTERN on stream OS in the |
|
2117 // format specified by FMT. If SAVE_BUILTINS is nonzero, also save |
|
2118 // builtin variables with names that match PATTERN. |
|
2119 |
|
2120 static int |
1755
|
2121 save_vars (ostream& os, const string& pattern, int save_builtins, |
630
|
2122 load_save_format fmt, int save_as_floats) |
604
|
2123 { |
|
2124 int count; |
|
2125 |
|
2126 symbol_record **vars = curr_sym_tab->glob |
|
2127 (count, pattern, symbol_def::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2128 |
|
2129 int saved = count; |
|
2130 |
|
2131 int i; |
|
2132 |
|
2133 for (i = 0; i < count; i++) |
620
|
2134 { |
630
|
2135 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2136 |
|
2137 if (error_state) |
|
2138 break; |
|
2139 } |
604
|
2140 |
|
2141 delete [] vars; |
|
2142 |
620
|
2143 if (! error_state && save_builtins) |
604
|
2144 { |
|
2145 symbol_record **vars = global_sym_tab->glob |
|
2146 (count, pattern, symbol_def::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2147 |
|
2148 saved += count; |
|
2149 |
|
2150 for (i = 0; i < count; i++) |
620
|
2151 { |
630
|
2152 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2153 |
|
2154 if (error_state) |
|
2155 break; |
|
2156 } |
604
|
2157 |
|
2158 delete [] vars; |
|
2159 } |
|
2160 |
|
2161 return saved; |
|
2162 } |
|
2163 |
|
2164 static load_save_format |
|
2165 get_default_save_format (void) |
|
2166 { |
|
2167 load_save_format retval = LS_ASCII; |
|
2168 |
2194
|
2169 string fmt = Vdefault_save_format; |
1755
|
2170 |
|
2171 if (fmt == "binary") |
604
|
2172 retval = LS_BINARY; |
1755
|
2173 else if (fmt == "mat-binary" || fmt =="mat_binary") |
911
|
2174 retval = LS_MAT_BINARY; |
604
|
2175 |
|
2176 return retval; |
|
2177 } |
|
2178 |
863
|
2179 static void |
2095
|
2180 write_binary_header (ostream& os, load_save_format format) |
863
|
2181 { |
|
2182 if (format == LS_BINARY) |
|
2183 { |
2318
|
2184 os << (oct_mach_info::words_big_endian () |
|
2185 ? "Octave-1-B" : "Octave-1-L"); |
863
|
2186 |
2318
|
2187 oct_mach_info::float_format flt_fmt = |
|
2188 oct_mach_info::native_float_format (); |
|
2189 |
|
2190 char tmp = (char) float_format_to_mopt_digit (flt_fmt); |
2095
|
2191 |
|
2192 os.write (&tmp, 1); |
863
|
2193 } |
|
2194 } |
|
2195 |
|
2196 static void |
1755
|
2197 save_vars (const string_vector& argv, int argv_idx, int argc, |
|
2198 ostream& os, int save_builtins, load_save_format fmt, |
|
2199 int save_as_floats) |
863
|
2200 { |
|
2201 write_binary_header (os, fmt); |
|
2202 |
1755
|
2203 if (argv_idx == argc) |
863
|
2204 { |
|
2205 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
|
2206 } |
|
2207 else |
|
2208 { |
1755
|
2209 for (int i = argv_idx; i < argc; i++) |
863
|
2210 { |
1755
|
2211 if (! save_vars (os, argv[i], save_builtins, fmt, save_as_floats)) |
863
|
2212 { |
1755
|
2213 warning ("save: no such variable `%s'", argv[i].c_str ()); |
863
|
2214 } |
|
2215 } |
|
2216 } |
|
2217 } |
|
2218 |
1380
|
2219 void |
|
2220 save_user_variables (void) |
|
2221 { |
|
2222 // XXX FIXME XXX -- should choose better file name? |
|
2223 |
|
2224 const char *fname = "octave-core"; |
|
2225 |
|
2226 message (0, "attempting to save variables to `%s'...", fname); |
|
2227 |
|
2228 load_save_format format = get_default_save_format (); |
|
2229 |
|
2230 unsigned mode = ios::out|ios::trunc; |
|
2231 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2232 mode |= ios::bin; |
|
2233 |
|
2234 ofstream file (fname, mode); |
|
2235 |
|
2236 if (file) |
|
2237 { |
1755
|
2238 save_vars (string_vector (), 0, 0, file, 0, format, 0); |
1380
|
2239 message (0, "save to `%s' complete", fname); |
|
2240 } |
|
2241 else |
|
2242 warning ("unable to open `%s' for writing...", fname); |
|
2243 } |
|
2244 |
1957
|
2245 DEFUN_TEXT (save, args, , |
910
|
2246 "save [-ascii] [-binary] [-float-binary] [-mat-binary] \n\ |
667
|
2247 [-save-builtins] file [pattern ...]\n\ |
604
|
2248 \n\ |
|
2249 save variables in a file") |
|
2250 { |
2086
|
2251 octave_value_list retval; |
604
|
2252 |
1755
|
2253 int argc = args.length () + 1; |
|
2254 |
1968
|
2255 string_vector argv = args.make_argv ("save"); |
1755
|
2256 |
|
2257 if (error_state) |
|
2258 return retval; |
604
|
2259 |
1358
|
2260 // Here is where we would get the default save format if it were |
|
2261 // stored in a user preference variable. |
604
|
2262 |
|
2263 int save_builtins = 0; |
|
2264 |
630
|
2265 int save_as_floats = 0; |
|
2266 |
604
|
2267 load_save_format format = get_default_save_format (); |
|
2268 |
1755
|
2269 int i; |
|
2270 for (i = 1; i < argc; i++) |
604
|
2271 { |
1755
|
2272 if (argv[i] == "-ascii" || argv[i] == "-a") |
604
|
2273 { |
|
2274 format = LS_ASCII; |
|
2275 } |
1755
|
2276 else if (argv[i] == "-binary" || argv[i] == "-b") |
604
|
2277 { |
|
2278 format = LS_BINARY; |
|
2279 } |
1755
|
2280 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
667
|
2281 { |
|
2282 format = LS_MAT_BINARY; |
|
2283 } |
1755
|
2284 else if (argv[i] == "-float-binary" || argv[i] == "-f") |
630
|
2285 { |
|
2286 format = LS_BINARY; |
|
2287 save_as_floats = 1; |
|
2288 } |
1755
|
2289 else if (argv[i] == "-save-builtins") |
604
|
2290 { |
|
2291 save_builtins = 1; |
|
2292 } |
|
2293 else |
|
2294 break; |
|
2295 } |
|
2296 |
2057
|
2297 if (i == argc) |
604
|
2298 { |
|
2299 print_usage ("save"); |
|
2300 return retval; |
|
2301 } |
|
2302 |
630
|
2303 if (save_as_floats && format == LS_ASCII) |
|
2304 { |
|
2305 error ("save: cannot specify both -ascii and -float-binary"); |
|
2306 return retval; |
|
2307 } |
|
2308 |
1755
|
2309 if (argv[i] == "-") |
604
|
2310 { |
1755
|
2311 i++; |
863
|
2312 |
1358
|
2313 // XXX FIXME XXX -- should things intended for the screen end up |
2086
|
2314 // in a octave_value (string)? |
863
|
2315 |
2095
|
2316 save_vars (argv, i, argc, octave_stdout, save_builtins, format, |
863
|
2317 save_as_floats); |
604
|
2318 } |
1755
|
2319 |
|
2320 // Guard against things like `save a*', which are probably mistakes... |
|
2321 |
|
2322 else if (i == argc - 1 && glob_pattern_p (argv[i])) |
|
2323 { |
|
2324 print_usage ("save"); |
604
|
2325 return retval; |
|
2326 } |
|
2327 else |
|
2328 { |
1755
|
2329 string fname = oct_tilde_expand (argv[i]); |
|
2330 |
|
2331 i++; |
604
|
2332 |
911
|
2333 unsigned mode = ios::out|ios::trunc; |
604
|
2334 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2335 mode |= ios::bin; |
|
2336 |
1750
|
2337 ofstream file (fname.c_str (), mode); |
863
|
2338 |
|
2339 if (file) |
|
2340 { |
1755
|
2341 save_vars (argv, i, argc, file, save_builtins, format, |
863
|
2342 save_as_floats); |
|
2343 } |
|
2344 else |
604
|
2345 { |
1755
|
2346 error ("save: couldn't open output file `%s'", fname.c_str ()); |
604
|
2347 return retval; |
|
2348 } |
|
2349 } |
|
2350 |
|
2351 return retval; |
|
2352 } |
|
2353 |
|
2354 // Maybe this should be a static function in tree-plot.cc? |
|
2355 |
620
|
2356 // If TC is matrix, save it on stream OS in a format useful for |
604
|
2357 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
2358 // nonzero, assume a parametric 3-dimensional plot will be generated. |
|
2359 |
|
2360 int |
2086
|
2361 save_three_d (ostream& os, const octave_value& tc, int parametric) |
604
|
2362 { |
620
|
2363 int fail = 0; |
604
|
2364 |
620
|
2365 int nr = tc.rows (); |
|
2366 int nc = tc.columns (); |
|
2367 |
|
2368 if (tc.is_real_matrix ()) |
604
|
2369 { |
|
2370 os << "# 3D data...\n" |
|
2371 << "# type: matrix\n" |
|
2372 << "# total rows: " << nr << "\n" |
|
2373 << "# total columns: " << nc << "\n"; |
|
2374 |
|
2375 if (parametric) |
|
2376 { |
|
2377 int extras = nc % 3; |
|
2378 if (extras) |
|
2379 warning ("ignoring last %d columns", extras); |
|
2380 |
620
|
2381 Matrix tmp = tc.matrix_value (); |
872
|
2382 tmp = strip_infnan (tmp); |
|
2383 nr = tmp.rows (); |
|
2384 |
604
|
2385 for (int i = 0; i < nc-extras; i += 3) |
|
2386 { |
|
2387 os << tmp.extract (0, i, nr-1, i+2); |
|
2388 if (i+3 < nc-extras) |
|
2389 os << "\n"; |
|
2390 } |
|
2391 } |
|
2392 else |
|
2393 { |
620
|
2394 Matrix tmp = tc.matrix_value (); |
872
|
2395 tmp = strip_infnan (tmp); |
|
2396 nr = tmp.rows (); |
|
2397 |
604
|
2398 for (int i = 0; i < nc; i++) |
|
2399 { |
|
2400 os << tmp.extract (0, i, nr-1, i); |
|
2401 if (i+1 < nc) |
|
2402 os << "\n"; |
|
2403 } |
|
2404 } |
620
|
2405 } |
|
2406 else |
|
2407 { |
604
|
2408 ::error ("for now, I can only save real matrices in 3D format"); |
620
|
2409 fail = 1; |
604
|
2410 } |
620
|
2411 |
|
2412 return (os && ! fail); |
604
|
2413 } |
|
2414 |
2194
|
2415 static int |
|
2416 default_save_format (void) |
|
2417 { |
|
2418 int status = 0; |
|
2419 |
|
2420 string s = builtin_string_variable ("default_save_format"); |
|
2421 |
|
2422 if (s.empty ()) |
|
2423 { |
|
2424 gripe_invalid_value_specified ("default_save_format"); |
|
2425 status = -1; |
|
2426 } |
|
2427 else |
|
2428 Vdefault_save_format = s; |
|
2429 |
|
2430 return status; |
|
2431 } |
|
2432 |
|
2433 static int |
|
2434 save_precision (void) |
|
2435 { |
|
2436 double val; |
|
2437 if (builtin_real_scalar_variable ("save_precision", val) |
|
2438 && ! xisnan (val)) |
|
2439 { |
|
2440 int ival = NINT (val); |
|
2441 if (ival >= 0 && (double) ival == val) |
|
2442 { |
|
2443 Vsave_precision = ival; |
|
2444 return 0; |
|
2445 } |
|
2446 } |
|
2447 gripe_invalid_value_specified ("save_precision"); |
|
2448 return -1; |
|
2449 } |
|
2450 |
|
2451 void |
|
2452 symbols_of_load_save (void) |
|
2453 { |
|
2454 DEFVAR (default_save_format, "ascii", 0, default_save_format, |
|
2455 "default format for files created with save, may be one of\n\ |
|
2456 \"binary\", \"text\", or \"mat-binary\""); |
|
2457 |
|
2458 DEFVAR (save_precision, 15.0, 0, save_precision, |
|
2459 "number of significant figures kept by the ASCII save command"); |
|
2460 } |
|
2461 |
604
|
2462 /* |
|
2463 ;;; Local Variables: *** |
|
2464 ;;; mode: C++ *** |
|
2465 ;;; End: *** |
|
2466 */ |