Mercurial > hg > octave-lyh
annotate src/oct-stream.cc @ 7736:a059b5679fbb
implement dbstack
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 25 Apr 2008 15:11:03 -0400 |
parents | 932b0cf51834 |
children | 82be108cc558 |
rev | line source |
---|---|
2117 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
4 2005, 2006, 2007 John W. Eaton | |
2117 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2117 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2117 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3268 | 28 #include <cassert> |
7709
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
29 #include <cctype> |
2215 | 30 #include <cstring> |
31 | |
3503 | 32 #include <iomanip> |
3559 | 33 #include <fstream> |
5765 | 34 #include <sstream> |
3535 | 35 #include <string> |
2117 | 36 |
4944 | 37 #include <Array.h> |
38 #include <Array2.h> | |
39 #include <Array3.h> | |
40 | |
41 #include <Array.cc> | |
42 | |
43 #include "byte-swap.h" | |
2117 | 44 #include "lo-ieee.h" |
45 #include "lo-mappers.h" | |
46 #include "lo-utils.h" | |
47 #include "str-vec.h" | |
4153 | 48 #include "quit.h" |
2117 | 49 |
50 #include "error.h" | |
7352 | 51 #include "gripes.h" |
3342 | 52 #include "input.h" |
3775 | 53 #include "oct-stdstrm.h" |
2117 | 54 #include "oct-stream.h" |
2877 | 55 #include "oct-obj.h" |
2117 | 56 #include "utils.h" |
57 | |
58 // Possible values for conv_err: | |
59 // | |
60 // 1 : not a real scalar | |
2902 | 61 // 2 : value is NaN |
62 // 3 : value is not an integer | |
2117 | 63 |
64 static int | |
65 convert_to_valid_int (const octave_value& tc, int& conv_err) | |
66 { | |
67 int retval = 0; | |
68 | |
69 conv_err = 0; | |
70 | |
2902 | 71 double dval = tc.double_value (); |
72 | |
73 if (! error_state) | |
2117 | 74 { |
5389 | 75 if (! lo_ieee_isnan (dval)) |
2117 | 76 { |
2902 | 77 int ival = NINT (dval); |
78 | |
79 if (ival == dval) | |
80 retval = ival; | |
2117 | 81 else |
82 conv_err = 3; | |
83 } | |
84 else | |
85 conv_err = 2; | |
86 } | |
87 else | |
88 conv_err = 1; | |
89 | |
90 return retval; | |
91 } | |
92 | |
93 static int | |
4468 | 94 get_size (double d, const std::string& who) |
2117 | 95 { |
96 int retval = -1; | |
97 | |
5389 | 98 if (! lo_ieee_isnan (d)) |
2117 | 99 { |
100 if (! xisinf (d)) | |
101 { | |
3268 | 102 if (d >= 0.0) |
2117 | 103 retval = NINT (d); |
104 else | |
105 ::error ("%s: negative value invalid as size specification", | |
4468 | 106 who.c_str ()); |
2117 | 107 } |
108 else | |
109 retval = -1; | |
110 } | |
111 else | |
4468 | 112 ::error ("%s: NaN is invalid as size specification", who.c_str ()); |
2117 | 113 |
114 return retval; | |
115 } | |
116 | |
117 static void | |
5275 | 118 get_size (const Array<double>& size, octave_idx_type& nr, octave_idx_type& nc, bool& one_elt_size_spec, |
4468 | 119 const std::string& who) |
2117 | 120 { |
121 nr = -1; | |
122 nc = -1; | |
123 | |
3268 | 124 one_elt_size_spec = false; |
125 | |
2117 | 126 double dnr = -1.0; |
127 double dnc = -1.0; | |
128 | |
5275 | 129 octave_idx_type sz_len = size.length (); |
3810 | 130 |
131 if (sz_len == 1) | |
2601 | 132 { |
3268 | 133 one_elt_size_spec = true; |
134 | |
3810 | 135 dnr = size (0); |
4293 | 136 |
137 dnc = (dnr == 0.0) ? 0.0 : 1.0; | |
2601 | 138 } |
3810 | 139 else if (sz_len == 2) |
2117 | 140 { |
3810 | 141 dnr = size (0); |
142 | |
143 if (! xisinf (dnr)) | |
144 dnc = size (1); | |
145 else | |
4468 | 146 ::error ("%s: invalid size specification", who.c_str ()); |
2117 | 147 } |
148 else | |
4468 | 149 ::error ("%s: invalid size specification", who.c_str ()); |
2117 | 150 |
151 if (! error_state) | |
152 { | |
4468 | 153 nr = get_size (dnr, who); |
2117 | 154 |
3268 | 155 if (! error_state && dnc >= 0.0) |
4468 | 156 nc = get_size (dnc, who); |
2117 | 157 } |
158 } | |
159 | |
3523 | 160 scanf_format_list::scanf_format_list (const std::string& s) |
2117 | 161 : nconv (0), curr_idx (0), list (16), buf (0) |
162 { | |
163 int num_elts = 0; | |
164 | |
165 int n = s.length (); | |
166 | |
167 int i = 0; | |
168 | |
2215 | 169 int width = 0; |
2117 | 170 bool discard = false; |
171 char modifier = '\0'; | |
172 char type = '\0'; | |
173 | |
174 bool have_more = true; | |
175 | |
176 while (i < n) | |
177 { | |
178 have_more = true; | |
179 | |
180 if (! buf) | |
5765 | 181 buf = new std::ostringstream (); |
2117 | 182 |
183 if (s[i] == '%') | |
184 { | |
3483 | 185 // Process percent-escape conversion type. |
186 | |
2215 | 187 process_conversion (s, i, n, width, discard, type, modifier, |
188 num_elts); | |
2117 | 189 have_more = (buf != 0); |
190 } | |
3483 | 191 else if (isspace (s[i])) |
2117 | 192 { |
3483 | 193 type = scanf_format_elt::whitespace_conversion; |
194 | |
2215 | 195 width = 0; |
2117 | 196 discard = false; |
197 modifier = '\0'; | |
3483 | 198 *buf << " "; |
199 | |
200 while (++i < n && isspace (s[i])) | |
201 /* skip whitespace */; | |
202 | |
203 add_elt_to_list (width, discard, type, modifier, num_elts); | |
204 | |
205 have_more = false; | |
206 } | |
207 else | |
208 { | |
209 type = scanf_format_elt::literal_conversion; | |
210 | |
211 width = 0; | |
212 discard = false; | |
213 modifier = '\0'; | |
214 | |
215 while (i < n && ! isspace (s[i]) && s[i] != '%') | |
216 *buf << s[i++]; | |
217 | |
218 add_elt_to_list (width, discard, type, modifier, num_elts); | |
219 | |
220 have_more = false; | |
2117 | 221 } |
222 | |
223 if (nconv < 0) | |
224 { | |
225 have_more = false; | |
226 break; | |
227 } | |
228 } | |
229 | |
230 if (have_more) | |
2215 | 231 add_elt_to_list (width, discard, type, modifier, num_elts); |
2117 | 232 |
233 list.resize (num_elts); | |
234 | |
235 delete buf; | |
236 } | |
237 | |
238 scanf_format_list::~scanf_format_list (void) | |
239 { | |
5275 | 240 octave_idx_type n = list.length (); |
241 | |
242 for (octave_idx_type i = 0; i < n; i++) | |
2117 | 243 { |
3340 | 244 scanf_format_elt *elt = list(i); |
2117 | 245 delete elt; |
246 } | |
247 } | |
248 | |
249 void | |
2215 | 250 scanf_format_list::add_elt_to_list (int width, bool discard, char type, |
3483 | 251 char modifier, int& num_elts, |
3523 | 252 const std::string& char_class) |
2117 | 253 { |
254 if (buf) | |
255 { | |
5765 | 256 std::string text = buf->str (); |
4051 | 257 |
258 if (! text.empty ()) | |
2117 | 259 { |
4051 | 260 scanf_format_elt *elt |
261 = new scanf_format_elt (text.c_str (), width, discard, type, | |
262 modifier, char_class); | |
263 | |
264 if (num_elts == list.length ()) | |
265 list.resize (2 * num_elts); | |
266 | |
267 list(num_elts++) = elt; | |
2117 | 268 } |
269 | |
270 delete buf; | |
271 buf = 0; | |
272 } | |
273 } | |
274 | |
3535 | 275 static std::string |
3523 | 276 expand_char_class (const std::string& s) |
3483 | 277 { |
3523 | 278 std::string retval; |
3483 | 279 |
280 size_t len = s.length (); | |
281 | |
282 size_t i = 0; | |
283 | |
284 while (i < len) | |
285 { | |
286 unsigned char c = s[i++]; | |
287 | |
288 if (c == '-' && i > 1 && i < len | |
5760 | 289 && static_cast<unsigned char> (s[i-2]) <= static_cast<unsigned char> (s[i])) |
3483 | 290 { |
291 // Add all characters from the range except the first (we | |
292 // already added it below). | |
293 | |
294 for (c = s[i-2]+1; c < s[i]; c++) | |
295 retval += c; | |
296 } | |
297 else | |
298 { | |
299 // Add the character to the class. Only add '-' if it is | |
300 // the last character in the class. | |
301 | |
302 if (c != '-' || i == len) | |
303 retval += c; | |
304 } | |
305 } | |
306 | |
307 return retval; | |
308 } | |
309 | |
2117 | 310 void |
3523 | 311 scanf_format_list::process_conversion (const std::string& s, int& i, int n, |
2215 | 312 int& width, bool& discard, char& type, |
2117 | 313 char& modifier, int& num_elts) |
314 { | |
2215 | 315 width = 0; |
2117 | 316 discard = false; |
317 modifier = '\0'; | |
318 type = '\0'; | |
319 | |
320 *buf << s[i++]; | |
321 | |
322 bool have_width = false; | |
323 | |
324 while (i < n) | |
325 { | |
326 switch (s[i]) | |
327 { | |
328 case '*': | |
329 if (discard) | |
330 nconv = -1; | |
331 else | |
332 { | |
333 discard = true; | |
334 *buf << s[i++]; | |
335 } | |
336 break; | |
337 | |
338 case '0': case '1': case '2': case '3': case '4': | |
339 case '5': case '6': case '7': case '8': case '9': | |
340 if (have_width) | |
341 nconv = -1; | |
342 else | |
343 { | |
2215 | 344 char c = s[i++]; |
345 width = width * 10 + c - '0'; | |
2117 | 346 have_width = true; |
2215 | 347 *buf << c; |
2117 | 348 while (i < n && isdigit (s[i])) |
2215 | 349 { |
350 c = s[i++]; | |
351 width = width * 10 + c - '0'; | |
352 *buf << c; | |
353 } | |
2117 | 354 } |
355 break; | |
356 | |
357 case 'h': case 'l': case 'L': | |
358 if (modifier != '\0') | |
359 nconv = -1; | |
360 else | |
2663 | 361 modifier = s[i++]; |
2117 | 362 break; |
363 | |
364 case 'd': case 'i': case 'o': case 'u': case 'x': | |
365 if (modifier == 'L') | |
366 { | |
367 nconv = -1; | |
368 break; | |
369 } | |
370 goto fini; | |
371 | |
372 case 'e': case 'f': case 'g': | |
373 if (modifier == 'h') | |
374 { | |
375 nconv = -1; | |
376 break; | |
377 } | |
2663 | 378 |
379 // No float or long double conversions, thanks. | |
380 *buf << 'l'; | |
381 | |
2117 | 382 goto fini; |
383 | |
384 case 'c': case 's': case 'p': case '%': case '[': | |
385 if (modifier != '\0') | |
386 { | |
387 nconv = -1; | |
388 break; | |
389 } | |
390 goto fini; | |
391 | |
392 fini: | |
393 { | |
2215 | 394 if (finish_conversion (s, i, n, width, discard, type, |
2117 | 395 modifier, num_elts) == 0) |
396 return; | |
397 } | |
398 break; | |
399 | |
400 default: | |
401 nconv = -1; | |
402 break; | |
403 } | |
404 | |
405 if (nconv < 0) | |
406 break; | |
407 } | |
408 | |
409 nconv = -1; | |
410 } | |
411 | |
412 int | |
3523 | 413 scanf_format_list::finish_conversion (const std::string& s, int& i, int n, |
2215 | 414 int& width, bool discard, char& type, |
2117 | 415 char modifier, int& num_elts) |
416 { | |
417 int retval = 0; | |
418 | |
3523 | 419 std::string char_class; |
3483 | 420 |
3640 | 421 int beg_idx = -1; |
422 int end_idx = -1; | |
423 | |
2117 | 424 if (s[i] == '%') |
3640 | 425 { |
426 type = '%'; | |
427 *buf << s[i++]; | |
428 } | |
2117 | 429 else |
430 { | |
431 type = s[i]; | |
432 | |
433 if (s[i] == '[') | |
434 { | |
435 *buf << s[i++]; | |
436 | |
437 if (i < n) | |
438 { | |
3483 | 439 beg_idx = i; |
440 | |
2117 | 441 if (s[i] == '^') |
442 { | |
443 type = '^'; | |
444 *buf << s[i++]; | |
3483 | 445 |
446 if (i < n) | |
447 { | |
448 beg_idx = i; | |
449 | |
450 if (s[i] == ']') | |
451 *buf << s[i++]; | |
452 } | |
2117 | 453 } |
454 else if (s[i] == ']') | |
455 *buf << s[i++]; | |
456 } | |
457 | |
458 while (i < n && s[i] != ']') | |
459 *buf << s[i++]; | |
460 | |
461 if (i < n && s[i] == ']') | |
3483 | 462 { |
463 end_idx = i-1; | |
464 *buf << s[i++]; | |
465 } | |
2117 | 466 |
467 if (s[i-1] != ']') | |
468 retval = nconv = -1; | |
469 } | |
470 else | |
2215 | 471 *buf << s[i++]; |
3640 | 472 } |
473 | |
474 nconv++; | |
475 | |
476 if (nconv > 0) | |
477 { | |
478 if (beg_idx >= 0 && end_idx >= 0) | |
479 char_class = expand_char_class (s.substr (beg_idx, | |
480 end_idx - beg_idx + 1)); | |
481 | |
482 add_elt_to_list (width, discard, type, modifier, num_elts, char_class); | |
2117 | 483 } |
484 | |
485 return retval; | |
486 } | |
487 | |
488 void | |
489 scanf_format_list::printme (void) const | |
490 { | |
491 int n = list.length (); | |
492 | |
493 for (int i = 0; i < n; i++) | |
494 { | |
3340 | 495 scanf_format_elt *elt = list(i); |
2117 | 496 |
3531 | 497 std::cerr |
498 << "width: " << elt->width << "\n" | |
499 << "discard: " << elt->discard << "\n" | |
500 << "type: "; | |
3483 | 501 |
502 if (elt->type == scanf_format_elt::literal_conversion) | |
3531 | 503 std::cerr << "literal text\n"; |
3483 | 504 else if (elt->type == scanf_format_elt::whitespace_conversion) |
3531 | 505 std::cerr << "whitespace\n"; |
3483 | 506 else |
3531 | 507 std::cerr << elt->type << "\n"; |
508 | |
509 std::cerr | |
510 << "modifier: " << elt->modifier << "\n" | |
511 << "char_class: `" << undo_string_escapes (elt->char_class) << "'\n" | |
512 << "text: `" << undo_string_escapes (elt->text) << "'\n\n"; | |
2117 | 513 } |
514 } | |
515 | |
516 bool | |
517 scanf_format_list::all_character_conversions (void) | |
518 { | |
519 int n = list.length (); | |
520 | |
521 if (n > 0) | |
522 { | |
523 for (int i = 0; i < n; i++) | |
524 { | |
3340 | 525 scanf_format_elt *elt = list(i); |
2117 | 526 |
527 switch (elt->type) | |
528 { | |
3483 | 529 case 'c': case 's': case '%': case '[': case '^': |
530 case scanf_format_elt::literal_conversion: | |
531 case scanf_format_elt::whitespace_conversion: | |
2117 | 532 break; |
533 | |
534 default: | |
535 return false; | |
536 break; | |
537 } | |
538 } | |
539 | |
540 return true; | |
541 } | |
542 else | |
543 return false; | |
544 } | |
545 | |
546 bool | |
547 scanf_format_list::all_numeric_conversions (void) | |
548 { | |
549 int n = list.length (); | |
550 | |
551 if (n > 0) | |
552 { | |
553 for (int i = 0; i < n; i++) | |
554 { | |
3340 | 555 scanf_format_elt *elt = list(i); |
2117 | 556 |
557 switch (elt->type) | |
558 { | |
559 case 'd': case 'i': case 'o': case 'u': case 'x': | |
560 case 'e': case 'f': case 'g': | |
561 break; | |
562 | |
563 default: | |
564 return false; | |
565 break; | |
566 } | |
567 } | |
568 | |
569 return true; | |
570 } | |
571 else | |
572 return false; | |
573 } | |
574 | |
575 // Ugh again. | |
576 | |
3523 | 577 printf_format_list::printf_format_list (const std::string& s) |
2117 | 578 : nconv (0), curr_idx (0), list (16), buf (0) |
579 { | |
580 int num_elts = 0; | |
581 | |
582 int n = s.length (); | |
583 | |
584 int i = 0; | |
585 | |
586 int args = 0; | |
3643 | 587 std::string flags; |
3640 | 588 int fw = 0; |
589 int prec = 0; | |
2117 | 590 char modifier = '\0'; |
591 char type = '\0'; | |
592 | |
593 bool have_more = true; | |
3640 | 594 bool empty_buf = true; |
2117 | 595 |
4223 | 596 if (n == 0) |
2117 | 597 { |
4223 | 598 printf_format_elt *elt |
599 = new printf_format_elt ("", args, fw, prec, flags, type, modifier); | |
600 | |
601 list(num_elts++) = elt; | |
602 | |
603 list.resize (num_elts); | |
604 } | |
605 else | |
606 { | |
607 while (i < n) | |
3640 | 608 { |
4223 | 609 have_more = true; |
610 | |
611 if (! buf) | |
612 { | |
5765 | 613 buf = new std::ostringstream (); |
4223 | 614 empty_buf = true; |
615 } | |
616 | |
617 switch (s[i]) | |
618 { | |
619 case '%': | |
3640 | 620 { |
4223 | 621 if (empty_buf) |
622 { | |
623 process_conversion (s, i, n, args, flags, fw, prec, | |
624 type, modifier, num_elts); | |
625 | |
626 have_more = (buf != 0); | |
627 } | |
628 else | |
629 add_elt_to_list (args, flags, fw, prec, type, modifier, | |
630 num_elts); | |
3640 | 631 } |
4223 | 632 break; |
633 | |
634 default: | |
635 { | |
636 args = 0; | |
637 flags = ""; | |
638 fw = 0; | |
639 prec = 0; | |
640 modifier = '\0'; | |
641 type = '\0'; | |
642 *buf << s[i++]; | |
643 empty_buf = false; | |
644 } | |
645 break; | |
646 } | |
647 | |
648 if (nconv < 0) | |
649 { | |
650 have_more = false; | |
651 break; | |
652 } | |
2117 | 653 } |
654 | |
4223 | 655 if (have_more) |
656 add_elt_to_list (args, flags, fw, prec, type, modifier, num_elts); | |
657 | |
658 list.resize (num_elts); | |
659 | |
660 delete buf; | |
2117 | 661 } |
662 } | |
663 | |
664 printf_format_list::~printf_format_list (void) | |
665 { | |
666 int n = list.length (); | |
667 | |
668 for (int i = 0; i < n; i++) | |
669 { | |
3340 | 670 printf_format_elt *elt = list(i); |
2117 | 671 delete elt; |
672 } | |
673 } | |
674 | |
675 void | |
3640 | 676 printf_format_list::add_elt_to_list (int args, const std::string& flags, |
677 int fw, int prec, char type, | |
678 char modifier, int& num_elts) | |
2117 | 679 { |
680 if (buf) | |
681 { | |
5765 | 682 std::string text = buf->str (); |
4051 | 683 |
684 if (! text.empty ()) | |
2117 | 685 { |
4051 | 686 printf_format_elt *elt |
687 = new printf_format_elt (text.c_str (), args, fw, prec, flags, | |
688 type, modifier); | |
689 | |
690 if (num_elts == list.length ()) | |
691 list.resize (2 * num_elts); | |
692 | |
693 list(num_elts++) = elt; | |
2117 | 694 } |
695 | |
696 delete buf; | |
697 buf = 0; | |
698 } | |
699 } | |
700 | |
701 void | |
3640 | 702 printf_format_list::process_conversion |
703 (const std::string& s, int& i, int n, int& args, std::string& flags, | |
704 int& fw, int& prec, char& modifier, char& type, int& num_elts) | |
2117 | 705 { |
706 args = 0; | |
3640 | 707 flags = ""; |
708 fw = 0; | |
709 prec = 0; | |
2117 | 710 modifier = '\0'; |
711 type = '\0'; | |
712 | |
713 *buf << s[i++]; | |
714 | |
4587 | 715 bool nxt = false; |
2117 | 716 |
717 while (i < n) | |
718 { | |
719 switch (s[i]) | |
720 { | |
721 case '-': case '+': case ' ': case '0': case '#': | |
3640 | 722 flags += s[i]; |
2117 | 723 *buf << s[i++]; |
724 break; | |
725 | |
726 default: | |
4587 | 727 nxt = true; |
2117 | 728 break; |
729 } | |
730 | |
4587 | 731 if (nxt) |
2117 | 732 break; |
733 } | |
734 | |
735 if (i < n) | |
736 { | |
737 if (s[i] == '*') | |
738 { | |
3640 | 739 fw = -1; |
2117 | 740 args++; |
741 *buf << s[i++]; | |
742 } | |
743 else | |
744 { | |
3640 | 745 if (isdigit (s[i])) |
746 { | |
4587 | 747 int nn = 0; |
3643 | 748 std::string tmp = s.substr (i); |
4587 | 749 sscanf (tmp.c_str (), "%d%n", &fw, &nn); |
3640 | 750 } |
751 | |
2117 | 752 while (i < n && isdigit (s[i])) |
753 *buf << s[i++]; | |
754 } | |
755 } | |
756 | |
757 if (i < n && s[i] == '.') | |
758 { | |
759 *buf << s[i++]; | |
760 | |
761 if (i < n) | |
762 { | |
763 if (s[i] == '*') | |
764 { | |
3640 | 765 prec = -1; |
2117 | 766 args++; |
767 *buf << s[i++]; | |
768 } | |
769 else | |
770 { | |
3640 | 771 if (isdigit (s[i])) |
772 { | |
4587 | 773 int nn = 0; |
3643 | 774 std::string tmp = s.substr (i); |
4587 | 775 sscanf (tmp.c_str (), "%d%n", &prec, &nn); |
3640 | 776 } |
777 | |
2117 | 778 while (i < n && isdigit (s[i])) |
779 *buf << s[i++]; | |
780 } | |
781 } | |
782 } | |
783 | |
784 if (i < n) | |
785 { | |
786 switch (s[i]) | |
787 { | |
788 case 'h': case 'l': case 'L': | |
789 modifier = s[i]; | |
790 *buf << s[i++]; | |
791 break; | |
792 | |
793 default: | |
794 break; | |
795 } | |
796 } | |
797 | |
798 if (i < n) | |
3640 | 799 finish_conversion (s, i, args, flags, fw, prec, modifier, type, num_elts); |
2117 | 800 else |
801 nconv = -1; | |
802 } | |
803 | |
804 void | |
3640 | 805 printf_format_list::finish_conversion |
806 (const std::string& s, int& i, int args, const std::string& flags, | |
807 int fw, int prec, char modifier, char& type, int& num_elts) | |
2117 | 808 |
809 { | |
810 switch (s[i]) | |
811 { | |
812 case 'd': case 'i': case 'o': case 'x': case 'X': | |
813 case 'u': case 'c': | |
814 if (modifier == 'L') | |
815 { | |
816 nconv = -1; | |
817 break; | |
818 } | |
819 goto fini; | |
820 | |
821 case 'f': case 'e': case 'E': case 'g': case 'G': | |
822 if (modifier == 'h' || modifier == 'l') | |
823 { | |
824 nconv = -1; | |
825 break; | |
826 } | |
827 goto fini; | |
828 | |
829 case 's': case 'p': case '%': | |
830 if (modifier != '\0') | |
831 { | |
832 nconv = -1; | |
833 break; | |
834 } | |
835 goto fini; | |
836 | |
837 fini: | |
838 | |
3640 | 839 type = s[i]; |
840 | |
841 *buf << s[i++]; | |
842 | |
843 if (type != '%' || args != 0) | |
844 nconv++; | |
845 | |
846 if (type != '%') | |
847 args++; | |
848 | |
849 add_elt_to_list (args, flags, fw, prec, type, modifier, num_elts); | |
850 | |
2117 | 851 break; |
852 | |
853 default: | |
854 nconv = -1; | |
855 break; | |
856 } | |
857 } | |
858 | |
859 void | |
860 printf_format_list::printme (void) const | |
861 { | |
862 int n = list.length (); | |
863 | |
864 for (int i = 0; i < n; i++) | |
865 { | |
3340 | 866 printf_format_elt *elt = list(i); |
2117 | 867 |
3640 | 868 std::cerr |
869 << "args: " << elt->args << "\n" | |
870 << "flags: `" << elt->flags << "'\n" | |
871 << "width: " << elt->fw << "\n" | |
872 << "prec: " << elt->prec << "\n" | |
873 << "type: `" << elt->type << "'\n" | |
874 << "modifier: `" << elt->modifier << "'\n" | |
875 << "text: `" << undo_string_escapes (elt->text) << "'\n\n"; | |
2117 | 876 } |
877 } | |
878 | |
3145 | 879 int |
3148 | 880 octave_base_stream::file_number (void) |
3145 | 881 { |
882 // Kluge alert! | |
883 | |
884 if (name () == "stdin") | |
885 return 0; | |
886 | |
887 if (name () == "stdout") | |
888 return 1; | |
889 | |
890 if (name () == "stderr") | |
891 return 2; | |
892 | |
893 int retval = -1; | |
894 | |
3523 | 895 std::istream *is = input_stream (); |
896 std::ostream *os = output_stream (); | |
3145 | 897 |
3775 | 898 // There is no standard way to get the underlying file descriptor from |
899 // std::filebuf (nor in the GNU libstdc++-v3 implementation). We cache | |
900 // the descriptor in c_file_ptr_buf, and then extract it here. | |
901 | |
6757 | 902 c_file_ptr_buf *ibuf |
903 = is ? dynamic_cast<c_file_ptr_buf *> (is->rdbuf ()) : 0; | |
904 | |
905 c_file_ptr_buf *obuf | |
906 = os ? dynamic_cast<c_file_ptr_buf *> (os->rdbuf ()) : 0; | |
3775 | 907 |
908 int i_fid = ibuf ? ibuf->file_number () : -1; | |
909 int o_fid = obuf ? obuf->file_number () : -1; | |
3145 | 910 |
911 if (i_fid >= 0) | |
912 { | |
913 if (o_fid >= 0) | |
914 retval = (i_fid == o_fid) ? i_fid : -1; | |
915 else | |
916 retval = i_fid; | |
917 } | |
918 else if (o_fid >= 0) | |
919 retval = o_fid; | |
920 | |
921 return retval; | |
922 } | |
923 | |
2117 | 924 void |
3523 | 925 octave_base_stream::error (const std::string& msg) |
2117 | 926 { |
927 fail = true; | |
928 errmsg = msg; | |
929 } | |
930 | |
931 void | |
4468 | 932 octave_base_stream::error (const std::string& who, const std::string& msg) |
933 { | |
934 fail = true; | |
6296 | 935 errmsg = who + ": " + msg; |
4468 | 936 } |
937 | |
938 void | |
2117 | 939 octave_base_stream::clear (void) |
940 { | |
4889 | 941 fail = false; |
942 errmsg = ""; | |
943 } | |
944 | |
945 void | |
946 octave_base_stream::clearerr (void) | |
947 { | |
4888 | 948 std::istream *is = input_stream (); |
949 std::ostream *os = output_stream (); | |
950 | |
951 if (is) | |
952 is->clear (); | |
953 | |
954 if (os) | |
955 os->clear (); | |
2117 | 956 } |
957 | |
958 // Functions that are defined for all input streams (input streams | |
959 // are those that define is). | |
960 | |
3536 | 961 std::string |
5275 | 962 octave_base_stream::do_gets (octave_idx_type max_len, bool& err, |
4468 | 963 bool strip_newline, const std::string& who) |
2117 | 964 { |
3523 | 965 std::string retval; |
2117 | 966 |
967 err = false; | |
968 | |
3523 | 969 std::istream *isp = input_stream (); |
2117 | 970 |
971 if (isp) | |
972 { | |
3523 | 973 std::istream& is = *isp; |
2117 | 974 |
5765 | 975 std::ostringstream buf; |
2117 | 976 |
977 int c = 0; | |
3553 | 978 int char_count = 0; |
6345 | 979 |
980 if (max_len != 0) | |
2117 | 981 { |
6345 | 982 while (is && (c = is.get ()) != EOF) |
2117 | 983 { |
6345 | 984 char_count++; |
985 | |
986 if (c == '\n') | |
987 { | |
988 if (! strip_newline) | |
989 buf << static_cast<char> (c); | |
990 | |
991 break; | |
992 } | |
993 else | |
5760 | 994 buf << static_cast<char> (c); |
6345 | 995 |
996 if (max_len > 0 && char_count == max_len) | |
997 break; | |
2117 | 998 } |
6345 | 999 } |
1000 | |
1001 if (! is.eof () && char_count > 0) | |
1002 { | |
1003 // GAGME. Matlab seems to check for EOF even if the last | |
1004 // character in a file is a newline character. This is NOT | |
1005 // what the corresponding C-library functions do. | |
1006 int disgusting_compatibility_hack = is.get (); | |
1007 if (! is.eof ()) | |
1008 is.putback (disgusting_compatibility_hack); | |
2117 | 1009 } |
1010 | |
4224 | 1011 if (is.good () || (is.eof () && char_count > 0)) |
5765 | 1012 retval = buf.str (); |
4224 | 1013 else |
1014 { | |
1015 err = true; | |
4468 | 1016 |
4224 | 1017 if (is.eof () && char_count == 0) |
4468 | 1018 error (who, "at end of file"); |
4224 | 1019 else |
4468 | 1020 error (who, "read error"); |
4224 | 1021 } |
2117 | 1022 } |
1023 else | |
1024 { | |
1025 err = true; | |
4468 | 1026 invalid_operation (who, "reading"); |
2117 | 1027 } |
1028 | |
1029 return retval; | |
1030 } | |
1031 | |
3536 | 1032 std::string |
5275 | 1033 octave_base_stream::getl (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 1034 { |
4468 | 1035 return do_gets (max_len, err, true, who); |
2117 | 1036 } |
1037 | |
3536 | 1038 std::string |
5275 | 1039 octave_base_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 1040 { |
4468 | 1041 return do_gets (max_len, err, false, who); |
2117 | 1042 } |
1043 | |
3640 | 1044 #define OCTAVE_SCAN(is, fmt, arg) octave_scan (is, fmt, arg) |
3636 | 1045 |
1046 template <class T> | |
1047 std::istream& | |
6767 | 1048 octave_scan_1 (std::istream& is, const scanf_format_elt& fmt, T* valptr) |
3636 | 1049 { |
3779 | 1050 T& ref = *valptr; |
1051 | |
1052 switch (fmt.type) | |
1053 { | |
1054 case 'o': | |
4926 | 1055 is >> std::oct >> ref >> std::dec; |
3779 | 1056 break; |
1057 | |
1058 case 'x': | |
4926 | 1059 is >> std::hex >> ref >> std::dec; |
1060 break; | |
1061 | |
1062 case 'i': | |
1063 { | |
1064 int c1 = is.get (); | |
1065 | |
1066 if (! is.eof ()) | |
1067 { | |
1068 if (c1 == '0') | |
1069 { | |
7709
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1070 int c2 = is.peek (); |
4926 | 1071 |
1072 if (c2 == 'x' || c2 == 'X') | |
7709
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1073 { |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1074 is.ignore (); |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1075 if (std::isxdigit (is.peek ())) |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1076 is >> std::hex >> ref >> std::dec; |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1077 else |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1078 ref = 0; |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1079 } |
4926 | 1080 else |
4927 | 1081 { |
1082 if (c2 == '0' || c2 == '1' || c2 == '2' | |
1083 || c2 == '3' || c2 == '4' || c2 == '5' | |
1084 || c2 == '6' || c2 == '7') | |
1085 is >> std::oct >> ref >> std::dec; | |
1086 else | |
1087 ref = 0; | |
1088 } | |
4926 | 1089 } |
1090 else | |
1091 { | |
1092 is.putback (c1); | |
1093 | |
1094 is >> ref; | |
1095 } | |
1096 } | |
1097 } | |
3779 | 1098 break; |
1099 | |
1100 default: | |
1101 is >> ref; | |
1102 break; | |
1103 } | |
3639 | 1104 |
3638 | 1105 return is; |
3636 | 1106 } |
1107 | |
6767 | 1108 template <class T> |
1109 std::istream& | |
1110 octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr) | |
1111 { | |
1112 if (fmt.width) | |
1113 { | |
1114 // Limit input to fmt.width characters by reading into a | |
1115 // temporary stringstream buffer. | |
1116 | |
1117 std::string tmp; | |
1118 | |
1119 is.width (fmt.width); | |
1120 is >> tmp; | |
1121 | |
1122 std::istringstream ss (tmp); | |
1123 | |
1124 octave_scan_1 (ss, fmt, valptr); | |
1125 } | |
1126 else | |
1127 octave_scan_1 (is, fmt, valptr); | |
1128 | |
1129 return is; | |
1130 } | |
1131 | |
3779 | 1132 // Note that this specialization is only used for reading characters, not |
1133 // character strings. See BEGIN_S_CONVERSION for details. | |
1134 | |
1135 template<> | |
1136 std::istream& | |
4661 | 1137 octave_scan<> (std::istream& is, const scanf_format_elt& /* fmt */, |
1138 char* valptr) | |
3779 | 1139 { |
1140 return is >> valptr; | |
1141 } | |
3636 | 1142 |
4595 | 1143 template std::istream& |
1144 octave_scan (std::istream&, const scanf_format_elt&, int*); | |
1145 | |
1146 template std::istream& | |
1147 octave_scan (std::istream&, const scanf_format_elt&, long int*); | |
1148 | |
1149 template std::istream& | |
1150 octave_scan (std::istream&, const scanf_format_elt&, short int*); | |
1151 | |
1152 template std::istream& | |
1153 octave_scan (std::istream&, const scanf_format_elt&, unsigned int*); | |
1154 | |
1155 template std::istream& | |
1156 octave_scan (std::istream&, const scanf_format_elt&, unsigned long int*); | |
1157 | |
1158 template std::istream& | |
1159 octave_scan (std::istream&, const scanf_format_elt&, unsigned short int*); | |
1160 | |
1161 #if 0 | |
1162 template std::istream& | |
1163 octave_scan (std::istream&, const scanf_format_elt&, float*); | |
1164 #endif | |
1165 | |
5403 | 1166 template<> |
5176 | 1167 std::istream& |
5403 | 1168 octave_scan<> (std::istream& is, const scanf_format_elt& fmt, double* valptr) |
5176 | 1169 { |
1170 double& ref = *valptr; | |
1171 | |
1172 switch (fmt.type) | |
1173 { | |
1174 case 'e': | |
1175 case 'f': | |
1176 case 'g': | |
1177 { | |
5259 | 1178 int c1 = EOF; |
5176 | 1179 |
1180 while (is && (c1 = is.get ()) != EOF && isspace (c1)) | |
1181 /* skip whitespace */; | |
1182 | |
1183 if (c1 != EOF) | |
1184 { | |
1185 if (c1 == 'N') | |
1186 { | |
1187 int c2 = is.get (); | |
1188 | |
1189 if (c2 != EOF) | |
1190 { | |
1191 if (c2 == 'A') | |
1192 { | |
1193 int c3 = is.get (); | |
1194 | |
1195 if (c3 != EOF) | |
1196 { | |
1197 is.putback (c3); | |
1198 | |
1199 if (isspace (c3) || ispunct (c3)) | |
1200 ref = octave_NA; | |
1201 else | |
1202 { | |
1203 is.putback (c2); | |
1204 is.putback (c1); | |
1205 | |
1206 is >> ref; | |
1207 } | |
1208 } | |
1209 else | |
1210 { | |
1211 is.clear (); | |
1212 | |
1213 ref = octave_NA; | |
1214 } | |
1215 } | |
1216 else if (c2 == 'a') | |
1217 { | |
1218 int c3 = is.get (); | |
1219 | |
1220 if (c3 != EOF) | |
1221 { | |
1222 if (c3 == 'N') | |
1223 { | |
1224 int c4 = is.get (); | |
1225 | |
1226 if (c4 != EOF) | |
1227 { | |
1228 is.putback (c4); | |
1229 | |
1230 if (isspace (c4) || ispunct (c4)) | |
1231 ref = octave_NaN; | |
1232 else | |
1233 { | |
1234 is.putback (c3); | |
1235 is.putback (c2); | |
1236 is.putback (c1); | |
1237 | |
1238 is >> ref; | |
1239 } | |
1240 } | |
1241 else | |
1242 { | |
1243 is.clear (); | |
1244 | |
1245 ref = octave_NaN; | |
1246 } | |
1247 } | |
1248 else | |
1249 { | |
1250 is.putback (c3); | |
1251 is.putback (c2); | |
1252 is.putback (c1); | |
1253 | |
1254 is >> ref; | |
1255 } | |
1256 } | |
1257 } | |
1258 else | |
1259 { | |
1260 is.putback (c2); | |
1261 is.putback (c1); | |
1262 | |
1263 is >> ref; | |
1264 } | |
1265 } | |
1266 } | |
1267 else if (c1 == 'I') | |
1268 { | |
1269 int c2 = is.get (); | |
1270 | |
1271 if (c2 != EOF) | |
1272 { | |
1273 if (c2 == 'n') | |
1274 { | |
1275 int c3 = is.get (); | |
1276 | |
1277 if (c3 != EOF) | |
6483 | 1278 { |
1279 if (c3 == 'f') | |
1280 { | |
1281 int c4 = is.get (); | |
1282 | |
1283 if (c4 != EOF) | |
1284 { | |
1285 is.putback (c4); | |
1286 | |
1287 if (isspace (c4) || ispunct (c4)) | |
1288 ref = octave_Inf; | |
1289 else | |
1290 { | |
1291 is.putback (c3); | |
1292 is.putback (c2); | |
1293 is.putback (c1); | |
1294 | |
1295 is >> ref; | |
1296 } | |
1297 } | |
1298 else | |
1299 { | |
1300 is.clear (); | |
1301 | |
5176 | 1302 ref = octave_Inf; |
6483 | 1303 } |
1304 } | |
1305 else | |
1306 { | |
1307 is.putback (c3); | |
1308 is.putback (c2); | |
1309 is.putback (c1); | |
1310 | |
1311 is >> ref; | |
1312 } | |
1313 } | |
1314 else | |
1315 { | |
1316 is.putback (c2); | |
1317 is.putback (c1); | |
1318 | |
1319 is >> ref; | |
1320 } | |
5176 | 1321 } |
1322 } | |
1323 } | |
1324 else | |
1325 { | |
1326 is.putback (c1); | |
1327 | |
1328 is >> ref; | |
1329 } | |
1330 } | |
1331 } | |
1332 break; | |
1333 | |
1334 default: | |
1335 panic_impossible (); | |
1336 break; | |
1337 } | |
1338 | |
1339 return is; | |
1340 } | |
1341 | |
2572 | 1342 template <class T> |
1343 void | |
3636 | 1344 do_scanf_conv (std::istream& is, const scanf_format_elt& fmt, |
5275 | 1345 T valptr, Matrix& mval, double *data, octave_idx_type& idx, |
1346 octave_idx_type& conversion_count, octave_idx_type nr, octave_idx_type max_size, | |
3636 | 1347 bool discard) |
2572 | 1348 { |
3640 | 1349 OCTAVE_SCAN (is, fmt, valptr); |
2572 | 1350 |
1351 if (is) | |
1352 { | |
1353 if (idx == max_size && ! discard) | |
1354 { | |
1355 max_size *= 2; | |
1356 | |
1357 if (nr > 0) | |
1358 mval.resize (nr, max_size / nr, 0.0); | |
1359 else | |
1360 mval.resize (max_size, 1, 0.0); | |
1361 | |
1362 data = mval.fortran_vec (); | |
1363 } | |
1364 | |
1365 if (! discard) | |
3268 | 1366 { |
3559 | 1367 conversion_count++; |
3268 | 1368 data[idx++] = *(valptr); |
1369 } | |
2572 | 1370 } |
1371 } | |
1372 | |
1373 template void | |
3878 | 1374 do_scanf_conv (std::istream&, const scanf_format_elt&, int*, |
5275 | 1375 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2572 | 1376 |
3233 | 1377 template void |
3636 | 1378 do_scanf_conv (std::istream&, const scanf_format_elt&, long int*, |
5275 | 1379 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3233 | 1380 |
1381 template void | |
3636 | 1382 do_scanf_conv (std::istream&, const scanf_format_elt&, short int*, |
5275 | 1383 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3233 | 1384 |
3878 | 1385 template void |
1386 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned int*, | |
5275 | 1387 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878 | 1388 |
1389 template void | |
1390 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned long int*, | |
5275 | 1391 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878 | 1392 |
1393 template void | |
1394 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned short int*, | |
5275 | 1395 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878 | 1396 |
2600 | 1397 #if 0 |
2572 | 1398 template void |
3636 | 1399 do_scanf_conv (std::istream&, const scanf_format_elt&, float*, |
5275 | 1400 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2600 | 1401 #endif |
2572 | 1402 |
1403 template void | |
3636 | 1404 do_scanf_conv (std::istream&, const scanf_format_elt&, double*, |
5275 | 1405 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2572 | 1406 |
3483 | 1407 #define DO_WHITESPACE_CONVERSION() \ |
1408 do \ | |
1409 { \ | |
1410 int c = EOF; \ | |
1411 \ | |
1412 while (is && (c = is.get ()) != EOF && isspace (c)) \ | |
1413 /* skip whitespace */; \ | |
1414 \ | |
1415 if (c != EOF) \ | |
1416 is.putback (c); \ | |
1417 } \ | |
1418 while (0) | |
1419 | |
1420 #define DO_LITERAL_CONVERSION() \ | |
1421 do \ | |
1422 { \ | |
1423 int c = EOF; \ | |
1424 \ | |
1425 int n = strlen (fmt); \ | |
1426 int i = 0; \ | |
1427 \ | |
1428 while (i < n && is && (c = is.get ()) != EOF) \ | |
1429 { \ | |
5326 | 1430 if (c == static_cast<unsigned char> (fmt[i])) \ |
3483 | 1431 { \ |
1432 i++; \ | |
1433 continue; \ | |
1434 } \ | |
1435 else \ | |
1436 { \ | |
1437 is.putback (c); \ | |
1438 break; \ | |
1439 } \ | |
1440 } \ | |
1441 \ | |
1442 if (i != n) \ | |
3538 | 1443 is.setstate (std::ios::failbit); \ |
3483 | 1444 } \ |
1445 while (0) | |
1446 | |
3640 | 1447 #define DO_PCT_CONVERSION() \ |
1448 do \ | |
1449 { \ | |
1450 int c = is.get (); \ | |
1451 \ | |
1452 if (c != EOF) \ | |
1453 { \ | |
1454 if (c != '%') \ | |
1455 { \ | |
1456 is.putback (c); \ | |
1457 is.setstate (std::ios::failbit); \ | |
1458 } \ | |
1459 } \ | |
1460 else \ | |
1461 is.setstate (std::ios::failbit); \ | |
1462 } \ | |
1463 while (0) | |
1464 | |
3483 | 1465 #define BEGIN_C_CONVERSION() \ |
3538 | 1466 is.unsetf (std::ios::skipws); \ |
3483 | 1467 \ |
1468 int width = elt->width ? elt->width : 1; \ | |
1469 \ | |
4051 | 1470 char *tbuf = new char[width + 1]; \ |
3483 | 1471 \ |
1472 int c = EOF; \ | |
1473 int n = 0; \ | |
1474 \ | |
1475 while (is && n < width && (c = is.get ()) != EOF) \ | |
5760 | 1476 tbuf[n++] = static_cast<char> (c); \ |
4051 | 1477 \ |
1478 tbuf[n] = '\0'; \ | |
3483 | 1479 \ |
5266 | 1480 if (n > 0 && c == EOF) \ |
1481 is.clear (); \ | |
1482 \ | |
4051 | 1483 std::string tmp = tbuf; \ |
1484 \ | |
1485 delete [] tbuf | |
3483 | 1486 |
1487 // For a `%s' format, skip initial whitespace and then read until the | |
5338 | 1488 // next whitespace character or until WIDTH characters have been read. |
3483 | 1489 #define BEGIN_S_CONVERSION() \ |
1490 int width = elt->width; \ | |
1491 \ | |
4051 | 1492 std::string tmp; \ |
3483 | 1493 \ |
1494 do \ | |
1495 { \ | |
1496 if (width) \ | |
5338 | 1497 { \ |
1498 char *tbuf = new char [width+1]; \ | |
1499 \ | |
1500 int c = EOF; \ | |
1501 \ | |
1502 int n = 0; \ | |
1503 \ | |
1504 while (is && (c = is.get ()) != EOF) \ | |
1505 { \ | |
1506 if (! isspace (c)) \ | |
1507 { \ | |
1508 tbuf[n++] = static_cast<char> (c); \ | |
1509 break; \ | |
1510 } \ | |
1511 } \ | |
4051 | 1512 \ |
5338 | 1513 while (is && n < width && (c = is.get ()) != EOF) \ |
1514 { \ | |
1515 if (isspace (c)) \ | |
1516 { \ | |
1517 is.putback (c); \ | |
1518 break; \ | |
1519 } \ | |
1520 else \ | |
1521 tbuf[n++] = static_cast<char> (c); \ | |
1522 } \ | |
3483 | 1523 \ |
5338 | 1524 tbuf[n] = '\0'; \ |
1525 \ | |
1526 if (n > 0 && c == EOF) \ | |
1527 is.clear (); \ | |
1528 \ | |
4051 | 1529 tmp = tbuf; \ |
5338 | 1530 \ |
4051 | 1531 delete [] tbuf; \ |
5338 | 1532 } \ |
3483 | 1533 else \ |
5338 | 1534 { \ |
1535 is >> std::ws >> tmp; \ | |
1536 } \ | |
3483 | 1537 } \ |
1538 while (0) | |
1539 | |
1540 // This format must match a nonempty sequence of characters. | |
1541 #define BEGIN_CHAR_CLASS_CONVERSION() \ | |
1542 int width = elt->width; \ | |
1543 \ | |
4051 | 1544 std::string tmp; \ |
3483 | 1545 \ |
1546 do \ | |
1547 { \ | |
7426 | 1548 if (! width) \ |
7427 | 1549 width = INT_MAX; \ |
1550 \ | |
7426 | 1551 std::ostringstream buf; \ |
1552 \ | |
1553 std::string char_class = elt->char_class; \ | |
4051 | 1554 \ |
7426 | 1555 int c = EOF; \ |
3483 | 1556 \ |
7426 | 1557 if (elt->type == '[') \ |
1558 { \ | |
1559 int chars_read = 0; \ | |
1560 while (is && chars_read++ < width && (c = is.get ()) != EOF \ | |
1561 && char_class.find (c) != NPOS) \ | |
1562 buf << static_cast<char> (c); \ | |
3483 | 1563 } \ |
1564 else \ | |
1565 { \ | |
7426 | 1566 int chars_read = 0; \ |
1567 while (is && chars_read++ < width && (c = is.get ()) != EOF \ | |
1568 && char_class.find (c) == NPOS) \ | |
1569 buf << static_cast<char> (c); \ | |
1570 } \ | |
3483 | 1571 \ |
7426 | 1572 if (width == INT_MAX && c != EOF) \ |
1573 is.putback (c); \ | |
3483 | 1574 \ |
7426 | 1575 tmp = buf.str (); \ |
3483 | 1576 \ |
7426 | 1577 if (tmp.empty ()) \ |
1578 is.setstate (std::ios::failbit); \ | |
3483 | 1579 } \ |
1580 while (0) | |
1581 | |
3410 | 1582 #define FINISH_CHARACTER_CONVERSION() \ |
1583 do \ | |
1584 { \ | |
4051 | 1585 width = tmp.length (); \ |
3410 | 1586 \ |
1587 if (is) \ | |
1588 { \ | |
1589 int i = 0; \ | |
1590 \ | |
1591 if (! discard) \ | |
1592 { \ | |
1593 conversion_count++; \ | |
1594 \ | |
1595 while (i < width && tmp[i] != '\0') \ | |
1596 { \ | |
1597 if (data_index == max_size) \ | |
1598 { \ | |
1599 max_size *= 2; \ | |
1600 \ | |
4420 | 1601 if (all_char_conv) \ |
3410 | 1602 { \ |
4420 | 1603 if (one_elt_size_spec) \ |
3410 | 1604 mval.resize (1, max_size, 0.0); \ |
4420 | 1605 else if (nr > 0) \ |
1606 mval.resize (nr, max_size / nr, 0.0); \ | |
3410 | 1607 else \ |
4420 | 1608 panic_impossible (); \ |
3410 | 1609 } \ |
4420 | 1610 else if (nr > 0) \ |
6970 | 1611 mval.resize (nr, max_size / nr, 0.0); \ |
4420 | 1612 else \ |
1613 mval.resize (max_size, 1, 0.0); \ | |
3410 | 1614 \ |
1615 data = mval.fortran_vec (); \ | |
1616 } \ | |
1617 \ | |
1618 data[data_index++] = tmp[i++]; \ | |
1619 } \ | |
1620 } \ | |
1621 } \ | |
1622 } \ | |
1623 while (0) | |
2117 | 1624 |
1625 octave_value | |
1626 octave_base_stream::do_scanf (scanf_format_list& fmt_list, | |
5275 | 1627 octave_idx_type nr, octave_idx_type nc, bool one_elt_size_spec, |
1628 octave_idx_type& conversion_count, const std::string& who) | |
2117 | 1629 { |
3268 | 1630 conversion_count = 0; |
1631 | |
3640 | 1632 int nconv = fmt_list.num_conversions (); |
1633 | |
5275 | 1634 octave_idx_type data_index = 0; |
2121 | 1635 |
2117 | 1636 octave_value retval = Matrix (); |
1637 | |
3268 | 1638 if (nr == 0 || nc == 0) |
1639 { | |
1640 if (one_elt_size_spec) | |
1641 nc = 0; | |
1642 | |
1643 return Matrix (nr, nc, 0.0); | |
1644 } | |
1645 | |
3523 | 1646 std::istream *isp = input_stream (); |
2117 | 1647 |
1648 bool all_char_conv = fmt_list.all_character_conversions (); | |
1649 | |
1650 Matrix mval; | |
1651 double *data = 0; | |
5275 | 1652 octave_idx_type max_size = 0; |
1653 octave_idx_type max_conv = 0; | |
1654 | |
1655 octave_idx_type final_nr = 0; | |
1656 octave_idx_type final_nc = 0; | |
2117 | 1657 |
3268 | 1658 if (all_char_conv) |
1659 { | |
4420 | 1660 // Any of these could be resized later (if we have %s |
1661 // conversions, we may read more than one element for each | |
1662 // conversion). | |
1663 | |
3268 | 1664 if (one_elt_size_spec) |
1665 { | |
3410 | 1666 max_size = 512; |
1667 mval.resize (1, max_size, 0.0); | |
1668 | |
3268 | 1669 if (nr > 0) |
1670 max_conv = nr; | |
1671 } | |
4420 | 1672 else if (nr > 0) |
3268 | 1673 { |
4420 | 1674 if (nc > 0) |
1675 { | |
1676 mval.resize (nr, nc, 0.0); | |
1677 max_size = max_conv = nr * nc; | |
1678 } | |
1679 else | |
1680 { | |
1681 mval.resize (nr, 32, 0.0); | |
1682 max_size = nr * 32; | |
1683 } | |
3268 | 1684 } |
4420 | 1685 else |
1686 panic_impossible (); | |
3268 | 1687 } |
1688 else if (nr > 0) | |
2117 | 1689 { |
1690 if (nc > 0) | |
1691 { | |
4420 | 1692 // Will not resize later. |
2117 | 1693 mval.resize (nr, nc, 0.0); |
1694 max_size = nr * nc; | |
3268 | 1695 max_conv = max_size; |
2117 | 1696 } |
1697 else | |
1698 { | |
4420 | 1699 // Maybe resize later. |
2117 | 1700 mval.resize (nr, 32, 0.0); |
2121 | 1701 max_size = nr * 32; |
2117 | 1702 } |
1703 } | |
1704 else | |
1705 { | |
4420 | 1706 // Maybe resize later. |
2117 | 1707 mval.resize (32, 1, 0.0); |
1708 max_size = 32; | |
1709 } | |
1710 | |
4420 | 1711 data = mval.fortran_vec (); |
1712 | |
2117 | 1713 if (isp) |
1714 { | |
3523 | 1715 std::istream& is = *isp; |
2117 | 1716 |
1717 const scanf_format_elt *elt = fmt_list.first (); | |
1718 | |
3538 | 1719 std::ios::fmtflags flags = is.flags (); |
2213 | 1720 |
2117 | 1721 for (;;) |
1722 { | |
4153 | 1723 OCTAVE_QUIT; |
1724 | |
2117 | 1725 if (elt) |
1726 { | |
3268 | 1727 if (max_conv > 0 && conversion_count == max_conv) |
1728 { | |
1729 if (all_char_conv && one_elt_size_spec) | |
1730 { | |
1731 final_nr = 1; | |
1732 final_nc = data_index; | |
1733 } | |
1734 else | |
1735 { | |
1736 final_nr = nr; | |
1737 final_nc = (data_index - 1) / nr + 1; | |
1738 } | |
1739 | |
1740 break; | |
1741 } | |
1742 else if (data_index == max_size) | |
2117 | 1743 { |
3410 | 1744 max_size *= 2; |
1745 | |
4420 | 1746 if (all_char_conv) |
2687 | 1747 { |
4420 | 1748 if (one_elt_size_spec) |
3410 | 1749 mval.resize (1, max_size, 0.0); |
4420 | 1750 else if (nr > 0) |
1751 mval.resize (nr, max_size / nr, 0.0); | |
3410 | 1752 else |
4420 | 1753 panic_impossible (); |
2687 | 1754 } |
4420 | 1755 else if (nr > 0) |
6970 | 1756 mval.resize (nr, max_size / nr, 0.0); |
4420 | 1757 else |
1758 mval.resize (max_size, 1, 0.0); | |
3410 | 1759 |
1760 data = mval.fortran_vec (); | |
2117 | 1761 } |
1762 | |
1763 const char *fmt = elt->text; | |
1764 | |
1765 bool discard = elt->discard; | |
1766 | |
1767 switch (elt->type) | |
1768 { | |
3483 | 1769 case scanf_format_elt::whitespace_conversion: |
1770 DO_WHITESPACE_CONVERSION (); | |
1771 break; | |
1772 | |
1773 case scanf_format_elt::literal_conversion: | |
1774 DO_LITERAL_CONVERSION (); | |
1775 break; | |
1776 | |
2117 | 1777 case '%': |
3640 | 1778 DO_PCT_CONVERSION (); |
3483 | 1779 break; |
2117 | 1780 |
3878 | 1781 case 'd': case 'i': |
2117 | 1782 { |
3233 | 1783 switch (elt->modifier) |
1784 { | |
1785 case 'h': | |
1786 { | |
1787 short int tmp; | |
3779 | 1788 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268 | 1789 data_index, conversion_count, |
3233 | 1790 nr, max_size, discard); |
1791 } | |
3483 | 1792 break; |
3233 | 1793 |
1794 case 'l': | |
1795 { | |
1796 long int tmp; | |
3779 | 1797 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268 | 1798 data_index, conversion_count, |
3233 | 1799 nr, max_size, discard); |
1800 } | |
3483 | 1801 break; |
3233 | 1802 |
1803 default: | |
1804 { | |
1805 int tmp; | |
3779 | 1806 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268 | 1807 data_index, conversion_count, |
3233 | 1808 nr, max_size, discard); |
1809 } | |
3483 | 1810 break; |
3233 | 1811 } |
2117 | 1812 } |
3483 | 1813 break; |
2117 | 1814 |
3878 | 1815 case 'o': case 'u': case 'x': |
1816 { | |
1817 switch (elt->modifier) | |
1818 { | |
1819 case 'h': | |
1820 { | |
1821 unsigned short int tmp; | |
1822 do_scanf_conv (is, *elt, &tmp, mval, data, | |
1823 data_index, conversion_count, | |
1824 nr, max_size, discard); | |
1825 } | |
1826 break; | |
1827 | |
1828 case 'l': | |
1829 { | |
1830 unsigned long int tmp; | |
1831 do_scanf_conv (is, *elt, &tmp, mval, data, | |
1832 data_index, conversion_count, | |
1833 nr, max_size, discard); | |
1834 } | |
1835 break; | |
1836 | |
1837 default: | |
1838 { | |
1839 unsigned int tmp; | |
1840 do_scanf_conv (is, *elt, &tmp, mval, data, | |
1841 data_index, conversion_count, | |
1842 nr, max_size, discard); | |
1843 } | |
1844 break; | |
1845 } | |
1846 } | |
1847 break; | |
1848 | |
2117 | 1849 case 'e': case 'f': case 'g': |
1850 { | |
2600 | 1851 double tmp; |
1852 | |
3779 | 1853 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268 | 1854 data_index, conversion_count, |
2600 | 1855 nr, max_size, discard); |
2117 | 1856 } |
3483 | 1857 break; |
2117 | 1858 |
2213 | 1859 case 'c': |
3410 | 1860 { |
3483 | 1861 BEGIN_C_CONVERSION (); |
3410 | 1862 |
1863 FINISH_CHARACTER_CONVERSION (); | |
3483 | 1864 |
1865 is.setf (flags); | |
3410 | 1866 } |
1867 break; | |
2213 | 1868 |
2117 | 1869 case 's': |
1870 { | |
3483 | 1871 BEGIN_S_CONVERSION (); |
3268 | 1872 |
3410 | 1873 FINISH_CHARACTER_CONVERSION (); |
2117 | 1874 } |
3483 | 1875 break; |
1876 | |
1877 case '[': case '^': | |
1878 { | |
1879 BEGIN_CHAR_CLASS_CONVERSION (); | |
1880 | |
1881 FINISH_CHARACTER_CONVERSION (); | |
1882 } | |
1883 break; | |
1884 | |
1885 case 'p': | |
4468 | 1886 error ("%s: unsupported format specifier", who.c_str ()); |
2117 | 1887 break; |
1888 | |
1889 default: | |
4468 | 1890 error ("%s: internal format error", who.c_str ()); |
2117 | 1891 break; |
1892 } | |
1893 | |
1894 if (! ok ()) | |
1895 { | |
1896 break; | |
1897 } | |
1898 else if (! is) | |
1899 { | |
3268 | 1900 if (all_char_conv) |
2117 | 1901 { |
3268 | 1902 if (one_elt_size_spec) |
1903 { | |
1904 final_nr = 1; | |
1905 final_nc = data_index; | |
1906 } | |
1907 else if (data_index > nr) | |
2117 | 1908 { |
2759 | 1909 final_nr = nr; |
3268 | 1910 final_nc = (data_index - 1) / nr + 1; |
2117 | 1911 } |
1912 else | |
1913 { | |
3268 | 1914 final_nr = data_index; |
1915 final_nc = 1; | |
1916 } | |
1917 } | |
1918 else if (nr > 0) | |
1919 { | |
1920 if (data_index > nr) | |
1921 { | |
1922 final_nr = nr; | |
1923 final_nc = (data_index - 1) / nr + 1; | |
1924 } | |
1925 else | |
1926 { | |
1927 final_nr = data_index; | |
2117 | 1928 final_nc = 1; |
1929 } | |
1930 } | |
1931 else | |
1932 { | |
3268 | 1933 final_nr = data_index; |
2759 | 1934 final_nc = 1; |
1935 } | |
1936 | |
3337 | 1937 // If it looks like we have a matching failure, then |
1938 // reset the failbit in the stream state. | |
1939 | |
3538 | 1940 if (is.rdstate () & std::ios::failbit) |
1941 is.clear (is.rdstate () & (~std::ios::failbit)); | |
3337 | 1942 |
5775 | 1943 // FIXME -- is this the right thing to do? |
3342 | 1944 |
1945 if (interactive && name () == "stdin") | |
2759 | 1946 { |
1947 is.clear (); | |
1948 | |
1949 // Skip to end of line. | |
1950 | |
1951 bool err; | |
4468 | 1952 do_gets (-1, err, false, who); |
2117 | 1953 } |
1954 | |
1955 break; | |
1956 } | |
1957 } | |
1958 else | |
1959 { | |
4468 | 1960 error ("%s: internal format error", who.c_str ()); |
2117 | 1961 break; |
1962 } | |
1963 | |
3640 | 1964 elt = fmt_list.next (nconv > 0); |
2117 | 1965 } |
1966 } | |
1967 | |
1968 if (ok ()) | |
1969 { | |
2121 | 1970 mval.resize (final_nr, final_nc, 0.0); |
2117 | 1971 |
3268 | 1972 retval = mval; |
1973 | |
2117 | 1974 if (all_char_conv) |
5279 | 1975 retval = retval.convert_to_str (false, true); |
2117 | 1976 } |
1977 | |
1978 return retval; | |
1979 } | |
1980 | |
1981 octave_value | |
3810 | 1982 octave_base_stream::scanf (const std::string& fmt, const Array<double>& size, |
5275 | 1983 octave_idx_type& conversion_count, const std::string& who) |
2117 | 1984 { |
1985 octave_value retval = Matrix (); | |
1986 | |
3559 | 1987 conversion_count = 0; |
2117 | 1988 |
3523 | 1989 std::istream *isp = input_stream (); |
2117 | 1990 |
1991 if (isp) | |
1992 { | |
1993 scanf_format_list fmt_list (fmt); | |
1994 | |
3640 | 1995 if (fmt_list.num_conversions () == -1) |
4468 | 1996 ::error ("%s: invalid format specified", who.c_str ()); |
3640 | 1997 else |
2117 | 1998 { |
5275 | 1999 octave_idx_type nr = -1; |
2000 octave_idx_type nc = -1; | |
3640 | 2001 |
2002 bool one_elt_size_spec; | |
2003 | |
4468 | 2004 get_size (size, nr, nc, one_elt_size_spec, who); |
3640 | 2005 |
2006 if (! error_state) | |
2007 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec, | |
4468 | 2008 conversion_count, who); |
2215 | 2009 } |
2010 } | |
2011 else | |
4468 | 2012 invalid_operation (who, "reading"); |
2572 | 2013 |
2014 return retval; | |
2015 } | |
2016 | |
2712 | 2017 bool |
2018 octave_base_stream::do_oscanf (const scanf_format_elt *elt, | |
4468 | 2019 octave_value& retval, const std::string& who) |
2572 | 2020 { |
2712 | 2021 bool quit = false; |
2215 | 2022 |
3523 | 2023 std::istream *isp = input_stream (); |
2215 | 2024 |
2025 if (isp) | |
2026 { | |
3523 | 2027 std::istream& is = *isp; |
2215 | 2028 |
3538 | 2029 std::ios::fmtflags flags = is.flags (); |
2215 | 2030 |
2031 if (elt) | |
2032 { | |
2033 const char *fmt = elt->text; | |
2034 | |
2035 bool discard = elt->discard; | |
2036 | |
2037 switch (elt->type) | |
2038 { | |
3483 | 2039 case scanf_format_elt::whitespace_conversion: |
2040 DO_WHITESPACE_CONVERSION (); | |
2041 break; | |
2042 | |
2043 case scanf_format_elt::literal_conversion: | |
2044 DO_LITERAL_CONVERSION (); | |
2045 break; | |
2046 | |
2215 | 2047 case '%': |
2048 { | |
3640 | 2049 DO_PCT_CONVERSION (); |
2050 | |
2051 if (! is) | |
2712 | 2052 quit = true; |
3640 | 2053 |
2215 | 2054 } |
2055 break; | |
2056 | |
3878 | 2057 case 'd': case 'i': |
2215 | 2058 { |
2059 int tmp; | |
2060 | |
3640 | 2061 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712 | 2062 { |
2063 if (! discard) | |
4233 | 2064 retval = tmp; |
2712 | 2065 } |
2066 else | |
2067 quit = true; | |
2215 | 2068 } |
2069 break; | |
2070 | |
3878 | 2071 case 'o': case 'u': case 'x': |
2072 { | |
2073 long int tmp; | |
2074 | |
2075 if (OCTAVE_SCAN (is, *elt, &tmp)) | |
2076 { | |
2077 if (! discard) | |
4254 | 2078 retval = tmp; |
3878 | 2079 } |
2080 else | |
2081 quit = true; | |
2082 } | |
2083 break; | |
2084 | |
2215 | 2085 case 'e': case 'f': case 'g': |
2086 { | |
2600 | 2087 double tmp; |
2088 | |
3640 | 2089 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712 | 2090 { |
2091 if (! discard) | |
2092 retval = tmp; | |
2093 } | |
2094 else | |
2095 quit = true; | |
2215 | 2096 } |
2097 break; | |
2098 | |
2099 case 'c': | |
2100 { | |
3483 | 2101 BEGIN_C_CONVERSION (); |
2102 | |
2103 if (! discard) | |
2104 retval = tmp; | |
2105 | |
2106 if (! is) | |
2712 | 2107 quit = true; |
2215 | 2108 |
2109 is.setf (flags); | |
2110 } | |
2111 break; | |
2112 | |
2113 case 's': | |
2114 { | |
3483 | 2115 BEGIN_S_CONVERSION (); |
2116 | |
2117 if (! discard) | |
2118 retval = tmp; | |
2572 | 2119 |
3268 | 2120 if (! is) |
2121 quit = true; | |
2215 | 2122 } |
2123 break; | |
2124 | |
3483 | 2125 case '[': case '^': |
2126 { | |
2127 BEGIN_CHAR_CLASS_CONVERSION (); | |
2128 | |
2129 if (! discard) | |
2130 retval = tmp; | |
2131 | |
2132 if (! is) | |
2133 quit = true; | |
2134 } | |
2135 break; | |
2136 | |
2137 case 'p': | |
4468 | 2138 error ("%s: unsupported format specifier", who.c_str ()); |
2215 | 2139 break; |
2140 | |
2141 default: | |
4468 | 2142 error ("%s: internal format error", who.c_str ()); |
2215 | 2143 break; |
2144 } | |
2145 } | |
2146 | |
2147 if (ok () && is.fail ()) | |
2148 { | |
4468 | 2149 error ("%s: read error", who.c_str ()); |
3483 | 2150 |
5775 | 2151 // FIXME -- is this the right thing to do? |
3342 | 2152 |
2153 if (interactive && name () == "stdin") | |
2215 | 2154 { |
2155 // Skip to end of line. | |
2156 | |
2157 bool err; | |
4468 | 2158 do_gets (-1, err, false, who); |
2215 | 2159 } |
2160 } | |
2161 } | |
2162 | |
2712 | 2163 return quit; |
2215 | 2164 } |
2165 | |
2166 octave_value_list | |
4468 | 2167 octave_base_stream::oscanf (const std::string& fmt, const std::string& who) |
2215 | 2168 { |
2169 octave_value_list retval; | |
2170 | |
3523 | 2171 std::istream *isp = input_stream (); |
2215 | 2172 |
2173 if (isp) | |
2174 { | |
3523 | 2175 std::istream& is = *isp; |
2215 | 2176 |
2177 scanf_format_list fmt_list (fmt); | |
2178 | |
2179 int nconv = fmt_list.num_conversions (); | |
2180 | |
3640 | 2181 if (nconv == -1) |
4468 | 2182 ::error ("%s: invalid format specified", who.c_str ()); |
3640 | 2183 else |
2215 | 2184 { |
3640 | 2185 is.clear (); |
2186 | |
2187 int len = fmt_list.length (); | |
2188 | |
2189 retval.resize (nconv+1, Matrix ()); | |
2190 | |
2191 const scanf_format_elt *elt = fmt_list.first (); | |
2192 | |
2193 int num_values = 0; | |
2194 | |
2195 bool quit = false; | |
2196 | |
5275 | 2197 for (octave_idx_type i = 0; i < len; i++) |
3640 | 2198 { |
2199 octave_value tmp; | |
2200 | |
4468 | 2201 quit = do_oscanf (elt, tmp, who); |
3640 | 2202 |
2203 if (quit) | |
2204 break; | |
2205 else | |
2206 { | |
2207 if (tmp.is_defined ()) | |
2208 retval (num_values++) = tmp; | |
2209 | |
2210 if (! ok ()) | |
2211 break; | |
2212 | |
2213 elt = fmt_list.next (nconv > 0); | |
2214 } | |
2215 } | |
2216 | |
4233 | 2217 retval(nconv) = num_values; |
3640 | 2218 |
2219 if (! quit) | |
2220 { | |
2221 // Pick up any trailing stuff. | |
2222 if (ok () && len > nconv) | |
2223 { | |
2224 octave_value tmp; | |
3704 | 2225 |
2226 elt = fmt_list.next (); | |
2227 | |
4468 | 2228 do_oscanf (elt, tmp, who); |
3640 | 2229 } |
2230 } | |
2117 | 2231 } |
2232 } | |
2233 else | |
4468 | 2234 invalid_operation (who, "reading"); |
2117 | 2235 |
2236 return retval; | |
2237 } | |
2238 | |
2239 // Functions that are defined for all output streams (output streams | |
2240 // are those that define os). | |
2241 | |
2242 int | |
2243 octave_base_stream::flush (void) | |
2244 { | |
2245 int retval = -1; | |
2246 | |
3523 | 2247 std::ostream *os = output_stream (); |
2117 | 2248 |
2249 if (os) | |
2250 { | |
2251 os->flush (); | |
2252 | |
2253 if (os->good ()) | |
2254 retval = 0; | |
2255 } | |
2256 else | |
2257 invalid_operation ("fflush", "writing"); | |
2258 | |
2259 return retval; | |
2260 } | |
2261 | |
2262 class | |
2263 printf_value_cache | |
2264 { | |
2265 public: | |
2266 | |
3653 | 2267 enum state { ok, conversion_error }; |
2117 | 2268 |
7352 | 2269 printf_value_cache (const octave_value_list& args, const std::string& who) |
2117 | 2270 : values (args), val_idx (0), elt_idx (0), |
2271 n_vals (values.length ()), n_elts (0), data (0), | |
7352 | 2272 curr_state (ok) |
2273 { | |
2274 for (octave_idx_type i = 0; i < values.length (); i++) | |
2275 { | |
2276 octave_value val = values(i); | |
2277 | |
2278 if (val.is_map () || val.is_cell () || val.is_object () | |
2279 || val.is_list ()) | |
2280 { | |
2281 gripe_wrong_type_arg (who, val); | |
2282 break; | |
2283 } | |
2284 } | |
2285 } | |
2117 | 2286 |
2287 ~printf_value_cache (void) { } | |
2288 | |
2289 // Get the current value as a double and advance the internal pointer. | |
2290 double double_value (void); | |
2291 | |
2292 // Get the current value as an int and advance the internal pointer. | |
2293 int int_value (void); | |
2294 | |
2295 // Get the current value as a string and advance the internal pointer. | |
3523 | 2296 std::string string_value (void); |
2117 | 2297 |
3145 | 2298 operator bool () const { return (curr_state == ok); } |
2117 | 2299 |
3653 | 2300 bool exhausted (void) { return (val_idx >= n_vals); } |
2117 | 2301 |
2302 private: | |
2303 | |
2304 const octave_value_list values; | |
2305 int val_idx; | |
2306 int elt_idx; | |
2307 int n_vals; | |
2308 int n_elts; | |
2309 const double *data; | |
4874 | 2310 NDArray curr_val; |
2117 | 2311 state curr_state; |
2312 | |
2313 // Must create value cache with values! | |
2314 | |
2315 printf_value_cache (void); | |
2316 | |
2317 // No copying! | |
2318 | |
2319 printf_value_cache (const printf_value_cache&); | |
2320 | |
2321 printf_value_cache& operator = (const printf_value_cache&); | |
2322 }; | |
2323 | |
2324 double | |
2325 printf_value_cache::double_value (void) | |
2326 { | |
2327 double retval = 0.0; | |
2328 | |
3711 | 2329 if (exhausted ()) |
2330 curr_state = conversion_error; | |
2331 | |
2332 while (! exhausted ()) | |
2117 | 2333 { |
2334 if (! data) | |
2335 { | |
2336 octave_value tmp_val = values (val_idx); | |
2337 | |
5476 | 2338 // Force string conversion here for compatibility. |
2339 | |
2340 curr_val = tmp_val.array_value (true); | |
2117 | 2341 |
2342 if (! error_state) | |
2343 { | |
2344 elt_idx = 0; | |
2345 n_elts = curr_val.length (); | |
2346 data = curr_val.data (); | |
2347 } | |
2348 else | |
2349 { | |
2350 curr_state = conversion_error; | |
2351 break; | |
2352 } | |
2353 } | |
2354 | |
2355 if (elt_idx < n_elts) | |
2356 { | |
3653 | 2357 retval = data[elt_idx++]; |
2358 | |
2359 if (elt_idx >= n_elts) | |
2360 { | |
2361 elt_idx = 0; | |
2362 val_idx++; | |
2363 data = 0; | |
2364 } | |
2365 | |
2117 | 2366 break; |
2367 } | |
2368 else | |
2369 { | |
2370 val_idx++; | |
2371 data = 0; | |
3969 | 2372 |
2373 if (n_elts == 0 && exhausted ()) | |
2374 curr_state = conversion_error; | |
2375 | |
2117 | 2376 continue; |
2377 } | |
2378 } | |
2379 | |
2380 return retval; | |
2381 } | |
2382 | |
2383 int | |
2384 printf_value_cache::int_value (void) | |
2385 { | |
2386 int retval = 0; | |
2387 | |
2388 double dval = double_value (); | |
2389 | |
2390 if (! error_state) | |
2391 { | |
2392 if (D_NINT (dval) == dval) | |
2393 retval = NINT (dval); | |
2394 else | |
2395 curr_state = conversion_error; | |
2396 } | |
2397 | |
2398 return retval; | |
2399 } | |
2400 | |
3536 | 2401 std::string |
2117 | 2402 printf_value_cache::string_value (void) |
2403 { | |
3523 | 2404 std::string retval; |
2117 | 2405 |
4425 | 2406 if (exhausted ()) |
2407 curr_state = conversion_error; | |
4257 | 2408 else |
2117 | 2409 { |
4425 | 2410 octave_value tval = values (val_idx++); |
2411 | |
2412 if (tval.rows () == 1) | |
2413 retval = tval.string_value (); | |
2414 else | |
2415 { | |
2416 // In the name of Matlab compatibility. | |
2417 | |
2418 charMatrix chm = tval.char_matrix_value (); | |
2419 | |
5275 | 2420 octave_idx_type nr = chm.rows (); |
2421 octave_idx_type nc = chm.columns (); | |
4425 | 2422 |
2423 int k = 0; | |
2424 | |
2425 retval.resize (nr * nc, '\0'); | |
2426 | |
5275 | 2427 for (octave_idx_type j = 0; j < nc; j++) |
2428 for (octave_idx_type i = 0; i < nr; i++) | |
4425 | 2429 retval[k++] = chm(i,j); |
2430 } | |
2431 | |
2432 if (error_state) | |
2433 curr_state = conversion_error; | |
2117 | 2434 } |
4257 | 2435 |
2117 | 2436 return retval; |
2437 } | |
2438 | |
2439 // Ugh again and again. | |
2440 | |
2572 | 2441 template <class T> |
3620 | 2442 int |
3523 | 2443 do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1, |
4468 | 2444 int sa_2, T arg, const std::string& who) |
2572 | 2445 { |
3620 | 2446 int retval = 0; |
2447 | |
2572 | 2448 switch (nsa) |
2449 { | |
2450 case 2: | |
3640 | 2451 retval = octave_format (os, fmt, sa_1, sa_2, arg); |
2572 | 2452 break; |
2453 | |
2454 case 1: | |
3640 | 2455 retval = octave_format (os, fmt, sa_1, arg); |
2572 | 2456 break; |
2457 | |
2458 case 0: | |
3640 | 2459 retval = octave_format (os, fmt, arg); |
2572 | 2460 break; |
2461 | |
2462 default: | |
4468 | 2463 ::error ("%s: internal error handling format", who.c_str ()); |
2572 | 2464 break; |
2465 } | |
3620 | 2466 |
2467 return retval; | |
2572 | 2468 } |
2469 | |
3620 | 2470 template int |
4468 | 2471 do_printf_conv (std::ostream&, const char*, int, int, int, int, |
2472 const std::string&); | |
2473 | |
2474 template int | |
2475 do_printf_conv (std::ostream&, const char*, int, int, int, long, | |
2476 const std::string&); | |
2477 | |
3878 | 2478 template int |
4468 | 2479 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned int, |
2480 const std::string&); | |
2481 | |
2482 template int | |
2483 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned long, | |
2484 const std::string&); | |
2485 | |
2486 template int | |
2487 do_printf_conv (std::ostream&, const char*, int, int, int, double, | |
2488 const std::string&); | |
2489 | |
2490 template int | |
2491 do_printf_conv (std::ostream&, const char*, int, int, int, const char*, | |
2492 const std::string&); | |
2117 | 2493 |
6492 | 2494 #define DO_DOUBLE_CONV(TQUAL) \ |
2495 do \ | |
2496 { \ | |
7199 | 2497 if (val > std::numeric_limits<TQUAL long>::max () \ |
2498 || val < std::numeric_limits<TQUAL long>::min ()) \ | |
6492 | 2499 { \ |
7199 | 2500 std::string tfmt = fmt; \ |
6492 | 2501 \ |
7199 | 2502 tfmt.replace (tfmt.rfind (elt->type), 1, ".f"); \ |
6492 | 2503 \ |
7199 | 2504 if (elt->modifier == 'l') \ |
2505 tfmt.replace (tfmt.rfind (elt->modifier), 1, ""); \ | |
2506 \ | |
2507 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2, \ | |
2508 val, who); \ | |
6492 | 2509 } \ |
2510 else \ | |
7199 | 2511 retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, \ |
2512 static_cast<TQUAL long> (val), who); \ | |
6492 | 2513 } \ |
2514 while (0) | |
2515 | |
2117 | 2516 int |
2517 octave_base_stream::do_printf (printf_format_list& fmt_list, | |
4468 | 2518 const octave_value_list& args, |
2519 const std::string& who) | |
2117 | 2520 { |
3620 | 2521 int retval = 0; |
2117 | 2522 |
3640 | 2523 int nconv = fmt_list.num_conversions (); |
2524 | |
3523 | 2525 std::ostream *osp = output_stream (); |
2117 | 2526 |
2527 if (osp) | |
2528 { | |
3523 | 2529 std::ostream& os = *osp; |
2117 | 2530 |
2531 const printf_format_elt *elt = fmt_list.first (); | |
2532 | |
7352 | 2533 printf_value_cache val_cache (args, who); |
2534 | |
2535 if (error_state) | |
2536 return retval; | |
2117 | 2537 |
2538 for (;;) | |
2539 { | |
4153 | 2540 OCTAVE_QUIT; |
2541 | |
2117 | 2542 if (elt) |
2543 { | |
3640 | 2544 // NSA is the number of `star' args to convert. |
2545 | |
2546 int nsa = (elt->fw < 0) + (elt->prec < 0); | |
2117 | 2547 |
2548 int sa_1 = 0; | |
2549 int sa_2 = 0; | |
2550 | |
2551 if (nsa > 0) | |
2552 { | |
2553 sa_1 = val_cache.int_value (); | |
2554 | |
2555 if (! val_cache) | |
2556 break; | |
2557 else | |
2558 { | |
2559 if (nsa > 1) | |
2560 { | |
2561 sa_2 = val_cache.int_value (); | |
2562 | |
2563 if (! val_cache) | |
2564 break; | |
2565 } | |
2566 } | |
2567 } | |
2568 | |
2569 const char *fmt = elt->text; | |
2570 | |
3640 | 2571 if (elt->type == '%') |
2572 { | |
2573 os << "%"; | |
2574 retval++; | |
2575 } | |
2576 else if (elt->args == 0 && elt->text) | |
2577 { | |
2578 os << elt->text; | |
2579 retval += strlen (elt->text); | |
2580 } | |
4257 | 2581 else if (elt->type == 's') |
3640 | 2582 { |
2583 std::string val = val_cache.string_value (); | |
2584 | |
2585 if (val_cache) | |
2586 retval += do_printf_conv (os, fmt, nsa, sa_1, | |
4468 | 2587 sa_2, val.c_str (), who); |
3640 | 2588 else |
2589 break; | |
2590 } | |
2117 | 2591 else |
2592 { | |
3640 | 2593 double val = val_cache.double_value (); |
2594 | |
2595 if (val_cache) | |
2117 | 2596 { |
6492 | 2597 if (lo_ieee_isnan (val) || xisinf (val)) |
4303 | 2598 { |
2599 std::string tfmt = fmt; | |
6865 | 2600 std::string::size_type i1, i2; |
2601 | |
2602 tfmt.replace ((i1 = tfmt.rfind (elt->type)), | |
2603 1, 1, 's'); | |
2604 | |
2605 if ((i2 = tfmt.rfind ('.')) != NPOS && i2 < i1) | |
2606 { | |
2607 tfmt.erase (i2, i1-i2); | |
2608 if (elt->prec < 0) | |
2609 nsa--; | |
2610 } | |
6492 | 2611 |
2612 const char *tval = xisinf (val) | |
2613 ? (val < 0 ? "-Inf" : "Inf") | |
2614 : (lo_ieee_is_NA (val) ? "NA" : "NaN"); | |
2615 | |
2616 retval += do_printf_conv (os, tfmt.c_str (), | |
2617 nsa, sa_1, sa_2, | |
2618 tval, who); | |
4303 | 2619 } |
2620 else | |
3640 | 2621 { |
6492 | 2622 char type = elt->type; |
2623 | |
2624 switch (type) | |
4303 | 2625 { |
2626 case 'd': case 'i': case 'c': | |
7227 | 2627 DO_DOUBLE_CONV (OCTAVE_EMPTY_CPP_ARG); |
4303 | 2628 break; |
2629 | |
2630 case 'o': case 'x': case 'X': case 'u': | |
6492 | 2631 DO_DOUBLE_CONV (unsigned); |
4303 | 2632 break; |
2633 | |
2634 case 'f': case 'e': case 'E': | |
2635 case 'g': case 'G': | |
2636 retval | |
2637 += do_printf_conv (os, fmt, nsa, sa_1, sa_2, | |
4468 | 2638 val, who); |
4303 | 2639 break; |
2640 | |
2641 default: | |
4468 | 2642 error ("%s: invalid format specifier", |
2643 who.c_str ()); | |
4303 | 2644 return -1; |
2645 break; | |
2646 } | |
3640 | 2647 } |
2117 | 2648 } |
2649 else | |
3620 | 2650 break; |
2117 | 2651 } |
2652 | |
3620 | 2653 if (! os) |
2117 | 2654 { |
4468 | 2655 error ("%s: write error", who.c_str ()); |
2117 | 2656 break; |
2657 } | |
2658 } | |
2659 else | |
2660 { | |
4468 | 2661 ::error ("%s: internal error handling format", who.c_str ()); |
2117 | 2662 retval = -1; |
2663 break; | |
2664 } | |
2665 | |
3640 | 2666 elt = fmt_list.next (nconv > 0 && ! val_cache.exhausted ()); |
2667 | |
2668 if (! elt || (val_cache.exhausted () && elt->args > 0)) | |
2669 break; | |
2670 } | |
2117 | 2671 } |
3640 | 2672 else |
4468 | 2673 invalid_operation (who, "writing"); |
2117 | 2674 |
2675 return retval; | |
2676 } | |
2677 | |
2678 int | |
3640 | 2679 octave_base_stream::printf (const std::string& fmt, |
4468 | 2680 const octave_value_list& args, |
2681 const std::string& who) | |
2117 | 2682 { |
3640 | 2683 int retval = 0; |
2684 | |
2685 printf_format_list fmt_list (fmt); | |
2686 | |
2687 if (fmt_list.num_conversions () == -1) | |
4468 | 2688 ::error ("%s: invalid format specified", who.c_str ()); |
2117 | 2689 else |
4468 | 2690 retval = do_printf (fmt_list, args, who); |
2117 | 2691 |
2692 return retval; | |
2693 } | |
2694 | |
2695 int | |
4468 | 2696 octave_base_stream::puts (const std::string& s, const std::string& who) |
2117 | 2697 { |
2698 int retval = -1; | |
2699 | |
3523 | 2700 std::ostream *osp = output_stream (); |
2117 | 2701 |
2702 if (osp) | |
2703 { | |
3523 | 2704 std::ostream& os = *osp; |
2117 | 2705 |
2706 os << s; | |
2707 | |
2708 if (os) | |
2709 { | |
5775 | 2710 // FIXME -- why does this seem to be necessary? |
2117 | 2711 // Without it, output from a loop like |
2712 // | |
2713 // for i = 1:100, fputs (stdout, "foo\n"); endfor | |
2714 // | |
2715 // doesn't seem to go to the pager immediately. | |
2716 | |
2717 os.flush (); | |
2718 | |
2719 if (os) | |
2720 retval = 0; | |
2721 else | |
4468 | 2722 error ("%s: write error", who.c_str ()); |
2117 | 2723 } |
2724 else | |
4468 | 2725 error ("%s: write error", who.c_str ()); |
2117 | 2726 } |
2727 else | |
4468 | 2728 invalid_operation (who, "writing"); |
2117 | 2729 |
2730 return retval; | |
2731 } | |
2732 | |
2733 // Return current error message for this stream. | |
2734 | |
3536 | 2735 std::string |
2435 | 2736 octave_base_stream::error (bool clear_err, int& err_num) |
2117 | 2737 { |
2435 | 2738 err_num = fail ? -1 : 0; |
2117 | 2739 |
3523 | 2740 std::string tmp = errmsg; |
2117 | 2741 |
2742 if (clear_err) | |
2743 clear (); | |
2744 | |
2745 return tmp; | |
2746 } | |
2747 | |
2748 void | |
4468 | 2749 octave_base_stream::invalid_operation (const std::string& who, const char *rw) |
2117 | 2750 { |
4468 | 2751 // Note that this is not ::error () ! |
2752 | |
6297 | 2753 error (who, std::string ("stream not open for ") + rw); |
2117 | 2754 } |
2755 | |
3552 | 2756 octave_stream::octave_stream (octave_base_stream *bs) |
3340 | 2757 : rep (bs) |
2758 { | |
2759 if (rep) | |
2760 rep->count = 1; | |
2761 } | |
2762 | |
2763 octave_stream::~octave_stream (void) | |
2764 { | |
2765 if (rep && --rep->count == 0) | |
2766 delete rep; | |
2767 } | |
2768 | |
2769 octave_stream::octave_stream (const octave_stream& s) | |
2770 : rep (s.rep) | |
2771 { | |
2772 if (rep) | |
2773 rep->count++; | |
2774 } | |
2775 | |
2776 octave_stream& | |
2777 octave_stream::operator = (const octave_stream& s) | |
2778 { | |
2779 if (rep != s.rep) | |
2780 { | |
2781 if (rep && --rep->count == 0) | |
2782 delete rep; | |
2783 | |
2784 rep = s.rep; | |
2785 | |
2786 if (rep) | |
2787 rep->count++; | |
2788 } | |
2789 | |
2790 return *this; | |
2791 } | |
2792 | |
2117 | 2793 int |
2794 octave_stream::flush (void) | |
2795 { | |
2796 int retval = -1; | |
2797 | |
5659 | 2798 if (stream_ok ()) |
2117 | 2799 retval = rep->flush (); |
2800 | |
2801 return retval; | |
2802 } | |
2803 | |
3536 | 2804 std::string |
5275 | 2805 octave_stream::getl (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 2806 { |
3523 | 2807 std::string retval; |
2117 | 2808 |
5659 | 2809 if (stream_ok ()) |
4468 | 2810 retval = rep->getl (max_len, err, who); |
2117 | 2811 |
2812 return retval; | |
2813 } | |
2814 | |
3536 | 2815 std::string |
4468 | 2816 octave_stream::getl (const octave_value& tc_max_len, bool& err, |
2817 const std::string& who) | |
2117 | 2818 { |
3523 | 2819 std::string retval; |
2117 | 2820 |
2821 err = false; | |
2822 | |
2823 int conv_err = 0; | |
2824 | |
6345 | 2825 int max_len = -1; |
2826 | |
2827 if (tc_max_len.is_defined ()) | |
2117 | 2828 { |
6345 | 2829 max_len = convert_to_valid_int (tc_max_len, conv_err); |
2830 | |
2831 if (conv_err || max_len < 0) | |
2832 { | |
2833 err = true; | |
2834 ::error ("%s: invalid maximum length specified", who.c_str ()); | |
2835 } | |
2117 | 2836 } |
6345 | 2837 |
2838 if (! error_state) | |
4468 | 2839 retval = getl (max_len, err, who); |
2117 | 2840 |
2841 return retval; | |
2842 } | |
2843 | |
3536 | 2844 std::string |
5275 | 2845 octave_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 2846 { |
3523 | 2847 std::string retval; |
2117 | 2848 |
5659 | 2849 if (stream_ok ()) |
4468 | 2850 retval = rep->gets (max_len, err, who); |
2117 | 2851 |
2852 return retval; | |
2853 } | |
2854 | |
3536 | 2855 std::string |
4468 | 2856 octave_stream::gets (const octave_value& tc_max_len, bool& err, |
2857 const std::string& who) | |
2117 | 2858 { |
3523 | 2859 std::string retval; |
2117 | 2860 |
2861 err = false; | |
2862 | |
2863 int conv_err = 0; | |
2864 | |
6345 | 2865 int max_len = -1; |
2866 | |
2867 if (tc_max_len.is_defined ()) | |
2117 | 2868 { |
6345 | 2869 max_len = convert_to_valid_int (tc_max_len, conv_err); |
2870 | |
2871 if (conv_err || max_len < 0) | |
2872 { | |
2873 err = true; | |
2874 ::error ("%s: invalid maximum length specified", who.c_str ()); | |
2875 } | |
2117 | 2876 } |
6345 | 2877 |
2878 if (! error_state) | |
4468 | 2879 retval = gets (max_len, err, who); |
2117 | 2880 |
2881 return retval; | |
2882 } | |
2883 | |
2884 int | |
4797 | 2885 octave_stream::seek (long offset, int origin) |
2117 | 2886 { |
5065 | 2887 int status = -1; |
2117 | 2888 |
5659 | 2889 if (stream_ok ()) |
4889 | 2890 { |
2891 clearerr (); | |
2892 | |
5065 | 2893 long orig_pos = rep->tell (); |
2894 | |
2895 status = rep->seek (offset, origin); | |
2896 | |
2897 if (status == 0) | |
2898 { | |
2899 long save_pos = rep->tell (); | |
2900 | |
2901 rep->seek (0, SEEK_END); | |
2902 | |
2903 long pos_eof = rep->tell (); | |
2904 | |
2905 // I don't think save_pos can be less than zero, but we'll | |
2906 // check anyway... | |
2907 | |
2908 if (save_pos > pos_eof || save_pos < 0) | |
2909 { | |
2910 // Seek outside bounds of file. Failure should leave | |
2911 // position unchanged. | |
2912 | |
2913 rep->seek (orig_pos, SEEK_SET); | |
2914 | |
2915 status = -1; | |
2916 } | |
2917 else | |
2918 { | |
2919 // Is it possible for this to fail? We are just | |
2920 // returning to a position after the first successful | |
2921 // seek. | |
2922 | |
2923 rep->seek (save_pos, SEEK_SET); | |
2924 } | |
2925 } | |
4889 | 2926 } |
2117 | 2927 |
5065 | 2928 return status; |
2117 | 2929 } |
2930 | |
2931 int | |
2932 octave_stream::seek (const octave_value& tc_offset, | |
2933 const octave_value& tc_origin) | |
2934 { | |
2935 int retval = -1; | |
2936 | |
4797 | 2937 long xoffset = tc_offset.long_value (true); |
4645 | 2938 |
2939 if (! error_state) | |
2117 | 2940 { |
4645 | 2941 int conv_err = 0; |
2942 | |
4797 | 2943 int origin = SEEK_SET; |
2117 | 2944 |
2341 | 2945 if (tc_origin.is_string ()) |
2117 | 2946 { |
3523 | 2947 std::string xorigin = tc_origin.string_value (); |
2341 | 2948 |
2949 if (xorigin == "bof") | |
4797 | 2950 origin = SEEK_SET; |
2341 | 2951 else if (xorigin == "cof") |
4797 | 2952 origin = SEEK_CUR; |
2341 | 2953 else if (xorigin == "eof") |
4797 | 2954 origin = SEEK_END; |
2117 | 2955 else |
2956 conv_err = -1; | |
2957 } | |
2341 | 2958 else |
2959 { | |
2960 int xorigin = convert_to_valid_int (tc_origin, conv_err); | |
2961 | |
2962 if (! conv_err) | |
2963 { | |
2964 if (xorigin == -1) | |
4797 | 2965 origin = SEEK_SET; |
2341 | 2966 else if (xorigin == 0) |
4797 | 2967 origin = SEEK_CUR; |
2341 | 2968 else if (xorigin == 1) |
4797 | 2969 origin = SEEK_END; |
2341 | 2970 else |
2971 conv_err = -1; | |
2972 } | |
2973 } | |
2117 | 2974 |
2975 if (! conv_err) | |
5065 | 2976 { |
2977 retval = seek (xoffset, origin); | |
2978 | |
2979 if (retval != 0) | |
2980 error ("fseek: failed to seek to requested position"); | |
2981 } | |
2117 | 2982 else |
2983 error ("fseek: invalid value for origin"); | |
2984 } | |
2985 else | |
2986 error ("fseek: invalid value for offset"); | |
2987 | |
2988 return retval; | |
2989 } | |
2990 | |
4797 | 2991 long |
2992 octave_stream::tell (void) | |
2117 | 2993 { |
4797 | 2994 long retval = -1; |
2117 | 2995 |
5659 | 2996 if (stream_ok ()) |
2117 | 2997 retval = rep->tell (); |
2998 | |
2999 return retval; | |
3000 } | |
3001 | |
3002 int | |
3003 octave_stream::rewind (void) | |
3004 { | |
6296 | 3005 return seek (0, SEEK_SET); |
2117 | 3006 } |
3007 | |
3340 | 3008 bool |
3009 octave_stream::is_open (void) const | |
3010 { | |
3011 bool retval = false; | |
3012 | |
5659 | 3013 if (stream_ok ()) |
3340 | 3014 retval = rep->is_open (); |
3015 | |
3016 return retval; | |
3017 } | |
3018 | |
3019 void | |
3020 octave_stream::close (void) | |
3021 { | |
5659 | 3022 if (stream_ok ()) |
3340 | 3023 rep->close (); |
3024 } | |
3025 | |
4944 | 3026 template <class RET_T, class READ_T> |
2117 | 3027 octave_value |
5275 | 3028 do_read (octave_stream& strm, octave_idx_type nr, octave_idx_type nc, octave_idx_type block_size, |
3029 octave_idx_type skip, bool do_float_fmt_conv, | |
3030 oct_mach_info::float_format from_flt_fmt, octave_idx_type& count) | |
2117 | 3031 { |
3032 octave_value retval; | |
3033 | |
4944 | 3034 RET_T nda; |
3035 | |
3036 count = 0; | |
3037 | |
3038 typename octave_array_type_traits<RET_T>::element_type elt_zero | |
3039 = typename octave_array_type_traits<RET_T>::element_type (); | |
3040 | |
3041 typename octave_array_type_traits<RET_T>::element_type *dat = 0; | |
3042 | |
5275 | 3043 octave_idx_type max_size = 0; |
3044 | |
3045 octave_idx_type final_nr = 0; | |
3046 octave_idx_type final_nc = 1; | |
4944 | 3047 |
3048 if (nr > 0) | |
3049 { | |
3050 if (nc > 0) | |
3051 { | |
3052 nda.resize (dim_vector (nr, nc), elt_zero); | |
3053 dat = nda.fortran_vec (); | |
3054 max_size = nr * nc; | |
3055 } | |
3056 else | |
3057 { | |
3058 nda.resize (dim_vector (nr, 32), elt_zero); | |
3059 dat = nda.fortran_vec (); | |
3060 max_size = nr * 32; | |
3061 } | |
3062 } | |
3063 else | |
3064 { | |
3065 nda.resize (dim_vector (32, 1), elt_zero); | |
3066 dat = nda.fortran_vec (); | |
3067 max_size = 32; | |
3068 } | |
3069 | |
5775 | 3070 // FIXME -- byte order for Cray? |
4944 | 3071 |
3072 bool swap = false; | |
3073 | |
3074 if (oct_mach_info::words_big_endian ()) | |
3075 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian | |
3076 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g | |
3077 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g); | |
3078 else | |
3079 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); | |
3080 | |
3081 union | |
3082 { | |
3083 char buf[sizeof (typename octave_type_traits<READ_T>::val_type)]; | |
3084 typename octave_type_traits<READ_T>::val_type val; | |
3085 } u; | |
3086 | |
3087 std::istream *isp = strm.input_stream (); | |
3088 | |
3089 if (isp) | |
3090 { | |
3091 std::istream& is = *isp; | |
3092 | |
5275 | 3093 octave_idx_type elts_read = 0; |
4944 | 3094 |
3095 for (;;) | |
3096 { | |
5775 | 3097 // FIXME -- maybe there should be a special case for |
4944 | 3098 // skip == 0. |
3099 | |
3100 if (is) | |
3101 { | |
3102 if (nr > 0 && nc > 0 && count == max_size) | |
3103 { | |
3104 final_nr = nr; | |
3105 final_nc = nc; | |
3106 | |
3107 break; | |
3108 } | |
3109 | |
3110 is.read (u.buf, sizeof (typename octave_type_traits<READ_T>::val_type)); | |
3111 | |
3112 // We only swap bytes for integer types. For float | |
3113 // types, the format conversion will also handle byte | |
3114 // swapping. | |
3115 | |
3116 if (swap) | |
3117 swap_bytes<sizeof (typename octave_type_traits<READ_T>::val_type)> (u.buf); | |
3118 else if (do_float_fmt_conv) | |
3119 do_float_format_conversion | |
3120 (u.buf, | |
3121 sizeof (typename octave_type_traits<READ_T>::val_type), | |
3122 1, from_flt_fmt, oct_mach_info::float_format ()); | |
3123 | |
3124 typename octave_array_type_traits<RET_T>::element_type tmp | |
3125 = static_cast <typename octave_array_type_traits<RET_T>::element_type> (u.val); | |
3126 | |
5030 | 3127 if (is) |
4944 | 3128 { |
5030 | 3129 if (count == max_size) |
4944 | 3130 { |
5030 | 3131 max_size *= 2; |
3132 | |
3133 if (nr > 0) | |
3134 nda.resize (dim_vector (nr, max_size / nr), | |
3135 elt_zero); | |
3136 else | |
3137 nda.resize (dim_vector (max_size, 1), elt_zero); | |
3138 | |
3139 dat = nda.fortran_vec (); | |
4944 | 3140 } |
3141 | |
5030 | 3142 dat[count++] = tmp; |
3143 | |
3144 elts_read++; | |
3145 } | |
3146 | |
7538
2c4b0cbda85a
oct-stream.cc (do_read): stop reading if seek fails
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
3147 int seek_status = 0; |
2c4b0cbda85a
oct-stream.cc (do_read): stop reading if seek fails
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
3148 |
5030 | 3149 if (skip != 0 && elts_read == block_size) |
3150 { | |
7538
2c4b0cbda85a
oct-stream.cc (do_read): stop reading if seek fails
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
3151 seek_status = strm.seek (skip, SEEK_CUR); |
5030 | 3152 elts_read = 0; |
3153 } | |
3154 | |
7538
2c4b0cbda85a
oct-stream.cc (do_read): stop reading if seek fails
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
3155 if (is.eof () || seek_status < 0) |
5030 | 3156 { |
3157 if (nr > 0) | |
4944 | 3158 { |
5030 | 3159 if (count > nr) |
4944 | 3160 { |
5030 | 3161 final_nr = nr; |
3162 final_nc = (count - 1) / nr + 1; | |
4944 | 3163 } |
3164 else | |
3165 { | |
3166 final_nr = count; | |
3167 final_nc = 1; | |
3168 } | |
3169 } | |
5030 | 3170 else |
3171 { | |
3172 final_nr = count; | |
3173 final_nc = 1; | |
3174 } | |
3175 | |
4944 | 3176 break; |
3177 } | |
3178 } | |
5030 | 3179 else if (is.eof ()) |
3180 break; | |
4944 | 3181 } |
3182 } | |
3183 | |
5030 | 3184 nda.resize (dim_vector (final_nr, final_nc), elt_zero); |
3185 | |
3186 retval = nda; | |
4944 | 3187 |
3188 return retval; | |
3189 } | |
3190 | |
3191 #define DO_READ_VAL_TEMPLATE(RET_T, READ_T) \ | |
3192 template octave_value \ | |
5275 | 3193 do_read<RET_T, READ_T> (octave_stream&, octave_idx_type, octave_idx_type, octave_idx_type, octave_idx_type, bool, \ |
3194 oct_mach_info::float_format, octave_idx_type&) | |
4944 | 3195 |
5775 | 3196 // FIXME -- should we only have float if it is a different |
4944 | 3197 // size from double? |
3198 | |
3199 #define INSTANTIATE_DO_READ(VAL_T) \ | |
3200 DO_READ_VAL_TEMPLATE (VAL_T, octave_int8); \ | |
3201 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint8); \ | |
3202 DO_READ_VAL_TEMPLATE (VAL_T, octave_int16); \ | |
3203 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint16); \ | |
3204 DO_READ_VAL_TEMPLATE (VAL_T, octave_int32); \ | |
3205 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint32); \ | |
3206 DO_READ_VAL_TEMPLATE (VAL_T, octave_int64); \ | |
3207 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint64); \ | |
3208 DO_READ_VAL_TEMPLATE (VAL_T, float); \ | |
3209 DO_READ_VAL_TEMPLATE (VAL_T, double); \ | |
3210 DO_READ_VAL_TEMPLATE (VAL_T, char); \ | |
3211 DO_READ_VAL_TEMPLATE (VAL_T, signed char); \ | |
3212 DO_READ_VAL_TEMPLATE (VAL_T, unsigned char) | |
3213 | |
4970 | 3214 INSTANTIATE_DO_READ (int8NDArray); |
3215 INSTANTIATE_DO_READ (uint8NDArray); | |
3216 INSTANTIATE_DO_READ (int16NDArray); | |
3217 INSTANTIATE_DO_READ (uint16NDArray); | |
3218 INSTANTIATE_DO_READ (int32NDArray); | |
3219 INSTANTIATE_DO_READ (uint32NDArray); | |
3220 INSTANTIATE_DO_READ (int64NDArray); | |
3221 INSTANTIATE_DO_READ (uint64NDArray); | |
3222 // INSTANTIATE_DO_READ (floatNDArray); | |
3223 INSTANTIATE_DO_READ (NDArray); | |
3224 INSTANTIATE_DO_READ (charNDArray); | |
5780 | 3225 INSTANTIATE_DO_READ (boolNDArray); |
4944 | 3226 |
5275 | 3227 typedef octave_value (*read_fptr) (octave_stream&, octave_idx_type, octave_idx_type, octave_idx_type, octave_idx_type, bool, |
3228 oct_mach_info::float_format ffmt, octave_idx_type&); | |
4944 | 3229 |
7433 | 3230 NO_INSTANTIATE_ARRAY_SORT (read_fptr); |
6708 | 3231 INSTANTIATE_ARRAY (read_fptr,); |
4944 | 3232 template class Array2<read_fptr>; |
3233 | |
3234 #define FILL_TABLE_ROW(R, VAL_T) \ | |
3235 read_fptr_table(R,oct_data_conv::dt_int8) = do_read<VAL_T, octave_int8>; \ | |
3236 read_fptr_table(R,oct_data_conv::dt_uint8) = do_read<VAL_T, octave_uint8>; \ | |
3237 read_fptr_table(R,oct_data_conv::dt_int16) = do_read<VAL_T, octave_int16>; \ | |
3238 read_fptr_table(R,oct_data_conv::dt_uint16) = do_read<VAL_T, octave_uint16>; \ | |
3239 read_fptr_table(R,oct_data_conv::dt_int32) = do_read<VAL_T, octave_int32>; \ | |
3240 read_fptr_table(R,oct_data_conv::dt_uint32) = do_read<VAL_T, octave_uint32>; \ | |
3241 read_fptr_table(R,oct_data_conv::dt_int64) = do_read<VAL_T, octave_int64>; \ | |
3242 read_fptr_table(R,oct_data_conv::dt_uint64) = do_read<VAL_T, octave_uint64>; \ | |
3243 read_fptr_table(R,oct_data_conv::dt_single) = do_read<VAL_T, float>; \ | |
3244 read_fptr_table(R,oct_data_conv::dt_double) = do_read<VAL_T, double>; \ | |
3245 read_fptr_table(R,oct_data_conv::dt_char) = do_read<VAL_T, char>; \ | |
3246 read_fptr_table(R,oct_data_conv::dt_schar) = do_read<VAL_T, signed char>; \ | |
4970 | 3247 read_fptr_table(R,oct_data_conv::dt_uchar) = do_read<VAL_T, unsigned char>; \ |
3248 read_fptr_table(R,oct_data_conv::dt_logical) = do_read<VAL_T, unsigned char> | |
4944 | 3249 |
3250 octave_value | |
5275 | 3251 octave_stream::read (const Array<double>& size, octave_idx_type block_size, |
4944 | 3252 oct_data_conv::data_type input_type, |
3253 oct_data_conv::data_type output_type, | |
5275 | 3254 octave_idx_type skip, oct_mach_info::float_format ffmt, |
3255 octave_idx_type& char_count) | |
4944 | 3256 { |
3257 static bool initialized = false; | |
3258 | |
3259 // Table function pointers for return types x read types. | |
3260 | |
4970 | 3261 static Array2<read_fptr> read_fptr_table (oct_data_conv::dt_unknown, 14, 0); |
4944 | 3262 |
3263 if (! initialized) | |
3264 { | |
3265 FILL_TABLE_ROW (oct_data_conv::dt_int8, int8NDArray); | |
3266 FILL_TABLE_ROW (oct_data_conv::dt_uint8, uint8NDArray); | |
3267 FILL_TABLE_ROW (oct_data_conv::dt_int16, int16NDArray); | |
3268 FILL_TABLE_ROW (oct_data_conv::dt_uint16, uint16NDArray); | |
3269 FILL_TABLE_ROW (oct_data_conv::dt_int32, int32NDArray); | |
3270 FILL_TABLE_ROW (oct_data_conv::dt_uint32, uint32NDArray); | |
3271 FILL_TABLE_ROW (oct_data_conv::dt_int64, int64NDArray); | |
3272 FILL_TABLE_ROW (oct_data_conv::dt_uint64, uint64NDArray); | |
7724
932b0cf51834
octave_stream::read: allow single data type specification but return double
John W. Eaton <jwe@octave.org>
parents:
7709
diff
changeset
|
3273 // FIXME -- the following line allows things like int8=>single |
932b0cf51834
octave_stream::read: allow single data type specification but return double
John W. Eaton <jwe@octave.org>
parents:
7709
diff
changeset
|
3274 // to work, but they will actually return a double value. We |
932b0cf51834
octave_stream::read: allow single data type specification but return double
John W. Eaton <jwe@octave.org>
parents:
7709
diff
changeset
|
3275 // need a floatNDArray for this to work properly. |
932b0cf51834
octave_stream::read: allow single data type specification but return double
John W. Eaton <jwe@octave.org>
parents:
7709
diff
changeset
|
3276 FILL_TABLE_ROW (oct_data_conv::dt_single, NDArray); |
4944 | 3277 FILL_TABLE_ROW (oct_data_conv::dt_double, NDArray); |
3278 FILL_TABLE_ROW (oct_data_conv::dt_char, charNDArray); | |
3279 FILL_TABLE_ROW (oct_data_conv::dt_schar, charNDArray); | |
3280 FILL_TABLE_ROW (oct_data_conv::dt_uchar, charNDArray); | |
4970 | 3281 FILL_TABLE_ROW (oct_data_conv::dt_logical, boolNDArray); |
4944 | 3282 |
3283 initialized = true; | |
3284 } | |
3285 | |
3286 octave_value retval; | |
3287 | |
5659 | 3288 if (stream_ok ()) |
4944 | 3289 { |
5775 | 3290 // FIXME -- we may eventually want to make this extensible. |
3291 | |
3292 // FIXME -- we need a better way to ensure that this | |
4944 | 3293 // numbering stays consistent with the order of the elements in the |
3294 // data_type enum in the oct_data_conv class. | |
3295 | |
3296 char_count = 0; | |
3297 | |
5275 | 3298 octave_idx_type nr = -1; |
3299 octave_idx_type nc = -1; | |
4944 | 3300 |
3301 bool ignore; | |
3302 | |
3303 get_size (size, nr, nc, ignore, "fread"); | |
3304 | |
3305 if (! error_state) | |
3306 { | |
3307 if (nr == 0 || nc == 0) | |
3308 retval = Matrix (nr, nc); | |
3309 else | |
3310 { | |
3311 if (ffmt == oct_mach_info::flt_fmt_unknown) | |
3312 ffmt = float_format (); | |
3313 | |
3314 read_fptr fcn = read_fptr_table (output_type, input_type); | |
3315 | |
3316 bool do_float_fmt_conv = ((input_type == oct_data_conv::dt_double | |
3317 || input_type == oct_data_conv::dt_single) | |
3318 && ffmt != float_format ()); | |
3319 | |
3320 if (fcn) | |
3321 { | |
3322 retval = (*fcn) (*this, nr, nc, block_size, skip, | |
3323 do_float_fmt_conv, ffmt, char_count); | |
3324 | |
5775 | 3325 // FIXME -- kluge! |
4944 | 3326 |
3327 if (! error_state | |
3328 && (output_type == oct_data_conv::dt_char | |
3329 || output_type == oct_data_conv::dt_schar | |
3330 || output_type == oct_data_conv::dt_uchar)) | |
3331 retval = octave_value (retval.char_matrix_value (), true); | |
3332 } | |
3333 else | |
3334 error ("fread: unable to read and convert requested types"); | |
3335 } | |
3336 } | |
3337 else | |
3338 invalid_operation ("fread", "reading"); | |
3339 } | |
2117 | 3340 |
3341 return retval; | |
3342 } | |
3343 | |
5275 | 3344 octave_idx_type |
3345 octave_stream::write (const octave_value& data, octave_idx_type block_size, | |
3346 oct_data_conv::data_type output_type, octave_idx_type skip, | |
2317 | 3347 oct_mach_info::float_format flt_fmt) |
2117 | 3348 { |
5275 | 3349 octave_idx_type retval = -1; |
2117 | 3350 |
5659 | 3351 if (stream_ok ()) |
4944 | 3352 { |
3353 if (! error_state) | |
3354 { | |
3355 if (flt_fmt == oct_mach_info::flt_fmt_unknown) | |
3356 flt_fmt = float_format (); | |
3357 | |
5275 | 3358 octave_idx_type status = data.write (*this, block_size, output_type, |
4944 | 3359 skip, flt_fmt); |
3360 | |
3361 if (status < 0) | |
3362 error ("fwrite: write error"); | |
3363 else | |
3364 retval = status; | |
3365 } | |
3366 else | |
3367 invalid_operation ("fwrite", "writing"); | |
3368 } | |
2117 | 3369 |
3370 return retval; | |
3371 } | |
3372 | |
4944 | 3373 template <class T> |
3374 void | |
3375 write_int (std::ostream& os, bool swap, const T& val) | |
3376 { | |
3377 typename octave_type_traits<T>::val_type tmp = val.value (); | |
3378 | |
3379 if (swap) | |
3380 swap_bytes<sizeof (typename octave_type_traits<T>::val_type)> (&tmp); | |
3381 | |
3382 os.write (reinterpret_cast<const char *> (&tmp), | |
3383 sizeof (typename octave_type_traits<T>::val_type)); | |
3384 } | |
3385 | |
3386 template void write_int (std::ostream&, bool, const octave_int8&); | |
3387 template void write_int (std::ostream&, bool, const octave_uint8&); | |
3388 template void write_int (std::ostream&, bool, const octave_int16&); | |
3389 template void write_int (std::ostream&, bool, const octave_uint16&); | |
3390 template void write_int (std::ostream&, bool, const octave_int32&); | |
3391 template void write_int (std::ostream&, bool, const octave_uint32&); | |
3392 template void write_int (std::ostream&, bool, const octave_int64&); | |
3393 template void write_int (std::ostream&, bool, const octave_uint64&); | |
3394 | |
3395 template <class T> | |
3396 static inline bool | |
3397 do_write (std::ostream& os, const T& val, oct_data_conv::data_type output_type, | |
3398 oct_mach_info::float_format flt_fmt, bool swap, | |
3399 bool do_float_conversion) | |
3400 { | |
3401 bool retval = true; | |
3402 | |
3403 // For compatibility, Octave converts to the output type, then | |
3404 // writes. This means that truncation happens on the conversion. | |
3405 // For example, the following program prints 0: | |
3406 // | |
3407 // x = int8 (-1) | |
3408 // f = fopen ("foo.dat", "w"); | |
3409 // fwrite (f, x, "unsigned char"); | |
3410 // fclose (f); | |
3411 // f = fopen ("foo.dat", "r"); | |
3412 // y = fread (f, 1, "unsigned char"); | |
3413 // printf ("%d\n", y); | |
3414 | |
3415 switch (output_type) | |
3416 { | |
3417 case oct_data_conv::dt_char: | |
3418 case oct_data_conv::dt_schar: | |
3419 case oct_data_conv::dt_int8: | |
3420 write_int (os, swap, octave_int8 (val)); | |
3421 break; | |
3422 | |
3423 case oct_data_conv::dt_uchar: | |
3424 case oct_data_conv::dt_uint8: | |
3425 write_int (os, swap, octave_uint8 (val)); | |
3426 break; | |
3427 | |
3428 case oct_data_conv::dt_int16: | |
3429 write_int (os, swap, octave_int16 (val)); | |
3430 break; | |
3431 | |
3432 case oct_data_conv::dt_uint16: | |
3433 write_int (os, swap, octave_uint16 (val)); | |
3434 break; | |
3435 | |
3436 case oct_data_conv::dt_int32: | |
3437 write_int (os, swap, octave_int32 (val)); | |
3438 break; | |
3439 | |
3440 case oct_data_conv::dt_uint32: | |
3441 write_int (os, swap, octave_uint32 (val)); | |
3442 break; | |
3443 | |
3444 case oct_data_conv::dt_int64: | |
3445 write_int (os, swap, octave_int64 (val)); | |
3446 break; | |
3447 | |
3448 case oct_data_conv::dt_uint64: | |
3449 write_int (os, swap, octave_uint64 (val)); | |
3450 break; | |
3451 | |
3452 case oct_data_conv::dt_single: | |
3453 { | |
3454 float f = static_cast<float> (val); | |
3455 | |
3456 if (do_float_conversion) | |
3457 do_float_format_conversion (&f, 1, flt_fmt); | |
3458 | |
3459 os.write (reinterpret_cast<const char *> (&f), sizeof (float)); | |
3460 } | |
3461 break; | |
3462 | |
3463 case oct_data_conv::dt_double: | |
3464 { | |
3465 double d = static_cast<double> (val); | |
3466 if (do_float_conversion) | |
3467 do_double_format_conversion (&d, 1, flt_fmt); | |
3468 | |
3469 os.write (reinterpret_cast<const char *> (&d), sizeof (double)); | |
3470 } | |
3471 break; | |
3472 | |
3473 default: | |
3474 retval = false; | |
3475 (*current_liboctave_error_handler) | |
3476 ("write: invalid type specification"); | |
3477 break; | |
3478 } | |
3479 | |
3480 return retval; | |
3481 } | |
3482 | |
5887 | 3483 template bool |
3484 do_write (std::ostream&, const octave_int8&, oct_data_conv::data_type, | |
3485 oct_mach_info::float_format, bool, bool); | |
3486 | |
3487 template bool | |
3488 do_write (std::ostream&, const octave_uint8&, oct_data_conv::data_type, | |
3489 oct_mach_info::float_format, bool, bool); | |
3490 | |
3491 template bool | |
3492 do_write (std::ostream&, const octave_int16&, oct_data_conv::data_type, | |
3493 oct_mach_info::float_format, bool, bool); | |
3494 | |
3495 template bool | |
3496 do_write (std::ostream&, const octave_uint16&, oct_data_conv::data_type, | |
3497 oct_mach_info::float_format, bool, bool); | |
3498 | |
3499 template bool | |
3500 do_write (std::ostream&, const octave_int32&, oct_data_conv::data_type, | |
3501 oct_mach_info::float_format, bool, bool); | |
3502 | |
3503 template bool | |
3504 do_write (std::ostream&, const octave_uint32&, oct_data_conv::data_type, | |
3505 oct_mach_info::float_format, bool, bool); | |
3506 | |
3507 template bool | |
3508 do_write (std::ostream&, const octave_int64&, oct_data_conv::data_type, | |
3509 oct_mach_info::float_format, bool, bool); | |
3510 | |
3511 template bool | |
3512 do_write (std::ostream&, const octave_uint64&, oct_data_conv::data_type, | |
3513 oct_mach_info::float_format, bool, bool); | |
3514 | |
4944 | 3515 template <class T> |
5275 | 3516 octave_idx_type |
3517 octave_stream::write (const Array<T>& data, octave_idx_type block_size, | |
4944 | 3518 oct_data_conv::data_type output_type, |
5275 | 3519 octave_idx_type skip, oct_mach_info::float_format flt_fmt) |
4944 | 3520 { |
5275 | 3521 octave_idx_type retval = -1; |
4944 | 3522 |
3523 bool status = true; | |
3524 | |
5275 | 3525 octave_idx_type count = 0; |
4944 | 3526 |
3527 const T *d = data.data (); | |
3528 | |
5275 | 3529 octave_idx_type n = data.length (); |
4944 | 3530 |
3531 oct_mach_info::float_format native_flt_fmt | |
3532 = oct_mach_info::float_format (); | |
3533 | |
3534 bool do_float_conversion = (flt_fmt != native_flt_fmt); | |
3535 | |
5775 | 3536 // FIXME -- byte order for Cray? |
4944 | 3537 |
3538 bool swap = false; | |
3539 | |
3540 if (oct_mach_info::words_big_endian ()) | |
3541 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian | |
3542 || flt_fmt == oct_mach_info::flt_fmt_vax_g | |
3543 || flt_fmt == oct_mach_info::flt_fmt_vax_g); | |
3544 else | |
3545 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); | |
3546 | |
5275 | 3547 for (octave_idx_type i = 0; i < n; i++) |
4944 | 3548 { |
3549 std::ostream *osp = output_stream (); | |
3550 | |
3551 if (osp) | |
3552 { | |
3553 std::ostream& os = *osp; | |
3554 | |
5254 | 3555 // It seems that Matlab writes zeros instead of actually |
3556 // seeking. Hmm... | |
3557 | |
4944 | 3558 if (skip != 0 && (i % block_size) == 0) |
5254 | 3559 { |
5775 | 3560 // FIXME -- probably should try to write larger |
5254 | 3561 // blocks... |
3562 | |
3563 unsigned char zero = 0; | |
3564 for (int j = 0; j < skip; j++) | |
3565 os.write (reinterpret_cast<const char *> (&zero), 1); | |
3566 } | |
4944 | 3567 |
3568 if (os) | |
3569 { | |
3570 status = do_write (os, d[i], output_type, flt_fmt, swap, | |
3571 do_float_conversion); | |
3572 | |
3573 if (os && status) | |
3574 count++; | |
3575 else | |
3576 break; | |
3577 } | |
3578 else | |
3579 { | |
3580 status = false; | |
3581 break; | |
3582 } | |
3583 } | |
3584 else | |
3585 { | |
3586 status = false; | |
3587 break; | |
3588 } | |
3589 } | |
3590 | |
3591 if (status) | |
3592 retval = count; | |
3593 | |
3594 return retval; | |
3595 } | |
3596 | |
5275 | 3597 template octave_idx_type |
3598 octave_stream::write (const Array<char>&, octave_idx_type, | |
4944 | 3599 oct_data_conv::data_type, |
5275 | 3600 octave_idx_type, oct_mach_info::float_format); |
3601 | |
3602 template octave_idx_type | |
3603 octave_stream::write (const Array<bool>&, octave_idx_type, | |
4970 | 3604 oct_data_conv::data_type, |
5275 | 3605 octave_idx_type, oct_mach_info::float_format); |
3606 | |
3607 template octave_idx_type | |
3608 octave_stream::write (const Array<double>&, octave_idx_type, | |
4944 | 3609 oct_data_conv::data_type, |
5275 | 3610 octave_idx_type, oct_mach_info::float_format); |
3611 | |
3612 template octave_idx_type | |
3613 octave_stream::write (const Array<octave_int8>&, octave_idx_type, | |
4944 | 3614 oct_data_conv::data_type, |
5275 | 3615 octave_idx_type, oct_mach_info::float_format); |
3616 | |
3617 template octave_idx_type | |
3618 octave_stream::write (const Array<octave_uint8>&, octave_idx_type, | |
4944 | 3619 oct_data_conv::data_type, |
5275 | 3620 octave_idx_type, oct_mach_info::float_format); |
3621 | |
3622 template octave_idx_type | |
3623 octave_stream::write (const Array<octave_int16>&, octave_idx_type, | |
4944 | 3624 oct_data_conv::data_type, |
5275 | 3625 octave_idx_type, oct_mach_info::float_format); |
3626 | |
3627 template octave_idx_type | |
3628 octave_stream::write (const Array<octave_uint16>&, octave_idx_type, | |
4944 | 3629 oct_data_conv::data_type, |
5275 | 3630 octave_idx_type, oct_mach_info::float_format); |
3631 | |
3632 template octave_idx_type | |
3633 octave_stream::write (const Array<octave_int32>&, octave_idx_type, | |
4944 | 3634 oct_data_conv::data_type, |
5275 | 3635 octave_idx_type, oct_mach_info::float_format); |
3636 | |
3637 template octave_idx_type | |
3638 octave_stream::write (const Array<octave_uint32>&, octave_idx_type, | |
4944 | 3639 oct_data_conv::data_type, |
5275 | 3640 octave_idx_type, oct_mach_info::float_format); |
3641 | |
3642 template octave_idx_type | |
3643 octave_stream::write (const Array<octave_int64>&, octave_idx_type, | |
4944 | 3644 oct_data_conv::data_type, |
5275 | 3645 octave_idx_type, oct_mach_info::float_format); |
3646 | |
3647 template octave_idx_type | |
3648 octave_stream::write (const Array<octave_uint64>&, octave_idx_type, | |
4944 | 3649 oct_data_conv::data_type, |
5275 | 3650 octave_idx_type, oct_mach_info::float_format); |
4944 | 3651 |
2117 | 3652 octave_value |
3810 | 3653 octave_stream::scanf (const std::string& fmt, const Array<double>& size, |
5275 | 3654 octave_idx_type& count, const std::string& who) |
2117 | 3655 { |
3656 octave_value retval; | |
3657 | |
5659 | 3658 if (stream_ok ()) |
4468 | 3659 retval = rep->scanf (fmt, size, count, who); |
2117 | 3660 |
3661 return retval; | |
3662 } | |
3663 | |
5279 | 3664 octave_value |
3665 octave_stream::scanf (const octave_value& fmt, const Array<double>& size, | |
5299 | 3666 octave_idx_type& count, const std::string& who) |
5279 | 3667 { |
3668 octave_value retval = Matrix (); | |
3669 | |
3670 if (fmt.is_string ()) | |
3671 { | |
3672 std::string sfmt = fmt.string_value (); | |
3673 | |
3674 if (fmt.is_sq_string ()) | |
3675 sfmt = do_string_escapes (sfmt); | |
3676 | |
3677 retval = scanf (sfmt, size, count, who); | |
3678 } | |
3679 else | |
3680 { | |
3681 // Note that this is not ::error () ! | |
3682 | |
3683 error (who + ": format must be a string"); | |
3684 } | |
3685 | |
3686 return retval; | |
3687 } | |
3688 | |
2215 | 3689 octave_value_list |
4468 | 3690 octave_stream::oscanf (const std::string& fmt, const std::string& who) |
2215 | 3691 { |
3692 octave_value_list retval; | |
3693 | |
5659 | 3694 if (stream_ok ()) |
4468 | 3695 retval = rep->oscanf (fmt, who); |
2215 | 3696 |
3697 return retval; | |
3698 } | |
3699 | |
5279 | 3700 octave_value_list |
3701 octave_stream::oscanf (const octave_value& fmt, const std::string& who) | |
3702 { | |
3703 octave_value_list retval; | |
3704 | |
3705 if (fmt.is_string ()) | |
3706 { | |
3707 std::string sfmt = fmt.string_value (); | |
3708 | |
3709 if (fmt.is_sq_string ()) | |
3710 sfmt = do_string_escapes (sfmt); | |
3711 | |
3712 retval = oscanf (sfmt, who); | |
3713 } | |
3714 else | |
3715 { | |
3716 // Note that this is not ::error () ! | |
3717 | |
3718 error (who + ": format must be a string"); | |
3719 } | |
3720 | |
3721 return retval; | |
3722 } | |
3723 | |
2117 | 3724 int |
4468 | 3725 octave_stream::printf (const std::string& fmt, const octave_value_list& args, |
3726 const std::string& who) | |
2117 | 3727 { |
3728 int retval = -1; | |
3729 | |
5659 | 3730 if (stream_ok ()) |
4468 | 3731 retval = rep->printf (fmt, args, who); |
2117 | 3732 |
3733 return retval; | |
3734 } | |
3735 | |
3736 int | |
5279 | 3737 octave_stream::printf (const octave_value& fmt, const octave_value_list& args, |
3738 const std::string& who) | |
3739 { | |
3740 int retval = 0; | |
3741 | |
3742 if (fmt.is_string ()) | |
3743 { | |
3744 std::string sfmt = fmt.string_value (); | |
3745 | |
3746 if (fmt.is_sq_string ()) | |
3747 sfmt = do_string_escapes (sfmt); | |
3748 | |
3749 retval = printf (sfmt, args, who); | |
3750 } | |
3751 else | |
3752 { | |
3753 // Note that this is not ::error () ! | |
3754 | |
3755 error (who + ": format must be a string"); | |
3756 } | |
3757 | |
3758 return retval; | |
3759 } | |
3760 | |
3761 int | |
4468 | 3762 octave_stream::puts (const std::string& s, const std::string& who) |
2117 | 3763 { |
3764 int retval = -1; | |
3765 | |
5659 | 3766 if (stream_ok ()) |
4468 | 3767 retval = rep->puts (s, who); |
2117 | 3768 |
3769 return retval; | |
3770 } | |
3771 | |
5775 | 3772 // FIXME -- maybe this should work for string arrays too. |
2117 | 3773 |
3774 int | |
4468 | 3775 octave_stream::puts (const octave_value& tc_s, const std::string& who) |
2117 | 3776 { |
3777 int retval = -1; | |
3778 | |
3779 if (tc_s.is_string ()) | |
3780 { | |
3523 | 3781 std::string s = tc_s.string_value (); |
5279 | 3782 retval = puts (s, who); |
2117 | 3783 } |
3784 else | |
4468 | 3785 { |
3786 // Note that this is not ::error () ! | |
3787 | |
3788 error (who + ": argument must be a string"); | |
3789 } | |
2117 | 3790 |
3791 return retval; | |
3792 } | |
3793 | |
3794 bool | |
3795 octave_stream::eof (void) const | |
3796 { | |
3797 int retval = -1; | |
3798 | |
5659 | 3799 if (stream_ok ()) |
2117 | 3800 retval = rep->eof (); |
3801 | |
3802 return retval; | |
3803 } | |
3804 | |
3536 | 3805 std::string |
2435 | 3806 octave_stream::error (bool clear, int& err_num) |
2117 | 3807 { |
5649 | 3808 std::string retval = "invalid stream object"; |
3809 | |
5659 | 3810 if (stream_ok (false)) |
2435 | 3811 retval = rep->error (clear, err_num); |
2117 | 3812 |
3813 return retval; | |
3814 } | |
3815 | |
3536 | 3816 std::string |
3340 | 3817 octave_stream::name (void) const |
2117 | 3818 { |
3523 | 3819 std::string retval; |
2117 | 3820 |
5659 | 3821 if (stream_ok ()) |
2117 | 3822 retval = rep->name (); |
3823 | |
3824 return retval; | |
3825 } | |
3826 | |
3827 int | |
3340 | 3828 octave_stream::mode (void) const |
2117 | 3829 { |
3830 int retval = 0; | |
3831 | |
5659 | 3832 if (stream_ok ()) |
2117 | 3833 retval = rep->mode (); |
3834 | |
3835 return retval; | |
3836 } | |
3837 | |
2317 | 3838 oct_mach_info::float_format |
3340 | 3839 octave_stream::float_format (void) const |
2117 | 3840 { |
4574 | 3841 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
2317 | 3842 |
5659 | 3843 if (stream_ok ()) |
2317 | 3844 retval = rep->float_format (); |
2117 | 3845 |
3846 return retval; | |
3847 } | |
3848 | |
3536 | 3849 std::string |
2117 | 3850 octave_stream::mode_as_string (int mode) |
3851 { | |
3523 | 3852 std::string retval = "???"; |
3775 | 3853 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode); |
3854 | |
3855 if (in_mode == std::ios::in) | |
3856 retval = "r"; | |
3857 else if (in_mode == std::ios::out | |
4078 | 3858 || in_mode == (std::ios::out | std::ios::trunc)) |
3775 | 3859 retval = "w"; |
4078 | 3860 else if (in_mode == (std::ios::out | std::ios::app)) |
3775 | 3861 retval = "a"; |
4078 | 3862 else if (in_mode == (std::ios::in | std::ios::out)) |
3775 | 3863 retval = "r+"; |
4078 | 3864 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc)) |
3775 | 3865 retval = "w+"; |
4078 | 3866 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate)) |
3775 | 3867 retval = "a+"; |
4078 | 3868 else if (in_mode == (std::ios::in | std::ios::binary)) |
3775 | 3869 retval = "rb"; |
4078 | 3870 else if (in_mode == (std::ios::out | std::ios::binary) |
3871 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary)) | |
3775 | 3872 retval = "wb"; |
4078 | 3873 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary)) |
3775 | 3874 retval = "ab"; |
4078 | 3875 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary)) |
3775 | 3876 retval = "r+b"; |
4078 | 3877 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc |
3878 | std::ios::binary)) | |
3775 | 3879 retval = "w+b"; |
4078 | 3880 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate |
3881 | std::ios::binary)) | |
3775 | 3882 retval = "a+b"; |
2117 | 3883 |
3884 return retval; | |
3885 } | |
3886 | |
3887 octave_stream_list *octave_stream_list::instance = 0; | |
3888 | |
2926 | 3889 bool |
3890 octave_stream_list::instance_ok (void) | |
3891 { | |
3892 bool retval = true; | |
3893 | |
3894 if (! instance) | |
3895 instance = new octave_stream_list (); | |
3896 | |
3897 if (! instance) | |
3898 { | |
3899 ::error ("unable to create stream list object!"); | |
3900 | |
3901 retval = false; | |
3902 } | |
3903 | |
3904 return retval; | |
3905 } | |
3906 | |
5353 | 3907 int |
6757 | 3908 octave_stream_list::insert (octave_stream& os) |
2926 | 3909 { |
5353 | 3910 return (instance_ok ()) ? instance->do_insert (os) : -1; |
2926 | 3911 } |
3912 | |
3340 | 3913 octave_stream |
3523 | 3914 octave_stream_list::lookup (int fid, const std::string& who) |
2926 | 3915 { |
3341 | 3916 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926 | 3917 } |
3918 | |
3340 | 3919 octave_stream |
3523 | 3920 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926 | 3921 { |
3341 | 3922 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926 | 3923 } |
3924 | |
3925 int | |
3523 | 3926 octave_stream_list::remove (int fid, const std::string& who) |
2926 | 3927 { |
3341 | 3928 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926 | 3929 } |
3930 | |
3931 int | |
3523 | 3932 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926 | 3933 { |
3341 | 3934 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926 | 3935 } |
3936 | |
3937 void | |
3938 octave_stream_list::clear (void) | |
3939 { | |
3940 if (instance) | |
3941 instance->do_clear (); | |
3942 } | |
3943 | |
3944 string_vector | |
3945 octave_stream_list::get_info (int fid) | |
3946 { | |
3947 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); | |
3948 } | |
3949 | |
3950 string_vector | |
3951 octave_stream_list::get_info (const octave_value& fid) | |
3952 { | |
3953 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); | |
3954 } | |
3955 | |
3536 | 3956 std::string |
2926 | 3957 octave_stream_list::list_open_files (void) |
3958 { | |
3523 | 3959 return (instance_ok ()) ? instance->do_list_open_files () : std::string (); |
2926 | 3960 } |
3961 | |
3962 octave_value | |
3963 octave_stream_list::open_file_numbers (void) | |
3964 { | |
3965 return (instance_ok ()) | |
3966 ? instance->do_open_file_numbers () : octave_value (); | |
3967 } | |
3968 | |
3969 int | |
3970 octave_stream_list::get_file_number (const octave_value& fid) | |
3971 { | |
3972 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; | |
3973 } | |
3974 | |
5353 | 3975 int |
6757 | 3976 octave_stream_list::do_insert (octave_stream& os) |
2117 | 3977 { |
6757 | 3978 // Insert item with key corresponding to file-descriptor. |
3979 | |
3980 int stream_number; | |
3981 | |
3982 if ((stream_number = os.file_number ()) == -1) | |
3983 return stream_number; | |
3984 | |
3985 // Should we test for "(list.find (stream_number) != list.end ()) && | |
3986 // list[stream_number].is_open ()" and respond with "error | |
3987 // ("internal error: ...")"? It should not happen except for some | |
3988 // bug or if the user has opened a stream with an interpreted | |
3989 // command, but closed it directly with a system call in an | |
3990 // oct-file; then the kernel knows the fd is free, but Octave does | |
3991 // not know. If it happens, it should not do harm here to simply | |
3992 // overwrite this entry, although the wrong entry might have done | |
3993 // harm before. | |
3994 | |
3995 if (list.size () < list.max_size ()) | |
3996 list[stream_number] = os; | |
3997 else | |
2117 | 3998 { |
6757 | 3999 stream_number = -1; |
4000 error ("could not create file id"); | |
3340 | 4001 } |
2117 | 4002 |
5353 | 4003 return stream_number; |
6757 | 4004 |
2117 | 4005 } |
4006 | |
3341 | 4007 static void |
3523 | 4008 gripe_invalid_file_id (int fid, const std::string& who) |
3341 | 4009 { |
4010 if (who.empty ()) | |
4011 ::error ("invalid stream number = %d", fid); | |
4012 else | |
4013 ::error ("%s: invalid stream number = %d", who.c_str (), fid); | |
4014 } | |
4015 | |
3340 | 4016 octave_stream |
3523 | 4017 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117 | 4018 { |
3340 | 4019 octave_stream retval; |
2117 | 4020 |
6757 | 4021 if (fid >= 0) |
4022 { | |
4023 ostrl_map::const_iterator iter = list.find (fid); | |
4024 | |
4025 if (iter != list.end ()) | |
4026 retval = iter->second; | |
4027 else | |
4028 gripe_invalid_file_id (fid, who); | |
4029 } | |
3341 | 4030 else |
4031 gripe_invalid_file_id (fid, who); | |
2117 | 4032 |
4033 return retval; | |
4034 } | |
4035 | |
3340 | 4036 octave_stream |
3341 | 4037 octave_stream_list::do_lookup (const octave_value& fid, |
3523 | 4038 const std::string& who) const |
2117 | 4039 { |
3340 | 4040 octave_stream retval; |
2117 | 4041 |
4042 int i = get_file_number (fid); | |
4043 | |
4044 if (! error_state) | |
3341 | 4045 retval = do_lookup (i, who); |
2117 | 4046 |
4047 return retval; | |
4048 } | |
4049 | |
4050 int | |
3523 | 4051 octave_stream_list::do_remove (int fid, const std::string& who) |
2117 | 4052 { |
4053 int retval = -1; | |
4054 | |
3531 | 4055 // Can't remove stdin (std::cin), stdout (std::cout), or stderr |
4056 // (std::cerr). | |
2117 | 4057 |
6757 | 4058 if (fid > 2) |
2117 | 4059 { |
6757 | 4060 ostrl_map::iterator iter = list.find (fid); |
4061 | |
4062 if (iter != list.end ()) | |
2117 | 4063 { |
6757 | 4064 octave_stream os = iter->second; |
4065 | |
4066 if (os.is_valid ()) | |
4067 { | |
4068 os.close (); | |
4069 iter->second = octave_stream (); | |
4070 retval = 0; | |
4071 } | |
4072 else | |
4073 gripe_invalid_file_id (fid, who); | |
2117 | 4074 } |
3341 | 4075 else |
4076 gripe_invalid_file_id (fid, who); | |
2117 | 4077 } |
3341 | 4078 else |
4079 gripe_invalid_file_id (fid, who); | |
2117 | 4080 |
4081 return retval; | |
4082 } | |
4083 | |
4084 int | |
3523 | 4085 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117 | 4086 { |
4087 int retval = -1; | |
4088 | |
6054 | 4089 if (fid.is_string () && fid.string_value () == "all") |
4090 { | |
6757 | 4091 for (ostrl_map::iterator p = list.begin (); p != list.end (); p++) |
6054 | 4092 { |
6757 | 4093 // Skip stdin, stdout, and stderr. |
4094 | |
4095 if (p->first > 2) | |
4096 { | |
4097 octave_stream os = p->second; | |
4098 | |
4099 if (os.is_valid ()) | |
4100 do_remove (p->first, who); | |
4101 } | |
6054 | 4102 } |
4103 | |
4104 retval = 0; | |
4105 } | |
4106 else | |
4107 { | |
4108 int i = get_file_number (fid); | |
4109 | |
4110 if (! error_state) | |
4111 retval = do_remove (i, who); | |
4112 } | |
2117 | 4113 |
4114 return retval; | |
4115 } | |
4116 | |
4117 void | |
4118 octave_stream_list::do_clear (void) | |
4119 { | |
4120 // Do flush stdout and stderr. | |
4121 | |
6757 | 4122 list[0].flush (); |
4123 list[1].flush (); | |
2117 | 4124 |
4125 // But don't delete them or stdin. | |
4126 | |
6757 | 4127 for (ostrl_map::iterator p = list.begin (); p != list.end (); p++) |
4128 { | |
4129 // Skip stdin, stdout, and stderr. | |
4130 | |
4131 if (p->first > 2) | |
4132 p->second = octave_stream (); | |
4133 } | |
2117 | 4134 } |
4135 | |
4136 string_vector | |
4137 octave_stream_list::do_get_info (int fid) const | |
4138 { | |
4139 string_vector retval; | |
4140 | |
3340 | 4141 octave_stream os = do_lookup (fid); |
2117 | 4142 |
3341 | 4143 if (os.is_valid ()) |
2117 | 4144 { |
4145 retval.resize (3); | |
4146 | |
3340 | 4147 retval(0) = os.name (); |
4148 retval(1) = octave_stream::mode_as_string (os.mode ()); | |
4149 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); | |
2117 | 4150 } |
4151 else | |
3341 | 4152 ::error ("invalid file id = %d", fid); |
2117 | 4153 |
4154 return retval; | |
4155 } | |
4156 | |
4157 string_vector | |
4158 octave_stream_list::do_get_info (const octave_value& fid) const | |
4159 { | |
4160 string_vector retval; | |
4161 | |
4162 int conv_err = 0; | |
4163 | |
4164 int int_fid = convert_to_valid_int (fid, conv_err); | |
4165 | |
4166 if (! conv_err) | |
4167 retval = do_get_info (int_fid); | |
4168 else | |
2915 | 4169 ::error ("file id must be a file object or integer value"); |
2117 | 4170 |
4171 return retval; | |
4172 } | |
4173 | |
3536 | 4174 std::string |
2117 | 4175 octave_stream_list::do_list_open_files (void) const |
4176 { | |
3523 | 4177 std::string retval; |
2117 | 4178 |
5765 | 4179 std::ostringstream buf; |
2117 | 4180 |
4181 buf << "\n" | |
4182 << " number mode arch name\n" | |
4183 << " ------ ---- ---- ----\n"; | |
4184 | |
6757 | 4185 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4186 { |
6757 | 4187 octave_stream os = p->second; |
2117 | 4188 |
4326 | 4189 buf << " " |
4190 << std::setiosflags (std::ios::right) | |
6757 | 4191 << std::setw (4) << p->first << " " |
4326 | 4192 << std::setiosflags (std::ios::left) |
4193 << std::setw (3) | |
4194 << octave_stream::mode_as_string (os.mode ()) | |
4195 << " " | |
4196 << std::setw (9) | |
4197 << oct_mach_info::float_format_as_string (os.float_format ()) | |
4198 << " " | |
4199 << os.name () << "\n"; | |
2117 | 4200 } |
4201 | |
5765 | 4202 buf << "\n"; |
4203 | |
4204 retval = buf.str (); | |
2117 | 4205 |
4206 return retval; | |
4207 } | |
4208 | |
4209 octave_value | |
4210 octave_stream_list::do_open_file_numbers (void) const | |
4211 { | |
6757 | 4212 Matrix retval (1, list.size (), 0.0); |
2117 | 4213 |
4214 int num_open = 0; | |
4215 | |
6757 | 4216 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4217 { |
6757 | 4218 // Skip stdin, stdout, and stderr. |
4219 | |
4220 if (p->first > 2 && p->second) | |
4221 retval(0,num_open++) = p->first; | |
2117 | 4222 } |
4223 | |
4224 retval.resize ((num_open > 0), num_open); | |
4225 | |
4226 return retval; | |
4227 } | |
4228 | |
4229 int | |
2609 | 4230 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117 | 4231 { |
4232 int retval = -1; | |
4233 | |
4234 if (fid.is_string ()) | |
4235 { | |
3523 | 4236 std::string nm = fid.string_value (); |
2117 | 4237 |
6757 | 4238 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4239 { |
6757 | 4240 // stdin (std::cin), stdout (std::cout), and stderr (std::cerr) |
4241 // are unnamed. | |
4242 | |
4243 if (p->first > 2) | |
2117 | 4244 { |
6757 | 4245 octave_stream os = p->second; |
4246 | |
4247 if (os && os.name () == nm) | |
4248 { | |
4249 retval = p->first; | |
4250 break; | |
4251 } | |
2117 | 4252 } |
4253 } | |
4254 } | |
4255 else | |
4256 { | |
4257 int conv_err = 0; | |
4258 | |
4259 int int_fid = convert_to_valid_int (fid, conv_err); | |
4260 | |
4261 if (conv_err) | |
3523 | 4262 ::error ("file id must be a file object, std::string, or integer value"); |
2117 | 4263 else |
4264 retval = int_fid; | |
4265 } | |
4266 | |
4267 return retval; | |
4268 } | |
4269 | |
4270 /* | |
4271 ;;; Local Variables: *** | |
4272 ;;; mode: C++ *** | |
4273 ;;; End: *** | |
4274 */ |