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