2117
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <iomanip.h> |
|
28 #include <strstream.h> |
|
29 |
|
30 #include "lo-ieee.h" |
|
31 #include "lo-mappers.h" |
|
32 #include "lo-utils.h" |
|
33 #include "str-vec.h" |
|
34 |
|
35 #include "error.h" |
|
36 #include "oct-stream.h" |
|
37 #include "utils.h" |
|
38 |
|
39 // Possible values for conv_err: |
|
40 // |
|
41 // 1 : not a real scalar |
|
42 // 2 : error extracting scalar value (should not happen) |
|
43 // 3 : value is NaN |
|
44 // 4 : value is not an integer |
|
45 |
|
46 static int |
|
47 convert_to_valid_int (const octave_value& tc, int& conv_err) |
|
48 { |
|
49 int retval = 0; |
|
50 |
|
51 conv_err = 0; |
|
52 |
|
53 if (tc.is_real_scalar ()) |
|
54 { |
|
55 double dval = tc.double_value (); |
|
56 |
|
57 if (! error_state) |
|
58 { |
|
59 if (! xisnan (dval)) |
|
60 { |
|
61 int ival = NINT (dval); |
|
62 |
|
63 if ((double) ival == dval) |
|
64 retval = ival; |
|
65 else |
|
66 conv_err = 4; |
|
67 } |
|
68 else |
|
69 conv_err = 3; |
|
70 } |
|
71 else |
|
72 conv_err = 2; |
|
73 } |
|
74 else |
|
75 conv_err = 1; |
|
76 |
|
77 return retval; |
|
78 } |
|
79 |
|
80 static int |
|
81 get_size (double d, const char *warn_for) |
|
82 { |
|
83 int retval = -1; |
|
84 |
|
85 if (! xisnan (d)) |
|
86 { |
|
87 if (! xisinf (d)) |
|
88 { |
|
89 if (d > 0.0) |
|
90 retval = NINT (d); |
|
91 else |
|
92 ::error ("%s: negative value invalid as size specification", |
|
93 warn_for); |
|
94 } |
|
95 else |
|
96 retval = -1; |
|
97 } |
|
98 else |
|
99 ::error ("%s: NaN is invalid as size specification", warn_for); |
|
100 |
|
101 return retval; |
|
102 } |
|
103 |
|
104 static void |
|
105 get_size (const Matrix& size, int& nr, int& nc, const char *warn_for) |
|
106 { |
|
107 nr = -1; |
|
108 nc = -1; |
|
109 |
|
110 double dnr = -1.0; |
|
111 double dnc = -1.0; |
|
112 |
|
113 if (size.rows () == 1 && size.cols () > 0) |
|
114 { |
|
115 dnr = size.elem (0, 0); |
|
116 |
|
117 if (size.cols () == 2) |
|
118 dnc = size.elem (0, 1); |
|
119 else if (size.cols () > 2) |
|
120 ::error ("%s: invalid size specification", warn_for); |
|
121 } |
|
122 else if (size.cols () == 1 && size.rows () > 0) |
|
123 { |
|
124 dnr = size.elem (0, 0); |
|
125 |
|
126 if (size.rows () == 2) |
|
127 dnc = size.elem (1, 0); |
|
128 else if (size.rows () > 2) |
|
129 ::error ("%s: invalid size specification", warn_for); |
|
130 } |
|
131 else |
|
132 ::error ("%s: invalid size specification", warn_for); |
|
133 |
|
134 if (! error_state) |
|
135 { |
|
136 nr = get_size (dnr, warn_for); |
|
137 |
|
138 if (! error_state && dnc > 0.0) |
|
139 nc = get_size (dnc, warn_for); |
|
140 } |
|
141 } |
|
142 |
|
143 scanf_format_list::scanf_format_list (const string& s) |
|
144 : nconv (0), curr_idx (0), list (16), buf (0) |
|
145 { |
|
146 int num_elts = 0; |
|
147 |
|
148 int n = s.length (); |
|
149 |
|
150 int i = 0; |
|
151 |
|
152 bool discard = false; |
|
153 char modifier = '\0'; |
|
154 char type = '\0'; |
|
155 |
|
156 bool have_more = true; |
|
157 |
|
158 while (i < n) |
|
159 { |
|
160 have_more = true; |
|
161 |
|
162 if (! buf) |
|
163 buf = new ostrstream (); |
|
164 |
|
165 if (s[i] == '%') |
|
166 { |
|
167 process_conversion (s, i, n, discard, type, modifier, num_elts); |
|
168 have_more = (buf != 0); |
|
169 } |
|
170 else |
|
171 { |
|
172 discard = false; |
|
173 modifier = '\0'; |
|
174 type = '\0'; |
|
175 *buf << s[i++]; |
|
176 } |
|
177 |
|
178 if (nconv < 0) |
|
179 { |
|
180 have_more = false; |
|
181 break; |
|
182 } |
|
183 } |
|
184 |
|
185 if (have_more) |
|
186 add_elt_to_list (discard, type, modifier, num_elts); |
|
187 |
|
188 list.resize (num_elts); |
|
189 |
|
190 delete buf; |
|
191 } |
|
192 |
|
193 scanf_format_list::~scanf_format_list (void) |
|
194 { |
|
195 int n = list.length (); |
|
196 |
|
197 for (int i = 0; i < n; i++) |
|
198 { |
|
199 scanf_format_elt *elt = list.elem (i); |
|
200 delete elt; |
|
201 } |
|
202 } |
|
203 |
|
204 void |
|
205 scanf_format_list::add_elt_to_list (bool discard, char type, |
|
206 char modifier, int& num_elts) |
|
207 { |
|
208 if (buf) |
|
209 { |
|
210 *buf << ends; |
|
211 |
|
212 char *text = buf->str (); |
|
213 |
|
214 if (text) |
|
215 { |
|
216 if (*text) |
|
217 { |
|
218 scanf_format_elt *elt |
|
219 = new scanf_format_elt (text, discard, type, modifier); |
|
220 |
|
221 if (num_elts == list.length ()) |
|
222 list.resize (2 * num_elts); |
|
223 |
|
224 list.elem (num_elts++) = elt; |
|
225 } |
|
226 else |
|
227 delete [] text; |
|
228 } |
|
229 |
|
230 delete buf; |
|
231 buf = 0; |
|
232 } |
|
233 } |
|
234 |
|
235 void |
|
236 scanf_format_list::process_conversion (const string& s, int& i, int n, |
|
237 bool& discard, char& type, |
|
238 char& modifier, int& num_elts) |
|
239 |
|
240 { |
|
241 discard = false; |
|
242 modifier = '\0'; |
|
243 type = '\0'; |
|
244 |
|
245 *buf << s[i++]; |
|
246 |
|
247 bool have_width = false; |
|
248 |
|
249 while (i < n) |
|
250 { |
|
251 switch (s[i]) |
|
252 { |
|
253 case '*': |
|
254 if (discard) |
|
255 nconv = -1; |
|
256 else |
|
257 { |
|
258 discard = true; |
|
259 *buf << s[i++]; |
|
260 } |
|
261 break; |
|
262 |
|
263 case '0': case '1': case '2': case '3': case '4': |
|
264 case '5': case '6': case '7': case '8': case '9': |
|
265 if (have_width) |
|
266 nconv = -1; |
|
267 else |
|
268 { |
|
269 have_width = true; |
|
270 *buf << s[i++]; |
|
271 while (i < n && isdigit (s[i])) |
|
272 *buf << s[i++]; |
|
273 } |
|
274 break; |
|
275 |
|
276 case 'h': case 'l': case 'L': |
|
277 if (modifier != '\0') |
|
278 nconv = -1; |
|
279 else |
|
280 { |
|
281 modifier = s[i]; |
|
282 *buf << s[i++]; |
|
283 } |
|
284 break; |
|
285 |
|
286 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
287 if (modifier == 'L') |
|
288 { |
|
289 nconv = -1; |
|
290 break; |
|
291 } |
|
292 goto fini; |
|
293 |
|
294 case 'e': case 'f': case 'g': |
|
295 if (modifier == 'h') |
|
296 { |
|
297 nconv = -1; |
|
298 break; |
|
299 } |
|
300 goto fini; |
|
301 |
|
302 case 'c': case 's': case 'p': case '%': case '[': |
|
303 if (modifier != '\0') |
|
304 { |
|
305 nconv = -1; |
|
306 break; |
|
307 } |
|
308 goto fini; |
|
309 |
|
310 fini: |
|
311 { |
|
312 if (finish_conversion (s, i, n, discard, type, |
|
313 modifier, num_elts) == 0) |
|
314 return; |
|
315 } |
|
316 break; |
|
317 |
|
318 default: |
|
319 nconv = -1; |
|
320 break; |
|
321 } |
|
322 |
|
323 if (nconv < 0) |
|
324 break; |
|
325 } |
|
326 |
|
327 nconv = -1; |
|
328 } |
|
329 |
|
330 int |
|
331 scanf_format_list::finish_conversion (const string& s, int& i, int n, |
|
332 bool discard, char& type, |
|
333 char modifier, int& num_elts) |
|
334 { |
|
335 int retval = 0; |
|
336 |
|
337 if (s[i] == '%') |
|
338 *buf << s[i++]; |
|
339 else |
|
340 { |
|
341 type = s[i]; |
|
342 |
|
343 if (s[i] == '[') |
|
344 { |
|
345 *buf << s[i++]; |
|
346 |
|
347 if (i < n) |
|
348 { |
|
349 if (s[i] == '^') |
|
350 { |
|
351 type = '^'; |
|
352 *buf << s[i++]; |
|
353 } |
|
354 else if (s[i] == ']') |
|
355 *buf << s[i++]; |
|
356 } |
|
357 |
|
358 while (i < n && s[i] != ']') |
|
359 *buf << s[i++]; |
|
360 |
|
361 if (i < n && s[i] == ']') |
|
362 *buf << s[i++]; |
|
363 |
|
364 if (s[i-1] != ']') |
|
365 retval = nconv = -1; |
|
366 } |
|
367 else |
|
368 { |
|
369 // XXX FIXME XXX -- this is a kludge, and probably not the |
|
370 // right thing to do here. |
|
371 |
|
372 if (type == 's') |
|
373 { |
|
374 *buf << 'c'; |
|
375 i++; |
|
376 } |
|
377 else |
|
378 *buf << s[i++]; |
|
379 } |
|
380 |
|
381 nconv++; |
|
382 |
|
383 if (nconv > 0) |
|
384 add_elt_to_list (discard, type, modifier, num_elts); |
|
385 } |
|
386 |
|
387 return retval; |
|
388 } |
|
389 |
|
390 void |
|
391 scanf_format_list::printme (void) const |
|
392 { |
|
393 int n = list.length (); |
|
394 |
|
395 for (int i = 0; i < n; i++) |
|
396 { |
|
397 scanf_format_elt *elt = list.elem (i); |
|
398 |
|
399 cerr << elt->discard << "\t" |
|
400 << elt->type << "\t" |
|
401 << elt->modifier << "\t" |
|
402 << undo_string_escapes (elt->text) << "\n"; |
|
403 } |
|
404 } |
|
405 |
|
406 bool |
|
407 scanf_format_list::all_character_conversions (void) |
|
408 { |
|
409 int n = list.length (); |
|
410 |
|
411 if (n > 0) |
|
412 { |
|
413 for (int i = 0; i < n; i++) |
|
414 { |
|
415 scanf_format_elt *elt = list.elem (i); |
|
416 |
|
417 switch (elt->type) |
|
418 { |
|
419 case 'c': case 's': case 'p': case '%': case '[': |
|
420 break; |
|
421 |
|
422 default: |
|
423 return false; |
|
424 break; |
|
425 } |
|
426 } |
|
427 |
|
428 return true; |
|
429 } |
|
430 else |
|
431 return false; |
|
432 } |
|
433 |
|
434 bool |
|
435 scanf_format_list::all_numeric_conversions (void) |
|
436 { |
|
437 int n = list.length (); |
|
438 |
|
439 if (n > 0) |
|
440 { |
|
441 for (int i = 0; i < n; i++) |
|
442 { |
|
443 scanf_format_elt *elt = list.elem (i); |
|
444 |
|
445 switch (elt->type) |
|
446 { |
|
447 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
448 case 'e': case 'f': case 'g': |
|
449 break; |
|
450 |
|
451 default: |
|
452 return false; |
|
453 break; |
|
454 } |
|
455 } |
|
456 |
|
457 return true; |
|
458 } |
|
459 else |
|
460 return false; |
|
461 } |
|
462 |
|
463 // Ugh again. |
|
464 |
|
465 printf_format_list::printf_format_list (const string& s) |
|
466 : nconv (0), curr_idx (0), list (16), buf (0) |
|
467 { |
|
468 int num_elts = 0; |
|
469 |
|
470 int n = s.length (); |
|
471 |
|
472 int i = 0; |
|
473 |
|
474 int args = 0; |
|
475 char modifier = '\0'; |
|
476 char type = '\0'; |
|
477 |
|
478 bool have_more = true; |
|
479 |
|
480 while (i < n) |
|
481 { |
|
482 have_more = true; |
|
483 |
|
484 if (! buf) |
|
485 buf = new ostrstream (); |
|
486 |
|
487 switch (s[i]) |
|
488 { |
|
489 case '%': |
|
490 process_conversion (s, i, n, args, type, modifier, num_elts); |
|
491 have_more = (buf != 0); |
|
492 break; |
|
493 |
|
494 default: |
|
495 args = 0; |
|
496 modifier = '\0'; |
|
497 type = '\0'; |
|
498 *buf << s[i++]; |
|
499 break; |
|
500 } |
|
501 |
|
502 if (nconv < 0) |
|
503 { |
|
504 have_more = false; |
|
505 break; |
|
506 } |
|
507 } |
|
508 |
|
509 if (have_more) |
|
510 add_elt_to_list (args, type, modifier, num_elts); |
|
511 |
|
512 list.resize (num_elts); |
|
513 |
|
514 delete buf; |
|
515 } |
|
516 |
|
517 printf_format_list::~printf_format_list (void) |
|
518 { |
|
519 int n = list.length (); |
|
520 |
|
521 for (int i = 0; i < n; i++) |
|
522 { |
|
523 printf_format_elt *elt = list.elem (i); |
|
524 delete elt; |
|
525 } |
|
526 } |
|
527 |
|
528 void |
|
529 printf_format_list::add_elt_to_list (int args, char type, char modifier, |
|
530 int& num_elts) |
|
531 { |
|
532 if (buf) |
|
533 { |
|
534 *buf << ends; |
|
535 |
|
536 char *text = buf->str (); |
|
537 |
|
538 if (text) |
|
539 { |
|
540 if (*text) |
|
541 { |
|
542 printf_format_elt *elt |
|
543 = new printf_format_elt (text, args, type, modifier); |
|
544 |
|
545 if (num_elts == list.length ()) |
|
546 list.resize (2 * num_elts); |
|
547 |
|
548 list.elem (num_elts++) = elt; |
|
549 } |
|
550 else |
|
551 delete [] text; |
|
552 } |
|
553 |
|
554 delete buf; |
|
555 buf = 0; |
|
556 } |
|
557 } |
|
558 |
|
559 void |
|
560 printf_format_list::process_conversion (const string& s, int& i, int n, |
|
561 int& args, char& modifier, |
|
562 char& type, int& num_elts) |
|
563 { |
|
564 args = 0; |
|
565 modifier = '\0'; |
|
566 type = '\0'; |
|
567 |
|
568 *buf << s[i++]; |
|
569 |
|
570 bool next = false; |
|
571 |
|
572 while (i < n) |
|
573 { |
|
574 switch (s[i]) |
|
575 { |
|
576 case '-': case '+': case ' ': case '0': case '#': |
|
577 *buf << s[i++]; |
|
578 break; |
|
579 |
|
580 default: |
|
581 next = true; |
|
582 break; |
|
583 } |
|
584 |
|
585 if (next) |
|
586 break; |
|
587 } |
|
588 |
|
589 if (i < n) |
|
590 { |
|
591 if (s[i] == '*') |
|
592 { |
|
593 args++; |
|
594 *buf << s[i++]; |
|
595 } |
|
596 else |
|
597 { |
|
598 while (i < n && isdigit (s[i])) |
|
599 *buf << s[i++]; |
|
600 } |
|
601 } |
|
602 |
|
603 if (i < n && s[i] == '.') |
|
604 { |
|
605 *buf << s[i++]; |
|
606 |
|
607 if (i < n) |
|
608 { |
|
609 if (s[i] == '*') |
|
610 { |
|
611 args++; |
|
612 *buf << s[i++]; |
|
613 } |
|
614 else |
|
615 { |
|
616 while (i < n && isdigit (s[i])) |
|
617 *buf << s[i++]; |
|
618 } |
|
619 } |
|
620 } |
|
621 |
|
622 if (i < n) |
|
623 { |
|
624 switch (s[i]) |
|
625 { |
|
626 case 'h': case 'l': case 'L': |
|
627 modifier = s[i]; |
|
628 *buf << s[i++]; |
|
629 break; |
|
630 |
|
631 default: |
|
632 break; |
|
633 } |
|
634 } |
|
635 |
|
636 if (i < n) |
|
637 finish_conversion (s, i, args, modifier, type, num_elts); |
|
638 else |
|
639 nconv = -1; |
|
640 } |
|
641 |
|
642 void |
|
643 printf_format_list::finish_conversion (const string& s, int& i, |
|
644 int args, char modifier, |
|
645 char& type, int& num_elts) |
|
646 |
|
647 { |
|
648 switch (s[i]) |
|
649 { |
|
650 case 'd': case 'i': case 'o': case 'x': case 'X': |
|
651 case 'u': case 'c': |
|
652 if (modifier == 'L') |
|
653 { |
|
654 nconv = -1; |
|
655 break; |
|
656 } |
|
657 goto fini; |
|
658 |
|
659 case 'f': case 'e': case 'E': case 'g': case 'G': |
|
660 if (modifier == 'h' || modifier == 'l') |
|
661 { |
|
662 nconv = -1; |
|
663 break; |
|
664 } |
|
665 goto fini; |
|
666 |
|
667 case 's': case 'p': case '%': |
|
668 if (modifier != '\0') |
|
669 { |
|
670 nconv = -1; |
|
671 break; |
|
672 } |
|
673 goto fini; |
|
674 |
|
675 fini: |
|
676 |
|
677 if (s[i] == '%' && args == 0) |
|
678 *buf << s[i++]; |
|
679 else |
|
680 { |
|
681 if (s[i] != '%') |
|
682 args++; |
|
683 |
|
684 type = s[i]; |
|
685 |
|
686 *buf << s[i++]; |
|
687 |
|
688 add_elt_to_list (args, type, modifier, num_elts); |
|
689 |
|
690 nconv++; |
|
691 } |
|
692 break; |
|
693 |
|
694 default: |
|
695 nconv = -1; |
|
696 break; |
|
697 } |
|
698 } |
|
699 |
|
700 void |
|
701 printf_format_list::printme (void) const |
|
702 { |
|
703 int n = list.length (); |
|
704 |
|
705 for (int i = 0; i < n; i++) |
|
706 { |
|
707 printf_format_elt *elt = list.elem (i); |
|
708 |
|
709 cerr << elt->args<< "\t" |
|
710 << elt->type << "\t" |
|
711 << elt->modifier << "\t" |
|
712 << undo_string_escapes (elt->text) << "\n"; |
|
713 } |
|
714 } |
|
715 |
|
716 void |
|
717 octave_base_stream::error (const string& msg) |
|
718 { |
|
719 fail = true; |
|
720 errmsg = msg; |
|
721 } |
|
722 |
|
723 void |
|
724 octave_base_stream::clear (void) |
|
725 { |
|
726 fail = false; |
|
727 errmsg = ""; |
|
728 } |
|
729 |
|
730 // Functions that are defined for all input streams (input streams |
|
731 // are those that define is). |
|
732 |
|
733 string |
|
734 octave_base_stream::do_gets (int max_len, bool& err, |
|
735 bool strip_newline, const char *fcn) |
|
736 { |
|
737 string retval; |
|
738 |
|
739 err = false; |
|
740 |
|
741 istream *isp = input_stream (); |
|
742 |
|
743 if (isp) |
|
744 { |
|
745 istream is = *isp; |
|
746 |
|
747 // XXX FIXME XXX -- this should probably be converted to use |
|
748 // sstream when that is available. |
|
749 ostrstream buf; |
|
750 |
|
751 int c = 0; |
|
752 int count = 0; |
|
753 int newline_stripped = 0; |
|
754 |
|
755 while (is && (c = is.get ()) != EOF) |
|
756 { |
|
757 count++; |
|
758 |
|
759 if (c == '\n') |
|
760 { |
|
761 if (! strip_newline) |
|
762 buf << (char) c; |
|
763 else |
|
764 newline_stripped = 1; |
|
765 |
|
766 break; |
|
767 } |
|
768 else |
|
769 buf << (char) c; |
|
770 |
|
771 if (max_len > 0 && count == max_len) |
|
772 break; |
|
773 } |
|
774 |
|
775 if (is.fail ()) |
|
776 { |
|
777 err = true; |
|
778 string msg = fcn; |
|
779 msg.append (": read error"); |
|
780 error (msg); |
|
781 } |
|
782 else if (is.eof ()) |
|
783 { |
|
784 err = true; |
|
785 string msg = fcn; |
|
786 msg.append (": at end of file"); |
|
787 error (msg); |
|
788 } |
|
789 else |
|
790 { |
|
791 buf << ends; |
|
792 char *tmp = buf.str (); |
|
793 retval = tmp; |
|
794 delete [] tmp; |
|
795 } |
|
796 } |
|
797 else |
|
798 { |
|
799 err = true; |
|
800 invalid_operation (fcn, "reading"); |
|
801 } |
|
802 |
|
803 return retval; |
|
804 } |
|
805 |
|
806 string |
|
807 octave_base_stream::getl (int max_len, bool& err) |
|
808 { |
|
809 return do_gets (max_len, err, true, "fgetl"); |
|
810 } |
|
811 |
|
812 string |
|
813 octave_base_stream::gets (int max_len, bool& err) |
|
814 { |
|
815 return do_gets (max_len, err, false, "fgets"); |
|
816 } |
|
817 |
|
818 // XXX FIXME XXX -- need to handle architecture type conversions. |
|
819 |
|
820 #define do_read_elem(is, type, val) \ |
|
821 do \ |
|
822 { \ |
|
823 type tmp_val; \ |
|
824 is.read ((char *) &tmp_val, sizeof (type)); \ |
|
825 val = tmp_val; \ |
|
826 } \ |
|
827 while (0) |
|
828 |
|
829 octave_value |
|
830 octave_base_stream::do_read (int nr, int nc, data_type dt, int skip, |
|
831 arch_type at, int& count) |
|
832 { |
|
833 count = 0; |
|
834 |
|
835 octave_value retval = Matrix (); |
|
836 |
|
837 istream *isp = input_stream (); |
|
838 |
|
839 Matrix mval; |
|
840 double *data = 0; |
|
841 int max_size = 0; |
|
842 |
|
843 int final_nr = 0; |
|
844 int final_nc = 0; |
|
845 |
|
846 if (nr > 0) |
|
847 { |
|
848 if (nc > 0) |
|
849 { |
|
850 mval.resize (nr, nc, 0.0); |
|
851 data = mval.fortran_vec (); |
|
852 max_size = nr * nc; |
|
853 } |
|
854 else |
|
855 { |
|
856 mval.resize (nr, 32, 0.0); |
|
857 data = mval.fortran_vec (); |
2121
|
858 max_size = nr * 32; |
2117
|
859 } |
|
860 } |
|
861 else |
|
862 { |
|
863 mval.resize (32, 1, 0.0); |
|
864 data = mval.fortran_vec (); |
|
865 max_size = 32; |
|
866 } |
|
867 |
|
868 if (isp) |
|
869 { |
|
870 istream is = *isp; |
|
871 |
|
872 for (;;) |
|
873 { |
|
874 // XXX FIXME XXX -- maybe there should be a special case for |
|
875 // skip == 0. |
|
876 |
|
877 if (is) |
|
878 { |
|
879 if (nr > 0 && nc > 0 && count == max_size) |
|
880 { |
|
881 final_nr = nr; |
|
882 final_nc = nc; |
|
883 |
|
884 break; |
|
885 } |
|
886 |
|
887 if (skip != 0) |
|
888 seek (skip, ios::cur); |
|
889 |
|
890 if (is) |
|
891 { |
2127
|
892 double tmp = 0.0; |
2117
|
893 |
|
894 switch (dt) |
|
895 { |
|
896 case dt_char: |
|
897 do_read_elem (is, char, tmp); |
|
898 break; |
|
899 |
|
900 case dt_schar: |
|
901 do_read_elem (is, signed char, tmp); |
|
902 break; |
|
903 |
|
904 case dt_uchar: |
|
905 do_read_elem (is, unsigned char, tmp); |
|
906 break; |
|
907 |
|
908 case dt_short: |
|
909 do_read_elem (is, short, tmp); |
|
910 break; |
|
911 |
|
912 case dt_ushort: |
|
913 do_read_elem (is, unsigned short, tmp); |
|
914 break; |
|
915 |
|
916 case dt_int: |
|
917 do_read_elem (is, int, tmp); |
|
918 break; |
|
919 |
|
920 case dt_uint: |
|
921 do_read_elem (is, unsigned int, tmp); |
|
922 break; |
|
923 |
|
924 case dt_long: |
|
925 do_read_elem (is, long, tmp); |
|
926 break; |
|
927 |
|
928 case dt_ulong: |
|
929 do_read_elem (is, unsigned long, tmp); |
|
930 break; |
|
931 |
|
932 case dt_float: |
|
933 do_read_elem (is, float, tmp); |
|
934 break; |
|
935 |
|
936 case dt_double: |
|
937 do_read_elem (is, double, tmp); |
|
938 break; |
|
939 |
|
940 default: |
|
941 error ("fread: invalid type specification"); |
|
942 } |
|
943 |
|
944 if (is && ok ()) |
|
945 { |
|
946 if (count == max_size) |
|
947 { |
|
948 max_size *= 2; |
|
949 |
|
950 if (nr > 0) |
|
951 mval.resize (nr, max_size / 2, 0.0); |
|
952 else |
|
953 mval.resize (max_size, 1, 0.0); |
|
954 |
|
955 data = mval.fortran_vec (); |
|
956 } |
|
957 |
|
958 data[count++] = tmp; |
|
959 } |
|
960 else |
|
961 { |
|
962 if (is.eof ()) |
|
963 { |
|
964 if (nr > 0) |
|
965 { |
|
966 if (count > nr) |
|
967 { |
|
968 final_nr = nr; |
|
969 final_nc = (count - 1) / nr + 1; |
|
970 } |
|
971 else |
|
972 { |
|
973 final_nr = count; |
|
974 final_nc = 1; |
|
975 } |
|
976 } |
|
977 else |
|
978 { |
|
979 final_nr = count; |
|
980 final_nc = 1; |
|
981 } |
|
982 } |
|
983 |
|
984 break; |
|
985 } |
|
986 } |
|
987 else |
|
988 { |
|
989 error ("fread: read error"); |
|
990 break; |
|
991 } |
|
992 } |
|
993 else |
|
994 { |
|
995 error ("fread: read error"); |
|
996 break; |
|
997 } |
|
998 } |
|
999 } |
|
1000 |
|
1001 if (ok ()) |
|
1002 { |
2121
|
1003 mval.resize (final_nr, final_nc, 0.0); |
2117
|
1004 |
|
1005 retval = mval; |
|
1006 } |
|
1007 |
|
1008 return retval; |
|
1009 } |
|
1010 |
|
1011 octave_value |
|
1012 octave_base_stream::read (const Matrix& size, data_type dt, int skip, |
|
1013 arch_type at, int& count) |
|
1014 { |
|
1015 octave_value retval; |
|
1016 |
|
1017 count = 0; |
|
1018 |
|
1019 istream *is = input_stream (); |
|
1020 |
|
1021 if (is) |
|
1022 { |
|
1023 int nr = -1; |
|
1024 int nc = -1; |
|
1025 |
|
1026 get_size (size, nr, nc, "fread"); |
|
1027 |
|
1028 if (! error_state) |
|
1029 retval = do_read (nr, nc, dt, skip, at, count); |
|
1030 } |
|
1031 else |
|
1032 invalid_operation ("fread", "reading"); |
|
1033 |
|
1034 return retval; |
|
1035 } |
|
1036 |
|
1037 #define do_scanf_conv(is, fmt, valptr, mval, data, idx, max_size, discard) \ |
|
1038 do \ |
|
1039 { \ |
|
1040 is.clear (); \ |
|
1041 \ |
|
1042 is.scan (fmt, valptr); \ |
|
1043 \ |
|
1044 if (is) \ |
|
1045 { \ |
|
1046 if (idx == max_size && ! discard) \ |
|
1047 { \ |
|
1048 max_size *= 2; \ |
|
1049 \ |
|
1050 if (nr > 0) \ |
2121
|
1051 mval.resize (nr, max_size / nr, 0.0); \ |
2117
|
1052 else \ |
|
1053 mval.resize (max_size, 1, 0.0); \ |
|
1054 \ |
|
1055 data = mval.fortran_vec (); \ |
|
1056 } \ |
|
1057 \ |
|
1058 if (! discard) \ |
|
1059 data[idx++] = *(valptr); \ |
|
1060 } \ |
|
1061 } \ |
|
1062 while (0) |
|
1063 |
|
1064 octave_value |
|
1065 octave_base_stream::do_scanf (scanf_format_list& fmt_list, |
|
1066 int nr, int nc, int& count) |
|
1067 { |
2121
|
1068 count = 0; |
|
1069 |
2117
|
1070 octave_value retval = Matrix (); |
|
1071 |
|
1072 istream *isp = input_stream (); |
|
1073 |
|
1074 bool all_char_conv = fmt_list.all_character_conversions (); |
|
1075 |
|
1076 Matrix mval; |
|
1077 double *data = 0; |
|
1078 int max_size = 0; |
|
1079 |
|
1080 int final_nr = 0; |
|
1081 int final_nc = 0; |
|
1082 |
|
1083 if (nr > 0) |
|
1084 { |
|
1085 if (nc > 0) |
|
1086 { |
|
1087 mval.resize (nr, nc, 0.0); |
|
1088 data = mval.fortran_vec (); |
|
1089 max_size = nr * nc; |
|
1090 } |
|
1091 else |
|
1092 { |
|
1093 mval.resize (nr, 32, 0.0); |
|
1094 data = mval.fortran_vec (); |
2121
|
1095 max_size = nr * 32; |
2117
|
1096 } |
|
1097 } |
|
1098 else |
|
1099 { |
|
1100 mval.resize (32, 1, 0.0); |
|
1101 data = mval.fortran_vec (); |
|
1102 max_size = 32; |
|
1103 } |
|
1104 |
|
1105 if (isp) |
|
1106 { |
|
1107 istream is = *isp; |
|
1108 |
|
1109 const scanf_format_elt *elt = fmt_list.first (); |
|
1110 |
2213
|
1111 ios::fmtflags flags = is.flags (); |
|
1112 |
2117
|
1113 for (;;) |
|
1114 { |
2213
|
1115 // Restore format flags in case we had to change them (note |
|
1116 // 'c' conversion below). |
|
1117 |
|
1118 is.setf (flags); |
|
1119 |
2117
|
1120 if (elt) |
|
1121 { |
|
1122 if (nr > 0 && nc > 0 && count == max_size) |
|
1123 { |
|
1124 final_nr = nr; |
|
1125 final_nc = nc; |
|
1126 |
|
1127 break; |
|
1128 } |
|
1129 |
|
1130 const char *fmt = elt->text; |
|
1131 |
|
1132 bool discard = elt->discard; |
|
1133 |
|
1134 switch (elt->type) |
|
1135 { |
|
1136 case '%': |
|
1137 { |
|
1138 int dummy; |
|
1139 |
|
1140 do_scanf_conv (is, fmt, &dummy, mval, data, count, |
|
1141 max_size, discard); |
|
1142 } |
|
1143 break; |
|
1144 |
|
1145 case 'd': case 'i': case 'o': case 'u': case 'x': |
|
1146 { |
|
1147 int tmp; |
|
1148 |
|
1149 do_scanf_conv (is, fmt, &tmp, mval, data, count, |
|
1150 max_size, discard); |
|
1151 } |
|
1152 break; |
|
1153 |
|
1154 case 'e': case 'f': case 'g': |
|
1155 { |
|
1156 if (elt->modifier == 'l') |
|
1157 { |
|
1158 double tmp; |
|
1159 |
|
1160 do_scanf_conv (is, fmt, &tmp, mval, data, |
|
1161 count, max_size, discard); |
|
1162 } |
|
1163 else |
|
1164 { |
|
1165 float tmp; |
|
1166 |
|
1167 do_scanf_conv (is, fmt, &tmp, mval, data, |
|
1168 count, max_size, discard); |
|
1169 } |
|
1170 } |
|
1171 break; |
|
1172 |
2213
|
1173 case 'c': |
|
1174 is.unsetf (ios::skipws); |
|
1175 // Fall through... |
|
1176 |
2117
|
1177 case 's': |
|
1178 { |
|
1179 char tmp; |
|
1180 |
|
1181 do_scanf_conv (is, fmt, &tmp, mval, data, count, |
|
1182 max_size, discard); |
|
1183 } |
|
1184 break; |
|
1185 |
|
1186 case 'p': case '[': |
|
1187 error ("fscanf: unrecognized format specifier"); |
|
1188 break; |
|
1189 |
|
1190 default: |
|
1191 error ("fscanf: internal format error"); |
|
1192 break; |
|
1193 } |
|
1194 |
|
1195 if (! ok ()) |
|
1196 { |
|
1197 break; |
|
1198 } |
|
1199 else if (! is) |
|
1200 { |
|
1201 if (is.eof ()) |
|
1202 { |
|
1203 if (nr > 0) |
|
1204 { |
|
1205 if (count > nr) |
|
1206 { |
|
1207 final_nr = nr; |
|
1208 final_nc = (count - 1) / nr + 1; |
|
1209 } |
|
1210 else |
|
1211 { |
|
1212 final_nr = count; |
|
1213 final_nc = 1; |
|
1214 } |
|
1215 } |
|
1216 else |
|
1217 { |
|
1218 final_nr = count; |
|
1219 final_nc = 1; |
|
1220 } |
|
1221 } |
|
1222 else |
|
1223 { |
|
1224 error ("fscanf: read error"); |
|
1225 |
|
1226 // XXX FIXME XXX -- is this the right thing to do? |
|
1227 // What about other streams? |
|
1228 if (name () == "stdin") |
|
1229 { |
|
1230 is.clear (); |
|
1231 |
|
1232 // Skip to end of line. |
|
1233 |
|
1234 bool err; |
|
1235 do_gets (-1, err, false, "fscanf"); |
|
1236 } |
|
1237 } |
|
1238 |
|
1239 break; |
|
1240 } |
|
1241 } |
|
1242 else |
|
1243 { |
|
1244 error ("fscanf: internal format error"); |
|
1245 break; |
|
1246 } |
|
1247 |
|
1248 elt = fmt_list.next (); |
|
1249 } |
|
1250 } |
|
1251 |
|
1252 if (ok ()) |
|
1253 { |
2121
|
1254 mval.resize (final_nr, final_nc, 0.0); |
2117
|
1255 |
|
1256 if (all_char_conv) |
|
1257 { |
|
1258 if (nr < 0) |
|
1259 mval = mval.transpose (); |
|
1260 |
|
1261 retval = mval; |
|
1262 |
|
1263 retval = retval.convert_to_str (); |
|
1264 } |
|
1265 else |
|
1266 retval = mval; |
|
1267 } |
|
1268 |
|
1269 return retval; |
|
1270 } |
|
1271 |
|
1272 octave_value |
|
1273 octave_base_stream::scanf (const string& fmt, const Matrix& size, |
|
1274 int& count) |
|
1275 { |
|
1276 // XXX FIXME XXX -- is this the right thing to do? |
|
1277 if (name () == "stdin") |
|
1278 fail = false; |
|
1279 |
|
1280 octave_value retval = Matrix (); |
|
1281 |
|
1282 count = 0; |
|
1283 |
|
1284 istream *isp = input_stream (); |
|
1285 |
|
1286 if (isp) |
|
1287 { |
|
1288 istream is = *isp; |
|
1289 |
|
1290 scanf_format_list fmt_list (fmt); |
|
1291 |
|
1292 switch (fmt_list.num_conversions ()) |
|
1293 { |
|
1294 case -1: |
|
1295 ::error ("fscanf: invalid format specified"); |
|
1296 break; |
|
1297 |
|
1298 case 0: |
|
1299 { |
|
1300 const scanf_format_elt *elt = fmt_list.first (); |
|
1301 |
|
1302 if (elt) |
|
1303 { |
|
1304 is.clear (); |
|
1305 |
|
1306 is.scan (elt->text); |
|
1307 |
|
1308 if (! is) |
|
1309 { |
|
1310 error ("fscanf: read error"); |
|
1311 |
|
1312 // XXX FIXME XXX -- is this the right thing to do? |
|
1313 // Maybe. We should probably also arrange to |
|
1314 // flush the pending input prior to printing a |
|
1315 // prompt. Or maybe just blow off scanf for stdin |
|
1316 // like the MathWorks did. What about other streams? |
|
1317 |
|
1318 if (name () == "stdin") |
|
1319 { |
|
1320 is.clear (); |
|
1321 |
|
1322 // Skip to end of line. |
|
1323 |
|
1324 bool err; |
|
1325 do_gets (-1, err, false, "fscanf"); |
|
1326 } |
|
1327 } |
|
1328 } |
|
1329 } |
|
1330 break; |
|
1331 |
|
1332 default: |
|
1333 { |
|
1334 int nr = -1; |
|
1335 int nc = -1; |
|
1336 |
|
1337 get_size (size, nr, nc, "fscanf"); |
|
1338 |
|
1339 if (! error_state) |
|
1340 retval = do_scanf (fmt_list, nr, nc, count); |
|
1341 } |
|
1342 break; |
|
1343 } |
|
1344 } |
|
1345 else |
|
1346 invalid_operation ("fscanf", "writing"); |
|
1347 |
|
1348 return retval; |
|
1349 } |
|
1350 |
|
1351 // Functions that are defined for all output streams (output streams |
|
1352 // are those that define os). |
|
1353 |
|
1354 int |
|
1355 octave_base_stream::flush (void) |
|
1356 { |
|
1357 int retval = -1; |
|
1358 |
|
1359 ostream *os = output_stream (); |
|
1360 |
|
1361 if (os) |
|
1362 { |
|
1363 os->flush (); |
|
1364 |
|
1365 if (os->good ()) |
|
1366 retval = 0; |
|
1367 } |
|
1368 else |
|
1369 invalid_operation ("fflush", "writing"); |
|
1370 |
|
1371 return retval; |
|
1372 } |
|
1373 |
|
1374 // XXX FIXME XXX -- need to handle architecture type conversions. |
|
1375 |
|
1376 #define do_write_elem(os, type, val) \ |
|
1377 do \ |
|
1378 { \ |
|
1379 type tmp_val = (type) val; \ |
|
1380 os.write ((char *) &tmp_val, sizeof (type)); \ |
|
1381 } \ |
|
1382 while (0) |
|
1383 |
|
1384 int |
|
1385 octave_base_stream::do_write (const double *data, int n, data_type dt, |
|
1386 int skip, arch_type at) |
|
1387 { |
|
1388 int retval = -1; |
|
1389 |
|
1390 int count = 0; |
|
1391 |
|
1392 ostream *osp = output_stream (); |
|
1393 |
|
1394 if (osp) |
|
1395 { |
|
1396 ostream os = *osp; |
|
1397 |
|
1398 // XXX FIXME XXX -- maybe there should be a special case for |
|
1399 // skip == 0. |
|
1400 |
|
1401 for (int i = 0; i < n; i++) |
|
1402 { |
|
1403 if (os) |
|
1404 { |
|
1405 if (skip != 0) |
|
1406 seek (skip, ios::cur); |
|
1407 |
|
1408 if (os) |
|
1409 { |
|
1410 double tmp = data[i]; |
|
1411 |
|
1412 switch (dt) |
|
1413 { |
|
1414 case dt_char: |
|
1415 do_write_elem (os, char, tmp); |
|
1416 break; |
|
1417 |
|
1418 case dt_schar: |
|
1419 do_write_elem (os, signed char, tmp); |
|
1420 break; |
|
1421 |
|
1422 case dt_uchar: |
|
1423 do_write_elem (os, unsigned char, tmp); |
|
1424 break; |
|
1425 |
|
1426 case dt_short: |
|
1427 do_write_elem (os, short, tmp); |
|
1428 break; |
|
1429 |
|
1430 case dt_ushort: |
|
1431 do_write_elem (os, unsigned short, tmp); |
|
1432 break; |
|
1433 |
|
1434 case dt_int: |
|
1435 do_write_elem (os, int, tmp); |
|
1436 break; |
|
1437 |
|
1438 case dt_uint: |
|
1439 do_write_elem (os, unsigned int, tmp); |
|
1440 break; |
|
1441 |
|
1442 case dt_long: |
|
1443 do_write_elem (os, long, tmp); |
|
1444 break; |
|
1445 |
|
1446 case dt_ulong: |
|
1447 do_write_elem (os, unsigned long, tmp); |
|
1448 break; |
|
1449 |
|
1450 case dt_float: |
|
1451 do_write_elem (os, float, tmp); |
|
1452 break; |
|
1453 |
|
1454 case dt_double: |
|
1455 do_write_elem (os, double, tmp); |
|
1456 break; |
|
1457 |
|
1458 default: |
|
1459 error ("fwrite: invalid type specification"); |
|
1460 } |
|
1461 |
|
1462 if (os && ok ()) |
|
1463 count++; |
|
1464 else |
|
1465 break; |
|
1466 } |
|
1467 else |
|
1468 { |
|
1469 error ("fwrite: write error"); |
|
1470 break; |
|
1471 } |
|
1472 } |
|
1473 else |
|
1474 { |
|
1475 error ("fwrite: write error"); |
|
1476 break; |
|
1477 } |
|
1478 } |
|
1479 } |
|
1480 |
|
1481 if (ok ()) |
|
1482 retval = count; |
|
1483 |
|
1484 return retval; |
|
1485 } |
|
1486 |
|
1487 int |
|
1488 octave_base_stream::write (const octave_value& data, data_type dt, |
|
1489 int skip, arch_type at) |
|
1490 { |
|
1491 int retval = -1; |
|
1492 |
|
1493 ostream *os = output_stream (); |
|
1494 |
|
1495 if (os) |
|
1496 { |
|
1497 Matrix mval = data.matrix_value (); |
|
1498 |
|
1499 if (! error_state) |
|
1500 { |
|
1501 int n = mval.length (); |
|
1502 |
|
1503 const double *d = mval.data (); |
|
1504 |
|
1505 retval = octave_base_stream::do_write (d, n, dt, skip, at); |
|
1506 } |
|
1507 } |
|
1508 else |
|
1509 invalid_operation ("fwrite", "writing"); |
|
1510 |
|
1511 return retval; |
|
1512 } |
|
1513 |
|
1514 class |
|
1515 printf_value_cache |
|
1516 { |
|
1517 public: |
|
1518 |
|
1519 enum state { ok, list_exhausted, conversion_error }; |
|
1520 |
|
1521 printf_value_cache (const octave_value_list& args) |
|
1522 : values (args), val_idx (0), elt_idx (0), |
|
1523 n_vals (values.length ()), n_elts (0), data (0), |
|
1524 curr_state (ok) { } |
|
1525 |
|
1526 ~printf_value_cache (void) { } |
|
1527 |
|
1528 // Get the current value as a double and advance the internal pointer. |
|
1529 double double_value (void); |
|
1530 |
|
1531 // Get the current value as an int and advance the internal pointer. |
|
1532 int int_value (void); |
|
1533 |
|
1534 // Get the current value as a string and advance the internal pointer. |
|
1535 string string_value (void); |
|
1536 |
|
1537 operator void* () const |
|
1538 { return (curr_state == ok) ? (void *) -1 : (void *) 0; } |
|
1539 |
|
1540 bool no_more_values (void) { return curr_state == list_exhausted; } |
|
1541 |
|
1542 bool looking_at_string (void); |
|
1543 |
|
1544 private: |
|
1545 |
|
1546 const octave_value_list values; |
|
1547 int val_idx; |
|
1548 int elt_idx; |
|
1549 int n_vals; |
|
1550 int n_elts; |
|
1551 const double *data; |
|
1552 Matrix curr_val; |
|
1553 state curr_state; |
|
1554 |
|
1555 // Must create value cache with values! |
|
1556 |
|
1557 printf_value_cache (void); |
|
1558 |
|
1559 // No copying! |
|
1560 |
|
1561 printf_value_cache (const printf_value_cache&); |
|
1562 |
|
1563 printf_value_cache& operator = (const printf_value_cache&); |
|
1564 }; |
|
1565 |
|
1566 bool |
|
1567 printf_value_cache::looking_at_string (void) |
|
1568 { |
|
1569 bool retval = false; |
|
1570 |
|
1571 int idx = -1; |
|
1572 |
|
1573 if (elt_idx == 0) |
|
1574 idx = val_idx; |
|
1575 else if (elt_idx >= n_elts) |
|
1576 idx = val_idx + 1; |
|
1577 |
|
1578 if (idx >= 0 && idx < n_vals) |
|
1579 { |
|
1580 octave_value tmp_val = values (idx); |
|
1581 |
|
1582 retval = tmp_val.is_string () && tmp_val.rows () == 1; |
|
1583 } |
|
1584 |
|
1585 return retval; |
|
1586 } |
|
1587 |
|
1588 double |
|
1589 printf_value_cache::double_value (void) |
|
1590 { |
|
1591 double retval = 0.0; |
|
1592 |
|
1593 while (val_idx < n_vals) |
|
1594 { |
|
1595 if (! data) |
|
1596 { |
|
1597 octave_value tmp_val = values (val_idx); |
|
1598 |
|
1599 curr_val = tmp_val.matrix_value (); |
|
1600 |
|
1601 if (! error_state) |
|
1602 { |
|
1603 elt_idx = 0; |
|
1604 n_elts = curr_val.length (); |
|
1605 data = curr_val.data (); |
|
1606 } |
|
1607 else |
|
1608 { |
|
1609 curr_state = conversion_error; |
|
1610 break; |
|
1611 } |
|
1612 } |
|
1613 |
|
1614 if (elt_idx < n_elts) |
|
1615 { |
|
1616 return data[elt_idx++]; |
|
1617 break; |
|
1618 } |
|
1619 else |
|
1620 { |
|
1621 val_idx++; |
|
1622 data = 0; |
|
1623 continue; |
|
1624 } |
|
1625 } |
|
1626 |
|
1627 curr_state = list_exhausted; |
|
1628 |
|
1629 return retval; |
|
1630 } |
|
1631 |
|
1632 int |
|
1633 printf_value_cache::int_value (void) |
|
1634 { |
|
1635 int retval = 0; |
|
1636 |
|
1637 double dval = double_value (); |
|
1638 |
|
1639 if (! error_state) |
|
1640 { |
|
1641 if (D_NINT (dval) == dval) |
|
1642 retval = NINT (dval); |
|
1643 else |
|
1644 curr_state = conversion_error; |
|
1645 } |
|
1646 |
|
1647 return retval; |
|
1648 } |
|
1649 |
|
1650 string |
|
1651 printf_value_cache::string_value (void) |
|
1652 { |
|
1653 string retval; |
|
1654 |
|
1655 if (looking_at_string ()) |
|
1656 { |
|
1657 if (elt_idx != 0) |
|
1658 { |
|
1659 val_idx++; |
|
1660 elt_idx = 0; |
|
1661 data = 0; |
|
1662 } |
|
1663 |
|
1664 retval = values (val_idx++).string_value (); |
|
1665 } |
|
1666 else |
|
1667 curr_state = conversion_error; |
|
1668 |
|
1669 return retval; |
|
1670 } |
|
1671 |
|
1672 // Ugh again and again. |
|
1673 |
|
1674 #define do_printf_conv(os, fmt, nsa, sa_1, sa_2, have_arg, arg) \ |
|
1675 do \ |
|
1676 { \ |
|
1677 switch (nsa) \ |
|
1678 { \ |
|
1679 case 2: \ |
|
1680 if (have_arg) \ |
|
1681 os.form (fmt, sa_1, sa_2, arg); \ |
|
1682 else \ |
|
1683 os.form (fmt, sa_1, sa_2); \ |
|
1684 break; \ |
|
1685 \ |
|
1686 case 1: \ |
|
1687 if (have_arg) \ |
|
1688 os.form (fmt, sa_1, arg); \ |
|
1689 else \ |
|
1690 os.form (fmt, sa_1); \ |
|
1691 break; \ |
|
1692 \ |
|
1693 case 0: \ |
|
1694 if (have_arg) \ |
|
1695 os.form (fmt, arg); \ |
|
1696 else \ |
|
1697 os.form (fmt); \ |
|
1698 break; \ |
|
1699 \ |
|
1700 default: \ |
|
1701 ::error ("fprintf: internal error handling format"); \ |
|
1702 break; \ |
|
1703 } \ |
|
1704 } \ |
|
1705 while (0) |
|
1706 |
|
1707 int |
|
1708 octave_base_stream::do_printf (printf_format_list& fmt_list, |
|
1709 const octave_value_list& args) |
|
1710 { |
|
1711 int retval = -1; |
|
1712 |
|
1713 ostream *osp = output_stream (); |
|
1714 |
|
1715 if (osp) |
|
1716 { |
|
1717 ostream os = *osp; |
|
1718 |
|
1719 const printf_format_elt *elt = fmt_list.first (); |
|
1720 |
|
1721 printf_value_cache val_cache (args); |
|
1722 |
|
1723 for (;;) |
|
1724 { |
|
1725 if (elt) |
|
1726 { |
|
1727 int args = elt->args; |
|
1728 int nsa = args; |
|
1729 |
|
1730 int doing_percent = elt->type == '%'; |
|
1731 |
|
1732 if (args > 0 && ! doing_percent) |
|
1733 nsa--; |
|
1734 |
|
1735 int sa_1 = 0; |
|
1736 int sa_2 = 0; |
|
1737 |
|
1738 if (nsa > 0) |
|
1739 { |
|
1740 sa_1 = val_cache.int_value (); |
|
1741 |
|
1742 if (! val_cache) |
|
1743 break; |
|
1744 else |
|
1745 { |
|
1746 if (nsa > 1) |
|
1747 { |
|
1748 sa_2 = val_cache.int_value (); |
|
1749 |
|
1750 if (! val_cache) |
|
1751 break; |
|
1752 } |
|
1753 } |
|
1754 } |
|
1755 |
|
1756 const char *fmt = elt->text; |
|
1757 |
|
1758 if (doing_percent || args == 0) |
|
1759 do_printf_conv (os, fmt, nsa, sa_1, sa_2, 0, 0.0); |
|
1760 else |
|
1761 { |
|
1762 if (elt->type == 's' && val_cache.looking_at_string ()) |
|
1763 { |
|
1764 string val = val_cache.string_value (); |
|
1765 |
|
1766 if (val_cache) |
|
1767 do_printf_conv (os, fmt, nsa, sa_1, sa_2, 1, |
|
1768 val.c_str ()); |
|
1769 else |
|
1770 break; |
|
1771 } |
|
1772 else |
|
1773 { |
|
1774 double val = val_cache.double_value (); |
|
1775 |
|
1776 if (val_cache) |
|
1777 { |
|
1778 switch (elt->type) |
|
1779 { |
|
1780 case 'd': case 'i': case 'o': case 'x': |
|
1781 case 'X': case 'u': case 'c': |
|
1782 { |
|
1783 if (elt->modifier == 'l') |
|
1784 do_printf_conv (os, fmt, nsa, sa_1, |
|
1785 sa_2, 1, (long) val); |
|
1786 else |
|
1787 do_printf_conv (os, fmt, nsa, sa_1, |
|
1788 sa_2, 1, (int) val); |
|
1789 } |
|
1790 break; |
|
1791 |
|
1792 case 'f': case 'e': case 'E': |
|
1793 case 'g': case 'G': |
|
1794 do_printf_conv (os, fmt, nsa, sa_1, |
|
1795 sa_2, 1, val); |
|
1796 break; |
|
1797 |
|
1798 default: |
|
1799 error ("fprintf: invalid format specifier"); |
|
1800 return -1; |
|
1801 break; |
|
1802 } |
|
1803 } |
|
1804 else |
|
1805 break; |
|
1806 } |
|
1807 |
|
1808 if (val_cache.no_more_values ()) |
|
1809 { |
|
1810 retval = 0; |
|
1811 break; |
|
1812 } |
|
1813 } |
|
1814 |
|
1815 if (os) |
|
1816 retval += nsa + (doing_percent ? 0 : 1); |
|
1817 else |
|
1818 { |
|
1819 error ("fprintf: write error"); |
|
1820 retval = -1; |
|
1821 break; |
|
1822 } |
|
1823 } |
|
1824 else |
|
1825 { |
|
1826 ::error ("fprintf: internal error handling format"); |
|
1827 retval = -1; |
|
1828 break; |
|
1829 } |
|
1830 |
|
1831 elt = fmt_list.next (); |
|
1832 } |
|
1833 } |
|
1834 |
|
1835 return retval; |
|
1836 } |
|
1837 |
|
1838 int |
|
1839 octave_base_stream::printf (const string& fmt, const octave_value_list& args) |
|
1840 { |
|
1841 int retval = -1; |
|
1842 |
|
1843 ostream *osp = output_stream (); |
|
1844 |
|
1845 if (osp) |
|
1846 { |
|
1847 ostream os = *osp; |
|
1848 |
|
1849 printf_format_list fmt_list (fmt); |
|
1850 |
|
1851 switch (fmt_list.num_conversions ()) |
|
1852 { |
|
1853 case -1: |
|
1854 ::error ("fprintf: invalid format specified"); |
|
1855 break; |
|
1856 |
|
1857 case 0: |
|
1858 { |
|
1859 const printf_format_elt *elt = fmt_list.first (); |
|
1860 |
|
1861 if (elt) |
|
1862 { |
|
1863 os.form (elt->text); |
|
1864 |
|
1865 if (os) |
|
1866 retval = 0; |
|
1867 else |
|
1868 error ("fprintf: write error"); |
|
1869 } |
|
1870 } |
|
1871 break; |
|
1872 |
|
1873 default: |
|
1874 { |
|
1875 if (args.length () == 0) |
|
1876 ::error ("fprintf: no arguments available for specified format"); |
|
1877 else |
|
1878 retval = do_printf (fmt_list, args); |
|
1879 } |
|
1880 break; |
|
1881 } |
|
1882 } |
|
1883 else |
|
1884 invalid_operation ("fprintf", "writing"); |
|
1885 |
|
1886 return retval; |
|
1887 } |
|
1888 |
|
1889 int |
|
1890 octave_base_stream::puts (const string& s) |
|
1891 { |
|
1892 int retval = -1; |
|
1893 |
|
1894 ostream *osp = output_stream (); |
|
1895 |
|
1896 if (osp) |
|
1897 { |
|
1898 ostream os = *osp; |
|
1899 |
|
1900 os << s; |
|
1901 |
|
1902 if (os) |
|
1903 { |
|
1904 // XXX FIXME XXX -- why does this seem to be necessary? |
|
1905 // Without it, output from a loop like |
|
1906 // |
|
1907 // for i = 1:100, fputs (stdout, "foo\n"); endfor |
|
1908 // |
|
1909 // doesn't seem to go to the pager immediately. |
|
1910 |
|
1911 os.flush (); |
|
1912 |
|
1913 if (os) |
|
1914 retval = 0; |
|
1915 else |
|
1916 error ("fputs: write error"); |
|
1917 } |
|
1918 else |
|
1919 error ("fputs: write error"); |
|
1920 } |
|
1921 else |
|
1922 invalid_operation ("fputs", "writing"); |
|
1923 |
|
1924 return retval; |
|
1925 } |
|
1926 |
|
1927 int |
|
1928 octave_base_stream::rewind (void) |
|
1929 { |
|
1930 return seek (0, ios::beg); |
|
1931 } |
|
1932 |
|
1933 // Return current error message for this stream. |
|
1934 |
|
1935 string |
|
1936 octave_base_stream::error (bool clear_err, int& errno) |
|
1937 { |
|
1938 errno = fail ? -1 : 0; |
|
1939 |
|
1940 string tmp = errmsg; |
|
1941 |
|
1942 if (clear_err) |
|
1943 clear (); |
|
1944 |
|
1945 return tmp; |
|
1946 } |
|
1947 |
|
1948 void |
|
1949 octave_base_stream::invalid_operation (const char *op, const char *rw) |
|
1950 { |
|
1951 string msg = op; |
|
1952 msg.append (": stream not open for "); |
|
1953 msg.append (rw); |
|
1954 error (msg); |
|
1955 } |
|
1956 |
|
1957 int |
|
1958 octave_stream::flush (void) |
|
1959 { |
|
1960 int retval = -1; |
|
1961 |
|
1962 if (stream_ok ("fflush")) |
|
1963 retval = rep->flush (); |
|
1964 |
|
1965 return retval; |
|
1966 } |
|
1967 |
|
1968 string |
|
1969 octave_stream::getl (int max_len, bool& err) |
|
1970 { |
|
1971 string retval; |
|
1972 |
|
1973 if (stream_ok ("getl")) |
|
1974 retval = rep->getl (max_len, err); |
|
1975 |
|
1976 return retval; |
|
1977 } |
|
1978 |
|
1979 string |
|
1980 octave_stream::getl (const octave_value& tc_max_len, bool& err) |
|
1981 { |
|
1982 string retval; |
|
1983 |
|
1984 err = false; |
|
1985 |
|
1986 int conv_err = 0; |
|
1987 |
|
1988 int max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
1989 |
|
1990 if (conv_err || max_len < 0) |
|
1991 { |
|
1992 err = true; |
|
1993 ::error ("fgetl: invalid maximum length specified"); |
|
1994 } |
|
1995 else |
|
1996 retval = getl (max_len, err); |
|
1997 |
|
1998 return retval; |
|
1999 } |
|
2000 |
|
2001 string |
|
2002 octave_stream::gets (int max_len, bool& err) |
|
2003 { |
|
2004 string retval; |
|
2005 |
|
2006 if (stream_ok ("fgets")) |
|
2007 retval = rep->gets (max_len, err); |
|
2008 |
|
2009 return retval; |
|
2010 } |
|
2011 |
|
2012 string |
|
2013 octave_stream::gets (const octave_value& tc_max_len, bool& err) |
|
2014 { |
|
2015 string retval; |
|
2016 |
|
2017 err = false; |
|
2018 |
|
2019 int conv_err = 0; |
|
2020 |
|
2021 int max_len = convert_to_valid_int (tc_max_len, conv_err); |
|
2022 |
|
2023 if (conv_err || max_len < 0) |
|
2024 { |
|
2025 err = true; |
|
2026 ::error ("fgets: invalid maximum length specified"); |
|
2027 } |
|
2028 else |
|
2029 retval = gets (max_len, err); |
|
2030 |
|
2031 return retval; |
|
2032 } |
|
2033 |
|
2034 int |
|
2035 octave_stream::seek (streampos offset, ios::seek_dir origin) |
|
2036 { |
|
2037 int retval = -1; |
|
2038 |
|
2039 if (stream_ok ("fseek")) |
|
2040 retval = rep->seek (offset, origin); |
|
2041 |
|
2042 return retval; |
|
2043 } |
|
2044 |
|
2045 int |
|
2046 octave_stream::seek (const octave_value& tc_offset, |
|
2047 const octave_value& tc_origin) |
|
2048 { |
|
2049 int retval = -1; |
|
2050 |
|
2051 int conv_err = 0; |
|
2052 |
|
2053 int xoffset = convert_to_valid_int (tc_offset, conv_err); |
|
2054 |
|
2055 if (! conv_err) |
|
2056 { |
|
2057 int xorigin = convert_to_valid_int (tc_origin, conv_err); |
|
2058 |
|
2059 ios::seek_dir origin = ios::beg; |
|
2060 |
|
2061 // XXX FIXME XXX -- matlab allows origin to be: |
|
2062 // |
|
2063 // "bof" or -1 == ios::beg |
|
2064 // "cof" or 0 == ios::cur |
|
2065 // "eof" or 1 == ios::end |
|
2066 |
|
2067 if (! conv_err) |
|
2068 { |
|
2069 if (xorigin == 0) |
|
2070 origin = ios::beg; |
|
2071 else if (xorigin == 1) |
|
2072 origin = ios::cur; |
|
2073 else if (xorigin == 2) |
|
2074 origin = ios::end; |
|
2075 else |
|
2076 conv_err = -1; |
|
2077 } |
|
2078 |
|
2079 if (! conv_err) |
|
2080 retval = seek (xoffset, origin); |
|
2081 else |
|
2082 error ("fseek: invalid value for origin"); |
|
2083 } |
|
2084 else |
|
2085 error ("fseek: invalid value for offset"); |
|
2086 |
|
2087 return retval; |
|
2088 } |
|
2089 |
|
2090 long |
|
2091 octave_stream::tell (void) const |
|
2092 { |
|
2093 long retval = -1; |
|
2094 |
|
2095 if (stream_ok ("tell")) |
|
2096 retval = rep->tell (); |
|
2097 |
|
2098 return retval; |
|
2099 } |
|
2100 |
|
2101 int |
|
2102 octave_stream::rewind (void) |
|
2103 { |
|
2104 int retval = -1; |
|
2105 |
|
2106 if (stream_ok ("frewind")) |
|
2107 retval = rep->rewind (); |
|
2108 |
|
2109 return retval; |
|
2110 } |
|
2111 |
|
2112 octave_value |
|
2113 octave_stream::read (const Matrix& size, |
|
2114 octave_base_stream::data_type dt, int skip, |
|
2115 octave_base_stream::arch_type at, int& count) |
|
2116 { |
|
2117 octave_value retval; |
|
2118 |
|
2119 if (stream_ok ("fread")) |
|
2120 retval = rep->read (size, dt, skip, at, count); |
|
2121 |
|
2122 return retval; |
|
2123 } |
|
2124 |
|
2125 int |
|
2126 octave_stream::write (const octave_value& data, |
|
2127 octave_base_stream::data_type dt, int skip, |
|
2128 octave_base_stream::arch_type at) |
|
2129 { |
|
2130 int retval = -1; |
|
2131 |
|
2132 if (stream_ok ("fwrite")) |
|
2133 retval = rep->write (data, dt, skip, at); |
|
2134 |
|
2135 return retval; |
|
2136 } |
|
2137 |
|
2138 octave_value |
|
2139 octave_stream::scanf (const string& fmt, const Matrix& size, int& count) |
|
2140 { |
|
2141 octave_value retval; |
|
2142 |
|
2143 if (stream_ok ("fscanf")) |
|
2144 retval = rep->scanf (fmt, size, count); |
|
2145 |
|
2146 return retval; |
|
2147 } |
|
2148 |
|
2149 int |
|
2150 octave_stream::printf (const string& fmt, const octave_value_list& args) |
|
2151 { |
|
2152 int retval = -1; |
|
2153 |
|
2154 if (stream_ok ("fprintf")) |
|
2155 retval = rep->printf (fmt, args); |
|
2156 |
|
2157 return retval; |
|
2158 } |
|
2159 |
|
2160 int |
|
2161 octave_stream::puts (const string& s) |
|
2162 { |
|
2163 int retval = -1; |
|
2164 |
|
2165 if (stream_ok ("fputs")) |
|
2166 retval = rep->puts (s); |
|
2167 |
|
2168 return retval; |
|
2169 } |
|
2170 |
|
2171 // XXX FIXME XXX -- maybe this should work for string arrays too. |
|
2172 |
|
2173 int |
|
2174 octave_stream::puts (const octave_value& tc_s) |
|
2175 { |
|
2176 int retval = -1; |
|
2177 |
|
2178 if (tc_s.is_string ()) |
|
2179 { |
|
2180 string s = tc_s.string_value (); |
|
2181 retval = rep->puts (s); |
|
2182 } |
|
2183 else |
|
2184 error ("fputs: argument must be a string"); |
|
2185 |
|
2186 return retval; |
|
2187 } |
|
2188 |
|
2189 bool |
|
2190 octave_stream::eof (void) const |
|
2191 { |
|
2192 int retval = -1; |
|
2193 |
|
2194 if (stream_ok ("feof")) |
|
2195 retval = rep->eof (); |
|
2196 |
|
2197 return retval; |
|
2198 } |
|
2199 |
|
2200 string |
|
2201 octave_stream::error (bool clear, int& errno) |
|
2202 { |
|
2203 string retval; |
|
2204 |
|
2205 if (stream_ok ("ferror", false)) |
|
2206 retval = rep->error (clear, errno); |
|
2207 |
|
2208 cerr << retval; |
|
2209 |
|
2210 return retval; |
|
2211 } |
|
2212 |
|
2213 string |
|
2214 octave_stream::name (void) |
|
2215 { |
|
2216 string retval; |
|
2217 |
|
2218 if (stream_ok ("name")) |
|
2219 retval = rep->name (); |
|
2220 |
|
2221 return retval; |
|
2222 } |
|
2223 |
|
2224 int |
|
2225 octave_stream::mode (void) |
|
2226 { |
|
2227 int retval = 0; |
|
2228 |
|
2229 if (stream_ok ("mode")) |
|
2230 retval = rep->mode (); |
|
2231 |
|
2232 return retval; |
|
2233 } |
|
2234 |
|
2235 octave_base_stream::arch_type |
|
2236 octave_stream::architecture (void) |
|
2237 { |
|
2238 octave_base_stream::arch_type retval = octave_base_stream::at_unknown; |
|
2239 |
|
2240 if (stream_ok ("architecture")) |
|
2241 retval = rep->architecture (); |
|
2242 |
|
2243 return retval; |
|
2244 } |
|
2245 |
|
2246 string |
|
2247 octave_stream::mode_as_string (int mode) |
|
2248 { |
|
2249 string retval = "???"; |
|
2250 |
|
2251 switch (mode) |
|
2252 { |
|
2253 case ios::in: |
|
2254 retval = "r"; |
|
2255 break; |
|
2256 |
|
2257 case ios::out: |
|
2258 case ios::out | ios::trunc: |
|
2259 retval = "w"; |
|
2260 break; |
|
2261 |
|
2262 case ios::out | ios::app: |
|
2263 retval = "a"; |
|
2264 break; |
|
2265 |
|
2266 case ios::in | ios::out: |
|
2267 retval = "r+"; |
|
2268 break; |
|
2269 |
|
2270 case ios::in | ios::out | ios::trunc: |
|
2271 retval = "w+"; |
|
2272 break; |
|
2273 |
|
2274 case ios::in | ios::out | ios::app: |
|
2275 retval = "a+"; |
|
2276 break; |
|
2277 |
|
2278 case ios::in | ios::bin: |
|
2279 retval = "rb"; |
|
2280 break; |
|
2281 |
|
2282 case ios::out | ios::bin: |
|
2283 case ios::out | ios::trunc | ios::bin: |
|
2284 retval = "wb"; |
|
2285 break; |
|
2286 |
|
2287 case ios::out | ios::app | ios::bin: |
|
2288 retval = "ab"; |
|
2289 break; |
|
2290 |
|
2291 case ios::in | ios::out | ios::bin: |
|
2292 retval = "r+b"; |
|
2293 break; |
|
2294 |
|
2295 case ios::in | ios::out | ios::trunc | ios::bin: |
|
2296 retval = "w+b"; |
|
2297 break; |
|
2298 |
|
2299 case ios::in | ios::out | ios::app | ios::bin: |
|
2300 retval = "a+b"; |
|
2301 break; |
|
2302 |
|
2303 default: |
|
2304 break; |
|
2305 } |
|
2306 |
|
2307 return retval; |
|
2308 } |
|
2309 |
|
2310 string |
|
2311 octave_stream::arch_as_string (octave_base_stream::arch_type at) |
|
2312 { |
|
2313 string retval = "unknown"; |
|
2314 |
|
2315 switch (at) |
|
2316 { |
|
2317 case octave_base_stream::at_native: |
|
2318 retval = "native"; |
|
2319 break; |
|
2320 |
|
2321 default: |
|
2322 break; |
|
2323 } |
|
2324 |
|
2325 return retval; |
|
2326 } |
|
2327 |
|
2328 octave_base_stream::data_type |
|
2329 octave_stream::string_to_data_type (const string& s) |
|
2330 { |
|
2331 octave_base_stream::data_type retval = octave_base_stream::dt_unknown; |
|
2332 |
|
2333 // XXX FINISHME XXX |
|
2334 |
|
2335 /* |
|
2336 int16 |
|
2337 integer*2 |
|
2338 int32 |
|
2339 integer*4 */ |
|
2340 |
|
2341 // XXX FIXME XXX -- before checking s, need to strip spaces and downcase. |
|
2342 |
|
2343 if (s == "char" || s == "char*1" || s == "integer*1" || s == "int8") |
|
2344 retval = octave_base_stream::dt_char; |
|
2345 else if (s == "schar" || s == "signedchar") |
|
2346 retval = octave_base_stream::dt_schar; |
|
2347 else if (s == "uchar" || s == "unsignedchar") |
|
2348 retval = octave_base_stream::dt_uchar; |
|
2349 else if (s == "short") |
|
2350 retval = octave_base_stream::dt_short; |
|
2351 else if (s == "ushort" || s == "unsignedshort") |
|
2352 retval = octave_base_stream::dt_ushort; |
|
2353 else if (s == "int") |
|
2354 retval = octave_base_stream::dt_int; |
|
2355 else if (s == "uint" || s == "unsignedint") |
|
2356 retval = octave_base_stream::dt_uint; |
|
2357 else if (s == "long") |
|
2358 retval = octave_base_stream::dt_long; |
|
2359 else if (s == "ulong" || s == "unsignedlong") |
|
2360 retval = octave_base_stream::dt_ulong; |
|
2361 else if (s == "float" || s == "float32" || s == "real*4") |
|
2362 retval = octave_base_stream::dt_float; |
|
2363 else if (s == "double" || s == "float64" || s == "real**") |
|
2364 retval = octave_base_stream::dt_double; |
|
2365 else |
|
2366 ::error ("invalid data type specified"); |
|
2367 |
|
2368 return retval; |
|
2369 } |
|
2370 |
|
2371 octave_base_stream::arch_type |
|
2372 octave_stream::string_to_arch_type (const string& s) |
|
2373 { |
|
2374 octave_base_stream::arch_type retval = octave_base_stream::at_unknown; |
|
2375 |
|
2376 if (s == "native") |
|
2377 retval = octave_base_stream::at_native; |
|
2378 else |
|
2379 ::error ("invalid architecture type specified"); |
|
2380 |
|
2381 return retval; |
|
2382 } |
|
2383 |
|
2384 void |
|
2385 octave_stream::invalid_stream_error (const char *op) const |
|
2386 { |
|
2387 ::error ("%s: attempt to use invalid I/O stream", op); |
|
2388 } |
|
2389 |
|
2390 octave_stream_list *octave_stream_list::instance = 0; |
|
2391 |
|
2392 int |
|
2393 octave_stream_list::do_insert (octave_base_stream *obs) |
|
2394 { |
|
2395 int retval = -1; |
|
2396 |
|
2397 if (obs) |
|
2398 { |
|
2399 octave_stream *os = new octave_stream (obs); |
|
2400 |
|
2401 // Insert item in first open slot, increasing size of list if |
|
2402 // necessary. |
|
2403 |
|
2404 for (int i = 0; i < curr_len; i++) |
|
2405 { |
|
2406 octave_stream *tmp = list.elem (i); |
|
2407 |
|
2408 if (! tmp) |
|
2409 { |
|
2410 list.elem (i) = os; |
|
2411 retval = i; |
|
2412 break; |
|
2413 } |
|
2414 } |
|
2415 |
|
2416 if (retval < 0) |
|
2417 { |
|
2418 int total_len = list.length (); |
|
2419 |
|
2420 if (curr_len == total_len) |
|
2421 list.resize (total_len * 2); |
|
2422 |
|
2423 list.elem (curr_len) = os; |
|
2424 retval = curr_len; |
|
2425 curr_len++; |
|
2426 } |
|
2427 } |
|
2428 else |
|
2429 ::error ("octave_stream_list: attempt to insert invalid stream"); |
|
2430 |
|
2431 return retval; |
|
2432 } |
|
2433 |
|
2434 int |
|
2435 octave_stream_list::insert (octave_base_stream *obs) |
|
2436 { |
|
2437 int retval = -1; |
|
2438 |
|
2439 if (! instance) |
|
2440 instance = new octave_stream_list (); |
|
2441 |
|
2442 if (instance) |
|
2443 retval = instance->do_insert (obs); |
|
2444 else |
|
2445 panic_impossible (); |
|
2446 |
|
2447 return retval; |
|
2448 } |
|
2449 |
|
2450 octave_stream * |
|
2451 octave_stream_list::do_lookup (int fid) const |
|
2452 { |
|
2453 octave_stream *retval = 0; |
|
2454 |
|
2455 if (fid >= 0 && fid < curr_len) |
|
2456 retval = list.elem (fid); |
|
2457 |
|
2458 return retval; |
|
2459 } |
|
2460 |
|
2461 octave_stream * |
|
2462 octave_stream_list::do_lookup (const octave_value& fid) const |
|
2463 { |
|
2464 octave_stream *retval = 0; |
|
2465 |
|
2466 int i = get_file_number (fid); |
|
2467 |
|
2468 if (! error_state) |
|
2469 retval = do_lookup (i); |
|
2470 |
|
2471 return retval; |
|
2472 } |
|
2473 |
|
2474 octave_stream * |
|
2475 octave_stream_list::lookup (int fid) |
|
2476 { |
|
2477 octave_stream *retval = 0; |
|
2478 |
|
2479 if (instance) |
|
2480 retval = instance->do_lookup (fid); |
|
2481 |
|
2482 return retval; |
|
2483 } |
|
2484 |
|
2485 octave_stream * |
|
2486 octave_stream_list::lookup (const octave_value& fid) |
|
2487 { |
|
2488 octave_stream *retval = 0; |
|
2489 |
|
2490 if (instance) |
|
2491 retval = instance->do_lookup (fid); |
|
2492 |
|
2493 return retval; |
|
2494 } |
|
2495 |
|
2496 int |
|
2497 octave_stream_list::do_remove (int fid) |
|
2498 { |
|
2499 int retval = -1; |
|
2500 |
|
2501 // Can't remove stdin (cin), stdout (cout), or stderr (cerr). |
|
2502 |
|
2503 if (fid > 2 && fid < curr_len) |
|
2504 { |
|
2505 octave_stream *os = list.elem (fid); |
|
2506 |
|
2507 if (os) |
|
2508 { |
|
2509 delete os; |
|
2510 list.elem (fid) = 0; |
|
2511 retval = 0; |
|
2512 } |
|
2513 } |
|
2514 |
|
2515 return retval; |
|
2516 } |
|
2517 |
|
2518 int |
|
2519 octave_stream_list::do_remove (const octave_value& fid) |
|
2520 { |
|
2521 int retval = -1; |
|
2522 |
|
2523 int i = get_file_number (fid); |
|
2524 |
|
2525 if (! error_state) |
|
2526 retval = do_remove (i); |
|
2527 |
|
2528 return retval; |
|
2529 } |
|
2530 |
|
2531 int |
|
2532 octave_stream_list::remove (int fid) |
|
2533 { |
|
2534 int retval = -1; |
|
2535 |
|
2536 if (instance) |
|
2537 retval = instance->do_remove (fid); |
|
2538 |
|
2539 return retval; |
|
2540 } |
|
2541 |
|
2542 int |
|
2543 octave_stream_list::remove (const octave_value& fid) |
|
2544 { |
|
2545 int retval = -1; |
|
2546 |
|
2547 if (instance) |
|
2548 retval = instance->do_remove (fid); |
|
2549 |
|
2550 return retval; |
|
2551 } |
|
2552 |
|
2553 void |
|
2554 octave_stream_list::do_clear (void) |
|
2555 { |
|
2556 // Do flush stdout and stderr. |
|
2557 |
|
2558 list.elem (0) -> flush (); |
|
2559 list.elem (1) -> flush (); |
|
2560 |
|
2561 // But don't delete them or stdin. |
|
2562 |
|
2563 for (int i = 3; i < curr_len; i++) |
|
2564 { |
|
2565 octave_stream *os = list.elem (i); |
|
2566 |
|
2567 delete os; |
|
2568 |
|
2569 list.elem (i) = 0; |
|
2570 } |
|
2571 } |
|
2572 |
|
2573 void |
|
2574 octave_stream_list::clear (void) |
|
2575 { |
|
2576 if (instance) |
|
2577 instance->do_clear (); |
|
2578 } |
|
2579 |
|
2580 string_vector |
|
2581 octave_stream_list::do_get_info (int fid) const |
|
2582 { |
|
2583 string_vector retval; |
|
2584 |
|
2585 octave_stream *os = do_lookup (fid); |
|
2586 |
|
2587 if (os) |
|
2588 { |
|
2589 retval.resize (3); |
|
2590 |
|
2591 retval(0) = os->name (); |
|
2592 retval(1) = octave_stream::mode_as_string (os->mode ()); |
|
2593 retval(2) = octave_stream::arch_as_string (os->architecture ()); |
|
2594 } |
|
2595 else |
|
2596 ::error ("invalid file id"); |
|
2597 |
|
2598 return retval; |
|
2599 } |
|
2600 |
|
2601 string_vector |
|
2602 octave_stream_list::do_get_info (const octave_value& fid) const |
|
2603 { |
|
2604 string_vector retval; |
|
2605 |
|
2606 int conv_err = 0; |
|
2607 |
|
2608 int int_fid = convert_to_valid_int (fid, conv_err); |
|
2609 |
|
2610 if (! conv_err) |
|
2611 retval = do_get_info (int_fid); |
|
2612 else |
|
2613 ::error ("file id must be valid integer"); |
|
2614 |
|
2615 return retval; |
|
2616 } |
|
2617 |
|
2618 string_vector |
|
2619 octave_stream_list::get_info (int fid) |
|
2620 { |
|
2621 string_vector retval; |
|
2622 |
|
2623 if (instance) |
|
2624 retval = instance->do_get_info (fid); |
|
2625 |
|
2626 return retval; |
|
2627 } |
|
2628 |
|
2629 string_vector |
|
2630 octave_stream_list::get_info (const octave_value& fid) |
|
2631 { |
|
2632 string_vector retval; |
|
2633 |
|
2634 if (instance) |
|
2635 retval = instance->do_get_info (fid); |
|
2636 |
|
2637 return retval; |
|
2638 } |
|
2639 |
|
2640 string |
|
2641 octave_stream_list::do_list_open_files (void) const |
|
2642 { |
|
2643 string retval; |
|
2644 |
|
2645 // XXX FIXME XXX -- this should probably be converted to use sstream |
|
2646 // when that is available. |
|
2647 ostrstream buf; |
|
2648 |
|
2649 buf << "\n" |
|
2650 << " number mode arch name\n" |
|
2651 << " ------ ---- ---- ----\n"; |
|
2652 |
|
2653 for (int i = 0; i < curr_len; i++) |
|
2654 { |
|
2655 octave_stream *os = list.elem (i); |
|
2656 |
|
2657 if (os) |
|
2658 { |
|
2659 string mode = octave_stream::mode_as_string (os->mode ()); |
|
2660 string arch = octave_stream::arch_as_string (os->architecture ()); |
|
2661 string name = os->name (); |
|
2662 |
|
2663 buf.form (" %4d %-3s %-9s %s\n", |
|
2664 i, mode.c_str (), arch.c_str (), name.c_str ()); |
|
2665 } |
|
2666 } |
|
2667 |
|
2668 buf << "\n" << ends; |
|
2669 |
|
2670 char *tmp = buf.str (); |
|
2671 |
|
2672 retval = tmp; |
|
2673 |
|
2674 delete [] tmp; |
|
2675 |
|
2676 return retval; |
|
2677 } |
|
2678 |
|
2679 string |
|
2680 octave_stream_list::list_open_files (void) |
|
2681 { |
|
2682 string retval; |
|
2683 |
|
2684 if (instance) |
|
2685 retval = instance->do_list_open_files (); |
|
2686 |
|
2687 return retval; |
|
2688 } |
|
2689 |
|
2690 octave_value |
|
2691 octave_stream_list::do_open_file_numbers (void) const |
|
2692 { |
|
2693 Matrix retval (1, curr_len, 0.0); |
|
2694 |
|
2695 int num_open = 0; |
|
2696 |
|
2697 // Skip stdin, stdout, and stderr. |
|
2698 |
|
2699 for (int i = 3; i < curr_len; i++) |
|
2700 { |
|
2701 if (list.elem (i)) |
|
2702 retval (0, num_open++) = i; |
|
2703 } |
|
2704 |
|
2705 retval.resize ((num_open > 0), num_open); |
|
2706 |
|
2707 return retval; |
|
2708 } |
|
2709 |
|
2710 octave_value |
|
2711 octave_stream_list::open_file_numbers (void) |
|
2712 { |
|
2713 octave_value retval; |
|
2714 |
|
2715 if (instance) |
|
2716 retval = instance->do_open_file_numbers (); |
|
2717 |
|
2718 return retval; |
|
2719 } |
|
2720 |
|
2721 int |
|
2722 octave_stream_list::get_file_number (const octave_value& fid) const |
|
2723 { |
|
2724 int retval = -1; |
|
2725 |
|
2726 if (fid.is_string ()) |
|
2727 { |
|
2728 string nm = fid.string_value (); |
|
2729 |
|
2730 // stdin (cin), stdout (cout), and stderr (cerr) are unnamed. |
|
2731 |
|
2732 for (int i = 3; i < curr_len; i++) |
|
2733 { |
|
2734 octave_stream *os = list.elem (i); |
|
2735 |
|
2736 if (os && os->name () == nm) |
|
2737 { |
|
2738 retval = i; |
|
2739 break; |
|
2740 } |
|
2741 } |
|
2742 } |
|
2743 else |
|
2744 { |
|
2745 int conv_err = 0; |
|
2746 |
|
2747 int int_fid = convert_to_valid_int (fid, conv_err); |
|
2748 |
|
2749 if (conv_err) |
|
2750 ::error ("file id must be a string or integer value"); |
|
2751 else |
|
2752 retval = int_fid; |
|
2753 } |
|
2754 |
|
2755 return retval; |
|
2756 } |
|
2757 |
|
2758 /* |
|
2759 ;;; Local Variables: *** |
|
2760 ;;; mode: C++ *** |
|
2761 ;;; End: *** |
|
2762 */ |