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