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