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