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