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