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