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