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