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 |
604
|
1641 is.read (&mopt, 4); |
|
1642 if (! is) |
|
1643 { |
|
1644 if (! is.eof ()) |
|
1645 goto data_read_error; |
|
1646 return 1; |
|
1647 } |
|
1648 |
|
1649 if (! is.read (&nr, 4)) |
|
1650 goto data_read_error; |
|
1651 |
|
1652 if (! is.read (&nc, 4)) |
|
1653 goto data_read_error; |
|
1654 |
|
1655 if (! is.read (&imag, 4)) |
|
1656 goto data_read_error; |
|
1657 |
|
1658 if (! is.read (&len, 4)) |
|
1659 goto data_read_error; |
|
1660 |
|
1661 // If mopt is nonzero and the byte order is swapped, mopt will be |
|
1662 // bigger than we expect, so we swap bytes. |
|
1663 // |
|
1664 // If mopt is zero, it means the file was written on a little endian |
|
1665 // machine, and we only need to swap if we are running on a big endian |
|
1666 // machine. |
|
1667 // |
|
1668 // Gag me. |
|
1669 |
|
1670 #if defined (WORDS_BIGENDIAN) |
|
1671 if (mopt == 0) |
|
1672 swap = 1; |
|
1673 #endif |
|
1674 |
|
1675 if (mopt > 9999) |
|
1676 swap = 1; |
|
1677 |
|
1678 if (swap) |
|
1679 { |
|
1680 swap_4_bytes ((char *) &mopt); |
|
1681 swap_4_bytes ((char *) &nr); |
|
1682 swap_4_bytes ((char *) &nc); |
|
1683 swap_4_bytes ((char *) &imag); |
|
1684 swap_4_bytes ((char *) &len); |
|
1685 } |
|
1686 |
|
1687 if (mopt > 9999 || imag > 1 || imag < 0 || len > 8192) |
|
1688 { |
|
1689 if (! quiet) |
|
1690 error ("load: can't read binary file"); |
|
1691 return -1; |
|
1692 } |
|
1693 |
|
1694 return 0; |
|
1695 |
|
1696 data_read_error: |
|
1697 return -1; |
|
1698 } |
|
1699 |
617
|
1700 // We don't just use a cast here, because we need to be able to detect |
|
1701 // possible errors. |
|
1702 |
|
1703 static floating_point_format |
|
1704 get_floating_point_format (int mach) |
|
1705 { |
619
|
1706 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
1707 |
617
|
1708 switch (mach) |
|
1709 { |
|
1710 case 0: |
|
1711 flt_fmt = LS_IEEE_LITTLE; |
|
1712 break; |
|
1713 |
|
1714 case 1: |
|
1715 flt_fmt = LS_IEEE_BIG; |
|
1716 break; |
|
1717 |
|
1718 case 2: |
|
1719 flt_fmt = LS_VAX_D; |
|
1720 break; |
|
1721 |
|
1722 case 3: |
|
1723 flt_fmt = LS_VAX_G; |
|
1724 break; |
|
1725 |
|
1726 case 4: |
|
1727 flt_fmt = LS_CRAY; |
|
1728 break; |
|
1729 |
|
1730 default: |
619
|
1731 flt_fmt = LS_UNKNOWN_FLT_FMT; |
617
|
1732 break; |
|
1733 } |
619
|
1734 |
|
1735 return flt_fmt; |
617
|
1736 } |
619
|
1737 |
604
|
1738 // Extract one value (scalar, matrix, string, etc.) from stream IS and |
|
1739 // place it in TC, returning the name of the variable. |
|
1740 // |
|
1741 // The data is expected to be in Matlab's .mat format, though not all |
|
1742 // the features of that format are supported. |
|
1743 // |
|
1744 // FILENAME is used for error messages. |
|
1745 // |
|
1746 // This format provides no way to tag the data as global. |
|
1747 |
|
1748 static char * |
|
1749 read_mat_binary_data (istream& is, const char *filename, |
|
1750 tree_constant& tc) |
|
1751 { |
|
1752 // These are initialized here instead of closer to where they are |
|
1753 // first used to avoid errors from gcc about goto crossing |
|
1754 // initialization of variable. |
|
1755 |
|
1756 Matrix re; |
|
1757 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
1758 char *name = 0; |
|
1759 int swap = 0, type = 0, prec = 0, mach = 0, dlen = 0; |
|
1760 |
|
1761 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
1762 |
|
1763 int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); |
|
1764 if (err) |
|
1765 { |
|
1766 if (err < 0) |
|
1767 goto data_read_error; |
|
1768 else |
|
1769 return 0; |
|
1770 } |
|
1771 |
|
1772 type = mopt % 10; // Full, sparse, etc. |
|
1773 mopt /= 10; // Eliminate first digit. |
|
1774 prec = mopt % 10; // double, float, int, etc. |
|
1775 mopt /= 100; // Skip unused third digit too. |
|
1776 mach = mopt % 10; // IEEE, VAX, etc. |
|
1777 |
617
|
1778 flt_fmt = get_floating_point_format (mach); |
|
1779 if (flt_fmt == LS_UNKNOWN_FLT_FMT) |
604
|
1780 { |
|
1781 error ("load: unrecognized binary format!"); |
|
1782 return 0; |
|
1783 } |
|
1784 |
|
1785 if (type != 0 && type != 1) |
|
1786 { |
|
1787 error ("load: can't read sparse matrices"); |
|
1788 return 0; |
|
1789 } |
|
1790 |
|
1791 if (imag && type == 1) |
|
1792 { |
|
1793 error ("load: encountered complex matrix with string flag set!"); |
|
1794 return 0; |
|
1795 } |
|
1796 |
|
1797 name = new char [len]; |
|
1798 if (! is.read (name, len)) |
|
1799 goto data_read_error; |
|
1800 |
|
1801 dlen = nr * nc; |
|
1802 if (dlen < 0) |
|
1803 goto data_read_error; |
|
1804 |
|
1805 re.resize (nr, nc); |
|
1806 |
|
1807 read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1808 |
|
1809 if (! is || error_state) |
|
1810 { |
|
1811 error ("load: reading matrix data for `%s'", name); |
|
1812 goto data_read_error; |
|
1813 } |
|
1814 |
|
1815 if (imag) |
|
1816 { |
|
1817 Matrix im (nr, nc); |
|
1818 |
|
1819 read_mat_binary_data (is, im.fortran_vec (), prec, dlen, swap, flt_fmt); |
|
1820 |
|
1821 if (! is || error_state) |
|
1822 { |
|
1823 error ("load: reading imaginary matrix data for `%s'", name); |
|
1824 goto data_read_error; |
|
1825 } |
|
1826 |
|
1827 ComplexMatrix ctmp (nr, nc); |
|
1828 |
|
1829 for (int j = 0; j < nc; j++) |
|
1830 for (int i = 0; i < nr; i++) |
|
1831 ctmp.elem (i, j) = Complex (re.elem (i, j), im.elem (i, j)); |
|
1832 |
|
1833 tc = ctmp; |
|
1834 } |
|
1835 else |
|
1836 tc = re; |
|
1837 |
|
1838 if (type == 1) |
|
1839 tc = tc.convert_to_str (); |
|
1840 |
|
1841 return name; |
|
1842 |
|
1843 data_read_error: |
|
1844 error ("load: trouble reading binary file `%s'", filename); |
|
1845 delete [] name; |
|
1846 return 0; |
|
1847 } |
|
1848 |
|
1849 // Return nonzero if NAME matches one of the given globbing PATTERNS. |
|
1850 |
|
1851 static int |
|
1852 matches_patterns (char **patterns, int num_pat, char *name) |
|
1853 { |
|
1854 while (num_pat-- > 0) |
|
1855 { |
|
1856 if (fnmatch (*patterns++, name, __FNM_FLAGS) == 0) |
|
1857 return 1; |
|
1858 } |
|
1859 return 0; |
|
1860 } |
|
1861 |
|
1862 static int |
|
1863 read_binary_file_header (istream& is, int& swap, |
630
|
1864 floating_point_format& flt_fmt, int quiet = 0) |
604
|
1865 { |
|
1866 int magic_len = 10; |
|
1867 char magic [magic_len+1]; |
|
1868 is.read (magic, magic_len); |
|
1869 magic[magic_len] = '\0'; |
|
1870 if (strncmp (magic, "Octave-1-L", magic_len) == 0) |
|
1871 { |
|
1872 #if defined (WORDS_BIGENDIAN) |
|
1873 swap = 1; |
|
1874 #else |
|
1875 swap = 0; |
|
1876 #endif |
|
1877 } |
|
1878 else if (strncmp (magic, "Octave-1-B", magic_len) == 0) |
|
1879 { |
|
1880 #if defined (WORDS_BIGENDIAN) |
|
1881 swap = 0; |
|
1882 #else |
|
1883 swap = 1; |
|
1884 #endif |
|
1885 } |
|
1886 else |
|
1887 { |
|
1888 if (! quiet) |
|
1889 error ("load: can't read binary file"); |
|
1890 return -1; |
|
1891 } |
|
1892 |
|
1893 char tmp = 0; |
|
1894 is.read (&tmp, 1); |
|
1895 |
617
|
1896 flt_fmt = get_floating_point_format (tmp); |
|
1897 if (flt_fmt == LS_UNKNOWN_FLT_FMT) |
604
|
1898 { |
|
1899 if (! quiet) |
|
1900 error ("load: unrecognized binary format!"); |
|
1901 return -1; |
|
1902 } |
|
1903 |
|
1904 return 0; |
|
1905 } |
|
1906 |
|
1907 static load_save_format |
|
1908 get_file_format (const char *fname, const char *orig_fname) |
|
1909 { |
|
1910 load_save_format retval = LS_UNKNOWN; |
|
1911 |
|
1912 ifstream file; |
|
1913 |
|
1914 file.open (fname); |
|
1915 |
|
1916 if (! file) |
|
1917 { |
|
1918 error ("load: couldn't open input file `%s'", orig_fname); |
|
1919 return retval; |
|
1920 } |
|
1921 |
|
1922 int swap; |
|
1923 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
1924 |
|
1925 if (read_binary_file_header (file, swap, flt_fmt, 1) == 0) |
|
1926 retval = LS_BINARY; |
|
1927 else |
|
1928 { |
|
1929 file.seekg (0, ios::beg); |
|
1930 |
|
1931 FOUR_BYTE_INT mopt, nr, nc, imag, len; |
|
1932 int swap; |
|
1933 |
|
1934 if (read_mat_file_header (file, swap, mopt, nr, nc, imag, len, 1) == 0) |
|
1935 retval = LS_MAT_BINARY; |
|
1936 else |
|
1937 { |
|
1938 file.seekg (0, ios::beg); |
|
1939 |
|
1940 char *tmp = extract_keyword (file, "name"); |
|
1941 if (tmp) |
|
1942 retval = LS_ASCII; |
|
1943 |
|
1944 delete [] tmp; |
|
1945 } |
|
1946 } |
|
1947 |
|
1948 file.close (); |
|
1949 |
|
1950 if (retval == LS_UNKNOWN) |
|
1951 error ("load: unable to determine file format for `%s'", orig_fname); |
|
1952 |
|
1953 return retval; |
|
1954 } |
|
1955 |
863
|
1956 static Octave_object |
|
1957 do_load (istream& stream, const char *orig_fname, int force, |
|
1958 load_save_format format, floating_point_format flt_fmt, |
|
1959 int list_only, int swap, int verbose, char **argv, |
|
1960 int argc, int nargout) |
604
|
1961 { |
|
1962 Octave_object retval; |
|
1963 |
621
|
1964 ostrstream output_buf; |
604
|
1965 int count = 0; |
|
1966 for (;;) |
|
1967 { |
|
1968 int global = 0; |
|
1969 tree_constant tc; |
|
1970 |
|
1971 char *name = 0; |
|
1972 char *doc = 0; |
|
1973 |
|
1974 switch (format) |
|
1975 { |
|
1976 case LS_ASCII: |
|
1977 name = read_ascii_data (stream, orig_fname, global, tc); |
|
1978 break; |
|
1979 |
|
1980 case LS_BINARY: |
|
1981 name = read_binary_data (stream, swap, flt_fmt, orig_fname, |
|
1982 global, tc, doc); |
|
1983 break; |
|
1984 |
|
1985 case LS_MAT_BINARY: |
|
1986 name = read_mat_binary_data (stream, orig_fname, tc); |
|
1987 break; |
|
1988 |
|
1989 default: |
775
|
1990 gripe_unrecognized_data_fmt ("load"); |
604
|
1991 break; |
|
1992 } |
|
1993 |
867
|
1994 if (error_state || stream.eof () || ! name) |
604
|
1995 { |
867
|
1996 delete [] name; |
|
1997 delete [] doc; |
604
|
1998 break; |
|
1999 } |
|
2000 else if (! error_state && name) |
|
2001 { |
|
2002 if (tc.is_defined ()) |
|
2003 { |
|
2004 if (argc == 0 || matches_patterns (argv, argc, name)) |
|
2005 { |
|
2006 count++; |
621
|
2007 if (list_only) |
|
2008 { |
|
2009 if (verbose) |
|
2010 { |
|
2011 if (count == 1) |
|
2012 output_buf |
|
2013 << "type rows cols name\n" |
|
2014 << "==== ==== ==== ====\n"; |
|
2015 |
|
2016 output_buf.form ("%-16s", tc.type_as_string ()); |
|
2017 output_buf.form ("%7d", tc.rows ()); |
|
2018 output_buf.form ("%7d", tc.columns ()); |
|
2019 output_buf << " "; |
|
2020 } |
|
2021 output_buf << name << "\n"; |
|
2022 } |
|
2023 else |
|
2024 { |
|
2025 install_loaded_variable (force, name, tc, global, doc); |
|
2026 } |
604
|
2027 } |
|
2028 } |
|
2029 else |
|
2030 error ("load: unable to load variable `%s'", name); |
|
2031 } |
|
2032 else |
|
2033 { |
|
2034 if (count == 0) |
|
2035 error ("load: are you sure `%s' is an Octave data file?", |
|
2036 orig_fname); |
|
2037 |
867
|
2038 delete [] name; |
|
2039 delete [] doc; |
604
|
2040 break; |
|
2041 } |
|
2042 |
|
2043 delete [] name; |
|
2044 delete [] doc; |
|
2045 } |
|
2046 |
621
|
2047 if (list_only && count) |
|
2048 { |
|
2049 if (nargout > 0) |
|
2050 { |
|
2051 output_buf << ends; |
|
2052 char *msg = output_buf.str (); |
|
2053 retval = msg; |
|
2054 delete [] msg; |
|
2055 } |
|
2056 else |
|
2057 maybe_page_output (output_buf); |
|
2058 } |
|
2059 |
863
|
2060 return retval; |
|
2061 } |
|
2062 |
|
2063 DEFUN_TEXT ("load", Fload, Sload, -1, 1, |
910
|
2064 "load [-force] [-ascii] [-binary] [-mat-binary] file [pattern ...]\n\ |
863
|
2065 \n\ |
|
2066 Load variables from a file.\n\ |
|
2067 \n\ |
|
2068 If no argument is supplied to select a format, load tries to read the |
|
2069 named file as an Octave binary, then as a .mat file, and then as an |
|
2070 Octave text file.\n\ |
|
2071 \n\ |
|
2072 If the option -force is given, variables with the same names as those |
|
2073 found in the file will be replaced with the values read from the file.") |
|
2074 { |
|
2075 Octave_object retval; |
|
2076 |
|
2077 DEFINE_ARGV ("load"); |
|
2078 |
|
2079 argc--; |
|
2080 argv++; |
|
2081 |
|
2082 int force = 0; |
|
2083 |
|
2084 // It isn't necessary to have the default load format stored in a user |
|
2085 // preference variable since we can determine the type of file as we |
|
2086 // are reading. |
|
2087 |
|
2088 load_save_format format = LS_UNKNOWN; |
|
2089 |
|
2090 int list_only = 0; |
|
2091 int verbose = 0; |
|
2092 |
|
2093 while (argc > 0) |
|
2094 { |
|
2095 if (strcmp (*argv, "-force") == 0 || strcmp (*argv, "-f") == 0) |
|
2096 { |
|
2097 force++; |
|
2098 argc--; |
|
2099 argv++; |
|
2100 } |
|
2101 else if (strcmp (*argv, "-list") == 0 || strcmp (*argv, "-l") == 0) |
|
2102 { |
|
2103 list_only = 1; |
|
2104 argc--; |
|
2105 argv++; |
|
2106 } |
|
2107 else if (strcmp (*argv, "-verbose") == 0 || strcmp (*argv, "-v") == 0) |
|
2108 { |
|
2109 verbose = 1; |
|
2110 argc--; |
|
2111 argv++; |
|
2112 } |
|
2113 else if (strcmp (*argv, "-ascii") == 0 || strcmp (*argv, "-a") == 0) |
|
2114 { |
|
2115 format = LS_ASCII; |
|
2116 argc--; |
|
2117 argv++; |
|
2118 } |
|
2119 else if (strcmp (*argv, "-binary") == 0 || strcmp (*argv, "-b") == 0) |
|
2120 { |
|
2121 format = LS_BINARY; |
|
2122 argc--; |
|
2123 argv++; |
|
2124 } |
|
2125 else if (strcmp (*argv, "-mat-binary") == 0 || strcmp (*argv, "-m") == 0) |
|
2126 { |
|
2127 format = LS_MAT_BINARY; |
|
2128 argc--; |
|
2129 argv++; |
|
2130 } |
|
2131 else |
|
2132 break; |
|
2133 } |
|
2134 |
|
2135 if (argc < 1) |
|
2136 { |
|
2137 error ("load: you must specify a single file to read"); |
|
2138 DELETE_ARGV; |
|
2139 return retval; |
|
2140 } |
|
2141 |
|
2142 char *orig_fname = *argv; |
|
2143 |
|
2144 floating_point_format flt_fmt = LS_UNKNOWN_FLT_FMT; |
|
2145 |
|
2146 int swap = 0; |
|
2147 |
|
2148 if (strcmp (*argv, "-") == 0) |
|
2149 { |
|
2150 argc--; |
|
2151 argv++; |
|
2152 |
|
2153 if (format != LS_UNKNOWN) |
|
2154 { |
|
2155 // XXX FIXME XXX -- if we have already seen EOF on a previous call, |
|
2156 // how do we fix up the state of cin so that we can get additional |
|
2157 // input? I'm afraid that we can't fix this using cin only. |
|
2158 |
|
2159 retval = do_load (cin, orig_fname, force, format, flt_fmt, |
|
2160 list_only, swap, verbose, argv, argc, |
|
2161 nargout); |
|
2162 } |
|
2163 else |
|
2164 error ("load: must specify file format if reading from stdin"); |
|
2165 } |
|
2166 else |
|
2167 { |
|
2168 char *fname = tilde_expand (*argv); |
|
2169 |
|
2170 if (format == LS_UNKNOWN) |
|
2171 format = get_file_format (fname, orig_fname); |
|
2172 |
|
2173 if (format != LS_UNKNOWN) |
|
2174 { |
|
2175 argv++; |
|
2176 argc--; |
|
2177 |
|
2178 unsigned mode = ios::in; |
|
2179 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2180 mode |= ios::bin; |
|
2181 |
|
2182 ifstream file (fname, mode); |
|
2183 |
|
2184 if (file) |
|
2185 { |
|
2186 if (format == LS_BINARY) |
|
2187 { |
|
2188 if (read_binary_file_header (file, swap, flt_fmt) < 0) |
|
2189 { |
|
2190 file.close (); |
|
2191 DELETE_ARGV; |
|
2192 return retval; |
|
2193 } |
|
2194 } |
|
2195 |
|
2196 retval = do_load (file, orig_fname, force, format, |
|
2197 flt_fmt, list_only, swap, verbose, |
|
2198 argv, argc, nargout); |
|
2199 |
|
2200 file.close (); |
|
2201 } |
|
2202 else |
|
2203 error ("load: couldn't open input file `%s'", orig_fname); |
|
2204 } |
|
2205 } |
604
|
2206 |
|
2207 DELETE_ARGV; |
|
2208 |
|
2209 return retval; |
|
2210 } |
|
2211 |
|
2212 // Return nonzero if PATTERN has any special globbing chars in it. |
|
2213 |
|
2214 static int |
|
2215 glob_pattern_p (char *pattern) |
|
2216 { |
|
2217 char *p = pattern; |
|
2218 char c; |
|
2219 int open = 0; |
|
2220 |
|
2221 while ((c = *p++) != '\0') |
|
2222 { |
|
2223 switch (c) |
|
2224 { |
|
2225 case '?': |
|
2226 case '*': |
|
2227 return 1; |
|
2228 |
|
2229 case '[': // Only accept an open brace if there is a close |
|
2230 open++; // brace to match it. Bracket expressions must be |
|
2231 continue; // complete, according to Posix.2 |
|
2232 |
|
2233 case ']': |
|
2234 if (open) |
|
2235 return 1; |
|
2236 continue; |
|
2237 |
|
2238 case '\\': |
|
2239 if (*p++ == '\0') |
|
2240 return 0; |
|
2241 |
|
2242 default: |
|
2243 continue; |
|
2244 } |
|
2245 } |
|
2246 |
|
2247 return 0; |
|
2248 } |
|
2249 |
618
|
2250 // MAX_VAL and MIN_VAL are assumed to have integral values even though |
|
2251 // they are stored in doubles. |
|
2252 |
604
|
2253 static save_type |
|
2254 get_save_type (double max_val, double min_val) |
|
2255 { |
|
2256 save_type st = LS_DOUBLE; |
|
2257 |
|
2258 if (max_val < 256 && min_val > -1) |
|
2259 st = LS_U_CHAR; |
|
2260 else if (max_val < 65536 && min_val > -1) |
|
2261 st = LS_U_SHORT; |
618
|
2262 else if (max_val < 4294967295 && min_val > -1) |
|
2263 st = LS_U_INT; |
|
2264 else if (max_val < 128 && min_val >= -128) |
|
2265 st = LS_CHAR; |
604
|
2266 else if (max_val < 32768 && min_val >= -32768) |
|
2267 st = LS_SHORT; |
|
2268 else if (max_val < 2147483648 && min_val > -2147483648) |
|
2269 st = LS_INT; |
|
2270 |
|
2271 return st; |
|
2272 } |
|
2273 |
|
2274 // Save the data from TC along with the corresponding NAME, help |
|
2275 // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the |
|
2276 // binary format described above for load_binary_data. |
|
2277 |
|
2278 static int |
|
2279 save_binary_data (ostream& os, const tree_constant& tc, char *name, |
630
|
2280 char *doc, int mark_as_global, int save_as_floats) |
604
|
2281 { |
620
|
2282 int fail = 0; |
|
2283 |
604
|
2284 FOUR_BYTE_INT name_len = 0; |
|
2285 if (name) |
|
2286 name_len = strlen (name); |
|
2287 |
|
2288 os.write (&name_len, 4); |
|
2289 os.write (name, name_len); |
|
2290 |
|
2291 FOUR_BYTE_INT doc_len = 0; |
|
2292 if (doc) |
|
2293 doc_len = strlen (doc); |
|
2294 |
|
2295 os.write (&doc_len, 4); |
|
2296 os.write (doc, doc_len); |
|
2297 |
|
2298 char tmp; |
|
2299 |
|
2300 tmp = mark_as_global; |
|
2301 os.write (&tmp, 1); |
|
2302 |
620
|
2303 if (tc.is_real_scalar ()) |
604
|
2304 { |
|
2305 tmp = 1; |
|
2306 os.write (&tmp, 1); |
630
|
2307 tmp = (char) LS_DOUBLE; |
|
2308 os.write (&tmp, 1); |
604
|
2309 double tmp = tc.double_value (); |
|
2310 os.write (&tmp, 8); |
|
2311 } |
620
|
2312 else if (tc.is_real_matrix ()) |
604
|
2313 { |
|
2314 tmp = 2; |
|
2315 os.write (&tmp, 1); |
|
2316 Matrix m = tc.matrix_value (); |
|
2317 FOUR_BYTE_INT nr = m.rows (); |
|
2318 FOUR_BYTE_INT nc = m.columns (); |
|
2319 os.write (&nr, 4); |
|
2320 os.write (&nc, 4); |
|
2321 int len = nr * nc; |
|
2322 save_type st = LS_DOUBLE; |
630
|
2323 if (save_as_floats) |
|
2324 { |
|
2325 if (too_large_for_float (m)) |
|
2326 { |
|
2327 warning ("save: some values too large to save as floats --"); |
|
2328 warning ("save: saving as doubles instead"); |
|
2329 } |
|
2330 else |
|
2331 st = LS_FLOAT; |
|
2332 } |
|
2333 else if (len > 8192) // XXX FIXME XXX -- make this configurable. |
604
|
2334 { |
|
2335 double max_val, min_val; |
|
2336 if (all_parts_int (m, max_val, min_val)) |
|
2337 st = get_save_type (max_val, min_val); |
|
2338 } |
630
|
2339 const double *mtmp = m.data (); |
604
|
2340 write_doubles (os, mtmp, st, len); |
|
2341 } |
|
2342 else if (tc.is_complex_scalar ()) |
|
2343 { |
|
2344 tmp = 3; |
|
2345 os.write (&tmp, 1); |
630
|
2346 tmp = (char) LS_DOUBLE; |
|
2347 os.write (&tmp, 1); |
604
|
2348 Complex tmp = tc.complex_value (); |
|
2349 os.write (&tmp, 16); |
|
2350 } |
|
2351 else if (tc.is_complex_matrix ()) |
|
2352 { |
|
2353 tmp = 4; |
|
2354 os.write (&tmp, 1); |
|
2355 ComplexMatrix m = tc.complex_matrix_value (); |
|
2356 FOUR_BYTE_INT nr = m.rows (); |
|
2357 FOUR_BYTE_INT nc = m.columns (); |
|
2358 os.write (&nr, 4); |
|
2359 os.write (&nc, 4); |
|
2360 int len = nr * nc; |
|
2361 save_type st = LS_DOUBLE; |
630
|
2362 if (save_as_floats) |
|
2363 { |
|
2364 if (too_large_for_float (m)) |
|
2365 { |
|
2366 warning ("save: some values too large to save as floats --"); |
|
2367 warning ("save: saving as doubles instead"); |
|
2368 } |
|
2369 else |
|
2370 st = LS_FLOAT; |
|
2371 } |
|
2372 else if (len > 4096) // XXX FIXME XXX -- make this configurable. |
604
|
2373 { |
|
2374 double max_val, min_val; |
|
2375 if (all_parts_int (m, max_val, min_val)) |
|
2376 st = get_save_type (max_val, min_val); |
|
2377 } |
630
|
2378 const Complex *mtmp = m.data (); |
|
2379 write_doubles (os, (const double *) mtmp, st, 2*len); |
604
|
2380 } |
|
2381 else if (tc.is_string ()) |
|
2382 { |
|
2383 tmp = 5; |
|
2384 os.write (&tmp, 1); |
|
2385 int nr = tc.rows (); |
|
2386 int nc = tc.columns (); |
|
2387 FOUR_BYTE_INT len = nr * nc; |
|
2388 os.write (&len, 4); |
|
2389 char *s = tc.string_value (); |
|
2390 os.write (s, len); |
|
2391 } |
|
2392 else if (tc.is_range ()) |
|
2393 { |
|
2394 tmp = 6; |
|
2395 os.write (&tmp, 1); |
630
|
2396 tmp = (char) LS_DOUBLE; |
|
2397 os.write (&tmp, 1); |
604
|
2398 Range r = tc.range_value (); |
|
2399 double bas = r.base (); |
|
2400 double lim = r.limit (); |
|
2401 double inc = r.inc (); |
|
2402 os.write (&bas, 8); |
|
2403 os.write (&lim, 8); |
|
2404 os.write (&inc, 8); |
|
2405 } |
|
2406 else |
620
|
2407 { |
|
2408 gripe_wrong_type_arg ("save", tc); |
|
2409 fail = 1; |
|
2410 } |
604
|
2411 |
620
|
2412 return (os && ! fail); |
604
|
2413 } |
|
2414 |
667
|
2415 // Save the data from TC along with the corresponding NAME on stream OS |
|
2416 // in the MatLab binary format. |
|
2417 |
|
2418 static int |
|
2419 save_mat_binary_data (ostream& os, const tree_constant& tc, char *name) |
|
2420 { |
|
2421 int fail = 0; |
|
2422 |
|
2423 FOUR_BYTE_INT mopt = 0; |
|
2424 |
|
2425 mopt += tc.is_string () ? 1 : 0; |
|
2426 mopt += 1000 * get_floating_point_format (NATIVE_FLOAT_FORMAT); |
|
2427 |
|
2428 os.write (&mopt, 4); |
|
2429 |
|
2430 FOUR_BYTE_INT nr = tc.rows (); |
|
2431 os.write (&nr, 4); |
|
2432 |
|
2433 FOUR_BYTE_INT nc = tc.columns (); |
|
2434 os.write (&nc, 4); |
|
2435 |
|
2436 int len = nr * nc; |
|
2437 |
|
2438 FOUR_BYTE_INT imag = tc.is_complex_type () ? 1 : 0; |
|
2439 os.write (&imag, 4); |
|
2440 |
|
2441 FOUR_BYTE_INT name_len = name ? strlen (name) + 1 : 0; |
|
2442 |
|
2443 os.write (&name_len, 4); |
|
2444 os.write (name, name_len); |
|
2445 |
|
2446 if (tc.is_real_scalar ()) |
|
2447 { |
|
2448 double tmp = tc.double_value (); |
|
2449 os.write (&tmp, 8); |
|
2450 } |
909
|
2451 else if (tc.is_real_matrix () || tc.is_range ()) |
667
|
2452 { |
|
2453 Matrix m = tc.matrix_value (); |
|
2454 os.write (m.data (), 8 * len); |
|
2455 } |
|
2456 else if (tc.is_complex_scalar ()) |
|
2457 { |
|
2458 Complex tmp = tc.complex_value (); |
|
2459 os.write (&tmp, 16); |
|
2460 } |
|
2461 else if (tc.is_complex_matrix ()) |
|
2462 { |
|
2463 ComplexMatrix m_cmplx = tc.complex_matrix_value (); |
|
2464 Matrix m = ::real(m_cmplx); |
|
2465 os.write (m.data (), 8 * len); |
|
2466 m = ::imag(m_cmplx); |
|
2467 os.write (m.data (), 8 * len); |
|
2468 } |
|
2469 else if (tc.is_string ()) |
|
2470 { |
|
2471 begin_unwind_frame ("save_mat_binary_data"); |
|
2472 unwind_protect_int (user_pref.implicit_str_to_num_ok); |
|
2473 user_pref.implicit_str_to_num_ok = 1; |
|
2474 Matrix m = tc.matrix_value (); |
|
2475 os.write (m.data (), 8 * len); |
|
2476 run_unwind_frame ("save_mat_binary_data"); |
|
2477 } |
|
2478 else |
|
2479 { |
|
2480 gripe_wrong_type_arg ("save", tc); |
|
2481 fail = 1; |
|
2482 } |
|
2483 |
|
2484 return (os && ! fail); |
|
2485 } |
|
2486 |
620
|
2487 static void |
|
2488 ascii_save_type (ostream& os, char *type, int mark_as_global) |
|
2489 { |
|
2490 if (mark_as_global) |
|
2491 os << "# type: global "; |
|
2492 else |
|
2493 os << "# type: "; |
|
2494 |
|
2495 os << type << "\n"; |
|
2496 } |
|
2497 |
872
|
2498 static Matrix |
|
2499 strip_infnan (const Matrix& m) |
|
2500 { |
|
2501 int nr = m.rows (); |
|
2502 int nc = m.columns (); |
|
2503 |
|
2504 Matrix retval (nr, nc); |
|
2505 |
|
2506 int k = 0; |
|
2507 for (int i = 0; i < nr; i++) |
|
2508 { |
|
2509 for (int j = 0; j < nc; j++) |
|
2510 { |
|
2511 double d = m.elem (i, j); |
|
2512 if (xisnan (d)) |
|
2513 goto next_row; |
|
2514 else |
|
2515 retval.elem (k, j) = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
2516 } |
|
2517 k++; |
|
2518 |
|
2519 next_row: |
|
2520 continue; |
|
2521 } |
|
2522 |
|
2523 if (k > 0) |
|
2524 retval.resize (k, nc); |
|
2525 |
|
2526 return retval; |
|
2527 } |
|
2528 |
|
2529 static ComplexMatrix |
|
2530 strip_infnan (const ComplexMatrix& m) |
|
2531 { |
|
2532 int nr = m.rows (); |
|
2533 int nc = m.columns (); |
|
2534 |
|
2535 ComplexMatrix 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 Complex c = m.elem (i, j); |
|
2543 if (xisnan (c)) |
|
2544 goto next_row; |
|
2545 else |
|
2546 { |
|
2547 double re = real (c); |
|
2548 double im = imag (c); |
|
2549 |
|
2550 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
2551 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
2552 |
|
2553 retval.elem (k, j) = Complex (re, im); |
|
2554 } |
|
2555 } |
|
2556 k++; |
|
2557 |
|
2558 next_row: |
|
2559 continue; |
|
2560 } |
|
2561 |
|
2562 if (k > 0) |
|
2563 retval.resize (k, nc); |
|
2564 |
|
2565 return retval; |
|
2566 } |
|
2567 |
620
|
2568 // Save the data from TC along with the corresponding NAME, and global |
604
|
2569 // flag MARK_AS_GLOBAL on stream OS in the plain text format described |
|
2570 // above for load_ascii_data. If NAME is null, the name: line is not |
|
2571 // generated. PRECISION specifies the number of decimal digits to print. |
872
|
2572 // If STRIP_NAN_AND_INF is nonzero, rows containing NaNs are deleted, |
|
2573 // and Infinite values are converted to +/-OCT_RBV (A Real Big Value, |
|
2574 // but not so big that gnuplot can't handle it when trying to compute |
|
2575 // axis ranges, etc.). |
|
2576 // |
|
2577 // Assumes ranges and strings cannot contain Inf or NaN values. |
|
2578 // |
|
2579 // Returns 1 for success and 0 for failure. |
604
|
2580 |
|
2581 // XXX FIXME XXX -- should probably write the help string here too. |
|
2582 |
|
2583 int |
620
|
2584 save_ascii_data (ostream& os, const tree_constant& tc, |
872
|
2585 char *name, int strip_nan_and_inf, |
|
2586 int mark_as_global, int precision) |
604
|
2587 { |
872
|
2588 int success = 1; |
620
|
2589 |
604
|
2590 if (! precision) |
|
2591 precision = user_pref.save_precision; |
|
2592 |
|
2593 if (name) |
|
2594 os << "# name: " << name << "\n"; |
|
2595 |
|
2596 long old_precision = os.precision (); |
|
2597 os.precision (precision); |
|
2598 |
620
|
2599 if (tc.is_real_scalar ()) |
|
2600 { |
|
2601 ascii_save_type (os, "scalar", mark_as_global); |
872
|
2602 |
|
2603 double d = tc.double_value (); |
|
2604 if (strip_nan_and_inf) |
|
2605 { |
|
2606 if (xisnan (d)) |
|
2607 { |
|
2608 error ("only value to plot is NaN"); |
|
2609 success = 0; |
|
2610 } |
|
2611 else |
|
2612 { |
|
2613 d = xisinf (d) ? (d > 0 ? OCT_RBV : -OCT_RBV) : d; |
|
2614 os << d << "\n"; |
|
2615 } |
|
2616 } |
|
2617 else |
|
2618 os << d << "\n"; |
620
|
2619 } |
|
2620 else if (tc.is_real_matrix ()) |
|
2621 { |
|
2622 ascii_save_type (os, "matrix", mark_as_global); |
|
2623 os << "# rows: " << tc.rows () << "\n" |
872
|
2624 << "# columns: " << tc.columns () << "\n"; |
|
2625 |
|
2626 Matrix tmp = tc.matrix_value (); |
|
2627 if (strip_nan_and_inf) |
|
2628 tmp = strip_infnan (tmp); |
|
2629 |
|
2630 os << tmp; |
620
|
2631 } |
|
2632 else if (tc.is_complex_scalar ()) |
|
2633 { |
|
2634 ascii_save_type (os, "complex scalar", mark_as_global); |
872
|
2635 |
|
2636 Complex c = tc.complex_value (); |
|
2637 if (strip_nan_and_inf) |
|
2638 { |
|
2639 if (xisnan (c)) |
|
2640 { |
|
2641 error ("only value to plot is NaN"); |
|
2642 success = 0; |
|
2643 } |
|
2644 else |
|
2645 { |
|
2646 double re = real (c); |
|
2647 double im = imag (c); |
|
2648 |
|
2649 re = xisinf (re) ? (re > 0 ? OCT_RBV : -OCT_RBV) : re; |
|
2650 im = xisinf (im) ? (im > 0 ? OCT_RBV : -OCT_RBV) : im; |
|
2651 |
|
2652 c = Complex (re, im); |
|
2653 |
|
2654 os << c << "\n"; |
|
2655 } |
|
2656 } |
|
2657 else |
|
2658 os << c << "\n"; |
620
|
2659 } |
|
2660 else if (tc.is_complex_matrix ()) |
604
|
2661 { |
620
|
2662 ascii_save_type (os, "complex matrix", mark_as_global); |
|
2663 os << "# rows: " << tc.rows () << "\n" |
875
|
2664 << "# columns: " << tc.columns () << "\n"; |
|
2665 |
|
2666 ComplexMatrix tmp = tc.complex_matrix_value (); |
872
|
2667 if (strip_nan_and_inf) |
|
2668 tmp = strip_infnan (tmp); |
|
2669 |
|
2670 os << tmp; |
620
|
2671 } |
|
2672 else if (tc.is_string ()) |
|
2673 { |
|
2674 ascii_save_type (os, "string", mark_as_global); |
|
2675 char *tmp = tc.string_value (); |
|
2676 os << "# length: " << strlen (tmp) << "\n" |
|
2677 << tmp << "\n"; |
|
2678 } |
872
|
2679 else if (tc.is_range ()) |
620
|
2680 { |
|
2681 ascii_save_type (os, "range", mark_as_global); |
|
2682 Range tmp = tc.range_value (); |
|
2683 os << "# base, limit, increment\n" |
|
2684 << tmp.base () << " " |
|
2685 << tmp.limit () << " " |
|
2686 << tmp.inc () << "\n"; |
|
2687 } |
|
2688 else |
|
2689 { |
|
2690 gripe_wrong_type_arg ("save", tc); |
872
|
2691 success = 0; |
604
|
2692 } |
|
2693 |
|
2694 os.precision (old_precision); |
|
2695 |
872
|
2696 return (os && success); |
604
|
2697 } |
|
2698 |
|
2699 // Save the info from sr on stream os in the format specified by fmt. |
|
2700 |
|
2701 static void |
630
|
2702 do_save (ostream& os, symbol_record *sr, load_save_format fmt, |
|
2703 int save_as_floats) |
604
|
2704 { |
|
2705 if (! sr->is_variable ()) |
|
2706 { |
|
2707 error ("save: can only save variables, not functions"); |
|
2708 return; |
|
2709 } |
|
2710 |
|
2711 char *name = sr->name (); |
|
2712 char *help = sr->help (); |
|
2713 int global = sr->is_linked_to_global (); |
|
2714 tree_constant tc = *((tree_constant *) sr->def ()); |
|
2715 |
|
2716 if (! name || ! tc.is_defined ()) |
|
2717 return; |
|
2718 |
|
2719 switch (fmt) |
|
2720 { |
|
2721 case LS_ASCII: |
872
|
2722 save_ascii_data (os, tc, name, 0, global); |
604
|
2723 break; |
|
2724 |
|
2725 case LS_BINARY: |
630
|
2726 save_binary_data (os, tc, name, help, global, save_as_floats); |
604
|
2727 break; |
|
2728 |
667
|
2729 case LS_MAT_BINARY: |
|
2730 save_mat_binary_data (os, tc, name); |
|
2731 break; |
|
2732 |
604
|
2733 default: |
775
|
2734 gripe_unrecognized_data_fmt ("save"); |
604
|
2735 break; |
|
2736 } |
|
2737 } |
|
2738 |
|
2739 // Save variables with names matching PATTERN on stream OS in the |
|
2740 // format specified by FMT. If SAVE_BUILTINS is nonzero, also save |
|
2741 // builtin variables with names that match PATTERN. |
|
2742 |
|
2743 static int |
|
2744 save_vars (ostream& os, char *pattern, int save_builtins, |
630
|
2745 load_save_format fmt, int save_as_floats) |
604
|
2746 { |
|
2747 int count; |
|
2748 |
|
2749 symbol_record **vars = curr_sym_tab->glob |
|
2750 (count, pattern, symbol_def::USER_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2751 |
|
2752 int saved = count; |
|
2753 |
|
2754 int i; |
|
2755 |
|
2756 for (i = 0; i < count; i++) |
620
|
2757 { |
630
|
2758 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2759 |
|
2760 if (error_state) |
|
2761 break; |
|
2762 } |
604
|
2763 |
|
2764 delete [] vars; |
|
2765 |
620
|
2766 if (! error_state && save_builtins) |
604
|
2767 { |
|
2768 symbol_record **vars = global_sym_tab->glob |
|
2769 (count, pattern, symbol_def::BUILTIN_VARIABLE, SYMTAB_ALL_SCOPES); |
|
2770 |
|
2771 saved += count; |
|
2772 |
|
2773 for (i = 0; i < count; i++) |
620
|
2774 { |
630
|
2775 do_save (os, vars[i], fmt, save_as_floats); |
620
|
2776 |
|
2777 if (error_state) |
|
2778 break; |
|
2779 } |
604
|
2780 |
|
2781 delete [] vars; |
|
2782 } |
|
2783 |
|
2784 return saved; |
|
2785 } |
|
2786 |
|
2787 static load_save_format |
|
2788 get_default_save_format (void) |
|
2789 { |
|
2790 load_save_format retval = LS_ASCII; |
|
2791 |
|
2792 char *fmt = user_pref.default_save_format; |
|
2793 |
|
2794 if (strcasecmp (fmt, "binary") == 0) |
|
2795 retval = LS_BINARY; |
|
2796 |
|
2797 return retval; |
|
2798 } |
|
2799 |
863
|
2800 static void |
|
2801 write_binary_header (ostream& stream, load_save_format format) |
|
2802 { |
|
2803 if (format == LS_BINARY) |
|
2804 { |
|
2805 #if defined (WORDS_BIGENDIAN) |
|
2806 stream << "Octave-1-B"; |
|
2807 #else |
|
2808 stream << "Octave-1-L"; |
|
2809 #endif |
|
2810 |
|
2811 char tmp = (char) NATIVE_FLOAT_FORMAT; |
|
2812 stream.write (&tmp, 1); |
|
2813 } |
|
2814 } |
|
2815 |
|
2816 static void |
|
2817 save_vars (char **argv, int argc, ostream& os, int save_builtins, |
|
2818 load_save_format fmt, int save_as_floats) |
|
2819 { |
|
2820 write_binary_header (os, fmt); |
|
2821 |
|
2822 if (argc == 0) |
|
2823 { |
|
2824 save_vars (os, "*", save_builtins, fmt, save_as_floats); |
|
2825 } |
|
2826 else |
|
2827 { |
|
2828 while (argc-- > 0) |
|
2829 { |
|
2830 if (! save_vars (os, *argv, save_builtins, fmt, save_as_floats)) |
|
2831 { |
|
2832 warning ("save: no such variable `%s'", *argv); |
|
2833 } |
|
2834 |
|
2835 argv++; |
|
2836 } |
|
2837 } |
|
2838 } |
|
2839 |
604
|
2840 DEFUN_TEXT ("save", Fsave, Ssave, -1, 1, |
910
|
2841 "save [-ascii] [-binary] [-float-binary] [-mat-binary] \n\ |
667
|
2842 [-save-builtins] file [pattern ...]\n\ |
604
|
2843 \n\ |
|
2844 save variables in a file") |
|
2845 { |
|
2846 Octave_object retval; |
|
2847 |
863
|
2848 DEFINE_ARGV ("save"); |
604
|
2849 |
|
2850 argc--; |
|
2851 argv++; |
|
2852 |
|
2853 // Here is where we would get the default save format if it were |
|
2854 // stored in a user preference variable. |
|
2855 |
|
2856 int save_builtins = 0; |
|
2857 |
630
|
2858 int save_as_floats = 0; |
|
2859 |
604
|
2860 load_save_format format = get_default_save_format (); |
|
2861 |
|
2862 while (argc > 0) |
|
2863 { |
|
2864 if (strcmp (*argv, "-ascii") == 0 || strcmp (*argv, "-a") == 0) |
|
2865 { |
|
2866 format = LS_ASCII; |
|
2867 argc--; |
|
2868 argv++; |
|
2869 } |
|
2870 else if (strcmp (*argv, "-binary") == 0 || strcmp (*argv, "-b") == 0) |
|
2871 { |
|
2872 format = LS_BINARY; |
|
2873 argc--; |
|
2874 argv++; |
|
2875 } |
667
|
2876 else if (strcmp (*argv, "-mat-binary") == 0 || strcmp (*argv, "-m") == 0) |
|
2877 { |
|
2878 format = LS_MAT_BINARY; |
|
2879 argc--; |
|
2880 argv++; |
|
2881 } |
630
|
2882 else if (strcmp (*argv, "-float-binary") == 0 |
|
2883 || strcmp (*argv, "-f") == 0) |
|
2884 { |
|
2885 format = LS_BINARY; |
|
2886 save_as_floats = 1; |
|
2887 argc--; |
|
2888 argv++; |
|
2889 } |
604
|
2890 else if (strcmp (*argv, "-save-builtins") == 0) |
|
2891 { |
|
2892 save_builtins = 1; |
|
2893 argc--; |
|
2894 argv++; |
|
2895 } |
|
2896 else |
|
2897 break; |
|
2898 } |
|
2899 |
|
2900 if (argc < 1) |
|
2901 { |
|
2902 print_usage ("save"); |
|
2903 DELETE_ARGV; |
|
2904 return retval; |
|
2905 } |
|
2906 |
630
|
2907 if (save_as_floats && format == LS_ASCII) |
|
2908 { |
|
2909 error ("save: cannot specify both -ascii and -float-binary"); |
|
2910 DELETE_ARGV; |
|
2911 return retval; |
|
2912 } |
|
2913 |
604
|
2914 if (strcmp (*argv, "-") == 0) |
|
2915 { |
863
|
2916 argc--; |
|
2917 argv++; |
|
2918 |
604
|
2919 // XXX FIXME XXX -- should things intended for the screen end up in a |
|
2920 // tree_constant (string)? |
863
|
2921 |
|
2922 save_vars (argv, argc, cout, save_builtins, format, |
|
2923 save_as_floats); |
604
|
2924 } |
|
2925 else if (argc == 1 && glob_pattern_p (*argv)) // Guard against things |
|
2926 { // like `save a*', |
|
2927 print_usage ("save"); // which are probably |
|
2928 DELETE_ARGV; // mistakes... |
|
2929 return retval; |
|
2930 } |
|
2931 else |
|
2932 { |
|
2933 char *fname = tilde_expand (*argv); |
|
2934 |
|
2935 argc--; |
|
2936 argv++; |
|
2937 |
887
|
2938 unsigned mode = ios::out; |
604
|
2939 if (format == LS_BINARY || format == LS_MAT_BINARY) |
|
2940 mode |= ios::bin; |
|
2941 |
863
|
2942 ofstream file (fname, mode); |
|
2943 |
|
2944 if (file) |
|
2945 { |
|
2946 save_vars (argv, argc, file, save_builtins, format, |
|
2947 save_as_floats); |
|
2948 } |
|
2949 else |
604
|
2950 { |
|
2951 error ("save: couldn't open output file `%s'", *argv); |
|
2952 DELETE_ARGV; |
|
2953 return retval; |
|
2954 } |
|
2955 } |
|
2956 |
|
2957 DELETE_ARGV; |
|
2958 |
|
2959 return retval; |
|
2960 } |
|
2961 |
|
2962 // Maybe this should be a static function in tree-plot.cc? |
|
2963 |
620
|
2964 // If TC is matrix, save it on stream OS in a format useful for |
604
|
2965 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is |
|
2966 // nonzero, assume a parametric 3-dimensional plot will be generated. |
|
2967 |
|
2968 int |
620
|
2969 save_three_d (ostream& os, const tree_constant& tc, int parametric) |
604
|
2970 { |
620
|
2971 int fail = 0; |
604
|
2972 |
620
|
2973 int nr = tc.rows (); |
|
2974 int nc = tc.columns (); |
|
2975 |
|
2976 if (tc.is_real_matrix ()) |
604
|
2977 { |
|
2978 os << "# 3D data...\n" |
|
2979 << "# type: matrix\n" |
|
2980 << "# total rows: " << nr << "\n" |
|
2981 << "# total columns: " << nc << "\n"; |
|
2982 |
|
2983 if (parametric) |
|
2984 { |
|
2985 int extras = nc % 3; |
|
2986 if (extras) |
|
2987 warning ("ignoring last %d columns", extras); |
|
2988 |
620
|
2989 Matrix tmp = tc.matrix_value (); |
872
|
2990 tmp = strip_infnan (tmp); |
|
2991 nr = tmp.rows (); |
|
2992 |
604
|
2993 for (int i = 0; i < nc-extras; i += 3) |
|
2994 { |
|
2995 os << tmp.extract (0, i, nr-1, i+2); |
|
2996 if (i+3 < nc-extras) |
|
2997 os << "\n"; |
|
2998 } |
|
2999 } |
|
3000 else |
|
3001 { |
620
|
3002 Matrix tmp = tc.matrix_value (); |
872
|
3003 tmp = strip_infnan (tmp); |
|
3004 nr = tmp.rows (); |
|
3005 |
604
|
3006 for (int i = 0; i < nc; i++) |
|
3007 { |
|
3008 os << tmp.extract (0, i, nr-1, i); |
|
3009 if (i+1 < nc) |
|
3010 os << "\n"; |
|
3011 } |
|
3012 } |
620
|
3013 } |
|
3014 else |
|
3015 { |
604
|
3016 ::error ("for now, I can only save real matrices in 3D format"); |
620
|
3017 fail = 1; |
604
|
3018 } |
620
|
3019 |
|
3020 return (os && ! fail); |
604
|
3021 } |
|
3022 |
|
3023 /* |
|
3024 ;;; Local Variables: *** |
|
3025 ;;; mode: C++ *** |
|
3026 ;;; page-delimiter: "^/\\*" *** |
|
3027 ;;; End: *** |
|
3028 */ |