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