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