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