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