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