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