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