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