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