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 |
2511
|
1074 if (valid_identifier (varname.c_str ())) |
|
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; |
|
1099 if (is) |
|
1100 tmp.elem (i, j) = d; |
|
1101 else |
|
1102 goto done; |
|
1103 } |
|
1104 } |
|
1105 } |
|
1106 |
|
1107 done: |
2511
|
1108 |
|
1109 if (is) |
|
1110 { |
|
1111 tc = tmp; |
|
1112 |
|
1113 name = strsave (varname.c_str ()); |
|
1114 } |
|
1115 else |
|
1116 error ("load: failed to read matrix from file `%s'", |
|
1117 filename.c_str ()); |
|
1118 } |
|
1119 else |
|
1120 error ("load: unable to extract matrix size from file `%s'", |
|
1121 filename.c_str ()); |
|
1122 } |
|
1123 else |
|
1124 error ("load: unable to convert filename `%s' to valid identifier", |
|
1125 filename.c_str ()); |
|
1126 |
|
1127 return name; |
|
1128 } |
|
1129 |
604
|
1130 // Read LEN elements of data from IS in the format specified by |
3019
|
1131 // PRECISION, placing the result in DATA. If SWAP is TRUE, swap |
604
|
1132 // the bytes of each element before copying to DATA. FLT_FMT |
|
1133 // specifies the format of the data if we are reading floating point |
|
1134 // numbers. |
|
1135 |
|
1136 static void |
|
1137 read_mat_binary_data (istream& is, double *data, int precision, |
3019
|
1138 int len, bool swap, |
2318
|
1139 oct_mach_info::float_format flt_fmt) |
604
|
1140 { |
|
1141 switch (precision) |
|
1142 { |
|
1143 case 0: |
630
|
1144 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); |
604
|
1145 break; |
|
1146 |
|
1147 case 1: |
630
|
1148 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); |
604
|
1149 break; |
|
1150 |
|
1151 case 2: |
|
1152 read_doubles (is, data, LS_INT, len, swap, flt_fmt); |
|
1153 break; |
|
1154 |
|
1155 case 3: |
|
1156 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); |
|
1157 break; |
|
1158 |
|
1159 case 4: |
|
1160 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); |
|
1161 break; |
|
1162 |
|
1163 case 5: |
|
1164 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); |
|
1165 break; |
|
1166 |
|
1167 default: |
|
1168 break; |
|
1169 } |
|
1170 } |
|
1171 |
|
1172 static int |
3019
|
1173 read_mat_file_header (istream& is, bool& swap, FOUR_BYTE_INT& mopt, |
604
|
1174 FOUR_BYTE_INT& nr, FOUR_BYTE_INT& nc, |
|
1175 FOUR_BYTE_INT& imag, FOUR_BYTE_INT& len, |
|
1176 int quiet = 0) |
|
1177 { |
3019
|
1178 swap = false; |
671
|
1179 |
1358
|
1180 // We expect to fail here, at the beginning of a record, so not |
|
1181 // being able to read another mopt value should not result in an |
|
1182 // error. |
911
|
1183 |
604
|
1184 is.read (&mopt, 4); |
|
1185 if (! is) |
911
|
1186 return 1; |
604
|
1187 |
|
1188 if (! is.read (&nr, 4)) |
|
1189 goto data_read_error; |
|
1190 |
|
1191 if (! is.read (&nc, 4)) |
|
1192 goto data_read_error; |
|
1193 |
|
1194 if (! is.read (&imag, 4)) |
|
1195 goto data_read_error; |
|
1196 |
|
1197 if (! is.read (&len, 4)) |
|
1198 goto data_read_error; |
|
1199 |
|
1200 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
1201 // bigger than we expect, so we swap bytes. |
|
1202 // |
|
1203 // If mopt is zero, it means the file was written on a little endian |
|
1204 // machine, and we only need to swap if we are running on a big endian |
|
1205 // machine. |
|
1206 // |
|
1207 // Gag me. |
|
1208 |
2318
|
1209 if (oct_mach_info::words_big_endian () && mopt == 0) |
3019
|
1210 swap = true; |
604
|
1211 |
1358
|
1212 // mopt is signed, therefore byte swap may result in negative value. |
911
|
1213 |
|
1214 if (mopt > 9999 || mopt < 0) |
3019
|
1215 swap = true; |
604
|
1216 |
|
1217 if (swap) |
|
1218 { |
3145
|
1219 swap_4_bytes (X_CAST (char *, &mopt)); |
|
1220 swap_4_bytes (X_CAST (char *, &nr)); |
|
1221 swap_4_bytes (X_CAST (char *, &nc)); |
|
1222 swap_4_bytes (X_CAST (char *, &imag)); |
|
1223 swap_4_bytes (X_CAST (char *, &len)); |
604
|
1224 } |
|
1225 |
911
|
1226 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) |
604
|
1227 { |
|
1228 if (! quiet) |
|
1229 error ("load: can't read binary file"); |
|
1230 return -1; |
|
1231 } |
|
1232 |
|
1233 return 0; |
|
1234 |
|
1235 data_read_error: |
|
1236 return -1; |
|
1237 } |
|
1238 |
617
|
1239 // We don't just use a cast here, because we need to be able to detect |
|
1240 // possible errors. |
|
1241 |
2318
|
1242 static oct_mach_info::float_format |
|
1243 mopt_digit_to_float_format (int mach) |
617
|
1244 { |
2318
|
1245 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
619
|
1246 |
617
|
1247 switch (mach) |
|
1248 { |
|
1249 case 0: |
2318
|
1250 flt_fmt = oct_mach_info::ieee_little_endian; |
617
|
1251 break; |
|
1252 |
|
1253 case 1: |
2318
|
1254 flt_fmt = oct_mach_info::ieee_big_endian; |
617
|
1255 break; |
|
1256 |
|
1257 case 2: |
2318
|
1258 flt_fmt = oct_mach_info::vax_d; |
617
|
1259 break; |
|
1260 |
|
1261 case 3: |
2318
|
1262 flt_fmt = oct_mach_info::vax_g; |
617
|
1263 break; |
|
1264 |
|
1265 case 4: |
2318
|
1266 flt_fmt = oct_mach_info::cray; |
617
|
1267 break; |
|
1268 |
|
1269 default: |
2318
|
1270 flt_fmt = oct_mach_info::unknown; |
617
|
1271 break; |
|
1272 } |
619
|
1273 |
|
1274 return flt_fmt; |
617
|
1275 } |
619
|
1276 |
2318
|
1277 static int |
|
1278 float_format_to_mopt_digit (oct_mach_info::float_format flt_fmt) |
|
1279 { |
|
1280 int retval = -1; |
|
1281 |
|
1282 switch (flt_fmt) |
|
1283 { |
|
1284 case oct_mach_info::ieee_little_endian: |
|
1285 retval = 0; |
|
1286 break; |
|
1287 |
|
1288 case oct_mach_info::ieee_big_endian: |
|
1289 retval = 1; |
|
1290 break; |
|
1291 |
|
1292 case oct_mach_info::vax_d: |
|
1293 retval = 2; |
|
1294 break; |
|
1295 |
|
1296 case oct_mach_info::vax_g: |
|
1297 retval = 3; |
|
1298 break; |
|
1299 |
|
1300 case oct_mach_info::cray: |
|
1301 retval = 4; |
|
1302 break; |
|
1303 |
|
1304 default: |
|
1305 break; |
|
1306 } |
|
1307 |
|
1308 return retval; |
|
1309 } |
|
1310 |
604
|
1311 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
1312 // place it in TC, returning the name of the variable. |
|
1313 // |
|
1314 // The data is expected to be in Matlab's .mat format, though not all |
|
1315 // the features of that format are supported. |
|
1316 // |
|
1317 // FILENAME is used for error messages. |
|
1318 // |
|
1319 // This format provides no way to tag the data as global. |
|
1320 |
|
1321 static char * |
1755
|
1322 read_mat_binary_data (istream& is, const string& filename, |
2086
|
1323 octave_value& tc) |
604
|
1324 { |
1358
|
1325 // These are initialized here instead of closer to where they are |
|
1326 // first used to avoid errors from gcc about goto crossing |
|
1327 // initialization of variable. |
604
|
1328 |
|
1329 Matrix re; |
2318
|
1330 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
604
|
1331 char *name = 0; |
3019
|
1332 bool swap = false; |
|
1333 int type = 0; |
|
1334 int prec = 0; |
3136
|
1335 int order = 0; |
3019
|
1336 int mach = 0; |
|
1337 int dlen = 0; |
604
|
1338 |
|
1339 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
1340 |
|
1341 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
1342 if (err) |
|
1343 { |
|
1344 if (err < 0) |
|
1345 goto data_read_error; |
|
1346 else |
|
1347 return 0; |
|
1348 } |
|
1349 |
3136
|
1350 type = mopt % 10; // Full, sparse, etc. |
|
1351 mopt /= 10; // Eliminate first digit. |
|
1352 prec = mopt % 10; // double, float, int, etc. |
|
1353 mopt /= 10; // Eliminate second digit. |
|
1354 order = mopt % 10; // Row or column major ordering. |
|
1355 mopt /= 10; // Eliminate third digit. |
|
1356 mach = mopt % 10; // IEEE, VAX, etc. |
604
|
1357 |
2318
|
1358 flt_fmt = mopt_digit_to_float_format (mach); |
|
1359 |
|
1360 if (flt_fmt == oct_mach_info::unknown) |
604
|
1361 { |
|
1362 error ("load: unrecognized binary format!"); |
|
1363 return 0; |
|
1364 } |
|
1365 |
|
1366 if (type != 0 && type != 1) |
|
1367 { |
|
1368 error ("load: can't read sparse matrices"); |
|
1369 return 0; |
|
1370 } |
|
1371 |
|
1372 if (imag && type == 1) |
|
1373 { |
|
1374 error ("load: encountered complex matrix with string flag set!"); |
|
1375 return 0; |
|
1376 } |
|
1377 |
2345
|
1378 // LEN includes the terminating character, and the file is also |
2436
|
1379 // supposed to include it, but apparently not all files do. Either |
|
1380 // way, I think this should work. |
2345
|
1381 |
2436
|
1382 name = new char [len+1]; |
604
|
1383 if (! is.read (name, len)) |
|
1384 goto data_read_error; |
2436
|
1385 name[len] = '\0'; |
604
|
1386 |
|
1387 dlen = nr * nc; |
|
1388 if (dlen < 0) |
|
1389 goto data_read_error; |
|
1390 |
3136
|
1391 if (order) |
|
1392 { |
|
1393 int tmp = nr; |
|
1394 nr = nc; |
|
1395 nc = tmp; |
|
1396 } |
|
1397 |
604
|
1398 re.resize (nr, nc); |
|
1399 |
|
1400 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1401 |
|
1402 if (! is || error_state) |
|
1403 { |
|
1404 error ("load: reading matrix data for `%s'", name); |
|
1405 goto data_read_error; |
|
1406 } |
|
1407 |
|
1408 if (imag) |
|
1409 { |
|
1410 Matrix im (nr, nc); |
|
1411 |
|
1412 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1413 |
|
1414 if (! is || error_state) |
|
1415 { |
|
1416 error ("load: reading imaginary matrix data for `%s'", name); |
|
1417 goto data_read_error; |
|
1418 } |
|
1419 |
|
1420 ComplexMatrix ctmp (nr, nc); |
|
1421 |
|
1422 for (int j = 0; j < nc; j++) |
|
1423 for (int i = 0; i < nr; i++) |
2305
|
1424 ctmp (i, j) = Complex (re (i, j), im (i, j)); |
604
|
1425 |
3136
|
1426 tc = order ? ctmp.transpose () : ctmp; |
604
|
1427 } |
|
1428 else |
3136
|
1429 tc = order ? re.transpose () : re; |
604
|
1430 |
1427
|
1431 if (type == 1) |
604
|
1432 tc = tc.convert_to_str (); |
|
1433 |
|
1434 return name; |
|
1435 |
|
1436 data_read_error: |
1755
|
1437 error ("load: trouble reading binary file `%s'", filename.c_str ()); |
604
|
1438 delete [] name; |
|
1439 return 0; |
|
1440 } |
|
1441 |
3019
|
1442 // Return TRUE if NAME matches one of the given globbing PATTERNS. |
604
|
1443 |
3013
|
1444 static bool |
1755
|
1445 matches_patterns (const string_vector& patterns, int pat_idx, |
1792
|
1446 int num_pat, const string& name) |
604
|
1447 { |
1755
|
1448 for (int i = pat_idx; i < num_pat; i++) |
604
|
1449 { |
1792
|
1450 glob_match pattern (patterns[i]); |
3013
|
1451 |
1792
|
1452 if (pattern.match (name)) |
3013
|
1453 return true; |
604
|
1454 } |
3013
|
1455 return false; |
604
|
1456 } |
|
1457 |
|
1458 static int |
3019
|
1459 read_binary_file_header (istream& is, bool& swap, |
2318
|
1460 oct_mach_info::float_format& flt_fmt, |
3019
|
1461 bool quiet = false) |
604
|
1462 { |
|
1463 int magic_len = 10; |
|
1464 char magic [magic_len+1]; |
|
1465 is.read (magic, magic_len); |
|
1466 magic[magic_len] = '\0'; |
|
1467 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
2318
|
1468 swap = oct_mach_info::words_big_endian (); |
604
|
1469 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
2318
|
1470 swap = ! oct_mach_info::words_big_endian (); |
604
|
1471 else |
|
1472 { |
|
1473 if (! quiet) |
|
1474 error ("load: can't read binary file"); |
|
1475 return -1; |
|
1476 } |
|
1477 |
|
1478 char tmp = 0; |
|
1479 is.read (&tmp, 1); |
|
1480 |
2318
|
1481 flt_fmt = mopt_digit_to_float_format (tmp); |
|
1482 |
|
1483 if (flt_fmt == oct_mach_info::unknown) |
604
|
1484 { |
|
1485 if (! quiet) |
|
1486 error ("load: unrecognized binary format!"); |
|
1487 return -1; |
|
1488 } |
|
1489 |
|
1490 return 0; |
|
1491 } |
|
1492 |
|
1493 static load_save_format |
1750
|
1494 get_file_format (const string& fname, const string& orig_fname) |
604
|
1495 { |
|
1496 load_save_format retval = LS_UNKNOWN; |
|
1497 |
1750
|
1498 ifstream file (fname.c_str ()); |
604
|
1499 |
|
1500 if (! file) |
|
1501 { |
1750
|
1502 error ("load: couldn't open input file `%s'", orig_fname.c_str ()); |
604
|
1503 return retval; |
|
1504 } |
|
1505 |
2318
|
1506 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
604
|
1507 |
3019
|
1508 bool swap = false; |
|
1509 |
|
1510 if (read_binary_file_header (file, swap, flt_fmt, true) == 0) |
604
|
1511 retval = LS_BINARY; |
|
1512 else |
|
1513 { |
|
1514 file.seekg (0, ios::beg); |
|
1515 |
|
1516 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
1180
|
1517 |
|
1518 int err = read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1); |
|
1519 |
|
1520 if (! err) |
604
|
1521 retval = LS_MAT_BINARY; |
|
1522 else |
|
1523 { |
2511
|
1524 file.clear (); |
604
|
1525 file.seekg (0, ios::beg); |
|
1526 |
|
1527 char *tmp = extract_keyword (file, "name"); |
1180
|
1528 |
604
|
1529 if (tmp) |
1180
|
1530 { |
|
1531 retval = LS_ASCII; |
2511
|
1532 |
1180
|
1533 delete [] tmp; |
|
1534 } |
2511
|
1535 else |
|
1536 { |
|
1537 // Try reading the file as numbers only, determining the |
|
1538 // number of rows and columns from the data. We don't |
|
1539 // even bother to check to see if the first item in the |
|
1540 // file is a number, so that get_complete_line() can |
|
1541 // skip any comments that might appear at the top of the |
|
1542 // file. |
|
1543 |
|
1544 retval = LS_MAT_ASCII; |
|
1545 } |
604
|
1546 } |
|
1547 } |
|
1548 |
|
1549 file.close (); |
|
1550 |
|
1551 if (retval == LS_UNKNOWN) |
1750
|
1552 error ("load: unable to determine file format for `%s'", |
|
1553 orig_fname.c_str ()); |
604
|
1554 |
|
1555 return retval; |
|
1556 } |
|
1557 |
2086
|
1558 static octave_value_list |
3019
|
1559 do_load (istream& stream, const string& orig_fname, bool force, |
2318
|
1560 load_save_format format, oct_mach_info::float_format flt_fmt, |
3019
|
1561 bool list_only, bool swap, bool verbose, const string_vector& argv, |
1755
|
1562 int argv_idx, int argc, int nargout) |
604
|
1563 { |
2086
|
1564 octave_value_list retval; |
604
|
1565 |
621
|
1566 ostrstream output_buf; |
604
|
1567 int count = 0; |
|
1568 for (;;) |
|
1569 { |
3019
|
1570 bool global = false; |
2086
|
1571 octave_value tc; |
604
|
1572 |
|
1573 char *name = 0; |
|
1574 char *doc = 0; |
|
1575 |
|
1576 switch (format) |
|
1577 { |
|
1578 case LS_ASCII: |
3136
|
1579 name = read_ascii_data (stream, orig_fname, global, tc, count); |
604
|
1580 break; |
|
1581 |
|
1582 case LS_BINARY: |
|
1583 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
1584 global, tc, doc); |
|
1585 break; |
|
1586 |
2511
|
1587 case LS_MAT_ASCII: |
|
1588 name = read_mat_ascii_data (stream, orig_fname, tc); |
|
1589 break; |
|
1590 |
604
|
1591 case LS_MAT_BINARY: |
|
1592 name = read_mat_binary_data (stream, orig_fname, tc); |
|
1593 break; |
|
1594 |
|
1595 default: |
775
|
1596 gripe_unrecognized_data_fmt ("load"); |
604
|
1597 break; |
|
1598 } |
|
1599 |
867
|
1600 if (error_state || stream.eof () || ! name) |
604
|
1601 { |
867
|
1602 delete [] name; |
|
1603 delete [] doc; |
2511
|
1604 |
604
|
1605 break; |
|
1606 } |
|
1607 else if (! error_state && name) |
|
1608 { |
|
1609 if (tc.is_defined ()) |
|
1610 { |
3136
|
1611 if (format == LS_MAT_ASCII && argv_idx < argc) |
|
1612 warning ("load: loaded ASCII file `%s' -- ignoring extra args", |
|
1613 orig_fname.c_str()); |
|
1614 |
|
1615 if (format == LS_MAT_ASCII |
|
1616 || argv_idx == argc |
1755
|
1617 || matches_patterns (argv, argv_idx, argc, name)) |
604
|
1618 { |
|
1619 count++; |
621
|
1620 if (list_only) |
|
1621 { |
|
1622 if (verbose) |
|
1623 { |
|
1624 if (count == 1) |
|
1625 output_buf |
|
1626 << "type rows cols name\n" |
|
1627 << "==== ==== ==== ====\n"; |
|
1628 |
3013
|
1629 output_buf |
|
1630 << setiosflags (ios::left) |
|
1631 << setw (16) << tc.type_name () . c_str () |
|
1632 << setiosflags (ios::right) |
|
1633 << setw (7) << tc.rows () |
|
1634 << setw (7) << tc.columns () |
|
1635 << " "; |
621
|
1636 } |
|
1637 output_buf << name << "\n"; |
|
1638 } |
|
1639 else |
|
1640 { |
|
1641 install_loaded_variable (force, name, tc, global, doc); |
|
1642 } |
604
|
1643 } |
2511
|
1644 |
|
1645 delete [] name; |
|
1646 delete [] doc; |
|
1647 |
|
1648 // Only attempt to read one item from a headless text file. |
|
1649 |
|
1650 if (format == LS_MAT_ASCII) |
|
1651 break; |
604
|
1652 } |
|
1653 else |
|
1654 error ("load: unable to load variable `%s'", name); |
|
1655 } |
|
1656 else |
|
1657 { |
|
1658 if (count == 0) |
|
1659 error ("load: are you sure `%s' is an Octave data file?", |
1755
|
1660 orig_fname.c_str ()); |
604
|
1661 |
867
|
1662 delete [] name; |
|
1663 delete [] doc; |
2511
|
1664 |
604
|
1665 break; |
|
1666 } |
|
1667 } |
|
1668 |
621
|
1669 if (list_only && count) |
|
1670 { |
2095
|
1671 output_buf << ends; |
|
1672 |
|
1673 char *msg = output_buf.str (); |
|
1674 |
621
|
1675 if (nargout > 0) |
2095
|
1676 retval = msg; |
621
|
1677 else |
2095
|
1678 octave_stdout << msg; |
|
1679 |
|
1680 delete [] msg; |
621
|
1681 } |
|
1682 |
863
|
1683 return retval; |
|
1684 } |
|
1685 |
1957
|
1686 DEFUN_TEXT (load, args, nargout, |
910
|
1687 "load [-force] [-ascii] [-binary] [-mat-binary] file [pattern ...]\n\ |
863
|
1688 \n\ |
|
1689 Load variables from a file.\n\ |
|
1690 \n\ |
2802
|
1691 If no argument is supplied to select a format, load tries to read the\n\ |
|
1692 named file as an Octave binary, then as a .mat file, and then as an\n\ |
863
|
1693 Octave text file.\n\ |
|
1694 \n\ |
2802
|
1695 If the option -force is given, variables with the same names as those\n\ |
863
|
1696 found in the file will be replaced with the values read from the file.") |
|
1697 { |
2086
|
1698 octave_value_list retval; |
863
|
1699 |
1755
|
1700 int argc = args.length () + 1; |
|
1701 |
1968
|
1702 string_vector argv = args.make_argv ("load"); |
1755
|
1703 |
|
1704 if (error_state) |
|
1705 return retval; |
863
|
1706 |
1358
|
1707 // It isn't necessary to have the default load format stored in a |
|
1708 // user preference variable since we can determine the type of file |
|
1709 // as we are reading. |
863
|
1710 |
|
1711 load_save_format format = LS_UNKNOWN; |
|
1712 |
3019
|
1713 bool force = false; |
|
1714 bool list_only = false; |
|
1715 bool verbose = false; |
863
|
1716 |
1755
|
1717 int i; |
|
1718 for (i = 1; i < argc; i++) |
863
|
1719 { |
1755
|
1720 if (argv[i] == "-force" || argv[i] == "-f") |
863
|
1721 { |
3019
|
1722 force = true; |
863
|
1723 } |
1755
|
1724 else if (argv[i] == "-list" || argv[i] == "-l") |
863
|
1725 { |
3019
|
1726 list_only = true; |
863
|
1727 } |
1755
|
1728 else if (argv[i] == "-verbose" || argv[i] == "-v") |
863
|
1729 { |
3019
|
1730 verbose = true; |
863
|
1731 } |
1755
|
1732 else if (argv[i] == "-ascii" || argv[i] == "-a") |
863
|
1733 { |
|
1734 format = LS_ASCII; |
|
1735 } |
1755
|
1736 else if (argv[i] == "-binary" || argv[i] == "-b") |
863
|
1737 { |
|
1738 format = LS_BINARY; |
|
1739 } |
1755
|
1740 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
863
|
1741 { |
|
1742 format = LS_MAT_BINARY; |
|
1743 } |
|
1744 else |
|
1745 break; |
|
1746 } |
|
1747 |
1755
|
1748 if (i == argc) |
863
|
1749 { |
2057
|
1750 print_usage ("load"); |
863
|
1751 return retval; |
|
1752 } |
|
1753 |
1755
|
1754 string orig_fname = argv[i]; |
863
|
1755 |
2318
|
1756 oct_mach_info::float_format flt_fmt = oct_mach_info::unknown; |
863
|
1757 |
3019
|
1758 bool swap = false; |
863
|
1759 |
1755
|
1760 if (argv[i] == "-") |
863
|
1761 { |
1755
|
1762 i++; |
863
|
1763 |
|
1764 if (format != LS_UNKNOWN) |
|
1765 { |
1358
|
1766 // XXX FIXME XXX -- if we have already seen EOF on a |
|
1767 // previous call, how do we fix up the state of cin so that |
|
1768 // we can get additional input? I'm afraid that we can't |
|
1769 // fix this using cin only. |
863
|
1770 |
|
1771 retval = do_load (cin, orig_fname, force, format, flt_fmt, |
1755
|
1772 list_only, swap, verbose, argv, i, argc, |
863
|
1773 nargout); |
|
1774 } |
|
1775 else |
|
1776 error ("load: must specify file format if reading from stdin"); |
|
1777 } |
|
1778 else |
|
1779 { |
2926
|
1780 string fname = file_ops::tilde_expand (argv[i]); |
863
|
1781 |
|
1782 if (format == LS_UNKNOWN) |
|
1783 format = get_file_format (fname, orig_fname); |
|
1784 |
|
1785 if (format != LS_UNKNOWN) |
|
1786 { |
1755
|
1787 i++; |
863
|
1788 |
|
1789 unsigned mode = ios::in; |
|
1790 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
1791 mode |= ios::bin; |
|
1792 |
1750
|
1793 ifstream file (fname.c_str (), mode); |
863
|
1794 |
|
1795 if (file) |
|
1796 { |
|
1797 if (format == LS_BINARY) |
|
1798 { |
|
1799 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
1800 { |
|
1801 file.close (); |
|
1802 return retval; |
|
1803 } |
|
1804 } |
|
1805 |
|
1806 retval = do_load (file, orig_fname, force, format, |
|
1807 flt_fmt, list_only, swap, verbose, |
1755
|
1808 argv, i, argc, nargout); |
863
|
1809 |
|
1810 file.close (); |
|
1811 } |
|
1812 else |
1755
|
1813 error ("load: couldn't open input file `%s'", |
|
1814 orig_fname.c_str ()); |
863
|
1815 } |
|
1816 } |
604
|
1817 |
|
1818 return retval; |
|
1819 } |
|
1820 |
3019
|
1821 // Return TRUE if PATTERN has any special globbing chars in it. |
|
1822 |
|
1823 static bool |
1755
|
1824 glob_pattern_p (const string& pattern) |
604
|
1825 { |
|
1826 int open = 0; |
|
1827 |
1755
|
1828 int len = pattern.length (); |
|
1829 |
|
1830 for (int i = 0; i < len; i++) |
604
|
1831 { |
1755
|
1832 char c = pattern[i]; |
|
1833 |
604
|
1834 switch (c) |
|
1835 { |
|
1836 case '?': |
|
1837 case '*': |
3019
|
1838 return true; |
604
|
1839 |
|
1840 case '[': // Only accept an open brace if there is a close |
|
1841 open++; // brace to match it. Bracket expressions must be |
|
1842 continue; // complete, according to Posix.2 |
|
1843 |
|
1844 case ']': |
|
1845 if (open) |
3019
|
1846 return true; |
604
|
1847 continue; |
|
1848 |
|
1849 case '\\': |
1755
|
1850 if (i == len - 1) |
3019
|
1851 return false; |
604
|
1852 |
|
1853 default: |
|
1854 continue; |
|
1855 } |
|
1856 } |
|
1857 |
3019
|
1858 return false; |
604
|
1859 } |
|
1860 |
618
|
1861 // MAX_VAL and MIN_VAL are assumed to have integral values even though |
|
1862 // they are stored in doubles. |
|
1863 |
604
|
1864 static save_type |
|
1865 get_save_type (double max_val, double min_val) |
|
1866 { |
|
1867 save_type st = LS_DOUBLE; |
|
1868 |
|
1869 if (max_val < 256 && min_val > -1) |
|
1870 st = LS_U_CHAR; |
|
1871 else if (max_val < 65536 && min_val > -1) |
|
1872 st = LS_U_SHORT; |
3131
|
1873 else if (max_val < 4294967295UL && min_val > -1) |
618
|
1874 st = LS_U_INT; |
|
1875 else if (max_val < 128 && min_val >= -128) |
|
1876 st = LS_CHAR; |
604
|
1877 else if (max_val < 32768 && min_val >= -32768) |
|
1878 st = LS_SHORT; |
3131
|
1879 else if (max_val <= 2147483647L && min_val >= -2147483647L) |
604
|
1880 st = LS_INT; |
|
1881 |
|
1882 return st; |
|
1883 } |
|
1884 |
|
1885 // Save the data from TC along with the corresponding NAME, help |
|
1886 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
1427
|
1887 // binary format described above for read_binary_data. |
604
|
1888 |
2799
|
1889 static bool |
2086
|
1890 save_binary_data (ostream& os, const octave_value& tc, |
1755
|
1891 const string& name, const string& doc, |
3019
|
1892 bool mark_as_global, bool save_as_floats) |
604
|
1893 { |
1755
|
1894 FOUR_BYTE_INT name_len = name.length (); |
604
|
1895 |
|
1896 os.write (&name_len, 4); |
1755
|
1897 os << name; |
|
1898 |
|
1899 FOUR_BYTE_INT doc_len = doc.length (); |
604
|
1900 |
|
1901 os.write (&doc_len, 4); |
1755
|
1902 os << doc; |
604
|
1903 |
|
1904 char tmp; |
|
1905 |
|
1906 tmp = mark_as_global; |
|
1907 os.write (&tmp, 1); |
|
1908 |
3233
|
1909 if (tc.is_string ()) |
|
1910 { |
|
1911 tmp = 7; |
|
1912 os.write (&tmp, 1); |
|
1913 FOUR_BYTE_INT nr = tc.rows (); |
|
1914 os.write (&nr, 4); |
|
1915 charMatrix chm = tc.char_matrix_value (); |
|
1916 for (int i = 0; i < nr; i++) |
|
1917 { |
|
1918 FOUR_BYTE_INT len = chm.cols (); |
|
1919 os.write (&len, 4); |
|
1920 string tstr = chm.row_as_string (i); |
|
1921 const char *tmp = tstr.data (); |
|
1922 os.write (tmp, len); |
|
1923 } |
|
1924 } |
|
1925 else if (tc.is_range ()) |
|
1926 { |
|
1927 tmp = 6; |
|
1928 os.write (&tmp, 1); |
|
1929 tmp = (char) LS_DOUBLE; |
|
1930 os.write (&tmp, 1); |
|
1931 Range r = tc.range_value (); |
|
1932 double bas = r.base (); |
|
1933 double lim = r.limit (); |
|
1934 double inc = r.inc (); |
|
1935 os.write (&bas, 8); |
|
1936 os.write (&lim, 8); |
|
1937 os.write (&inc, 8); |
|
1938 } |
|
1939 else if (tc.is_real_scalar ()) |
604
|
1940 { |
|
1941 tmp = 1; |
|
1942 os.write (&tmp, 1); |
630
|
1943 tmp = (char) LS_DOUBLE; |
|
1944 os.write (&tmp, 1); |
604
|
1945 double tmp = tc.double_value (); |
|
1946 os.write (&tmp, 8); |
|
1947 } |
620
|
1948 else if (tc.is_real_matrix ()) |
604
|
1949 { |
|
1950 tmp = 2; |
|
1951 os.write (&tmp, 1); |
|
1952 Matrix m = tc.matrix_value (); |
|
1953 FOUR_BYTE_INT nr = m.rows (); |
|
1954 FOUR_BYTE_INT nc = m.columns (); |
|
1955 os.write (&nr, 4); |
|
1956 os.write (&nc, 4); |
|
1957 int len = nr * nc; |
|
1958 save_type st = LS_DOUBLE; |
630
|
1959 if (save_as_floats) |
|
1960 { |
1963
|
1961 if (m.too_large_for_float ()) |
630
|
1962 { |
|
1963 warning ("save: some values too large to save as floats --"); |
|
1964 warning ("save: saving as doubles instead"); |
|
1965 } |
|
1966 else |
|
1967 st = LS_FLOAT; |
|
1968 } |
|
1969 else if (len > 8192) // XXX FIXME XXX -- make this configurable. |
604
|
1970 { |
|
1971 double max_val, min_val; |
1963
|
1972 if (m.all_integers (max_val, min_val)) |
604
|
1973 st = get_save_type (max_val, min_val); |
|
1974 } |
630
|
1975 const double *mtmp = m.data (); |
604
|
1976 write_doubles (os, mtmp, st, len); |
|
1977 } |
|
1978 else if (tc.is_complex_scalar ()) |
|
1979 { |
|
1980 tmp = 3; |
|
1981 os.write (&tmp, 1); |
630
|
1982 tmp = (char) LS_DOUBLE; |
|
1983 os.write (&tmp, 1); |
604
|
1984 Complex tmp = tc.complex_value (); |
|
1985 os.write (&tmp, 16); |
|
1986 } |
|
1987 else if (tc.is_complex_matrix ()) |
|
1988 { |
|
1989 tmp = 4; |
|
1990 os.write (&tmp, 1); |
|
1991 ComplexMatrix m = tc.complex_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 > 4096) // 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 Complex *mtmp = m.data (); |
3145
|
2015 write_doubles (os, X_CAST (const double *, mtmp), st, 2*len); |
604
|
2016 } |
|
2017 else |
2799
|
2018 gripe_wrong_type_arg ("save", tc, false); |
|
2019 |
|
2020 return os; |
604
|
2021 } |
|
2022 |
667
|
2023 // Save the data from TC along with the corresponding NAME on stream OS |
|
2024 // in the MatLab binary format. |
|
2025 |
2799
|
2026 static bool |
2086
|
2027 save_mat_binary_data (ostream& os, const octave_value& tc, |
1755
|
2028 const string& name) |
667
|
2029 { |
|
2030 FOUR_BYTE_INT mopt = 0; |
|
2031 |
|
2032 mopt += tc.is_string () ? 1 : 0; |
2318
|
2033 |
|
2034 oct_mach_info::float_format flt_fmt = |
|
2035 oct_mach_info::native_float_format ();; |
|
2036 |
|
2037 mopt += 1000 * float_format_to_mopt_digit (flt_fmt); |
667
|
2038 |
|
2039 os.write (&mopt, 4); |
|
2040 |
|
2041 FOUR_BYTE_INT nr = tc.rows (); |
|
2042 os.write (&nr, 4); |
|
2043 |
|
2044 FOUR_BYTE_INT nc = tc.columns (); |
|
2045 os.write (&nc, 4); |
|
2046 |
|
2047 int len = nr * nc; |
|
2048 |
|
2049 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; |
|
2050 os.write (&imag, 4); |
|
2051 |
2345
|
2052 // LEN includes the terminating character, and the file is also |
|
2053 // supposed to include it. |
|
2054 |
|
2055 FOUR_BYTE_INT name_len = name.length () + 1; |
667
|
2056 |
|
2057 os.write (&name_len, 4); |
2345
|
2058 os << name << '\0'; |
667
|
2059 |
3233
|
2060 if (tc.is_string ()) |
|
2061 { |
|
2062 unwind_protect::begin_frame ("save_mat_binary_data"); |
|
2063 unwind_protect_int (Vimplicit_str_to_num_ok); |
|
2064 Vimplicit_str_to_num_ok = true; |
|
2065 Matrix m = tc.matrix_value (); |
|
2066 os.write (m.data (), 8 * len); |
|
2067 unwind_protect::run_frame ("save_mat_binary_data"); |
|
2068 } |
|
2069 else if (tc.is_range ()) |
|
2070 { |
|
2071 Range r = tc.range_value (); |
|
2072 double base = r.base (); |
|
2073 double inc = r.inc (); |
|
2074 int nel = r.nelem (); |
|
2075 for (int i = 0; i < nel; i++) |
|
2076 { |
|
2077 double x = base + i * inc; |
|
2078 os.write (&x, 8); |
|
2079 } |
|
2080 } |
|
2081 else if (tc.is_real_scalar ()) |
667
|
2082 { |
|
2083 double tmp = tc.double_value (); |
|
2084 os.write (&tmp, 8); |
|
2085 } |
911
|
2086 else if (tc.is_real_matrix ()) |
667
|
2087 { |
|
2088 Matrix m = tc.matrix_value (); |
|
2089 os.write (m.data (), 8 * len); |
|
2090 } |
|
2091 else if (tc.is_complex_scalar ()) |
|
2092 { |
|
2093 Complex tmp = tc.complex_value (); |
|
2094 os.write (&tmp, 16); |
|
2095 } |
|
2096 else if (tc.is_complex_matrix ()) |
|
2097 { |
|
2098 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
2099 Matrix m = ::real(m_cmplx); |
|
2100 os.write (m.data (), 8 * len); |
|
2101 m = ::imag(m_cmplx); |
|
2102 os.write (m.data (), 8 * len); |
|
2103 } |
|
2104 else |
2799
|
2105 gripe_wrong_type_arg ("save", tc, false); |
|
2106 |
|
2107 return os; |
667
|
2108 } |
|
2109 |
620
|
2110 static void |
2804
|
2111 ascii_save_type (ostream& os, const char *type, bool mark_as_global) |
620
|
2112 { |
|
2113 if (mark_as_global) |
|
2114 os << "# type: global "; |
|
2115 else |
|
2116 os << "# type: "; |
|
2117 |
|
2118 os << type << "\n"; |
|
2119 } |
|
2120 |
872
|
2121 static Matrix |
|
2122 strip_infnan (const Matrix& m) |
|
2123 { |
|
2124 int nr = m.rows (); |
|
2125 int nc = m.columns (); |
|
2126 |
|
2127 Matrix retval (nr, nc); |
|
2128 |
|
2129 int k = 0; |
|
2130 for (int i = 0; i < nr; i++) |
|
2131 { |
|
2132 for (int j = 0; j < nc; j++) |
|
2133 { |
2305
|
2134 double d = m (i, j); |
872
|
2135 if (xisnan (d)) |
|
2136 goto next_row; |
|
2137 else |
2305
|
2138 retval (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
872
|
2139 } |
|
2140 k++; |
|
2141 |
|
2142 next_row: |
|
2143 continue; |
|
2144 } |
|
2145 |
|
2146 if (k > 0) |
|
2147 retval.resize (k, nc); |
|
2148 |
|
2149 return retval; |
|
2150 } |
|
2151 |
|
2152 static ComplexMatrix |
|
2153 strip_infnan (const ComplexMatrix& m) |
|
2154 { |
|
2155 int nr = m.rows (); |
|
2156 int nc = m.columns (); |
|
2157 |
|
2158 ComplexMatrix retval (nr, nc); |
|
2159 |
|
2160 int k = 0; |
|
2161 for (int i = 0; i < nr; i++) |
|
2162 { |
|
2163 for (int j = 0; j < nc; j++) |
|
2164 { |
2305
|
2165 Complex c = m (i, j); |
872
|
2166 if (xisnan (c)) |
|
2167 goto next_row; |
|
2168 else |
|
2169 { |
|
2170 double re = real (c); |
|
2171 double im = imag (c); |
|
2172 |
|
2173 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
2174 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
2175 |
2305
|
2176 retval (k, j) = Complex (re, im); |
872
|
2177 } |
|
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 |
620
|
2191 // Save the data from TC along with the corresponding NAME, and global |
604
|
2192 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
1755
|
2193 // above for load_ascii_data. If NAME is empty, the name: line is not |
604
|
2194 // generated. PRECISION specifies the number of decimal digits to print. |
3019
|
2195 // If STRIP_NAN_AND_INF is TRUE, rows containing NaNs are deleted, |
872
|
2196 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
2197 // but not so big that gnuplot can't handle it when trying to compute |
|
2198 // axis ranges, etc.). |
|
2199 // |
|
2200 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
2201 // |
|
2202 // Returns 1 for success and 0 for failure. |
604
|
2203 |
|
2204 // XXX FIXME XXX -- should probably write the help string here too. |
|
2205 |
2799
|
2206 bool |
2086
|
2207 save_ascii_data (ostream& os, const octave_value& tc, |
2799
|
2208 const string& name, bool strip_nan_and_inf, |
|
2209 bool mark_as_global, int precision) |
604
|
2210 { |
2799
|
2211 bool success = true; |
620
|
2212 |
604
|
2213 if (! precision) |
2194
|
2214 precision = Vsave_precision; |
604
|
2215 |
1755
|
2216 if (! name.empty ()) |
604
|
2217 os << "# name: " << name << "\n"; |
|
2218 |
|
2219 long old_precision = os.precision (); |
|
2220 os.precision (precision); |
|
2221 |
3233
|
2222 if (tc.is_string ()) |
|
2223 { |
|
2224 ascii_save_type (os, "string array", mark_as_global); |
|
2225 charMatrix chm = tc.char_matrix_value (); |
|
2226 int elements = chm.rows (); |
|
2227 os << "# elements: " << elements << "\n"; |
|
2228 for (int i = 0; i < elements; i++) |
|
2229 { |
|
2230 int len = chm.cols (); |
|
2231 os << "# length: " << len << "\n"; |
|
2232 string tstr = chm.row_as_string (i); |
|
2233 const char *tmp = tstr.data (); |
|
2234 os.write (tmp, len); |
|
2235 os << "\n"; |
|
2236 } |
|
2237 } |
|
2238 else if (tc.is_range ()) |
|
2239 { |
|
2240 ascii_save_type (os, "range", mark_as_global); |
|
2241 Range tmp = tc.range_value (); |
|
2242 os << "# base, limit, increment\n" |
|
2243 << tmp.base () << " " |
|
2244 << tmp.limit () << " " |
|
2245 << tmp.inc () << "\n"; |
|
2246 } |
|
2247 else if (tc.is_real_scalar ()) |
620
|
2248 { |
|
2249 ascii_save_type (os, "scalar", mark_as_global); |
872
|
2250 |
|
2251 double d = tc.double_value (); |
|
2252 if (strip_nan_and_inf) |
|
2253 { |
|
2254 if (xisnan (d)) |
|
2255 { |
|
2256 error ("only value to plot is NaN"); |
2799
|
2257 success = false; |
872
|
2258 } |
|
2259 else |
|
2260 { |
|
2261 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
2262 os << d << "\n"; |
|
2263 } |
|
2264 } |
|
2265 else |
|
2266 os << d << "\n"; |
620
|
2267 } |
|
2268 else if (tc.is_real_matrix ()) |
|
2269 { |
|
2270 ascii_save_type (os, "matrix", mark_as_global); |
|
2271 os << "# rows: " << tc.rows () << "\n" |
872
|
2272 << "# columns: " << tc.columns () << "\n"; |
|
2273 |
|
2274 Matrix tmp = tc.matrix_value (); |
|
2275 if (strip_nan_and_inf) |
|
2276 tmp = strip_infnan (tmp); |
|
2277 |
|
2278 os << tmp; |
620
|
2279 } |
|
2280 else if (tc.is_complex_scalar ()) |
|
2281 { |
|
2282 ascii_save_type (os, "complex scalar", mark_as_global); |
872
|
2283 |
|
2284 Complex c = tc.complex_value (); |
|
2285 if (strip_nan_and_inf) |
|
2286 { |
|
2287 if (xisnan (c)) |
|
2288 { |
|
2289 error ("only value to plot is NaN"); |
2799
|
2290 success = false; |
872
|
2291 } |
|
2292 else |
|
2293 { |
|
2294 double re = real (c); |
|
2295 double im = imag (c); |
|
2296 |
|
2297 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
2298 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
2299 |
|
2300 c = Complex (re, im); |
|
2301 |
|
2302 os << c << "\n"; |
|
2303 } |
|
2304 } |
|
2305 else |
|
2306 os << c << "\n"; |
620
|
2307 } |
|
2308 else if (tc.is_complex_matrix ()) |
604
|
2309 { |
620
|
2310 ascii_save_type (os, "complex matrix", mark_as_global); |
|
2311 os << "# rows: " << tc.rows () << "\n" |
875
|
2312 << "# columns: " << tc.columns () << "\n"; |
|
2313 |
|
2314 ComplexMatrix tmp = tc.complex_matrix_value (); |
872
|
2315 if (strip_nan_and_inf) |
|
2316 tmp = strip_infnan (tmp); |
|
2317 |
|
2318 os << tmp; |
620
|
2319 } |
|
2320 else |
2799
|
2321 gripe_wrong_type_arg ("save", tc, false); |
604
|
2322 |
|
2323 os.precision (old_precision); |
|
2324 |
872
|
2325 return (os && success); |
604
|
2326 } |
|
2327 |
|
2328 // Save the info from sr on stream os in the format specified by fmt. |
|
2329 |
|
2330 static void |
630
|
2331 do_save (ostream& os, symbol_record *sr, load_save_format fmt, |
|
2332 int save_as_floats) |
604
|
2333 { |
|
2334 if (! sr->is_variable ()) |
|
2335 { |
|
2336 error ("save: can only save variables, not functions"); |
|
2337 return; |
|
2338 } |
|
2339 |
1755
|
2340 string name = sr->name (); |
|
2341 string help = sr->help (); |
604
|
2342 int global = sr->is_linked_to_global (); |
2970
|
2343 |
|
2344 octave_value tc = sr->def (); |
604
|
2345 |
1755
|
2346 if (tc.is_undefined ()) |
604
|
2347 return; |
|
2348 |
|
2349 switch (fmt) |
|
2350 { |
|
2351 case LS_ASCII: |
2799
|
2352 save_ascii_data (os, tc, name, false, global); |
604
|
2353 break; |
|
2354 |
|
2355 case LS_BINARY: |
630
|
2356 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
2357 break; |
|
2358 |
667
|
2359 case LS_MAT_BINARY: |
|
2360 save_mat_binary_data (os, tc, name); |
|
2361 break; |
|
2362 |
604
|
2363 default: |
775
|
2364 gripe_unrecognized_data_fmt ("save"); |
604
|
2365 break; |
|
2366 } |
|
2367 } |
|
2368 |
|
2369 // Save variables with names matching PATTERN on stream OS in the |
3019
|
2370 // format specified by FMT. If SAVE_BUILTINS is TRUE, also save |
604
|
2371 // builtin variables with names that match PATTERN. |
|
2372 |
|
2373 static int |
3019
|
2374 save_vars (ostream& os, const string& pattern, bool save_builtins, |
630
|
2375 load_save_format fmt, int save_as_floats) |
604
|
2376 { |
|
2377 int count; |
|
2378 |
|
2379 symbol_record **vars = curr_sym_tab->glob |
3010
|
2380 (count, pattern, symbol_record::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
604
|
2381 |
|
2382 int saved = count; |
|
2383 |
|
2384 int i; |
|
2385 |
|
2386 for (i = 0; i < count; i++) |
620
|
2387 { |
630
|
2388 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2389 |
|
2390 if (error_state) |
|
2391 break; |
|
2392 } |
604
|
2393 |
|
2394 delete [] vars; |
|
2395 |
620
|
2396 if (! error_state && save_builtins) |
604
|
2397 { |
|
2398 symbol_record **vars = global_sym_tab->glob |
3010
|
2399 (count, pattern, symbol_record::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
604
|
2400 |
|
2401 saved += count; |
|
2402 |
|
2403 for (i = 0; i < count; i++) |
620
|
2404 { |
630
|
2405 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2406 |
|
2407 if (error_state) |
|
2408 break; |
|
2409 } |
604
|
2410 |
|
2411 delete [] vars; |
|
2412 } |
|
2413 |
|
2414 return saved; |
|
2415 } |
|
2416 |
|
2417 static load_save_format |
|
2418 get_default_save_format (void) |
|
2419 { |
|
2420 load_save_format retval = LS_ASCII; |
|
2421 |
2194
|
2422 string fmt = Vdefault_save_format; |
1755
|
2423 |
|
2424 if (fmt == "binary") |
604
|
2425 retval = LS_BINARY; |
1755
|
2426 else if (fmt == "mat-binary" || fmt =="mat_binary") |
911
|
2427 retval = LS_MAT_BINARY; |
604
|
2428 |
|
2429 return retval; |
|
2430 } |
|
2431 |
863
|
2432 static void |
3185
|
2433 write_header (ostream& os, load_save_format format) |
863
|
2434 { |
3185
|
2435 switch (format) |
863
|
2436 { |
3185
|
2437 case LS_BINARY: |
|
2438 { |
|
2439 os << (oct_mach_info::words_big_endian () |
|
2440 ? "Octave-1-B" : "Octave-1-L"); |
|
2441 |
|
2442 oct_mach_info::float_format flt_fmt = |
|
2443 oct_mach_info::native_float_format (); |
|
2444 |
|
2445 char tmp = (char) float_format_to_mopt_digit (flt_fmt); |
|
2446 |
|
2447 os.write (&tmp, 1); |
|
2448 } |
|
2449 break; |
|
2450 |
|
2451 case LS_ASCII: |
|
2452 { |
3258
|
2453 octave_gmtime now; |
|
2454 string time_string = now.asctime (); |
3185
|
2455 time_string = time_string.substr (0, time_string.length () - 1); |
|
2456 |
|
2457 os << "# Created by Octave " OCTAVE_VERSION ", " |
|
2458 << time_string |
|
2459 << " <" |
|
2460 << octave_env::get_user_name () |
|
2461 << "@" |
|
2462 << octave_env::get_host_name () |
|
2463 << ">" << "\n"; |
|
2464 } |
|
2465 break; |
|
2466 |
|
2467 default: |
|
2468 break; |
863
|
2469 } |
|
2470 } |
|
2471 |
|
2472 static void |
1755
|
2473 save_vars (const string_vector& argv, int argv_idx, int argc, |
3019
|
2474 ostream& os, bool save_builtins, load_save_format fmt, |
3185
|
2475 bool save_as_floats, bool write_header_info) |
863
|
2476 { |
3185
|
2477 if (write_header_info) |
|
2478 write_header (os, fmt); |
863
|
2479 |
1755
|
2480 if (argv_idx == argc) |
863
|
2481 { |
|
2482 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
|
2483 } |
|
2484 else |
|
2485 { |
1755
|
2486 for (int i = argv_idx; i < argc; i++) |
863
|
2487 { |
1755
|
2488 if (! save_vars (os, argv[i], save_builtins, fmt, save_as_floats)) |
863
|
2489 { |
1755
|
2490 warning ("save: no such variable `%s'", argv[i].c_str ()); |
863
|
2491 } |
|
2492 } |
|
2493 } |
|
2494 } |
|
2495 |
1380
|
2496 void |
|
2497 save_user_variables (void) |
|
2498 { |
3189
|
2499 if (Vcrash_dumps_octave_core) |
1380
|
2500 { |
3189
|
2501 // XXX FIXME XXX -- should choose better file name? |
|
2502 |
|
2503 const char *fname = "octave-core"; |
|
2504 |
|
2505 message (0, "attempting to save variables to `%s'...", fname); |
|
2506 |
|
2507 load_save_format format = get_default_save_format (); |
|
2508 |
|
2509 unsigned mode = ios::out|ios::trunc; |
|
2510 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2511 mode |= ios::bin; |
|
2512 |
|
2513 ofstream file (fname, mode); |
|
2514 |
|
2515 if (file) |
|
2516 { |
|
2517 save_vars (string_vector (), 0, 0, file, false, format, false, true); |
|
2518 message (0, "save to `%s' complete", fname); |
|
2519 } |
|
2520 else |
|
2521 warning ("unable to open `%s' for writing...", fname); |
1380
|
2522 } |
|
2523 } |
|
2524 |
1957
|
2525 DEFUN_TEXT (save, args, , |
3185
|
2526 "save [-append] [-ascii] [-binary] [-float-binary] [-mat-binary] \n\ |
667
|
2527 [-save-builtins] file [pattern ...]\n\ |
604
|
2528 \n\ |
|
2529 save variables in a file") |
|
2530 { |
2086
|
2531 octave_value_list retval; |
604
|
2532 |
1755
|
2533 int argc = args.length () + 1; |
|
2534 |
1968
|
2535 string_vector argv = args.make_argv ("save"); |
1755
|
2536 |
|
2537 if (error_state) |
|
2538 return retval; |
604
|
2539 |
1358
|
2540 // Here is where we would get the default save format if it were |
|
2541 // stored in a user preference variable. |
604
|
2542 |
3019
|
2543 bool save_builtins = false; |
|
2544 |
|
2545 bool save_as_floats = false; |
630
|
2546 |
604
|
2547 load_save_format format = get_default_save_format (); |
|
2548 |
3185
|
2549 bool append = false; |
|
2550 |
1755
|
2551 int i; |
|
2552 for (i = 1; i < argc; i++) |
604
|
2553 { |
3185
|
2554 if (argv[i] == "-append") |
|
2555 { |
|
2556 append = true; |
|
2557 } |
1755
|
2558 if (argv[i] == "-ascii" || argv[i] == "-a") |
604
|
2559 { |
|
2560 format = LS_ASCII; |
|
2561 } |
1755
|
2562 else if (argv[i] == "-binary" || argv[i] == "-b") |
604
|
2563 { |
|
2564 format = LS_BINARY; |
|
2565 } |
1755
|
2566 else if (argv[i] == "-mat-binary" || argv[i] == "-m") |
667
|
2567 { |
|
2568 format = LS_MAT_BINARY; |
|
2569 } |
1755
|
2570 else if (argv[i] == "-float-binary" || argv[i] == "-f") |
630
|
2571 { |
|
2572 format = LS_BINARY; |
3019
|
2573 save_as_floats = true; |
630
|
2574 } |
1755
|
2575 else if (argv[i] == "-save-builtins") |
604
|
2576 { |
3019
|
2577 save_builtins = true; |
604
|
2578 } |
|
2579 else |
|
2580 break; |
|
2581 } |
|
2582 |
2057
|
2583 if (i == argc) |
604
|
2584 { |
|
2585 print_usage ("save"); |
|
2586 return retval; |
|
2587 } |
|
2588 |
630
|
2589 if (save_as_floats && format == LS_ASCII) |
|
2590 { |
|
2591 error ("save: cannot specify both -ascii and -float-binary"); |
|
2592 return retval; |
|
2593 } |
|
2594 |
1755
|
2595 if (argv[i] == "-") |
604
|
2596 { |
1755
|
2597 i++; |
863
|
2598 |
1358
|
2599 // XXX FIXME XXX -- should things intended for the screen end up |
2086
|
2600 // in a octave_value (string)? |
863
|
2601 |
2095
|
2602 save_vars (argv, i, argc, octave_stdout, save_builtins, format, |
3185
|
2603 save_as_floats, true); |
604
|
2604 } |
1755
|
2605 |
|
2606 // Guard against things like `save a*', which are probably mistakes... |
|
2607 |
|
2608 else if (i == argc - 1 && glob_pattern_p (argv[i])) |
|
2609 { |
|
2610 print_usage ("save"); |
604
|
2611 return retval; |
|
2612 } |
|
2613 else |
|
2614 { |
2926
|
2615 string fname = file_ops::tilde_expand (argv[i]); |
1755
|
2616 |
|
2617 i++; |
604
|
2618 |
3185
|
2619 unsigned mode = ios::out; |
604
|
2620 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2621 mode |= ios::bin; |
|
2622 |
3185
|
2623 mode |= append ? ios::ate : ios::trunc; |
|
2624 |
1750
|
2625 ofstream file (fname.c_str (), mode); |
863
|
2626 |
|
2627 if (file) |
|
2628 { |
3185
|
2629 bool write_header_info |
|
2630 = ((file.rdbuf ())->seekoff (0, ios::cur) == 0); |
|
2631 |
1755
|
2632 save_vars (argv, i, argc, file, save_builtins, format, |
3185
|
2633 save_as_floats, write_header_info); |
863
|
2634 } |
|
2635 else |
604
|
2636 { |
1755
|
2637 error ("save: couldn't open output file `%s'", fname.c_str ()); |
604
|
2638 return retval; |
|
2639 } |
|
2640 } |
|
2641 |
|
2642 return retval; |
|
2643 } |
|
2644 |
|
2645 // Maybe this should be a static function in tree-plot.cc? |
|
2646 |
620
|
2647 // If TC is matrix, save it on stream OS in a format useful for |
604
|
2648 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
3019
|
2649 // TRUE, assume a parametric 3-dimensional plot will be generated. |
604
|
2650 |
2799
|
2651 bool |
|
2652 save_three_d (ostream& os, const octave_value& tc, bool parametric) |
604
|
2653 { |
3019
|
2654 bool fail = false; |
604
|
2655 |
620
|
2656 int nr = tc.rows (); |
|
2657 int nc = tc.columns (); |
|
2658 |
|
2659 if (tc.is_real_matrix ()) |
604
|
2660 { |
|
2661 os << "# 3D data...\n" |
|
2662 << "# type: matrix\n" |
|
2663 << "# total rows: " << nr << "\n" |
|
2664 << "# total columns: " << nc << "\n"; |
|
2665 |
|
2666 if (parametric) |
|
2667 { |
|
2668 int extras = nc % 3; |
|
2669 if (extras) |
|
2670 warning ("ignoring last %d columns", extras); |
|
2671 |
620
|
2672 Matrix tmp = tc.matrix_value (); |
872
|
2673 tmp = strip_infnan (tmp); |
|
2674 nr = tmp.rows (); |
|
2675 |
604
|
2676 for (int i = 0; i < nc-extras; i += 3) |
|
2677 { |
|
2678 os << tmp.extract (0, i, nr-1, i+2); |
|
2679 if (i+3 < nc-extras) |
|
2680 os << "\n"; |
|
2681 } |
|
2682 } |
|
2683 else |
|
2684 { |
620
|
2685 Matrix tmp = tc.matrix_value (); |
872
|
2686 tmp = strip_infnan (tmp); |
|
2687 nr = tmp.rows (); |
|
2688 |
604
|
2689 for (int i = 0; i < nc; i++) |
|
2690 { |
|
2691 os << tmp.extract (0, i, nr-1, i); |
|
2692 if (i+1 < nc) |
|
2693 os << "\n"; |
|
2694 } |
|
2695 } |
620
|
2696 } |
|
2697 else |
|
2698 { |
604
|
2699 ::error ("for now, I can only save real matrices in 3D format"); |
3019
|
2700 fail = true; |
604
|
2701 } |
620
|
2702 |
|
2703 return (os && ! fail); |
604
|
2704 } |
|
2705 |
2194
|
2706 static int |
3189
|
2707 crash_dumps_octave_core (void) |
|
2708 { |
|
2709 Vcrash_dumps_octave_core = check_preference ("crash_dumps_octave_core"); |
|
2710 return 0; |
|
2711 } |
|
2712 |
|
2713 |
|
2714 static int |
2194
|
2715 default_save_format (void) |
|
2716 { |
|
2717 int status = 0; |
|
2718 |
|
2719 string s = builtin_string_variable ("default_save_format"); |
|
2720 |
|
2721 if (s.empty ()) |
|
2722 { |
|
2723 gripe_invalid_value_specified ("default_save_format"); |
|
2724 status = -1; |
|
2725 } |
|
2726 else |
|
2727 Vdefault_save_format = s; |
|
2728 |
|
2729 return status; |
|
2730 } |
|
2731 |
|
2732 static int |
|
2733 save_precision (void) |
|
2734 { |
|
2735 double val; |
|
2736 if (builtin_real_scalar_variable ("save_precision", val) |
|
2737 && ! xisnan (val)) |
|
2738 { |
|
2739 int ival = NINT (val); |
2800
|
2740 if (ival >= 0 && ival == val) |
2194
|
2741 { |
|
2742 Vsave_precision = ival; |
|
2743 return 0; |
|
2744 } |
|
2745 } |
|
2746 gripe_invalid_value_specified ("save_precision"); |
|
2747 return -1; |
|
2748 } |
|
2749 |
|
2750 void |
|
2751 symbols_of_load_save (void) |
|
2752 { |
3258
|
2753 DEFVAR (crash_dumps_octave_core, 1.0, crash_dumps_octave_core, |
3189
|
2754 "write octave-core file if Octave crashes or is killed by a signal"); |
|
2755 |
3258
|
2756 DEFVAR (default_save_format, "ascii", default_save_format, |
2194
|
2757 "default format for files created with save, may be one of\n\ |
|
2758 \"binary\", \"text\", or \"mat-binary\""); |
|
2759 |
3258
|
2760 DEFVAR (save_precision, 15.0, save_precision, |
2194
|
2761 "number of significant figures kept by the ASCII save command"); |
|
2762 } |
|
2763 |
604
|
2764 /* |
|
2765 ;;; Local Variables: *** |
|
2766 ;;; mode: C++ *** |
|
2767 ;;; End: *** |
|
2768 */ |