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