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