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