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