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