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