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