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