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