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: |
774
|
523 gripe_unrecognized_float_fmt (); |
630
|
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: |
774
|
623 gripe_unrecognized_float_fmt (); |
604
|
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: |
774
|
723 error ("unrecognized data format requested"); |
604
|
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 |
774
|
957 error ("load: unable to load data for unknown symbol type"); |
604
|
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 |
774
|
1003 error ("load: unable to load data for unknown symbol type"); |
604
|
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); |
774
|
1456 if (error_state || ! is) |
630
|
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); |
774
|
1479 if (error_state || ! is) |
604
|
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); |
774
|
1491 if (error_state || ! is) |
630
|
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); |
774
|
1515 if (error_state || ! is) |
604
|
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 { |
671
|
1621 swap = 0; |
|
1622 |
604
|
1623 is.read (&mopt, 4); |
|
1624 if (! is) |
|
1625 { |
|
1626 if (! is.eof ()) |
|
1627 goto data_read_error; |
|
1628 return 1; |
|
1629 } |
|
1630 |
|
1631 if (! is.read (&nr, 4)) |
|
1632 goto data_read_error; |
|
1633 |
|
1634 if (! is.read (&nc, 4)) |
|
1635 goto data_read_error; |
|
1636 |
|
1637 if (! is.read (&imag, 4)) |
|
1638 goto data_read_error; |
|
1639 |
|
1640 if (! is.read (&len, 4)) |
|
1641 goto data_read_error; |
|
1642 |
|
1643 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
1644 // bigger than we expect, so we swap bytes. |
|
1645 // |
|
1646 // If mopt is zero, it means the file was written on a little endian |
|
1647 // machine, and we only need to swap if we are running on a big endian |
|
1648 // machine. |
|
1649 // |
|
1650 // Gag me. |
|
1651 |
|
1652 #if defined (WORDS_BIGENDIAN) |
|
1653 if (mopt == 0) |
|
1654 swap = 1; |
|
1655 #endif |
|
1656 |
|
1657 if (mopt > 9999) |
|
1658 swap = 1; |
|
1659 |
|
1660 if (swap) |
|
1661 { |
|
1662 swap_4_bytes ((char *) &mopt); |
|
1663 swap_4_bytes ((char *) &nr); |
|
1664 swap_4_bytes ((char *) &nc); |
|
1665 swap_4_bytes ((char *) &imag); |
|
1666 swap_4_bytes ((char *) &len); |
|
1667 } |
|
1668 |
|
1669 if (mopt > 9999 || imag > 1 || imag < 0 || len > 8192) |
|
1670 { |
|
1671 if (! quiet) |
|
1672 error ("load: can't read binary file"); |
|
1673 return -1; |
|
1674 } |
|
1675 |
|
1676 return 0; |
|
1677 |
|
1678 data_read_error: |
|
1679 return -1; |
|
1680 } |
|
1681 |
617
|
1682 // We don't just use a cast here, because we need to be able to detect |
|
1683 // possible errors. |
|
1684 |
|
1685 static floating_point_format |
|
1686 get_floating_point_format (int mach) |
|
1687 { |
619
|
1688 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
1689 |
617
|
1690 switch (mach) |
|
1691 { |
|
1692 case 0: |
|
1693 flt_fmt = LS_IEEE_LITTLE; |
|
1694 break; |
|
1695 |
|
1696 case 1: |
|
1697 flt_fmt = LS_IEEE_BIG; |
|
1698 break; |
|
1699 |
|
1700 case 2: |
|
1701 flt_fmt = LS_VAX_D; |
|
1702 break; |
|
1703 |
|
1704 case 3: |
|
1705 flt_fmt = LS_VAX_G; |
|
1706 break; |
|
1707 |
|
1708 case 4: |
|
1709 flt_fmt = LS_CRAY; |
|
1710 break; |
|
1711 |
|
1712 default: |
619
|
1713 flt_fmt = LS_UNKNOWN_FLT_FMT; |
617
|
1714 break; |
|
1715 } |
619
|
1716 |
|
1717 return flt_fmt; |
617
|
1718 } |
619
|
1719 |
604
|
1720 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
1721 // place it in TC, returning the name of the variable. |
|
1722 // |
|
1723 // The data is expected to be in Matlab's .mat format, though not all |
|
1724 // the features of that format are supported. |
|
1725 // |
|
1726 // FILENAME is used for error messages. |
|
1727 // |
|
1728 // This format provides no way to tag the data as global. |
|
1729 |
|
1730 static char * |
|
1731 read_mat_binary_data (istream& is, const char *filename, |
|
1732 tree_constant& tc) |
|
1733 { |
|
1734 // These are initialized here instead of closer to where they are |
|
1735 // first used to avoid errors from gcc about goto crossing |
|
1736 // initialization of variable. |
|
1737 |
|
1738 Matrix re; |
|
1739 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
1740 char *name = 0; |
|
1741 int swap = 0, type = 0, prec = 0, mach = 0, dlen = 0; |
|
1742 |
|
1743 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
1744 |
|
1745 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
1746 if (err) |
|
1747 { |
|
1748 if (err < 0) |
|
1749 goto data_read_error; |
|
1750 else |
|
1751 return 0; |
|
1752 } |
|
1753 |
|
1754 type = mopt % 10; // Full, sparse, etc. |
|
1755 mopt /= 10; // Eliminate first digit. |
|
1756 prec = mopt % 10; // double, float, int, etc. |
|
1757 mopt /= 100; // Skip unused third digit too. |
|
1758 mach = mopt % 10; // IEEE, VAX, etc. |
|
1759 |
617
|
1760 flt_fmt = get_floating_point_format (mach); |
|
1761 if (flt_fmt == LS_UNKNOWN_FLT_FMT) |
604
|
1762 { |
|
1763 error ("load: unrecognized binary format!"); |
|
1764 return 0; |
|
1765 } |
|
1766 |
|
1767 if (type != 0 && type != 1) |
|
1768 { |
|
1769 error ("load: can't read sparse matrices"); |
|
1770 return 0; |
|
1771 } |
|
1772 |
|
1773 if (imag && type == 1) |
|
1774 { |
|
1775 error ("load: encountered complex matrix with string flag set!"); |
|
1776 return 0; |
|
1777 } |
|
1778 |
|
1779 name = new char [len]; |
|
1780 if (! is.read (name, len)) |
|
1781 goto data_read_error; |
|
1782 |
|
1783 dlen = nr * nc; |
|
1784 if (dlen < 0) |
|
1785 goto data_read_error; |
|
1786 |
|
1787 re.resize (nr, nc); |
|
1788 |
|
1789 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1790 |
|
1791 if (! is || error_state) |
|
1792 { |
|
1793 error ("load: reading matrix data for `%s'", name); |
|
1794 goto data_read_error; |
|
1795 } |
|
1796 |
|
1797 if (imag) |
|
1798 { |
|
1799 Matrix im (nr, nc); |
|
1800 |
|
1801 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1802 |
|
1803 if (! is || error_state) |
|
1804 { |
|
1805 error ("load: reading imaginary matrix data for `%s'", name); |
|
1806 goto data_read_error; |
|
1807 } |
|
1808 |
|
1809 ComplexMatrix ctmp (nr, nc); |
|
1810 |
|
1811 for (int j = 0; j < nc; j++) |
|
1812 for (int i = 0; i < nr; i++) |
|
1813 ctmp.elem (i, j) = Complex (re.elem (i, j), im.elem (i, j)); |
|
1814 |
|
1815 tc = ctmp; |
|
1816 } |
|
1817 else |
|
1818 tc = re; |
|
1819 |
|
1820 if (type == 1) |
|
1821 tc = tc.convert_to_str (); |
|
1822 |
|
1823 return name; |
|
1824 |
|
1825 data_read_error: |
|
1826 error ("load: trouble reading binary file `%s'", filename); |
|
1827 delete [] name; |
|
1828 return 0; |
|
1829 } |
|
1830 |
|
1831 // Return nonzero if NAME matches one of the given globbing PATTERNS. |
|
1832 |
|
1833 static int |
|
1834 matches_patterns (char **patterns, int num_pat, char *name) |
|
1835 { |
|
1836 while (num_pat-- > 0) |
|
1837 { |
|
1838 if (fnmatch (*patterns++, name, __FNM_FLAGS) == 0) |
|
1839 return 1; |
|
1840 } |
|
1841 return 0; |
|
1842 } |
|
1843 |
|
1844 static int |
|
1845 read_binary_file_header (istream& is, int& swap, |
630
|
1846 floating_point_format& flt_fmt, int quiet = 0) |
604
|
1847 { |
|
1848 int magic_len = 10; |
|
1849 char magic [magic_len+1]; |
|
1850 is.read (magic, magic_len); |
|
1851 magic[magic_len] = '\0'; |
|
1852 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
|
1853 { |
|
1854 #if defined (WORDS_BIGENDIAN) |
|
1855 swap = 1; |
|
1856 #else |
|
1857 swap = 0; |
|
1858 #endif |
|
1859 } |
|
1860 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
|
1861 { |
|
1862 #if defined (WORDS_BIGENDIAN) |
|
1863 swap = 0; |
|
1864 #else |
|
1865 swap = 1; |
|
1866 #endif |
|
1867 } |
|
1868 else |
|
1869 { |
|
1870 if (! quiet) |
|
1871 error ("load: can't read binary file"); |
|
1872 return -1; |
|
1873 } |
|
1874 |
|
1875 char tmp = 0; |
|
1876 is.read (&tmp, 1); |
|
1877 |
617
|
1878 flt_fmt = get_floating_point_format (tmp); |
|
1879 if (flt_fmt == LS_UNKNOWN_FLT_FMT) |
604
|
1880 { |
|
1881 if (! quiet) |
|
1882 error ("load: unrecognized binary format!"); |
|
1883 return -1; |
|
1884 } |
|
1885 |
|
1886 return 0; |
|
1887 } |
|
1888 |
|
1889 static load_save_format |
|
1890 get_file_format (const char *fname, const char *orig_fname) |
|
1891 { |
|
1892 load_save_format retval = LS_UNKNOWN; |
|
1893 |
|
1894 ifstream file; |
|
1895 |
|
1896 file.open (fname); |
|
1897 |
|
1898 if (! file) |
|
1899 { |
|
1900 error ("load: couldn't open input file `%s'", orig_fname); |
|
1901 return retval; |
|
1902 } |
|
1903 |
|
1904 int swap; |
|
1905 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
1906 |
|
1907 if (read_binary_file_header (file, swap, flt_fmt, 1) == 0) |
|
1908 retval = LS_BINARY; |
|
1909 else |
|
1910 { |
|
1911 file.seekg (0, ios::beg); |
|
1912 |
|
1913 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
1914 int swap; |
|
1915 |
|
1916 if (read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1) == 0) |
|
1917 retval = LS_MAT_BINARY; |
|
1918 else |
|
1919 { |
|
1920 file.seekg (0, ios::beg); |
|
1921 |
|
1922 char *tmp = extract_keyword (file, "name"); |
|
1923 if (tmp) |
|
1924 retval = LS_ASCII; |
|
1925 |
|
1926 delete [] tmp; |
|
1927 } |
|
1928 } |
|
1929 |
|
1930 file.close (); |
|
1931 |
|
1932 if (retval == LS_UNKNOWN) |
|
1933 error ("load: unable to determine file format for `%s'", orig_fname); |
|
1934 |
|
1935 return retval; |
|
1936 } |
|
1937 |
|
1938 DEFUN_TEXT ("load", Fload, Sload, -1, 1, |
|
1939 "load [-force] [-ascii] [-binary] [-mat-binary] file [pattern ...]\n |
|
1940 \n\ |
|
1941 Load variables from a file.\n\ |
|
1942 \n\ |
|
1943 If no argument is supplied to select a format, load tries to read the |
|
1944 named file as an Octave binary, then as a .mat file, and then as an |
|
1945 Octave text file.\n\ |
|
1946 \n\ |
|
1947 If the option -force is given, variables with the same names as those |
|
1948 found in the file will be replaced with the values read from the file.") |
|
1949 { |
|
1950 Octave_object retval; |
|
1951 |
|
1952 DEFINE_ARGV("load"); |
|
1953 |
|
1954 argc--; |
|
1955 argv++; |
|
1956 |
|
1957 int force = 0; |
|
1958 |
621
|
1959 // It isn't necessary to have the default load format stored in a user |
|
1960 // preference variable since we can determine the type of file as we |
|
1961 // are reading. |
604
|
1962 |
|
1963 load_save_format format = LS_UNKNOWN; |
|
1964 |
621
|
1965 int list_only = 0; |
|
1966 int verbose = 0; |
|
1967 |
604
|
1968 while (argc > 0) |
|
1969 { |
|
1970 if (strcmp (*argv, "-force") == 0 || strcmp (*argv, "-f") == 0) |
|
1971 { |
|
1972 force++; |
|
1973 argc--; |
|
1974 argv++; |
|
1975 } |
621
|
1976 else if (strcmp (*argv, "-list") == 0 || strcmp (*argv, "-l") == 0) |
|
1977 { |
|
1978 list_only = 1; |
|
1979 argc--; |
|
1980 argv++; |
|
1981 } |
|
1982 else if (strcmp (*argv, "-verbose") == 0 || strcmp (*argv, "-v") == 0) |
|
1983 { |
|
1984 verbose = 1; |
|
1985 argc--; |
|
1986 argv++; |
|
1987 } |
604
|
1988 else if (strcmp (*argv, "-ascii") == 0 || strcmp (*argv, "-a") == 0) |
|
1989 { |
|
1990 format = LS_ASCII; |
|
1991 argc--; |
|
1992 argv++; |
|
1993 } |
|
1994 else if (strcmp (*argv, "-binary") == 0 || strcmp (*argv, "-b") == 0) |
|
1995 { |
|
1996 format = LS_BINARY; |
|
1997 argc--; |
|
1998 argv++; |
|
1999 } |
|
2000 else if (strcmp (*argv, "-mat-binary") == 0 || strcmp (*argv, "-m") == 0) |
|
2001 { |
|
2002 format = LS_MAT_BINARY; |
|
2003 argc--; |
|
2004 argv++; |
|
2005 } |
|
2006 else |
|
2007 break; |
|
2008 } |
|
2009 |
|
2010 if (argc < 1) |
|
2011 { |
|
2012 error ("load: you must specify a single file to read"); |
|
2013 DELETE_ARGV; |
|
2014 return retval; |
|
2015 } |
|
2016 |
|
2017 char *orig_fname = *argv; |
|
2018 static istream stream; |
|
2019 static ifstream file; |
|
2020 if (strcmp (*argv, "-") == 0) |
|
2021 { |
|
2022 if (format == LS_UNKNOWN) |
|
2023 { |
|
2024 error ("load: must specify file format if reading from stdin"); |
|
2025 DELETE_ARGV; |
|
2026 return retval; |
|
2027 } |
|
2028 stream = cin; |
|
2029 } |
|
2030 else |
|
2031 { |
|
2032 char *fname = tilde_expand (*argv); |
|
2033 |
|
2034 if (format == LS_UNKNOWN) |
|
2035 format = get_file_format (fname, orig_fname); |
|
2036 |
|
2037 if (format == LS_UNKNOWN) |
|
2038 { |
|
2039 DELETE_ARGV; |
|
2040 return retval; |
|
2041 } |
|
2042 |
|
2043 argv++; |
|
2044 argc--; |
|
2045 |
|
2046 unsigned mode = ios::in; |
|
2047 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2048 mode |= ios::bin; |
|
2049 |
|
2050 file.open (fname, mode); |
|
2051 |
|
2052 if (! file) |
|
2053 { |
|
2054 error ("load: couldn't open input file `%s'", orig_fname); |
|
2055 DELETE_ARGV; |
|
2056 return retval; |
|
2057 } |
|
2058 stream = file; |
|
2059 } |
|
2060 |
|
2061 int swap = 0; |
|
2062 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
2063 |
|
2064 if (format == LS_BINARY) |
|
2065 { |
|
2066 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
2067 { |
|
2068 file.close (); |
|
2069 DELETE_ARGV; |
|
2070 return retval; |
|
2071 } |
|
2072 } |
|
2073 |
621
|
2074 ostrstream output_buf; |
604
|
2075 int count = 0; |
|
2076 for (;;) |
|
2077 { |
|
2078 int global = 0; |
|
2079 tree_constant tc; |
|
2080 |
|
2081 char *name = 0; |
|
2082 char *doc = 0; |
|
2083 |
|
2084 switch (format) |
|
2085 { |
|
2086 case LS_ASCII: |
|
2087 name = read_ascii_data (stream, orig_fname, global, tc); |
|
2088 break; |
|
2089 |
|
2090 case LS_BINARY: |
|
2091 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
2092 global, tc, doc); |
|
2093 break; |
|
2094 |
|
2095 case LS_MAT_BINARY: |
|
2096 name = read_mat_binary_data (stream, orig_fname, tc); |
|
2097 break; |
|
2098 |
|
2099 default: |
|
2100 panic_impossible (); |
|
2101 break; |
|
2102 } |
|
2103 |
|
2104 if (stream.eof ()) |
|
2105 { |
|
2106 break; |
|
2107 } |
|
2108 else if (! error_state && name) |
|
2109 { |
|
2110 if (tc.is_defined ()) |
|
2111 { |
|
2112 if (argc == 0 || matches_patterns (argv, argc, name)) |
|
2113 { |
|
2114 count++; |
621
|
2115 if (list_only) |
|
2116 { |
|
2117 if (verbose) |
|
2118 { |
|
2119 if (count == 1) |
|
2120 output_buf |
|
2121 << "type rows cols name\n" |
|
2122 << "==== ==== ==== ====\n"; |
|
2123 |
|
2124 output_buf.form ("%-16s", tc.type_as_string ()); |
|
2125 output_buf.form ("%7d", tc.rows ()); |
|
2126 output_buf.form ("%7d", tc.columns ()); |
|
2127 output_buf << " "; |
|
2128 } |
|
2129 output_buf << name << "\n"; |
|
2130 } |
|
2131 else |
|
2132 { |
|
2133 install_loaded_variable (force, name, tc, global, doc); |
|
2134 } |
604
|
2135 } |
|
2136 } |
|
2137 else |
|
2138 error ("load: unable to load variable `%s'", name); |
|
2139 } |
|
2140 else |
|
2141 { |
|
2142 if (count == 0) |
|
2143 error ("load: are you sure `%s' is an Octave data file?", |
|
2144 orig_fname); |
|
2145 |
|
2146 break; |
|
2147 } |
|
2148 |
|
2149 delete [] name; |
|
2150 delete [] doc; |
|
2151 } |
|
2152 |
621
|
2153 if (list_only && count) |
|
2154 { |
|
2155 if (nargout > 0) |
|
2156 { |
|
2157 output_buf << ends; |
|
2158 char *msg = output_buf.str (); |
|
2159 retval = msg; |
|
2160 delete [] msg; |
|
2161 } |
|
2162 else |
|
2163 maybe_page_output (output_buf); |
|
2164 } |
|
2165 |
604
|
2166 if (file); |
|
2167 file.close (); |
|
2168 |
|
2169 DELETE_ARGV; |
|
2170 |
|
2171 return retval; |
|
2172 } |
|
2173 |
|
2174 // Return nonzero if PATTERN has any special globbing chars in it. |
|
2175 |
|
2176 static int |
|
2177 glob_pattern_p (char *pattern) |
|
2178 { |
|
2179 char *p = pattern; |
|
2180 char c; |
|
2181 int open = 0; |
|
2182 |
|
2183 while ((c = *p++) != '\0') |
|
2184 { |
|
2185 switch (c) |
|
2186 { |
|
2187 case '?': |
|
2188 case '*': |
|
2189 return 1; |
|
2190 |
|
2191 case '[': // Only accept an open brace if there is a close |
|
2192 open++; // brace to match it. Bracket expressions must be |
|
2193 continue; // complete, according to Posix.2 |
|
2194 |
|
2195 case ']': |
|
2196 if (open) |
|
2197 return 1; |
|
2198 continue; |
|
2199 |
|
2200 case '\\': |
|
2201 if (*p++ == '\0') |
|
2202 return 0; |
|
2203 |
|
2204 default: |
|
2205 continue; |
|
2206 } |
|
2207 } |
|
2208 |
|
2209 return 0; |
|
2210 } |
|
2211 |
618
|
2212 // MAX_VAL and MIN_VAL are assumed to have integral values even though |
|
2213 // they are stored in doubles. |
|
2214 |
604
|
2215 static save_type |
|
2216 get_save_type (double max_val, double min_val) |
|
2217 { |
|
2218 save_type st = LS_DOUBLE; |
|
2219 |
|
2220 if (max_val < 256 && min_val > -1) |
|
2221 st = LS_U_CHAR; |
|
2222 else if (max_val < 65536 && min_val > -1) |
|
2223 st = LS_U_SHORT; |
618
|
2224 else if (max_val < 4294967295 && min_val > -1) |
|
2225 st = LS_U_INT; |
|
2226 else if (max_val < 128 && min_val >= -128) |
|
2227 st = LS_CHAR; |
604
|
2228 else if (max_val < 32768 && min_val >= -32768) |
|
2229 st = LS_SHORT; |
|
2230 else if (max_val < 2147483648 && min_val > -2147483648) |
|
2231 st = LS_INT; |
|
2232 |
|
2233 return st; |
|
2234 } |
|
2235 |
|
2236 // Save the data from TC along with the corresponding NAME, help |
|
2237 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
|
2238 // binary format described above for load_binary_data. |
|
2239 |
|
2240 static int |
|
2241 save_binary_data (ostream& os, const tree_constant& tc, char *name, |
630
|
2242 char *doc, int mark_as_global, int save_as_floats) |
604
|
2243 { |
620
|
2244 int fail = 0; |
|
2245 |
604
|
2246 FOUR_BYTE_INT name_len = 0; |
|
2247 if (name) |
|
2248 name_len = strlen (name); |
|
2249 |
|
2250 os.write (&name_len, 4); |
|
2251 os.write (name, name_len); |
|
2252 |
|
2253 FOUR_BYTE_INT doc_len = 0; |
|
2254 if (doc) |
|
2255 doc_len = strlen (doc); |
|
2256 |
|
2257 os.write (&doc_len, 4); |
|
2258 os.write (doc, doc_len); |
|
2259 |
|
2260 char tmp; |
|
2261 |
|
2262 tmp = mark_as_global; |
|
2263 os.write (&tmp, 1); |
|
2264 |
620
|
2265 if (tc.is_real_scalar ()) |
604
|
2266 { |
|
2267 tmp = 1; |
|
2268 os.write (&tmp, 1); |
630
|
2269 tmp = (char) LS_DOUBLE; |
|
2270 os.write (&tmp, 1); |
604
|
2271 double tmp = tc.double_value (); |
|
2272 os.write (&tmp, 8); |
|
2273 } |
620
|
2274 else if (tc.is_real_matrix ()) |
604
|
2275 { |
|
2276 tmp = 2; |
|
2277 os.write (&tmp, 1); |
|
2278 Matrix m = tc.matrix_value (); |
|
2279 FOUR_BYTE_INT nr = m.rows (); |
|
2280 FOUR_BYTE_INT nc = m.columns (); |
|
2281 os.write (&nr, 4); |
|
2282 os.write (&nc, 4); |
|
2283 int len = nr * nc; |
|
2284 save_type st = LS_DOUBLE; |
630
|
2285 if (save_as_floats) |
|
2286 { |
|
2287 if (too_large_for_float (m)) |
|
2288 { |
|
2289 warning ("save: some values too large to save as floats --"); |
|
2290 warning ("save: saving as doubles instead"); |
|
2291 } |
|
2292 else |
|
2293 st = LS_FLOAT; |
|
2294 } |
|
2295 else if (len > 8192) // XXX FIXME XXX -- make this configurable. |
604
|
2296 { |
|
2297 double max_val, min_val; |
|
2298 if (all_parts_int (m, max_val, min_val)) |
|
2299 st = get_save_type (max_val, min_val); |
|
2300 } |
630
|
2301 const double *mtmp = m.data (); |
604
|
2302 write_doubles (os, mtmp, st, len); |
|
2303 } |
|
2304 else if (tc.is_complex_scalar ()) |
|
2305 { |
|
2306 tmp = 3; |
|
2307 os.write (&tmp, 1); |
630
|
2308 tmp = (char) LS_DOUBLE; |
|
2309 os.write (&tmp, 1); |
604
|
2310 Complex tmp = tc.complex_value (); |
|
2311 os.write (&tmp, 16); |
|
2312 } |
|
2313 else if (tc.is_complex_matrix ()) |
|
2314 { |
|
2315 tmp = 4; |
|
2316 os.write (&tmp, 1); |
|
2317 ComplexMatrix m = tc.complex_matrix_value (); |
|
2318 FOUR_BYTE_INT nr = m.rows (); |
|
2319 FOUR_BYTE_INT nc = m.columns (); |
|
2320 os.write (&nr, 4); |
|
2321 os.write (&nc, 4); |
|
2322 int len = nr * nc; |
|
2323 save_type st = LS_DOUBLE; |
630
|
2324 if (save_as_floats) |
|
2325 { |
|
2326 if (too_large_for_float (m)) |
|
2327 { |
|
2328 warning ("save: some values too large to save as floats --"); |
|
2329 warning ("save: saving as doubles instead"); |
|
2330 } |
|
2331 else |
|
2332 st = LS_FLOAT; |
|
2333 } |
|
2334 else if (len > 4096) // XXX FIXME XXX -- make this configurable. |
604
|
2335 { |
|
2336 double max_val, min_val; |
|
2337 if (all_parts_int (m, max_val, min_val)) |
|
2338 st = get_save_type (max_val, min_val); |
|
2339 } |
630
|
2340 const Complex *mtmp = m.data (); |
|
2341 write_doubles (os, (const double *) mtmp, st, 2*len); |
604
|
2342 } |
|
2343 else if (tc.is_string ()) |
|
2344 { |
|
2345 tmp = 5; |
|
2346 os.write (&tmp, 1); |
|
2347 int nr = tc.rows (); |
|
2348 int nc = tc.columns (); |
|
2349 FOUR_BYTE_INT len = nr * nc; |
|
2350 os.write (&len, 4); |
|
2351 char *s = tc.string_value (); |
|
2352 os.write (s, len); |
|
2353 } |
|
2354 else if (tc.is_range ()) |
|
2355 { |
|
2356 tmp = 6; |
|
2357 os.write (&tmp, 1); |
630
|
2358 tmp = (char) LS_DOUBLE; |
|
2359 os.write (&tmp, 1); |
604
|
2360 Range r = tc.range_value (); |
|
2361 double bas = r.base (); |
|
2362 double lim = r.limit (); |
|
2363 double inc = r.inc (); |
|
2364 os.write (&bas, 8); |
|
2365 os.write (&lim, 8); |
|
2366 os.write (&inc, 8); |
|
2367 } |
|
2368 else |
620
|
2369 { |
|
2370 gripe_wrong_type_arg ("save", tc); |
|
2371 fail = 1; |
|
2372 } |
604
|
2373 |
620
|
2374 return (os && ! fail); |
604
|
2375 } |
|
2376 |
667
|
2377 // Save the data from TC along with the corresponding NAME on stream OS |
|
2378 // in the MatLab binary format. |
|
2379 |
|
2380 static int |
|
2381 save_mat_binary_data (ostream& os, const tree_constant& tc, char *name) |
|
2382 { |
|
2383 int fail = 0; |
|
2384 |
|
2385 FOUR_BYTE_INT mopt = 0; |
|
2386 |
|
2387 mopt += tc.is_string () ? 1 : 0; |
|
2388 mopt += 1000 * get_floating_point_format (NATIVE_FLOAT_FORMAT); |
|
2389 |
|
2390 os.write (&mopt, 4); |
|
2391 |
|
2392 FOUR_BYTE_INT nr = tc.rows (); |
|
2393 os.write (&nr, 4); |
|
2394 |
|
2395 FOUR_BYTE_INT nc = tc.columns (); |
|
2396 os.write (&nc, 4); |
|
2397 |
|
2398 int len = nr * nc; |
|
2399 |
|
2400 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; |
|
2401 os.write (&imag, 4); |
|
2402 |
|
2403 FOUR_BYTE_INT name_len = name ? strlen (name) + 1 : 0; |
|
2404 |
|
2405 os.write (&name_len, 4); |
|
2406 os.write (name, name_len); |
|
2407 |
|
2408 if (tc.is_real_scalar ()) |
|
2409 { |
|
2410 double tmp = tc.double_value (); |
|
2411 os.write (&tmp, 8); |
|
2412 } |
|
2413 else if (tc.is_real_matrix ()) |
|
2414 { |
|
2415 Matrix m = tc.matrix_value (); |
|
2416 os.write (m.data (), 8 * len); |
|
2417 } |
|
2418 else if (tc.is_complex_scalar ()) |
|
2419 { |
|
2420 Complex tmp = tc.complex_value (); |
|
2421 os.write (&tmp, 16); |
|
2422 } |
|
2423 else if (tc.is_complex_matrix ()) |
|
2424 { |
|
2425 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
2426 Matrix m = ::real(m_cmplx); |
|
2427 os.write (m.data (), 8 * len); |
|
2428 m = ::imag(m_cmplx); |
|
2429 os.write (m.data (), 8 * len); |
|
2430 } |
|
2431 else if (tc.is_string ()) |
|
2432 { |
|
2433 begin_unwind_frame ("save_mat_binary_data"); |
|
2434 unwind_protect_int (user_pref.implicit_str_to_num_ok); |
|
2435 user_pref.implicit_str_to_num_ok = 1; |
|
2436 Matrix m = tc.matrix_value (); |
|
2437 os.write (m.data (), 8 * len); |
|
2438 run_unwind_frame ("save_mat_binary_data"); |
|
2439 } |
|
2440 else |
|
2441 { |
|
2442 gripe_wrong_type_arg ("save", tc); |
|
2443 fail = 1; |
|
2444 } |
|
2445 |
|
2446 return (os && ! fail); |
|
2447 } |
|
2448 |
620
|
2449 static void |
|
2450 ascii_save_type (ostream& os, char *type, int mark_as_global) |
|
2451 { |
|
2452 if (mark_as_global) |
|
2453 os << "# type: global "; |
|
2454 else |
|
2455 os << "# type: "; |
|
2456 |
|
2457 os << type << "\n"; |
|
2458 } |
|
2459 |
|
2460 // Save the data from TC along with the corresponding NAME, and global |
604
|
2461 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
|
2462 // above for load_ascii_data. If NAME is null, the name: line is not |
|
2463 // generated. PRECISION specifies the number of decimal digits to print. |
|
2464 |
|
2465 // XXX FIXME XXX -- should probably write the help string here too. |
|
2466 |
|
2467 int |
620
|
2468 save_ascii_data (ostream& os, const tree_constant& tc, |
604
|
2469 char *name, int mark_as_global, int precision) |
|
2470 { |
620
|
2471 int fail = 0; |
|
2472 |
604
|
2473 if (! precision) |
|
2474 precision = user_pref.save_precision; |
|
2475 |
|
2476 if (name) |
|
2477 os << "# name: " << name << "\n"; |
|
2478 |
|
2479 long old_precision = os.precision (); |
|
2480 os.precision (precision); |
|
2481 |
620
|
2482 if (tc.is_real_scalar ()) |
|
2483 { |
|
2484 ascii_save_type (os, "scalar", mark_as_global); |
|
2485 os << tc.double_value () << "\n"; |
|
2486 } |
|
2487 else if (tc.is_real_matrix ()) |
|
2488 { |
|
2489 ascii_save_type (os, "matrix", mark_as_global); |
|
2490 os << "# rows: " << tc.rows () << "\n" |
|
2491 << "# columns: " << tc.columns () << "\n" |
|
2492 << tc.matrix_value () ; |
|
2493 } |
|
2494 else if (tc.is_complex_scalar ()) |
|
2495 { |
|
2496 ascii_save_type (os, "complex scalar", mark_as_global); |
|
2497 os << tc.complex_value () << "\n"; |
|
2498 } |
|
2499 else if (tc.is_complex_matrix ()) |
604
|
2500 { |
620
|
2501 ascii_save_type (os, "complex matrix", mark_as_global); |
|
2502 os << "# rows: " << tc.rows () << "\n" |
|
2503 << "# columns: " << tc.columns () << "\n" |
|
2504 << tc.complex_matrix_value () ; |
|
2505 } |
|
2506 else if (tc.is_string ()) |
|
2507 { |
|
2508 ascii_save_type (os, "string", mark_as_global); |
|
2509 char *tmp = tc.string_value (); |
|
2510 os << "# length: " << strlen (tmp) << "\n" |
|
2511 << tmp << "\n"; |
|
2512 } |
|
2513 else if (tc.is_string ()) |
|
2514 { |
|
2515 ascii_save_type (os, "range", mark_as_global); |
|
2516 Range tmp = tc.range_value (); |
|
2517 os << "# base, limit, increment\n" |
|
2518 << tmp.base () << " " |
|
2519 << tmp.limit () << " " |
|
2520 << tmp.inc () << "\n"; |
|
2521 } |
|
2522 else |
|
2523 { |
|
2524 gripe_wrong_type_arg ("save", tc); |
|
2525 fail = 1; |
604
|
2526 } |
|
2527 |
|
2528 os.precision (old_precision); |
|
2529 |
620
|
2530 return (os && ! fail); |
604
|
2531 } |
|
2532 |
|
2533 // Save the info from sr on stream os in the format specified by fmt. |
|
2534 |
|
2535 static void |
630
|
2536 do_save (ostream& os, symbol_record *sr, load_save_format fmt, |
|
2537 int save_as_floats) |
604
|
2538 { |
|
2539 if (! sr->is_variable ()) |
|
2540 { |
|
2541 error ("save: can only save variables, not functions"); |
|
2542 return; |
|
2543 } |
|
2544 |
|
2545 char *name = sr->name (); |
|
2546 char *help = sr->help (); |
|
2547 int global = sr->is_linked_to_global (); |
|
2548 tree_constant tc = *((tree_constant *) sr->def ()); |
|
2549 |
|
2550 if (! name || ! tc.is_defined ()) |
|
2551 return; |
|
2552 |
|
2553 switch (fmt) |
|
2554 { |
|
2555 case LS_ASCII: |
|
2556 save_ascii_data (os, tc, name, global); |
|
2557 break; |
|
2558 |
|
2559 case LS_BINARY: |
630
|
2560 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
2561 break; |
|
2562 |
667
|
2563 case LS_MAT_BINARY: |
|
2564 save_mat_binary_data (os, tc, name); |
|
2565 break; |
|
2566 |
604
|
2567 default: |
774
|
2568 error ("save: unrecognized data format requested"); |
604
|
2569 break; |
|
2570 } |
|
2571 } |
|
2572 |
|
2573 // Save variables with names matching PATTERN on stream OS in the |
|
2574 // format specified by FMT. If SAVE_BUILTINS is nonzero, also save |
|
2575 // builtin variables with names that match PATTERN. |
|
2576 |
|
2577 static int |
|
2578 save_vars (ostream& os, char *pattern, int save_builtins, |
630
|
2579 load_save_format fmt, int save_as_floats) |
604
|
2580 { |
|
2581 int count; |
|
2582 |
|
2583 symbol_record **vars = curr_sym_tab->glob |
|
2584 (count, pattern, symbol_def::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2585 |
|
2586 int saved = count; |
|
2587 |
|
2588 int i; |
|
2589 |
|
2590 for (i = 0; i < count; i++) |
620
|
2591 { |
630
|
2592 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2593 |
|
2594 if (error_state) |
|
2595 break; |
|
2596 } |
604
|
2597 |
|
2598 delete [] vars; |
|
2599 |
620
|
2600 if (! error_state && save_builtins) |
604
|
2601 { |
|
2602 symbol_record **vars = global_sym_tab->glob |
|
2603 (count, pattern, symbol_def::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2604 |
|
2605 saved += count; |
|
2606 |
|
2607 for (i = 0; i < count; i++) |
620
|
2608 { |
630
|
2609 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2610 |
|
2611 if (error_state) |
|
2612 break; |
|
2613 } |
604
|
2614 |
|
2615 delete [] vars; |
|
2616 } |
|
2617 |
|
2618 return saved; |
|
2619 } |
|
2620 |
|
2621 static load_save_format |
|
2622 get_default_save_format (void) |
|
2623 { |
|
2624 load_save_format retval = LS_ASCII; |
|
2625 |
|
2626 char *fmt = user_pref.default_save_format; |
|
2627 |
|
2628 if (strcasecmp (fmt, "binary") == 0) |
|
2629 retval = LS_BINARY; |
|
2630 |
|
2631 return retval; |
|
2632 } |
|
2633 |
|
2634 DEFUN_TEXT ("save", Fsave, Ssave, -1, 1, |
667
|
2635 "save [-ascii] [-binary] [-float-binary] [-mat-binary] \ |
|
2636 [-save-builtins] file [pattern ...]\n\ |
604
|
2637 \n\ |
|
2638 save variables in a file") |
|
2639 { |
|
2640 Octave_object retval; |
|
2641 |
|
2642 DEFINE_ARGV("save"); |
|
2643 |
|
2644 argc--; |
|
2645 argv++; |
|
2646 |
|
2647 // Here is where we would get the default save format if it were |
|
2648 // stored in a user preference variable. |
|
2649 |
|
2650 int save_builtins = 0; |
|
2651 |
630
|
2652 int save_as_floats = 0; |
|
2653 |
604
|
2654 load_save_format format = get_default_save_format (); |
|
2655 |
|
2656 while (argc > 0) |
|
2657 { |
|
2658 if (strcmp (*argv, "-ascii") == 0 || strcmp (*argv, "-a") == 0) |
|
2659 { |
|
2660 format = LS_ASCII; |
|
2661 argc--; |
|
2662 argv++; |
|
2663 } |
|
2664 else if (strcmp (*argv, "-binary") == 0 || strcmp (*argv, "-b") == 0) |
|
2665 { |
|
2666 format = LS_BINARY; |
|
2667 argc--; |
|
2668 argv++; |
|
2669 } |
667
|
2670 else if (strcmp (*argv, "-mat-binary") == 0 || strcmp (*argv, "-m") == 0) |
|
2671 { |
|
2672 format = LS_MAT_BINARY; |
|
2673 argc--; |
|
2674 argv++; |
|
2675 } |
630
|
2676 else if (strcmp (*argv, "-float-binary") == 0 |
|
2677 || strcmp (*argv, "-f") == 0) |
|
2678 { |
|
2679 format = LS_BINARY; |
|
2680 save_as_floats = 1; |
|
2681 argc--; |
|
2682 argv++; |
|
2683 } |
604
|
2684 else if (strcmp (*argv, "-save-builtins") == 0) |
|
2685 { |
|
2686 save_builtins = 1; |
|
2687 argc--; |
|
2688 argv++; |
|
2689 } |
|
2690 else |
|
2691 break; |
|
2692 } |
|
2693 |
|
2694 if (argc < 1) |
|
2695 { |
|
2696 print_usage ("save"); |
|
2697 DELETE_ARGV; |
|
2698 return retval; |
|
2699 } |
|
2700 |
630
|
2701 if (save_as_floats && format == LS_ASCII) |
|
2702 { |
|
2703 error ("save: cannot specify both -ascii and -float-binary"); |
|
2704 DELETE_ARGV; |
|
2705 return retval; |
|
2706 } |
|
2707 |
767
|
2708 // Not declaring these static causes trouble on some systems with |
|
2709 // g++/libg++ iostream. Hmm. |
|
2710 |
|
2711 static ostream stream; |
|
2712 static ofstream file; |
|
2713 |
604
|
2714 if (strcmp (*argv, "-") == 0) |
|
2715 { |
|
2716 // XXX FIXME XXX -- should things intended for the screen end up in a |
|
2717 // tree_constant (string)? |
|
2718 stream = cout; |
|
2719 } |
|
2720 else if (argc == 1 && glob_pattern_p (*argv)) // Guard against things |
|
2721 { // like `save a*', |
|
2722 print_usage ("save"); // which are probably |
|
2723 DELETE_ARGV; // mistakes... |
|
2724 return retval; |
|
2725 } |
|
2726 else |
|
2727 { |
|
2728 char *fname = tilde_expand (*argv); |
|
2729 |
|
2730 argc--; |
|
2731 argv++; |
|
2732 |
|
2733 unsigned mode = ios::in; |
|
2734 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2735 mode |= ios::bin; |
|
2736 |
|
2737 file.open (fname); |
|
2738 |
|
2739 if (! file) |
|
2740 { |
|
2741 error ("save: couldn't open output file `%s'", *argv); |
|
2742 DELETE_ARGV; |
|
2743 return retval; |
|
2744 } |
|
2745 stream = file; |
|
2746 |
|
2747 } |
|
2748 |
|
2749 if (format == LS_BINARY) |
|
2750 { |
|
2751 #if defined (WORDS_BIGENDIAN) |
|
2752 stream << "Octave-1-B"; |
|
2753 #else |
|
2754 stream << "Octave-1-L"; |
|
2755 #endif |
660
|
2756 |
|
2757 char tmp = (char) NATIVE_FLOAT_FORMAT; |
|
2758 stream.write (&tmp, 1); |
604
|
2759 } |
|
2760 |
|
2761 if (argc == 0) |
|
2762 { |
630
|
2763 save_vars (stream, "*", save_builtins, format, save_as_floats); |
604
|
2764 } |
|
2765 else |
|
2766 { |
|
2767 while (argc-- > 0) |
|
2768 { |
630
|
2769 if (! save_vars (stream, *argv, save_builtins, format, |
|
2770 save_as_floats)) |
|
2771 { |
|
2772 warning ("save: no such variable `%s'", *argv); |
|
2773 } |
604
|
2774 |
|
2775 argv++; |
|
2776 } |
|
2777 } |
|
2778 |
|
2779 if (file); |
|
2780 file.close (); |
|
2781 |
|
2782 DELETE_ARGV; |
|
2783 |
|
2784 return retval; |
|
2785 } |
|
2786 |
|
2787 // Maybe this should be a static function in tree-plot.cc? |
|
2788 |
620
|
2789 // If TC is matrix, save it on stream OS in a format useful for |
604
|
2790 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
2791 // nonzero, assume a parametric 3-dimensional plot will be generated. |
|
2792 |
|
2793 int |
620
|
2794 save_three_d (ostream& os, const tree_constant& tc, int parametric) |
604
|
2795 { |
620
|
2796 int fail = 0; |
604
|
2797 |
620
|
2798 int nr = tc.rows (); |
|
2799 int nc = tc.columns (); |
|
2800 |
|
2801 if (tc.is_real_matrix ()) |
604
|
2802 { |
|
2803 os << "# 3D data...\n" |
|
2804 << "# type: matrix\n" |
|
2805 << "# total rows: " << nr << "\n" |
|
2806 << "# total columns: " << nc << "\n"; |
|
2807 |
|
2808 if (parametric) |
|
2809 { |
|
2810 int extras = nc % 3; |
|
2811 if (extras) |
|
2812 warning ("ignoring last %d columns", extras); |
|
2813 |
620
|
2814 Matrix tmp = tc.matrix_value (); |
604
|
2815 for (int i = 0; i < nc-extras; i += 3) |
|
2816 { |
|
2817 os << tmp.extract (0, i, nr-1, i+2); |
|
2818 if (i+3 < nc-extras) |
|
2819 os << "\n"; |
|
2820 } |
|
2821 } |
|
2822 else |
|
2823 { |
620
|
2824 Matrix tmp = tc.matrix_value (); |
604
|
2825 for (int i = 0; i < nc; i++) |
|
2826 { |
|
2827 os << tmp.extract (0, i, nr-1, i); |
|
2828 if (i+1 < nc) |
|
2829 os << "\n"; |
|
2830 } |
|
2831 } |
620
|
2832 } |
|
2833 else |
|
2834 { |
604
|
2835 ::error ("for now, I can only save real matrices in 3D format"); |
620
|
2836 fail = 1; |
604
|
2837 } |
620
|
2838 |
|
2839 return (os && ! fail); |
604
|
2840 } |
|
2841 |
|
2842 /* |
|
2843 ;;; Local Variables: *** |
|
2844 ;;; mode: C++ *** |
|
2845 ;;; page-delimiter: "^/\\*" *** |
|
2846 ;;; End: *** |
|
2847 */ |