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