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