Mercurial > hg > octave-nkf
annotate src/oct-stream.cc @ 8771:d3382daaf4d2
Use CARBON_LIBS instead of LIBS for framework Carbon.
author | Thomas Treichl <Thomas.Treichl@gmx.net> |
---|---|
date | Tue, 17 Feb 2009 00:28:48 -0500 |
parents | d477e57e811c |
children | 9e3111d203c0 |
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 | |
8739
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
986 // Handle CRLF, CR, or LF as line ending. |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
987 |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
988 if (c == '\r') |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
989 { |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
990 if (! strip_newline) |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
991 buf << static_cast<char> (c); |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
992 |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
993 c = is.get (); |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
994 |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
995 if (c != EOF) |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
996 { |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
997 if (c == '\n') |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
998 { |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
999 char_count++; |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1000 |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1001 if (! strip_newline) |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1002 buf << static_cast<char> (c); |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1003 } |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1004 else |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1005 is.putback (c); |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1006 } |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1007 |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1008 break; |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1009 } |
d477e57e811c
handle CRLF and CR in fgetl/fgets
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
1010 else if (c == '\n') |
6345 | 1011 { |
1012 if (! strip_newline) | |
1013 buf << static_cast<char> (c); | |
1014 | |
1015 break; | |
1016 } | |
1017 else | |
5760 | 1018 buf << static_cast<char> (c); |
6345 | 1019 |
1020 if (max_len > 0 && char_count == max_len) | |
1021 break; | |
2117 | 1022 } |
6345 | 1023 } |
1024 | |
1025 if (! is.eof () && char_count > 0) | |
1026 { | |
1027 // GAGME. Matlab seems to check for EOF even if the last | |
1028 // character in a file is a newline character. This is NOT | |
1029 // what the corresponding C-library functions do. | |
1030 int disgusting_compatibility_hack = is.get (); | |
1031 if (! is.eof ()) | |
1032 is.putback (disgusting_compatibility_hack); | |
2117 | 1033 } |
1034 | |
4224 | 1035 if (is.good () || (is.eof () && char_count > 0)) |
5765 | 1036 retval = buf.str (); |
4224 | 1037 else |
1038 { | |
1039 err = true; | |
4468 | 1040 |
4224 | 1041 if (is.eof () && char_count == 0) |
4468 | 1042 error (who, "at end of file"); |
4224 | 1043 else |
4468 | 1044 error (who, "read error"); |
4224 | 1045 } |
2117 | 1046 } |
1047 else | |
1048 { | |
1049 err = true; | |
4468 | 1050 invalid_operation (who, "reading"); |
2117 | 1051 } |
1052 | |
1053 return retval; | |
1054 } | |
1055 | |
3536 | 1056 std::string |
5275 | 1057 octave_base_stream::getl (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 1058 { |
4468 | 1059 return do_gets (max_len, err, true, who); |
2117 | 1060 } |
1061 | |
3536 | 1062 std::string |
5275 | 1063 octave_base_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 1064 { |
4468 | 1065 return do_gets (max_len, err, false, who); |
2117 | 1066 } |
1067 | |
3640 | 1068 #define OCTAVE_SCAN(is, fmt, arg) octave_scan (is, fmt, arg) |
3636 | 1069 |
1070 template <class T> | |
1071 std::istream& | |
6767 | 1072 octave_scan_1 (std::istream& is, const scanf_format_elt& fmt, T* valptr) |
3636 | 1073 { |
3779 | 1074 T& ref = *valptr; |
1075 | |
1076 switch (fmt.type) | |
1077 { | |
1078 case 'o': | |
4926 | 1079 is >> std::oct >> ref >> std::dec; |
3779 | 1080 break; |
1081 | |
1082 case 'x': | |
4926 | 1083 is >> std::hex >> ref >> std::dec; |
1084 break; | |
1085 | |
1086 case 'i': | |
1087 { | |
1088 int c1 = is.get (); | |
1089 | |
1090 if (! is.eof ()) | |
1091 { | |
1092 if (c1 == '0') | |
1093 { | |
7709
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1094 int c2 = is.peek (); |
4926 | 1095 |
1096 if (c2 == 'x' || c2 == 'X') | |
7709
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1097 { |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1098 is.ignore (); |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1099 if (std::isxdigit (is.peek ())) |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1100 is >> std::hex >> ref >> std::dec; |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1101 else |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1102 ref = 0; |
fa41af732801
octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents:
7538
diff
changeset
|
1103 } |
4926 | 1104 else |
4927 | 1105 { |
1106 if (c2 == '0' || c2 == '1' || c2 == '2' | |
1107 || c2 == '3' || c2 == '4' || c2 == '5' | |
1108 || c2 == '6' || c2 == '7') | |
1109 is >> std::oct >> ref >> std::dec; | |
1110 else | |
1111 ref = 0; | |
1112 } | |
4926 | 1113 } |
1114 else | |
1115 { | |
1116 is.putback (c1); | |
1117 | |
1118 is >> ref; | |
1119 } | |
1120 } | |
1121 } | |
3779 | 1122 break; |
1123 | |
1124 default: | |
1125 is >> ref; | |
1126 break; | |
1127 } | |
3639 | 1128 |
3638 | 1129 return is; |
3636 | 1130 } |
1131 | |
6767 | 1132 template <class T> |
1133 std::istream& | |
1134 octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr) | |
1135 { | |
1136 if (fmt.width) | |
1137 { | |
1138 // Limit input to fmt.width characters by reading into a | |
1139 // temporary stringstream buffer. | |
1140 | |
1141 std::string tmp; | |
1142 | |
1143 is.width (fmt.width); | |
1144 is >> tmp; | |
1145 | |
1146 std::istringstream ss (tmp); | |
1147 | |
1148 octave_scan_1 (ss, fmt, valptr); | |
1149 } | |
1150 else | |
1151 octave_scan_1 (is, fmt, valptr); | |
1152 | |
1153 return is; | |
1154 } | |
1155 | |
3779 | 1156 // Note that this specialization is only used for reading characters, not |
1157 // character strings. See BEGIN_S_CONVERSION for details. | |
1158 | |
1159 template<> | |
1160 std::istream& | |
4661 | 1161 octave_scan<> (std::istream& is, const scanf_format_elt& /* fmt */, |
1162 char* valptr) | |
3779 | 1163 { |
1164 return is >> valptr; | |
1165 } | |
3636 | 1166 |
4595 | 1167 template std::istream& |
1168 octave_scan (std::istream&, const scanf_format_elt&, int*); | |
1169 | |
1170 template std::istream& | |
1171 octave_scan (std::istream&, const scanf_format_elt&, long int*); | |
1172 | |
1173 template std::istream& | |
1174 octave_scan (std::istream&, const scanf_format_elt&, short int*); | |
1175 | |
1176 template std::istream& | |
1177 octave_scan (std::istream&, const scanf_format_elt&, unsigned int*); | |
1178 | |
1179 template std::istream& | |
1180 octave_scan (std::istream&, const scanf_format_elt&, unsigned long int*); | |
1181 | |
1182 template std::istream& | |
1183 octave_scan (std::istream&, const scanf_format_elt&, unsigned short int*); | |
1184 | |
1185 #if 0 | |
1186 template std::istream& | |
1187 octave_scan (std::istream&, const scanf_format_elt&, float*); | |
1188 #endif | |
1189 | |
5403 | 1190 template<> |
5176 | 1191 std::istream& |
5403 | 1192 octave_scan<> (std::istream& is, const scanf_format_elt& fmt, double* valptr) |
5176 | 1193 { |
1194 double& ref = *valptr; | |
1195 | |
1196 switch (fmt.type) | |
1197 { | |
1198 case 'e': | |
1199 case 'f': | |
1200 case 'g': | |
1201 { | |
5259 | 1202 int c1 = EOF; |
5176 | 1203 |
1204 while (is && (c1 = is.get ()) != EOF && isspace (c1)) | |
1205 /* skip whitespace */; | |
1206 | |
1207 if (c1 != EOF) | |
1208 { | |
1209 if (c1 == 'N') | |
1210 { | |
1211 int c2 = is.get (); | |
1212 | |
1213 if (c2 != EOF) | |
1214 { | |
1215 if (c2 == 'A') | |
1216 { | |
1217 int c3 = is.get (); | |
1218 | |
1219 if (c3 != EOF) | |
1220 { | |
1221 is.putback (c3); | |
1222 | |
1223 if (isspace (c3) || ispunct (c3)) | |
1224 ref = octave_NA; | |
1225 else | |
1226 { | |
1227 is.putback (c2); | |
1228 is.putback (c1); | |
1229 | |
1230 is >> ref; | |
1231 } | |
1232 } | |
1233 else | |
1234 { | |
1235 is.clear (); | |
1236 | |
1237 ref = octave_NA; | |
1238 } | |
1239 } | |
1240 else if (c2 == 'a') | |
1241 { | |
1242 int c3 = is.get (); | |
1243 | |
1244 if (c3 != EOF) | |
1245 { | |
1246 if (c3 == 'N') | |
1247 { | |
1248 int c4 = is.get (); | |
1249 | |
1250 if (c4 != EOF) | |
1251 { | |
1252 is.putback (c4); | |
1253 | |
1254 if (isspace (c4) || ispunct (c4)) | |
1255 ref = octave_NaN; | |
1256 else | |
1257 { | |
1258 is.putback (c3); | |
1259 is.putback (c2); | |
1260 is.putback (c1); | |
1261 | |
1262 is >> ref; | |
1263 } | |
1264 } | |
1265 else | |
1266 { | |
1267 is.clear (); | |
1268 | |
1269 ref = octave_NaN; | |
1270 } | |
1271 } | |
1272 else | |
1273 { | |
1274 is.putback (c3); | |
1275 is.putback (c2); | |
1276 is.putback (c1); | |
1277 | |
1278 is >> ref; | |
1279 } | |
1280 } | |
1281 } | |
1282 else | |
1283 { | |
1284 is.putback (c2); | |
1285 is.putback (c1); | |
1286 | |
1287 is >> ref; | |
1288 } | |
1289 } | |
1290 } | |
1291 else if (c1 == 'I') | |
1292 { | |
1293 int c2 = is.get (); | |
1294 | |
1295 if (c2 != EOF) | |
1296 { | |
1297 if (c2 == 'n') | |
1298 { | |
1299 int c3 = is.get (); | |
1300 | |
1301 if (c3 != EOF) | |
6483 | 1302 { |
1303 if (c3 == 'f') | |
1304 { | |
1305 int c4 = is.get (); | |
1306 | |
1307 if (c4 != EOF) | |
1308 { | |
1309 is.putback (c4); | |
1310 | |
1311 if (isspace (c4) || ispunct (c4)) | |
1312 ref = octave_Inf; | |
1313 else | |
1314 { | |
1315 is.putback (c3); | |
1316 is.putback (c2); | |
1317 is.putback (c1); | |
1318 | |
1319 is >> ref; | |
1320 } | |
1321 } | |
1322 else | |
1323 { | |
1324 is.clear (); | |
1325 | |
5176 | 1326 ref = octave_Inf; |
6483 | 1327 } |
1328 } | |
1329 else | |
1330 { | |
1331 is.putback (c3); | |
1332 is.putback (c2); | |
1333 is.putback (c1); | |
1334 | |
1335 is >> ref; | |
1336 } | |
1337 } | |
1338 else | |
1339 { | |
1340 is.putback (c2); | |
1341 is.putback (c1); | |
1342 | |
1343 is >> ref; | |
1344 } | |
5176 | 1345 } |
1346 } | |
1347 } | |
1348 else | |
1349 { | |
1350 is.putback (c1); | |
1351 | |
1352 is >> ref; | |
1353 } | |
1354 } | |
1355 } | |
1356 break; | |
1357 | |
1358 default: | |
1359 panic_impossible (); | |
1360 break; | |
1361 } | |
1362 | |
1363 return is; | |
1364 } | |
1365 | |
2572 | 1366 template <class T> |
1367 void | |
3636 | 1368 do_scanf_conv (std::istream& is, const scanf_format_elt& fmt, |
5275 | 1369 T valptr, Matrix& mval, double *data, octave_idx_type& idx, |
1370 octave_idx_type& conversion_count, octave_idx_type nr, octave_idx_type max_size, | |
3636 | 1371 bool discard) |
2572 | 1372 { |
3640 | 1373 OCTAVE_SCAN (is, fmt, valptr); |
2572 | 1374 |
1375 if (is) | |
1376 { | |
1377 if (idx == max_size && ! discard) | |
1378 { | |
1379 max_size *= 2; | |
1380 | |
1381 if (nr > 0) | |
1382 mval.resize (nr, max_size / nr, 0.0); | |
1383 else | |
1384 mval.resize (max_size, 1, 0.0); | |
1385 | |
1386 data = mval.fortran_vec (); | |
1387 } | |
1388 | |
1389 if (! discard) | |
3268 | 1390 { |
3559 | 1391 conversion_count++; |
3268 | 1392 data[idx++] = *(valptr); |
1393 } | |
2572 | 1394 } |
1395 } | |
1396 | |
1397 template void | |
3878 | 1398 do_scanf_conv (std::istream&, const scanf_format_elt&, int*, |
5275 | 1399 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2572 | 1400 |
3233 | 1401 template void |
3636 | 1402 do_scanf_conv (std::istream&, const scanf_format_elt&, long int*, |
5275 | 1403 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3233 | 1404 |
1405 template void | |
3636 | 1406 do_scanf_conv (std::istream&, const scanf_format_elt&, short int*, |
5275 | 1407 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3233 | 1408 |
3878 | 1409 template void |
1410 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned int*, | |
5275 | 1411 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878 | 1412 |
1413 template void | |
1414 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned long int*, | |
5275 | 1415 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878 | 1416 |
1417 template void | |
1418 do_scanf_conv (std::istream&, const scanf_format_elt&, unsigned short int*, | |
5275 | 1419 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
3878 | 1420 |
2600 | 1421 #if 0 |
2572 | 1422 template void |
3636 | 1423 do_scanf_conv (std::istream&, const scanf_format_elt&, float*, |
5275 | 1424 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2600 | 1425 #endif |
2572 | 1426 |
1427 template void | |
3636 | 1428 do_scanf_conv (std::istream&, const scanf_format_elt&, double*, |
5275 | 1429 Matrix&, double*, octave_idx_type&, octave_idx_type&, octave_idx_type, octave_idx_type, bool); |
2572 | 1430 |
3483 | 1431 #define DO_WHITESPACE_CONVERSION() \ |
1432 do \ | |
1433 { \ | |
1434 int c = EOF; \ | |
1435 \ | |
1436 while (is && (c = is.get ()) != EOF && isspace (c)) \ | |
1437 /* skip whitespace */; \ | |
1438 \ | |
1439 if (c != EOF) \ | |
1440 is.putback (c); \ | |
1441 } \ | |
1442 while (0) | |
1443 | |
1444 #define DO_LITERAL_CONVERSION() \ | |
1445 do \ | |
1446 { \ | |
1447 int c = EOF; \ | |
1448 \ | |
1449 int n = strlen (fmt); \ | |
1450 int i = 0; \ | |
1451 \ | |
1452 while (i < n && is && (c = is.get ()) != EOF) \ | |
1453 { \ | |
5326 | 1454 if (c == static_cast<unsigned char> (fmt[i])) \ |
3483 | 1455 { \ |
1456 i++; \ | |
1457 continue; \ | |
1458 } \ | |
1459 else \ | |
1460 { \ | |
1461 is.putback (c); \ | |
1462 break; \ | |
1463 } \ | |
1464 } \ | |
1465 \ | |
1466 if (i != n) \ | |
3538 | 1467 is.setstate (std::ios::failbit); \ |
3483 | 1468 } \ |
1469 while (0) | |
1470 | |
3640 | 1471 #define DO_PCT_CONVERSION() \ |
1472 do \ | |
1473 { \ | |
1474 int c = is.get (); \ | |
1475 \ | |
1476 if (c != EOF) \ | |
1477 { \ | |
1478 if (c != '%') \ | |
1479 { \ | |
1480 is.putback (c); \ | |
1481 is.setstate (std::ios::failbit); \ | |
1482 } \ | |
1483 } \ | |
1484 else \ | |
1485 is.setstate (std::ios::failbit); \ | |
1486 } \ | |
1487 while (0) | |
1488 | |
3483 | 1489 #define BEGIN_C_CONVERSION() \ |
3538 | 1490 is.unsetf (std::ios::skipws); \ |
3483 | 1491 \ |
1492 int width = elt->width ? elt->width : 1; \ | |
1493 \ | |
4051 | 1494 char *tbuf = new char[width + 1]; \ |
3483 | 1495 \ |
1496 int c = EOF; \ | |
1497 int n = 0; \ | |
1498 \ | |
1499 while (is && n < width && (c = is.get ()) != EOF) \ | |
5760 | 1500 tbuf[n++] = static_cast<char> (c); \ |
4051 | 1501 \ |
1502 tbuf[n] = '\0'; \ | |
3483 | 1503 \ |
5266 | 1504 if (n > 0 && c == EOF) \ |
1505 is.clear (); \ | |
1506 \ | |
4051 | 1507 std::string tmp = tbuf; \ |
1508 \ | |
1509 delete [] tbuf | |
3483 | 1510 |
1511 // For a `%s' format, skip initial whitespace and then read until the | |
5338 | 1512 // next whitespace character or until WIDTH characters have been read. |
3483 | 1513 #define BEGIN_S_CONVERSION() \ |
1514 int width = elt->width; \ | |
1515 \ | |
4051 | 1516 std::string tmp; \ |
3483 | 1517 \ |
1518 do \ | |
1519 { \ | |
1520 if (width) \ | |
5338 | 1521 { \ |
1522 char *tbuf = new char [width+1]; \ | |
1523 \ | |
1524 int c = EOF; \ | |
1525 \ | |
1526 int n = 0; \ | |
1527 \ | |
1528 while (is && (c = is.get ()) != EOF) \ | |
1529 { \ | |
1530 if (! isspace (c)) \ | |
1531 { \ | |
1532 tbuf[n++] = static_cast<char> (c); \ | |
1533 break; \ | |
1534 } \ | |
1535 } \ | |
4051 | 1536 \ |
5338 | 1537 while (is && n < width && (c = is.get ()) != EOF) \ |
1538 { \ | |
1539 if (isspace (c)) \ | |
1540 { \ | |
1541 is.putback (c); \ | |
1542 break; \ | |
1543 } \ | |
1544 else \ | |
1545 tbuf[n++] = static_cast<char> (c); \ | |
1546 } \ | |
3483 | 1547 \ |
5338 | 1548 tbuf[n] = '\0'; \ |
1549 \ | |
1550 if (n > 0 && c == EOF) \ | |
1551 is.clear (); \ | |
1552 \ | |
4051 | 1553 tmp = tbuf; \ |
5338 | 1554 \ |
4051 | 1555 delete [] tbuf; \ |
5338 | 1556 } \ |
3483 | 1557 else \ |
5338 | 1558 { \ |
1559 is >> std::ws >> tmp; \ | |
1560 } \ | |
3483 | 1561 } \ |
1562 while (0) | |
1563 | |
1564 // This format must match a nonempty sequence of characters. | |
1565 #define BEGIN_CHAR_CLASS_CONVERSION() \ | |
1566 int width = elt->width; \ | |
1567 \ | |
4051 | 1568 std::string tmp; \ |
3483 | 1569 \ |
1570 do \ | |
1571 { \ | |
7426 | 1572 if (! width) \ |
7427 | 1573 width = INT_MAX; \ |
1574 \ | |
7426 | 1575 std::ostringstream buf; \ |
1576 \ | |
1577 std::string char_class = elt->char_class; \ | |
4051 | 1578 \ |
7426 | 1579 int c = EOF; \ |
3483 | 1580 \ |
7426 | 1581 if (elt->type == '[') \ |
1582 { \ | |
1583 int chars_read = 0; \ | |
1584 while (is && chars_read++ < width && (c = is.get ()) != EOF \ | |
8021 | 1585 && char_class.find (c) != std::string::npos) \ |
7426 | 1586 buf << static_cast<char> (c); \ |
3483 | 1587 } \ |
1588 else \ | |
1589 { \ | |
7426 | 1590 int chars_read = 0; \ |
1591 while (is && chars_read++ < width && (c = is.get ()) != EOF \ | |
8021 | 1592 && char_class.find (c) == std::string::npos) \ |
7426 | 1593 buf << static_cast<char> (c); \ |
1594 } \ | |
3483 | 1595 \ |
7426 | 1596 if (width == INT_MAX && c != EOF) \ |
1597 is.putback (c); \ | |
3483 | 1598 \ |
7426 | 1599 tmp = buf.str (); \ |
3483 | 1600 \ |
7426 | 1601 if (tmp.empty ()) \ |
1602 is.setstate (std::ios::failbit); \ | |
3483 | 1603 } \ |
1604 while (0) | |
1605 | |
3410 | 1606 #define FINISH_CHARACTER_CONVERSION() \ |
1607 do \ | |
1608 { \ | |
4051 | 1609 width = tmp.length (); \ |
3410 | 1610 \ |
1611 if (is) \ | |
1612 { \ | |
1613 int i = 0; \ | |
1614 \ | |
1615 if (! discard) \ | |
1616 { \ | |
1617 conversion_count++; \ | |
1618 \ | |
1619 while (i < width && tmp[i] != '\0') \ | |
1620 { \ | |
1621 if (data_index == max_size) \ | |
1622 { \ | |
1623 max_size *= 2; \ | |
1624 \ | |
4420 | 1625 if (all_char_conv) \ |
3410 | 1626 { \ |
4420 | 1627 if (one_elt_size_spec) \ |
3410 | 1628 mval.resize (1, max_size, 0.0); \ |
4420 | 1629 else if (nr > 0) \ |
1630 mval.resize (nr, max_size / nr, 0.0); \ | |
3410 | 1631 else \ |
4420 | 1632 panic_impossible (); \ |
3410 | 1633 } \ |
4420 | 1634 else if (nr > 0) \ |
6970 | 1635 mval.resize (nr, max_size / nr, 0.0); \ |
4420 | 1636 else \ |
1637 mval.resize (max_size, 1, 0.0); \ | |
3410 | 1638 \ |
1639 data = mval.fortran_vec (); \ | |
1640 } \ | |
1641 \ | |
1642 data[data_index++] = tmp[i++]; \ | |
1643 } \ | |
1644 } \ | |
1645 } \ | |
1646 } \ | |
1647 while (0) | |
2117 | 1648 |
1649 octave_value | |
1650 octave_base_stream::do_scanf (scanf_format_list& fmt_list, | |
5275 | 1651 octave_idx_type nr, octave_idx_type nc, bool one_elt_size_spec, |
1652 octave_idx_type& conversion_count, const std::string& who) | |
2117 | 1653 { |
3268 | 1654 conversion_count = 0; |
1655 | |
3640 | 1656 int nconv = fmt_list.num_conversions (); |
1657 | |
5275 | 1658 octave_idx_type data_index = 0; |
2121 | 1659 |
2117 | 1660 octave_value retval = Matrix (); |
1661 | |
3268 | 1662 if (nr == 0 || nc == 0) |
1663 { | |
1664 if (one_elt_size_spec) | |
1665 nc = 0; | |
1666 | |
1667 return Matrix (nr, nc, 0.0); | |
1668 } | |
1669 | |
3523 | 1670 std::istream *isp = input_stream (); |
2117 | 1671 |
1672 bool all_char_conv = fmt_list.all_character_conversions (); | |
1673 | |
1674 Matrix mval; | |
1675 double *data = 0; | |
5275 | 1676 octave_idx_type max_size = 0; |
1677 octave_idx_type max_conv = 0; | |
1678 | |
1679 octave_idx_type final_nr = 0; | |
1680 octave_idx_type final_nc = 0; | |
2117 | 1681 |
3268 | 1682 if (all_char_conv) |
1683 { | |
4420 | 1684 // Any of these could be resized later (if we have %s |
1685 // conversions, we may read more than one element for each | |
1686 // conversion). | |
1687 | |
3268 | 1688 if (one_elt_size_spec) |
1689 { | |
3410 | 1690 max_size = 512; |
1691 mval.resize (1, max_size, 0.0); | |
1692 | |
3268 | 1693 if (nr > 0) |
1694 max_conv = nr; | |
1695 } | |
4420 | 1696 else if (nr > 0) |
3268 | 1697 { |
4420 | 1698 if (nc > 0) |
1699 { | |
1700 mval.resize (nr, nc, 0.0); | |
1701 max_size = max_conv = nr * nc; | |
1702 } | |
1703 else | |
1704 { | |
1705 mval.resize (nr, 32, 0.0); | |
1706 max_size = nr * 32; | |
1707 } | |
3268 | 1708 } |
4420 | 1709 else |
1710 panic_impossible (); | |
3268 | 1711 } |
1712 else if (nr > 0) | |
2117 | 1713 { |
1714 if (nc > 0) | |
1715 { | |
4420 | 1716 // Will not resize later. |
2117 | 1717 mval.resize (nr, nc, 0.0); |
1718 max_size = nr * nc; | |
3268 | 1719 max_conv = max_size; |
2117 | 1720 } |
1721 else | |
1722 { | |
4420 | 1723 // Maybe resize later. |
2117 | 1724 mval.resize (nr, 32, 0.0); |
2121 | 1725 max_size = nr * 32; |
2117 | 1726 } |
1727 } | |
1728 else | |
1729 { | |
4420 | 1730 // Maybe resize later. |
2117 | 1731 mval.resize (32, 1, 0.0); |
1732 max_size = 32; | |
1733 } | |
1734 | |
4420 | 1735 data = mval.fortran_vec (); |
1736 | |
2117 | 1737 if (isp) |
1738 { | |
3523 | 1739 std::istream& is = *isp; |
2117 | 1740 |
1741 const scanf_format_elt *elt = fmt_list.first (); | |
1742 | |
3538 | 1743 std::ios::fmtflags flags = is.flags (); |
2213 | 1744 |
2117 | 1745 for (;;) |
1746 { | |
4153 | 1747 OCTAVE_QUIT; |
1748 | |
2117 | 1749 if (elt) |
1750 { | |
3268 | 1751 if (max_conv > 0 && conversion_count == max_conv) |
1752 { | |
1753 if (all_char_conv && one_elt_size_spec) | |
1754 { | |
1755 final_nr = 1; | |
1756 final_nc = data_index; | |
1757 } | |
1758 else | |
1759 { | |
1760 final_nr = nr; | |
1761 final_nc = (data_index - 1) / nr + 1; | |
1762 } | |
1763 | |
1764 break; | |
1765 } | |
1766 else if (data_index == max_size) | |
2117 | 1767 { |
3410 | 1768 max_size *= 2; |
1769 | |
4420 | 1770 if (all_char_conv) |
2687 | 1771 { |
4420 | 1772 if (one_elt_size_spec) |
3410 | 1773 mval.resize (1, max_size, 0.0); |
4420 | 1774 else if (nr > 0) |
1775 mval.resize (nr, max_size / nr, 0.0); | |
3410 | 1776 else |
4420 | 1777 panic_impossible (); |
2687 | 1778 } |
4420 | 1779 else if (nr > 0) |
6970 | 1780 mval.resize (nr, max_size / nr, 0.0); |
4420 | 1781 else |
1782 mval.resize (max_size, 1, 0.0); | |
3410 | 1783 |
1784 data = mval.fortran_vec (); | |
2117 | 1785 } |
1786 | |
1787 const char *fmt = elt->text; | |
1788 | |
1789 bool discard = elt->discard; | |
1790 | |
1791 switch (elt->type) | |
1792 { | |
3483 | 1793 case scanf_format_elt::whitespace_conversion: |
1794 DO_WHITESPACE_CONVERSION (); | |
1795 break; | |
1796 | |
1797 case scanf_format_elt::literal_conversion: | |
1798 DO_LITERAL_CONVERSION (); | |
1799 break; | |
1800 | |
2117 | 1801 case '%': |
3640 | 1802 DO_PCT_CONVERSION (); |
3483 | 1803 break; |
2117 | 1804 |
3878 | 1805 case 'd': case 'i': |
2117 | 1806 { |
3233 | 1807 switch (elt->modifier) |
1808 { | |
1809 case 'h': | |
1810 { | |
1811 short int tmp; | |
3779 | 1812 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268 | 1813 data_index, conversion_count, |
3233 | 1814 nr, max_size, discard); |
1815 } | |
3483 | 1816 break; |
3233 | 1817 |
1818 case 'l': | |
1819 { | |
1820 long int tmp; | |
3779 | 1821 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268 | 1822 data_index, conversion_count, |
3233 | 1823 nr, max_size, discard); |
1824 } | |
3483 | 1825 break; |
3233 | 1826 |
1827 default: | |
1828 { | |
1829 int tmp; | |
3779 | 1830 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268 | 1831 data_index, conversion_count, |
3233 | 1832 nr, max_size, discard); |
1833 } | |
3483 | 1834 break; |
3233 | 1835 } |
2117 | 1836 } |
3483 | 1837 break; |
2117 | 1838 |
3878 | 1839 case 'o': case 'u': case 'x': |
1840 { | |
1841 switch (elt->modifier) | |
1842 { | |
1843 case 'h': | |
1844 { | |
1845 unsigned short int tmp; | |
1846 do_scanf_conv (is, *elt, &tmp, mval, data, | |
1847 data_index, conversion_count, | |
1848 nr, max_size, discard); | |
1849 } | |
1850 break; | |
1851 | |
1852 case 'l': | |
1853 { | |
1854 unsigned long int tmp; | |
1855 do_scanf_conv (is, *elt, &tmp, mval, data, | |
1856 data_index, conversion_count, | |
1857 nr, max_size, discard); | |
1858 } | |
1859 break; | |
1860 | |
1861 default: | |
1862 { | |
1863 unsigned int tmp; | |
1864 do_scanf_conv (is, *elt, &tmp, mval, data, | |
1865 data_index, conversion_count, | |
1866 nr, max_size, discard); | |
1867 } | |
1868 break; | |
1869 } | |
1870 } | |
1871 break; | |
1872 | |
2117 | 1873 case 'e': case 'f': case 'g': |
1874 { | |
2600 | 1875 double tmp; |
1876 | |
3779 | 1877 do_scanf_conv (is, *elt, &tmp, mval, data, |
3268 | 1878 data_index, conversion_count, |
2600 | 1879 nr, max_size, discard); |
2117 | 1880 } |
3483 | 1881 break; |
2117 | 1882 |
2213 | 1883 case 'c': |
3410 | 1884 { |
3483 | 1885 BEGIN_C_CONVERSION (); |
3410 | 1886 |
1887 FINISH_CHARACTER_CONVERSION (); | |
3483 | 1888 |
1889 is.setf (flags); | |
3410 | 1890 } |
1891 break; | |
2213 | 1892 |
2117 | 1893 case 's': |
1894 { | |
3483 | 1895 BEGIN_S_CONVERSION (); |
3268 | 1896 |
3410 | 1897 FINISH_CHARACTER_CONVERSION (); |
2117 | 1898 } |
3483 | 1899 break; |
1900 | |
1901 case '[': case '^': | |
1902 { | |
1903 BEGIN_CHAR_CLASS_CONVERSION (); | |
1904 | |
1905 FINISH_CHARACTER_CONVERSION (); | |
1906 } | |
1907 break; | |
1908 | |
1909 case 'p': | |
4468 | 1910 error ("%s: unsupported format specifier", who.c_str ()); |
2117 | 1911 break; |
1912 | |
1913 default: | |
4468 | 1914 error ("%s: internal format error", who.c_str ()); |
2117 | 1915 break; |
1916 } | |
1917 | |
1918 if (! ok ()) | |
1919 { | |
1920 break; | |
1921 } | |
1922 else if (! is) | |
1923 { | |
3268 | 1924 if (all_char_conv) |
2117 | 1925 { |
3268 | 1926 if (one_elt_size_spec) |
1927 { | |
1928 final_nr = 1; | |
1929 final_nc = data_index; | |
1930 } | |
1931 else if (data_index > nr) | |
2117 | 1932 { |
2759 | 1933 final_nr = nr; |
3268 | 1934 final_nc = (data_index - 1) / nr + 1; |
2117 | 1935 } |
1936 else | |
1937 { | |
3268 | 1938 final_nr = data_index; |
1939 final_nc = 1; | |
1940 } | |
1941 } | |
1942 else if (nr > 0) | |
1943 { | |
1944 if (data_index > nr) | |
1945 { | |
1946 final_nr = nr; | |
1947 final_nc = (data_index - 1) / nr + 1; | |
1948 } | |
1949 else | |
1950 { | |
1951 final_nr = data_index; | |
2117 | 1952 final_nc = 1; |
1953 } | |
1954 } | |
1955 else | |
1956 { | |
3268 | 1957 final_nr = data_index; |
2759 | 1958 final_nc = 1; |
1959 } | |
1960 | |
3337 | 1961 // If it looks like we have a matching failure, then |
1962 // reset the failbit in the stream state. | |
1963 | |
3538 | 1964 if (is.rdstate () & std::ios::failbit) |
1965 is.clear (is.rdstate () & (~std::ios::failbit)); | |
3337 | 1966 |
5775 | 1967 // FIXME -- is this the right thing to do? |
3342 | 1968 |
1969 if (interactive && name () == "stdin") | |
2759 | 1970 { |
1971 is.clear (); | |
1972 | |
1973 // Skip to end of line. | |
1974 | |
1975 bool err; | |
4468 | 1976 do_gets (-1, err, false, who); |
2117 | 1977 } |
1978 | |
1979 break; | |
1980 } | |
1981 } | |
1982 else | |
1983 { | |
4468 | 1984 error ("%s: internal format error", who.c_str ()); |
2117 | 1985 break; |
1986 } | |
1987 | |
3640 | 1988 elt = fmt_list.next (nconv > 0); |
2117 | 1989 } |
1990 } | |
1991 | |
1992 if (ok ()) | |
1993 { | |
2121 | 1994 mval.resize (final_nr, final_nc, 0.0); |
2117 | 1995 |
3268 | 1996 retval = mval; |
1997 | |
2117 | 1998 if (all_char_conv) |
5279 | 1999 retval = retval.convert_to_str (false, true); |
2117 | 2000 } |
2001 | |
2002 return retval; | |
2003 } | |
2004 | |
2005 octave_value | |
3810 | 2006 octave_base_stream::scanf (const std::string& fmt, const Array<double>& size, |
5275 | 2007 octave_idx_type& conversion_count, const std::string& who) |
2117 | 2008 { |
2009 octave_value retval = Matrix (); | |
2010 | |
3559 | 2011 conversion_count = 0; |
2117 | 2012 |
3523 | 2013 std::istream *isp = input_stream (); |
2117 | 2014 |
2015 if (isp) | |
2016 { | |
2017 scanf_format_list fmt_list (fmt); | |
2018 | |
3640 | 2019 if (fmt_list.num_conversions () == -1) |
4468 | 2020 ::error ("%s: invalid format specified", who.c_str ()); |
3640 | 2021 else |
2117 | 2022 { |
5275 | 2023 octave_idx_type nr = -1; |
2024 octave_idx_type nc = -1; | |
3640 | 2025 |
2026 bool one_elt_size_spec; | |
2027 | |
4468 | 2028 get_size (size, nr, nc, one_elt_size_spec, who); |
3640 | 2029 |
2030 if (! error_state) | |
2031 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec, | |
4468 | 2032 conversion_count, who); |
2215 | 2033 } |
2034 } | |
2035 else | |
4468 | 2036 invalid_operation (who, "reading"); |
2572 | 2037 |
2038 return retval; | |
2039 } | |
2040 | |
2712 | 2041 bool |
2042 octave_base_stream::do_oscanf (const scanf_format_elt *elt, | |
4468 | 2043 octave_value& retval, const std::string& who) |
2572 | 2044 { |
2712 | 2045 bool quit = false; |
2215 | 2046 |
3523 | 2047 std::istream *isp = input_stream (); |
2215 | 2048 |
2049 if (isp) | |
2050 { | |
3523 | 2051 std::istream& is = *isp; |
2215 | 2052 |
3538 | 2053 std::ios::fmtflags flags = is.flags (); |
2215 | 2054 |
2055 if (elt) | |
2056 { | |
2057 const char *fmt = elt->text; | |
2058 | |
2059 bool discard = elt->discard; | |
2060 | |
2061 switch (elt->type) | |
2062 { | |
3483 | 2063 case scanf_format_elt::whitespace_conversion: |
2064 DO_WHITESPACE_CONVERSION (); | |
2065 break; | |
2066 | |
2067 case scanf_format_elt::literal_conversion: | |
2068 DO_LITERAL_CONVERSION (); | |
2069 break; | |
2070 | |
2215 | 2071 case '%': |
2072 { | |
3640 | 2073 DO_PCT_CONVERSION (); |
2074 | |
2075 if (! is) | |
2712 | 2076 quit = true; |
3640 | 2077 |
2215 | 2078 } |
2079 break; | |
2080 | |
3878 | 2081 case 'd': case 'i': |
2215 | 2082 { |
2083 int tmp; | |
2084 | |
3640 | 2085 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712 | 2086 { |
2087 if (! discard) | |
4233 | 2088 retval = tmp; |
2712 | 2089 } |
2090 else | |
2091 quit = true; | |
2215 | 2092 } |
2093 break; | |
2094 | |
3878 | 2095 case 'o': case 'u': case 'x': |
2096 { | |
2097 long int tmp; | |
2098 | |
2099 if (OCTAVE_SCAN (is, *elt, &tmp)) | |
2100 { | |
2101 if (! discard) | |
4254 | 2102 retval = tmp; |
3878 | 2103 } |
2104 else | |
2105 quit = true; | |
2106 } | |
2107 break; | |
2108 | |
2215 | 2109 case 'e': case 'f': case 'g': |
2110 { | |
2600 | 2111 double tmp; |
2112 | |
3640 | 2113 if (OCTAVE_SCAN (is, *elt, &tmp)) |
2712 | 2114 { |
2115 if (! discard) | |
2116 retval = tmp; | |
2117 } | |
2118 else | |
2119 quit = true; | |
2215 | 2120 } |
2121 break; | |
2122 | |
2123 case 'c': | |
2124 { | |
3483 | 2125 BEGIN_C_CONVERSION (); |
2126 | |
2127 if (! discard) | |
2128 retval = tmp; | |
2129 | |
2130 if (! is) | |
2712 | 2131 quit = true; |
2215 | 2132 |
2133 is.setf (flags); | |
2134 } | |
2135 break; | |
2136 | |
2137 case 's': | |
2138 { | |
3483 | 2139 BEGIN_S_CONVERSION (); |
2140 | |
2141 if (! discard) | |
2142 retval = tmp; | |
2572 | 2143 |
3268 | 2144 if (! is) |
2145 quit = true; | |
2215 | 2146 } |
2147 break; | |
2148 | |
3483 | 2149 case '[': case '^': |
2150 { | |
2151 BEGIN_CHAR_CLASS_CONVERSION (); | |
2152 | |
2153 if (! discard) | |
2154 retval = tmp; | |
2155 | |
2156 if (! is) | |
2157 quit = true; | |
2158 } | |
2159 break; | |
2160 | |
2161 case 'p': | |
4468 | 2162 error ("%s: unsupported format specifier", who.c_str ()); |
2215 | 2163 break; |
2164 | |
2165 default: | |
4468 | 2166 error ("%s: internal format error", who.c_str ()); |
2215 | 2167 break; |
2168 } | |
2169 } | |
2170 | |
2171 if (ok () && is.fail ()) | |
2172 { | |
4468 | 2173 error ("%s: read error", who.c_str ()); |
3483 | 2174 |
5775 | 2175 // FIXME -- is this the right thing to do? |
3342 | 2176 |
2177 if (interactive && name () == "stdin") | |
2215 | 2178 { |
2179 // Skip to end of line. | |
2180 | |
2181 bool err; | |
4468 | 2182 do_gets (-1, err, false, who); |
2215 | 2183 } |
2184 } | |
2185 } | |
2186 | |
2712 | 2187 return quit; |
2215 | 2188 } |
2189 | |
2190 octave_value_list | |
4468 | 2191 octave_base_stream::oscanf (const std::string& fmt, const std::string& who) |
2215 | 2192 { |
2193 octave_value_list retval; | |
2194 | |
3523 | 2195 std::istream *isp = input_stream (); |
2215 | 2196 |
2197 if (isp) | |
2198 { | |
3523 | 2199 std::istream& is = *isp; |
2215 | 2200 |
2201 scanf_format_list fmt_list (fmt); | |
2202 | |
2203 int nconv = fmt_list.num_conversions (); | |
2204 | |
3640 | 2205 if (nconv == -1) |
4468 | 2206 ::error ("%s: invalid format specified", who.c_str ()); |
3640 | 2207 else |
2215 | 2208 { |
3640 | 2209 is.clear (); |
2210 | |
2211 int len = fmt_list.length (); | |
2212 | |
2213 retval.resize (nconv+1, Matrix ()); | |
2214 | |
2215 const scanf_format_elt *elt = fmt_list.first (); | |
2216 | |
2217 int num_values = 0; | |
2218 | |
2219 bool quit = false; | |
2220 | |
5275 | 2221 for (octave_idx_type i = 0; i < len; i++) |
3640 | 2222 { |
2223 octave_value tmp; | |
2224 | |
4468 | 2225 quit = do_oscanf (elt, tmp, who); |
3640 | 2226 |
2227 if (quit) | |
2228 break; | |
2229 else | |
2230 { | |
2231 if (tmp.is_defined ()) | |
2232 retval (num_values++) = tmp; | |
2233 | |
2234 if (! ok ()) | |
2235 break; | |
2236 | |
2237 elt = fmt_list.next (nconv > 0); | |
2238 } | |
2239 } | |
2240 | |
4233 | 2241 retval(nconv) = num_values; |
3640 | 2242 |
2243 if (! quit) | |
2244 { | |
2245 // Pick up any trailing stuff. | |
2246 if (ok () && len > nconv) | |
2247 { | |
2248 octave_value tmp; | |
3704 | 2249 |
2250 elt = fmt_list.next (); | |
2251 | |
4468 | 2252 do_oscanf (elt, tmp, who); |
3640 | 2253 } |
2254 } | |
2117 | 2255 } |
2256 } | |
2257 else | |
4468 | 2258 invalid_operation (who, "reading"); |
2117 | 2259 |
2260 return retval; | |
2261 } | |
2262 | |
2263 // Functions that are defined for all output streams (output streams | |
2264 // are those that define os). | |
2265 | |
2266 int | |
2267 octave_base_stream::flush (void) | |
2268 { | |
2269 int retval = -1; | |
2270 | |
3523 | 2271 std::ostream *os = output_stream (); |
2117 | 2272 |
2273 if (os) | |
2274 { | |
2275 os->flush (); | |
2276 | |
2277 if (os->good ()) | |
2278 retval = 0; | |
2279 } | |
2280 else | |
2281 invalid_operation ("fflush", "writing"); | |
2282 | |
2283 return retval; | |
2284 } | |
2285 | |
2286 class | |
2287 printf_value_cache | |
2288 { | |
2289 public: | |
2290 | |
3653 | 2291 enum state { ok, conversion_error }; |
2117 | 2292 |
7352 | 2293 printf_value_cache (const octave_value_list& args, const std::string& who) |
2117 | 2294 : values (args), val_idx (0), elt_idx (0), |
2295 n_vals (values.length ()), n_elts (0), data (0), | |
7352 | 2296 curr_state (ok) |
2297 { | |
2298 for (octave_idx_type i = 0; i < values.length (); i++) | |
2299 { | |
2300 octave_value val = values(i); | |
2301 | |
2302 if (val.is_map () || val.is_cell () || val.is_object () | |
2303 || val.is_list ()) | |
2304 { | |
2305 gripe_wrong_type_arg (who, val); | |
2306 break; | |
2307 } | |
2308 } | |
2309 } | |
2117 | 2310 |
2311 ~printf_value_cache (void) { } | |
2312 | |
2313 // Get the current value as a double and advance the internal pointer. | |
2314 double double_value (void); | |
2315 | |
2316 // Get the current value as an int and advance the internal pointer. | |
2317 int int_value (void); | |
2318 | |
2319 // Get the current value as a string and advance the internal pointer. | |
3523 | 2320 std::string string_value (void); |
2117 | 2321 |
3145 | 2322 operator bool () const { return (curr_state == ok); } |
2117 | 2323 |
3653 | 2324 bool exhausted (void) { return (val_idx >= n_vals); } |
2117 | 2325 |
2326 private: | |
2327 | |
2328 const octave_value_list values; | |
2329 int val_idx; | |
2330 int elt_idx; | |
2331 int n_vals; | |
2332 int n_elts; | |
2333 const double *data; | |
4874 | 2334 NDArray curr_val; |
2117 | 2335 state curr_state; |
2336 | |
2337 // Must create value cache with values! | |
2338 | |
2339 printf_value_cache (void); | |
2340 | |
2341 // No copying! | |
2342 | |
2343 printf_value_cache (const printf_value_cache&); | |
2344 | |
2345 printf_value_cache& operator = (const printf_value_cache&); | |
2346 }; | |
2347 | |
2348 double | |
2349 printf_value_cache::double_value (void) | |
2350 { | |
2351 double retval = 0.0; | |
2352 | |
3711 | 2353 if (exhausted ()) |
2354 curr_state = conversion_error; | |
2355 | |
2356 while (! exhausted ()) | |
2117 | 2357 { |
2358 if (! data) | |
2359 { | |
2360 octave_value tmp_val = values (val_idx); | |
2361 | |
5476 | 2362 // Force string conversion here for compatibility. |
2363 | |
2364 curr_val = tmp_val.array_value (true); | |
2117 | 2365 |
2366 if (! error_state) | |
2367 { | |
2368 elt_idx = 0; | |
2369 n_elts = curr_val.length (); | |
2370 data = curr_val.data (); | |
2371 } | |
2372 else | |
2373 { | |
2374 curr_state = conversion_error; | |
2375 break; | |
2376 } | |
2377 } | |
2378 | |
2379 if (elt_idx < n_elts) | |
2380 { | |
3653 | 2381 retval = data[elt_idx++]; |
2382 | |
2383 if (elt_idx >= n_elts) | |
2384 { | |
2385 elt_idx = 0; | |
2386 val_idx++; | |
2387 data = 0; | |
2388 } | |
2389 | |
2117 | 2390 break; |
2391 } | |
2392 else | |
2393 { | |
2394 val_idx++; | |
2395 data = 0; | |
3969 | 2396 |
2397 if (n_elts == 0 && exhausted ()) | |
2398 curr_state = conversion_error; | |
2399 | |
2117 | 2400 continue; |
2401 } | |
2402 } | |
2403 | |
2404 return retval; | |
2405 } | |
2406 | |
2407 int | |
2408 printf_value_cache::int_value (void) | |
2409 { | |
2410 int retval = 0; | |
2411 | |
2412 double dval = double_value (); | |
2413 | |
2414 if (! error_state) | |
2415 { | |
2416 if (D_NINT (dval) == dval) | |
2417 retval = NINT (dval); | |
2418 else | |
2419 curr_state = conversion_error; | |
2420 } | |
2421 | |
2422 return retval; | |
2423 } | |
2424 | |
3536 | 2425 std::string |
2117 | 2426 printf_value_cache::string_value (void) |
2427 { | |
3523 | 2428 std::string retval; |
2117 | 2429 |
4425 | 2430 if (exhausted ()) |
2431 curr_state = conversion_error; | |
4257 | 2432 else |
2117 | 2433 { |
4425 | 2434 octave_value tval = values (val_idx++); |
2435 | |
2436 if (tval.rows () == 1) | |
2437 retval = tval.string_value (); | |
2438 else | |
2439 { | |
2440 // In the name of Matlab compatibility. | |
2441 | |
2442 charMatrix chm = tval.char_matrix_value (); | |
2443 | |
5275 | 2444 octave_idx_type nr = chm.rows (); |
2445 octave_idx_type nc = chm.columns (); | |
4425 | 2446 |
2447 int k = 0; | |
2448 | |
2449 retval.resize (nr * nc, '\0'); | |
2450 | |
5275 | 2451 for (octave_idx_type j = 0; j < nc; j++) |
2452 for (octave_idx_type i = 0; i < nr; i++) | |
4425 | 2453 retval[k++] = chm(i,j); |
2454 } | |
2455 | |
2456 if (error_state) | |
2457 curr_state = conversion_error; | |
2117 | 2458 } |
4257 | 2459 |
2117 | 2460 return retval; |
2461 } | |
2462 | |
2463 // Ugh again and again. | |
2464 | |
2572 | 2465 template <class T> |
3620 | 2466 int |
3523 | 2467 do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1, |
4468 | 2468 int sa_2, T arg, const std::string& who) |
2572 | 2469 { |
3620 | 2470 int retval = 0; |
2471 | |
2572 | 2472 switch (nsa) |
2473 { | |
2474 case 2: | |
3640 | 2475 retval = octave_format (os, fmt, sa_1, sa_2, arg); |
2572 | 2476 break; |
2477 | |
2478 case 1: | |
3640 | 2479 retval = octave_format (os, fmt, sa_1, arg); |
2572 | 2480 break; |
2481 | |
2482 case 0: | |
3640 | 2483 retval = octave_format (os, fmt, arg); |
2572 | 2484 break; |
2485 | |
2486 default: | |
4468 | 2487 ::error ("%s: internal error handling format", who.c_str ()); |
2572 | 2488 break; |
2489 } | |
3620 | 2490 |
2491 return retval; | |
2572 | 2492 } |
2493 | |
3620 | 2494 template int |
4468 | 2495 do_printf_conv (std::ostream&, const char*, int, int, int, int, |
2496 const std::string&); | |
2497 | |
2498 template int | |
2499 do_printf_conv (std::ostream&, const char*, int, int, int, long, | |
2500 const std::string&); | |
2501 | |
3878 | 2502 template int |
4468 | 2503 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned int, |
2504 const std::string&); | |
2505 | |
2506 template int | |
2507 do_printf_conv (std::ostream&, const char*, int, int, int, unsigned long, | |
2508 const std::string&); | |
2509 | |
2510 template int | |
2511 do_printf_conv (std::ostream&, const char*, int, int, int, double, | |
2512 const std::string&); | |
2513 | |
2514 template int | |
2515 do_printf_conv (std::ostream&, const char*, int, int, int, const char*, | |
2516 const std::string&); | |
2117 | 2517 |
6492 | 2518 #define DO_DOUBLE_CONV(TQUAL) \ |
2519 do \ | |
2520 { \ | |
7199 | 2521 if (val > std::numeric_limits<TQUAL long>::max () \ |
2522 || val < std::numeric_limits<TQUAL long>::min ()) \ | |
6492 | 2523 { \ |
7199 | 2524 std::string tfmt = fmt; \ |
6492 | 2525 \ |
7199 | 2526 tfmt.replace (tfmt.rfind (elt->type), 1, ".f"); \ |
6492 | 2527 \ |
7199 | 2528 if (elt->modifier == 'l') \ |
2529 tfmt.replace (tfmt.rfind (elt->modifier), 1, ""); \ | |
2530 \ | |
2531 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2, \ | |
2532 val, who); \ | |
6492 | 2533 } \ |
2534 else \ | |
7199 | 2535 retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, \ |
2536 static_cast<TQUAL long> (val), who); \ | |
6492 | 2537 } \ |
2538 while (0) | |
2539 | |
2117 | 2540 int |
2541 octave_base_stream::do_printf (printf_format_list& fmt_list, | |
4468 | 2542 const octave_value_list& args, |
2543 const std::string& who) | |
2117 | 2544 { |
3620 | 2545 int retval = 0; |
2117 | 2546 |
3640 | 2547 int nconv = fmt_list.num_conversions (); |
2548 | |
3523 | 2549 std::ostream *osp = output_stream (); |
2117 | 2550 |
2551 if (osp) | |
2552 { | |
3523 | 2553 std::ostream& os = *osp; |
2117 | 2554 |
2555 const printf_format_elt *elt = fmt_list.first (); | |
2556 | |
7352 | 2557 printf_value_cache val_cache (args, who); |
2558 | |
2559 if (error_state) | |
2560 return retval; | |
2117 | 2561 |
2562 for (;;) | |
2563 { | |
4153 | 2564 OCTAVE_QUIT; |
2565 | |
2117 | 2566 if (elt) |
2567 { | |
3640 | 2568 // NSA is the number of `star' args to convert. |
2569 | |
2570 int nsa = (elt->fw < 0) + (elt->prec < 0); | |
2117 | 2571 |
2572 int sa_1 = 0; | |
2573 int sa_2 = 0; | |
2574 | |
2575 if (nsa > 0) | |
2576 { | |
2577 sa_1 = val_cache.int_value (); | |
2578 | |
2579 if (! val_cache) | |
2580 break; | |
2581 else | |
2582 { | |
2583 if (nsa > 1) | |
2584 { | |
2585 sa_2 = val_cache.int_value (); | |
2586 | |
2587 if (! val_cache) | |
2588 break; | |
2589 } | |
2590 } | |
2591 } | |
2592 | |
2593 const char *fmt = elt->text; | |
2594 | |
3640 | 2595 if (elt->type == '%') |
2596 { | |
2597 os << "%"; | |
2598 retval++; | |
2599 } | |
2600 else if (elt->args == 0 && elt->text) | |
2601 { | |
2602 os << elt->text; | |
2603 retval += strlen (elt->text); | |
2604 } | |
4257 | 2605 else if (elt->type == 's') |
3640 | 2606 { |
2607 std::string val = val_cache.string_value (); | |
2608 | |
2609 if (val_cache) | |
2610 retval += do_printf_conv (os, fmt, nsa, sa_1, | |
4468 | 2611 sa_2, val.c_str (), who); |
3640 | 2612 else |
2613 break; | |
2614 } | |
2117 | 2615 else |
2616 { | |
3640 | 2617 double val = val_cache.double_value (); |
2618 | |
2619 if (val_cache) | |
2117 | 2620 { |
6492 | 2621 if (lo_ieee_isnan (val) || xisinf (val)) |
4303 | 2622 { |
2623 std::string tfmt = fmt; | |
6865 | 2624 std::string::size_type i1, i2; |
2625 | |
2626 tfmt.replace ((i1 = tfmt.rfind (elt->type)), | |
2627 1, 1, 's'); | |
2628 | |
8021 | 2629 if ((i2 = tfmt.rfind ('.')) != std::string::npos && i2 < i1) |
6865 | 2630 { |
2631 tfmt.erase (i2, i1-i2); | |
2632 if (elt->prec < 0) | |
2633 nsa--; | |
2634 } | |
6492 | 2635 |
2636 const char *tval = xisinf (val) | |
2637 ? (val < 0 ? "-Inf" : "Inf") | |
2638 : (lo_ieee_is_NA (val) ? "NA" : "NaN"); | |
2639 | |
2640 retval += do_printf_conv (os, tfmt.c_str (), | |
2641 nsa, sa_1, sa_2, | |
2642 tval, who); | |
4303 | 2643 } |
2644 else | |
3640 | 2645 { |
6492 | 2646 char type = elt->type; |
2647 | |
2648 switch (type) | |
4303 | 2649 { |
2650 case 'd': case 'i': case 'c': | |
7227 | 2651 DO_DOUBLE_CONV (OCTAVE_EMPTY_CPP_ARG); |
4303 | 2652 break; |
2653 | |
2654 case 'o': case 'x': case 'X': case 'u': | |
6492 | 2655 DO_DOUBLE_CONV (unsigned); |
4303 | 2656 break; |
2657 | |
2658 case 'f': case 'e': case 'E': | |
2659 case 'g': case 'G': | |
2660 retval | |
2661 += do_printf_conv (os, fmt, nsa, sa_1, sa_2, | |
4468 | 2662 val, who); |
4303 | 2663 break; |
2664 | |
2665 default: | |
4468 | 2666 error ("%s: invalid format specifier", |
2667 who.c_str ()); | |
4303 | 2668 return -1; |
2669 break; | |
2670 } | |
3640 | 2671 } |
2117 | 2672 } |
2673 else | |
3620 | 2674 break; |
2117 | 2675 } |
2676 | |
3620 | 2677 if (! os) |
2117 | 2678 { |
4468 | 2679 error ("%s: write error", who.c_str ()); |
2117 | 2680 break; |
2681 } | |
2682 } | |
2683 else | |
2684 { | |
4468 | 2685 ::error ("%s: internal error handling format", who.c_str ()); |
2117 | 2686 retval = -1; |
2687 break; | |
2688 } | |
2689 | |
3640 | 2690 elt = fmt_list.next (nconv > 0 && ! val_cache.exhausted ()); |
2691 | |
2692 if (! elt || (val_cache.exhausted () && elt->args > 0)) | |
2693 break; | |
2694 } | |
2117 | 2695 } |
3640 | 2696 else |
4468 | 2697 invalid_operation (who, "writing"); |
2117 | 2698 |
2699 return retval; | |
2700 } | |
2701 | |
2702 int | |
3640 | 2703 octave_base_stream::printf (const std::string& fmt, |
4468 | 2704 const octave_value_list& args, |
2705 const std::string& who) | |
2117 | 2706 { |
3640 | 2707 int retval = 0; |
2708 | |
2709 printf_format_list fmt_list (fmt); | |
2710 | |
2711 if (fmt_list.num_conversions () == -1) | |
4468 | 2712 ::error ("%s: invalid format specified", who.c_str ()); |
2117 | 2713 else |
4468 | 2714 retval = do_printf (fmt_list, args, who); |
2117 | 2715 |
2716 return retval; | |
2717 } | |
2718 | |
2719 int | |
4468 | 2720 octave_base_stream::puts (const std::string& s, const std::string& who) |
2117 | 2721 { |
2722 int retval = -1; | |
2723 | |
3523 | 2724 std::ostream *osp = output_stream (); |
2117 | 2725 |
2726 if (osp) | |
2727 { | |
3523 | 2728 std::ostream& os = *osp; |
2117 | 2729 |
2730 os << s; | |
2731 | |
2732 if (os) | |
2733 { | |
5775 | 2734 // FIXME -- why does this seem to be necessary? |
2117 | 2735 // Without it, output from a loop like |
2736 // | |
2737 // for i = 1:100, fputs (stdout, "foo\n"); endfor | |
2738 // | |
2739 // doesn't seem to go to the pager immediately. | |
2740 | |
2741 os.flush (); | |
2742 | |
2743 if (os) | |
2744 retval = 0; | |
2745 else | |
4468 | 2746 error ("%s: write error", who.c_str ()); |
2117 | 2747 } |
2748 else | |
4468 | 2749 error ("%s: write error", who.c_str ()); |
2117 | 2750 } |
2751 else | |
4468 | 2752 invalid_operation (who, "writing"); |
2117 | 2753 |
2754 return retval; | |
2755 } | |
2756 | |
2757 // Return current error message for this stream. | |
2758 | |
3536 | 2759 std::string |
2435 | 2760 octave_base_stream::error (bool clear_err, int& err_num) |
2117 | 2761 { |
2435 | 2762 err_num = fail ? -1 : 0; |
2117 | 2763 |
3523 | 2764 std::string tmp = errmsg; |
2117 | 2765 |
2766 if (clear_err) | |
2767 clear (); | |
2768 | |
2769 return tmp; | |
2770 } | |
2771 | |
2772 void | |
4468 | 2773 octave_base_stream::invalid_operation (const std::string& who, const char *rw) |
2117 | 2774 { |
4468 | 2775 // Note that this is not ::error () ! |
2776 | |
6297 | 2777 error (who, std::string ("stream not open for ") + rw); |
2117 | 2778 } |
2779 | |
3552 | 2780 octave_stream::octave_stream (octave_base_stream *bs) |
3340 | 2781 : rep (bs) |
2782 { | |
2783 if (rep) | |
2784 rep->count = 1; | |
2785 } | |
2786 | |
2787 octave_stream::~octave_stream (void) | |
2788 { | |
2789 if (rep && --rep->count == 0) | |
2790 delete rep; | |
2791 } | |
2792 | |
2793 octave_stream::octave_stream (const octave_stream& s) | |
2794 : rep (s.rep) | |
2795 { | |
2796 if (rep) | |
2797 rep->count++; | |
2798 } | |
2799 | |
2800 octave_stream& | |
2801 octave_stream::operator = (const octave_stream& s) | |
2802 { | |
2803 if (rep != s.rep) | |
2804 { | |
2805 if (rep && --rep->count == 0) | |
2806 delete rep; | |
2807 | |
2808 rep = s.rep; | |
2809 | |
2810 if (rep) | |
2811 rep->count++; | |
2812 } | |
2813 | |
2814 return *this; | |
2815 } | |
2816 | |
2117 | 2817 int |
2818 octave_stream::flush (void) | |
2819 { | |
2820 int retval = -1; | |
2821 | |
5659 | 2822 if (stream_ok ()) |
2117 | 2823 retval = rep->flush (); |
2824 | |
2825 return retval; | |
2826 } | |
2827 | |
3536 | 2828 std::string |
5275 | 2829 octave_stream::getl (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 2830 { |
3523 | 2831 std::string retval; |
2117 | 2832 |
5659 | 2833 if (stream_ok ()) |
4468 | 2834 retval = rep->getl (max_len, err, who); |
2117 | 2835 |
2836 return retval; | |
2837 } | |
2838 | |
3536 | 2839 std::string |
4468 | 2840 octave_stream::getl (const octave_value& tc_max_len, bool& err, |
2841 const std::string& who) | |
2117 | 2842 { |
3523 | 2843 std::string retval; |
2117 | 2844 |
2845 err = false; | |
2846 | |
2847 int conv_err = 0; | |
2848 | |
6345 | 2849 int max_len = -1; |
2850 | |
2851 if (tc_max_len.is_defined ()) | |
2117 | 2852 { |
6345 | 2853 max_len = convert_to_valid_int (tc_max_len, conv_err); |
2854 | |
2855 if (conv_err || max_len < 0) | |
2856 { | |
2857 err = true; | |
2858 ::error ("%s: invalid maximum length specified", who.c_str ()); | |
2859 } | |
2117 | 2860 } |
6345 | 2861 |
2862 if (! error_state) | |
4468 | 2863 retval = getl (max_len, err, who); |
2117 | 2864 |
2865 return retval; | |
2866 } | |
2867 | |
3536 | 2868 std::string |
5275 | 2869 octave_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
2117 | 2870 { |
3523 | 2871 std::string retval; |
2117 | 2872 |
5659 | 2873 if (stream_ok ()) |
4468 | 2874 retval = rep->gets (max_len, err, who); |
2117 | 2875 |
2876 return retval; | |
2877 } | |
2878 | |
3536 | 2879 std::string |
4468 | 2880 octave_stream::gets (const octave_value& tc_max_len, bool& err, |
2881 const std::string& who) | |
2117 | 2882 { |
3523 | 2883 std::string retval; |
2117 | 2884 |
2885 err = false; | |
2886 | |
2887 int conv_err = 0; | |
2888 | |
6345 | 2889 int max_len = -1; |
2890 | |
2891 if (tc_max_len.is_defined ()) | |
2117 | 2892 { |
6345 | 2893 max_len = convert_to_valid_int (tc_max_len, conv_err); |
2894 | |
2895 if (conv_err || max_len < 0) | |
2896 { | |
2897 err = true; | |
2898 ::error ("%s: invalid maximum length specified", who.c_str ()); | |
2899 } | |
2117 | 2900 } |
6345 | 2901 |
2902 if (! error_state) | |
4468 | 2903 retval = gets (max_len, err, who); |
2117 | 2904 |
2905 return retval; | |
2906 } | |
2907 | |
2908 int | |
4797 | 2909 octave_stream::seek (long offset, int origin) |
2117 | 2910 { |
5065 | 2911 int status = -1; |
2117 | 2912 |
5659 | 2913 if (stream_ok ()) |
4889 | 2914 { |
2915 clearerr (); | |
2916 | |
5065 | 2917 long orig_pos = rep->tell (); |
2918 | |
2919 status = rep->seek (offset, origin); | |
2920 | |
2921 if (status == 0) | |
2922 { | |
2923 long save_pos = rep->tell (); | |
2924 | |
2925 rep->seek (0, SEEK_END); | |
2926 | |
2927 long pos_eof = rep->tell (); | |
2928 | |
2929 // I don't think save_pos can be less than zero, but we'll | |
2930 // check anyway... | |
2931 | |
2932 if (save_pos > pos_eof || save_pos < 0) | |
2933 { | |
2934 // Seek outside bounds of file. Failure should leave | |
2935 // position unchanged. | |
2936 | |
2937 rep->seek (orig_pos, SEEK_SET); | |
2938 | |
2939 status = -1; | |
2940 } | |
2941 else | |
2942 { | |
2943 // Is it possible for this to fail? We are just | |
2944 // returning to a position after the first successful | |
2945 // seek. | |
2946 | |
2947 rep->seek (save_pos, SEEK_SET); | |
2948 } | |
2949 } | |
4889 | 2950 } |
2117 | 2951 |
5065 | 2952 return status; |
2117 | 2953 } |
2954 | |
2955 int | |
2956 octave_stream::seek (const octave_value& tc_offset, | |
2957 const octave_value& tc_origin) | |
2958 { | |
2959 int retval = -1; | |
2960 | |
4797 | 2961 long xoffset = tc_offset.long_value (true); |
4645 | 2962 |
2963 if (! error_state) | |
2117 | 2964 { |
4645 | 2965 int conv_err = 0; |
2966 | |
4797 | 2967 int origin = SEEK_SET; |
2117 | 2968 |
2341 | 2969 if (tc_origin.is_string ()) |
2117 | 2970 { |
3523 | 2971 std::string xorigin = tc_origin.string_value (); |
2341 | 2972 |
2973 if (xorigin == "bof") | |
4797 | 2974 origin = SEEK_SET; |
2341 | 2975 else if (xorigin == "cof") |
4797 | 2976 origin = SEEK_CUR; |
2341 | 2977 else if (xorigin == "eof") |
4797 | 2978 origin = SEEK_END; |
2117 | 2979 else |
2980 conv_err = -1; | |
2981 } | |
2341 | 2982 else |
2983 { | |
2984 int xorigin = convert_to_valid_int (tc_origin, conv_err); | |
2985 | |
2986 if (! conv_err) | |
2987 { | |
2988 if (xorigin == -1) | |
4797 | 2989 origin = SEEK_SET; |
2341 | 2990 else if (xorigin == 0) |
4797 | 2991 origin = SEEK_CUR; |
2341 | 2992 else if (xorigin == 1) |
4797 | 2993 origin = SEEK_END; |
2341 | 2994 else |
2995 conv_err = -1; | |
2996 } | |
2997 } | |
2117 | 2998 |
2999 if (! conv_err) | |
5065 | 3000 { |
3001 retval = seek (xoffset, origin); | |
3002 | |
3003 if (retval != 0) | |
3004 error ("fseek: failed to seek to requested position"); | |
3005 } | |
2117 | 3006 else |
3007 error ("fseek: invalid value for origin"); | |
3008 } | |
3009 else | |
3010 error ("fseek: invalid value for offset"); | |
3011 | |
3012 return retval; | |
3013 } | |
3014 | |
4797 | 3015 long |
3016 octave_stream::tell (void) | |
2117 | 3017 { |
4797 | 3018 long retval = -1; |
2117 | 3019 |
5659 | 3020 if (stream_ok ()) |
2117 | 3021 retval = rep->tell (); |
3022 | |
3023 return retval; | |
3024 } | |
3025 | |
3026 int | |
3027 octave_stream::rewind (void) | |
3028 { | |
6296 | 3029 return seek (0, SEEK_SET); |
2117 | 3030 } |
3031 | |
3340 | 3032 bool |
3033 octave_stream::is_open (void) const | |
3034 { | |
3035 bool retval = false; | |
3036 | |
5659 | 3037 if (stream_ok ()) |
3340 | 3038 retval = rep->is_open (); |
3039 | |
3040 return retval; | |
3041 } | |
3042 | |
3043 void | |
3044 octave_stream::close (void) | |
3045 { | |
5659 | 3046 if (stream_ok ()) |
3340 | 3047 rep->close (); |
3048 } | |
3049 | |
4944 | 3050 template <class RET_T, class READ_T> |
2117 | 3051 octave_value |
5275 | 3052 do_read (octave_stream& strm, octave_idx_type nr, octave_idx_type nc, octave_idx_type block_size, |
7995 | 3053 octave_idx_type skip, bool do_float_fmt_conv, bool do_NA_conv, |
5275 | 3054 oct_mach_info::float_format from_flt_fmt, octave_idx_type& count) |
2117 | 3055 { |
3056 octave_value retval; | |
3057 | |
4944 | 3058 RET_T nda; |
3059 | |
3060 count = 0; | |
3061 | |
3062 typename octave_array_type_traits<RET_T>::element_type elt_zero | |
3063 = typename octave_array_type_traits<RET_T>::element_type (); | |
3064 | |
3065 typename octave_array_type_traits<RET_T>::element_type *dat = 0; | |
3066 | |
5275 | 3067 octave_idx_type max_size = 0; |
3068 | |
3069 octave_idx_type final_nr = 0; | |
3070 octave_idx_type final_nc = 1; | |
4944 | 3071 |
3072 if (nr > 0) | |
3073 { | |
3074 if (nc > 0) | |
3075 { | |
3076 nda.resize (dim_vector (nr, nc), elt_zero); | |
3077 dat = nda.fortran_vec (); | |
3078 max_size = nr * nc; | |
3079 } | |
3080 else | |
3081 { | |
3082 nda.resize (dim_vector (nr, 32), elt_zero); | |
3083 dat = nda.fortran_vec (); | |
3084 max_size = nr * 32; | |
3085 } | |
3086 } | |
3087 else | |
3088 { | |
3089 nda.resize (dim_vector (32, 1), elt_zero); | |
3090 dat = nda.fortran_vec (); | |
3091 max_size = 32; | |
3092 } | |
3093 | |
5775 | 3094 // FIXME -- byte order for Cray? |
4944 | 3095 |
3096 bool swap = false; | |
3097 | |
3098 if (oct_mach_info::words_big_endian ()) | |
3099 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian | |
3100 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g | |
3101 || from_flt_fmt == oct_mach_info::flt_fmt_vax_g); | |
3102 else | |
3103 swap = (from_flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); | |
3104 | |
3105 union | |
3106 { | |
3107 char buf[sizeof (typename octave_type_traits<READ_T>::val_type)]; | |
3108 typename octave_type_traits<READ_T>::val_type val; | |
3109 } u; | |
3110 | |
3111 std::istream *isp = strm.input_stream (); | |
3112 | |
3113 if (isp) | |
3114 { | |
3115 std::istream& is = *isp; | |
3116 | |
5275 | 3117 octave_idx_type elts_read = 0; |
4944 | 3118 |
3119 for (;;) | |
3120 { | |
5775 | 3121 // FIXME -- maybe there should be a special case for |
4944 | 3122 // skip == 0. |
3123 | |
3124 if (is) | |
3125 { | |
3126 if (nr > 0 && nc > 0 && count == max_size) | |
3127 { | |
3128 final_nr = nr; | |
3129 final_nc = nc; | |
3130 | |
3131 break; | |
3132 } | |
3133 | |
3134 is.read (u.buf, sizeof (typename octave_type_traits<READ_T>::val_type)); | |
3135 | |
3136 // We only swap bytes for integer types. For float | |
3137 // types, the format conversion will also handle byte | |
3138 // swapping. | |
3139 | |
3140 if (swap) | |
3141 swap_bytes<sizeof (typename octave_type_traits<READ_T>::val_type)> (u.buf); | |
3142 else if (do_float_fmt_conv) | |
3143 do_float_format_conversion | |
3144 (u.buf, | |
3145 sizeof (typename octave_type_traits<READ_T>::val_type), | |
3146 1, from_flt_fmt, oct_mach_info::float_format ()); | |
3147 | |
3148 typename octave_array_type_traits<RET_T>::element_type tmp | |
3149 = static_cast <typename octave_array_type_traits<RET_T>::element_type> (u.val); | |
3150 | |
5030 | 3151 if (is) |
4944 | 3152 { |
5030 | 3153 if (count == max_size) |
4944 | 3154 { |
5030 | 3155 max_size *= 2; |
3156 | |
3157 if (nr > 0) | |
3158 nda.resize (dim_vector (nr, max_size / nr), | |
3159 elt_zero); | |
3160 else | |
3161 nda.resize (dim_vector (max_size, 1), elt_zero); | |
3162 | |
3163 dat = nda.fortran_vec (); | |
4944 | 3164 } |
3165 | |
7995 | 3166 if (do_NA_conv && __lo_ieee_is_old_NA (tmp)) |
3167 tmp = __lo_ieee_replace_old_NA (tmp); | |
3168 | |
5030 | 3169 dat[count++] = tmp; |
3170 | |
3171 elts_read++; | |
3172 } | |
3173 | |
7538
2c4b0cbda85a
oct-stream.cc (do_read): stop reading if seek fails
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
3174 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
|
3175 |
5030 | 3176 if (skip != 0 && elts_read == block_size) |
3177 { | |
7538
2c4b0cbda85a
oct-stream.cc (do_read): stop reading if seek fails
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
3178 seek_status = strm.seek (skip, SEEK_CUR); |
5030 | 3179 elts_read = 0; |
3180 } | |
3181 | |
7538
2c4b0cbda85a
oct-stream.cc (do_read): stop reading if seek fails
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
3182 if (is.eof () || seek_status < 0) |
5030 | 3183 { |
3184 if (nr > 0) | |
4944 | 3185 { |
5030 | 3186 if (count > nr) |
4944 | 3187 { |
5030 | 3188 final_nr = nr; |
3189 final_nc = (count - 1) / nr + 1; | |
4944 | 3190 } |
3191 else | |
3192 { | |
3193 final_nr = count; | |
3194 final_nc = 1; | |
3195 } | |
3196 } | |
5030 | 3197 else |
3198 { | |
3199 final_nr = count; | |
3200 final_nc = 1; | |
3201 } | |
3202 | |
4944 | 3203 break; |
3204 } | |
3205 } | |
5030 | 3206 else if (is.eof ()) |
3207 break; | |
4944 | 3208 } |
3209 } | |
3210 | |
5030 | 3211 nda.resize (dim_vector (final_nr, final_nc), elt_zero); |
3212 | |
3213 retval = nda; | |
4944 | 3214 |
3215 return retval; | |
3216 } | |
3217 | |
3218 #define DO_READ_VAL_TEMPLATE(RET_T, READ_T) \ | |
3219 template octave_value \ | |
7995 | 3220 do_read<RET_T, READ_T> (octave_stream&, octave_idx_type, octave_idx_type, octave_idx_type, octave_idx_type, bool, bool, \ |
5275 | 3221 oct_mach_info::float_format, octave_idx_type&) |
4944 | 3222 |
5775 | 3223 // FIXME -- should we only have float if it is a different |
4944 | 3224 // size from double? |
3225 | |
3226 #define INSTANTIATE_DO_READ(VAL_T) \ | |
3227 DO_READ_VAL_TEMPLATE (VAL_T, octave_int8); \ | |
3228 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint8); \ | |
3229 DO_READ_VAL_TEMPLATE (VAL_T, octave_int16); \ | |
3230 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint16); \ | |
3231 DO_READ_VAL_TEMPLATE (VAL_T, octave_int32); \ | |
3232 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint32); \ | |
3233 DO_READ_VAL_TEMPLATE (VAL_T, octave_int64); \ | |
3234 DO_READ_VAL_TEMPLATE (VAL_T, octave_uint64); \ | |
3235 DO_READ_VAL_TEMPLATE (VAL_T, float); \ | |
3236 DO_READ_VAL_TEMPLATE (VAL_T, double); \ | |
3237 DO_READ_VAL_TEMPLATE (VAL_T, char); \ | |
3238 DO_READ_VAL_TEMPLATE (VAL_T, signed char); \ | |
3239 DO_READ_VAL_TEMPLATE (VAL_T, unsigned char) | |
3240 | |
4970 | 3241 INSTANTIATE_DO_READ (int8NDArray); |
3242 INSTANTIATE_DO_READ (uint8NDArray); | |
3243 INSTANTIATE_DO_READ (int16NDArray); | |
3244 INSTANTIATE_DO_READ (uint16NDArray); | |
3245 INSTANTIATE_DO_READ (int32NDArray); | |
3246 INSTANTIATE_DO_READ (uint32NDArray); | |
3247 INSTANTIATE_DO_READ (int64NDArray); | |
3248 INSTANTIATE_DO_READ (uint64NDArray); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3249 INSTANTIATE_DO_READ (FloatNDArray); |
4970 | 3250 INSTANTIATE_DO_READ (NDArray); |
3251 INSTANTIATE_DO_READ (charNDArray); | |
5780 | 3252 INSTANTIATE_DO_READ (boolNDArray); |
4944 | 3253 |
7995 | 3254 typedef octave_value (*read_fptr) (octave_stream&, octave_idx_type, octave_idx_type, octave_idx_type, octave_idx_type, bool, bool, |
5275 | 3255 oct_mach_info::float_format ffmt, octave_idx_type&); |
4944 | 3256 |
7433 | 3257 NO_INSTANTIATE_ARRAY_SORT (read_fptr); |
6708 | 3258 INSTANTIATE_ARRAY (read_fptr,); |
4944 | 3259 template class Array2<read_fptr>; |
3260 | |
3261 #define FILL_TABLE_ROW(R, VAL_T) \ | |
3262 read_fptr_table(R,oct_data_conv::dt_int8) = do_read<VAL_T, octave_int8>; \ | |
3263 read_fptr_table(R,oct_data_conv::dt_uint8) = do_read<VAL_T, octave_uint8>; \ | |
3264 read_fptr_table(R,oct_data_conv::dt_int16) = do_read<VAL_T, octave_int16>; \ | |
3265 read_fptr_table(R,oct_data_conv::dt_uint16) = do_read<VAL_T, octave_uint16>; \ | |
3266 read_fptr_table(R,oct_data_conv::dt_int32) = do_read<VAL_T, octave_int32>; \ | |
3267 read_fptr_table(R,oct_data_conv::dt_uint32) = do_read<VAL_T, octave_uint32>; \ | |
3268 read_fptr_table(R,oct_data_conv::dt_int64) = do_read<VAL_T, octave_int64>; \ | |
3269 read_fptr_table(R,oct_data_conv::dt_uint64) = do_read<VAL_T, octave_uint64>; \ | |
3270 read_fptr_table(R,oct_data_conv::dt_single) = do_read<VAL_T, float>; \ | |
3271 read_fptr_table(R,oct_data_conv::dt_double) = do_read<VAL_T, double>; \ | |
3272 read_fptr_table(R,oct_data_conv::dt_char) = do_read<VAL_T, char>; \ | |
3273 read_fptr_table(R,oct_data_conv::dt_schar) = do_read<VAL_T, signed char>; \ | |
4970 | 3274 read_fptr_table(R,oct_data_conv::dt_uchar) = do_read<VAL_T, unsigned char>; \ |
3275 read_fptr_table(R,oct_data_conv::dt_logical) = do_read<VAL_T, unsigned char> | |
4944 | 3276 |
3277 octave_value | |
5275 | 3278 octave_stream::read (const Array<double>& size, octave_idx_type block_size, |
4944 | 3279 oct_data_conv::data_type input_type, |
3280 oct_data_conv::data_type output_type, | |
5275 | 3281 octave_idx_type skip, oct_mach_info::float_format ffmt, |
3282 octave_idx_type& char_count) | |
4944 | 3283 { |
3284 static bool initialized = false; | |
3285 | |
3286 // Table function pointers for return types x read types. | |
3287 | |
4970 | 3288 static Array2<read_fptr> read_fptr_table (oct_data_conv::dt_unknown, 14, 0); |
4944 | 3289 |
3290 if (! initialized) | |
3291 { | |
3292 FILL_TABLE_ROW (oct_data_conv::dt_int8, int8NDArray); | |
3293 FILL_TABLE_ROW (oct_data_conv::dt_uint8, uint8NDArray); | |
3294 FILL_TABLE_ROW (oct_data_conv::dt_int16, int16NDArray); | |
3295 FILL_TABLE_ROW (oct_data_conv::dt_uint16, uint16NDArray); | |
3296 FILL_TABLE_ROW (oct_data_conv::dt_int32, int32NDArray); | |
3297 FILL_TABLE_ROW (oct_data_conv::dt_uint32, uint32NDArray); | |
3298 FILL_TABLE_ROW (oct_data_conv::dt_int64, int64NDArray); | |
3299 FILL_TABLE_ROW (oct_data_conv::dt_uint64, uint64NDArray); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3300 FILL_TABLE_ROW (oct_data_conv::dt_single, FloatNDArray); |
4944 | 3301 FILL_TABLE_ROW (oct_data_conv::dt_double, NDArray); |
3302 FILL_TABLE_ROW (oct_data_conv::dt_char, charNDArray); | |
3303 FILL_TABLE_ROW (oct_data_conv::dt_schar, charNDArray); | |
3304 FILL_TABLE_ROW (oct_data_conv::dt_uchar, charNDArray); | |
4970 | 3305 FILL_TABLE_ROW (oct_data_conv::dt_logical, boolNDArray); |
4944 | 3306 |
3307 initialized = true; | |
3308 } | |
3309 | |
3310 octave_value retval; | |
3311 | |
5659 | 3312 if (stream_ok ()) |
4944 | 3313 { |
5775 | 3314 // FIXME -- we may eventually want to make this extensible. |
3315 | |
3316 // FIXME -- we need a better way to ensure that this | |
4944 | 3317 // numbering stays consistent with the order of the elements in the |
3318 // data_type enum in the oct_data_conv class. | |
3319 | |
3320 char_count = 0; | |
3321 | |
5275 | 3322 octave_idx_type nr = -1; |
3323 octave_idx_type nc = -1; | |
4944 | 3324 |
3325 bool ignore; | |
3326 | |
3327 get_size (size, nr, nc, ignore, "fread"); | |
3328 | |
3329 if (! error_state) | |
3330 { | |
3331 if (nr == 0 || nc == 0) | |
3332 retval = Matrix (nr, nc); | |
3333 else | |
3334 { | |
3335 if (ffmt == oct_mach_info::flt_fmt_unknown) | |
3336 ffmt = float_format (); | |
3337 | |
3338 read_fptr fcn = read_fptr_table (output_type, input_type); | |
3339 | |
3340 bool do_float_fmt_conv = ((input_type == oct_data_conv::dt_double | |
3341 || input_type == oct_data_conv::dt_single) | |
3342 && ffmt != float_format ()); | |
3343 | |
7995 | 3344 bool do_NA_conv = (output_type == oct_data_conv::dt_double); |
3345 | |
4944 | 3346 if (fcn) |
3347 { | |
3348 retval = (*fcn) (*this, nr, nc, block_size, skip, | |
7995 | 3349 do_float_fmt_conv, do_NA_conv, |
3350 ffmt, char_count); | |
4944 | 3351 |
5775 | 3352 // FIXME -- kluge! |
4944 | 3353 |
3354 if (! error_state | |
3355 && (output_type == oct_data_conv::dt_char | |
3356 || output_type == oct_data_conv::dt_schar | |
3357 || output_type == oct_data_conv::dt_uchar)) | |
3358 retval = octave_value (retval.char_matrix_value (), true); | |
3359 } | |
3360 else | |
3361 error ("fread: unable to read and convert requested types"); | |
3362 } | |
3363 } | |
3364 else | |
3365 invalid_operation ("fread", "reading"); | |
3366 } | |
2117 | 3367 |
3368 return retval; | |
3369 } | |
3370 | |
5275 | 3371 octave_idx_type |
3372 octave_stream::write (const octave_value& data, octave_idx_type block_size, | |
3373 oct_data_conv::data_type output_type, octave_idx_type skip, | |
2317 | 3374 oct_mach_info::float_format flt_fmt) |
2117 | 3375 { |
5275 | 3376 octave_idx_type retval = -1; |
2117 | 3377 |
5659 | 3378 if (stream_ok ()) |
4944 | 3379 { |
3380 if (! error_state) | |
3381 { | |
3382 if (flt_fmt == oct_mach_info::flt_fmt_unknown) | |
3383 flt_fmt = float_format (); | |
3384 | |
5275 | 3385 octave_idx_type status = data.write (*this, block_size, output_type, |
4944 | 3386 skip, flt_fmt); |
3387 | |
3388 if (status < 0) | |
3389 error ("fwrite: write error"); | |
3390 else | |
3391 retval = status; | |
3392 } | |
3393 else | |
3394 invalid_operation ("fwrite", "writing"); | |
3395 } | |
2117 | 3396 |
3397 return retval; | |
3398 } | |
3399 | |
4944 | 3400 template <class T> |
3401 void | |
3402 write_int (std::ostream& os, bool swap, const T& val) | |
3403 { | |
3404 typename octave_type_traits<T>::val_type tmp = val.value (); | |
3405 | |
3406 if (swap) | |
3407 swap_bytes<sizeof (typename octave_type_traits<T>::val_type)> (&tmp); | |
3408 | |
3409 os.write (reinterpret_cast<const char *> (&tmp), | |
3410 sizeof (typename octave_type_traits<T>::val_type)); | |
3411 } | |
3412 | |
3413 template void write_int (std::ostream&, bool, const octave_int8&); | |
3414 template void write_int (std::ostream&, bool, const octave_uint8&); | |
3415 template void write_int (std::ostream&, bool, const octave_int16&); | |
3416 template void write_int (std::ostream&, bool, const octave_uint16&); | |
3417 template void write_int (std::ostream&, bool, const octave_int32&); | |
3418 template void write_int (std::ostream&, bool, const octave_uint32&); | |
3419 template void write_int (std::ostream&, bool, const octave_int64&); | |
3420 template void write_int (std::ostream&, bool, const octave_uint64&); | |
3421 | |
3422 template <class T> | |
3423 static inline bool | |
3424 do_write (std::ostream& os, const T& val, oct_data_conv::data_type output_type, | |
3425 oct_mach_info::float_format flt_fmt, bool swap, | |
3426 bool do_float_conversion) | |
3427 { | |
3428 bool retval = true; | |
3429 | |
3430 // For compatibility, Octave converts to the output type, then | |
3431 // writes. This means that truncation happens on the conversion. | |
3432 // For example, the following program prints 0: | |
3433 // | |
3434 // x = int8 (-1) | |
3435 // f = fopen ("foo.dat", "w"); | |
3436 // fwrite (f, x, "unsigned char"); | |
3437 // fclose (f); | |
3438 // f = fopen ("foo.dat", "r"); | |
3439 // y = fread (f, 1, "unsigned char"); | |
3440 // printf ("%d\n", y); | |
3441 | |
3442 switch (output_type) | |
3443 { | |
3444 case oct_data_conv::dt_char: | |
3445 case oct_data_conv::dt_schar: | |
3446 case oct_data_conv::dt_int8: | |
3447 write_int (os, swap, octave_int8 (val)); | |
3448 break; | |
3449 | |
3450 case oct_data_conv::dt_uchar: | |
3451 case oct_data_conv::dt_uint8: | |
3452 write_int (os, swap, octave_uint8 (val)); | |
3453 break; | |
3454 | |
3455 case oct_data_conv::dt_int16: | |
3456 write_int (os, swap, octave_int16 (val)); | |
3457 break; | |
3458 | |
3459 case oct_data_conv::dt_uint16: | |
3460 write_int (os, swap, octave_uint16 (val)); | |
3461 break; | |
3462 | |
3463 case oct_data_conv::dt_int32: | |
3464 write_int (os, swap, octave_int32 (val)); | |
3465 break; | |
3466 | |
3467 case oct_data_conv::dt_uint32: | |
3468 write_int (os, swap, octave_uint32 (val)); | |
3469 break; | |
3470 | |
3471 case oct_data_conv::dt_int64: | |
3472 write_int (os, swap, octave_int64 (val)); | |
3473 break; | |
3474 | |
3475 case oct_data_conv::dt_uint64: | |
3476 write_int (os, swap, octave_uint64 (val)); | |
3477 break; | |
3478 | |
3479 case oct_data_conv::dt_single: | |
3480 { | |
3481 float f = static_cast<float> (val); | |
3482 | |
3483 if (do_float_conversion) | |
3484 do_float_format_conversion (&f, 1, flt_fmt); | |
3485 | |
3486 os.write (reinterpret_cast<const char *> (&f), sizeof (float)); | |
3487 } | |
3488 break; | |
3489 | |
3490 case oct_data_conv::dt_double: | |
3491 { | |
3492 double d = static_cast<double> (val); | |
3493 if (do_float_conversion) | |
3494 do_double_format_conversion (&d, 1, flt_fmt); | |
3495 | |
3496 os.write (reinterpret_cast<const char *> (&d), sizeof (double)); | |
3497 } | |
3498 break; | |
3499 | |
3500 default: | |
3501 retval = false; | |
3502 (*current_liboctave_error_handler) | |
3503 ("write: invalid type specification"); | |
3504 break; | |
3505 } | |
3506 | |
3507 return retval; | |
3508 } | |
3509 | |
5887 | 3510 template bool |
3511 do_write (std::ostream&, const octave_int8&, oct_data_conv::data_type, | |
3512 oct_mach_info::float_format, bool, bool); | |
3513 | |
3514 template bool | |
3515 do_write (std::ostream&, const octave_uint8&, oct_data_conv::data_type, | |
3516 oct_mach_info::float_format, bool, bool); | |
3517 | |
3518 template bool | |
3519 do_write (std::ostream&, const octave_int16&, oct_data_conv::data_type, | |
3520 oct_mach_info::float_format, bool, bool); | |
3521 | |
3522 template bool | |
3523 do_write (std::ostream&, const octave_uint16&, oct_data_conv::data_type, | |
3524 oct_mach_info::float_format, bool, bool); | |
3525 | |
3526 template bool | |
3527 do_write (std::ostream&, const octave_int32&, oct_data_conv::data_type, | |
3528 oct_mach_info::float_format, bool, bool); | |
3529 | |
3530 template bool | |
3531 do_write (std::ostream&, const octave_uint32&, oct_data_conv::data_type, | |
3532 oct_mach_info::float_format, bool, bool); | |
3533 | |
3534 template bool | |
3535 do_write (std::ostream&, const octave_int64&, oct_data_conv::data_type, | |
3536 oct_mach_info::float_format, bool, bool); | |
3537 | |
3538 template bool | |
3539 do_write (std::ostream&, const octave_uint64&, oct_data_conv::data_type, | |
3540 oct_mach_info::float_format, bool, bool); | |
3541 | |
4944 | 3542 template <class T> |
5275 | 3543 octave_idx_type |
3544 octave_stream::write (const Array<T>& data, octave_idx_type block_size, | |
4944 | 3545 oct_data_conv::data_type output_type, |
5275 | 3546 octave_idx_type skip, oct_mach_info::float_format flt_fmt) |
4944 | 3547 { |
5275 | 3548 octave_idx_type retval = -1; |
4944 | 3549 |
3550 bool status = true; | |
3551 | |
5275 | 3552 octave_idx_type count = 0; |
4944 | 3553 |
3554 const T *d = data.data (); | |
3555 | |
5275 | 3556 octave_idx_type n = data.length (); |
4944 | 3557 |
3558 oct_mach_info::float_format native_flt_fmt | |
3559 = oct_mach_info::float_format (); | |
3560 | |
3561 bool do_float_conversion = (flt_fmt != native_flt_fmt); | |
3562 | |
5775 | 3563 // FIXME -- byte order for Cray? |
4944 | 3564 |
3565 bool swap = false; | |
3566 | |
3567 if (oct_mach_info::words_big_endian ()) | |
3568 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian | |
3569 || flt_fmt == oct_mach_info::flt_fmt_vax_g | |
3570 || flt_fmt == oct_mach_info::flt_fmt_vax_g); | |
3571 else | |
3572 swap = (flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian); | |
3573 | |
5275 | 3574 for (octave_idx_type i = 0; i < n; i++) |
4944 | 3575 { |
3576 std::ostream *osp = output_stream (); | |
3577 | |
3578 if (osp) | |
3579 { | |
3580 std::ostream& os = *osp; | |
3581 | |
5254 | 3582 // It seems that Matlab writes zeros instead of actually |
3583 // seeking. Hmm... | |
3584 | |
4944 | 3585 if (skip != 0 && (i % block_size) == 0) |
5254 | 3586 { |
5775 | 3587 // FIXME -- probably should try to write larger |
5254 | 3588 // blocks... |
3589 | |
3590 unsigned char zero = 0; | |
3591 for (int j = 0; j < skip; j++) | |
3592 os.write (reinterpret_cast<const char *> (&zero), 1); | |
3593 } | |
4944 | 3594 |
3595 if (os) | |
3596 { | |
3597 status = do_write (os, d[i], output_type, flt_fmt, swap, | |
3598 do_float_conversion); | |
3599 | |
3600 if (os && status) | |
3601 count++; | |
3602 else | |
3603 break; | |
3604 } | |
3605 else | |
3606 { | |
3607 status = false; | |
3608 break; | |
3609 } | |
3610 } | |
3611 else | |
3612 { | |
3613 status = false; | |
3614 break; | |
3615 } | |
3616 } | |
3617 | |
3618 if (status) | |
3619 retval = count; | |
3620 | |
3621 return retval; | |
3622 } | |
3623 | |
5275 | 3624 template octave_idx_type |
3625 octave_stream::write (const Array<char>&, octave_idx_type, | |
4944 | 3626 oct_data_conv::data_type, |
5275 | 3627 octave_idx_type, oct_mach_info::float_format); |
3628 | |
3629 template octave_idx_type | |
3630 octave_stream::write (const Array<bool>&, octave_idx_type, | |
4970 | 3631 oct_data_conv::data_type, |
5275 | 3632 octave_idx_type, oct_mach_info::float_format); |
3633 | |
3634 template octave_idx_type | |
3635 octave_stream::write (const Array<double>&, octave_idx_type, | |
4944 | 3636 oct_data_conv::data_type, |
5275 | 3637 octave_idx_type, oct_mach_info::float_format); |
3638 | |
3639 template octave_idx_type | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3640 octave_stream::write (const Array<float>&, octave_idx_type, |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3641 oct_data_conv::data_type, |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3642 octave_idx_type, oct_mach_info::float_format); |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3643 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7724
diff
changeset
|
3644 template octave_idx_type |
5275 | 3645 octave_stream::write (const Array<octave_int8>&, octave_idx_type, |
4944 | 3646 oct_data_conv::data_type, |
5275 | 3647 octave_idx_type, oct_mach_info::float_format); |
3648 | |
3649 template octave_idx_type | |
3650 octave_stream::write (const Array<octave_uint8>&, octave_idx_type, | |
4944 | 3651 oct_data_conv::data_type, |
5275 | 3652 octave_idx_type, oct_mach_info::float_format); |
3653 | |
3654 template octave_idx_type | |
3655 octave_stream::write (const Array<octave_int16>&, octave_idx_type, | |
4944 | 3656 oct_data_conv::data_type, |
5275 | 3657 octave_idx_type, oct_mach_info::float_format); |
3658 | |
3659 template octave_idx_type | |
3660 octave_stream::write (const Array<octave_uint16>&, octave_idx_type, | |
4944 | 3661 oct_data_conv::data_type, |
5275 | 3662 octave_idx_type, oct_mach_info::float_format); |
3663 | |
3664 template octave_idx_type | |
3665 octave_stream::write (const Array<octave_int32>&, octave_idx_type, | |
4944 | 3666 oct_data_conv::data_type, |
5275 | 3667 octave_idx_type, oct_mach_info::float_format); |
3668 | |
3669 template octave_idx_type | |
3670 octave_stream::write (const Array<octave_uint32>&, octave_idx_type, | |
4944 | 3671 oct_data_conv::data_type, |
5275 | 3672 octave_idx_type, oct_mach_info::float_format); |
3673 | |
3674 template octave_idx_type | |
3675 octave_stream::write (const Array<octave_int64>&, octave_idx_type, | |
4944 | 3676 oct_data_conv::data_type, |
5275 | 3677 octave_idx_type, oct_mach_info::float_format); |
3678 | |
3679 template octave_idx_type | |
3680 octave_stream::write (const Array<octave_uint64>&, octave_idx_type, | |
4944 | 3681 oct_data_conv::data_type, |
5275 | 3682 octave_idx_type, oct_mach_info::float_format); |
4944 | 3683 |
2117 | 3684 octave_value |
3810 | 3685 octave_stream::scanf (const std::string& fmt, const Array<double>& size, |
5275 | 3686 octave_idx_type& count, const std::string& who) |
2117 | 3687 { |
3688 octave_value retval; | |
3689 | |
5659 | 3690 if (stream_ok ()) |
4468 | 3691 retval = rep->scanf (fmt, size, count, who); |
2117 | 3692 |
3693 return retval; | |
3694 } | |
3695 | |
5279 | 3696 octave_value |
3697 octave_stream::scanf (const octave_value& fmt, const Array<double>& size, | |
5299 | 3698 octave_idx_type& count, const std::string& who) |
5279 | 3699 { |
3700 octave_value retval = Matrix (); | |
3701 | |
3702 if (fmt.is_string ()) | |
3703 { | |
3704 std::string sfmt = fmt.string_value (); | |
3705 | |
3706 if (fmt.is_sq_string ()) | |
3707 sfmt = do_string_escapes (sfmt); | |
3708 | |
3709 retval = scanf (sfmt, size, count, who); | |
3710 } | |
3711 else | |
3712 { | |
3713 // Note that this is not ::error () ! | |
3714 | |
3715 error (who + ": format must be a string"); | |
3716 } | |
3717 | |
3718 return retval; | |
3719 } | |
3720 | |
2215 | 3721 octave_value_list |
4468 | 3722 octave_stream::oscanf (const std::string& fmt, const std::string& who) |
2215 | 3723 { |
3724 octave_value_list retval; | |
3725 | |
5659 | 3726 if (stream_ok ()) |
4468 | 3727 retval = rep->oscanf (fmt, who); |
2215 | 3728 |
3729 return retval; | |
3730 } | |
3731 | |
5279 | 3732 octave_value_list |
3733 octave_stream::oscanf (const octave_value& fmt, const std::string& who) | |
3734 { | |
3735 octave_value_list retval; | |
3736 | |
3737 if (fmt.is_string ()) | |
3738 { | |
3739 std::string sfmt = fmt.string_value (); | |
3740 | |
3741 if (fmt.is_sq_string ()) | |
3742 sfmt = do_string_escapes (sfmt); | |
3743 | |
3744 retval = oscanf (sfmt, who); | |
3745 } | |
3746 else | |
3747 { | |
3748 // Note that this is not ::error () ! | |
3749 | |
3750 error (who + ": format must be a string"); | |
3751 } | |
3752 | |
3753 return retval; | |
3754 } | |
3755 | |
2117 | 3756 int |
4468 | 3757 octave_stream::printf (const std::string& fmt, const octave_value_list& args, |
3758 const std::string& who) | |
2117 | 3759 { |
3760 int retval = -1; | |
3761 | |
5659 | 3762 if (stream_ok ()) |
4468 | 3763 retval = rep->printf (fmt, args, who); |
2117 | 3764 |
3765 return retval; | |
3766 } | |
3767 | |
3768 int | |
5279 | 3769 octave_stream::printf (const octave_value& fmt, const octave_value_list& args, |
3770 const std::string& who) | |
3771 { | |
3772 int retval = 0; | |
3773 | |
3774 if (fmt.is_string ()) | |
3775 { | |
3776 std::string sfmt = fmt.string_value (); | |
3777 | |
3778 if (fmt.is_sq_string ()) | |
3779 sfmt = do_string_escapes (sfmt); | |
3780 | |
3781 retval = printf (sfmt, args, who); | |
3782 } | |
3783 else | |
3784 { | |
3785 // Note that this is not ::error () ! | |
3786 | |
3787 error (who + ": format must be a string"); | |
3788 } | |
3789 | |
3790 return retval; | |
3791 } | |
3792 | |
3793 int | |
4468 | 3794 octave_stream::puts (const std::string& s, const std::string& who) |
2117 | 3795 { |
3796 int retval = -1; | |
3797 | |
5659 | 3798 if (stream_ok ()) |
4468 | 3799 retval = rep->puts (s, who); |
2117 | 3800 |
3801 return retval; | |
3802 } | |
3803 | |
5775 | 3804 // FIXME -- maybe this should work for string arrays too. |
2117 | 3805 |
3806 int | |
4468 | 3807 octave_stream::puts (const octave_value& tc_s, const std::string& who) |
2117 | 3808 { |
3809 int retval = -1; | |
3810 | |
3811 if (tc_s.is_string ()) | |
3812 { | |
3523 | 3813 std::string s = tc_s.string_value (); |
5279 | 3814 retval = puts (s, who); |
2117 | 3815 } |
3816 else | |
4468 | 3817 { |
3818 // Note that this is not ::error () ! | |
3819 | |
3820 error (who + ": argument must be a string"); | |
3821 } | |
2117 | 3822 |
3823 return retval; | |
3824 } | |
3825 | |
3826 bool | |
3827 octave_stream::eof (void) const | |
3828 { | |
3829 int retval = -1; | |
3830 | |
5659 | 3831 if (stream_ok ()) |
2117 | 3832 retval = rep->eof (); |
3833 | |
3834 return retval; | |
3835 } | |
3836 | |
3536 | 3837 std::string |
2435 | 3838 octave_stream::error (bool clear, int& err_num) |
2117 | 3839 { |
5649 | 3840 std::string retval = "invalid stream object"; |
3841 | |
5659 | 3842 if (stream_ok (false)) |
2435 | 3843 retval = rep->error (clear, err_num); |
2117 | 3844 |
3845 return retval; | |
3846 } | |
3847 | |
3536 | 3848 std::string |
3340 | 3849 octave_stream::name (void) const |
2117 | 3850 { |
3523 | 3851 std::string retval; |
2117 | 3852 |
5659 | 3853 if (stream_ok ()) |
2117 | 3854 retval = rep->name (); |
3855 | |
3856 return retval; | |
3857 } | |
3858 | |
3859 int | |
3340 | 3860 octave_stream::mode (void) const |
2117 | 3861 { |
3862 int retval = 0; | |
3863 | |
5659 | 3864 if (stream_ok ()) |
2117 | 3865 retval = rep->mode (); |
3866 | |
3867 return retval; | |
3868 } | |
3869 | |
2317 | 3870 oct_mach_info::float_format |
3340 | 3871 octave_stream::float_format (void) const |
2117 | 3872 { |
4574 | 3873 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
2317 | 3874 |
5659 | 3875 if (stream_ok ()) |
2317 | 3876 retval = rep->float_format (); |
2117 | 3877 |
3878 return retval; | |
3879 } | |
3880 | |
3536 | 3881 std::string |
2117 | 3882 octave_stream::mode_as_string (int mode) |
3883 { | |
3523 | 3884 std::string retval = "???"; |
3775 | 3885 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode); |
3886 | |
3887 if (in_mode == std::ios::in) | |
3888 retval = "r"; | |
3889 else if (in_mode == std::ios::out | |
4078 | 3890 || in_mode == (std::ios::out | std::ios::trunc)) |
3775 | 3891 retval = "w"; |
4078 | 3892 else if (in_mode == (std::ios::out | std::ios::app)) |
3775 | 3893 retval = "a"; |
4078 | 3894 else if (in_mode == (std::ios::in | std::ios::out)) |
3775 | 3895 retval = "r+"; |
4078 | 3896 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc)) |
3775 | 3897 retval = "w+"; |
4078 | 3898 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate)) |
3775 | 3899 retval = "a+"; |
4078 | 3900 else if (in_mode == (std::ios::in | std::ios::binary)) |
3775 | 3901 retval = "rb"; |
4078 | 3902 else if (in_mode == (std::ios::out | std::ios::binary) |
3903 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary)) | |
3775 | 3904 retval = "wb"; |
4078 | 3905 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary)) |
3775 | 3906 retval = "ab"; |
4078 | 3907 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary)) |
3775 | 3908 retval = "r+b"; |
4078 | 3909 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc |
3910 | std::ios::binary)) | |
3775 | 3911 retval = "w+b"; |
4078 | 3912 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate |
3913 | std::ios::binary)) | |
3775 | 3914 retval = "a+b"; |
2117 | 3915 |
3916 return retval; | |
3917 } | |
3918 | |
3919 octave_stream_list *octave_stream_list::instance = 0; | |
3920 | |
2926 | 3921 bool |
3922 octave_stream_list::instance_ok (void) | |
3923 { | |
3924 bool retval = true; | |
3925 | |
3926 if (! instance) | |
3927 instance = new octave_stream_list (); | |
3928 | |
3929 if (! instance) | |
3930 { | |
3931 ::error ("unable to create stream list object!"); | |
3932 | |
3933 retval = false; | |
3934 } | |
3935 | |
3936 return retval; | |
3937 } | |
3938 | |
5353 | 3939 int |
6757 | 3940 octave_stream_list::insert (octave_stream& os) |
2926 | 3941 { |
5353 | 3942 return (instance_ok ()) ? instance->do_insert (os) : -1; |
2926 | 3943 } |
3944 | |
3340 | 3945 octave_stream |
3523 | 3946 octave_stream_list::lookup (int fid, const std::string& who) |
2926 | 3947 { |
3341 | 3948 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926 | 3949 } |
3950 | |
3340 | 3951 octave_stream |
3523 | 3952 octave_stream_list::lookup (const octave_value& fid, const std::string& who) |
2926 | 3953 { |
3341 | 3954 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream (); |
2926 | 3955 } |
3956 | |
3957 int | |
3523 | 3958 octave_stream_list::remove (int fid, const std::string& who) |
2926 | 3959 { |
3341 | 3960 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926 | 3961 } |
3962 | |
3963 int | |
3523 | 3964 octave_stream_list::remove (const octave_value& fid, const std::string& who) |
2926 | 3965 { |
3341 | 3966 return (instance_ok ()) ? instance->do_remove (fid, who) : -1; |
2926 | 3967 } |
3968 | |
3969 void | |
3970 octave_stream_list::clear (void) | |
3971 { | |
3972 if (instance) | |
3973 instance->do_clear (); | |
3974 } | |
3975 | |
3976 string_vector | |
3977 octave_stream_list::get_info (int fid) | |
3978 { | |
3979 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); | |
3980 } | |
3981 | |
3982 string_vector | |
3983 octave_stream_list::get_info (const octave_value& fid) | |
3984 { | |
3985 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector (); | |
3986 } | |
3987 | |
3536 | 3988 std::string |
2926 | 3989 octave_stream_list::list_open_files (void) |
3990 { | |
3523 | 3991 return (instance_ok ()) ? instance->do_list_open_files () : std::string (); |
2926 | 3992 } |
3993 | |
3994 octave_value | |
3995 octave_stream_list::open_file_numbers (void) | |
3996 { | |
3997 return (instance_ok ()) | |
3998 ? instance->do_open_file_numbers () : octave_value (); | |
3999 } | |
4000 | |
4001 int | |
4002 octave_stream_list::get_file_number (const octave_value& fid) | |
4003 { | |
4004 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1; | |
4005 } | |
4006 | |
5353 | 4007 int |
6757 | 4008 octave_stream_list::do_insert (octave_stream& os) |
2117 | 4009 { |
6757 | 4010 // Insert item with key corresponding to file-descriptor. |
4011 | |
4012 int stream_number; | |
4013 | |
4014 if ((stream_number = os.file_number ()) == -1) | |
4015 return stream_number; | |
4016 | |
4017 // Should we test for "(list.find (stream_number) != list.end ()) && | |
4018 // list[stream_number].is_open ()" and respond with "error | |
4019 // ("internal error: ...")"? It should not happen except for some | |
4020 // bug or if the user has opened a stream with an interpreted | |
4021 // command, but closed it directly with a system call in an | |
4022 // oct-file; then the kernel knows the fd is free, but Octave does | |
4023 // not know. If it happens, it should not do harm here to simply | |
4024 // overwrite this entry, although the wrong entry might have done | |
4025 // harm before. | |
4026 | |
4027 if (list.size () < list.max_size ()) | |
4028 list[stream_number] = os; | |
4029 else | |
2117 | 4030 { |
6757 | 4031 stream_number = -1; |
4032 error ("could not create file id"); | |
3340 | 4033 } |
2117 | 4034 |
5353 | 4035 return stream_number; |
6757 | 4036 |
2117 | 4037 } |
4038 | |
3341 | 4039 static void |
3523 | 4040 gripe_invalid_file_id (int fid, const std::string& who) |
3341 | 4041 { |
4042 if (who.empty ()) | |
4043 ::error ("invalid stream number = %d", fid); | |
4044 else | |
4045 ::error ("%s: invalid stream number = %d", who.c_str (), fid); | |
4046 } | |
4047 | |
3340 | 4048 octave_stream |
3523 | 4049 octave_stream_list::do_lookup (int fid, const std::string& who) const |
2117 | 4050 { |
3340 | 4051 octave_stream retval; |
2117 | 4052 |
6757 | 4053 if (fid >= 0) |
4054 { | |
4055 ostrl_map::const_iterator iter = list.find (fid); | |
4056 | |
4057 if (iter != list.end ()) | |
4058 retval = iter->second; | |
4059 else | |
4060 gripe_invalid_file_id (fid, who); | |
4061 } | |
3341 | 4062 else |
4063 gripe_invalid_file_id (fid, who); | |
2117 | 4064 |
4065 return retval; | |
4066 } | |
4067 | |
3340 | 4068 octave_stream |
3341 | 4069 octave_stream_list::do_lookup (const octave_value& fid, |
3523 | 4070 const std::string& who) const |
2117 | 4071 { |
3340 | 4072 octave_stream retval; |
2117 | 4073 |
4074 int i = get_file_number (fid); | |
4075 | |
4076 if (! error_state) | |
3341 | 4077 retval = do_lookup (i, who); |
2117 | 4078 |
4079 return retval; | |
4080 } | |
4081 | |
4082 int | |
3523 | 4083 octave_stream_list::do_remove (int fid, const std::string& who) |
2117 | 4084 { |
4085 int retval = -1; | |
4086 | |
3531 | 4087 // Can't remove stdin (std::cin), stdout (std::cout), or stderr |
4088 // (std::cerr). | |
2117 | 4089 |
6757 | 4090 if (fid > 2) |
2117 | 4091 { |
6757 | 4092 ostrl_map::iterator iter = list.find (fid); |
4093 | |
4094 if (iter != list.end ()) | |
2117 | 4095 { |
6757 | 4096 octave_stream os = iter->second; |
4097 | |
4098 if (os.is_valid ()) | |
4099 { | |
4100 os.close (); | |
4101 iter->second = octave_stream (); | |
4102 retval = 0; | |
4103 } | |
4104 else | |
4105 gripe_invalid_file_id (fid, who); | |
2117 | 4106 } |
3341 | 4107 else |
4108 gripe_invalid_file_id (fid, who); | |
2117 | 4109 } |
3341 | 4110 else |
4111 gripe_invalid_file_id (fid, who); | |
2117 | 4112 |
4113 return retval; | |
4114 } | |
4115 | |
4116 int | |
3523 | 4117 octave_stream_list::do_remove (const octave_value& fid, const std::string& who) |
2117 | 4118 { |
4119 int retval = -1; | |
4120 | |
6054 | 4121 if (fid.is_string () && fid.string_value () == "all") |
4122 { | |
6757 | 4123 for (ostrl_map::iterator p = list.begin (); p != list.end (); p++) |
6054 | 4124 { |
6757 | 4125 // Skip stdin, stdout, and stderr. |
4126 | |
4127 if (p->first > 2) | |
4128 { | |
4129 octave_stream os = p->second; | |
4130 | |
4131 if (os.is_valid ()) | |
4132 do_remove (p->first, who); | |
4133 } | |
6054 | 4134 } |
4135 | |
4136 retval = 0; | |
4137 } | |
4138 else | |
4139 { | |
4140 int i = get_file_number (fid); | |
4141 | |
4142 if (! error_state) | |
4143 retval = do_remove (i, who); | |
4144 } | |
2117 | 4145 |
4146 return retval; | |
4147 } | |
4148 | |
4149 void | |
4150 octave_stream_list::do_clear (void) | |
4151 { | |
4152 // Do flush stdout and stderr. | |
4153 | |
6757 | 4154 list[0].flush (); |
4155 list[1].flush (); | |
2117 | 4156 |
4157 // But don't delete them or stdin. | |
4158 | |
6757 | 4159 for (ostrl_map::iterator p = list.begin (); p != list.end (); p++) |
4160 { | |
4161 // Skip stdin, stdout, and stderr. | |
4162 | |
4163 if (p->first > 2) | |
4164 p->second = octave_stream (); | |
4165 } | |
2117 | 4166 } |
4167 | |
4168 string_vector | |
4169 octave_stream_list::do_get_info (int fid) const | |
4170 { | |
4171 string_vector retval; | |
4172 | |
3340 | 4173 octave_stream os = do_lookup (fid); |
2117 | 4174 |
3341 | 4175 if (os.is_valid ()) |
2117 | 4176 { |
4177 retval.resize (3); | |
4178 | |
3340 | 4179 retval(0) = os.name (); |
4180 retval(1) = octave_stream::mode_as_string (os.mode ()); | |
4181 retval(2) = oct_mach_info::float_format_as_string (os.float_format ()); | |
2117 | 4182 } |
4183 else | |
3341 | 4184 ::error ("invalid file id = %d", fid); |
2117 | 4185 |
4186 return retval; | |
4187 } | |
4188 | |
4189 string_vector | |
4190 octave_stream_list::do_get_info (const octave_value& fid) const | |
4191 { | |
4192 string_vector retval; | |
4193 | |
4194 int conv_err = 0; | |
4195 | |
4196 int int_fid = convert_to_valid_int (fid, conv_err); | |
4197 | |
4198 if (! conv_err) | |
4199 retval = do_get_info (int_fid); | |
4200 else | |
2915 | 4201 ::error ("file id must be a file object or integer value"); |
2117 | 4202 |
4203 return retval; | |
4204 } | |
4205 | |
3536 | 4206 std::string |
2117 | 4207 octave_stream_list::do_list_open_files (void) const |
4208 { | |
3523 | 4209 std::string retval; |
2117 | 4210 |
5765 | 4211 std::ostringstream buf; |
2117 | 4212 |
4213 buf << "\n" | |
4214 << " number mode arch name\n" | |
4215 << " ------ ---- ---- ----\n"; | |
4216 | |
6757 | 4217 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4218 { |
6757 | 4219 octave_stream os = p->second; |
2117 | 4220 |
4326 | 4221 buf << " " |
4222 << std::setiosflags (std::ios::right) | |
6757 | 4223 << std::setw (4) << p->first << " " |
4326 | 4224 << std::setiosflags (std::ios::left) |
4225 << std::setw (3) | |
4226 << octave_stream::mode_as_string (os.mode ()) | |
4227 << " " | |
4228 << std::setw (9) | |
4229 << oct_mach_info::float_format_as_string (os.float_format ()) | |
4230 << " " | |
4231 << os.name () << "\n"; | |
2117 | 4232 } |
4233 | |
5765 | 4234 buf << "\n"; |
4235 | |
4236 retval = buf.str (); | |
2117 | 4237 |
4238 return retval; | |
4239 } | |
4240 | |
4241 octave_value | |
4242 octave_stream_list::do_open_file_numbers (void) const | |
4243 { | |
6757 | 4244 Matrix retval (1, list.size (), 0.0); |
2117 | 4245 |
4246 int num_open = 0; | |
4247 | |
6757 | 4248 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4249 { |
6757 | 4250 // Skip stdin, stdout, and stderr. |
4251 | |
4252 if (p->first > 2 && p->second) | |
4253 retval(0,num_open++) = p->first; | |
2117 | 4254 } |
4255 | |
4256 retval.resize ((num_open > 0), num_open); | |
4257 | |
4258 return retval; | |
4259 } | |
4260 | |
4261 int | |
2609 | 4262 octave_stream_list::do_get_file_number (const octave_value& fid) const |
2117 | 4263 { |
4264 int retval = -1; | |
4265 | |
4266 if (fid.is_string ()) | |
4267 { | |
3523 | 4268 std::string nm = fid.string_value (); |
2117 | 4269 |
6757 | 4270 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++) |
2117 | 4271 { |
6757 | 4272 // stdin (std::cin), stdout (std::cout), and stderr (std::cerr) |
4273 // are unnamed. | |
4274 | |
4275 if (p->first > 2) | |
2117 | 4276 { |
6757 | 4277 octave_stream os = p->second; |
4278 | |
4279 if (os && os.name () == nm) | |
4280 { | |
4281 retval = p->first; | |
4282 break; | |
4283 } | |
2117 | 4284 } |
4285 } | |
4286 } | |
4287 else | |
4288 { | |
4289 int conv_err = 0; | |
4290 | |
4291 int int_fid = convert_to_valid_int (fid, conv_err); | |
4292 | |
4293 if (conv_err) | |
3523 | 4294 ::error ("file id must be a file object, std::string, or integer value"); |
2117 | 4295 else |
4296 retval = int_fid; | |
4297 } | |
4298 | |
4299 return retval; | |
4300 } | |
4301 | |
4302 /* | |
4303 ;;; Local Variables: *** | |
4304 ;;; mode: C++ *** | |
4305 ;;; End: *** | |
4306 */ |