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