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