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