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