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