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