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