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