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