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