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 |
|
1296 if (extract_keyword (is, "rows", nr) && nr > 0 |
|
1297 && extract_keyword (is, "columns", nc) && nc > 0) |
|
1298 { |
|
1299 Matrix tmp (nr, nc); |
|
1300 is >> tmp; |
|
1301 if (is) |
|
1302 tc = tmp; |
|
1303 else |
|
1304 error ("load: failed to load matrix constant"); |
|
1305 } |
|
1306 else |
|
1307 error ("load: failed to extract number of rows and columns"); |
|
1308 } |
|
1309 else if (strncmp (ptr, "complex scalar", 14) == 0) |
|
1310 { |
|
1311 Complex tmp; |
|
1312 is >> tmp; |
|
1313 if (is) |
|
1314 tc = tmp; |
|
1315 else |
|
1316 error ("load: failed to load complex scalar constant"); |
|
1317 } |
|
1318 else if (strncmp (ptr, "complex matrix", 14) == 0) |
|
1319 { |
|
1320 int nr = 0, nc = 0; |
|
1321 |
|
1322 if (extract_keyword (is, "rows", nr) && nr > 0 |
|
1323 && extract_keyword (is, "columns", nc) && nc > 0) |
|
1324 { |
|
1325 ComplexMatrix tmp (nr, nc); |
|
1326 is >> tmp; |
|
1327 if (is) |
|
1328 tc = tmp; |
|
1329 else |
|
1330 error ("load: failed to load complex matrix constant"); |
|
1331 } |
|
1332 else |
|
1333 error ("load: failed to extract number of rows and columns"); |
|
1334 } |
|
1335 else if (strncmp (ptr, "string", 6) == 0) |
|
1336 { |
|
1337 int len; |
|
1338 if (extract_keyword (is, "length", len) && len > 0) |
|
1339 { |
|
1340 char *tmp = new char [len+1]; |
|
1341 is.get (tmp, len+1, EOF); |
|
1342 if (is) |
|
1343 tc = tmp; |
|
1344 else |
|
1345 error ("load: failed to load string constant"); |
|
1346 } |
|
1347 else |
|
1348 error ("load: failed to extract string length"); |
|
1349 } |
|
1350 else if (strncmp (ptr, "range", 5) == 0) |
|
1351 { |
|
1352 // # base, limit, range comment added by save(). |
|
1353 skip_comments (is); |
|
1354 Range tmp; |
|
1355 is >> tmp; |
|
1356 if (is) |
|
1357 tc = tmp; |
|
1358 else |
|
1359 error ("load: failed to load range constant"); |
|
1360 } |
|
1361 else |
|
1362 error ("load: unknown constant type `%s'", tag); |
|
1363 } |
|
1364 else |
|
1365 error ("load: failed to extract keyword specifying value type"); |
|
1366 |
|
1367 delete [] tag; |
|
1368 |
|
1369 if (error_state) |
|
1370 { |
|
1371 error ("load: reading file %s", filename); |
|
1372 return 0; |
|
1373 } |
|
1374 |
|
1375 return name; |
|
1376 } |
|
1377 |
|
1378 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
1379 // place it in TC, returning the name of the variable. If the value |
|
1380 // is tagged as global in the file, return nonzero in GLOBAL. If SWAP |
|
1381 // is nonzero, swap bytes after reading. |
|
1382 // |
|
1383 // The data is expected to be in the following format: |
|
1384 // |
867
|
1385 // Header (one per file): |
|
1386 // ===================== |
604
|
1387 // |
867
|
1388 // object type bytes |
|
1389 // ------ ---- ----- |
|
1390 // magic number string 10 |
604
|
1391 // |
867
|
1392 // float format integer 1 |
|
1393 // |
604
|
1394 // |
867
|
1395 // Data (one set for each item): |
|
1396 // ============================ |
604
|
1397 // |
867
|
1398 // object type bytes |
|
1399 // ------ ---- ----- |
|
1400 // name_length integer 4 |
604
|
1401 // |
867
|
1402 // name string name_length |
604
|
1403 // |
867
|
1404 // doc_length integer 4 |
|
1405 // |
|
1406 // doc string doc_length |
604
|
1407 // |
867
|
1408 // global flag integer 1 |
604
|
1409 // |
867
|
1410 // data type integer 1 |
604
|
1411 // |
867
|
1412 // data (one of): |
|
1413 // |
|
1414 // scalar: |
|
1415 // data real 8 |
604
|
1416 // |
867
|
1417 // complex scalar: |
|
1418 // data complex 16 |
|
1419 // |
|
1420 // matrix: |
|
1421 // rows integer 4 |
|
1422 // columns integer 4 |
|
1423 // data real r*c*8 |
604
|
1424 // |
867
|
1425 // complex matrix: |
|
1426 // rows integer 4 |
|
1427 // columns integer 4 |
|
1428 // data complex r*c*16 |
604
|
1429 // |
867
|
1430 // string: |
|
1431 // length int 4 |
|
1432 // data string length |
604
|
1433 // |
867
|
1434 // range: |
|
1435 // base real 8 |
|
1436 // limit real 8 |
|
1437 // increment real 8 |
604
|
1438 // |
|
1439 // FILENAME is used for error messages. |
|
1440 |
|
1441 static char * |
|
1442 read_binary_data (istream& is, int swap, floating_point_format fmt, |
|
1443 const char *filename, int& global, |
|
1444 tree_constant& tc, char *&doc) |
|
1445 { |
|
1446 char tmp = 0; |
|
1447 |
|
1448 FOUR_BYTE_INT name_len = 0, doc_len = 0; |
|
1449 char *name = 0; |
|
1450 |
|
1451 doc = 0; |
|
1452 |
867
|
1453 // We expect to fail here, at the beginning of a record, so not being |
|
1454 // able to read another name should not result in an error. |
|
1455 |
604
|
1456 is.read (&name_len, 4); |
|
1457 if (! is) |
867
|
1458 return 0; |
604
|
1459 if (swap) |
|
1460 swap_4_bytes ((char *) &name_len); |
|
1461 |
|
1462 name = new char [name_len+1]; |
|
1463 name[name_len] = '\0'; |
|
1464 if (! is.read (name, name_len)) |
|
1465 goto data_read_error; |
|
1466 |
|
1467 is.read (&doc_len, 4); |
|
1468 if (! is) |
|
1469 goto data_read_error; |
|
1470 if (swap) |
|
1471 swap_4_bytes ((char *) &doc_len); |
|
1472 |
|
1473 doc = new char [doc_len+1]; |
|
1474 doc[doc_len] = '\0'; |
|
1475 if (! is.read (doc, doc_len)) |
|
1476 goto data_read_error; |
|
1477 |
|
1478 if (! is.read (&tmp, 1)) |
|
1479 goto data_read_error; |
|
1480 global = tmp ? 1 : 0; |
|
1481 |
|
1482 tmp = 0; |
|
1483 if (! is.read (&tmp, 1)) |
|
1484 goto data_read_error; |
|
1485 |
|
1486 switch (tmp) |
|
1487 { |
|
1488 case 1: |
|
1489 { |
630
|
1490 if (! is.read (&tmp, 1)) |
604
|
1491 goto data_read_error; |
630
|
1492 double dtmp; |
|
1493 read_doubles (is, &dtmp, (save_type) tmp, 1, swap, fmt); |
774
|
1494 if (error_state || ! is) |
630
|
1495 goto data_read_error; |
604
|
1496 tc = dtmp; |
|
1497 } |
|
1498 break; |
|
1499 |
|
1500 case 2: |
|
1501 { |
|
1502 FOUR_BYTE_INT nr, nc; |
|
1503 if (! is.read (&nr, 4)) |
|
1504 goto data_read_error; |
|
1505 if (swap) |
|
1506 swap_4_bytes ((char *) &nr); |
|
1507 if (! is.read (&nc, 4)) |
|
1508 goto data_read_error; |
|
1509 if (swap) |
|
1510 swap_4_bytes ((char *) &nc); |
|
1511 if (! is.read (&tmp, 1)) |
|
1512 goto data_read_error; |
|
1513 Matrix m (nr, nc); |
|
1514 double *re = m.fortran_vec (); |
|
1515 int len = nr * nc; |
|
1516 read_doubles (is, re, (save_type) tmp, len, swap, fmt); |
774
|
1517 if (error_state || ! is) |
604
|
1518 goto data_read_error; |
|
1519 tc = m; |
|
1520 } |
|
1521 break; |
|
1522 |
|
1523 case 3: |
|
1524 { |
630
|
1525 if (! is.read (&tmp, 1)) |
604
|
1526 goto data_read_error; |
630
|
1527 Complex ctmp; |
|
1528 read_doubles (is, (double *) &ctmp, (save_type) tmp, 2, swap, fmt); |
774
|
1529 if (error_state || ! is) |
630
|
1530 goto data_read_error; |
604
|
1531 tc = ctmp; |
|
1532 } |
|
1533 break; |
|
1534 |
|
1535 case 4: |
|
1536 { |
|
1537 FOUR_BYTE_INT nr, nc; |
|
1538 if (! is.read (&nr, 4)) |
|
1539 goto data_read_error; |
|
1540 if (swap) |
|
1541 swap_4_bytes ((char *) &nr); |
|
1542 if (! is.read (&nc, 4)) |
|
1543 goto data_read_error; |
|
1544 if (swap) |
|
1545 swap_4_bytes ((char *) &nc); |
|
1546 if (! is.read (&tmp, 1)) |
|
1547 goto data_read_error; |
|
1548 ComplexMatrix m (nr, nc); |
|
1549 Complex *im = m.fortran_vec (); |
|
1550 int len = nr * nc; |
630
|
1551 read_doubles (is, (double *) im, (save_type) tmp, 2*len, |
|
1552 swap, fmt); |
774
|
1553 if (error_state || ! is) |
604
|
1554 goto data_read_error; |
|
1555 tc = m; |
|
1556 } |
|
1557 break; |
|
1558 |
|
1559 case 5: |
|
1560 { |
|
1561 int nr = tc.rows (); |
|
1562 int nc = tc.columns (); |
|
1563 FOUR_BYTE_INT len = nr * nc; |
|
1564 if (! is.read (&len, 4)) |
|
1565 goto data_read_error; |
|
1566 if (swap) |
|
1567 swap_4_bytes ((char *) &len); |
|
1568 char *s = new char [len+1]; |
|
1569 if (! is.read (s, len)) |
|
1570 { |
|
1571 delete [] s; |
|
1572 goto data_read_error; |
|
1573 } |
|
1574 s[len] = '\0'; |
|
1575 tc = s; |
|
1576 } |
|
1577 break; |
|
1578 |
|
1579 case 6: |
|
1580 { |
630
|
1581 if (! is.read (&tmp, 1)) |
|
1582 goto data_read_error; |
604
|
1583 double bas, lim, inc; |
|
1584 if (! is.read (&bas, 8)) |
|
1585 goto data_read_error; |
|
1586 if (swap) |
|
1587 swap_8_bytes ((char *) &bas); |
|
1588 if (! is.read (&lim, 8)) |
|
1589 goto data_read_error; |
|
1590 if (swap) |
|
1591 swap_8_bytes ((char *) &lim); |
|
1592 if (! is.read (&inc, 8)) |
|
1593 goto data_read_error; |
|
1594 if (swap) |
|
1595 swap_8_bytes ((char *) &inc); |
|
1596 Range r (bas, lim, inc); |
|
1597 tc = r; |
|
1598 } |
|
1599 break; |
|
1600 |
|
1601 default: |
|
1602 data_read_error: |
|
1603 error ("load: trouble reading binary file `%s'", filename); |
|
1604 delete [] name; |
|
1605 name = 0; |
|
1606 break; |
|
1607 } |
|
1608 |
|
1609 return name; |
|
1610 } |
|
1611 |
|
1612 // Read LEN elements of data from IS in the format specified by |
|
1613 // PRECISION, placing the result in DATA. If SWAP is nonzero, swap |
|
1614 // the bytes of each element before copying to DATA. FLT_FMT |
|
1615 // specifies the format of the data if we are reading floating point |
|
1616 // numbers. |
|
1617 |
|
1618 static void |
|
1619 read_mat_binary_data (istream& is, double *data, int precision, |
|
1620 int len, int swap, floating_point_format flt_fmt) |
|
1621 { |
|
1622 switch (precision) |
|
1623 { |
|
1624 case 0: |
630
|
1625 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); |
604
|
1626 break; |
|
1627 |
|
1628 case 1: |
630
|
1629 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); |
604
|
1630 break; |
|
1631 |
|
1632 case 2: |
|
1633 read_doubles (is, data, LS_INT, len, swap, flt_fmt); |
|
1634 break; |
|
1635 |
|
1636 case 3: |
|
1637 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); |
|
1638 break; |
|
1639 |
|
1640 case 4: |
|
1641 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); |
|
1642 break; |
|
1643 |
|
1644 case 5: |
|
1645 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); |
|
1646 break; |
|
1647 |
|
1648 default: |
|
1649 break; |
|
1650 } |
|
1651 } |
|
1652 |
|
1653 static int |
|
1654 read_mat_file_header (istream& is, int& swap, FOUR_BYTE_INT& mopt, |
|
1655 FOUR_BYTE_INT& nr, FOUR_BYTE_INT& nc, |
|
1656 FOUR_BYTE_INT& imag, FOUR_BYTE_INT& len, |
|
1657 int quiet = 0) |
|
1658 { |
671
|
1659 swap = 0; |
|
1660 |
911
|
1661 // We expect to fail here, at the beginning of a record, so not being |
|
1662 // able to read another mopt value should not result in an error. |
|
1663 |
604
|
1664 is.read (&mopt, 4); |
|
1665 if (! is) |
911
|
1666 return 1; |
604
|
1667 |
|
1668 if (! is.read (&nr, 4)) |
|
1669 goto data_read_error; |
|
1670 |
|
1671 if (! is.read (&nc, 4)) |
|
1672 goto data_read_error; |
|
1673 |
|
1674 if (! is.read (&imag, 4)) |
|
1675 goto data_read_error; |
|
1676 |
|
1677 if (! is.read (&len, 4)) |
|
1678 goto data_read_error; |
|
1679 |
|
1680 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
1681 // bigger than we expect, so we swap bytes. |
|
1682 // |
|
1683 // If mopt is zero, it means the file was written on a little endian |
|
1684 // machine, and we only need to swap if we are running on a big endian |
|
1685 // machine. |
|
1686 // |
|
1687 // Gag me. |
|
1688 |
|
1689 #if defined (WORDS_BIGENDIAN) |
|
1690 if (mopt == 0) |
|
1691 swap = 1; |
|
1692 #endif |
|
1693 |
911
|
1694 // mopt is signed, therefore byte swap may result in negative value. |
|
1695 |
|
1696 if (mopt > 9999 || mopt < 0) |
604
|
1697 swap = 1; |
|
1698 |
|
1699 if (swap) |
|
1700 { |
|
1701 swap_4_bytes ((char *) &mopt); |
|
1702 swap_4_bytes ((char *) &nr); |
|
1703 swap_4_bytes ((char *) &nc); |
|
1704 swap_4_bytes ((char *) &imag); |
|
1705 swap_4_bytes ((char *) &len); |
|
1706 } |
|
1707 |
911
|
1708 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) |
604
|
1709 { |
|
1710 if (! quiet) |
|
1711 error ("load: can't read binary file"); |
|
1712 return -1; |
|
1713 } |
|
1714 |
|
1715 return 0; |
|
1716 |
|
1717 data_read_error: |
|
1718 return -1; |
|
1719 } |
|
1720 |
617
|
1721 // We don't just use a cast here, because we need to be able to detect |
|
1722 // possible errors. |
|
1723 |
|
1724 static floating_point_format |
|
1725 get_floating_point_format (int mach) |
|
1726 { |
1226
|
1727 floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; |
619
|
1728 |
617
|
1729 switch (mach) |
|
1730 { |
|
1731 case 0: |
1226
|
1732 flt_fmt = OCTAVE_IEEE_LITTLE; |
617
|
1733 break; |
|
1734 |
|
1735 case 1: |
1226
|
1736 flt_fmt = OCTAVE_IEEE_BIG; |
617
|
1737 break; |
|
1738 |
|
1739 case 2: |
1226
|
1740 flt_fmt = OCTAVE_VAX_D; |
617
|
1741 break; |
|
1742 |
|
1743 case 3: |
1226
|
1744 flt_fmt = OCTAVE_VAX_G; |
617
|
1745 break; |
|
1746 |
|
1747 case 4: |
1226
|
1748 flt_fmt = OCTAVE_CRAY; |
617
|
1749 break; |
|
1750 |
|
1751 default: |
1226
|
1752 flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; |
617
|
1753 break; |
|
1754 } |
619
|
1755 |
|
1756 return flt_fmt; |
617
|
1757 } |
619
|
1758 |
604
|
1759 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
1760 // place it in TC, returning the name of the variable. |
|
1761 // |
|
1762 // The data is expected to be in Matlab's .mat format, though not all |
|
1763 // the features of that format are supported. |
|
1764 // |
|
1765 // FILENAME is used for error messages. |
|
1766 // |
|
1767 // This format provides no way to tag the data as global. |
|
1768 |
|
1769 static char * |
|
1770 read_mat_binary_data (istream& is, const char *filename, |
|
1771 tree_constant& tc) |
|
1772 { |
|
1773 // These are initialized here instead of closer to where they are |
|
1774 // first used to avoid errors from gcc about goto crossing |
|
1775 // initialization of variable. |
|
1776 |
|
1777 Matrix re; |
1226
|
1778 floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; |
604
|
1779 char *name = 0; |
|
1780 int swap = 0, type = 0, prec = 0, mach = 0, dlen = 0; |
|
1781 |
|
1782 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
1783 |
|
1784 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
1785 if (err) |
|
1786 { |
|
1787 if (err < 0) |
|
1788 goto data_read_error; |
|
1789 else |
|
1790 return 0; |
|
1791 } |
|
1792 |
|
1793 type = mopt % 10; // Full, sparse, etc. |
|
1794 mopt /= 10; // Eliminate first digit. |
|
1795 prec = mopt % 10; // double, float, int, etc. |
|
1796 mopt /= 100; // Skip unused third digit too. |
|
1797 mach = mopt % 10; // IEEE, VAX, etc. |
|
1798 |
617
|
1799 flt_fmt = get_floating_point_format (mach); |
1226
|
1800 if (flt_fmt == OCTAVE_UNKNOWN_FLT_FMT) |
604
|
1801 { |
|
1802 error ("load: unrecognized binary format!"); |
|
1803 return 0; |
|
1804 } |
|
1805 |
|
1806 if (type != 0 && type != 1) |
|
1807 { |
|
1808 error ("load: can't read sparse matrices"); |
|
1809 return 0; |
|
1810 } |
|
1811 |
|
1812 if (imag && type == 1) |
|
1813 { |
|
1814 error ("load: encountered complex matrix with string flag set!"); |
|
1815 return 0; |
|
1816 } |
|
1817 |
|
1818 name = new char [len]; |
|
1819 if (! is.read (name, len)) |
|
1820 goto data_read_error; |
|
1821 |
|
1822 dlen = nr * nc; |
|
1823 if (dlen < 0) |
|
1824 goto data_read_error; |
|
1825 |
|
1826 re.resize (nr, nc); |
|
1827 |
|
1828 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1829 |
|
1830 if (! is || error_state) |
|
1831 { |
|
1832 error ("load: reading matrix data for `%s'", name); |
|
1833 goto data_read_error; |
|
1834 } |
|
1835 |
|
1836 if (imag) |
|
1837 { |
|
1838 Matrix im (nr, nc); |
|
1839 |
|
1840 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1841 |
|
1842 if (! is || error_state) |
|
1843 { |
|
1844 error ("load: reading imaginary matrix data for `%s'", name); |
|
1845 goto data_read_error; |
|
1846 } |
|
1847 |
|
1848 ComplexMatrix ctmp (nr, nc); |
|
1849 |
|
1850 for (int j = 0; j < nc; j++) |
|
1851 for (int i = 0; i < nr; i++) |
|
1852 ctmp.elem (i, j) = Complex (re.elem (i, j), im.elem (i, j)); |
|
1853 |
|
1854 tc = ctmp; |
|
1855 } |
|
1856 else |
|
1857 tc = re; |
|
1858 |
1097
|
1859 // XXX FIXME XXX -- this needs to change once strings really work. |
|
1860 |
|
1861 if (type == 1 && nr == 1) |
604
|
1862 tc = tc.convert_to_str (); |
|
1863 |
|
1864 return name; |
|
1865 |
|
1866 data_read_error: |
|
1867 error ("load: trouble reading binary file `%s'", filename); |
|
1868 delete [] name; |
|
1869 return 0; |
|
1870 } |
|
1871 |
|
1872 // Return nonzero if NAME matches one of the given globbing PATTERNS. |
|
1873 |
|
1874 static int |
|
1875 matches_patterns (char **patterns, int num_pat, char *name) |
|
1876 { |
|
1877 while (num_pat-- > 0) |
|
1878 { |
|
1879 if (fnmatch (*patterns++, name, __FNM_FLAGS) == 0) |
|
1880 return 1; |
|
1881 } |
|
1882 return 0; |
|
1883 } |
|
1884 |
|
1885 static int |
|
1886 read_binary_file_header (istream& is, int& swap, |
630
|
1887 floating_point_format& flt_fmt, int quiet = 0) |
604
|
1888 { |
|
1889 int magic_len = 10; |
|
1890 char magic [magic_len+1]; |
|
1891 is.read (magic, magic_len); |
|
1892 magic[magic_len] = '\0'; |
|
1893 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
|
1894 { |
|
1895 #if defined (WORDS_BIGENDIAN) |
|
1896 swap = 1; |
|
1897 #else |
|
1898 swap = 0; |
|
1899 #endif |
|
1900 } |
|
1901 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
|
1902 { |
|
1903 #if defined (WORDS_BIGENDIAN) |
|
1904 swap = 0; |
|
1905 #else |
|
1906 swap = 1; |
|
1907 #endif |
|
1908 } |
|
1909 else |
|
1910 { |
|
1911 if (! quiet) |
|
1912 error ("load: can't read binary file"); |
|
1913 return -1; |
|
1914 } |
|
1915 |
|
1916 char tmp = 0; |
|
1917 is.read (&tmp, 1); |
|
1918 |
617
|
1919 flt_fmt = get_floating_point_format (tmp); |
1226
|
1920 if (flt_fmt == OCTAVE_UNKNOWN_FLT_FMT) |
604
|
1921 { |
|
1922 if (! quiet) |
|
1923 error ("load: unrecognized binary format!"); |
|
1924 return -1; |
|
1925 } |
|
1926 |
|
1927 return 0; |
|
1928 } |
|
1929 |
|
1930 static load_save_format |
|
1931 get_file_format (const char *fname, const char *orig_fname) |
|
1932 { |
|
1933 load_save_format retval = LS_UNKNOWN; |
|
1934 |
984
|
1935 ifstream file (fname); |
604
|
1936 |
|
1937 if (! file) |
|
1938 { |
|
1939 error ("load: couldn't open input file `%s'", orig_fname); |
|
1940 return retval; |
|
1941 } |
|
1942 |
|
1943 int swap; |
1226
|
1944 floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; |
604
|
1945 |
|
1946 if (read_binary_file_header (file, swap, flt_fmt, 1) == 0) |
|
1947 retval = LS_BINARY; |
|
1948 else |
|
1949 { |
|
1950 file.seekg (0, ios::beg); |
|
1951 |
|
1952 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
1180
|
1953 |
|
1954 int err = read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1); |
|
1955 |
|
1956 if (! err) |
604
|
1957 retval = LS_MAT_BINARY; |
|
1958 else |
|
1959 { |
|
1960 file.seekg (0, ios::beg); |
|
1961 |
|
1962 char *tmp = extract_keyword (file, "name"); |
1180
|
1963 |
604
|
1964 if (tmp) |
1180
|
1965 { |
|
1966 retval = LS_ASCII; |
|
1967 delete [] tmp; |
|
1968 } |
604
|
1969 } |
|
1970 } |
|
1971 |
|
1972 file.close (); |
|
1973 |
|
1974 if (retval == LS_UNKNOWN) |
|
1975 error ("load: unable to determine file format for `%s'", orig_fname); |
|
1976 |
|
1977 return retval; |
|
1978 } |
|
1979 |
863
|
1980 static Octave_object |
|
1981 do_load (istream& stream, const char *orig_fname, int force, |
|
1982 load_save_format format, floating_point_format flt_fmt, |
|
1983 int list_only, int swap, int verbose, char **argv, |
|
1984 int argc, int nargout) |
604
|
1985 { |
|
1986 Octave_object retval; |
|
1987 |
621
|
1988 ostrstream output_buf; |
604
|
1989 int count = 0; |
|
1990 for (;;) |
|
1991 { |
|
1992 int global = 0; |
|
1993 tree_constant tc; |
|
1994 |
|
1995 char *name = 0; |
|
1996 char *doc = 0; |
|
1997 |
|
1998 switch (format) |
|
1999 { |
|
2000 case LS_ASCII: |
|
2001 name = read_ascii_data (stream, orig_fname, global, tc); |
|
2002 break; |
|
2003 |
|
2004 case LS_BINARY: |
|
2005 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
2006 global, tc, doc); |
|
2007 break; |
|
2008 |
|
2009 case LS_MAT_BINARY: |
|
2010 name = read_mat_binary_data (stream, orig_fname, tc); |
|
2011 break; |
|
2012 |
|
2013 default: |
775
|
2014 gripe_unrecognized_data_fmt ("load"); |
604
|
2015 break; |
|
2016 } |
|
2017 |
867
|
2018 if (error_state || stream.eof () || ! name) |
604
|
2019 { |
867
|
2020 delete [] name; |
|
2021 delete [] doc; |
604
|
2022 break; |
|
2023 } |
|
2024 else if (! error_state && name) |
|
2025 { |
|
2026 if (tc.is_defined ()) |
|
2027 { |
|
2028 if (argc == 0 || matches_patterns (argv, argc, name)) |
|
2029 { |
|
2030 count++; |
621
|
2031 if (list_only) |
|
2032 { |
|
2033 if (verbose) |
|
2034 { |
|
2035 if (count == 1) |
|
2036 output_buf |
|
2037 << "type rows cols name\n" |
|
2038 << "==== ==== ==== ====\n"; |
|
2039 |
|
2040 output_buf.form ("%-16s", tc.type_as_string ()); |
|
2041 output_buf.form ("%7d", tc.rows ()); |
|
2042 output_buf.form ("%7d", tc.columns ()); |
|
2043 output_buf << " "; |
|
2044 } |
|
2045 output_buf << name << "\n"; |
|
2046 } |
|
2047 else |
|
2048 { |
|
2049 install_loaded_variable (force, name, tc, global, doc); |
|
2050 } |
604
|
2051 } |
|
2052 } |
|
2053 else |
|
2054 error ("load: unable to load variable `%s'", name); |
|
2055 } |
|
2056 else |
|
2057 { |
|
2058 if (count == 0) |
|
2059 error ("load: are you sure `%s' is an Octave data file?", |
|
2060 orig_fname); |
|
2061 |
867
|
2062 delete [] name; |
|
2063 delete [] doc; |
604
|
2064 break; |
|
2065 } |
|
2066 |
|
2067 delete [] name; |
|
2068 delete [] doc; |
|
2069 } |
|
2070 |
621
|
2071 if (list_only && count) |
|
2072 { |
|
2073 if (nargout > 0) |
|
2074 { |
|
2075 output_buf << ends; |
|
2076 char *msg = output_buf.str (); |
|
2077 retval = msg; |
|
2078 delete [] msg; |
|
2079 } |
|
2080 else |
|
2081 maybe_page_output (output_buf); |
|
2082 } |
|
2083 |
863
|
2084 return retval; |
|
2085 } |
|
2086 |
|
2087 DEFUN_TEXT ("load", Fload, Sload, -1, 1, |
910
|
2088 "load [-force] [-ascii] [-binary] [-mat-binary] file [pattern ...]\n\ |
863
|
2089 \n\ |
|
2090 Load variables from a file.\n\ |
|
2091 \n\ |
|
2092 If no argument is supplied to select a format, load tries to read the |
|
2093 named file as an Octave binary, then as a .mat file, and then as an |
|
2094 Octave text file.\n\ |
|
2095 \n\ |
|
2096 If the option -force is given, variables with the same names as those |
|
2097 found in the file will be replaced with the values read from the file.") |
|
2098 { |
|
2099 Octave_object retval; |
|
2100 |
|
2101 DEFINE_ARGV ("load"); |
|
2102 |
|
2103 argc--; |
|
2104 argv++; |
|
2105 |
|
2106 int force = 0; |
|
2107 |
|
2108 // It isn't necessary to have the default load format stored in a user |
|
2109 // preference variable since we can determine the type of file as we |
|
2110 // are reading. |
|
2111 |
|
2112 load_save_format format = LS_UNKNOWN; |
|
2113 |
|
2114 int list_only = 0; |
|
2115 int verbose = 0; |
|
2116 |
|
2117 while (argc > 0) |
|
2118 { |
|
2119 if (strcmp (*argv, "-force") == 0 || strcmp (*argv, "-f") == 0) |
|
2120 { |
|
2121 force++; |
|
2122 argc--; |
|
2123 argv++; |
|
2124 } |
|
2125 else if (strcmp (*argv, "-list") == 0 || strcmp (*argv, "-l") == 0) |
|
2126 { |
|
2127 list_only = 1; |
|
2128 argc--; |
|
2129 argv++; |
|
2130 } |
|
2131 else if (strcmp (*argv, "-verbose") == 0 || strcmp (*argv, "-v") == 0) |
|
2132 { |
|
2133 verbose = 1; |
|
2134 argc--; |
|
2135 argv++; |
|
2136 } |
|
2137 else if (strcmp (*argv, "-ascii") == 0 || strcmp (*argv, "-a") == 0) |
|
2138 { |
|
2139 format = LS_ASCII; |
|
2140 argc--; |
|
2141 argv++; |
|
2142 } |
|
2143 else if (strcmp (*argv, "-binary") == 0 || strcmp (*argv, "-b") == 0) |
|
2144 { |
|
2145 format = LS_BINARY; |
|
2146 argc--; |
|
2147 argv++; |
|
2148 } |
|
2149 else if (strcmp (*argv, "-mat-binary") == 0 || strcmp (*argv, "-m") == 0) |
|
2150 { |
|
2151 format = LS_MAT_BINARY; |
|
2152 argc--; |
|
2153 argv++; |
|
2154 } |
|
2155 else |
|
2156 break; |
|
2157 } |
|
2158 |
|
2159 if (argc < 1) |
|
2160 { |
|
2161 error ("load: you must specify a single file to read"); |
|
2162 DELETE_ARGV; |
|
2163 return retval; |
|
2164 } |
|
2165 |
|
2166 char *orig_fname = *argv; |
|
2167 |
1226
|
2168 floating_point_format flt_fmt = OCTAVE_UNKNOWN_FLT_FMT; |
863
|
2169 |
|
2170 int swap = 0; |
|
2171 |
|
2172 if (strcmp (*argv, "-") == 0) |
|
2173 { |
|
2174 argc--; |
|
2175 argv++; |
|
2176 |
|
2177 if (format != LS_UNKNOWN) |
|
2178 { |
|
2179 // XXX FIXME XXX -- if we have already seen EOF on a previous call, |
|
2180 // how do we fix up the state of cin so that we can get additional |
|
2181 // input? I'm afraid that we can't fix this using cin only. |
|
2182 |
|
2183 retval = do_load (cin, orig_fname, force, format, flt_fmt, |
|
2184 list_only, swap, verbose, argv, argc, |
|
2185 nargout); |
|
2186 } |
|
2187 else |
|
2188 error ("load: must specify file format if reading from stdin"); |
|
2189 } |
|
2190 else |
|
2191 { |
1158
|
2192 static char *fname = 0; |
|
2193 |
|
2194 if (fname) |
|
2195 free (fname); |
|
2196 |
|
2197 fname = tilde_expand (*argv); |
863
|
2198 |
|
2199 if (format == LS_UNKNOWN) |
|
2200 format = get_file_format (fname, orig_fname); |
|
2201 |
|
2202 if (format != LS_UNKNOWN) |
|
2203 { |
|
2204 argv++; |
|
2205 argc--; |
|
2206 |
|
2207 unsigned mode = ios::in; |
|
2208 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2209 mode |= ios::bin; |
|
2210 |
|
2211 ifstream file (fname, mode); |
|
2212 |
|
2213 if (file) |
|
2214 { |
|
2215 if (format == LS_BINARY) |
|
2216 { |
|
2217 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
2218 { |
|
2219 file.close (); |
|
2220 DELETE_ARGV; |
|
2221 return retval; |
|
2222 } |
|
2223 } |
|
2224 |
|
2225 retval = do_load (file, orig_fname, force, format, |
|
2226 flt_fmt, list_only, swap, verbose, |
|
2227 argv, argc, nargout); |
|
2228 |
|
2229 file.close (); |
|
2230 } |
|
2231 else |
|
2232 error ("load: couldn't open input file `%s'", orig_fname); |
|
2233 } |
|
2234 } |
604
|
2235 |
|
2236 DELETE_ARGV; |
|
2237 |
|
2238 return retval; |
|
2239 } |
|
2240 |
|
2241 // Return nonzero if PATTERN has any special globbing chars in it. |
|
2242 |
|
2243 static int |
|
2244 glob_pattern_p (char *pattern) |
|
2245 { |
|
2246 char *p = pattern; |
|
2247 char c; |
|
2248 int open = 0; |
|
2249 |
|
2250 while ((c = *p++) != '\0') |
|
2251 { |
|
2252 switch (c) |
|
2253 { |
|
2254 case '?': |
|
2255 case '*': |
|
2256 return 1; |
|
2257 |
|
2258 case '[': // Only accept an open brace if there is a close |
|
2259 open++; // brace to match it. Bracket expressions must be |
|
2260 continue; // complete, according to Posix.2 |
|
2261 |
|
2262 case ']': |
|
2263 if (open) |
|
2264 return 1; |
|
2265 continue; |
|
2266 |
|
2267 case '\\': |
|
2268 if (*p++ == '\0') |
|
2269 return 0; |
|
2270 |
|
2271 default: |
|
2272 continue; |
|
2273 } |
|
2274 } |
|
2275 |
|
2276 return 0; |
|
2277 } |
|
2278 |
618
|
2279 // MAX_VAL and MIN_VAL are assumed to have integral values even though |
|
2280 // they are stored in doubles. |
|
2281 |
604
|
2282 static save_type |
|
2283 get_save_type (double max_val, double min_val) |
|
2284 { |
|
2285 save_type st = LS_DOUBLE; |
|
2286 |
|
2287 if (max_val < 256 && min_val > -1) |
|
2288 st = LS_U_CHAR; |
|
2289 else if (max_val < 65536 && min_val > -1) |
|
2290 st = LS_U_SHORT; |
618
|
2291 else if (max_val < 4294967295 && min_val > -1) |
|
2292 st = LS_U_INT; |
|
2293 else if (max_val < 128 && min_val >= -128) |
|
2294 st = LS_CHAR; |
604
|
2295 else if (max_val < 32768 && min_val >= -32768) |
|
2296 st = LS_SHORT; |
|
2297 else if (max_val < 2147483648 && min_val > -2147483648) |
|
2298 st = LS_INT; |
|
2299 |
|
2300 return st; |
|
2301 } |
|
2302 |
|
2303 // Save the data from TC along with the corresponding NAME, help |
|
2304 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
|
2305 // binary format described above for load_binary_data. |
|
2306 |
|
2307 static int |
|
2308 save_binary_data (ostream& os, const tree_constant& tc, char *name, |
630
|
2309 char *doc, int mark_as_global, int save_as_floats) |
604
|
2310 { |
620
|
2311 int fail = 0; |
|
2312 |
604
|
2313 FOUR_BYTE_INT name_len = 0; |
|
2314 if (name) |
|
2315 name_len = strlen (name); |
|
2316 |
|
2317 os.write (&name_len, 4); |
|
2318 os.write (name, name_len); |
|
2319 |
|
2320 FOUR_BYTE_INT doc_len = 0; |
|
2321 if (doc) |
|
2322 doc_len = strlen (doc); |
|
2323 |
|
2324 os.write (&doc_len, 4); |
|
2325 os.write (doc, doc_len); |
|
2326 |
|
2327 char tmp; |
|
2328 |
|
2329 tmp = mark_as_global; |
|
2330 os.write (&tmp, 1); |
|
2331 |
620
|
2332 if (tc.is_real_scalar ()) |
604
|
2333 { |
|
2334 tmp = 1; |
|
2335 os.write (&tmp, 1); |
630
|
2336 tmp = (char) LS_DOUBLE; |
|
2337 os.write (&tmp, 1); |
604
|
2338 double tmp = tc.double_value (); |
|
2339 os.write (&tmp, 8); |
|
2340 } |
620
|
2341 else if (tc.is_real_matrix ()) |
604
|
2342 { |
|
2343 tmp = 2; |
|
2344 os.write (&tmp, 1); |
|
2345 Matrix m = tc.matrix_value (); |
|
2346 FOUR_BYTE_INT nr = m.rows (); |
|
2347 FOUR_BYTE_INT nc = m.columns (); |
|
2348 os.write (&nr, 4); |
|
2349 os.write (&nc, 4); |
|
2350 int len = nr * nc; |
|
2351 save_type st = LS_DOUBLE; |
630
|
2352 if (save_as_floats) |
|
2353 { |
|
2354 if (too_large_for_float (m)) |
|
2355 { |
|
2356 warning ("save: some values too large to save as floats --"); |
|
2357 warning ("save: saving as doubles instead"); |
|
2358 } |
|
2359 else |
|
2360 st = LS_FLOAT; |
|
2361 } |
|
2362 else if (len > 8192) // XXX FIXME XXX -- make this configurable. |
604
|
2363 { |
|
2364 double max_val, min_val; |
|
2365 if (all_parts_int (m, max_val, min_val)) |
|
2366 st = get_save_type (max_val, min_val); |
|
2367 } |
630
|
2368 const double *mtmp = m.data (); |
604
|
2369 write_doubles (os, mtmp, st, len); |
|
2370 } |
|
2371 else if (tc.is_complex_scalar ()) |
|
2372 { |
|
2373 tmp = 3; |
|
2374 os.write (&tmp, 1); |
630
|
2375 tmp = (char) LS_DOUBLE; |
|
2376 os.write (&tmp, 1); |
604
|
2377 Complex tmp = tc.complex_value (); |
|
2378 os.write (&tmp, 16); |
|
2379 } |
|
2380 else if (tc.is_complex_matrix ()) |
|
2381 { |
|
2382 tmp = 4; |
|
2383 os.write (&tmp, 1); |
|
2384 ComplexMatrix m = tc.complex_matrix_value (); |
|
2385 FOUR_BYTE_INT nr = m.rows (); |
|
2386 FOUR_BYTE_INT nc = m.columns (); |
|
2387 os.write (&nr, 4); |
|
2388 os.write (&nc, 4); |
|
2389 int len = nr * nc; |
|
2390 save_type st = LS_DOUBLE; |
630
|
2391 if (save_as_floats) |
|
2392 { |
|
2393 if (too_large_for_float (m)) |
|
2394 { |
|
2395 warning ("save: some values too large to save as floats --"); |
|
2396 warning ("save: saving as doubles instead"); |
|
2397 } |
|
2398 else |
|
2399 st = LS_FLOAT; |
|
2400 } |
|
2401 else if (len > 4096) // XXX FIXME XXX -- make this configurable. |
604
|
2402 { |
|
2403 double max_val, min_val; |
|
2404 if (all_parts_int (m, max_val, min_val)) |
|
2405 st = get_save_type (max_val, min_val); |
|
2406 } |
630
|
2407 const Complex *mtmp = m.data (); |
|
2408 write_doubles (os, (const double *) mtmp, st, 2*len); |
604
|
2409 } |
|
2410 else if (tc.is_string ()) |
|
2411 { |
|
2412 tmp = 5; |
|
2413 os.write (&tmp, 1); |
|
2414 int nr = tc.rows (); |
|
2415 int nc = tc.columns (); |
|
2416 FOUR_BYTE_INT len = nr * nc; |
|
2417 os.write (&len, 4); |
|
2418 char *s = tc.string_value (); |
|
2419 os.write (s, len); |
|
2420 } |
|
2421 else if (tc.is_range ()) |
|
2422 { |
|
2423 tmp = 6; |
|
2424 os.write (&tmp, 1); |
630
|
2425 tmp = (char) LS_DOUBLE; |
|
2426 os.write (&tmp, 1); |
604
|
2427 Range r = tc.range_value (); |
|
2428 double bas = r.base (); |
|
2429 double lim = r.limit (); |
|
2430 double inc = r.inc (); |
|
2431 os.write (&bas, 8); |
|
2432 os.write (&lim, 8); |
|
2433 os.write (&inc, 8); |
|
2434 } |
|
2435 else |
620
|
2436 { |
|
2437 gripe_wrong_type_arg ("save", tc); |
|
2438 fail = 1; |
|
2439 } |
604
|
2440 |
620
|
2441 return (os && ! fail); |
604
|
2442 } |
|
2443 |
667
|
2444 // Save the data from TC along with the corresponding NAME on stream OS |
|
2445 // in the MatLab binary format. |
|
2446 |
|
2447 static int |
|
2448 save_mat_binary_data (ostream& os, const tree_constant& tc, char *name) |
|
2449 { |
|
2450 int fail = 0; |
|
2451 |
|
2452 FOUR_BYTE_INT mopt = 0; |
|
2453 |
|
2454 mopt += tc.is_string () ? 1 : 0; |
1226
|
2455 mopt += 1000 * get_floating_point_format (native_float_format); |
667
|
2456 |
|
2457 os.write (&mopt, 4); |
|
2458 |
|
2459 FOUR_BYTE_INT nr = tc.rows (); |
|
2460 os.write (&nr, 4); |
|
2461 |
|
2462 FOUR_BYTE_INT nc = tc.columns (); |
|
2463 os.write (&nc, 4); |
|
2464 |
|
2465 int len = nr * nc; |
|
2466 |
|
2467 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; |
|
2468 os.write (&imag, 4); |
|
2469 |
|
2470 FOUR_BYTE_INT name_len = name ? strlen (name) + 1 : 0; |
|
2471 |
|
2472 os.write (&name_len, 4); |
|
2473 os.write (name, name_len); |
|
2474 |
|
2475 if (tc.is_real_scalar ()) |
|
2476 { |
|
2477 double tmp = tc.double_value (); |
|
2478 os.write (&tmp, 8); |
|
2479 } |
911
|
2480 else if (tc.is_real_matrix ()) |
667
|
2481 { |
|
2482 Matrix m = tc.matrix_value (); |
|
2483 os.write (m.data (), 8 * len); |
|
2484 } |
|
2485 else if (tc.is_complex_scalar ()) |
|
2486 { |
|
2487 Complex tmp = tc.complex_value (); |
|
2488 os.write (&tmp, 16); |
|
2489 } |
|
2490 else if (tc.is_complex_matrix ()) |
|
2491 { |
|
2492 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
2493 Matrix m = ::real(m_cmplx); |
|
2494 os.write (m.data (), 8 * len); |
|
2495 m = ::imag(m_cmplx); |
|
2496 os.write (m.data (), 8 * len); |
|
2497 } |
|
2498 else if (tc.is_string ()) |
|
2499 { |
|
2500 begin_unwind_frame ("save_mat_binary_data"); |
|
2501 unwind_protect_int (user_pref.implicit_str_to_num_ok); |
|
2502 user_pref.implicit_str_to_num_ok = 1; |
|
2503 Matrix m = tc.matrix_value (); |
|
2504 os.write (m.data (), 8 * len); |
|
2505 run_unwind_frame ("save_mat_binary_data"); |
|
2506 } |
911
|
2507 else if (tc.is_range ()) |
|
2508 { |
|
2509 Range r = tc.range_value (); |
|
2510 double base = r.base (); |
|
2511 double inc = r.inc (); |
|
2512 int nel = r.nelem (); |
|
2513 for (int i = 0; i < nel; i++) |
|
2514 { |
|
2515 double x = base + i * inc; |
|
2516 os.write (&x, 8); |
|
2517 } |
|
2518 } |
667
|
2519 else |
|
2520 { |
|
2521 gripe_wrong_type_arg ("save", tc); |
|
2522 fail = 1; |
|
2523 } |
|
2524 |
|
2525 return (os && ! fail); |
|
2526 } |
|
2527 |
620
|
2528 static void |
|
2529 ascii_save_type (ostream& os, char *type, int mark_as_global) |
|
2530 { |
|
2531 if (mark_as_global) |
|
2532 os << "# type: global "; |
|
2533 else |
|
2534 os << "# type: "; |
|
2535 |
|
2536 os << type << "\n"; |
|
2537 } |
|
2538 |
872
|
2539 static Matrix |
|
2540 strip_infnan (const Matrix& m) |
|
2541 { |
|
2542 int nr = m.rows (); |
|
2543 int nc = m.columns (); |
|
2544 |
|
2545 Matrix retval (nr, nc); |
|
2546 |
|
2547 int k = 0; |
|
2548 for (int i = 0; i < nr; i++) |
|
2549 { |
|
2550 for (int j = 0; j < nc; j++) |
|
2551 { |
|
2552 double d = m.elem (i, j); |
|
2553 if (xisnan (d)) |
|
2554 goto next_row; |
|
2555 else |
|
2556 retval.elem (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
2557 } |
|
2558 k++; |
|
2559 |
|
2560 next_row: |
|
2561 continue; |
|
2562 } |
|
2563 |
|
2564 if (k > 0) |
|
2565 retval.resize (k, nc); |
|
2566 |
|
2567 return retval; |
|
2568 } |
|
2569 |
|
2570 static ComplexMatrix |
|
2571 strip_infnan (const ComplexMatrix& m) |
|
2572 { |
|
2573 int nr = m.rows (); |
|
2574 int nc = m.columns (); |
|
2575 |
|
2576 ComplexMatrix retval (nr, nc); |
|
2577 |
|
2578 int k = 0; |
|
2579 for (int i = 0; i < nr; i++) |
|
2580 { |
|
2581 for (int j = 0; j < nc; j++) |
|
2582 { |
|
2583 Complex c = m.elem (i, j); |
|
2584 if (xisnan (c)) |
|
2585 goto next_row; |
|
2586 else |
|
2587 { |
|
2588 double re = real (c); |
|
2589 double im = imag (c); |
|
2590 |
|
2591 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
2592 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
2593 |
|
2594 retval.elem (k, j) = Complex (re, im); |
|
2595 } |
|
2596 } |
|
2597 k++; |
|
2598 |
|
2599 next_row: |
|
2600 continue; |
|
2601 } |
|
2602 |
|
2603 if (k > 0) |
|
2604 retval.resize (k, nc); |
|
2605 |
|
2606 return retval; |
|
2607 } |
|
2608 |
620
|
2609 // Save the data from TC along with the corresponding NAME, and global |
604
|
2610 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
|
2611 // above for load_ascii_data. If NAME is null, the name: line is not |
|
2612 // generated. PRECISION specifies the number of decimal digits to print. |
872
|
2613 // If STRIP_NAN_AND_INF is nonzero, rows containing NaNs are deleted, |
|
2614 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
2615 // but not so big that gnuplot can't handle it when trying to compute |
|
2616 // axis ranges, etc.). |
|
2617 // |
|
2618 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
2619 // |
|
2620 // Returns 1 for success and 0 for failure. |
604
|
2621 |
|
2622 // XXX FIXME XXX -- should probably write the help string here too. |
|
2623 |
|
2624 int |
620
|
2625 save_ascii_data (ostream& os, const tree_constant& tc, |
872
|
2626 char *name, int strip_nan_and_inf, |
|
2627 int mark_as_global, int precision) |
604
|
2628 { |
872
|
2629 int success = 1; |
620
|
2630 |
604
|
2631 if (! precision) |
|
2632 precision = user_pref.save_precision; |
|
2633 |
|
2634 if (name) |
|
2635 os << "# name: " << name << "\n"; |
|
2636 |
|
2637 long old_precision = os.precision (); |
|
2638 os.precision (precision); |
|
2639 |
620
|
2640 if (tc.is_real_scalar ()) |
|
2641 { |
|
2642 ascii_save_type (os, "scalar", mark_as_global); |
872
|
2643 |
|
2644 double d = tc.double_value (); |
|
2645 if (strip_nan_and_inf) |
|
2646 { |
|
2647 if (xisnan (d)) |
|
2648 { |
|
2649 error ("only value to plot is NaN"); |
|
2650 success = 0; |
|
2651 } |
|
2652 else |
|
2653 { |
|
2654 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
2655 os << d << "\n"; |
|
2656 } |
|
2657 } |
|
2658 else |
|
2659 os << d << "\n"; |
620
|
2660 } |
|
2661 else if (tc.is_real_matrix ()) |
|
2662 { |
|
2663 ascii_save_type (os, "matrix", mark_as_global); |
|
2664 os << "# rows: " << tc.rows () << "\n" |
872
|
2665 << "# columns: " << tc.columns () << "\n"; |
|
2666 |
|
2667 Matrix tmp = tc.matrix_value (); |
|
2668 if (strip_nan_and_inf) |
|
2669 tmp = strip_infnan (tmp); |
|
2670 |
|
2671 os << tmp; |
620
|
2672 } |
|
2673 else if (tc.is_complex_scalar ()) |
|
2674 { |
|
2675 ascii_save_type (os, "complex scalar", mark_as_global); |
872
|
2676 |
|
2677 Complex c = tc.complex_value (); |
|
2678 if (strip_nan_and_inf) |
|
2679 { |
|
2680 if (xisnan (c)) |
|
2681 { |
|
2682 error ("only value to plot is NaN"); |
|
2683 success = 0; |
|
2684 } |
|
2685 else |
|
2686 { |
|
2687 double re = real (c); |
|
2688 double im = imag (c); |
|
2689 |
|
2690 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
2691 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
2692 |
|
2693 c = Complex (re, im); |
|
2694 |
|
2695 os << c << "\n"; |
|
2696 } |
|
2697 } |
|
2698 else |
|
2699 os << c << "\n"; |
620
|
2700 } |
|
2701 else if (tc.is_complex_matrix ()) |
604
|
2702 { |
620
|
2703 ascii_save_type (os, "complex matrix", mark_as_global); |
|
2704 os << "# rows: " << tc.rows () << "\n" |
875
|
2705 << "# columns: " << tc.columns () << "\n"; |
|
2706 |
|
2707 ComplexMatrix tmp = tc.complex_matrix_value (); |
872
|
2708 if (strip_nan_and_inf) |
|
2709 tmp = strip_infnan (tmp); |
|
2710 |
|
2711 os << tmp; |
620
|
2712 } |
|
2713 else if (tc.is_string ()) |
|
2714 { |
|
2715 ascii_save_type (os, "string", mark_as_global); |
|
2716 char *tmp = tc.string_value (); |
|
2717 os << "# length: " << strlen (tmp) << "\n" |
|
2718 << tmp << "\n"; |
|
2719 } |
872
|
2720 else if (tc.is_range ()) |
620
|
2721 { |
|
2722 ascii_save_type (os, "range", mark_as_global); |
|
2723 Range tmp = tc.range_value (); |
|
2724 os << "# base, limit, increment\n" |
|
2725 << tmp.base () << " " |
|
2726 << tmp.limit () << " " |
|
2727 << tmp.inc () << "\n"; |
|
2728 } |
|
2729 else |
|
2730 { |
|
2731 gripe_wrong_type_arg ("save", tc); |
872
|
2732 success = 0; |
604
|
2733 } |
|
2734 |
|
2735 os.precision (old_precision); |
|
2736 |
872
|
2737 return (os && success); |
604
|
2738 } |
|
2739 |
|
2740 // Save the info from sr on stream os in the format specified by fmt. |
|
2741 |
|
2742 static void |
630
|
2743 do_save (ostream& os, symbol_record *sr, load_save_format fmt, |
|
2744 int save_as_floats) |
604
|
2745 { |
|
2746 if (! sr->is_variable ()) |
|
2747 { |
|
2748 error ("save: can only save variables, not functions"); |
|
2749 return; |
|
2750 } |
|
2751 |
|
2752 char *name = sr->name (); |
|
2753 char *help = sr->help (); |
|
2754 int global = sr->is_linked_to_global (); |
|
2755 tree_constant tc = *((tree_constant *) sr->def ()); |
|
2756 |
|
2757 if (! name || ! tc.is_defined ()) |
|
2758 return; |
|
2759 |
|
2760 switch (fmt) |
|
2761 { |
|
2762 case LS_ASCII: |
872
|
2763 save_ascii_data (os, tc, name, 0, global); |
604
|
2764 break; |
|
2765 |
|
2766 case LS_BINARY: |
630
|
2767 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
2768 break; |
|
2769 |
667
|
2770 case LS_MAT_BINARY: |
|
2771 save_mat_binary_data (os, tc, name); |
|
2772 break; |
|
2773 |
604
|
2774 default: |
775
|
2775 gripe_unrecognized_data_fmt ("save"); |
604
|
2776 break; |
|
2777 } |
|
2778 } |
|
2779 |
|
2780 // Save variables with names matching PATTERN on stream OS in the |
|
2781 // format specified by FMT. If SAVE_BUILTINS is nonzero, also save |
|
2782 // builtin variables with names that match PATTERN. |
|
2783 |
|
2784 static int |
|
2785 save_vars (ostream& os, char *pattern, int save_builtins, |
630
|
2786 load_save_format fmt, int save_as_floats) |
604
|
2787 { |
|
2788 int count; |
|
2789 |
|
2790 symbol_record **vars = curr_sym_tab->glob |
|
2791 (count, pattern, symbol_def::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2792 |
|
2793 int saved = count; |
|
2794 |
|
2795 int i; |
|
2796 |
|
2797 for (i = 0; i < count; i++) |
620
|
2798 { |
630
|
2799 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2800 |
|
2801 if (error_state) |
|
2802 break; |
|
2803 } |
604
|
2804 |
|
2805 delete [] vars; |
|
2806 |
620
|
2807 if (! error_state && save_builtins) |
604
|
2808 { |
|
2809 symbol_record **vars = global_sym_tab->glob |
|
2810 (count, pattern, symbol_def::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2811 |
|
2812 saved += count; |
|
2813 |
|
2814 for (i = 0; i < count; i++) |
620
|
2815 { |
630
|
2816 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2817 |
|
2818 if (error_state) |
|
2819 break; |
|
2820 } |
604
|
2821 |
|
2822 delete [] vars; |
|
2823 } |
|
2824 |
|
2825 return saved; |
|
2826 } |
|
2827 |
|
2828 static load_save_format |
|
2829 get_default_save_format (void) |
|
2830 { |
|
2831 load_save_format retval = LS_ASCII; |
|
2832 |
|
2833 char *fmt = user_pref.default_save_format; |
|
2834 |
|
2835 if (strcasecmp (fmt, "binary") == 0) |
|
2836 retval = LS_BINARY; |
911
|
2837 else if (strcasecmp (fmt, "mat-binary") == 0 |
|
2838 || strcasecmp (fmt, "mat_binary") == 0) |
|
2839 retval = LS_MAT_BINARY; |
604
|
2840 |
|
2841 return retval; |
|
2842 } |
|
2843 |
863
|
2844 static void |
|
2845 write_binary_header (ostream& stream, load_save_format format) |
|
2846 { |
|
2847 if (format == LS_BINARY) |
|
2848 { |
|
2849 #if defined (WORDS_BIGENDIAN) |
|
2850 stream << "Octave-1-B"; |
|
2851 #else |
|
2852 stream << "Octave-1-L"; |
|
2853 #endif |
|
2854 |
1226
|
2855 char tmp = (char) native_float_format; |
863
|
2856 stream.write (&tmp, 1); |
|
2857 } |
|
2858 } |
|
2859 |
|
2860 static void |
|
2861 save_vars (char **argv, int argc, ostream& os, int save_builtins, |
|
2862 load_save_format fmt, int save_as_floats) |
|
2863 { |
|
2864 write_binary_header (os, fmt); |
|
2865 |
|
2866 if (argc == 0) |
|
2867 { |
|
2868 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
|
2869 } |
|
2870 else |
|
2871 { |
|
2872 while (argc-- > 0) |
|
2873 { |
|
2874 if (! save_vars (os, *argv, save_builtins, fmt, save_as_floats)) |
|
2875 { |
|
2876 warning ("save: no such variable `%s'", *argv); |
|
2877 } |
|
2878 |
|
2879 argv++; |
|
2880 } |
|
2881 } |
|
2882 } |
|
2883 |
604
|
2884 DEFUN_TEXT ("save", Fsave, Ssave, -1, 1, |
910
|
2885 "save [-ascii] [-binary] [-float-binary] [-mat-binary] \n\ |
667
|
2886 [-save-builtins] file [pattern ...]\n\ |
604
|
2887 \n\ |
|
2888 save variables in a file") |
|
2889 { |
|
2890 Octave_object retval; |
|
2891 |
863
|
2892 DEFINE_ARGV ("save"); |
604
|
2893 |
|
2894 argc--; |
|
2895 argv++; |
|
2896 |
|
2897 // Here is where we would get the default save format if it were |
|
2898 // stored in a user preference variable. |
|
2899 |
|
2900 int save_builtins = 0; |
|
2901 |
630
|
2902 int save_as_floats = 0; |
|
2903 |
604
|
2904 load_save_format format = get_default_save_format (); |
|
2905 |
|
2906 while (argc > 0) |
|
2907 { |
|
2908 if (strcmp (*argv, "-ascii") == 0 || strcmp (*argv, "-a") == 0) |
|
2909 { |
|
2910 format = LS_ASCII; |
|
2911 argc--; |
|
2912 argv++; |
|
2913 } |
|
2914 else if (strcmp (*argv, "-binary") == 0 || strcmp (*argv, "-b") == 0) |
|
2915 { |
|
2916 format = LS_BINARY; |
|
2917 argc--; |
|
2918 argv++; |
|
2919 } |
667
|
2920 else if (strcmp (*argv, "-mat-binary") == 0 || strcmp (*argv, "-m") == 0) |
|
2921 { |
|
2922 format = LS_MAT_BINARY; |
|
2923 argc--; |
|
2924 argv++; |
|
2925 } |
630
|
2926 else if (strcmp (*argv, "-float-binary") == 0 |
|
2927 || strcmp (*argv, "-f") == 0) |
|
2928 { |
|
2929 format = LS_BINARY; |
|
2930 save_as_floats = 1; |
|
2931 argc--; |
|
2932 argv++; |
|
2933 } |
604
|
2934 else if (strcmp (*argv, "-save-builtins") == 0) |
|
2935 { |
|
2936 save_builtins = 1; |
|
2937 argc--; |
|
2938 argv++; |
|
2939 } |
|
2940 else |
|
2941 break; |
|
2942 } |
|
2943 |
|
2944 if (argc < 1) |
|
2945 { |
|
2946 print_usage ("save"); |
|
2947 DELETE_ARGV; |
|
2948 return retval; |
|
2949 } |
|
2950 |
630
|
2951 if (save_as_floats && format == LS_ASCII) |
|
2952 { |
|
2953 error ("save: cannot specify both -ascii and -float-binary"); |
|
2954 DELETE_ARGV; |
|
2955 return retval; |
|
2956 } |
|
2957 |
604
|
2958 if (strcmp (*argv, "-") == 0) |
|
2959 { |
863
|
2960 argc--; |
|
2961 argv++; |
|
2962 |
604
|
2963 // XXX FIXME XXX -- should things intended for the screen end up in a |
|
2964 // tree_constant (string)? |
863
|
2965 |
1043
|
2966 ostrstream buf; |
|
2967 |
|
2968 save_vars (argv, argc, buf, save_builtins, format, |
863
|
2969 save_as_floats); |
1043
|
2970 |
|
2971 maybe_page_output (buf); |
604
|
2972 } |
|
2973 else if (argc == 1 && glob_pattern_p (*argv)) // Guard against things |
|
2974 { // like `save a*', |
|
2975 print_usage ("save"); // which are probably |
|
2976 DELETE_ARGV; // mistakes... |
|
2977 return retval; |
|
2978 } |
|
2979 else |
|
2980 { |
1159
|
2981 static char *fname = 0; |
1158
|
2982 |
|
2983 if (fname) |
|
2984 free (fname); |
|
2985 |
|
2986 fname = tilde_expand (*argv); |
604
|
2987 |
|
2988 argc--; |
|
2989 argv++; |
|
2990 |
911
|
2991 unsigned mode = ios::out|ios::trunc; |
604
|
2992 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2993 mode |= ios::bin; |
|
2994 |
863
|
2995 ofstream file (fname, mode); |
|
2996 |
|
2997 if (file) |
|
2998 { |
|
2999 save_vars (argv, argc, file, save_builtins, format, |
|
3000 save_as_floats); |
|
3001 } |
|
3002 else |
604
|
3003 { |
|
3004 error ("save: couldn't open output file `%s'", *argv); |
|
3005 DELETE_ARGV; |
|
3006 return retval; |
|
3007 } |
|
3008 } |
|
3009 |
|
3010 DELETE_ARGV; |
|
3011 |
|
3012 return retval; |
|
3013 } |
|
3014 |
|
3015 // Maybe this should be a static function in tree-plot.cc? |
|
3016 |
620
|
3017 // If TC is matrix, save it on stream OS in a format useful for |
604
|
3018 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
3019 // nonzero, assume a parametric 3-dimensional plot will be generated. |
|
3020 |
|
3021 int |
620
|
3022 save_three_d (ostream& os, const tree_constant& tc, int parametric) |
604
|
3023 { |
620
|
3024 int fail = 0; |
604
|
3025 |
620
|
3026 int nr = tc.rows (); |
|
3027 int nc = tc.columns (); |
|
3028 |
|
3029 if (tc.is_real_matrix ()) |
604
|
3030 { |
|
3031 os << "# 3D data...\n" |
|
3032 << "# type: matrix\n" |
|
3033 << "# total rows: " << nr << "\n" |
|
3034 << "# total columns: " << nc << "\n"; |
|
3035 |
|
3036 if (parametric) |
|
3037 { |
|
3038 int extras = nc % 3; |
|
3039 if (extras) |
|
3040 warning ("ignoring last %d columns", extras); |
|
3041 |
620
|
3042 Matrix tmp = tc.matrix_value (); |
872
|
3043 tmp = strip_infnan (tmp); |
|
3044 nr = tmp.rows (); |
|
3045 |
604
|
3046 for (int i = 0; i < nc-extras; i += 3) |
|
3047 { |
|
3048 os << tmp.extract (0, i, nr-1, i+2); |
|
3049 if (i+3 < nc-extras) |
|
3050 os << "\n"; |
|
3051 } |
|
3052 } |
|
3053 else |
|
3054 { |
620
|
3055 Matrix tmp = tc.matrix_value (); |
872
|
3056 tmp = strip_infnan (tmp); |
|
3057 nr = tmp.rows (); |
|
3058 |
604
|
3059 for (int i = 0; i < nc; i++) |
|
3060 { |
|
3061 os << tmp.extract (0, i, nr-1, i); |
|
3062 if (i+1 < nc) |
|
3063 os << "\n"; |
|
3064 } |
|
3065 } |
620
|
3066 } |
|
3067 else |
|
3068 { |
604
|
3069 ::error ("for now, I can only save real matrices in 3D format"); |
620
|
3070 fail = 1; |
604
|
3071 } |
620
|
3072 |
|
3073 return (os && ! fail); |
604
|
3074 } |
|
3075 |
|
3076 /* |
|
3077 ;;; Local Variables: *** |
|
3078 ;;; mode: C++ *** |
|
3079 ;;; page-delimiter: "^/\\*" *** |
|
3080 ;;; End: *** |
|
3081 */ |