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