604
|
1 // load-save.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
604
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
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 // |
918
|
1058 // #[ \t]*keyword[ \t]*:[ \t]*string-value[ \t]*\n |
604
|
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 } |
918
|
1104 |
|
1105 if (retval) |
|
1106 { |
|
1107 int len = strlen (retval); |
|
1108 if (len > 0) |
|
1109 { |
|
1110 char *ptr = retval + len - 1; |
|
1111 while (*ptr == ' ' || *ptr == '\t') |
|
1112 ptr--; |
|
1113 *(ptr+1) = '\0'; |
|
1114 } |
|
1115 } |
|
1116 |
604
|
1117 return retval; |
|
1118 } |
|
1119 |
|
1120 // Match KEYWORD on stream IS, placing the associated value in VALUE, |
|
1121 // returning 1 if successful and 0 otherwise. |
|
1122 // |
|
1123 // Input should look something like: |
|
1124 // |
918
|
1125 // [ \t]*keyword[ \t]*int-value.*\n |
604
|
1126 |
|
1127 static int |
|
1128 extract_keyword (istream& is, char *keyword, int& value) |
|
1129 { |
|
1130 ostrstream buf; |
|
1131 |
|
1132 int status = 0; |
|
1133 value = 0; |
|
1134 |
|
1135 char c; |
|
1136 while (is.get (c)) |
|
1137 { |
|
1138 if (c == '#') |
|
1139 { |
|
1140 while (is.get (c) && (c == ' ' || c == '\t' || c == '#')) |
|
1141 ; // Skip whitespace and comment characters. |
|
1142 |
|
1143 if (isalpha (c)) |
|
1144 buf << c; |
|
1145 |
|
1146 while (is.get (c) && isalpha (c)) |
|
1147 buf << c; |
|
1148 |
|
1149 buf << ends; |
|
1150 char *tmp = buf.str (); |
|
1151 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
1152 delete [] tmp; |
|
1153 |
|
1154 if (match) |
|
1155 { |
|
1156 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
1157 ; // Skip whitespace and the colon. |
|
1158 |
|
1159 is.putback (c); |
|
1160 if (c != '\n') |
|
1161 is >> value; |
|
1162 if (is) |
|
1163 status = 1; |
|
1164 while (is.get (c) && c != '\n') |
|
1165 ; // Skip to beginning of next line; |
|
1166 break; |
|
1167 } |
|
1168 } |
|
1169 } |
|
1170 return status; |
|
1171 } |
|
1172 |
|
1173 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
1174 // place it in TC, returning the name of the variable. If the value |
|
1175 // is tagged as global in the file, return nonzero in GLOBAL. |
|
1176 // |
|
1177 // FILENAME is used for error messages. |
|
1178 // |
|
1179 // The data is expected to be in the following format: |
|
1180 // |
|
1181 // The input file must have a header followed by some data. |
|
1182 // |
|
1183 // All lines in the header must begin with a `#' character. |
|
1184 // |
|
1185 // The header must contain a list of keyword and value pairs with the |
|
1186 // keyword and value separated by a colon. |
|
1187 // |
|
1188 // Keywords must appear in the following order: |
|
1189 // |
|
1190 // # name: <name> |
|
1191 // # type: <type> |
|
1192 // # <info> |
|
1193 // |
|
1194 // Where: |
|
1195 // |
|
1196 // <name> : a valid identifier |
|
1197 // |
|
1198 // <type> : <typename> |
|
1199 // | global <typename> |
|
1200 // |
|
1201 // <typename> : scalar |
|
1202 // | complex scalar |
|
1203 // | matrix |
|
1204 // | complex matrix |
|
1205 // | string |
|
1206 // | range |
|
1207 // |
|
1208 // <info> : <matrix info> |
|
1209 // | <string info> |
|
1210 // |
|
1211 // <matrix info> : # rows: <integer> |
|
1212 // | # columns: <integer> |
|
1213 // |
|
1214 // <string info> : # len: <integer> |
|
1215 // |
|
1216 // Formatted ASCII data follows the header. |
|
1217 // |
|
1218 // Example: |
|
1219 // |
|
1220 // # name: foo |
|
1221 // # type: matrix |
|
1222 // # rows: 2 |
|
1223 // # columns: 2 |
|
1224 // 2 4 |
|
1225 // 1 3 |
|
1226 // |
|
1227 // XXX FIXME XXX -- this format is fairly rigid, and doesn't allow for |
|
1228 // arbitrary comments, etc. Someone should fix that. |
|
1229 |
|
1230 static char * |
|
1231 read_ascii_data (istream& is, const char *filename, int& global, |
|
1232 tree_constant& tc) |
|
1233 { |
|
1234 // Read name for this entry or break on EOF. |
|
1235 |
|
1236 char *name = extract_keyword (is, "name"); |
|
1237 |
|
1238 if (! name) |
|
1239 return 0; |
|
1240 |
|
1241 if (! *name) |
|
1242 { |
|
1243 error ("load: empty name keyword found in file `%s'", filename); |
|
1244 delete [] name; |
|
1245 return 0; |
|
1246 } |
|
1247 |
|
1248 |
|
1249 if (! valid_identifier (name)) |
|
1250 { |
|
1251 error ("load: bogus identifier `%s' found in file `%s'", name, filename); |
|
1252 delete [] name; |
|
1253 return 0; |
|
1254 } |
|
1255 |
|
1256 // Look for type keyword |
|
1257 |
|
1258 char *tag = extract_keyword (is, "type"); |
|
1259 |
|
1260 if (tag && *tag) |
|
1261 { |
|
1262 char *ptr = strchr (tag, ' '); |
|
1263 if (ptr) |
|
1264 { |
|
1265 *ptr = '\0'; |
|
1266 global = (strncmp (tag, "global", 6) == 0); |
|
1267 *ptr = ' '; |
|
1268 if (global) |
|
1269 ptr++; |
|
1270 else |
|
1271 ptr = tag; |
|
1272 } |
|
1273 else |
|
1274 ptr = tag; |
|
1275 |
|
1276 if (strncmp (ptr, "scalar", 6) == 0) |
|
1277 { |
|
1278 double tmp; |
|
1279 is >> tmp; |
|
1280 if (is) |
|
1281 tc = tmp; |
|
1282 else |
|
1283 error ("load: failed to load scalar constant"); |
|
1284 } |
|
1285 else if (strncmp (ptr, "matrix", 6) == 0) |
|
1286 { |
|
1287 int nr = 0, nc = 0; |
|
1288 |
|
1289 if (extract_keyword (is, "rows", nr) && nr > 0 |
|
1290 && extract_keyword (is, "columns", nc) && nc > 0) |
|
1291 { |
|
1292 Matrix tmp (nr, nc); |
|
1293 is >> tmp; |
|
1294 if (is) |
|
1295 tc = tmp; |
|
1296 else |
|
1297 error ("load: failed to load matrix constant"); |
|
1298 } |
|
1299 else |
|
1300 error ("load: failed to extract number of rows and columns"); |
|
1301 } |
|
1302 else if (strncmp (ptr, "complex scalar", 14) == 0) |
|
1303 { |
|
1304 Complex tmp; |
|
1305 is >> tmp; |
|
1306 if (is) |
|
1307 tc = tmp; |
|
1308 else |
|
1309 error ("load: failed to load complex scalar constant"); |
|
1310 } |
|
1311 else if (strncmp (ptr, "complex matrix", 14) == 0) |
|
1312 { |
|
1313 int nr = 0, nc = 0; |
|
1314 |
|
1315 if (extract_keyword (is, "rows", nr) && nr > 0 |
|
1316 && extract_keyword (is, "columns", nc) && nc > 0) |
|
1317 { |
|
1318 ComplexMatrix tmp (nr, nc); |
|
1319 is >> tmp; |
|
1320 if (is) |
|
1321 tc = tmp; |
|
1322 else |
|
1323 error ("load: failed to load complex matrix constant"); |
|
1324 } |
|
1325 else |
|
1326 error ("load: failed to extract number of rows and columns"); |
|
1327 } |
|
1328 else if (strncmp (ptr, "string", 6) == 0) |
|
1329 { |
|
1330 int len; |
|
1331 if (extract_keyword (is, "length", len) && len > 0) |
|
1332 { |
|
1333 char *tmp = new char [len+1]; |
|
1334 is.get (tmp, len+1, EOF); |
|
1335 if (is) |
|
1336 tc = tmp; |
|
1337 else |
|
1338 error ("load: failed to load string constant"); |
|
1339 } |
|
1340 else |
|
1341 error ("load: failed to extract string length"); |
|
1342 } |
|
1343 else if (strncmp (ptr, "range", 5) == 0) |
|
1344 { |
|
1345 // # base, limit, range comment added by save(). |
|
1346 skip_comments (is); |
|
1347 Range tmp; |
|
1348 is >> tmp; |
|
1349 if (is) |
|
1350 tc = tmp; |
|
1351 else |
|
1352 error ("load: failed to load range constant"); |
|
1353 } |
|
1354 else |
|
1355 error ("load: unknown constant type `%s'", tag); |
|
1356 } |
|
1357 else |
|
1358 error ("load: failed to extract keyword specifying value type"); |
|
1359 |
|
1360 delete [] tag; |
|
1361 |
|
1362 if (error_state) |
|
1363 { |
|
1364 error ("load: reading file %s", filename); |
|
1365 return 0; |
|
1366 } |
|
1367 |
|
1368 return name; |
|
1369 } |
|
1370 |
|
1371 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
1372 // place it in TC, returning the name of the variable. If the value |
|
1373 // is tagged as global in the file, return nonzero in GLOBAL. If SWAP |
|
1374 // is nonzero, swap bytes after reading. |
|
1375 // |
|
1376 // The data is expected to be in the following format: |
|
1377 // |
867
|
1378 // Header (one per file): |
|
1379 // ===================== |
604
|
1380 // |
867
|
1381 // object type bytes |
|
1382 // ------ ---- ----- |
|
1383 // magic number string 10 |
604
|
1384 // |
867
|
1385 // float format integer 1 |
|
1386 // |
604
|
1387 // |
867
|
1388 // Data (one set for each item): |
|
1389 // ============================ |
604
|
1390 // |
867
|
1391 // object type bytes |
|
1392 // ------ ---- ----- |
|
1393 // name_length integer 4 |
604
|
1394 // |
867
|
1395 // name string name_length |
604
|
1396 // |
867
|
1397 // doc_length integer 4 |
|
1398 // |
|
1399 // doc string doc_length |
604
|
1400 // |
867
|
1401 // global flag integer 1 |
604
|
1402 // |
867
|
1403 // data type integer 1 |
604
|
1404 // |
867
|
1405 // data (one of): |
|
1406 // |
|
1407 // scalar: |
|
1408 // data real 8 |
604
|
1409 // |
867
|
1410 // complex scalar: |
|
1411 // data complex 16 |
|
1412 // |
|
1413 // matrix: |
|
1414 // rows integer 4 |
|
1415 // columns integer 4 |
|
1416 // data real r*c*8 |
604
|
1417 // |
867
|
1418 // complex matrix: |
|
1419 // rows integer 4 |
|
1420 // columns integer 4 |
|
1421 // data complex r*c*16 |
604
|
1422 // |
867
|
1423 // string: |
|
1424 // length int 4 |
|
1425 // data string length |
604
|
1426 // |
867
|
1427 // range: |
|
1428 // base real 8 |
|
1429 // limit real 8 |
|
1430 // increment real 8 |
604
|
1431 // |
|
1432 // FILENAME is used for error messages. |
|
1433 |
|
1434 static char * |
|
1435 read_binary_data (istream& is, int swap, floating_point_format fmt, |
|
1436 const char *filename, int& global, |
|
1437 tree_constant& tc, char *&doc) |
|
1438 { |
|
1439 char tmp = 0; |
|
1440 |
|
1441 FOUR_BYTE_INT name_len = 0, doc_len = 0; |
|
1442 char *name = 0; |
|
1443 |
|
1444 doc = 0; |
|
1445 |
867
|
1446 // We expect to fail here, at the beginning of a record, so not being |
|
1447 // able to read another name should not result in an error. |
|
1448 |
604
|
1449 is.read (&name_len, 4); |
|
1450 if (! is) |
867
|
1451 return 0; |
604
|
1452 if (swap) |
|
1453 swap_4_bytes ((char *) &name_len); |
|
1454 |
|
1455 name = new char [name_len+1]; |
|
1456 name[name_len] = '\0'; |
|
1457 if (! is.read (name, name_len)) |
|
1458 goto data_read_error; |
|
1459 |
|
1460 is.read (&doc_len, 4); |
|
1461 if (! is) |
|
1462 goto data_read_error; |
|
1463 if (swap) |
|
1464 swap_4_bytes ((char *) &doc_len); |
|
1465 |
|
1466 doc = new char [doc_len+1]; |
|
1467 doc[doc_len] = '\0'; |
|
1468 if (! is.read (doc, doc_len)) |
|
1469 goto data_read_error; |
|
1470 |
|
1471 if (! is.read (&tmp, 1)) |
|
1472 goto data_read_error; |
|
1473 global = tmp ? 1 : 0; |
|
1474 |
|
1475 tmp = 0; |
|
1476 if (! is.read (&tmp, 1)) |
|
1477 goto data_read_error; |
|
1478 |
|
1479 switch (tmp) |
|
1480 { |
|
1481 case 1: |
|
1482 { |
630
|
1483 if (! is.read (&tmp, 1)) |
604
|
1484 goto data_read_error; |
630
|
1485 double dtmp; |
|
1486 read_doubles (is, &dtmp, (save_type) tmp, 1, swap, fmt); |
774
|
1487 if (error_state || ! is) |
630
|
1488 goto data_read_error; |
604
|
1489 tc = dtmp; |
|
1490 } |
|
1491 break; |
|
1492 |
|
1493 case 2: |
|
1494 { |
|
1495 FOUR_BYTE_INT nr, nc; |
|
1496 if (! is.read (&nr, 4)) |
|
1497 goto data_read_error; |
|
1498 if (swap) |
|
1499 swap_4_bytes ((char *) &nr); |
|
1500 if (! is.read (&nc, 4)) |
|
1501 goto data_read_error; |
|
1502 if (swap) |
|
1503 swap_4_bytes ((char *) &nc); |
|
1504 if (! is.read (&tmp, 1)) |
|
1505 goto data_read_error; |
|
1506 Matrix m (nr, nc); |
|
1507 double *re = m.fortran_vec (); |
|
1508 int len = nr * nc; |
|
1509 read_doubles (is, re, (save_type) tmp, len, swap, fmt); |
774
|
1510 if (error_state || ! is) |
604
|
1511 goto data_read_error; |
|
1512 tc = m; |
|
1513 } |
|
1514 break; |
|
1515 |
|
1516 case 3: |
|
1517 { |
630
|
1518 if (! is.read (&tmp, 1)) |
604
|
1519 goto data_read_error; |
630
|
1520 Complex ctmp; |
|
1521 read_doubles (is, (double *) &ctmp, (save_type) tmp, 2, swap, fmt); |
774
|
1522 if (error_state || ! is) |
630
|
1523 goto data_read_error; |
604
|
1524 tc = ctmp; |
|
1525 } |
|
1526 break; |
|
1527 |
|
1528 case 4: |
|
1529 { |
|
1530 FOUR_BYTE_INT nr, nc; |
|
1531 if (! is.read (&nr, 4)) |
|
1532 goto data_read_error; |
|
1533 if (swap) |
|
1534 swap_4_bytes ((char *) &nr); |
|
1535 if (! is.read (&nc, 4)) |
|
1536 goto data_read_error; |
|
1537 if (swap) |
|
1538 swap_4_bytes ((char *) &nc); |
|
1539 if (! is.read (&tmp, 1)) |
|
1540 goto data_read_error; |
|
1541 ComplexMatrix m (nr, nc); |
|
1542 Complex *im = m.fortran_vec (); |
|
1543 int len = nr * nc; |
630
|
1544 read_doubles (is, (double *) im, (save_type) tmp, 2*len, |
|
1545 swap, fmt); |
774
|
1546 if (error_state || ! is) |
604
|
1547 goto data_read_error; |
|
1548 tc = m; |
|
1549 } |
|
1550 break; |
|
1551 |
|
1552 case 5: |
|
1553 { |
|
1554 int nr = tc.rows (); |
|
1555 int nc = tc.columns (); |
|
1556 FOUR_BYTE_INT len = nr * nc; |
|
1557 if (! is.read (&len, 4)) |
|
1558 goto data_read_error; |
|
1559 if (swap) |
|
1560 swap_4_bytes ((char *) &len); |
|
1561 char *s = new char [len+1]; |
|
1562 if (! is.read (s, len)) |
|
1563 { |
|
1564 delete [] s; |
|
1565 goto data_read_error; |
|
1566 } |
|
1567 s[len] = '\0'; |
|
1568 tc = s; |
|
1569 } |
|
1570 break; |
|
1571 |
|
1572 case 6: |
|
1573 { |
630
|
1574 if (! is.read (&tmp, 1)) |
|
1575 goto data_read_error; |
604
|
1576 double bas, lim, inc; |
|
1577 if (! is.read (&bas, 8)) |
|
1578 goto data_read_error; |
|
1579 if (swap) |
|
1580 swap_8_bytes ((char *) &bas); |
|
1581 if (! is.read (&lim, 8)) |
|
1582 goto data_read_error; |
|
1583 if (swap) |
|
1584 swap_8_bytes ((char *) &lim); |
|
1585 if (! is.read (&inc, 8)) |
|
1586 goto data_read_error; |
|
1587 if (swap) |
|
1588 swap_8_bytes ((char *) &inc); |
|
1589 Range r (bas, lim, inc); |
|
1590 tc = r; |
|
1591 } |
|
1592 break; |
|
1593 |
|
1594 default: |
|
1595 data_read_error: |
|
1596 error ("load: trouble reading binary file `%s'", filename); |
|
1597 delete [] name; |
|
1598 name = 0; |
|
1599 break; |
|
1600 } |
|
1601 |
|
1602 return name; |
|
1603 } |
|
1604 |
|
1605 // Read LEN elements of data from IS in the format specified by |
|
1606 // PRECISION, placing the result in DATA. If SWAP is nonzero, swap |
|
1607 // the bytes of each element before copying to DATA. FLT_FMT |
|
1608 // specifies the format of the data if we are reading floating point |
|
1609 // numbers. |
|
1610 |
|
1611 static void |
|
1612 read_mat_binary_data (istream& is, double *data, int precision, |
|
1613 int len, int swap, floating_point_format flt_fmt) |
|
1614 { |
|
1615 switch (precision) |
|
1616 { |
|
1617 case 0: |
630
|
1618 read_doubles (is, data, LS_DOUBLE, len, swap, flt_fmt); |
604
|
1619 break; |
|
1620 |
|
1621 case 1: |
630
|
1622 read_doubles (is, data, LS_FLOAT, len, swap, flt_fmt); |
604
|
1623 break; |
|
1624 |
|
1625 case 2: |
|
1626 read_doubles (is, data, LS_INT, len, swap, flt_fmt); |
|
1627 break; |
|
1628 |
|
1629 case 3: |
|
1630 read_doubles (is, data, LS_SHORT, len, swap, flt_fmt); |
|
1631 break; |
|
1632 |
|
1633 case 4: |
|
1634 read_doubles (is, data, LS_U_SHORT, len, swap, flt_fmt); |
|
1635 break; |
|
1636 |
|
1637 case 5: |
|
1638 read_doubles (is, data, LS_U_CHAR, len, swap, flt_fmt); |
|
1639 break; |
|
1640 |
|
1641 default: |
|
1642 break; |
|
1643 } |
|
1644 } |
|
1645 |
|
1646 static int |
|
1647 read_mat_file_header (istream& is, int& swap, FOUR_BYTE_INT& mopt, |
|
1648 FOUR_BYTE_INT& nr, FOUR_BYTE_INT& nc, |
|
1649 FOUR_BYTE_INT& imag, FOUR_BYTE_INT& len, |
|
1650 int quiet = 0) |
|
1651 { |
671
|
1652 swap = 0; |
|
1653 |
911
|
1654 // We expect to fail here, at the beginning of a record, so not being |
|
1655 // able to read another mopt value should not result in an error. |
|
1656 |
604
|
1657 is.read (&mopt, 4); |
|
1658 if (! is) |
911
|
1659 return 1; |
604
|
1660 |
|
1661 if (! is.read (&nr, 4)) |
|
1662 goto data_read_error; |
|
1663 |
|
1664 if (! is.read (&nc, 4)) |
|
1665 goto data_read_error; |
|
1666 |
|
1667 if (! is.read (&imag, 4)) |
|
1668 goto data_read_error; |
|
1669 |
|
1670 if (! is.read (&len, 4)) |
|
1671 goto data_read_error; |
|
1672 |
|
1673 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
1674 // bigger than we expect, so we swap bytes. |
|
1675 // |
|
1676 // If mopt is zero, it means the file was written on a little endian |
|
1677 // machine, and we only need to swap if we are running on a big endian |
|
1678 // machine. |
|
1679 // |
|
1680 // Gag me. |
|
1681 |
|
1682 #if defined (WORDS_BIGENDIAN) |
|
1683 if (mopt == 0) |
|
1684 swap = 1; |
|
1685 #endif |
|
1686 |
911
|
1687 // mopt is signed, therefore byte swap may result in negative value. |
|
1688 |
|
1689 if (mopt > 9999 || mopt < 0) |
604
|
1690 swap = 1; |
|
1691 |
|
1692 if (swap) |
|
1693 { |
|
1694 swap_4_bytes ((char *) &mopt); |
|
1695 swap_4_bytes ((char *) &nr); |
|
1696 swap_4_bytes ((char *) &nc); |
|
1697 swap_4_bytes ((char *) &imag); |
|
1698 swap_4_bytes ((char *) &len); |
|
1699 } |
|
1700 |
911
|
1701 if (mopt > 9999 || mopt < 0 || imag > 1 || imag < 0) |
604
|
1702 { |
|
1703 if (! quiet) |
|
1704 error ("load: can't read binary file"); |
|
1705 return -1; |
|
1706 } |
|
1707 |
|
1708 return 0; |
|
1709 |
|
1710 data_read_error: |
|
1711 return -1; |
|
1712 } |
|
1713 |
617
|
1714 // We don't just use a cast here, because we need to be able to detect |
|
1715 // possible errors. |
|
1716 |
|
1717 static floating_point_format |
|
1718 get_floating_point_format (int mach) |
|
1719 { |
619
|
1720 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
1721 |
617
|
1722 switch (mach) |
|
1723 { |
|
1724 case 0: |
|
1725 flt_fmt = LS_IEEE_LITTLE; |
|
1726 break; |
|
1727 |
|
1728 case 1: |
|
1729 flt_fmt = LS_IEEE_BIG; |
|
1730 break; |
|
1731 |
|
1732 case 2: |
|
1733 flt_fmt = LS_VAX_D; |
|
1734 break; |
|
1735 |
|
1736 case 3: |
|
1737 flt_fmt = LS_VAX_G; |
|
1738 break; |
|
1739 |
|
1740 case 4: |
|
1741 flt_fmt = LS_CRAY; |
|
1742 break; |
|
1743 |
|
1744 default: |
619
|
1745 flt_fmt = LS_UNKNOWN_FLT_FMT; |
617
|
1746 break; |
|
1747 } |
619
|
1748 |
|
1749 return flt_fmt; |
617
|
1750 } |
619
|
1751 |
604
|
1752 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
1753 // place it in TC, returning the name of the variable. |
|
1754 // |
|
1755 // The data is expected to be in Matlab's .mat format, though not all |
|
1756 // the features of that format are supported. |
|
1757 // |
|
1758 // FILENAME is used for error messages. |
|
1759 // |
|
1760 // This format provides no way to tag the data as global. |
|
1761 |
|
1762 static char * |
|
1763 read_mat_binary_data (istream& is, const char *filename, |
|
1764 tree_constant& tc) |
|
1765 { |
|
1766 // These are initialized here instead of closer to where they are |
|
1767 // first used to avoid errors from gcc about goto crossing |
|
1768 // initialization of variable. |
|
1769 |
|
1770 Matrix re; |
|
1771 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
1772 char *name = 0; |
|
1773 int swap = 0, type = 0, prec = 0, mach = 0, dlen = 0; |
|
1774 |
|
1775 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
1776 |
|
1777 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
1778 if (err) |
|
1779 { |
|
1780 if (err < 0) |
|
1781 goto data_read_error; |
|
1782 else |
|
1783 return 0; |
|
1784 } |
|
1785 |
|
1786 type = mopt % 10; // Full, sparse, etc. |
|
1787 mopt /= 10; // Eliminate first digit. |
|
1788 prec = mopt % 10; // double, float, int, etc. |
|
1789 mopt /= 100; // Skip unused third digit too. |
|
1790 mach = mopt % 10; // IEEE, VAX, etc. |
|
1791 |
617
|
1792 flt_fmt = get_floating_point_format (mach); |
|
1793 if (flt_fmt == LS_UNKNOWN_FLT_FMT) |
604
|
1794 { |
|
1795 error ("load: unrecognized binary format!"); |
|
1796 return 0; |
|
1797 } |
|
1798 |
|
1799 if (type != 0 && type != 1) |
|
1800 { |
|
1801 error ("load: can't read sparse matrices"); |
|
1802 return 0; |
|
1803 } |
|
1804 |
|
1805 if (imag && type == 1) |
|
1806 { |
|
1807 error ("load: encountered complex matrix with string flag set!"); |
|
1808 return 0; |
|
1809 } |
|
1810 |
|
1811 name = new char [len]; |
|
1812 if (! is.read (name, len)) |
|
1813 goto data_read_error; |
|
1814 |
|
1815 dlen = nr * nc; |
|
1816 if (dlen < 0) |
|
1817 goto data_read_error; |
|
1818 |
|
1819 re.resize (nr, nc); |
|
1820 |
|
1821 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1822 |
|
1823 if (! is || error_state) |
|
1824 { |
|
1825 error ("load: reading matrix data for `%s'", name); |
|
1826 goto data_read_error; |
|
1827 } |
|
1828 |
|
1829 if (imag) |
|
1830 { |
|
1831 Matrix im (nr, nc); |
|
1832 |
|
1833 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1834 |
|
1835 if (! is || error_state) |
|
1836 { |
|
1837 error ("load: reading imaginary matrix data for `%s'", name); |
|
1838 goto data_read_error; |
|
1839 } |
|
1840 |
|
1841 ComplexMatrix ctmp (nr, nc); |
|
1842 |
|
1843 for (int j = 0; j < nc; j++) |
|
1844 for (int i = 0; i < nr; i++) |
|
1845 ctmp.elem (i, j) = Complex (re.elem (i, j), im.elem (i, j)); |
|
1846 |
|
1847 tc = ctmp; |
|
1848 } |
|
1849 else |
|
1850 tc = re; |
|
1851 |
1097
|
1852 // XXX FIXME XXX -- this needs to change once strings really work. |
|
1853 |
|
1854 if (type == 1 && nr == 1) |
604
|
1855 tc = tc.convert_to_str (); |
|
1856 |
|
1857 return name; |
|
1858 |
|
1859 data_read_error: |
|
1860 error ("load: trouble reading binary file `%s'", filename); |
|
1861 delete [] name; |
|
1862 return 0; |
|
1863 } |
|
1864 |
|
1865 // Return nonzero if NAME matches one of the given globbing PATTERNS. |
|
1866 |
|
1867 static int |
|
1868 matches_patterns (char **patterns, int num_pat, char *name) |
|
1869 { |
|
1870 while (num_pat-- > 0) |
|
1871 { |
|
1872 if (fnmatch (*patterns++, name, __FNM_FLAGS) == 0) |
|
1873 return 1; |
|
1874 } |
|
1875 return 0; |
|
1876 } |
|
1877 |
|
1878 static int |
|
1879 read_binary_file_header (istream& is, int& swap, |
630
|
1880 floating_point_format& flt_fmt, int quiet = 0) |
604
|
1881 { |
|
1882 int magic_len = 10; |
|
1883 char magic [magic_len+1]; |
|
1884 is.read (magic, magic_len); |
|
1885 magic[magic_len] = '\0'; |
|
1886 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
|
1887 { |
|
1888 #if defined (WORDS_BIGENDIAN) |
|
1889 swap = 1; |
|
1890 #else |
|
1891 swap = 0; |
|
1892 #endif |
|
1893 } |
|
1894 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
|
1895 { |
|
1896 #if defined (WORDS_BIGENDIAN) |
|
1897 swap = 0; |
|
1898 #else |
|
1899 swap = 1; |
|
1900 #endif |
|
1901 } |
|
1902 else |
|
1903 { |
|
1904 if (! quiet) |
|
1905 error ("load: can't read binary file"); |
|
1906 return -1; |
|
1907 } |
|
1908 |
|
1909 char tmp = 0; |
|
1910 is.read (&tmp, 1); |
|
1911 |
617
|
1912 flt_fmt = get_floating_point_format (tmp); |
|
1913 if (flt_fmt == LS_UNKNOWN_FLT_FMT) |
604
|
1914 { |
|
1915 if (! quiet) |
|
1916 error ("load: unrecognized binary format!"); |
|
1917 return -1; |
|
1918 } |
|
1919 |
|
1920 return 0; |
|
1921 } |
|
1922 |
|
1923 static load_save_format |
|
1924 get_file_format (const char *fname, const char *orig_fname) |
|
1925 { |
|
1926 load_save_format retval = LS_UNKNOWN; |
|
1927 |
984
|
1928 ifstream file (fname); |
604
|
1929 |
|
1930 if (! file) |
|
1931 { |
|
1932 error ("load: couldn't open input file `%s'", orig_fname); |
|
1933 return retval; |
|
1934 } |
|
1935 |
|
1936 int swap; |
|
1937 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
1938 |
|
1939 if (read_binary_file_header (file, swap, flt_fmt, 1) == 0) |
|
1940 retval = LS_BINARY; |
|
1941 else |
|
1942 { |
|
1943 file.seekg (0, ios::beg); |
|
1944 |
|
1945 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
1946 int swap; |
|
1947 |
|
1948 if (read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1) == 0) |
|
1949 retval = LS_MAT_BINARY; |
|
1950 else |
|
1951 { |
|
1952 file.seekg (0, ios::beg); |
|
1953 |
|
1954 char *tmp = extract_keyword (file, "name"); |
|
1955 if (tmp) |
|
1956 retval = LS_ASCII; |
|
1957 |
|
1958 delete [] tmp; |
|
1959 } |
|
1960 } |
|
1961 |
|
1962 file.close (); |
|
1963 |
|
1964 if (retval == LS_UNKNOWN) |
|
1965 error ("load: unable to determine file format for `%s'", orig_fname); |
|
1966 |
|
1967 return retval; |
|
1968 } |
|
1969 |
863
|
1970 static Octave_object |
|
1971 do_load (istream& stream, const char *orig_fname, int force, |
|
1972 load_save_format format, floating_point_format flt_fmt, |
|
1973 int list_only, int swap, int verbose, char **argv, |
|
1974 int argc, int nargout) |
604
|
1975 { |
|
1976 Octave_object retval; |
|
1977 |
621
|
1978 ostrstream output_buf; |
604
|
1979 int count = 0; |
|
1980 for (;;) |
|
1981 { |
|
1982 int global = 0; |
|
1983 tree_constant tc; |
|
1984 |
|
1985 char *name = 0; |
|
1986 char *doc = 0; |
|
1987 |
|
1988 switch (format) |
|
1989 { |
|
1990 case LS_ASCII: |
|
1991 name = read_ascii_data (stream, orig_fname, global, tc); |
|
1992 break; |
|
1993 |
|
1994 case LS_BINARY: |
|
1995 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
1996 global, tc, doc); |
|
1997 break; |
|
1998 |
|
1999 case LS_MAT_BINARY: |
|
2000 name = read_mat_binary_data (stream, orig_fname, tc); |
|
2001 break; |
|
2002 |
|
2003 default: |
775
|
2004 gripe_unrecognized_data_fmt ("load"); |
604
|
2005 break; |
|
2006 } |
|
2007 |
867
|
2008 if (error_state || stream.eof () || ! name) |
604
|
2009 { |
867
|
2010 delete [] name; |
|
2011 delete [] doc; |
604
|
2012 break; |
|
2013 } |
|
2014 else if (! error_state && name) |
|
2015 { |
|
2016 if (tc.is_defined ()) |
|
2017 { |
|
2018 if (argc == 0 || matches_patterns (argv, argc, name)) |
|
2019 { |
|
2020 count++; |
621
|
2021 if (list_only) |
|
2022 { |
|
2023 if (verbose) |
|
2024 { |
|
2025 if (count == 1) |
|
2026 output_buf |
|
2027 << "type rows cols name\n" |
|
2028 << "==== ==== ==== ====\n"; |
|
2029 |
|
2030 output_buf.form ("%-16s", tc.type_as_string ()); |
|
2031 output_buf.form ("%7d", tc.rows ()); |
|
2032 output_buf.form ("%7d", tc.columns ()); |
|
2033 output_buf << " "; |
|
2034 } |
|
2035 output_buf << name << "\n"; |
|
2036 } |
|
2037 else |
|
2038 { |
|
2039 install_loaded_variable (force, name, tc, global, doc); |
|
2040 } |
604
|
2041 } |
|
2042 } |
|
2043 else |
|
2044 error ("load: unable to load variable `%s'", name); |
|
2045 } |
|
2046 else |
|
2047 { |
|
2048 if (count == 0) |
|
2049 error ("load: are you sure `%s' is an Octave data file?", |
|
2050 orig_fname); |
|
2051 |
867
|
2052 delete [] name; |
|
2053 delete [] doc; |
604
|
2054 break; |
|
2055 } |
|
2056 |
|
2057 delete [] name; |
|
2058 delete [] doc; |
|
2059 } |
|
2060 |
621
|
2061 if (list_only && count) |
|
2062 { |
|
2063 if (nargout > 0) |
|
2064 { |
|
2065 output_buf << ends; |
|
2066 char *msg = output_buf.str (); |
|
2067 retval = msg; |
|
2068 delete [] msg; |
|
2069 } |
|
2070 else |
|
2071 maybe_page_output (output_buf); |
|
2072 } |
|
2073 |
863
|
2074 return retval; |
|
2075 } |
|
2076 |
|
2077 DEFUN_TEXT ("load", Fload, Sload, -1, 1, |
910
|
2078 "load [-force] [-ascii] [-binary] [-mat-binary] file [pattern ...]\n\ |
863
|
2079 \n\ |
|
2080 Load variables from a file.\n\ |
|
2081 \n\ |
|
2082 If no argument is supplied to select a format, load tries to read the |
|
2083 named file as an Octave binary, then as a .mat file, and then as an |
|
2084 Octave text file.\n\ |
|
2085 \n\ |
|
2086 If the option -force is given, variables with the same names as those |
|
2087 found in the file will be replaced with the values read from the file.") |
|
2088 { |
|
2089 Octave_object retval; |
|
2090 |
|
2091 DEFINE_ARGV ("load"); |
|
2092 |
|
2093 argc--; |
|
2094 argv++; |
|
2095 |
|
2096 int force = 0; |
|
2097 |
|
2098 // It isn't necessary to have the default load format stored in a user |
|
2099 // preference variable since we can determine the type of file as we |
|
2100 // are reading. |
|
2101 |
|
2102 load_save_format format = LS_UNKNOWN; |
|
2103 |
|
2104 int list_only = 0; |
|
2105 int verbose = 0; |
|
2106 |
|
2107 while (argc > 0) |
|
2108 { |
|
2109 if (strcmp (*argv, "-force") == 0 || strcmp (*argv, "-f") == 0) |
|
2110 { |
|
2111 force++; |
|
2112 argc--; |
|
2113 argv++; |
|
2114 } |
|
2115 else if (strcmp (*argv, "-list") == 0 || strcmp (*argv, "-l") == 0) |
|
2116 { |
|
2117 list_only = 1; |
|
2118 argc--; |
|
2119 argv++; |
|
2120 } |
|
2121 else if (strcmp (*argv, "-verbose") == 0 || strcmp (*argv, "-v") == 0) |
|
2122 { |
|
2123 verbose = 1; |
|
2124 argc--; |
|
2125 argv++; |
|
2126 } |
|
2127 else if (strcmp (*argv, "-ascii") == 0 || strcmp (*argv, "-a") == 0) |
|
2128 { |
|
2129 format = LS_ASCII; |
|
2130 argc--; |
|
2131 argv++; |
|
2132 } |
|
2133 else if (strcmp (*argv, "-binary") == 0 || strcmp (*argv, "-b") == 0) |
|
2134 { |
|
2135 format = LS_BINARY; |
|
2136 argc--; |
|
2137 argv++; |
|
2138 } |
|
2139 else if (strcmp (*argv, "-mat-binary") == 0 || strcmp (*argv, "-m") == 0) |
|
2140 { |
|
2141 format = LS_MAT_BINARY; |
|
2142 argc--; |
|
2143 argv++; |
|
2144 } |
|
2145 else |
|
2146 break; |
|
2147 } |
|
2148 |
|
2149 if (argc < 1) |
|
2150 { |
|
2151 error ("load: you must specify a single file to read"); |
|
2152 DELETE_ARGV; |
|
2153 return retval; |
|
2154 } |
|
2155 |
|
2156 char *orig_fname = *argv; |
|
2157 |
|
2158 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
2159 |
|
2160 int swap = 0; |
|
2161 |
|
2162 if (strcmp (*argv, "-") == 0) |
|
2163 { |
|
2164 argc--; |
|
2165 argv++; |
|
2166 |
|
2167 if (format != LS_UNKNOWN) |
|
2168 { |
|
2169 // XXX FIXME XXX -- if we have already seen EOF on a previous call, |
|
2170 // how do we fix up the state of cin so that we can get additional |
|
2171 // input? I'm afraid that we can't fix this using cin only. |
|
2172 |
|
2173 retval = do_load (cin, orig_fname, force, format, flt_fmt, |
|
2174 list_only, swap, verbose, argv, argc, |
|
2175 nargout); |
|
2176 } |
|
2177 else |
|
2178 error ("load: must specify file format if reading from stdin"); |
|
2179 } |
|
2180 else |
|
2181 { |
1158
|
2182 static char *fname = 0; |
|
2183 |
|
2184 if (fname) |
|
2185 free (fname); |
|
2186 |
|
2187 fname = tilde_expand (*argv); |
863
|
2188 |
|
2189 if (format == LS_UNKNOWN) |
|
2190 format = get_file_format (fname, orig_fname); |
|
2191 |
|
2192 if (format != LS_UNKNOWN) |
|
2193 { |
|
2194 argv++; |
|
2195 argc--; |
|
2196 |
|
2197 unsigned mode = ios::in; |
|
2198 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2199 mode |= ios::bin; |
|
2200 |
|
2201 ifstream file (fname, mode); |
|
2202 |
|
2203 if (file) |
|
2204 { |
|
2205 if (format == LS_BINARY) |
|
2206 { |
|
2207 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
2208 { |
|
2209 file.close (); |
|
2210 DELETE_ARGV; |
|
2211 return retval; |
|
2212 } |
|
2213 } |
|
2214 |
|
2215 retval = do_load (file, orig_fname, force, format, |
|
2216 flt_fmt, list_only, swap, verbose, |
|
2217 argv, argc, nargout); |
|
2218 |
|
2219 file.close (); |
|
2220 } |
|
2221 else |
|
2222 error ("load: couldn't open input file `%s'", orig_fname); |
|
2223 } |
|
2224 } |
604
|
2225 |
|
2226 DELETE_ARGV; |
|
2227 |
|
2228 return retval; |
|
2229 } |
|
2230 |
|
2231 // Return nonzero if PATTERN has any special globbing chars in it. |
|
2232 |
|
2233 static int |
|
2234 glob_pattern_p (char *pattern) |
|
2235 { |
|
2236 char *p = pattern; |
|
2237 char c; |
|
2238 int open = 0; |
|
2239 |
|
2240 while ((c = *p++) != '\0') |
|
2241 { |
|
2242 switch (c) |
|
2243 { |
|
2244 case '?': |
|
2245 case '*': |
|
2246 return 1; |
|
2247 |
|
2248 case '[': // Only accept an open brace if there is a close |
|
2249 open++; // brace to match it. Bracket expressions must be |
|
2250 continue; // complete, according to Posix.2 |
|
2251 |
|
2252 case ']': |
|
2253 if (open) |
|
2254 return 1; |
|
2255 continue; |
|
2256 |
|
2257 case '\\': |
|
2258 if (*p++ == '\0') |
|
2259 return 0; |
|
2260 |
|
2261 default: |
|
2262 continue; |
|
2263 } |
|
2264 } |
|
2265 |
|
2266 return 0; |
|
2267 } |
|
2268 |
618
|
2269 // MAX_VAL and MIN_VAL are assumed to have integral values even though |
|
2270 // they are stored in doubles. |
|
2271 |
604
|
2272 static save_type |
|
2273 get_save_type (double max_val, double min_val) |
|
2274 { |
|
2275 save_type st = LS_DOUBLE; |
|
2276 |
|
2277 if (max_val < 256 && min_val > -1) |
|
2278 st = LS_U_CHAR; |
|
2279 else if (max_val < 65536 && min_val > -1) |
|
2280 st = LS_U_SHORT; |
618
|
2281 else if (max_val < 4294967295 && min_val > -1) |
|
2282 st = LS_U_INT; |
|
2283 else if (max_val < 128 && min_val >= -128) |
|
2284 st = LS_CHAR; |
604
|
2285 else if (max_val < 32768 && min_val >= -32768) |
|
2286 st = LS_SHORT; |
|
2287 else if (max_val < 2147483648 && min_val > -2147483648) |
|
2288 st = LS_INT; |
|
2289 |
|
2290 return st; |
|
2291 } |
|
2292 |
|
2293 // Save the data from TC along with the corresponding NAME, help |
|
2294 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
|
2295 // binary format described above for load_binary_data. |
|
2296 |
|
2297 static int |
|
2298 save_binary_data (ostream& os, const tree_constant& tc, char *name, |
630
|
2299 char *doc, int mark_as_global, int save_as_floats) |
604
|
2300 { |
620
|
2301 int fail = 0; |
|
2302 |
604
|
2303 FOUR_BYTE_INT name_len = 0; |
|
2304 if (name) |
|
2305 name_len = strlen (name); |
|
2306 |
|
2307 os.write (&name_len, 4); |
|
2308 os.write (name, name_len); |
|
2309 |
|
2310 FOUR_BYTE_INT doc_len = 0; |
|
2311 if (doc) |
|
2312 doc_len = strlen (doc); |
|
2313 |
|
2314 os.write (&doc_len, 4); |
|
2315 os.write (doc, doc_len); |
|
2316 |
|
2317 char tmp; |
|
2318 |
|
2319 tmp = mark_as_global; |
|
2320 os.write (&tmp, 1); |
|
2321 |
620
|
2322 if (tc.is_real_scalar ()) |
604
|
2323 { |
|
2324 tmp = 1; |
|
2325 os.write (&tmp, 1); |
630
|
2326 tmp = (char) LS_DOUBLE; |
|
2327 os.write (&tmp, 1); |
604
|
2328 double tmp = tc.double_value (); |
|
2329 os.write (&tmp, 8); |
|
2330 } |
620
|
2331 else if (tc.is_real_matrix ()) |
604
|
2332 { |
|
2333 tmp = 2; |
|
2334 os.write (&tmp, 1); |
|
2335 Matrix m = tc.matrix_value (); |
|
2336 FOUR_BYTE_INT nr = m.rows (); |
|
2337 FOUR_BYTE_INT nc = m.columns (); |
|
2338 os.write (&nr, 4); |
|
2339 os.write (&nc, 4); |
|
2340 int len = nr * nc; |
|
2341 save_type st = LS_DOUBLE; |
630
|
2342 if (save_as_floats) |
|
2343 { |
|
2344 if (too_large_for_float (m)) |
|
2345 { |
|
2346 warning ("save: some values too large to save as floats --"); |
|
2347 warning ("save: saving as doubles instead"); |
|
2348 } |
|
2349 else |
|
2350 st = LS_FLOAT; |
|
2351 } |
|
2352 else if (len > 8192) // XXX FIXME XXX -- make this configurable. |
604
|
2353 { |
|
2354 double max_val, min_val; |
|
2355 if (all_parts_int (m, max_val, min_val)) |
|
2356 st = get_save_type (max_val, min_val); |
|
2357 } |
630
|
2358 const double *mtmp = m.data (); |
604
|
2359 write_doubles (os, mtmp, st, len); |
|
2360 } |
|
2361 else if (tc.is_complex_scalar ()) |
|
2362 { |
|
2363 tmp = 3; |
|
2364 os.write (&tmp, 1); |
630
|
2365 tmp = (char) LS_DOUBLE; |
|
2366 os.write (&tmp, 1); |
604
|
2367 Complex tmp = tc.complex_value (); |
|
2368 os.write (&tmp, 16); |
|
2369 } |
|
2370 else if (tc.is_complex_matrix ()) |
|
2371 { |
|
2372 tmp = 4; |
|
2373 os.write (&tmp, 1); |
|
2374 ComplexMatrix m = tc.complex_matrix_value (); |
|
2375 FOUR_BYTE_INT nr = m.rows (); |
|
2376 FOUR_BYTE_INT nc = m.columns (); |
|
2377 os.write (&nr, 4); |
|
2378 os.write (&nc, 4); |
|
2379 int len = nr * nc; |
|
2380 save_type st = LS_DOUBLE; |
630
|
2381 if (save_as_floats) |
|
2382 { |
|
2383 if (too_large_for_float (m)) |
|
2384 { |
|
2385 warning ("save: some values too large to save as floats --"); |
|
2386 warning ("save: saving as doubles instead"); |
|
2387 } |
|
2388 else |
|
2389 st = LS_FLOAT; |
|
2390 } |
|
2391 else if (len > 4096) // XXX FIXME XXX -- make this configurable. |
604
|
2392 { |
|
2393 double max_val, min_val; |
|
2394 if (all_parts_int (m, max_val, min_val)) |
|
2395 st = get_save_type (max_val, min_val); |
|
2396 } |
630
|
2397 const Complex *mtmp = m.data (); |
|
2398 write_doubles (os, (const double *) mtmp, st, 2*len); |
604
|
2399 } |
|
2400 else if (tc.is_string ()) |
|
2401 { |
|
2402 tmp = 5; |
|
2403 os.write (&tmp, 1); |
|
2404 int nr = tc.rows (); |
|
2405 int nc = tc.columns (); |
|
2406 FOUR_BYTE_INT len = nr * nc; |
|
2407 os.write (&len, 4); |
|
2408 char *s = tc.string_value (); |
|
2409 os.write (s, len); |
|
2410 } |
|
2411 else if (tc.is_range ()) |
|
2412 { |
|
2413 tmp = 6; |
|
2414 os.write (&tmp, 1); |
630
|
2415 tmp = (char) LS_DOUBLE; |
|
2416 os.write (&tmp, 1); |
604
|
2417 Range r = tc.range_value (); |
|
2418 double bas = r.base (); |
|
2419 double lim = r.limit (); |
|
2420 double inc = r.inc (); |
|
2421 os.write (&bas, 8); |
|
2422 os.write (&lim, 8); |
|
2423 os.write (&inc, 8); |
|
2424 } |
|
2425 else |
620
|
2426 { |
|
2427 gripe_wrong_type_arg ("save", tc); |
|
2428 fail = 1; |
|
2429 } |
604
|
2430 |
620
|
2431 return (os && ! fail); |
604
|
2432 } |
|
2433 |
667
|
2434 // Save the data from TC along with the corresponding NAME on stream OS |
|
2435 // in the MatLab binary format. |
|
2436 |
|
2437 static int |
|
2438 save_mat_binary_data (ostream& os, const tree_constant& tc, char *name) |
|
2439 { |
|
2440 int fail = 0; |
|
2441 |
|
2442 FOUR_BYTE_INT mopt = 0; |
|
2443 |
|
2444 mopt += tc.is_string () ? 1 : 0; |
|
2445 mopt += 1000 * get_floating_point_format (NATIVE_FLOAT_FORMAT); |
|
2446 |
|
2447 os.write (&mopt, 4); |
|
2448 |
|
2449 FOUR_BYTE_INT nr = tc.rows (); |
|
2450 os.write (&nr, 4); |
|
2451 |
|
2452 FOUR_BYTE_INT nc = tc.columns (); |
|
2453 os.write (&nc, 4); |
|
2454 |
|
2455 int len = nr * nc; |
|
2456 |
|
2457 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; |
|
2458 os.write (&imag, 4); |
|
2459 |
|
2460 FOUR_BYTE_INT name_len = name ? strlen (name) + 1 : 0; |
|
2461 |
|
2462 os.write (&name_len, 4); |
|
2463 os.write (name, name_len); |
|
2464 |
|
2465 if (tc.is_real_scalar ()) |
|
2466 { |
|
2467 double tmp = tc.double_value (); |
|
2468 os.write (&tmp, 8); |
|
2469 } |
911
|
2470 else if (tc.is_real_matrix ()) |
667
|
2471 { |
|
2472 Matrix m = tc.matrix_value (); |
|
2473 os.write (m.data (), 8 * len); |
|
2474 } |
|
2475 else if (tc.is_complex_scalar ()) |
|
2476 { |
|
2477 Complex tmp = tc.complex_value (); |
|
2478 os.write (&tmp, 16); |
|
2479 } |
|
2480 else if (tc.is_complex_matrix ()) |
|
2481 { |
|
2482 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
2483 Matrix m = ::real(m_cmplx); |
|
2484 os.write (m.data (), 8 * len); |
|
2485 m = ::imag(m_cmplx); |
|
2486 os.write (m.data (), 8 * len); |
|
2487 } |
|
2488 else if (tc.is_string ()) |
|
2489 { |
|
2490 begin_unwind_frame ("save_mat_binary_data"); |
|
2491 unwind_protect_int (user_pref.implicit_str_to_num_ok); |
|
2492 user_pref.implicit_str_to_num_ok = 1; |
|
2493 Matrix m = tc.matrix_value (); |
|
2494 os.write (m.data (), 8 * len); |
|
2495 run_unwind_frame ("save_mat_binary_data"); |
|
2496 } |
911
|
2497 else if (tc.is_range ()) |
|
2498 { |
|
2499 Range r = tc.range_value (); |
|
2500 double base = r.base (); |
|
2501 double inc = r.inc (); |
|
2502 int nel = r.nelem (); |
|
2503 for (int i = 0; i < nel; i++) |
|
2504 { |
|
2505 double x = base + i * inc; |
|
2506 os.write (&x, 8); |
|
2507 } |
|
2508 } |
667
|
2509 else |
|
2510 { |
|
2511 gripe_wrong_type_arg ("save", tc); |
|
2512 fail = 1; |
|
2513 } |
|
2514 |
|
2515 return (os && ! fail); |
|
2516 } |
|
2517 |
620
|
2518 static void |
|
2519 ascii_save_type (ostream& os, char *type, int mark_as_global) |
|
2520 { |
|
2521 if (mark_as_global) |
|
2522 os << "# type: global "; |
|
2523 else |
|
2524 os << "# type: "; |
|
2525 |
|
2526 os << type << "\n"; |
|
2527 } |
|
2528 |
872
|
2529 static Matrix |
|
2530 strip_infnan (const Matrix& m) |
|
2531 { |
|
2532 int nr = m.rows (); |
|
2533 int nc = m.columns (); |
|
2534 |
|
2535 Matrix retval (nr, nc); |
|
2536 |
|
2537 int k = 0; |
|
2538 for (int i = 0; i < nr; i++) |
|
2539 { |
|
2540 for (int j = 0; j < nc; j++) |
|
2541 { |
|
2542 double d = m.elem (i, j); |
|
2543 if (xisnan (d)) |
|
2544 goto next_row; |
|
2545 else |
|
2546 retval.elem (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
2547 } |
|
2548 k++; |
|
2549 |
|
2550 next_row: |
|
2551 continue; |
|
2552 } |
|
2553 |
|
2554 if (k > 0) |
|
2555 retval.resize (k, nc); |
|
2556 |
|
2557 return retval; |
|
2558 } |
|
2559 |
|
2560 static ComplexMatrix |
|
2561 strip_infnan (const ComplexMatrix& m) |
|
2562 { |
|
2563 int nr = m.rows (); |
|
2564 int nc = m.columns (); |
|
2565 |
|
2566 ComplexMatrix retval (nr, nc); |
|
2567 |
|
2568 int k = 0; |
|
2569 for (int i = 0; i < nr; i++) |
|
2570 { |
|
2571 for (int j = 0; j < nc; j++) |
|
2572 { |
|
2573 Complex c = m.elem (i, j); |
|
2574 if (xisnan (c)) |
|
2575 goto next_row; |
|
2576 else |
|
2577 { |
|
2578 double re = real (c); |
|
2579 double im = imag (c); |
|
2580 |
|
2581 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
2582 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
2583 |
|
2584 retval.elem (k, j) = Complex (re, im); |
|
2585 } |
|
2586 } |
|
2587 k++; |
|
2588 |
|
2589 next_row: |
|
2590 continue; |
|
2591 } |
|
2592 |
|
2593 if (k > 0) |
|
2594 retval.resize (k, nc); |
|
2595 |
|
2596 return retval; |
|
2597 } |
|
2598 |
620
|
2599 // Save the data from TC along with the corresponding NAME, and global |
604
|
2600 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
|
2601 // above for load_ascii_data. If NAME is null, the name: line is not |
|
2602 // generated. PRECISION specifies the number of decimal digits to print. |
872
|
2603 // If STRIP_NAN_AND_INF is nonzero, rows containing NaNs are deleted, |
|
2604 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
2605 // but not so big that gnuplot can't handle it when trying to compute |
|
2606 // axis ranges, etc.). |
|
2607 // |
|
2608 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
2609 // |
|
2610 // Returns 1 for success and 0 for failure. |
604
|
2611 |
|
2612 // XXX FIXME XXX -- should probably write the help string here too. |
|
2613 |
|
2614 int |
620
|
2615 save_ascii_data (ostream& os, const tree_constant& tc, |
872
|
2616 char *name, int strip_nan_and_inf, |
|
2617 int mark_as_global, int precision) |
604
|
2618 { |
872
|
2619 int success = 1; |
620
|
2620 |
604
|
2621 if (! precision) |
|
2622 precision = user_pref.save_precision; |
|
2623 |
|
2624 if (name) |
|
2625 os << "# name: " << name << "\n"; |
|
2626 |
|
2627 long old_precision = os.precision (); |
|
2628 os.precision (precision); |
|
2629 |
620
|
2630 if (tc.is_real_scalar ()) |
|
2631 { |
|
2632 ascii_save_type (os, "scalar", mark_as_global); |
872
|
2633 |
|
2634 double d = tc.double_value (); |
|
2635 if (strip_nan_and_inf) |
|
2636 { |
|
2637 if (xisnan (d)) |
|
2638 { |
|
2639 error ("only value to plot is NaN"); |
|
2640 success = 0; |
|
2641 } |
|
2642 else |
|
2643 { |
|
2644 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
2645 os << d << "\n"; |
|
2646 } |
|
2647 } |
|
2648 else |
|
2649 os << d << "\n"; |
620
|
2650 } |
|
2651 else if (tc.is_real_matrix ()) |
|
2652 { |
|
2653 ascii_save_type (os, "matrix", mark_as_global); |
|
2654 os << "# rows: " << tc.rows () << "\n" |
872
|
2655 << "# columns: " << tc.columns () << "\n"; |
|
2656 |
|
2657 Matrix tmp = tc.matrix_value (); |
|
2658 if (strip_nan_and_inf) |
|
2659 tmp = strip_infnan (tmp); |
|
2660 |
|
2661 os << tmp; |
620
|
2662 } |
|
2663 else if (tc.is_complex_scalar ()) |
|
2664 { |
|
2665 ascii_save_type (os, "complex scalar", mark_as_global); |
872
|
2666 |
|
2667 Complex c = tc.complex_value (); |
|
2668 if (strip_nan_and_inf) |
|
2669 { |
|
2670 if (xisnan (c)) |
|
2671 { |
|
2672 error ("only value to plot is NaN"); |
|
2673 success = 0; |
|
2674 } |
|
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 c = Complex (re, im); |
|
2684 |
|
2685 os << c << "\n"; |
|
2686 } |
|
2687 } |
|
2688 else |
|
2689 os << c << "\n"; |
620
|
2690 } |
|
2691 else if (tc.is_complex_matrix ()) |
604
|
2692 { |
620
|
2693 ascii_save_type (os, "complex matrix", mark_as_global); |
|
2694 os << "# rows: " << tc.rows () << "\n" |
875
|
2695 << "# columns: " << tc.columns () << "\n"; |
|
2696 |
|
2697 ComplexMatrix tmp = tc.complex_matrix_value (); |
872
|
2698 if (strip_nan_and_inf) |
|
2699 tmp = strip_infnan (tmp); |
|
2700 |
|
2701 os << tmp; |
620
|
2702 } |
|
2703 else if (tc.is_string ()) |
|
2704 { |
|
2705 ascii_save_type (os, "string", mark_as_global); |
|
2706 char *tmp = tc.string_value (); |
|
2707 os << "# length: " << strlen (tmp) << "\n" |
|
2708 << tmp << "\n"; |
|
2709 } |
872
|
2710 else if (tc.is_range ()) |
620
|
2711 { |
|
2712 ascii_save_type (os, "range", mark_as_global); |
|
2713 Range tmp = tc.range_value (); |
|
2714 os << "# base, limit, increment\n" |
|
2715 << tmp.base () << " " |
|
2716 << tmp.limit () << " " |
|
2717 << tmp.inc () << "\n"; |
|
2718 } |
|
2719 else |
|
2720 { |
|
2721 gripe_wrong_type_arg ("save", tc); |
872
|
2722 success = 0; |
604
|
2723 } |
|
2724 |
|
2725 os.precision (old_precision); |
|
2726 |
872
|
2727 return (os && success); |
604
|
2728 } |
|
2729 |
|
2730 // Save the info from sr on stream os in the format specified by fmt. |
|
2731 |
|
2732 static void |
630
|
2733 do_save (ostream& os, symbol_record *sr, load_save_format fmt, |
|
2734 int save_as_floats) |
604
|
2735 { |
|
2736 if (! sr->is_variable ()) |
|
2737 { |
|
2738 error ("save: can only save variables, not functions"); |
|
2739 return; |
|
2740 } |
|
2741 |
|
2742 char *name = sr->name (); |
|
2743 char *help = sr->help (); |
|
2744 int global = sr->is_linked_to_global (); |
|
2745 tree_constant tc = *((tree_constant *) sr->def ()); |
|
2746 |
|
2747 if (! name || ! tc.is_defined ()) |
|
2748 return; |
|
2749 |
|
2750 switch (fmt) |
|
2751 { |
|
2752 case LS_ASCII: |
872
|
2753 save_ascii_data (os, tc, name, 0, global); |
604
|
2754 break; |
|
2755 |
|
2756 case LS_BINARY: |
630
|
2757 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
2758 break; |
|
2759 |
667
|
2760 case LS_MAT_BINARY: |
|
2761 save_mat_binary_data (os, tc, name); |
|
2762 break; |
|
2763 |
604
|
2764 default: |
775
|
2765 gripe_unrecognized_data_fmt ("save"); |
604
|
2766 break; |
|
2767 } |
|
2768 } |
|
2769 |
|
2770 // Save variables with names matching PATTERN on stream OS in the |
|
2771 // format specified by FMT. If SAVE_BUILTINS is nonzero, also save |
|
2772 // builtin variables with names that match PATTERN. |
|
2773 |
|
2774 static int |
|
2775 save_vars (ostream& os, char *pattern, int save_builtins, |
630
|
2776 load_save_format fmt, int save_as_floats) |
604
|
2777 { |
|
2778 int count; |
|
2779 |
|
2780 symbol_record **vars = curr_sym_tab->glob |
|
2781 (count, pattern, symbol_def::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2782 |
|
2783 int saved = count; |
|
2784 |
|
2785 int i; |
|
2786 |
|
2787 for (i = 0; i < count; i++) |
620
|
2788 { |
630
|
2789 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2790 |
|
2791 if (error_state) |
|
2792 break; |
|
2793 } |
604
|
2794 |
|
2795 delete [] vars; |
|
2796 |
620
|
2797 if (! error_state && save_builtins) |
604
|
2798 { |
|
2799 symbol_record **vars = global_sym_tab->glob |
|
2800 (count, pattern, symbol_def::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2801 |
|
2802 saved += count; |
|
2803 |
|
2804 for (i = 0; i < count; i++) |
620
|
2805 { |
630
|
2806 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2807 |
|
2808 if (error_state) |
|
2809 break; |
|
2810 } |
604
|
2811 |
|
2812 delete [] vars; |
|
2813 } |
|
2814 |
|
2815 return saved; |
|
2816 } |
|
2817 |
|
2818 static load_save_format |
|
2819 get_default_save_format (void) |
|
2820 { |
|
2821 load_save_format retval = LS_ASCII; |
|
2822 |
|
2823 char *fmt = user_pref.default_save_format; |
|
2824 |
|
2825 if (strcasecmp (fmt, "binary") == 0) |
|
2826 retval = LS_BINARY; |
911
|
2827 else if (strcasecmp (fmt, "mat-binary") == 0 |
|
2828 || strcasecmp (fmt, "mat_binary") == 0) |
|
2829 retval = LS_MAT_BINARY; |
604
|
2830 |
|
2831 return retval; |
|
2832 } |
|
2833 |
863
|
2834 static void |
|
2835 write_binary_header (ostream& stream, load_save_format format) |
|
2836 { |
|
2837 if (format == LS_BINARY) |
|
2838 { |
|
2839 #if defined (WORDS_BIGENDIAN) |
|
2840 stream << "Octave-1-B"; |
|
2841 #else |
|
2842 stream << "Octave-1-L"; |
|
2843 #endif |
|
2844 |
|
2845 char tmp = (char) NATIVE_FLOAT_FORMAT; |
|
2846 stream.write (&tmp, 1); |
|
2847 } |
|
2848 } |
|
2849 |
|
2850 static void |
|
2851 save_vars (char **argv, int argc, ostream& os, int save_builtins, |
|
2852 load_save_format fmt, int save_as_floats) |
|
2853 { |
|
2854 write_binary_header (os, fmt); |
|
2855 |
|
2856 if (argc == 0) |
|
2857 { |
|
2858 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
|
2859 } |
|
2860 else |
|
2861 { |
|
2862 while (argc-- > 0) |
|
2863 { |
|
2864 if (! save_vars (os, *argv, save_builtins, fmt, save_as_floats)) |
|
2865 { |
|
2866 warning ("save: no such variable `%s'", *argv); |
|
2867 } |
|
2868 |
|
2869 argv++; |
|
2870 } |
|
2871 } |
|
2872 } |
|
2873 |
604
|
2874 DEFUN_TEXT ("save", Fsave, Ssave, -1, 1, |
910
|
2875 "save [-ascii] [-binary] [-float-binary] [-mat-binary] \n\ |
667
|
2876 [-save-builtins] file [pattern ...]\n\ |
604
|
2877 \n\ |
|
2878 save variables in a file") |
|
2879 { |
|
2880 Octave_object retval; |
|
2881 |
863
|
2882 DEFINE_ARGV ("save"); |
604
|
2883 |
|
2884 argc--; |
|
2885 argv++; |
|
2886 |
|
2887 // Here is where we would get the default save format if it were |
|
2888 // stored in a user preference variable. |
|
2889 |
|
2890 int save_builtins = 0; |
|
2891 |
630
|
2892 int save_as_floats = 0; |
|
2893 |
604
|
2894 load_save_format format = get_default_save_format (); |
|
2895 |
|
2896 while (argc > 0) |
|
2897 { |
|
2898 if (strcmp (*argv, "-ascii") == 0 || strcmp (*argv, "-a") == 0) |
|
2899 { |
|
2900 format = LS_ASCII; |
|
2901 argc--; |
|
2902 argv++; |
|
2903 } |
|
2904 else if (strcmp (*argv, "-binary") == 0 || strcmp (*argv, "-b") == 0) |
|
2905 { |
|
2906 format = LS_BINARY; |
|
2907 argc--; |
|
2908 argv++; |
|
2909 } |
667
|
2910 else if (strcmp (*argv, "-mat-binary") == 0 || strcmp (*argv, "-m") == 0) |
|
2911 { |
|
2912 format = LS_MAT_BINARY; |
|
2913 argc--; |
|
2914 argv++; |
|
2915 } |
630
|
2916 else if (strcmp (*argv, "-float-binary") == 0 |
|
2917 || strcmp (*argv, "-f") == 0) |
|
2918 { |
|
2919 format = LS_BINARY; |
|
2920 save_as_floats = 1; |
|
2921 argc--; |
|
2922 argv++; |
|
2923 } |
604
|
2924 else if (strcmp (*argv, "-save-builtins") == 0) |
|
2925 { |
|
2926 save_builtins = 1; |
|
2927 argc--; |
|
2928 argv++; |
|
2929 } |
|
2930 else |
|
2931 break; |
|
2932 } |
|
2933 |
|
2934 if (argc < 1) |
|
2935 { |
|
2936 print_usage ("save"); |
|
2937 DELETE_ARGV; |
|
2938 return retval; |
|
2939 } |
|
2940 |
630
|
2941 if (save_as_floats && format == LS_ASCII) |
|
2942 { |
|
2943 error ("save: cannot specify both -ascii and -float-binary"); |
|
2944 DELETE_ARGV; |
|
2945 return retval; |
|
2946 } |
|
2947 |
604
|
2948 if (strcmp (*argv, "-") == 0) |
|
2949 { |
863
|
2950 argc--; |
|
2951 argv++; |
|
2952 |
604
|
2953 // XXX FIXME XXX -- should things intended for the screen end up in a |
|
2954 // tree_constant (string)? |
863
|
2955 |
1043
|
2956 ostrstream buf; |
|
2957 |
|
2958 save_vars (argv, argc, buf, save_builtins, format, |
863
|
2959 save_as_floats); |
1043
|
2960 |
|
2961 maybe_page_output (buf); |
604
|
2962 } |
|
2963 else if (argc == 1 && glob_pattern_p (*argv)) // Guard against things |
|
2964 { // like `save a*', |
|
2965 print_usage ("save"); // which are probably |
|
2966 DELETE_ARGV; // mistakes... |
|
2967 return retval; |
|
2968 } |
|
2969 else |
|
2970 { |
1159
|
2971 static char *fname = 0; |
1158
|
2972 |
|
2973 if (fname) |
|
2974 free (fname); |
|
2975 |
|
2976 fname = tilde_expand (*argv); |
604
|
2977 |
|
2978 argc--; |
|
2979 argv++; |
|
2980 |
911
|
2981 unsigned mode = ios::out|ios::trunc; |
604
|
2982 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2983 mode |= ios::bin; |
|
2984 |
863
|
2985 ofstream file (fname, mode); |
|
2986 |
|
2987 if (file) |
|
2988 { |
|
2989 save_vars (argv, argc, file, save_builtins, format, |
|
2990 save_as_floats); |
|
2991 } |
|
2992 else |
604
|
2993 { |
|
2994 error ("save: couldn't open output file `%s'", *argv); |
|
2995 DELETE_ARGV; |
|
2996 return retval; |
|
2997 } |
|
2998 } |
|
2999 |
|
3000 DELETE_ARGV; |
|
3001 |
|
3002 return retval; |
|
3003 } |
|
3004 |
|
3005 // Maybe this should be a static function in tree-plot.cc? |
|
3006 |
620
|
3007 // If TC is matrix, save it on stream OS in a format useful for |
604
|
3008 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
3009 // nonzero, assume a parametric 3-dimensional plot will be generated. |
|
3010 |
|
3011 int |
620
|
3012 save_three_d (ostream& os, const tree_constant& tc, int parametric) |
604
|
3013 { |
620
|
3014 int fail = 0; |
604
|
3015 |
620
|
3016 int nr = tc.rows (); |
|
3017 int nc = tc.columns (); |
|
3018 |
|
3019 if (tc.is_real_matrix ()) |
604
|
3020 { |
|
3021 os << "# 3D data...\n" |
|
3022 << "# type: matrix\n" |
|
3023 << "# total rows: " << nr << "\n" |
|
3024 << "# total columns: " << nc << "\n"; |
|
3025 |
|
3026 if (parametric) |
|
3027 { |
|
3028 int extras = nc % 3; |
|
3029 if (extras) |
|
3030 warning ("ignoring last %d columns", extras); |
|
3031 |
620
|
3032 Matrix tmp = tc.matrix_value (); |
872
|
3033 tmp = strip_infnan (tmp); |
|
3034 nr = tmp.rows (); |
|
3035 |
604
|
3036 for (int i = 0; i < nc-extras; i += 3) |
|
3037 { |
|
3038 os << tmp.extract (0, i, nr-1, i+2); |
|
3039 if (i+3 < nc-extras) |
|
3040 os << "\n"; |
|
3041 } |
|
3042 } |
|
3043 else |
|
3044 { |
620
|
3045 Matrix tmp = tc.matrix_value (); |
872
|
3046 tmp = strip_infnan (tmp); |
|
3047 nr = tmp.rows (); |
|
3048 |
604
|
3049 for (int i = 0; i < nc; i++) |
|
3050 { |
|
3051 os << tmp.extract (0, i, nr-1, i); |
|
3052 if (i+1 < nc) |
|
3053 os << "\n"; |
|
3054 } |
|
3055 } |
620
|
3056 } |
|
3057 else |
|
3058 { |
604
|
3059 ::error ("for now, I can only save real matrices in 3D format"); |
620
|
3060 fail = 1; |
604
|
3061 } |
620
|
3062 |
|
3063 return (os && ! fail); |
604
|
3064 } |
|
3065 |
|
3066 /* |
|
3067 ;;; Local Variables: *** |
|
3068 ;;; mode: C++ *** |
|
3069 ;;; page-delimiter: "^/\\*" *** |
|
3070 ;;; End: *** |
|
3071 */ |