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