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