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