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