GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-stream.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <cassert>
31#include <cctype>
32#include <cstring>
33
34#include <algorithm>
35#include <deque>
36#include <fstream>
37#include <iomanip>
38#include <iostream>
39#include <sstream>
40#include <string>
41
42#include "Array.h"
43#include "Cell.h"
44#include "byte-swap.h"
45#include "lo-cutils.h"
46#include "lo-ieee.h"
47#include "lo-mappers.h"
48#include "lo-utils.h"
49#include "oct-locbuf.h"
51#include "quit.h"
52#include "str-vec.h"
53
54#include "error.h"
55#include "errwarn.h"
56#include "input.h"
57#include "interpreter-private.h"
58#include "interpreter.h"
59#include "octave.h"
60#include "oct-iostrm.h"
61#include "oct-stdstrm.h"
62#include "oct-string.h"
63#include "oct-stream.h"
64#include "ov.h"
65#include "ovl.h"
66#include "pager.h"
67#include "utils.h"
68
69namespace octave
70{
71 // Programming Note: There are two very different error functions used
72 // in the stream code. When invoked with "error (...)" the member
73 // function from stream or base_stream is called. This
74 // function sets the error state on the stream AND returns control to
75 // the caller. The caller must then return a value at the end of the
76 // function. When invoked with "::error (...)" the exception-based
77 // error function from error.h is used. This function will throw an
78 // exception and not return control to the caller. BE CAREFUL and
79 // invoke the correct error function!
80
81 // Possible values for conv_err:
82 //
83 // 1 : not a real scalar
84 // 2 : value is NaN
85 // 3 : value is not an integer
86
87 static int
88 convert_to_valid_int (const octave_value& tc, int& conv_err)
89 {
90 conv_err = 0;
91
92 int retval = 0;
93
94 double dval = 0.0;
95
96 try
97 {
98 dval = tc.double_value ();
99 }
100 catch (const execution_exception&)
101 {
102 interpreter& interp = __get_interpreter__ ("convert_to_valid_int");
103
104 interp.recover_from_exception ();
105
106 conv_err = 1;
107 }
108
109 if (! conv_err)
110 {
111 if (! lo_ieee_isnan (dval))
112 {
113 int ival = math::nint (dval);
114
115 if (ival == dval)
116 retval = ival;
117 else
118 conv_err = 3;
119 }
120 else
121 conv_err = 2;
122 }
123
124 return retval;
125 }
126
127 static octave_idx_type
128 get_size (double d, const std::string& who)
129 {
130 octave_idx_type retval = -1;
131
132 if (lo_ieee_isnan (d))
133 ::error ("%s: NaN invalid as size specification", who.c_str ());
134
135 if (math::isinf (d))
136 retval = -1;
137 else
138 {
139 if (d < 0.0)
140 ::error ("%s: negative value invalid as size specification",
141 who.c_str ());
142
143 static const double out_of_range_top
144 = static_cast<double> (std::numeric_limits<octave_idx_type>::max ())
145 + 1.;
146 if (d >= out_of_range_top)
147 ::error ("%s: dimension too large for Octave's index type",
148 who.c_str ());
149
150 retval = math::nint_big (d);
151 }
152
153 return retval;
154 }
155
156 static void
159 bool& one_elt_size_spec, const std::string& who)
160 {
161 nr = -1;
162 nc = -1;
163
164 one_elt_size_spec = false;
165
166 double dnr = -1.0;
167 double dnc = -1.0;
168
169 octave_idx_type sz_len = size.numel ();
170
171 if (sz_len == 1)
172 {
173 one_elt_size_spec = true;
174
175 dnr = size(0);
176
177 dnc = (dnr == 0.0) ? 0.0 : 1.0;
178 }
179 else if (sz_len == 2)
180 {
181 dnr = size(0);
182
183 if (math::isinf (dnr))
184 ::error ("%s: infinite value invalid as size specification",
185 who.c_str ());
186
187 dnc = size(1);
188 }
189 else
190 ::error ("%s: invalid size specification (must be 2-D)", who.c_str ());
191
192 nr = get_size (dnr, who);
193
194 if (dnc >= 0.0)
195 {
196 nc = get_size (dnc, who);
197
198 // Check for overflow.
199 if (nr > 0 && nc > 0
201 ::error ("%s: size too large for Octave's index type", who.c_str ());
202 }
203 }
204
205 static std::string
206 expand_char_class (const std::string& s)
207 {
208 std::string retval;
209
210 std::size_t len = s.length ();
211
212 std::size_t i = 0;
213
214 while (i < len)
215 {
216 unsigned char c = s[i++];
217
218 if (c == '-' && i > 1 && i < len
219 && ( static_cast<unsigned char> (s[i-2])
220 <= static_cast<unsigned char> (s[i])))
221 {
222 // Add all characters from the range except the first (we
223 // already added it below).
224
225 for (c = s[i-2]+1; c < s[i]; c++)
226 retval += c;
227 }
228 else
229 {
230 // Add the character to the class. Only add '-' if it is
231 // the last character in the class.
232
233 if (c != '-' || i == len)
234 retval += c;
235 }
236 }
237
238 return retval;
239 }
240
241 class
243 {
244 public:
245
247 {
248 whitespace_conversion = 1,
249 literal_conversion = 2,
250 null = 3
251 };
252
253 scanf_format_elt (const std::string& txt = "", int w = 0, bool d = false,
254 char typ = '\0', char mod = '\0',
255 const std::string& ch_class = "")
256 : text (txt), width (w), discard (d), type (typ),
257 modifier (mod), char_class (ch_class)
258 { }
259
261
262 scanf_format_elt& operator = (const scanf_format_elt&) = default;
263
264 ~scanf_format_elt (void) = default;
265
266 // The C-style format string.
267 std::string text;
268
269 // The maximum field width.
270 int width;
271
272 // TRUE if we are not storing the result of this conversion.
274
275 // Type of conversion -- 'd', 'i', 'o', 'u', 'x', 'e', 'f', 'g',
276 // 'c', 's', 'p', '%', or '['.
277 char type;
278
279 // A length modifier -- 'h', 'l', or 'L'.
281
282 // The class of characters in a '[' format.
283 std::string char_class;
284 };
285
286 class
288 {
289 public:
290
291 scanf_format_list (const std::string& fmt = "");
292
293 // No copying!
294
296
297 scanf_format_list& operator = (const scanf_format_list&) = delete;
298
299 ~scanf_format_list (void);
300
301 octave_idx_type num_conversions (void) { return m_nconv; }
302
303 // The length can be different than the number of conversions.
304 // For example, "x %d y %d z" has 2 conversions but the length of
305 // the list is 3 because of the characters that appear after the
306 // last conversion.
307
308 std::size_t length (void) const { return m_fmt_elts.size (); }
309
310 const scanf_format_elt * first (void)
311 {
312 m_curr_idx = 0;
313 return current ();
314 }
315
316 const scanf_format_elt * current (void) const
317 {
318 return length () > 0 ? m_fmt_elts[m_curr_idx] : nullptr;
319 }
320
321 const scanf_format_elt * next (bool cycle = true)
322 {
323 static scanf_format_elt dummy
324 ("", 0, false, scanf_format_elt::null, '\0', "");
325
326 m_curr_idx++;
327
328 if (m_curr_idx >= length ())
329 {
330 if (cycle)
331 m_curr_idx = 0;
332 else
333 return &dummy;
334 }
335
336 return current ();
337 }
338
339 void printme (void) const;
340
341 bool ok (void) const { return (m_nconv >= 0); }
342
343 operator bool () const { return ok (); }
344
345 bool all_character_conversions (void);
346
347 bool all_numeric_conversions (void);
348
349 private:
350
351 void add_elt_to_list (int width, bool discard, char type, char modifier,
352 const std::string& char_class = "");
353
354 void process_conversion (const std::string& s, std::size_t& i, std::size_t n,
355 int& width, bool& discard, char& type,
356 char& modifier);
357
358 int finish_conversion (const std::string& s, std::size_t& i, std::size_t n,
359 int width, bool discard, char& type,
360 char modifier);
361
362 //--------
363
364 // Number of conversions specified by this format string, or -1 if
365 // invalid conversions have been found.
367
368 // Index to current element;
369 std::size_t m_curr_idx;
370
371 // List of format elements.
372 std::deque<scanf_format_elt *> m_fmt_elts;
373
374 // Temporary buffer.
375 std::ostringstream m_buf;
376
377 };
378
380 : m_nconv (0), m_curr_idx (0), m_fmt_elts (), m_buf ()
381 {
382 std::size_t n = s.length ();
383
384 std::size_t i = 0;
385
386 int width = 0;
387 bool discard = false;
388 char modifier = '\0';
389 char type = '\0';
390
391 bool have_more = true;
392
393 while (i < n)
394 {
395 have_more = true;
396
397 if (s[i] == '%')
398 {
399 // Process percent-escape conversion type.
400
401 process_conversion (s, i, n, width, discard, type, modifier);
402
403 have_more = (m_buf.tellp () != 0);
404 }
405 else if (isspace (s[i]))
406 {
408
409 width = 0;
410 discard = false;
411 modifier = '\0';
412 m_buf << ' ';
413
414 while (++i < n && isspace (s[i]))
415 ; // skip whitespace
416
417 add_elt_to_list (width, discard, type, modifier);
418
419 have_more = false;
420 }
421 else
422 {
424
425 width = 0;
426 discard = false;
427 modifier = '\0';
428
429 while (i < n && ! isspace (s[i]) && s[i] != '%')
430 m_buf << s[i++];
431
432 add_elt_to_list (width, discard, type, modifier);
433
434 have_more = false;
435 }
436
437 if (m_nconv < 0)
438 {
439 have_more = false;
440 break;
441 }
442 }
443
444 if (have_more)
445 add_elt_to_list (width, discard, type, modifier);
446
447 m_buf.clear ();
448 m_buf.str ("");
449 }
450
452 {
453 std::size_t n = m_fmt_elts.size ();
454
455 for (std::size_t i = 0; i < n; i++)
456 {
458 delete elt;
459 }
460 }
461
462 void
463 scanf_format_list::add_elt_to_list (int width, bool discard, char type,
464 char modifier,
465 const std::string& char_class)
466 {
467 std::string text = m_buf.str ();
468
469 if (! text.empty ())
470 {
472 = new scanf_format_elt (text, width, discard, type,
473 modifier, char_class);
474
475 m_fmt_elts.push_back (elt);
476 }
477
478 m_buf.clear ();
479 m_buf.str ("");
480 }
481
482 void
483 scanf_format_list::process_conversion (const std::string& s, std::size_t& i,
484 std::size_t n, int& width,
485 bool& discard, char& type,
486 char& modifier)
487 {
488 width = 0;
489 discard = false;
490 modifier = '\0';
491 type = '\0';
492
493 m_buf << s[i++];
494
495 bool have_width = false;
496
497 while (i < n)
498 {
499 switch (s[i])
500 {
501 case '*':
502 if (discard)
503 m_nconv = -1;
504 else
505 {
506 discard = true;
507 m_buf << s[i++];
508 }
509 break;
510
511 case '0': case '1': case '2': case '3': case '4':
512 case '5': case '6': case '7': case '8': case '9':
513 if (have_width)
514 m_nconv = -1;
515 else
516 {
517 char c = s[i++];
518 width = 10 * width + c - '0';
519 have_width = true;
520 m_buf << c;
521 while (i < n && isdigit (s[i]))
522 {
523 c = s[i++];
524 width = 10 * width + c - '0';
525 m_buf << c;
526 }
527 }
528 break;
529
530 case 'h': case 'l': case 'L':
531 if (modifier != '\0')
532 m_nconv = -1;
533 else
534 modifier = s[i++];
535 break;
536
537 // We accept X for compatibility with undocumented Matlab behavior.
538 case 'd': case 'i': case 'o': case 'u': case 'x':
539 case 'X':
540 if (modifier == 'L')
541 {
542 m_nconv = -1;
543 break;
544 }
545 goto fini;
546
547 // We accept E and G for compatibility with undocumented
548 // Matlab behavior.
549 case 'e': case 'f': case 'g':
550 case 'E': case 'G':
551 if (modifier == 'h')
552 {
553 m_nconv = -1;
554 break;
555 }
556
557 // No float or long double conversions, thanks.
558 m_buf << 'l';
559
560 goto fini;
561
562 case 'c': case 's': case 'p': case '%': case '[':
563 if (modifier != '\0')
564 {
565 m_nconv = -1;
566 break;
567 }
568 goto fini;
569
570 fini:
571 {
572 if (finish_conversion (s, i, n, width, discard,
573 type, modifier) == 0)
574 return;
575 }
576 break;
577
578 default:
579 m_nconv = -1;
580 break;
581 }
582
583 if (m_nconv < 0)
584 break;
585 }
586
587 m_nconv = -1;
588 }
589
590 int
591 scanf_format_list::finish_conversion (const std::string& s, std::size_t& i,
592 std::size_t n, int width, bool discard,
593 char& type, char modifier)
594 {
595 int retval = 0;
596
597 std::string char_class;
598
599 std::size_t beg_idx = std::string::npos;
600 std::size_t end_idx = std::string::npos;
601
602 if (s[i] == '%')
603 {
604 type = '%';
605 m_buf << s[i++];
606 }
607 else
608 {
609 type = s[i];
610
611 if (s[i] == '[')
612 {
613 m_buf << s[i++];
614
615 if (i < n)
616 {
617 beg_idx = i;
618
619 if (s[i] == '^')
620 {
621 type = '^';
622 m_buf << s[i++];
623
624 if (i < n)
625 {
626 beg_idx = i;
627
628 if (s[i] == ']')
629 m_buf << s[i++];
630 }
631 }
632 else if (s[i] == ']')
633 m_buf << s[i++];
634 }
635
636 while (i < n && s[i] != ']')
637 m_buf << s[i++];
638
639 if (i < n && s[i] == ']')
640 {
641 end_idx = i-1;
642 m_buf << s[i++];
643 }
644
645 if (s[i-1] != ']')
646 retval = m_nconv = -1;
647 }
648 else
649 m_buf << s[i++];
650
651 m_nconv++;
652 }
653
654 if (m_nconv >= 0)
655 {
656 if (beg_idx != std::string::npos && end_idx != std::string::npos)
657 char_class = expand_char_class (s.substr (beg_idx,
658 end_idx - beg_idx + 1));
659
660 add_elt_to_list (width, discard, type, modifier, char_class);
661 }
662
663 return retval;
664 }
665
666 void
668 {
669 std::size_t n = m_fmt_elts.size ();
670
671 for (std::size_t i = 0; i < n; i++)
672 {
674
675 std::cerr
676 << "width: " << elt->width << "\n"
677 << "discard: " << elt->discard << "\n"
678 << "type: ";
679
681 std::cerr << "literal text\n";
683 std::cerr << "whitespace\n";
684 else
685 std::cerr << elt->type << "\n";
686
687 std::cerr
688 << "modifier: " << elt->modifier << "\n"
689 << "char_class: '" << undo_string_escapes (elt->char_class) << "'\n"
690 << "text: '" << undo_string_escapes (elt->text) << "'\n\n";
691 }
692 }
693
694 bool
696 {
697 std::size_t n = m_fmt_elts.size ();
698
699 if (n > 0)
700 {
701 for (std::size_t i = 0; i < n; i++)
702 {
704
705 switch (elt->type)
706 {
707 case 'c': case 's': case '%': case '[': case '^':
710 break;
711
712 default:
713 return false;
714 break;
715 }
716 }
717
718 return true;
719 }
720 else
721 return false;
722 }
723
724 bool
726 {
727 std::size_t n = m_fmt_elts.size ();
728
729 if (n > 0)
730 {
731 for (std::size_t i = 0; i < n; i++)
732 {
734
735 switch (elt->type)
736 {
737 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
738 case 'e': case 'f': case 'g': case 'E': case 'G':
739 break;
740
741 default:
742 return false;
743 break;
744 }
745 }
746
747 return true;
748 }
749 else
750 return false;
751 }
752
753 class
755 {
756 public:
757
758 printf_format_elt (const std::string& txt = "", int n = 0, int w = -1,
759 int p = -1, const std::string& f = "",
760 char typ = '\0', char mod = '\0')
761 : text (txt), args (n), fw (w), prec (p), flags (f),
762 type (typ), modifier (mod)
763 { }
764
766
767 printf_format_elt& operator = (const printf_format_elt&) = default;
768
769 ~printf_format_elt (void) = default;
770
771 // The C-style format string.
772 std::string text;
773
774 // How many args do we expect to consume?
775 int args;
776
777 // Field width.
778 int fw;
779
780 // Precision.
781 int prec;
782
783 // Flags -- '-', '+', ' ', '0', or '#'.
784 std::string flags;
785
786 // Type of conversion -- 'd', 'i', 'o', 'x', 'X', 'u', 'c', 's',
787 // 'f', 'e', 'E', 'g', 'G', 'p', or '%'
788 char type;
789
790 // A length modifier -- 'h', 'l', or 'L'.
792 };
793
794 class
796 {
797 public:
798
799 printf_format_list (const std::string& fmt = "");
800
801 // No copying!
802
804
805 printf_format_list& operator = (const printf_format_list&) = delete;
806
807 ~printf_format_list (void);
808
809 octave_idx_type num_conversions (void) { return m_nconv; }
810
812 {
813 m_curr_idx = 0;
814 return current ();
815 }
816
817 const printf_format_elt * current (void) const
818 {
819 return length () > 0 ? m_fmt_elts[m_curr_idx] : nullptr;
820 }
821
822 std::size_t length (void) const { return m_fmt_elts.size (); }
823
824 const printf_format_elt * next (bool cycle = true)
825 {
826 m_curr_idx++;
827
828 if (m_curr_idx >= length ())
829 {
830 if (cycle)
831 m_curr_idx = 0;
832 else
833 return nullptr;
834 }
835
836 return current ();
837 }
838
839 bool last_elt_p (void) { return (m_curr_idx + 1 == length ()); }
840
841 void printme (void) const;
842
843 bool ok (void) const { return (m_nconv >= 0); }
844
845 operator bool () const { return ok (); }
846
847 private:
848
849 void add_elt_to_list (int args, const std::string& flags, int fw,
850 int prec, char type, char modifier);
851
852 void process_conversion (const std::string& s, std::size_t& i,
853 std::size_t n,
854 int& args, std::string& flags, int& fw,
855 int& prec, char& modifier, char& type);
856
857 void finish_conversion (const std::string& s, std::size_t& i, int args,
858 const std::string& flags, int fw, int prec,
859 char modifier, char& type);
860
861 //--------
862
863 // Number of conversions specified by this format string, or -1 if
864 // invalid conversions have been found.
866
867 // Index to current element;
868 std::size_t m_curr_idx;
869
870 // List of format elements.
871 std::deque<printf_format_elt *> m_fmt_elts;
872
873 // Temporary buffer.
874 std::ostringstream m_buf;
875
876 };
877
879 : m_nconv (0), m_curr_idx (0), m_fmt_elts (), m_buf ()
880 {
881 std::size_t n = s.length ();
882
883 std::size_t i = 0;
884
885 int args = 0;
886 std::string flags;
887 int fw = -1;
888 int prec = -1;
889 char modifier = '\0';
890 char type = '\0';
891
892 bool have_more = true;
893 bool empty_buf = true;
894
895 if (n == 0)
896 {
898 = new printf_format_elt ("", args, fw, prec, flags, type, modifier);
899
900 m_fmt_elts.push_back (elt);
901 }
902 else
903 {
904 while (i < n)
905 {
906 have_more = true;
907
908 empty_buf = (m_buf.tellp () == 0);
909
910 switch (s[i])
911 {
912 case '%':
913 {
914 if (empty_buf)
915 {
916 process_conversion (s, i, n, args, flags, fw, prec,
917 modifier, type);
918
919 // If there is nothing in the buffer, then
920 // add_elt_to_list must have just been called, so we
921 // are already done with the current element and we
922 // don't need to call add_elt_to_list if this is our
923 // last trip through the loop.
924
925 have_more = (m_buf.tellp () != 0);
926 }
927 else
928 add_elt_to_list (args, flags, fw, prec, type, modifier);
929 }
930 break;
931
932 default:
933 {
934 args = 0;
935 flags = "";
936 fw = -1;
937 prec = -1;
938 modifier = '\0';
939 type = '\0';
940 m_buf << s[i++];
941 }
942 break;
943 }
944
945 if (m_nconv < 0)
946 {
947 have_more = false;
948 break;
949 }
950 }
951
952 if (have_more)
953 add_elt_to_list (args, flags, fw, prec, type, modifier);
954
955 m_buf.clear ();
956 m_buf.str ("");
957 }
958 }
959
961 {
962 std::size_t n = m_fmt_elts.size ();
963
964 for (std::size_t i = 0; i < n; i++)
965 {
967 delete elt;
968 }
969 }
970
971 void
972 printf_format_list::add_elt_to_list (int args, const std::string& flags,
973 int fw, int prec, char type,
974 char modifier)
975 {
976 std::string text = m_buf.str ();
977
978 if (! text.empty ())
979 {
981 = new printf_format_elt (text, args, fw, prec, flags,
982 type, modifier);
983
984 m_fmt_elts.push_back (elt);
985 }
986
987 m_buf.clear ();
988 m_buf.str ("");
989 }
990
991 void
992 printf_format_list::process_conversion (const std::string& s, std::size_t& i,
993 std::size_t n, int& args,
994 std::string& flags, int& fw,
995 int& prec, char& modifier,
996 char& type)
997 {
998 args = 0;
999 flags = "";
1000 fw = -1;
1001 prec = -1;
1002 modifier = '\0';
1003 type = '\0';
1004
1005 m_buf << s[i++];
1006
1007 bool nxt = false;
1008
1009 while (i < n)
1010 {
1011 switch (s[i])
1012 {
1013 case '-': case '+': case ' ': case '0': case '#':
1014 flags += s[i];
1015 m_buf << s[i++];
1016 break;
1017
1018 default:
1019 nxt = true;
1020 break;
1021 }
1022
1023 if (nxt)
1024 break;
1025 }
1026
1027 if (i < n)
1028 {
1029 if (s[i] == '*')
1030 {
1031 fw = -2;
1032 args++;
1033 m_buf << s[i++];
1034 }
1035 else
1036 {
1037 if (isdigit (s[i]))
1038 {
1039 int nn = 0;
1040 std::string tmp = s.substr (i);
1041 sscanf (tmp.c_str (), "%d%n", &fw, &nn);
1042 }
1043
1044 while (i < n && isdigit (s[i]))
1045 m_buf << s[i++];
1046 }
1047 }
1048
1049 if (i < n && s[i] == '.')
1050 {
1051 // nothing before the . means 0.
1052 if (fw == -1)
1053 fw = 0;
1054
1055 // . followed by nothing is 0.
1056 prec = 0;
1057
1058 m_buf << s[i++];
1059
1060 if (i < n)
1061 {
1062 if (s[i] == '*')
1063 {
1064 prec = -2;
1065 args++;
1066 m_buf << s[i++];
1067 }
1068 else
1069 {
1070 if (isdigit (s[i]))
1071 {
1072 int nn = 0;
1073 std::string tmp = s.substr (i);
1074 sscanf (tmp.c_str (), "%d%n", &prec, &nn);
1075 }
1076
1077 while (i < n && isdigit (s[i]))
1078 m_buf << s[i++];
1079 }
1080 }
1081 }
1082
1083 if (i < n)
1084 {
1085 // Accept and record modifier, but don't place it in the format
1086 // item text. All integer conversions are handled as 64-bit
1087 // integers.
1088
1089 switch (s[i])
1090 {
1091 case 'h': case 'l': case 'L':
1092 modifier = s[i++];
1093 break;
1094
1095 default:
1096 break;
1097 }
1098 }
1099
1100 if (i < n)
1101 finish_conversion (s, i, args, flags, fw, prec, modifier, type);
1102 else
1103 m_nconv = -1;
1104 }
1105
1106 void
1107 printf_format_list::finish_conversion (const std::string& s, std::size_t& i,
1108 int args, const std::string& flags,
1109 int fw, int prec, char modifier,
1110 char& type)
1111 {
1112 switch (s[i])
1113 {
1114 case 'd': case 'i': case 'o': case 'x': case 'X':
1115 case 'u': case 'c':
1116 if (modifier == 'L')
1117 {
1118 m_nconv = -1;
1119 break;
1120 }
1121 goto fini;
1122
1123 case 'f': case 'e': case 'E': case 'g': case 'G':
1124 if (modifier == 'h' || modifier == 'l')
1125 {
1126 m_nconv = -1;
1127 break;
1128 }
1129 goto fini;
1130
1131 case 's': case 'p': case '%':
1132 if (modifier != '\0')
1133 {
1134 m_nconv = -1;
1135 break;
1136 }
1137 goto fini;
1138
1139 fini:
1140
1141 type = s[i];
1142
1143 m_buf << s[i++];
1144
1145 if (type != '%' || args != 0)
1146 m_nconv++;
1147
1148 if (type != '%')
1149 args++;
1150
1151 add_elt_to_list (args, flags, fw, prec, type, modifier);
1152
1153 break;
1154
1155 default:
1156 m_nconv = -1;
1157 break;
1158 }
1159 }
1160
1161 void
1163 {
1164 std::size_t n = m_fmt_elts.size ();
1165
1166 for (std::size_t i = 0; i < n; i++)
1167 {
1168 printf_format_elt *elt = m_fmt_elts[i];
1169
1170 std::cerr
1171 << "args: " << elt->args << "\n"
1172 << "flags: '" << elt->flags << "'\n"
1173 << "width: " << elt->fw << "\n"
1174 << "prec: " << elt->prec << "\n"
1175 << "type: '" << elt->type << "'\n"
1176 << "modifier: '" << elt->modifier << "'\n"
1177 << "text: '" << undo_string_escapes (elt->text) << "'\n\n";
1178 }
1179 }
1180
1181 // Calculate x^n. Used for ...e+nn so that, for example, 1e2 is
1182 // exactly 100 and 5e-1 is 1/2
1183
1184 static double
1185 pown (double x, unsigned int n)
1186 {
1187 double retval = 1;
1188
1189 for (unsigned int d = n; d; d >>= 1)
1190 {
1191 if (d & 1)
1192 retval *= x;
1193 x *= x;
1194 }
1195
1196 return retval;
1197 }
1198
1199 static Cell
1201 {
1202 Cell retval (dim_vector (1, 2));
1203
1204 retval(0) = Cell (octave_value ("inf"));
1205 retval(1) = Cell (octave_value ("nan"));
1206
1207 return retval;
1208 }
1209
1210 // Delimited stream, optimized to read strings of characters separated
1211 // by single-character delimiters.
1212 //
1213 // The reason behind this class is that octstream doesn't provide
1214 // seek/tell, but the opportunity has been taken to optimise for the
1215 // textscan workload.
1216 //
1217 // The function reads chunks into a 4kiB buffer, and marks where the
1218 // last delimiter occurs. Reads up to this delimiter can be fast.
1219 // After that last delimiter, the remaining text is moved to the front
1220 // of the buffer and the buffer is refilled. This also allows cheap
1221 // seek and tell operations within a "fast read" block.
1222
1223 class
1225 {
1226 public:
1227
1228 delimited_stream (std::istream& is, const std::string& delimiters,
1229 int longest_lookahead, octave_idx_type bsize = 4096);
1230
1231 delimited_stream (std::istream& is, const delimited_stream& ds);
1232
1233 // No copying!
1234
1236
1237 delimited_stream& operator = (const delimited_stream&) = delete;
1238
1239 ~delimited_stream (void);
1240
1241 // Called when optimized sequence of get is finished. Ensures that
1242 // there is a remaining delimiter in buf, or loads more data in.
1243 void field_done (void)
1244 {
1245 if (m_idx >= m_last)
1246 refresh_buf ();
1247 }
1248
1249 // Load new data into buffer, and set eob, last, idx.
1250 // Return EOF at end of file, 0 otherwise.
1251 int refresh_buf (void);
1252
1253 // Get a character, relying on caller to call field_done if
1254 // a delimiter has been reached.
1255 int get (void)
1256 {
1257 if (m_delimited)
1258 return eof () ? std::istream::traits_type::eof () : *m_idx++;
1259 else
1260 return get_undelim ();
1261 }
1262
1263 // Get a character, checking for underrun of the buffer.
1264 int get_undelim (void);
1265
1266 // Read character that will be got by the next get.
1267 // FIXME: This will not set EOF if delimited stream is at EOF and a peek
1268 // is attempted. This does *NOT* behave like C++ input stream.
1269 // For a compatible peek function, use peek_undelim. See bug #56917.
1270 int peek (void)
1271 { return eof () ? std::istream::traits_type::eof () : *m_idx; }
1272
1273 // Read character that will be got by the next get.
1274 int peek_undelim (void);
1275
1276 // Undo a 'get' or 'get_undelim'. It is the caller's responsibility
1277 // to avoid overflow by calling putbacks only for a character got by
1278 // get() or get_undelim(), with no intervening
1279 // get, get_delim, field_done, refresh_buf, getline, read or seekg.
1280 void putback (char /*ch*/ = 0) { if (! eof ()) --m_idx; }
1281
1282 int getline (std::string& dest, char delim);
1283
1284 // int skipline (char delim);
1285
1286 char * read (char *buffer, int size, char* &new_start);
1287
1288 // Return a position suitable to "seekg", valid only within this
1289 // block between calls to field_done.
1290 char * tellg (void) { return m_idx; }
1291
1292 void seekg (char *old_idx) { m_idx = old_idx; }
1293
1294 bool eof (void)
1295 {
1296 return (m_eob == m_buf && m_i_stream.eof ())
1297 || (m_flags & std::ios_base::eofbit);
1298 }
1299
1300 operator const void* (void)
1301 { return (! eof () && ! m_flags) ? this : nullptr; }
1302
1303 bool fail (void) { return m_flags & std::ios_base::failbit; }
1304
1305 std::ios_base::iostate rdstate (void) { return m_flags; }
1306
1307 void setstate (std::ios_base::iostate m) { m_flags = m_flags | m; }
1308
1309 void clear (std::ios_base::iostate m
1310 = (std::ios_base::eofbit & ~std::ios_base::eofbit))
1311 {
1312 m_flags = m_flags & m;
1313 }
1314
1315 // Report if any characters have been consumed.
1316 // (get, read, etc. not cancelled by putback or seekg)
1317
1318 void progress_benchmark (void) { m_progress_marker = m_idx; }
1319
1320 bool no_progress (void) { return m_progress_marker == m_idx; }
1321
1322 private:
1323
1324 // Number of characters to read from the file at once.
1326
1327 // Stream to read from.
1328 std::istream& m_i_stream;
1329
1330 // Temporary storage for a "chunk" of data.
1331 char *m_buf;
1332
1333 // Current read pointer.
1334 char *m_idx;
1335
1336 // Location of last delimiter in the buffer at buf (undefined if
1337 // delimited is false).
1338 char *m_last;
1339
1340 // Position after last character in buffer.
1341 char *m_eob;
1342
1343 // True if there is delimiter in the buffer after idx.
1345
1346 // Longest lookahead required.
1348
1349 // Sequence of single-character delimiters.
1350 const std::string m_delims;
1351
1352 // Position of start of buf in original stream.
1353 std::streampos m_buf_in_file;
1354
1355 // Marker to see if a read consumes any characters.
1357
1358 std::ios_base::iostate m_flags;
1359 };
1360
1361 // Create a delimited stream, reading from is, with delimiters delims,
1362 // and allowing reading of up to tellg + longest_lookeahead. When is
1363 // is at EOF, lookahead may be padded by ASCII nuls.
1364
1366 const std::string& delimiters,
1367 int longest_lookahead,
1368 octave_idx_type bsize)
1369 : m_bufsize (bsize), m_i_stream (is), m_longest (longest_lookahead),
1370 m_delims (delimiters),
1371 m_flags (std::ios::failbit & ~std::ios::failbit) // can't cast 0
1372 {
1373 m_buf = new char[m_bufsize];
1374 m_eob = m_buf + m_bufsize;
1375 m_idx = m_eob; // refresh_buf shouldn't try to copy old data
1377 refresh_buf (); // load the first batch of data
1378 }
1379
1380 // Used to create a stream from a strstream from data read from a dstr.
1382 const delimited_stream& ds)
1383 : delimited_stream (is, ds.m_delims, ds.m_longest, ds.m_bufsize)
1384 { }
1385
1387 {
1388 // Seek to the correct position in i_stream.
1389 if (! eof ())
1390 {
1391 m_i_stream.clear ();
1392 m_i_stream.seekg (m_buf_in_file);
1393 m_i_stream.read (m_buf, m_idx - m_buf);
1394 }
1395
1396 delete [] m_buf;
1397 }
1398
1399 // Read a character from the buffer, refilling the buffer from the file
1400 // if necessary.
1401
1402 int
1404 {
1405 int retval;
1406 if (eof ())
1407 {
1408 setstate (std::ios_base::failbit);
1409 return std::istream::traits_type::eof ();
1410 }
1411
1412 if (m_idx < m_eob)
1413 retval = *m_idx++;
1414 else
1415 {
1416 refresh_buf ();
1417
1418 if (eof ())
1419 {
1420 setstate (std::ios_base::eofbit);
1421 retval = std::istream::traits_type::eof ();
1422 }
1423 else
1424 retval = *m_idx++;
1425 }
1426
1427 if (m_idx >= m_last)
1428 m_delimited = false;
1429
1430 return retval;
1431 }
1432
1433 // Return the next character to be read without incrementing the
1434 // pointer, refilling the buffer from the file if necessary.
1435
1436 int
1438 {
1439 int retval = get_undelim ();
1440 putback ();
1441
1442 return retval;
1443 }
1444
1445 // Copy remaining unprocessed data to the start of the buffer and load
1446 // new data to fill it. Return EOF if the file is at EOF before
1447 // reading any data and all of the data that has been read has been
1448 // processed.
1449
1450 int
1452 {
1453 if (eof ())
1454 return std::istream::traits_type::eof ();
1455
1456 int retval;
1457
1458 if (m_eob < m_idx)
1459 m_idx = m_eob;
1460
1461 std::size_t old_remaining = m_eob - m_idx;
1462
1463 octave_quit (); // allow ctrl-C
1464
1465 if (old_remaining > 0)
1466 {
1467 m_buf_in_file += (m_idx - m_buf);
1468 memmove (m_buf, m_idx, old_remaining);
1469 }
1470 else
1471 m_buf_in_file = m_i_stream.tellg (); // record for destructor
1472
1473 m_progress_marker -= m_idx - m_buf; // where original idx would have been
1474 m_idx = m_buf;
1475
1476 int gcount; // chars read
1477 if (! m_i_stream.eof ())
1478 {
1479 m_i_stream.read (m_buf + old_remaining, m_bufsize - old_remaining);
1480 gcount = m_i_stream.gcount ();
1481 }
1482 else
1483 gcount = 0;
1484
1485 m_eob = m_buf + old_remaining + gcount;
1486 m_last = m_eob;
1487 if (gcount == 0)
1488 {
1489 m_delimited = false;
1490
1491 if (m_eob != m_buf) // no more data in file, but still some to go
1492 retval = 0;
1493 else
1494 // file and buffer are both done.
1495 retval = std::istream::traits_type::eof ();
1496 }
1497 else
1498 {
1499 m_delimited = true;
1500
1501 for (m_last = m_eob - m_longest; m_last - m_buf >= 0; m_last--)
1502 {
1503 if (m_delims.find (*m_last) != std::string::npos)
1504 break;
1505 }
1506
1507 if (m_last < m_buf)
1508 m_delimited = false;
1509
1510 retval = 0;
1511 }
1512
1513 // Ensure fast peek doesn't give valid char
1514 if (retval == std::istream::traits_type::eof ())
1515 *m_idx = '\0'; // FIXME: check that no TreatAsEmpty etc starts w. \0?
1516
1517 return retval;
1518 }
1519
1520 // Return a pointer to a block of data of size size, assuming that a
1521 // sufficiently large buffer is available in buffer, if required.
1522 // If called when delimited == true, and size is no greater than
1523 // longest_lookahead then this will not call refresh_buf, so seekg
1524 // still works. Otherwise, seekg may be invalidated.
1525
1526 char *
1527 delimited_stream::read (char *buffer, int size, char* &prior_tell)
1528 {
1529 char *retval;
1530
1531 if (m_eob - m_idx > size)
1532 {
1533 retval = m_idx;
1534 m_idx += size;
1535 if (m_idx > m_last)
1536 m_delimited = false;
1537 }
1538 else
1539 {
1540 // If there was a tellg pointing to an earlier point than the current
1541 // read position, try to keep it in the active buffer.
1542 // In the current code, prior_tell==idx for each call,
1543 // so this is not necessary, just a precaution.
1544
1545 if (m_eob - prior_tell + size < m_bufsize)
1546 {
1547 octave_idx_type gap = m_idx - prior_tell;
1548 m_idx = prior_tell;
1549 refresh_buf ();
1550 m_idx += gap;
1551 }
1552 else // can't keep the tellg in range. May skip some data.
1553 {
1554 refresh_buf ();
1555 }
1556
1557 prior_tell = m_buf;
1558
1559 if (m_eob - m_idx > size)
1560 {
1561 retval = m_idx;
1562 m_idx += size;
1563 if (m_idx > m_last)
1564 m_delimited = false;
1565 }
1566 else
1567 {
1568 if (size <= m_bufsize) // small read, but reached EOF
1569 {
1570 retval = m_idx;
1571 memset (m_eob, 0, size + (m_idx - m_buf));
1572 m_idx += size;
1573 }
1574 else // Reading more than the whole buf; return it in buffer
1575 {
1576 retval = buffer;
1577 // FIXME: read bufsize at a time
1578 int i;
1579 for (i = 0; i < size && ! eof (); i++)
1580 *buffer++ = get_undelim ();
1581 if (eof ())
1582 memset (buffer, 0, size - i);
1583 }
1584 }
1585 }
1586
1587 return retval;
1588 }
1589
1590 // Return in OUT an entire line, terminated by delim. On input, OUT
1591 // must have length at least 1.
1592
1593 int
1594 delimited_stream::getline (std::string& out, char delim)
1595 {
1596 int len = out.length ();
1597 int used = 0;
1598 int ch;
1599 while ((ch = get_undelim ()) != delim
1600 && ch != std::istream::traits_type::eof ())
1601 {
1602 out[used++] = ch;
1603 if (used == len)
1604 {
1605 len <<= 1;
1606 out.resize (len);
1607 }
1608 }
1609 out.resize (used);
1610 field_done ();
1611
1612 return ch;
1613 }
1614
1615 // A single conversion specifier, such as %f or %c.
1616
1617 class
1619 {
1620 public:
1621
1623 {
1624 whitespace_conversion = 1,
1625 literal_conversion = 2
1627
1628 textscan_format_elt (const std::string& txt, int w = 0, int p = -1,
1629 int bw = 0, bool dis = false, char typ = '\0',
1630 const std::string& ch_class = std::string ())
1631 : text (txt), width (w), prec (p), bitwidth (bw),
1632 char_class (ch_class), type (typ), discard (dis),
1633 numeric (typ == 'd' || typ == 'u' || type == 'f' || type == 'n')
1634 { }
1635
1637 : text (e.text), width (e.width), prec (e.prec),
1638 bitwidth (e.bitwidth), char_class (e.char_class), type (e.type),
1639 discard (e.discard), numeric (e.numeric)
1640 { }
1641
1643 {
1644 if (this != &e)
1645 {
1646 text = e.text;
1647 width = e.width;
1648 prec = e.prec;
1649 bitwidth = e.bitwidth;
1650 discard = e.discard;
1651 type = e.type;
1652 numeric = e.numeric;
1653 char_class = e.char_class;
1654 }
1655
1656 return *this;
1657 }
1658
1659 // The C-style format string.
1660 std::string text;
1661
1662 // The maximum field width.
1663 unsigned int width;
1664
1665 // The maximum number of digits to read after the decimal in a
1666 // floating point conversion.
1667 int prec;
1668
1669 // The size of the result. For integers, bitwidth may be 8, 16, 34,
1670 // or 64. For floating point values, bitwidth may be 32 or 64.
1672
1673 // The class of characters in a '[' or '^' format.
1674 std::string char_class;
1675
1676 // Type of conversion
1677 // -- 'd', 'u', 'f', 'n', 's', 'q', 'c', '%', 'C', 'D', '[' or '^'.
1678 char type;
1679
1680 // TRUE if we are not storing the result of this conversion.
1682
1683 // TRUE if the type is 'd', 'u', 'f', 'n'
1685 };
1686
1687 // The (parsed) sequence of format specifiers.
1688
1689 class textscan;
1690
1691 class
1693 {
1694 public:
1695
1696 textscan_format_list (const std::string& fmt = std::string (),
1697 const std::string& who = "textscan");
1698 // No copying!
1699
1701
1702 textscan_format_list& operator = (const textscan_format_list&) = delete;
1703
1704 ~textscan_format_list (void);
1705
1706 octave_idx_type num_conversions (void) const { return m_nconv; }
1707
1708 // The length can be different than the number of conversions.
1709 // For example, "x %d y %d z" has 2 conversions but the length of
1710 // the list is 3 because of the characters that appear after the
1711 // last conversion.
1712
1713 std::size_t numel (void) const { return m_fmt_elts.size (); }
1714
1716 {
1717 m_curr_idx = 0;
1718 return current ();
1719 }
1720
1721 const textscan_format_elt * current (void) const
1722 {
1723 return numel () > 0 ? m_fmt_elts[m_curr_idx] : nullptr;
1724 }
1725
1726 const textscan_format_elt * next (bool cycle = true)
1727 {
1728 m_curr_idx++;
1729
1730 if (m_curr_idx >= numel ())
1731 {
1732 if (cycle)
1733 m_curr_idx = 0;
1734 else
1735 return nullptr;
1736 }
1737
1738 return current ();
1739 }
1740
1741 void printme (void) const;
1742
1743 bool ok (void) const { return (m_nconv >= 0); }
1744
1745 operator const void* (void) const { return ok () ? this : nullptr; }
1746
1747 // What function name should be shown when reporting errors.
1748 std::string who;
1749
1750 // True if number of %f to be set from data file.
1752
1753 // At least one conversion specifier is s,q,c, or [...].
1755
1756 int read_first_row (delimited_stream& is, textscan& ts);
1757
1758 std::list<octave_value> out_buf (void) const { return (m_output_container); }
1759
1760 private:
1761
1762 void add_elt_to_list (unsigned int width, int prec, int bitwidth,
1763 octave_value val_type, bool discard,
1764 char type,
1765 const std::string& char_class = std::string ());
1766
1767 void process_conversion (const std::string& s, std::size_t& i,
1768 std::size_t n);
1769
1770 std::string parse_char_class (const std::string& pattern) const;
1771
1772 int finish_conversion (const std::string& s, std::size_t& i, std::size_t n,
1773 unsigned int width, int prec, int bitwidth,
1774 octave_value& val_type,
1775 bool discard, char& type);
1776
1777 //--------
1778
1779 // Number of conversions specified by this format string, or -1 if
1780 // invalid conversions have been found.
1782
1783 // Index to current element;
1784 std::size_t m_curr_idx;
1785
1786 // List of format elements.
1787 std::deque<textscan_format_elt *> m_fmt_elts;
1788
1789 // list holding column arrays of types specified by conversions
1790 std::list<octave_value> m_output_container;
1791
1792 // Temporary buffer.
1793 std::ostringstream m_buf;
1794
1795 };
1796
1797 // Main class to implement textscan. Read data and parse it
1798 // according to a format.
1799 //
1800 // The calling sequence is
1801 //
1802 // textscan scanner ();
1803 // scanner.scan (...);
1804
1805 class
1806 OCTINTERP_API
1807 textscan
1808 {
1809 public:
1810
1811 textscan (const std::string& who_arg = "textscan",
1812 const std::string& encoding = "utf-8");
1813
1814 // No copying!
1815
1816 textscan (const textscan&) = delete;
1817
1818 textscan& operator = (const textscan&) = delete;
1819
1820 ~textscan (void) = default;
1821
1822 octave_value scan (std::istream& isp, const std::string& fmt,
1823 octave_idx_type ntimes,
1824 const octave_value_list& options,
1825 octave_idx_type& read_count);
1826
1827 private:
1828
1830
1831 octave_value do_scan (std::istream& isp, textscan_format_list& fmt_list,
1832 octave_idx_type ntimes);
1833
1834 void parse_options (const octave_value_list& args,
1835 textscan_format_list& fmt_list);
1836
1837 int read_format_once (delimited_stream& isp, textscan_format_list& fmt_list,
1838 std::list<octave_value>& retval,
1839 Array<octave_idx_type> row, int& done_after);
1840
1841 void scan_one (delimited_stream& is, const textscan_format_elt& fmt,
1843
1844 // Methods to process a particular conversion specifier.
1845 double read_double (delimited_stream& is,
1846 const textscan_format_elt& fmt) const;
1847
1848 void scan_complex (delimited_stream& is, const textscan_format_elt& fmt,
1849 Complex& val) const;
1850
1851 int scan_bracket (delimited_stream& is, const std::string& pattern,
1852 std::string& val) const;
1853
1854 int scan_caret (delimited_stream& is, const std::string& pattern,
1855 std::string& val) const;
1856
1857 void scan_string (delimited_stream& is, const textscan_format_elt& fmt,
1858 std::string& val) const;
1859
1860 void scan_cstring (delimited_stream& is, const textscan_format_elt& fmt,
1861 std::string& val) const;
1862
1863 void scan_qstring (delimited_stream& is, const textscan_format_elt& fmt,
1864 std::string& val);
1865
1866 // Helper methods.
1867 std::string read_until (delimited_stream& is, const Cell& delimiters,
1868 const std::string& ends) const;
1869
1870 int lookahead (delimited_stream& is, const Cell& targets, int max_len,
1871 bool case_sensitive = true) const;
1872
1873 bool match_literal (delimited_stream& isp, const textscan_format_elt& elem);
1874
1875 int skip_whitespace (delimited_stream& is, bool EOLstop = true);
1876
1877 int skip_delim (delimited_stream& is);
1878
1879 bool is_delim (unsigned char ch) const
1880 {
1881 return ((m_delim_table.empty ()
1882 && (isspace (ch) || ch == m_eol1 || ch == m_eol2))
1883 || m_delim_table[ch] != '\0');
1884 }
1885
1886 bool isspace (unsigned int ch) const
1887 { return m_whitespace_table[ch & 0xff]; }
1888
1889 // True if the only delimiter is whitespace.
1890 bool whitespace_delim (void) const { return m_delim_table.empty (); }
1891
1892 //--------
1893
1894 // What function name should be shown when reporting errors.
1895 std::string m_who;
1896
1897 std::string m_encoding;
1898
1899 std::string m_buf;
1900
1901 // Three cases for delim_table and delim_list
1902 // 1. delim_table empty, delim_list empty: whitespace delimiters
1903 // 2. delim_table = look-up table of delim chars, delim_list empty.
1904 // 3. delim_table non-empty, delim_list = Cell array of delim strings
1905
1907
1908 // delim_table[i] == '\0' if i is not a delimiter.
1909 std::string m_delim_table;
1910
1911 // String of delimiter characters.
1912 std::string m_delims;
1913
1915
1916 // How far ahead to look to detect an open comment.
1918
1919 // First character of open comment.
1921
1923
1924 std::string m_date_locale;
1925
1926 // 'inf' and 'nan' for formatted_double.
1928
1929 // Array of strings of delimiters.
1931
1932 // Longest delimiter.
1934
1936 std::string m_exp_chars;
1939
1940 // Longest string to treat as "N/A".
1942
1943 std::string m_whitespace;
1944
1945 short m_eol1;
1946 short m_eol2;
1948
1952
1954 };
1955
1957 const std::string& who_arg)
1958 : who (who_arg), set_from_first (false), has_string (false),
1959 m_nconv (0), m_curr_idx (0), m_fmt_elts (), m_buf ()
1960 {
1961 std::size_t n = s.length ();
1962
1963 std::size_t i = 0;
1964
1965 unsigned int width = -1; // Unspecified width = max (except %c)
1966 int prec = -1;
1967 int bitwidth = 0;
1968 bool discard = false;
1969 char type = '\0';
1970
1971 bool have_more = true;
1972
1973 if (s.empty ())
1974 {
1975 m_buf.clear ();
1976 m_buf.str ("");
1977
1978 m_buf << "%f";
1979
1980 bitwidth = 64;
1981 type = 'f';
1982 add_elt_to_list (width, prec, bitwidth, octave_value (NDArray ()),
1983 discard, type);
1984 have_more = false;
1985 set_from_first = true;
1986 m_nconv = 1;
1987 }
1988 else
1989 {
1990 set_from_first = false;
1991
1992 while (i < n)
1993 {
1994 have_more = true;
1995
1996 if (s[i] == '%' && (i+1 == n || s[i+1] != '%'))
1997 {
1998 // Process percent-escape conversion type.
1999
2000 process_conversion (s, i, n);
2001
2002 // If there is nothing in the buffer, then add_elt_to_list
2003 // must have just been called, so we are already done with
2004 // the current element and we don't need to call
2005 // add_elt_to_list if this is our last trip through the
2006 // loop.
2007
2008 have_more = (m_buf.tellp () != 0);
2009 }
2010 else if (isspace (s[i]))
2011 {
2012 while (++i < n && isspace (s[i]))
2013 /* skip whitespace */;
2014
2015 have_more = false;
2016 }
2017 else
2018 {
2020
2021 width = 0;
2022 prec = -1;
2023 bitwidth = 0;
2024 discard = true;
2025
2026 while (i < n && ! isspace (s[i])
2027 && (s[i] != '%' || (i+1 < n && s[i+1] == '%')))
2028 {
2029 if (s[i] == '%') // if double %, skip one
2030 i++;
2031 m_buf << s[i++];
2032 width++;
2033 }
2034
2035 add_elt_to_list (width, prec, bitwidth, octave_value (),
2036 discard, type);
2037
2038 have_more = false;
2039 }
2040
2041 if (m_nconv < 0)
2042 {
2043 have_more = false;
2044 break;
2045 }
2046 }
2047 }
2048
2049 if (have_more)
2050 add_elt_to_list (width, prec, bitwidth, octave_value (), discard, type);
2051
2052 m_buf.clear ();
2053 m_buf.str ("");
2054 }
2055
2057 {
2058 std::size_t n = numel ();
2059
2060 for (std::size_t i = 0; i < n; i++)
2061 {
2063 delete elt;
2064 }
2065 }
2066
2067 void
2068 textscan_format_list::add_elt_to_list (unsigned int width, int prec,
2069 int bitwidth, octave_value val_type,
2070 bool discard, char type,
2071 const std::string& char_class)
2072 {
2073 std::string text = m_buf.str ();
2074
2075 if (! text.empty ())
2076 {
2078 = new textscan_format_elt (text, width, prec, bitwidth, discard,
2079 type, char_class);
2080
2081 if (! discard)
2082 m_output_container.push_back (val_type);
2083
2084 m_fmt_elts.push_back (elt);
2085 }
2086
2087 m_buf.clear ();
2088 m_buf.str ("");
2089 }
2090
2091 void
2093 std::size_t& i, std::size_t n)
2094 {
2095 unsigned width = 0;
2096 int prec = -1;
2097 int bitwidth = 0;
2098 bool discard = false;
2099 octave_value val_type;
2100 char type = '\0';
2101
2102 m_buf << s[i++];
2103
2104 bool have_width = false;
2105
2106 while (i < n)
2107 {
2108 switch (s[i])
2109 {
2110 case '*':
2111 if (discard)
2112 m_nconv = -1;
2113 else
2114 {
2115 discard = true;
2116 m_buf << s[i++];
2117 }
2118 break;
2119
2120 case '0': case '1': case '2': case '3': case '4':
2121 case '5': case '6': case '7': case '8': case '9':
2122 if (have_width)
2123 m_nconv = -1;
2124 else
2125 {
2126 char c = s[i++];
2127 width = width * 10 + c - '0';
2128 have_width = true;
2129 m_buf << c;
2130 while (i < n && isdigit (s[i]))
2131 {
2132 c = s[i++];
2133 width = width * 10 + c - '0';
2134 m_buf << c;
2135 }
2136
2137 if (i < n && s[i] == '.')
2138 {
2139 m_buf << s[i++];
2140 prec = 0;
2141 while (i < n && isdigit (s[i]))
2142 {
2143 c = s[i++];
2144 prec = prec * 10 + c - '0';
2145 m_buf << c;
2146 }
2147 }
2148 }
2149 break;
2150
2151 case 'd': case 'u':
2152 {
2153 bool done = true;
2154 m_buf << (type = s[i++]);
2155 if (i < n)
2156 {
2157 if (s[i] == '8')
2158 {
2159 bitwidth = 8;
2160 if (type == 'd')
2161 val_type = octave_value (int8NDArray ());
2162 else
2163 val_type = octave_value (uint8NDArray ());
2164 m_buf << s[i++];
2165 }
2166 else if (s[i] == '1' && i+1 < n && s[i+1] == '6')
2167 {
2168 bitwidth = 16;
2169 if (type == 'd')
2170 val_type = octave_value (int16NDArray ());
2171 else
2172 val_type = octave_value (uint16NDArray ());
2173 m_buf << s[i++];
2174 m_buf << s[i++];
2175 }
2176 else if (s[i] == '3' && i+1 < n && s[i+1] == '2')
2177 {
2178 done = false; // use default size below
2179 m_buf << s[i++];
2180 m_buf << s[i++];
2181 }
2182 else if (s[i] == '6' && i+1 < n && s[i+1] == '4')
2183 {
2184 bitwidth = 64;
2185 if (type == 'd')
2186 val_type = octave_value (int64NDArray ());
2187 else
2188 val_type = octave_value (uint64NDArray ());
2189 m_buf << s[i++];
2190 m_buf << s[i++];
2191 }
2192 else
2193 done = false;
2194 }
2195 else
2196 done = false;
2197
2198 if (! done)
2199 {
2200 bitwidth = 32;
2201 if (type == 'd')
2202 val_type = octave_value (int32NDArray ());
2203 else
2204 val_type = octave_value (uint32NDArray ());
2205 }
2206 goto fini;
2207 }
2208
2209 case 'f':
2210 m_buf << (type = s[i++]);
2211 bitwidth = 64;
2212 if (i < n)
2213 {
2214 if (s[i] == '3' && i+1 < n && s[i+1] == '2')
2215 {
2216 bitwidth = 32;
2217 val_type = octave_value (FloatNDArray ());
2218 m_buf << s[i++];
2219 m_buf << s[i++];
2220 }
2221 else if (s[i] == '6' && i+1 < n && s[i+1] == '4')
2222 {
2223 val_type = octave_value (NDArray ());
2224 m_buf << s[i++];
2225 m_buf << s[i++];
2226 }
2227 else
2228 val_type = octave_value (NDArray ());
2229 }
2230 else
2231 val_type = octave_value (NDArray ());
2232 goto fini;
2233
2234 case 'n':
2235 m_buf << (type = s[i++]);
2236 bitwidth = 64;
2237 val_type = octave_value (NDArray ());
2238 goto fini;
2239
2240 case 's': case 'q': case '[': case 'c':
2241 if (! discard)
2242 val_type = octave_value (Cell ());
2243 m_buf << (type = s[i++]);
2244 has_string = true;
2245 goto fini;
2246
2247 fini:
2248 {
2249 if (! have_width)
2250 {
2251 if (type == 'c') // %c defaults to one character
2252 width = 1;
2253 else
2254 width = static_cast<unsigned int> (-1); // others: unlimited
2255 }
2256
2257 if (finish_conversion (s, i, n, width, prec, bitwidth, val_type,
2258 discard, type) == 0)
2259 return;
2260 }
2261 break;
2262
2263 default:
2264 error ("%s: '%%%c' is not a valid format specifier",
2265 who.c_str (), s[i]);
2266 }
2267
2268 if (m_nconv < 0)
2269 break;
2270 }
2271
2272 m_nconv = -1;
2273 }
2274
2275 // Parse [...] and [^...]
2276 //
2277 // Matlab does not expand expressions like A-Z, but they are useful, and
2278 // so we parse them "carefully". We treat '-' as a usual character
2279 // unless both start and end characters are from the same class (upper
2280 // case, lower case, numeric), or this is not the first '-' in the
2281 // pattern.
2282 //
2283 // Keep both a running list of characters and a mask of which chars have
2284 // occurred. The first is efficient for patterns with few characters.
2285 // The latter is efficient for [^...] patterns.
2286
2287 std::string
2288 textscan_format_list::parse_char_class (const std::string& pattern) const
2289 {
2290 int len = pattern.length ();
2291 if (len == 0)
2292 return "";
2293
2294 std::string retval (256, '\0');
2295 std::string mask (256, '\0'); // number of times chr has been seen
2296
2297 int in = 0, out = 0;
2298 unsigned char ch, prev = 0;
2299 bool flip = false;
2300
2301 ch = pattern[in];
2302 if (ch == '^')
2303 {
2304 in++;
2305 flip = true;
2306 }
2307 mask[pattern[in]] = '\1';
2308 retval[out++] = pattern[in++]; // even copy ']' if it is first
2309
2310 bool prev_was_range = false; // disallow "a-m-z" as a pattern
2311 bool prev_prev_was_range = false;
2312 for (; in < len; in++)
2313 {
2314 bool was_range = false;
2315 ch = pattern[in];
2316 if (ch == ']')
2317 break;
2318
2319 if (prev == '-' && in > 1 && isalnum (ch) && ! prev_prev_was_range)
2320 {
2321 unsigned char start_of_range = pattern[in-2];
2322 if (start_of_range < ch
2323 && ((isupper (ch) && isupper (start_of_range))
2324 || (islower (ch) && islower (start_of_range))
2325 || (isdigit (ch) && isdigit (start_of_range))
2326 || mask['-'] > 1)) // not the first '-'
2327 {
2328 was_range = true;
2329 out--;
2330 mask['-']--;
2331 for (int i = start_of_range; i <= ch; i++)
2332 {
2333 if (mask[i] == '\0')
2334 {
2335 mask[i] = '\1';
2336 retval[out++] = i;
2337 }
2338 }
2339 }
2340 }
2341 if (! was_range)
2342 {
2343 if (mask[ch]++ == 0)
2344 retval[out++] = ch;
2345 else if (ch != '-')
2346 warning_with_id ("Octave:textscan-pattern",
2347 "%s: [...] contains two '%c's",
2348 who.c_str (), ch);
2349
2350 if (prev == '-' && mask['-'] >= 2)
2352 ("Octave:textscan-pattern",
2353 "%s: [...] contains two '-'s outside range expressions",
2354 who.c_str ());
2355 }
2356 prev = ch;
2357 prev_prev_was_range = prev_was_range;
2358 prev_was_range = was_range;
2359 }
2360
2361 if (flip) // [^...]
2362 {
2363 out = 0;
2364 for (int i = 0; i < 256; i++)
2365 if (! mask[i])
2366 retval[out++] = i;
2367 }
2368
2369 retval.resize (out);
2370
2371 return retval;
2372 }
2373
2374 int
2375 textscan_format_list::finish_conversion (const std::string& s, std::size_t& i,
2376 std::size_t n, unsigned int width,
2377 int prec, int bitwidth,
2378 octave_value& val_type, bool discard,
2379 char& type)
2380 {
2381 int retval = 0;
2382
2383 std::string char_class;
2384
2385 std::size_t beg_idx = std::string::npos;
2386 std::size_t end_idx = std::string::npos;
2387
2388 if (type != '%')
2389 {
2390 m_nconv++;
2391 if (type == '[')
2392 {
2393 if (i < n)
2394 {
2395 beg_idx = i;
2396
2397 if (s[i] == '^')
2398 {
2399 type = '^';
2400 m_buf << s[i++];
2401
2402 if (i < n)
2403 {
2404 beg_idx = i;
2405
2406 if (s[i] == ']')
2407 m_buf << s[i++];
2408 }
2409 }
2410 else if (s[i] == ']')
2411 m_buf << s[i++];
2412 }
2413
2414 while (i < n && s[i] != ']')
2415 m_buf << s[i++];
2416
2417 if (i < n && s[i] == ']')
2418 {
2419 end_idx = i-1;
2420 m_buf << s[i++];
2421 }
2422
2423 if (s[i-1] != ']')
2424 retval = m_nconv = -1;
2425 }
2426 }
2427
2428 if (m_nconv >= 0)
2429 {
2430 if (beg_idx != std::string::npos && end_idx != std::string::npos)
2431 char_class = parse_char_class (s.substr (beg_idx,
2432 end_idx - beg_idx + 1));
2433
2434 add_elt_to_list (width, prec, bitwidth, val_type, discard, type,
2435 char_class);
2436 }
2437
2438 return retval;
2439 }
2440
2441 void
2443 {
2444 std::size_t n = numel ();
2445
2446 for (std::size_t i = 0; i < n; i++)
2447 {
2449
2450 std::cerr
2451 << "width: " << elt->width << "\n"
2452 << "digits " << elt->prec << "\n"
2453 << "bitwidth: " << elt->bitwidth << "\n"
2454 << "discard: " << elt->discard << "\n"
2455 << "type: ";
2456
2458 std::cerr << "literal text\n";
2460 std::cerr << "whitespace\n";
2461 else
2462 std::cerr << elt->type << "\n";
2463
2464 std::cerr
2465 << "char_class: '" << undo_string_escapes (elt->char_class) << "'\n"
2466 << "text: '" << undo_string_escapes (elt->text) << "'\n\n";
2467 }
2468 }
2469
2470 // If FORMAT is explicitly "", it is assumed to be "%f" repeated enough
2471 // times to read the first row of the file. Set it now.
2472
2473 int
2475 {
2476 // Read first line and strip end-of-line, which may be two characters
2477 std::string first_line (20, ' ');
2478
2479 is.getline (first_line, static_cast<char> (ts.m_eol2));
2480
2481 if (! first_line.empty () && first_line.back () == ts.m_eol1)
2482 first_line.pop_back ();
2483
2484 std::istringstream strstr (first_line);
2485 delimited_stream ds (strstr, is);
2486
2487 dim_vector dv (1, 1); // initial size of each output_container
2488 Complex val;
2489 octave_value val_type;
2490 m_nconv = 0;
2491 int max_empty = 1000; // failsafe, if ds fails but not with eof
2492 int retval = 0;
2493
2494 // read line, creating output_container as we go
2495 while (! ds.eof ())
2496 {
2497 bool already_skipped_delim = false;
2498 ts.skip_whitespace (ds, false);
2499 ds.progress_benchmark ();
2500 ts.scan_complex (ds, *m_fmt_elts[0], val);
2501 if (ds.fail ())
2502 {
2503 ds.clear (ds.rdstate () & ~std::ios::failbit);
2504
2505 if (ds.eof ())
2506 break;
2507
2508 // Unless this was a missing value (i.e., followed by a delimiter),
2509 // return with an error status.
2510 ts.skip_delim (ds);
2511 if (ds.no_progress ())
2512 {
2513 retval = 4;
2514 break;
2515 }
2516 already_skipped_delim = true;
2517
2518 val = ts.m_empty_value.scalar_value ();
2519
2520 if (! --max_empty)
2521 break;
2522 }
2523
2524 if (val.imag () == 0)
2525 val_type = octave_value (NDArray (dv, val.real ()));
2526 else
2527 val_type = octave_value (ComplexNDArray (dv, val));
2528
2529 m_output_container.push_back (val_type);
2530
2531 if (! already_skipped_delim)
2532 ts.skip_delim (ds);
2533
2534 if (ds.no_progress ())
2535 break;
2536
2537 m_nconv++;
2538 }
2539
2540 m_output_container.pop_front (); // discard empty element from constructor
2541
2542 // Create fmt_list now that the size is known
2543 for (octave_idx_type i = 1; i < m_nconv; i++)
2544 m_fmt_elts.push_back (new textscan_format_elt (*m_fmt_elts[0]));
2545
2546 return retval; // May have returned 4 above.
2547 }
2548
2549 textscan::textscan (const std::string& who_arg, const std::string& encoding)
2550 : m_who (who_arg), m_encoding (encoding), m_buf (), m_whitespace_table (),
2551 m_delim_table (), m_delims (), m_comment_style (), m_comment_len (0),
2552 m_comment_char (-2), m_buffer_size (0), m_date_locale (),
2553 m_inf_nan (init_inf_nan ()),
2554 m_empty_value (numeric_limits<double>::NaN ()),
2555 m_exp_chars ("edED"), m_header_lines (0), m_treat_as_empty (),
2556 m_treat_as_empty_len (0), m_whitespace (" \b\t"), m_eol1 ('\r'),
2557 m_eol2 ('\n'), m_return_on_error (1), m_collect_output (false),
2558 multiple_delims_as_one (false), m_default_exp (true), m_lines (0)
2559 { }
2560
2562 textscan::scan (std::istream& isp, const std::string& fmt,
2563 octave_idx_type ntimes, const octave_value_list& options,
2564 octave_idx_type& count)
2565 {
2566 textscan_format_list fmt_list (fmt);
2567
2568 parse_options (options, fmt_list);
2569
2570 octave_value result = do_scan (isp, fmt_list, ntimes);
2571
2572 // FIXME: this is probably not the best way to get count. The
2573 // position could easily be larger than octave_idx_type when using
2574 // 32-bit indexing.
2575
2576 std::ios::iostate state = isp.rdstate ();
2577 isp.clear ();
2578 count = static_cast<octave_idx_type> (isp.tellg ());
2579 isp.setstate (state);
2580
2581 return result;
2582 }
2583
2585 textscan::do_scan (std::istream& isp, textscan_format_list& fmt_list,
2586 octave_idx_type ntimes)
2587 {
2588 octave_value retval;
2589
2590 if (fmt_list.num_conversions () == -1)
2591 error ("%s: invalid format specified", m_who.c_str ());
2592
2593 if (fmt_list.num_conversions () == 0)
2594 error ("%s: no valid format conversion specifiers", m_who.c_str ());
2595
2596 // skip the first header_lines
2597 std::string dummy;
2598 for (int i = 0; i < m_header_lines && isp; i++)
2599 getline (isp, dummy, static_cast<char> (m_eol2));
2600
2601 // Create our own buffered stream, for fast get/putback/tell/seek.
2602
2603 // First, see how far ahead it should let us look.
2604 int max_lookahead = std::max ({m_comment_len, m_treat_as_empty_len,
2605 m_delim_len, 3}); // 3 for NaN and Inf
2606
2607 // Next, choose a buffer size to avoid reading too much, or too often.
2608 octave_idx_type buf_size = 4096;
2609 if (m_buffer_size)
2610 buf_size = m_buffer_size;
2611 else if (ntimes > 0)
2612 {
2613 // Avoid overflow of 80*ntimes...
2614 buf_size = std::min (buf_size, std::max (ntimes, 80 * ntimes));
2615 buf_size = std::max (buf_size, ntimes);
2616 }
2617 // Finally, create the stream.
2618 delimited_stream is (isp,
2619 (m_delim_table.empty () ? m_whitespace + "\r\n"
2620 : m_delims),
2621 max_lookahead, buf_size);
2622
2623 // Grow retval dynamically. "size" is half the initial size
2624 // (FIXME: Should we start smaller if ntimes is large?)
2625 octave_idx_type size = ((ntimes < 8 && ntimes >= 0) ? ntimes : 1);
2626 Array<octave_idx_type> row_idx (dim_vector (1, 2));
2627 row_idx(1) = 0;
2628
2629 int err = 0;
2630 octave_idx_type row = 0;
2631
2632 if (multiple_delims_as_one) // bug #44750?
2633 skip_delim (is);
2634
2635 int done_after; // Number of columns read when EOF seen.
2636
2637 // If FORMAT explicitly "", read first line and see how many "%f" match
2638 if (fmt_list.set_from_first)
2639 {
2640 err = fmt_list.read_first_row (is, *this);
2641 m_lines = 1;
2642
2643 done_after = fmt_list.numel () + 1;
2644 if (! err)
2645 row = 1; // the above puts the first line into fmt_list.out_buf ()
2646 }
2647 else
2648 done_after = fmt_list.out_buf ().size () + 1;
2649
2650 std::list<octave_value> out = fmt_list.out_buf ();
2651
2652 // We will later merge adjacent columns of the same type.
2653 // Check now which columns to merge.
2654 // Reals may become complex, and so we can't trust types
2655 // after reading in data.
2656 // If the format was "", that conversion may already have happened,
2657 // so force all to be merged (as all are %f).
2658 bool merge_with_prev[fmt_list.numel ()];
2659 int conv = 0;
2660 if (m_collect_output)
2661 {
2662 int prev_type = -1;
2663 for (const auto& col : out)
2664 {
2665 if (col.type_id () == prev_type
2666 || (fmt_list.set_from_first && prev_type != -1))
2667 merge_with_prev[conv++] = true;
2668 else
2669 merge_with_prev[conv++] = false;
2670
2671 prev_type = col.type_id ();
2672 }
2673 }
2674
2675 // This should be caught by earlier code, but this avoids a possible
2676 // infinite loop below.
2677 if (fmt_list.num_conversions () == 0)
2678 error ("%s: No conversions specified", m_who.c_str ());
2679
2680 // Read the data. This is the main loop.
2681 if (! err)
2682 {
2683 for (/* row set ~30 m_lines above */;
2684 row < ntimes || ntimes == -1;
2685 row++)
2686 {
2687 if (row == 0 || row >= size)
2688 {
2689 size += (size+1);
2690 for (auto& col : out)
2691 col = col.resize (dim_vector (size, 1), 0);
2692 }
2693
2694 row_idx(0) = row;
2695 err = read_format_once (is, fmt_list, out, row_idx, done_after);
2696
2697 if ((err & ~1) > 0 || ! is || (m_lines >= ntimes && ntimes > -1))
2698 break;
2699 }
2700 }
2701
2702 if ((err & 4) && ! m_return_on_error)
2703 error ("%s: Read error in field %d of row %" OCTAVE_IDX_TYPE_FORMAT,
2704 m_who.c_str (), done_after + 1, row + 1);
2705
2706 // If file does not end in EOL, do not pad columns with NaN.
2707 bool uneven_columns = false;
2708 if (err & 4)
2709 uneven_columns = true;
2710 else if (isp.eof ())
2711 {
2712 isp.clear ();
2713 isp.seekg (-1, std::ios_base::end);
2714 int last_char = isp.get ();
2715 isp.setstate (isp.eofbit);
2716 uneven_columns = (last_char != m_eol1 && last_char != m_eol2);
2717 }
2718
2719 // convert return value to Cell array
2721
2722 // (err & 1) means "error, and no columns read this row
2723 // FIXME: This may redundant now that done_after=0 says the same
2724 if (err & 1)
2725 done_after = out.size () + 1;
2726
2727 int valid_rows = (row == ntimes
2728 ? ntimes
2729 : ((err & 1) && (err & 8)) ? row : row+1);
2730 dim_vector dv (valid_rows, 1);
2731
2732 ra_idx(0) = 0;
2733 int i = 0;
2734 if (! m_collect_output)
2735 {
2736 retval = Cell (dim_vector (1, out.size ()));
2737 for (auto& col : out)
2738 {
2739 // trim last columns if that was requested
2740 if (i == done_after && uneven_columns)
2741 dv = dim_vector (std::max (valid_rows - 1, 0), 1);
2742
2743 ra_idx(1) = i;
2744 retval = cat_op (retval, octave_value (Cell (col.resize (dv, 0))),
2745 ra_idx);
2746 i++;
2747 }
2748 }
2749 else // group adjacent cells of the same type into a single cell
2750 {
2751 octave_value cur; // current cell, accumulating columns
2752 octave_idx_type group_size = 0; // columns in this cell
2753 int prev_type = -1;
2754
2755 conv = 0;
2756 retval = Cell ();
2757 for (auto& col : out)
2758 {
2759 if (! merge_with_prev[conv++]) // including first time
2760 {
2761 if (prev_type != -1)
2762 {
2763 ra_idx(1) = i++;
2764 retval = cat_op (retval, octave_value (Cell (cur)), ra_idx);
2765 }
2766 cur = octave_value (col.resize (dv, 0));
2767 group_size = 1;
2768 prev_type = col.type_id ();
2769 }
2770 else
2771 {
2772 ra_idx(1) = group_size++;
2773 cur = cat_op (cur, octave_value (col.resize (dv, 0)), ra_idx);
2774 }
2775 }
2776 ra_idx(1) = i;
2777 retval = cat_op (retval, octave_value (Cell (cur)), ra_idx);
2778 }
2779
2780 return retval;
2781 }
2782
2783 // Read a double considering the "precision" field of FMT and the
2784 // EXP_CHARS option of OPTIONS.
2785
2786 double
2788 const textscan_format_elt& fmt) const
2789 {
2790 int sign = 1;
2791 unsigned int width_left = fmt.width;
2792 double retval = 0;
2793 bool valid = false; // syntactically correct double?
2794
2795 int ch = is.peek ();
2796
2797 if (ch == '+')
2798 {
2799 is.get ();
2800 ch = is.peek ();
2801 if (width_left)
2802 width_left--;
2803 }
2804 else if (ch == '-')
2805 {
2806 sign = -1;
2807 is.get ();
2808 ch = is.peek ();
2809 if (width_left)
2810 width_left--;
2811 }
2812
2813 // Read integer part
2814 if (ch != '.')
2815 {
2816 if (ch >= '0' && ch <= '9') // valid if at least one digit
2817 valid = true;
2818 while (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
2819 retval = retval * 10 + (ch - '0');
2820 width_left++;
2821 }
2822
2823 // Read fractional part, up to specified precision
2824 if (ch == '.' && width_left)
2825 {
2826 double multiplier = 1;
2827 int precision = fmt.prec;
2828 int i;
2829
2830 width_left--; // Consider width of '.'
2831
2832 if (precision == -1)
2833 precision = 1<<30; // FIXME: Should be MAXINT
2834
2835 if (! valid) // if there was nothing before '.'...
2836 is.get (); // ...ch was a "peek", not "get".
2837
2838 for (i = 0; i < precision; i++)
2839 {
2840 if (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
2841 retval += (ch - '0') * (multiplier *= 0.1);
2842 else
2843 {
2844 width_left++;
2845 break;
2846 }
2847 }
2848
2849 // round up if we truncated and the next digit is >= 5
2850 if ((i == precision || ! width_left) && (ch = is.get ()) >= '5'
2851 && ch <= '9')
2852 retval += multiplier;
2853
2854 if (i > 0)
2855 valid = true; // valid if at least one digit after '.'
2856
2857 // skip remainder after '.', to field width, to look for exponent
2858 if (i == precision)
2859 while (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
2860 ; // discard
2861
2862 width_left++;
2863 }
2864
2865 // look for exponent part in, e.g., 6.023E+23
2866 bool used_exp = false;
2867 if (valid && width_left > 1 && m_exp_chars.find (ch) != std::string::npos)
2868 {
2869 int ch1 = is.peek ();
2870 if (ch1 == '-' || ch1 == '+' || (ch1 >= '0' && ch1 <= '9'))
2871 {
2872 // if 1.0e+$ or some such, this will set failbit, as we want
2873 width_left--; // count "E"
2874 int exp = 0;
2875 int exp_sign = 1;
2876 if (ch1 == '+')
2877 {
2878 width_left--;
2879 is.get ();
2880 }
2881 else if (ch1 == '-')
2882 {
2883 width_left--;
2884 exp_sign = -1;
2885 is.get ();
2886 }
2887 valid = false;
2888 while (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
2889 {
2890 exp = exp*10 + ch - '0';
2891 valid = true;
2892 }
2893 width_left++;
2894 if (ch != std::istream::traits_type::eof () && width_left)
2895 is.putback (ch);
2896
2897 double multiplier = pown (10, exp);
2898 if (exp_sign > 0)
2899 retval *= multiplier;
2900 else
2901 retval /= multiplier;
2902
2903 used_exp = true;
2904 }
2905 }
2906 is.clear ();
2907 if (! used_exp && ch != std::istream::traits_type::eof () && width_left)
2908 is.putback (ch);
2909
2910 // Check for +/- inf and NaN
2911 if (! valid && width_left >= 3)
2912 {
2913 int i = lookahead (is, m_inf_nan, 3, false); // false -> case insensitive
2914 if (i == 0)
2915 {
2916 retval = numeric_limits<double>::Inf ();
2917 valid = true;
2918 }
2919 else if (i == 1)
2920 {
2921 retval = numeric_limits<double>::NaN ();
2922 valid = true;
2923 }
2924 }
2925
2926 if (! valid)
2927 is.setstate (std::ios::failbit);
2928 else
2929 is.setstate (is.rdstate () & ~std::ios::failbit);
2930
2931 return retval * sign;
2932 }
2933
2934 // Read a single number: real, complex, inf, NaN, possibly with limited
2935 // precision. Calls to this should be preceded by skip_whitespace.
2936 // Calling that inside scan_complex would violate its const declaration.
2937
2938 void
2940 Complex& val) const
2941 {
2942 double im = 0;
2943 double re = 0;
2944 bool as_empty = false; // did we fail but match a "treat_as_empty" string?
2945 bool inf = false;
2946
2947 int ch = is.peek ();
2948 if (ch == '+' || ch == '-') // check for [+-][ij] with no coefficients
2949 {
2950 ch = is.get ();
2951 int ch2 = is.peek ();
2952 if (ch2 == 'i' || ch2 == 'j')
2953 {
2954 double value = 1;
2955 is.get ();
2956 // Check not -inf
2957 if (is.peek () == 'n')
2958 {
2959 char *pos = is.tellg ();
2960 std::ios::iostate state = is.rdstate ();
2961
2962 is.get ();
2963 ch2 = is.get ();
2964 if (ch2 == 'f')
2965 {
2966 inf = true;
2967 re = (ch == '+' ? numeric_limits<double>::Inf ()
2969 value = 0;
2970 }
2971 else
2972 {
2973 is.clear (state);
2974 is.seekg (pos); // reset to position before look-ahead
2975 }
2976 }
2977
2978 im = (ch == '+') ? value : -value;
2979 }
2980 else
2981 is.putback (ch);
2982 }
2983
2984 if (! im && ! inf) // if not [+-][ij] or [+-]inf, read real normally
2985 {
2986 char *pos = is.tellg ();
2987 std::ios::iostate state = is.rdstate ();
2988 //re = read_value<double> (is);
2989 re = read_double (is, fmt);
2990
2991 // check for "treat as empty" string
2992 if (m_treat_as_empty.numel ()
2993 && (is.fail () || math::is_NaN_or_NA (Complex (re))
2994 || re == numeric_limits<double>::Inf ()))
2995 {
2996
2997 for (int i = 0; i < m_treat_as_empty.numel (); i++)
2998 {
2999 if (ch == m_treat_as_empty (i).string_value ()[0])
3000 {
3001 as_empty = true; // first char matches, so read the lot
3002 break;
3003 }
3004 }
3005 if (as_empty) // if first char matched...
3006 {
3007 as_empty = false; // ...look for the whole string
3008
3009 is.clear (state); // treat_as_empty "-" causes partial read
3010 is.seekg (pos); // reset to position before failed read
3011
3012 // treat_as_empty strings may be different sizes.
3013 // Read ahead longest, put it all back, then re-read the string
3014 // that matches.
3015 std::string look_buf (m_treat_as_empty_len, '\0');
3016 char *look = is.read (&look_buf[0], look_buf.size (), pos);
3017
3018 is.clear (state);
3019 is.seekg (pos); // reset to position before look-ahead
3020 // FIXME: is.read could invalidate pos
3021
3022 for (int i = 0; i < m_treat_as_empty.numel (); i++)
3023 {
3024 std::string s = m_treat_as_empty (i).string_value ();
3025 if (! strncmp (s.c_str (), look, s.size ()))
3026 {
3027 as_empty = true;
3028 // read just the right amount
3029 is.read (&look_buf[0], s.size (), pos);
3030 break;
3031 }
3032 }
3033 }
3034 }
3035
3036 if (! is.eof () && ! as_empty)
3037 {
3038 state = is.rdstate (); // before tellg, since that fails at EOF
3039
3040 ch = is.peek (); // ch == EOF if read failed; no need to chk fail
3041 if (ch == 'i' || ch == 'j') // pure imaginary
3042 {
3043 is.get ();
3044 im = re;
3045 re = 0;
3046 }
3047 else if (ch == '+' || ch == '-') // see if it is real+imag[ij]
3048 {
3049 // save stream state in case we have to restore it
3050 pos = is.tellg ();
3051 state = is.rdstate ();
3052
3053 //im = read_value<double> (is);
3054 im = read_double (is, fmt);
3055 if (is.fail ())
3056 im = 1;
3057
3058 if (is.peek () == 'i' || is.peek () == 'j')
3059 is.get ();
3060 else
3061 {
3062 im = 0; // no valid imaginary part. Restore state
3063 is.clear (state); // eof shouldn't cause fail.
3064 is.seekg (pos);
3065 }
3066 }
3067 else if (is.eof ()) // we've read enough to be a "valid" read
3068 is.clear (state); // failed peek shouldn't cause fail
3069 }
3070 }
3071 if (as_empty)
3072 val = m_empty_value.scalar_value ();
3073 else
3074 val = Complex (re, im);
3075 }
3076
3077 // Return in VAL the run of characters from IS NOT contained in PATTERN.
3078
3079 int
3080 textscan::scan_caret (delimited_stream& is, const std::string& pattern,
3081 std::string& val) const
3082 {
3083 int c1 = std::istream::traits_type::eof ();
3084 std::ostringstream obuf; // FIXME: is this optimized for growing?
3085
3086 while (is && ((c1 = (is && ! is.eof ())
3087 ? is.get_undelim ()
3088 : std::istream::traits_type::eof ())
3089 != std::istream::traits_type::eof ())
3090 && pattern.find (c1) == std::string::npos)
3091 obuf << static_cast<char> (c1);
3092
3093 val = obuf.str ();
3094
3095 if (c1 != std::istream::traits_type::eof ())
3096 is.putback (c1);
3097
3098 return c1;
3099 }
3100
3101 // Read until one of the strings in DELIMITERS is found. For
3102 // efficiency, ENDS is a list of the last character of each delimiter.
3103
3104 std::string
3106 const std::string& ends) const
3107 {
3108 std::string retval ("");
3109 bool done = false;
3110 do
3111 {
3112 // find sequence ending with an ending char
3113 std::string next;
3114 scan_caret (is, ends.c_str (), next);
3115 retval = retval + next; // FIXME: could use repeated doubling of size
3116
3117 int last = (! is.eof ()
3118 ? is.get_undelim () : std::istream::traits_type::eof ());
3119
3120 if (last != std::istream::traits_type::eof ())
3121 {
3122 if (last == m_eol1 || last == m_eol2)
3123 break;
3124
3125 retval = retval + static_cast<char> (last);
3126 for (int i = 0; i < delimiters.numel (); i++)
3127 {
3128 std::string delim = delimiters(i).string_value ();
3129 std::size_t start = (retval.length () > delim.length ()
3130 ? retval.length () - delim.length ()
3131 : 0);
3132 std::string may_match = retval.substr (start);
3133 if (may_match == delim)
3134 {
3135 done = true;
3136 retval = retval.substr (0, start);
3137 if (start == 0)
3138 is.putback (last);
3139 break;
3140 }
3141 }
3142 }
3143 }
3144 while (! done && is && ! is.eof ());
3145
3146 return retval;
3147 }
3148
3149 // Read stream until either fmt.width chars have been read, or
3150 // options.delimiter has been found. Does *not* rely on fmt being 's'.
3151 // Used by formats like %6f to limit to 6.
3152
3153 void
3155 std::string& val) const
3156 {
3157 if (m_delim_list.isempty ())
3158 {
3159 unsigned int i = 0;
3160 unsigned int width = fmt.width;
3161
3162 for (i = 0; i < width; i++)
3163 {
3164 // Grow string in an exponential fashion if necessary.
3165 if (i >= val.length ())
3166 val.append (std::max (val.length (),
3167 static_cast<std::size_t> (16)), '\0');
3168
3169 int ch = is.get ();
3170 if (is_delim (ch) || ch == std::istream::traits_type::eof ())
3171 {
3172 is.putback (ch);
3173 break;
3174 }
3175 else
3176 val[i] = ch;
3177 }
3178 val = val.substr (0, i); // trim pre-allocation
3179 }
3180 else // Cell array of multi-character delimiters
3181 {
3182 std::string ends (m_delim_list.numel () + 2, '\0');
3183 int i;
3184 for (i = 0; i < m_delim_list.numel (); i++)
3185 {
3186 std::string tmp = m_delim_list(i).string_value ();
3187 ends[i] = tmp.back ();
3188 }
3189 ends[i++] = m_eol1;
3190 ends[i++] = m_eol2;
3191 val = textscan::read_until (is, m_delim_list, ends);
3192 }
3193
3194 // convert from codepage
3195 if (m_encoding.compare ("utf-8"))
3196 val = string::u8_from_encoding ("textscan", val, m_encoding);
3197 }
3198
3199 // Return in VAL the run of characters from IS contained in PATTERN.
3200
3201 int
3202 textscan::scan_bracket (delimited_stream& is, const std::string& pattern,
3203 std::string& val) const
3204 {
3205 int c1 = std::istream::traits_type::eof ();
3206 std::ostringstream obuf; // Is this optimized for growing?
3207
3208 while (is && pattern.find (c1 = is.get_undelim ()) != std::string::npos)
3209 obuf << static_cast<char> (c1);
3210
3211 val = obuf.str ();
3212 if (c1 != std::istream::traits_type::eof ())
3213 is.putback (c1);
3214 return c1;
3215 }
3216
3217 // Return in VAL a string, either delimited by whitespace/delimiters, or
3218 // enclosed in a pair of double quotes ("..."). Enclosing quotes are
3219 // removed. A consecutive pair "" is inserted into VAL as a single ".
3220
3221 void
3223 std::string& val)
3224 {
3225 skip_whitespace (is);
3226
3227 if (is.peek () != '"')
3228 scan_string (is, fmt, val);
3229 else
3230 {
3231 is.get ();
3232 scan_caret (is, R"(")", val); // read everything until "
3233 is.get (); // swallow "
3234
3235 while (is && is.peek_undelim () == '"') // if double ",
3236 { // insert one in stream,
3237 is.get (); // keep looking for single "
3238 std::string val1;
3239 scan_caret (is, R"(")", val1);
3240 val = val + '"' + val1;
3241 is.get_undelim ();
3242 }
3243 }
3244
3245 // convert from codepage
3246 if (m_encoding.compare ("utf-8"))
3247 val = string::u8_from_encoding ("textscan", val, m_encoding);
3248 }
3249
3250 // Read from IS into VAL a string of the next fmt.width characters,
3251 // including any whitespace or delimiters.
3252
3253 void
3255 std::string& val) const
3256 {
3257 val.resize (fmt.width);
3258
3259 for (unsigned int i = 0; is && i < fmt.width; i++)
3260 {
3261 int ch = is.get_undelim ();
3262 if (ch != std::istream::traits_type::eof ())
3263 val[i] = ch;
3264 else
3265 {
3266 val.resize (i);
3267 break;
3268 }
3269 }
3270
3271 // convert from codepage
3272 if (m_encoding.compare ("utf-8"))
3273 val = string::u8_from_encoding ("textscan", val, m_encoding);
3274 }
3275
3276 // Read a single '%...' conversion and place it in position ROW of OV.
3277
3278 void
3281 {
3282 skip_whitespace (is);
3283
3284 is.clear ();
3285
3286 octave_value val;
3287 if (fmt.numeric)
3288 {
3289 if (fmt.type == 'f' || fmt.type == 'n')
3290 {
3291 Complex v;
3292 skip_whitespace (is);
3293 scan_complex (is, fmt, v);
3294
3295 if (! fmt.discard && ! is.fail ())
3296 {
3297 if (fmt.bitwidth == 64)
3298 {
3299 if (ov.isreal () && v.imag () == 0)
3300 ov.internal_rep ()->fast_elem_insert (row(0), v.real ());
3301 else
3302 {
3303 if (ov.isreal ()) // cat does type conversion
3304 ov = cat_op (ov, octave_value (v), row);
3305 else
3306 ov.internal_rep ()->fast_elem_insert (row(0), v);
3307 }
3308 }
3309 else
3310 {
3311 if (ov.isreal () && v.imag () == 0)
3312 ov.internal_rep ()->fast_elem_insert (row(0),
3313 float (v.real ()));
3314 else
3315 {
3316 if (ov.isreal ()) // cat does type conversion
3317 ov = cat_op (ov, octave_value (v), row);
3318 else
3319 ov.internal_rep ()->fast_elem_insert (row(0),
3320 FloatComplex (v));
3321 }
3322 }
3323 }
3324 }
3325 else
3326 {
3327 double v; // Matlab docs say 1e30 etc should be valid for %d and
3328 // 1000 as a %d8 should be 127, so read as double.
3329 // Some loss of precision for d64 and u64.
3330 skip_whitespace (is);
3331 v = read_double (is, fmt);
3332 if (! fmt.discard && ! is.fail ())
3333 switch (fmt.bitwidth)
3334 {
3335 case 64:
3336 switch (fmt.type)
3337 {
3338 case 'd':
3339 {
3340 octave_int64 vv = v;
3341 ov.internal_rep ()->fast_elem_insert (row(0), vv);
3342 }
3343 break;
3344
3345 case 'u':
3346 {
3347 octave_uint64 vv = v;
3348 ov.internal_rep ()->fast_elem_insert (row(0), vv);
3349 }
3350 break;
3351 }
3352 break;
3353
3354 case 32:
3355 switch (fmt.type)
3356 {
3357 case 'd':
3358 {
3359 octave_int32 vv = v;
3360 ov.internal_rep ()->fast_elem_insert (row(0), vv);
3361 }
3362 break;
3363
3364 case 'u':
3365 {
3366 octave_uint32 vv = v;
3367 ov.internal_rep ()->fast_elem_insert (row(0), vv);
3368 }
3369 break;
3370 }
3371 break;
3372
3373 case 16:
3374 if (fmt.type == 'd')
3375 {
3376 octave_int16 vv = v;
3377 ov.internal_rep ()->fast_elem_insert (row(0), vv);
3378 }
3379 else
3380 {
3381 octave_uint16 vv = v;
3382 ov.internal_rep ()->fast_elem_insert (row(0), vv);
3383 }
3384 break;
3385
3386 case 8:
3387 if (fmt.type == 'd')
3388 {
3389 octave_int8 vv = v;
3390 ov.internal_rep ()->fast_elem_insert (row(0), vv);
3391 }
3392 else
3393 {
3394 octave_uint8 vv = v;
3395 ov.internal_rep ()->fast_elem_insert (row(0), vv);
3396 }
3397 break;
3398 }
3399 }
3400
3401 if (is.fail () & ! fmt.discard)
3402 ov = cat_op (ov, m_empty_value, row);
3403 }
3404 else
3405 {
3406 std::string vv (" "); // initial buffer. Grows as needed
3407 switch (fmt.type)
3408 {
3409 case 's':
3410 scan_string (is, fmt, vv);
3411 break;
3412
3413 case 'q':
3414 scan_qstring (is, fmt, vv);
3415 break;
3416
3417 case 'c':
3418 scan_cstring (is, fmt, vv);
3419 break;
3420
3421 case '[':
3422 scan_bracket (is, fmt.char_class.c_str (), vv);
3423 break;
3424
3425 case '^':
3426 scan_caret (is, fmt.char_class.c_str (), vv);
3427 break;
3428 }
3429
3430 if (! fmt.discard)
3431 ov.internal_rep ()->fast_elem_insert (row (0),
3432 Cell (octave_value (vv)));
3433
3434 // FIXME: why does failbit get set at EOF, instead of eofbit?
3435 if (! vv.empty ())
3436 is.clear (is.rdstate () & ~std::ios_base::failbit);
3437 }
3438
3439 is.field_done ();
3440 }
3441
3442 // Read data corresponding to the entire format string once, placing the
3443 // values in row ROW of retval.
3444
3445 int
3447 textscan_format_list& fmt_list,
3448 std::list<octave_value>& retval,
3449 Array<octave_idx_type> row, int& done_after)
3450 {
3451 const textscan_format_elt *elem = fmt_list.first ();
3452 auto out = retval.begin ();
3453 bool no_conversions = true;
3454 bool done = false;
3455 bool conversion_failed = false; // Record for ReturnOnError
3456 bool nothing_worked = true;
3457
3458 octave_quit ();
3459
3460 for (std::size_t i = 0; i < fmt_list.numel (); i++)
3461 {
3462 bool this_conversion_failed = false;
3463
3464 // Clear fail of previous numeric conversions.
3465 is.clear ();
3466
3467 switch (elem->type)
3468 {
3469 case 'C':
3470 case 'D':
3471 warning ("%s: conversion %c not yet implemented",
3472 m_who.c_str (), elem->type);
3473 break;
3474
3475 case 'u':
3476 case 'd':
3477 case 'f':
3478 case 'n':
3479 case 's':
3480 case '[':
3481 case '^':
3482 case 'q':
3483 case 'c':
3484 scan_one (is, *elem, *out, row);
3485 break;
3486
3488 match_literal (is, *elem);
3489 break;
3490
3491 default:
3492 error ("Unknown format element '%c'", elem->type);
3493 }
3494
3495 if (! is.fail ())
3496 {
3497 if (! elem->discard)
3498 no_conversions = false;
3499 }
3500 else
3501 {
3502 is.clear (is.rdstate () & ~std::ios::failbit);
3503
3504 if (! is.eof ())
3505 {
3506 if (m_delim_list.isempty ())
3507 {
3508 if (! is_delim (is.peek ()))
3509 this_conversion_failed = true;
3510 }
3511 else // Cell array of multi-character delimiters
3512 {
3513 char *pos = is.tellg ();
3514 if (-1 == lookahead (is, m_delim_list, m_delim_len))
3515 this_conversion_failed = true;
3516 is.clear ();
3517 is.seekg (pos); // reset to position before look-ahead
3518 }
3519 }
3520 }
3521
3522 if (! elem->discard)
3523 out++;
3524
3525 elem = fmt_list.next ();
3526 char *pos = is.tellg ();
3527
3528 // Skip delimiter before reading the next fmt conversion,
3529 // unless the fmt is a string literal which begins with a delimiter,
3530 // in which case the literal must match everything. Bug #58008
3532 skip_delim (is);
3533 else if (! is_delim (elem->text[0]))
3534 skip_delim (is);
3535
3536 if (is.eof ())
3537 {
3538 if (! done)
3539 done_after = i+1;
3540
3541 // note EOF, but process others to get empty_val.
3542 done = true;
3543 }
3544
3545 if (this_conversion_failed)
3546 {
3547 if (is.tellg () == pos && ! conversion_failed)
3548 {
3549 // done_after = first failure
3550 done_after = i; // note fail, but parse others to get empty_val
3551 conversion_failed = true;
3552 }
3553 else
3554 this_conversion_failed = false;
3555 }
3556 else if (! done && ! conversion_failed)
3557 nothing_worked = false;
3558 }
3559
3560 if (done)
3561 is.setstate (std::ios::eofbit);
3562
3563 return no_conversions
3564 + (is.eof () ? 2 : 0)
3565 + (conversion_failed ? 4 : 0)
3566 + (nothing_worked ? 8 : 0);
3567
3568 }
3569
3570 void
3572 textscan_format_list& fmt_list)
3573 {
3574 int last = args.length ();
3575 int n = last;
3576
3577 if (n & 1)
3578 error ("%s: %d parameters given, but only %d values",
3579 m_who.c_str (), n-n/2, n/2);
3580
3581 m_delim_len = 1;
3582 bool have_delims = false;
3583 for (int i = 0; i < last; i += 2)
3584 {
3585 std::string param = args(i).xstring_value ("%s: Invalid parameter type <%s> for parameter %d",
3586 m_who.c_str (),
3587 args(i).type_name ().c_str (),
3588 i/2 + 1);
3589 std::transform (param.begin (), param.end (), param.begin (), ::tolower);
3590
3591 if (param == "delimiter")
3592 {
3593 bool invalid = true;
3594 if (args(i+1).is_string ())
3595 {
3596 invalid = false;
3597 have_delims = true;
3598 m_delims = args(i+1).string_value ();
3599 if (args(i+1).is_sq_string ())
3601 }
3602 else if (args(i+1).iscell ())
3603 {
3604 invalid = false;
3605 m_delim_list = args(i+1).cell_value ();
3606 m_delim_table = " "; // non-empty, to flag non-default delim
3607
3608 // Check that all elements are strings, and find max length
3609 for (int j = 0; j < m_delim_list.numel (); j++)
3610 {
3611 if (! m_delim_list(j).is_string ())
3612 invalid = true;
3613 else
3614 {
3615 if (m_delim_list(j).is_sq_string ())
3617 .string_value ());
3618 octave_idx_type len = m_delim_list(j).string_value ()
3619 .length ();
3620 m_delim_len = std::max (static_cast<int> (len),
3621 m_delim_len);
3622 }
3623 }
3624 }
3625 if (invalid)
3626 error ("%s: Delimiters must be either a string or cell array of strings",
3627 m_who.c_str ());
3628 }
3629 else if (param == "commentstyle")
3630 {
3631 if (args(i+1).is_string ())
3632 {
3633 // check here for names like "C++", "C", "shell", ...?
3634 m_comment_style = Cell (args(i+1));
3635 }
3636 else if (args(i+1).iscell ())
3637 {
3638 m_comment_style = args(i+1).cell_value ();
3639 int len = m_comment_style.numel ();
3640 if ((len >= 1 && ! m_comment_style (0).is_string ())
3641 || (len >= 2 && ! m_comment_style (1).is_string ())
3642 || (len >= 3))
3643 error ("%s: CommentStyle must be either a string or cell array of one or two strings",
3644 m_who.c_str ());
3645 }
3646 else
3647 error ("%s: CommentStyle must be either a string or cell array of one or two strings",
3648 m_who.c_str ());
3649
3650 // How far ahead do we need to look to detect an open comment
3651 // and which character do we look for?
3652 if (m_comment_style.numel () >= 1)
3653 {
3654 m_comment_len = m_comment_style (0).string_value ().size ();
3655 m_comment_char = m_comment_style (0).string_value ()[0];
3656 }
3657 }
3658 else if (param == "treatasempty")
3659 {
3660 bool invalid = false;
3661 if (args(i+1).is_string ())
3662 {
3663 m_treat_as_empty = Cell (args(i+1));
3664 m_treat_as_empty_len = args(i+1).string_value ().size ();
3665 }
3666 else if (args(i+1).iscell ())
3667 {
3668 m_treat_as_empty = args(i+1).cell_value ();
3669 for (int j = 0; j < m_treat_as_empty.numel (); j++)
3670 if (! m_treat_as_empty (j).is_string ())
3671 invalid = true;
3672 else
3673 {
3674 int k = m_treat_as_empty (j).string_value ().size ();
3675 if (k > m_treat_as_empty_len)
3677 }
3678 }
3679 if (invalid)
3680 error ("%s: TreatAsEmpty must be either a string or cell array of one or two strings",
3681 m_who.c_str ());
3682
3683 // FIXME: Ensure none is a prefix of a later one. Sort by length?
3684 }
3685 else if (param == "collectoutput")
3686 {
3687 m_collect_output = args(i+1).xbool_value ("%s: CollectOutput must be logical or numeric", m_who.c_str ());
3688 }
3689 else if (param == "emptyvalue")
3690 {
3691 m_empty_value = args(i+1).xscalar_value ("%s: EmptyValue must be numeric", m_who.c_str ());
3692 }
3693 else if (param == "headerlines")
3694 {
3695 m_header_lines = args(i+1).xscalar_value ("%s: HeaderLines must be numeric", m_who.c_str ());
3696 }
3697 else if (param == "bufsize")
3698 {
3699 m_buffer_size = args(i+1).xscalar_value ("%s: BufSize must be numeric", m_who.c_str ());
3700 }
3701 else if (param == "multipledelimsasone")
3702 {
3703 multiple_delims_as_one = args(i+1).xbool_value ("%s: MultipleDelimsAsOne must be logical or numeric", m_who.c_str ());
3704 }
3705 else if (param == "returnonerror")
3706 {
3707 m_return_on_error = args(i+1).xbool_value ("%s: ReturnOnError must be logical or numeric", m_who.c_str ());
3708 }
3709 else if (param == "whitespace")
3710 {
3711 m_whitespace = args(i+1).xstring_value ("%s: Whitespace must be a character string", m_who.c_str ());
3712 }
3713 else if (param == "expchars")
3714 {
3715 m_exp_chars = args(i+1).xstring_value ("%s: ExpChars must be a character string", m_who.c_str ());
3716 m_default_exp = false;
3717 }
3718 else if (param == "endofline")
3719 {
3720 bool valid = true;
3721 std::string s = args(i+1).xstring_value (R"(%s: EndOfLine must be at most one character or '\r\n')", m_who.c_str ());
3722 if (args(i+1).is_sq_string ())
3723 s = do_string_escapes (s);
3724 int l = s.length ();
3725 if (l == 0)
3726 m_eol1 = m_eol2 = -2;
3727 else if (l == 1)
3728 m_eol1 = m_eol2 = s.c_str ()[0];
3729 else if (l == 2)
3730 {
3731 m_eol1 = s.c_str ()[0];
3732 m_eol2 = s.c_str ()[1];
3733 if (m_eol1 != '\r' || m_eol2 != '\n') // Why limit it?
3734 valid = false;
3735 }
3736 else
3737 valid = false;
3738
3739 if (! valid)
3740 error (R"(%s: EndOfLine must be at most one character or '\r\n')",
3741 m_who.c_str ());
3742 }
3743 else
3744 error ("%s: unrecognized option '%s'", m_who.c_str (), param.c_str ());
3745 }
3746
3747 // Remove any user-supplied delimiter from whitespace list
3748 for (unsigned int j = 0; j < m_delims.length (); j++)
3749 {
3750 m_whitespace.erase (std::remove (m_whitespace.begin (),
3751 m_whitespace.end (),
3752 m_delims[j]),
3753 m_whitespace.end ());
3754 }
3755 for (int j = 0; j < m_delim_list.numel (); j++)
3756 {
3757 std::string delim = m_delim_list(j).string_value ();
3758 if (delim.length () == 1)
3759 m_whitespace.erase (std::remove (m_whitespace.begin (),
3760 m_whitespace.end (),
3761 delim[0]),
3762 m_whitespace.end ());
3763 }
3764
3765 m_whitespace_table = std::string (256, '\0');
3766 for (unsigned int i = 0; i < m_whitespace.length (); i++)
3768
3769 // For Matlab compatibility, add 0x20 to whitespace, unless
3770 // whitespace is explicitly ignored.
3771 if (! (m_whitespace.empty () && fmt_list.has_string))
3772 m_whitespace_table[' '] = '1';
3773
3774 // Create look-up table of delimiters, based on 'delimiter'
3775 m_delim_table = std::string (256, '\0');
3776 if (m_eol1 >= 0 && m_eol1 < 256)
3777 m_delim_table[m_eol1] = '1'; // EOL is always a delimiter
3778 if (m_eol2 >= 0 && m_eol2 < 256)
3779 m_delim_table[m_eol2] = '1'; // EOL is always a delimiter
3780 if (! have_delims)
3781 for (unsigned int i = 0; i < 256; i++)
3782 {
3783 if (isspace (i))
3784 m_delim_table[i] = '1';
3785 }
3786 else
3787 for (unsigned int i = 0; i < m_delims.length (); i++)
3788 m_delim_table[m_delims[i]] = '1';
3789 }
3790
3791 // Skip comments, and characters specified by the "Whitespace" option.
3792 // If EOLstop == true, don't skip end of line.
3793
3794 int
3796 {
3797 int c1 = std::istream::traits_type::eof ();
3798 bool found_comment = false;
3799
3800 do
3801 {
3802 found_comment = false;
3803 int prev = -1;
3804 while (is
3805 && (c1 = is.get_undelim ()) != std::istream::traits_type::eof ()
3806 && ( ( (c1 == m_eol1 || c1 == m_eol2) && ++m_lines && ! EOLstop)
3807 || isspace (c1)))
3808 {
3809 if (prev == m_eol1 && m_eol1 != m_eol2 && c1 == m_eol2)
3810 m_lines--;
3811 prev = c1;
3812 }
3813
3814 if (c1 == m_comment_char) // see if we match an open comment
3815 {
3816 // save stream state in case we have to restore it
3817 char *pos = is.tellg ();
3818 std::ios::iostate state = is.rdstate ();
3819
3820 std::string tmp (m_comment_len, '\0');
3821 char *look = is.read (&tmp[0], m_comment_len-1, pos); // already read first char
3822 if (is && m_comment_style.numel () > 0
3823 && ! strncmp (m_comment_style(0).string_value ().substr (1).c_str (),
3824 look, m_comment_len-1))
3825 {
3826 found_comment = true;
3827
3828 std::string dummy;
3829 if (m_comment_style.numel () == 1) // skip to end of line
3830 {
3831 std::string eol (3, '\0');
3832 eol[0] = m_eol1;
3833 eol[1] = m_eol2;
3834
3835 scan_caret (is, eol, dummy);
3836 c1 = is.get_undelim ();
3837 if (c1 == m_eol1 && m_eol1 != m_eol2
3838 && is.peek_undelim () == m_eol2)
3839 is.get_undelim ();
3840 m_lines++;
3841 }
3842 else // matching pair
3843 {
3844 std::string end_c = m_comment_style(1).string_value ();
3845 // last char of end-comment sequence
3846 std::string last = end_c.substr (end_c.size () - 1);
3847 std::string may_match ("");
3848 do
3849 {
3850 // find sequence ending with last char
3851 scan_caret (is, last, dummy);
3852 is.get_undelim (); // (read LAST itself)
3853
3854 may_match = may_match + dummy + last;
3855 if (may_match.length () > end_c.length ())
3856 {
3857 std::size_t start = may_match.length ()
3858 - end_c.length ();
3859 may_match = may_match.substr (start);
3860 }
3861 }
3862 while (may_match != end_c && is && ! is.eof ());
3863 }
3864 }
3865 else // wasn't really a comment; restore state
3866 {
3867 is.clear (state);
3868 is.seekg (pos);
3869 }
3870 }
3871 }
3872 while (found_comment);
3873
3874 if (c1 != std::istream::traits_type::eof ())
3875 is.putback (c1);
3876
3877 return c1;
3878 }
3879
3880 // See if the next few characters match one of the strings in target.
3881 // For efficiency, MAX_LEN is the cached longest length of any target.
3882 // Return -1 if none is found, or the index of the match.
3883
3884 int
3885 textscan::lookahead (delimited_stream& is, const Cell& targets, int max_len,
3886 bool case_sensitive) const
3887 {
3888 // target strings may be different sizes.
3889 // Read ahead longest, put it all back, then re-read the string
3890 // that matches.
3891
3892 char *pos = is.tellg ();
3893
3894 std::string tmp (max_len, '\0');
3895 char *look = is.read (&tmp[0], tmp.size (), pos);
3896
3897 is.clear ();
3898 is.seekg (pos); // reset to position before read
3899 // FIXME: pos may be corrupted by is.read
3900
3901 int i;
3902 int (*compare)(const char *, const char *, std::size_t);
3903 compare = (case_sensitive ? strncmp : octave_strncasecmp);
3904
3905 for (i = 0; i < targets.numel (); i++)
3906 {
3907 std::string s = targets (i).string_value ();
3908 if (! (*compare) (s.c_str (), look, s.size ()))
3909 {
3910 is.read (&tmp[0], s.size (), pos); // read just the right amount
3911 break;
3912 }
3913 }
3914
3915 if (i == targets.numel ())
3916 i = -1;
3917
3918 return i;
3919 }
3920
3921 // Skip delimiters -- multiple if MultipleDelimsAsOne specified.
3922 int
3924 {
3925 int c1 = skip_whitespace (is); // Stop once EOL is read
3926 if (m_delim_list.numel () == 0) // single character delimiter
3927 {
3928 if (is_delim (c1) || c1 == m_eol1 || c1 == m_eol2)
3929 {
3930 is.get ();
3931 if (c1 == m_eol1 && is.peek_undelim () == m_eol2)
3932 is.get_undelim (); // if \r\n, skip the \n too.
3933
3935 {
3936 int prev = -1;
3937 // skip multiple delims.
3938 // Increment lines for each end-of-line seen;
3939 // Decrement for \r\n
3940 while (is && ((c1 = is.get_undelim ())
3941 != std::istream::traits_type::eof ())
3942 && (((c1 == m_eol1 || c1 == m_eol2) && ++m_lines)
3943 || isspace (c1) || is_delim (c1)))
3944 {
3945 if (prev == m_eol1 && m_eol1 != m_eol2 && c1 == m_eol2)
3946 m_lines--;
3947 prev = c1;
3948 }
3949 if (c1 != std::istream::traits_type::eof ())
3950 is.putback (c1);
3951 }
3952 }
3953 }
3954 else // multi-character delimiter
3955 {
3956 int first_match;
3957
3958 if (c1 == m_eol1 || c1 == m_eol2
3959 || (-1 != (first_match = lookahead (is, m_delim_list, m_delim_len))))
3960 {
3961 if (c1 == m_eol1)
3962 {
3963 is.get_undelim ();
3964 if (is.peek_undelim () == m_eol2)
3965 is.get_undelim ();
3966 }
3967 else if (c1 == m_eol2)
3968 {
3969 is.get_undelim ();
3970 }
3971
3973 {
3974 int prev = -1;
3975 // skip multiple delims.
3976 // Increment lines for each end-of-line seen;
3977 // decrement for \r\n.
3978 while (is && ((c1 = skip_whitespace (is))
3979 != std::istream::traits_type::eof ())
3980 && (((c1 == m_eol1 || c1 == m_eol2) && ++m_lines)
3981 || -1 != lookahead (is, m_delim_list, m_delim_len)))
3982 {
3983 if (prev == m_eol1 && m_eol1 != m_eol2 && c1 == m_eol2)
3984 m_lines--;
3985 prev = c1;
3986 }
3987 }
3988 }
3989 }
3990
3991 return c1;
3992 }
3993
3994 // Read in as much of the input as coincides with the literal in the
3995 // format string. Return "true" if the entire literal is matched, else
3996 // false (and set failbit).
3997
3998 bool
4000 const textscan_format_elt& fmt)
4001 {
4002 // "false" -> treat EOL as normal space
4003 // since a delimiter at the start of a line is a mismatch, not empty field
4004 skip_whitespace (is, false);
4005
4006 for (unsigned int i = 0; i < fmt.width; i++)
4007 {
4008 int ch = is.get_undelim ();
4009 if (ch != fmt.text[i])
4010 {
4011 if (ch != std::istream::traits_type::eof ())
4012 is.putback (ch);
4013 is.setstate (std::ios::failbit);
4014 return false;
4015 }
4016 }
4017 return true;
4018 }
4019
4020 void
4021 base_stream::error (const std::string& msg)
4022 {
4023 m_fail = true;
4024 m_errmsg = msg;
4025 }
4026
4027 void
4028 base_stream::error (const std::string& who, const std::string& msg)
4029 {
4030 m_fail = true;
4031 m_errmsg = who + ": " + msg;
4032 }
4033
4034 void
4036 {
4037 m_fail = false;
4038 m_errmsg = "";
4039 }
4040
4041 void
4043 {
4044 std::istream *is = input_stream ();
4045 std::ostream *os = output_stream ();
4046
4047 if (is)
4048 is->clear ();
4049
4050 if (os)
4051 os->clear ();
4052 }
4053
4054 // Functions that are defined for all input streams (input streams
4055 // are those that define is).
4056
4057 std::string
4059 bool strip_newline, const std::string& who)
4060 {
4061 interpreter& interp = __get_interpreter__ ("base_stream::do_gets");
4062
4063 if (interp.interactive () && file_number () == 0)
4064 ::error ("%s: unable to read from stdin while running interactively",
4065 who.c_str ());
4066
4067 std::string retval;
4068
4069 err = false;
4070
4071 std::istream *isp = input_stream ();
4072
4073 if (! isp)
4074 {
4075 err = true;
4076 invalid_operation (who, "reading");
4077 }
4078 else
4079 {
4080 std::istream& is = *isp;
4081
4082 std::ostringstream buf;
4083
4084 int c = 0;
4085 int char_count = 0;
4086
4087 if (max_len != 0)
4088 {
4089 while (is && (c = is.get ()) != std::istream::traits_type::eof ())
4090 {
4091 char_count++;
4092
4093 // Handle CRLF, CR, or LF as line ending.
4094 if (c == '\r')
4095 {
4096 if (! strip_newline)
4097 buf << static_cast<char> (c);
4098
4099 c = is.get ();
4100
4101 if (c != std::istream::traits_type::eof ())
4102 {
4103 if (c == '\n')
4104 {
4105 char_count++;
4106
4107 if (! strip_newline)
4108 buf << static_cast<char> (c);
4109 }
4110 else
4111 is.putback (c);
4112 }
4113
4114 break;
4115 }
4116 else if (c == '\n')
4117 {
4118 if (! strip_newline)
4119 buf << static_cast<char> (c);
4120
4121 break;
4122 }
4123 else
4124 buf << static_cast<char> (c);
4125
4126 if (max_len > 0 && char_count == max_len)
4127 break;
4128 }
4129 }
4130
4131 if (! is.eof () && char_count > 0)
4132 {
4133 // GAGME. Matlab seems to check for EOF even if the last character
4134 // in a file is a newline character. This is NOT what the
4135 // corresponding C-library functions do.
4136 int disgusting_compatibility_hack = is.get ();
4137 if (! is.eof ())
4138 is.putback (disgusting_compatibility_hack);
4139 }
4140
4141 if (is.good () || (is.eof () && char_count > 0))
4142 {
4143 retval = buf.str ();
4144 if (encoding ().compare ("utf-8"))
4145 retval = string::u8_from_encoding (who, retval, encoding ());
4146 }
4147 else
4148 {
4149 err = true;
4150
4151 if (is.eof () && char_count == 0)
4152 error (who, "at end of file");
4153 else
4154 error (who, "read error");
4155 }
4156 }
4157
4158 return retval;
4159 }
4160
4161 std::string
4163 const std::string& who)
4164 {
4165 return do_gets (max_len, err, true, who);
4166 }
4167
4168 std::string
4170 const std::string& who)
4171 {
4172 return do_gets (max_len, err, false, who);
4173 }
4174
4175 off_t
4176 base_stream::skipl (off_t num, bool& err, const std::string& who)
4177 {
4178 interpreter& interp = __get_interpreter__ ("base_stream::skipl");
4179
4180 if (interp.interactive () && file_number () == 0)
4181 ::error ("%s: unable to read from stdin while running interactively",
4182 who.c_str ());
4183
4184 off_t cnt = -1;
4185
4186 err = false;
4187
4188 std::istream *isp = input_stream ();
4189
4190 if (! isp)
4191 {
4192 err = true;
4193 invalid_operation (who, "reading");
4194 }
4195 else
4196 {
4197 std::istream& is = *isp;
4198
4199 int c = 0;
4200 int lastc = -1;
4201 cnt = 0;
4202
4203 while (is && (c = is.get ()) != std::istream::traits_type::eof ())
4204 {
4205 // Handle CRLF, CR, or LF as line ending.
4206 if (c == '\r' || (c == '\n' && lastc != '\r'))
4207 {
4208 if (++cnt == num)
4209 break;
4210 }
4211
4212 lastc = c;
4213 }
4214
4215 // Maybe eat the following \n if \r was just met.
4216 if (c == '\r' && is.peek () == '\n')
4217 is.get ();
4218
4219 if (is.bad ())
4220 {
4221 err = true;
4222 error (who, "read error");
4223 }
4224
4225 if (err)
4226 cnt = -1;
4227 }
4228
4229 return cnt;
4230 }
4231
4232 template <typename T>
4233 static std::istream&
4234 octave_scan_1 (std::istream& is, const scanf_format_elt& fmt,
4235 T *valptr)
4236 {
4237 T value = T ();
4238
4239 switch (fmt.type)
4240 {
4241 case 'o':
4242 is >> std::oct >> value >> std::dec;
4243 break;
4244
4245 case 'x':
4246 case 'X':
4247 is >> std::hex >> value >> std::dec;
4248 break;
4249
4250 case 'i':
4251 {
4252 int c1 = std::istream::traits_type::eof ();
4253
4254 while (is && (c1 = is.get ()) != std::istream::traits_type::eof ()
4255 && isspace (c1))
4256 ; // skip whitespace
4257
4258 if (c1 != std::istream::traits_type::eof ())
4259 {
4260 if (c1 == '0')
4261 {
4262 int c2 = is.peek ();
4263
4264 if (c2 == 'x' || c2 == 'X')
4265 {
4266 is.ignore ();
4267 if (std::isxdigit (is.peek ()))
4268 is >> std::hex >> value >> std::dec;
4269 else
4270 value = 0;
4271 }
4272 else
4273 {
4274 if (c2 == '0' || c2 == '1' || c2 == '2'
4275 || c2 == '3' || c2 == '4' || c2 == '5'
4276 || c2 == '6' || c2 == '7')
4277 is >> std::oct >> value >> std::dec;
4278 else if (c2 == '8' || c2 == '9')
4279 {
4280 // FIXME: Would like to set error state on octave
4281 // stream. See bug #46493. But only std::istream is
4282 // input to fcn.
4283 // error ("internal failure to match octal format");
4284 value = 0;
4285 }
4286 else
4287 value = 0;
4288 }
4289 }
4290 else
4291 {
4292 is.putback (c1);
4293
4294 is >> value;
4295 }
4296 }
4297 }
4298 break;
4299
4300 default:
4301 is >> value;
4302 break;
4303 }
4304
4305 // If conversion produces an integer that overflows, failbit is set but
4306 // value is non-zero. We want to treat this case as success, so clear
4307 // failbit from the stream state to keep going.
4308 // FIXME: Maybe set error state on octave stream as above? Matlab does
4309 // *not* indicate an error message on overflow.
4310 if ((is.rdstate () & std::ios::failbit) && value != T ())
4311 is.clear (is.rdstate () & ~std::ios::failbit);
4312
4313 // Only copy the converted value if the stream is in a state where we
4314 // want to continue reading.
4315 if (! (is.rdstate () & std::ios::failbit))
4316 *valptr = value;
4317
4318 return is;
4319 }
4320
4321 template <typename T>
4322 static std::istream&
4323 octave_scan (std::istream& is, const scanf_format_elt& fmt, T *valptr)
4324 {
4325 if (fmt.width)
4326 {
4327 // Limit input to fmt.width characters by reading into a
4328 // temporary stringstream buffer.
4329 std::string strbuf;
4330
4331 auto orig_pos = is.tellg ();
4332
4333 is.width (fmt.width);
4334 is >> strbuf;
4335
4336 std::istringstream ss (strbuf);
4337
4338 octave_scan_1 (ss, fmt, valptr);
4339
4340 if (! ss.eof ())
4341 {
4342 // If fewer characters than width were used to read a number then
4343 // the original istream object positioning is incorrect.
4344 // Rather than attempt to update istream state and positioning,
4345 // just redo the '>>' operation with the correct width so that
4346 // all flags get set correctly.
4347
4348 is.clear (); // Clear EOF, FAILBIT, BADBIT
4349 is.seekg (orig_pos, is.beg);
4350
4351 int chars_read = ss.tellg ();
4352 if (chars_read > 0)
4353 {
4354 is.width (chars_read);
4355 is >> strbuf;
4356 }
4357 }
4358
4359 // If pattern failed to match then propagate fail bit to 'is' stream.
4360 if (ss.fail ())
4361 is.setstate (std::ios::failbit);
4362
4363 }
4364 else
4365 octave_scan_1 (is, fmt, valptr);
4366
4367 return is;
4368 }
4369
4370 template <>
4371 std::istream&
4372 octave_scan<> (std::istream& is, const scanf_format_elt& fmt, double *valptr)
4373 {
4374 double& ref = *valptr;
4375
4376 switch (fmt.type)
4377 {
4378 case 'e':
4379 case 'f':
4380 case 'g':
4381 case 'E':
4382 case 'G':
4383 {
4384 int c1 = std::istream::traits_type::eof ();
4385
4386 while (is && (c1 = is.get ()) != std::istream::traits_type::eof ()
4387 && isspace (c1))
4388 ; // skip whitespace
4389
4390 if (c1 != std::istream::traits_type::eof ())
4391 {
4392 is.putback (c1);
4393
4394 ref = read_value<double> (is);
4395 }
4396 }
4397 break;
4398
4399 default:
4401 break;
4402 }
4403
4404 return is;
4405 }
4406
4407 template <typename T>
4408 static void
4409 do_scanf_conv (std::istream& is, const scanf_format_elt& fmt,
4410 T valptr, Matrix& mval, double *data, octave_idx_type& idx,
4411 octave_idx_type& conversion_count, octave_idx_type nr,
4412 octave_idx_type max_size, bool discard)
4413 {
4414 octave_scan (is, fmt, valptr);
4415
4416 if (! is)
4417 return;
4418
4419 if (idx == max_size && ! discard)
4420 {
4421 max_size *= 2;
4422
4423 if (nr > 0)
4424 mval.resize (nr, max_size / nr, 0.0);
4425 else
4426 mval.resize (max_size, 1, 0.0);
4427
4428 data = mval.fortran_vec ();
4429 }
4430
4431 if (! discard)
4432 {
4433 conversion_count++;
4434 data[idx++] = *(valptr);
4435 }
4436 }
4437
4438 template void
4439 do_scanf_conv (std::istream&, const scanf_format_elt&, double *,
4440 Matrix&, double *, octave_idx_type&, octave_idx_type&,
4442
4443#define DO_WHITESPACE_CONVERSION() \
4444 do \
4445 { \
4446 int c = std::istream::traits_type::eof (); \
4447 \
4448 while (is && (c = is.get ()) != std::istream::traits_type::eof () \
4449 && isspace (c)) \
4450 { /* skip whitespace */ } \
4451 \
4452 if (c != std::istream::traits_type::eof ()) \
4453 is.putback (c); \
4454 } \
4455 while (0)
4456
4457#define DO_LITERAL_CONVERSION() \
4458 do \
4459 { \
4460 int c = std::istream::traits_type::eof (); \
4461 \
4462 int n = fmt.length (); \
4463 int i = 0; \
4464 \
4465 while (i < n && is \
4466 && (c = is.get ()) != std::istream::traits_type::eof ()) \
4467 { \
4468 if (c == static_cast<unsigned char> (fmt[i])) \
4469 { \
4470 i++; \
4471 continue; \
4472 } \
4473 else \
4474 { \
4475 is.putback (c); \
4476 break; \
4477 } \
4478 } \
4479 \
4480 if (i != n) \
4481 is.setstate (std::ios::failbit); \
4482 } \
4483 while (0)
4484
4485#define DO_PCT_CONVERSION() \
4486 do \
4487 { \
4488 int c = is.get (); \
4489 \
4490 if (c != std::istream::traits_type::eof ()) \
4491 { \
4492 if (c != '%') \
4493 { \
4494 is.putback (c); \
4495 is.setstate (std::ios::failbit); \
4496 } \
4497 } \
4498 else \
4499 is.setstate (std::ios::failbit); \
4500 } \
4501 while (0)
4502
4503#define BEGIN_C_CONVERSION() \
4504 is.unsetf (std::ios::skipws); \
4505 \
4506 int width = (elt->width ? elt->width : 1); \
4507 \
4508 std::string tmp (width, '\0'); \
4509 \
4510 int c = std::istream::traits_type::eof (); \
4511 int n = 0; \
4512 \
4513 while (is && n < width \
4514 && (c = is.get ()) != std::istream::traits_type::eof ()) \
4515 tmp[n++] = static_cast<char> (c); \
4516 \
4517 if (n > 0 && c == std::istream::traits_type::eof ()) \
4518 is.clear (); \
4519 \
4520 tmp.resize (n)
4521
4522 // For a '%s' format, skip initial whitespace and then read until the
4523 // next whitespace character or until WIDTH characters have been read.
4524#define BEGIN_S_CONVERSION() \
4525 int width = elt->width; \
4526 \
4527 std::string tmp; \
4528 \
4529 do \
4530 { \
4531 if (width) \
4532 { \
4533 tmp = std::string (width, '\0'); \
4534 \
4535 int c = std::istream::traits_type::eof (); \
4536 \
4537 int n = 0; \
4538 \
4539 while (is && (c = is.get ()) != std::istream::traits_type::eof ()) \
4540 { \
4541 if (! isspace (c)) \
4542 { \
4543 tmp[n++] = static_cast<char> (c); \
4544 break; \
4545 } \
4546 } \
4547 \
4548 while (is && n < width \
4549 && (c = is.get ()) != std::istream::traits_type::eof ()) \
4550 { \
4551 if (isspace (c)) \
4552 { \
4553 is.putback (c); \
4554 break; \
4555 } \
4556 else \
4557 tmp[n++] = static_cast<char> (c); \
4558 } \
4559 \
4560 if (n > 0 && c == std::istream::traits_type::eof ()) \
4561 is.clear (); \
4562 \
4563 tmp.resize (n); \
4564 } \
4565 else \
4566 { \
4567 is >> std::ws >> tmp; \
4568 } \
4569 } \
4570 while (0)
4571
4572 // This format must match a nonempty sequence of characters.
4573#define BEGIN_CHAR_CLASS_CONVERSION() \
4574 int width = elt->width; \
4575 \
4576 std::string tmp; \
4577 \
4578 do \
4579 { \
4580 if (! width) \
4581 width = std::numeric_limits<int>::max (); \
4582 \
4583 std::ostringstream buf; \
4584 \
4585 std::string char_class = elt->char_class; \
4586 \
4587 int c = std::istream::traits_type::eof (); \
4588 \
4589 if (elt->type == '[') \
4590 { \
4591 int chars_read = 0; \
4592 while (is && chars_read++ < width \
4593 && (c = is.get ()) != std::istream::traits_type::eof () \
4594 && char_class.find (c) != std::string::npos) \
4595 buf << static_cast<char> (c); \
4596 } \
4597 else \
4598 { \
4599 int chars_read = 0; \
4600 while (is && chars_read++ < width \
4601 && (c = is.get ()) != std::istream::traits_type::eof () \
4602 && char_class.find (c) == std::string::npos) \
4603 buf << static_cast<char> (c); \
4604 } \
4605 \
4606 if (width == std::numeric_limits<int>::max () \
4607 && c != std::istream::traits_type::eof ()) \
4608 is.putback (c); \
4609 \
4610 tmp = buf.str (); \
4611 \
4612 if (tmp.empty ()) \
4613 is.setstate (std::ios::failbit); \
4614 else if (c == std::istream::traits_type::eof ()) \
4615 is.clear (); \
4616 \
4617 } \
4618 while (0)
4619
4620#define FINISH_CHARACTER_CONVERSION() \
4621 do \
4622 { \
4623 if (encoding ().compare ("utf-8")) \
4624 tmp = string::u8_from_encoding (who, tmp, encoding ()); \
4625 width = tmp.length (); \
4626 \
4627 if (is) \
4628 { \
4629 int i = 0; \
4630 \
4631 if (! discard) \
4632 { \
4633 conversion_count++; \
4634 \
4635 while (i < width) \
4636 { \
4637 if (data_index == max_size) \
4638 { \
4639 max_size *= 2; \
4640 \
4641 if (all_char_conv) \
4642 { \
4643 if (one_elt_size_spec) \
4644 mval.resize (1, max_size, 0.0); \
4645 else if (nr > 0) \
4646 mval.resize (nr, max_size / nr, 0.0); \
4647 else \
4648 panic_impossible (); \
4649 } \
4650 else if (nr > 0) \
4651 mval.resize (nr, max_size / nr, 0.0); \
4652 else \
4653 mval.resize (max_size, 1, 0.0); \
4654 \
4655 data = mval.fortran_vec (); \
4656 } \
4657 \
4658 data[data_index++] = static_cast<unsigned char> \
4659 (tmp[i++]); \
4660 } \
4661 } \
4662 } \
4663 } \
4664 while (0)
4665
4669 bool one_elt_size_spec,
4670 octave_idx_type& conversion_count,
4671 const std::string& who)
4672 {
4673 interpreter& interp = __get_interpreter__ ("base_stream::do_scanf");
4674
4675 if (interp.interactive () && file_number () == 0)
4676 ::error ("%s: unable to read from stdin while running interactively",
4677 who.c_str ());
4678
4679 octave_value retval = Matrix ();
4680
4681 conversion_count = 0;
4682
4683 octave_idx_type m_nconv = fmt_list.num_conversions ();
4684
4685 octave_idx_type data_index = 0;
4686
4687 if (nr == 0 || nc == 0)
4688 {
4689 if (one_elt_size_spec)
4690 nc = 0;
4691
4692 return Matrix (nr, nc, 0.0);
4693 }
4694
4695 std::istream *isp = input_stream ();
4696
4697 bool all_char_conv = fmt_list.all_character_conversions ();
4698
4699 Matrix mval;
4700 octave_idx_type max_size = 0;
4701 octave_idx_type max_conv = 0;
4702
4703 octave_idx_type final_nr = 0;
4704 octave_idx_type final_nc = 0;
4705
4706 if (all_char_conv)
4707 {
4708 // Any of these could be resized later (if we have %s conversions,
4709 // we may read more than one element for each conversion).
4710 if (one_elt_size_spec)
4711 {
4712 max_size = 512;
4713 mval.resize (1, max_size, 0.0);
4714
4715 if (nr > 0)
4716 max_conv = nr;
4717 }
4718 else if (nr > 0)
4719 {
4720 if (nc > 0)
4721 {
4722 mval.resize (nr, nc, 0.0);
4723 max_size = max_conv = nr * nc;
4724 }
4725 else
4726 {
4727 mval.resize (nr, 32, 0.0);
4728 max_size = nr * 32;
4729 }
4730 }
4731 else
4733 }
4734 else if (nr > 0)
4735 {
4736 if (nc > 0)
4737 {
4738 // Will not resize later.
4739 mval.resize (nr, nc, 0.0);
4740 max_size = nr * nc;
4741 max_conv = max_size;
4742 }
4743 else
4744 {
4745 // Maybe resize later.
4746 mval.resize (nr, 32, 0.0);
4747 max_size = nr * 32;
4748 }
4749 }
4750 else
4751 {
4752 // Maybe resize later.
4753 mval.resize (32, 1, 0.0);
4754 max_size = 32;
4755 }
4756
4757 double *data = mval.fortran_vec ();
4758
4759 if (isp)
4760 {
4761 std::istream& is = *isp;
4762
4763 const scanf_format_elt *elt = fmt_list.first ();
4764
4765 std::ios::fmtflags flags = is.flags ();
4766
4767 octave_idx_type trips = 0;
4768
4769 octave_idx_type num_fmt_elts = fmt_list.length ();
4770
4771 for (;;)
4772 {
4773 octave_quit ();
4774
4775 if (elt)
4776 {
4777 if (elt->type == scanf_format_elt::null
4780 || elt->type == '%')
4781 && max_conv > 0 && conversion_count == max_conv))
4782 {
4783 // We are done, either because we have reached the end of
4784 // the format string and are not cycling through the format
4785 // again or because we've converted all the values that
4786 // have been requested and the next format element is a
4787 // conversion. Determine final array size and exit.
4788 if (all_char_conv && one_elt_size_spec)
4789 {
4790 final_nr = 1;
4791 final_nc = data_index;
4792 }
4793 else
4794 {
4795 final_nr = nr;
4796 final_nc = (data_index - 1) / nr + 1;
4797 }
4798
4799 break;
4800 }
4801 else if (data_index == max_size)
4802 {
4803 max_size *= 2;
4804
4805 if (all_char_conv)
4806 {
4807 if (one_elt_size_spec)
4808 mval.resize (1, max_size, 0.0);
4809 else if (nr > 0)
4810 mval.resize (nr, max_size / nr, 0.0);
4811 else
4813 }
4814 else if (nr > 0)
4815 mval.resize (nr, max_size / nr, 0.0);
4816 else
4817 mval.resize (max_size, 1, 0.0);
4818
4819 data = mval.fortran_vec ();
4820 }
4821
4822 std::string fmt = elt->text;
4823
4824 bool discard = elt->discard;
4825
4826 switch (elt->type)
4827 {
4830 break;
4831
4834 break;
4835
4836 case '%':
4838 break;
4839
4840 case 'd': case 'i':
4841 {
4842 switch (elt->modifier)
4843 {
4844 case 'h':
4845 {
4846 int16_t tmp;
4847 do_scanf_conv (is, *elt, &tmp, mval, data,
4848 data_index, conversion_count,
4849 nr, max_size, discard);
4850 }
4851 break;
4852
4853 case 'l':
4854 {
4855 int64_t tmp;
4856 do_scanf_conv (is, *elt, &tmp, mval, data,
4857 data_index, conversion_count,
4858 nr, max_size, discard);
4859 }
4860 break;
4861
4862 default:
4863 {
4864 int32_t tmp;
4865 do_scanf_conv (is, *elt, &tmp, mval, data,
4866 data_index, conversion_count,
4867 nr, max_size, discard);
4868 }
4869 break;
4870 }
4871 }
4872 break;
4873
4874 case 'o': case 'u': case 'x': case 'X':
4875 {
4876 switch (elt->modifier)
4877 {
4878 case 'h':
4879 {
4880 uint16_t tmp;
4881 do_scanf_conv (is, *elt, &tmp, mval, data,
4882 data_index, conversion_count,
4883 nr, max_size, discard);
4884 }
4885 break;
4886
4887 case 'l':
4888 {
4889 uint64_t tmp;
4890 do_scanf_conv (is, *elt, &tmp, mval, data,
4891 data_index, conversion_count,
4892 nr, max_size, discard);
4893 }
4894 break;
4895
4896 default:
4897 {
4898 uint32_t tmp;
4899 do_scanf_conv (is, *elt, &tmp, mval, data,
4900 data_index, conversion_count,
4901 nr, max_size, discard);
4902 }
4903 break;
4904 }
4905 }
4906 break;
4907
4908 case 'e': case 'f': case 'g':
4909 case 'E': case 'G':
4910 {
4911 double tmp;
4912
4913 do_scanf_conv (is, *elt, &tmp, mval, data,
4914 data_index, conversion_count,
4915 nr, max_size, discard);
4916 }
4917 break;
4918
4919 case 'c':
4920 {
4922
4924
4925 is.setf (flags);
4926 }
4927 break;
4928
4929 case 's':
4930 {
4932
4934 }
4935 break;
4936
4937 case '[': case '^':
4938 {
4940
4942 }
4943 break;
4944
4945 case 'p':
4946 error (who, "unsupported format specifier");
4947 break;
4948
4949 default:
4950 error (who, "internal format error");
4951 break;
4952 }
4953
4954 if (! ok ())
4955 {
4956 break;
4957 }
4958 else if (is.eof () || ! is)
4959 {
4960 if (all_char_conv)
4961 {
4962 if (one_elt_size_spec)
4963 {
4964 final_nr = 1;
4965 final_nc = data_index;
4966 }
4967 else if (data_index > nr)
4968 {
4969 final_nr = nr;
4970 final_nc = (data_index - 1) / nr + 1;
4971 }
4972 else
4973 {
4974 final_nr = data_index;
4975 final_nc = 1;
4976 }
4977 }
4978 else if (nr > 0)
4979 {
4980 if (data_index > nr)
4981 {
4982 final_nr = nr;
4983 final_nc = (data_index - 1) / nr + 1;
4984 }
4985 else
4986 {
4987 final_nr = data_index;
4988 final_nc = 1;
4989 }
4990 }
4991 else
4992 {
4993 final_nr = data_index;
4994 final_nc = 1;
4995 }
4996
4997 // If it looks like we have a matching failure, then
4998 // reset the failbit in the stream state.
4999 if (is.rdstate () & std::ios::failbit)
5000 {
5001 error (who, "format failed to match");
5002 is.clear (is.rdstate () & (~std::ios::failbit));
5003 }
5004
5005 // FIXME: is this the right thing to do?
5006 if (interp.interactive ()
5008 && name () == "stdin")
5009 {
5010 is.clear ();
5011
5012 // Skip to end of line.
5013 bool err;
5014 do_gets (-1, err, false, who);
5015 }
5016
5017 break;
5018 }
5019 }
5020 else
5021 {
5022 error (who, "internal format error");
5023 break;
5024 }
5025
5026 if (m_nconv == 0 && ++trips == num_fmt_elts)
5027 {
5028 if (all_char_conv && one_elt_size_spec)
5029 {
5030 final_nr = 1;
5031 final_nc = data_index;
5032 }
5033 else
5034 {
5035 final_nr = nr;
5036 final_nc = (data_index - 1) / nr + 1;
5037 }
5038
5039 break;
5040 }
5041 else
5042 {
5043 // Cycle through the format list more than once if we have some
5044 // conversions to make and we haven't reached the limit on the
5045 // number of values to convert (possibly because there is no
5046 // specified limit).
5047 elt = fmt_list.next (m_nconv > 0
5048 && (max_conv == 0
5049 || conversion_count < max_conv));
5050 }
5051 }
5052 }
5053
5054 mval.resize (final_nr, final_nc, 0.0);
5055
5056 retval = mval;
5057
5058 if (all_char_conv)
5059 retval = retval.convert_to_str (false, true);
5060
5061 return retval;
5062 }
5063
5065 base_stream::scanf (const std::string& fmt, const Array<double>& size,
5066 octave_idx_type& conversion_count,
5067 const std::string& who)
5068 {
5069 octave_value retval = Matrix ();
5070
5071 conversion_count = 0;
5072
5073 std::istream *isp = input_stream ();
5074
5075 if (! isp)
5076 invalid_operation (who, "reading");
5077 else
5078 {
5079 scanf_format_list fmt_list (fmt);
5080
5081 if (fmt_list.num_conversions () == -1)
5082 ::error ("%s: invalid format specified", who.c_str ());
5083
5084 octave_idx_type nr = -1;
5085 octave_idx_type nc = -1;
5086
5087 bool one_elt_size_spec;
5088
5089 get_size (size, nr, nc, one_elt_size_spec, who);
5090
5091 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec,
5092 conversion_count, who);
5093 }
5094
5095 return retval;
5096 }
5097
5098 bool
5100 octave_value& retval, const std::string& who)
5101 {
5102 std::istream *isp = input_stream ();
5103
5104 if (! isp)
5105 return false;
5106
5107 bool quit = false;
5108
5109 std::istream& is = *isp;
5110
5111 std::ios::fmtflags flags = is.flags ();
5112
5113 if (elt)
5114 {
5115 std::string fmt = elt->text;
5116
5117 bool discard = elt->discard;
5118
5119 switch (elt->type)
5120 {
5123 break;
5124
5127 break;
5128
5129 case '%':
5130 {
5132
5133 if (! is)
5134 quit = true;
5135 }
5136 break;
5137
5138 case 'd': case 'i':
5139 {
5140 switch (elt->modifier)
5141 {
5142 case 'h':
5143 {
5144 int16_t tmp;
5145 if (octave_scan (is, *elt, &tmp))
5146 {
5147 if (! discard)
5148 retval = tmp;
5149 }
5150 else
5151 quit = true;
5152 }
5153 break;
5154
5155 case 'l':
5156 {
5157 int64_t tmp;
5158 if (octave_scan (is, *elt, &tmp))
5159 {
5160 if (! discard)
5161 retval = tmp;
5162 }
5163 else
5164 quit = true;
5165 }
5166 break;
5167
5168 default:
5169 {
5170 int32_t tmp;
5171 if (octave_scan (is, *elt, &tmp))
5172 {
5173 if (! discard)
5174 retval = tmp;
5175 }
5176 else
5177 quit = true;
5178 }
5179 break;
5180 }
5181 }
5182 break;
5183
5184 case 'o': case 'u': case 'x': case 'X':
5185 {
5186 switch (elt->modifier)
5187 {
5188 case 'h':
5189 {
5190 uint16_t tmp;
5191 if (octave_scan (is, *elt, &tmp))
5192 {
5193 if (! discard)
5194 retval = tmp;
5195 }
5196 else
5197 quit = true;
5198 }
5199 break;
5200
5201 case 'l':
5202 {
5203 uint64_t tmp;
5204 if (octave_scan (is, *elt, &tmp))
5205 {
5206 if (! discard)
5207 retval = tmp;
5208 }
5209 else
5210 quit = true;
5211 }
5212 break;
5213
5214 default:
5215 {
5216 uint32_t tmp;
5217 if (octave_scan (is, *elt, &tmp))
5218 {
5219 if (! discard)
5220 retval = tmp;
5221 }
5222 else
5223 quit = true;
5224 }
5225 break;
5226 }
5227 }
5228 break;
5229
5230 case 'e': case 'f': case 'g':
5231 case 'E': case 'G':
5232 {
5233 double tmp;
5234
5235 if (octave_scan (is, *elt, &tmp))
5236 {
5237 if (! discard)
5238 retval = tmp;
5239 }
5240 else
5241 quit = true;
5242 }
5243 break;
5244
5245 case 'c':
5246 {
5248
5249 if (! discard)
5250 retval = tmp;
5251
5252 if (! is)
5253 quit = true;
5254
5255 is.setf (flags);
5256 }
5257 break;
5258
5259 case 's':
5260 {
5262
5263 if (! discard)
5264 retval = tmp;
5265
5266 if (! is)
5267 quit = true;
5268 }
5269 break;
5270
5271 case '[':
5272 case '^':
5273 {
5275
5276 if (! discard)
5277 retval = tmp;
5278
5279 if (! is)
5280 quit = true;
5281 }
5282 break;
5283
5284 case 'p':
5285 error (who, "unsupported format specifier");
5286 break;
5287
5288 default:
5289 error (who, "internal format error");
5290 break;
5291 }
5292 }
5293
5294 if (ok () && is.fail ())
5295 {
5296 error ("%s: read error", who.c_str ());
5297
5298 // FIXME: is this the right thing to do?
5299
5300 interpreter& interp = __get_interpreter__ ("base_stream::do_oscanf");
5301
5302 if (interp.interactive () && ! application::forced_interactive ()
5303 && name () == "stdin")
5304 {
5305 // Skip to end of line.
5306 bool err;
5307 do_gets (-1, err, false, who);
5308 }
5309 }
5310
5311 return quit;
5312 }
5313
5315 base_stream::oscanf (const std::string& fmt, const std::string& who)
5316 {
5317 octave_value_list retval;
5318
5319 std::istream *isp = input_stream ();
5320
5321 if (! isp)
5322 invalid_operation (who, "reading");
5323 else
5324 {
5325 std::istream& is = *isp;
5326
5327 scanf_format_list fmt_list (fmt);
5328
5329 octave_idx_type m_nconv = fmt_list.num_conversions ();
5330
5331 if (m_nconv == -1)
5332 ::error ("%s: invalid format specified", who.c_str ());
5333
5334 is.clear ();
5335
5336 octave_idx_type len = fmt_list.length ();
5337
5338 retval.resize (m_nconv+2, Matrix ());
5339
5340 const scanf_format_elt *elt = fmt_list.first ();
5341
5342 int num_values = 0;
5343
5344 bool quit = false;
5345
5346 for (octave_idx_type i = 0; i < len; i++)
5347 {
5348 octave_value tmp;
5349
5350 quit = do_oscanf (elt, tmp, who);
5351
5352 if (quit)
5353 break;
5354 else
5355 {
5356 if (tmp.is_defined ())
5357 retval(num_values++) = tmp;
5358
5359 if (! ok ())
5360 break;
5361
5362 elt = fmt_list.next (m_nconv > 0);
5363 }
5364 }
5365
5366 retval(m_nconv) = num_values;
5367
5368 int err_num;
5369 retval(m_nconv+1) = error (false, err_num);
5370
5371 if (! quit)
5372 {
5373 // Pick up any trailing stuff.
5374 if (ok () && len > m_nconv)
5375 {
5376 octave_value tmp;
5377
5378 elt = fmt_list.next ();
5379
5380 do_oscanf (elt, tmp, who);
5381 }
5382 }
5383 }
5384
5385 return retval;
5386 }
5387
5389 base_stream::do_textscan (const std::string& fmt,
5390 octave_idx_type ntimes,
5391 const octave_value_list& options,
5392 const std::string& who,
5393 octave_idx_type& read_count)
5394 {
5395 interpreter& interp = __get_interpreter__ ("base_stream::do_textscan");
5396
5397 if (interp.interactive () && file_number () == 0)
5398 ::error ("%s: unable to read from stdin while running interactively",
5399 who.c_str ());
5400
5401 octave_value retval = Cell (dim_vector (1, 1), Matrix (0, 1));
5402
5403 std::istream *isp = input_stream ();
5404
5405 if (! isp)
5406 invalid_operation (who, "reading");
5407 else
5408 {
5409 textscan scanner (who, encoding ());
5410
5411 retval = scanner.scan (*isp, fmt, ntimes, options, read_count);
5412 }
5413
5414 return retval;
5415 }
5416
5417 // Functions that are defined for all output streams
5418 // (output streams are those that define os).
5419
5420 int
5422 {
5423 int retval = -1;
5424
5425 std::ostream *os = output_stream ();
5426
5427 if (! os)
5428 invalid_operation ("fflush", "writing");
5429 else
5430 {
5431 os->flush ();
5432
5433 if (os->good ())
5434 retval = 0;
5435 }
5436
5437 return retval;
5438 }
5439
5440 class
5442 {
5443 public:
5444
5445 enum state { ok, conversion_error };
5446
5447 printf_value_cache (const octave_value_list& args, const std::string& who)
5448 : m_values (args), m_val_idx (0), m_elt_idx (0),
5449 m_n_vals (m_values.length ()), m_n_elts (0), m_have_data (false),
5450 m_curr_state (ok)
5451 {
5452 for (octave_idx_type i = 0; i < m_values.length (); i++)
5453 {
5454 octave_value val = m_values(i);
5455
5456 if (val.isstruct () || val.iscell () || val.isobject ())
5457 err_wrong_type_arg (who, val);
5458 }
5459 }
5460
5461 // No copying!
5462
5464
5465 printf_value_cache& operator = (const printf_value_cache&) = delete;
5466
5467 ~printf_value_cache (void) = default;
5468
5469 // Get the current value as a double and advance the internal pointer.
5470 octave_value get_next_value (char type = 0);
5471
5472 // Get the current value as an int and advance the internal
5473 // pointer. Value before conversion to int must be >= 0 and less
5474 // than std::numeric_limits<int>::max ().
5475
5476 int int_value (void);
5477
5478 operator bool () const { return (m_curr_state == ok); }
5479
5480 bool exhausted (void) { return (m_val_idx >= m_n_vals); }
5481
5482 private:
5483
5484 // Must create value cache with values!
5486
5487 //--------
5488
5497 };
5498
5501 {
5502 octave_value retval;
5503
5504 if (exhausted ())
5506
5507 while (! exhausted ())
5508 {
5509 if (! m_have_data)
5510 {
5512
5513 m_elt_idx = 0;
5515 m_have_data = true;
5516 }
5517
5518 if (m_elt_idx < m_n_elts)
5519 {
5520 if (type == 's')
5521 {
5522 if (m_curr_val.is_string ())
5523 {
5524 dim_vector dv (1, m_curr_val.numel ());
5525 octave_value tmp = m_curr_val.reshape (dv);
5526
5527 std::string sval = tmp.string_value ();
5528
5529 retval = sval.substr (m_elt_idx);
5530
5531 // We've consumed the rest of the value.
5533 }
5534 else
5535 {
5536 // Convert to character string while values are
5537 // integers in the range [0 : char max]
5538 const NDArray val = m_curr_val.array_value ();
5539
5541
5542 for (; idx < m_n_elts; idx++)
5543 {
5544 double dval = val(idx);
5545
5546 if (math::x_nint (dval) != dval
5547 || dval < 0 || dval > 255)
5548 break;
5549 }
5550
5551 octave_idx_type n = idx - m_elt_idx;
5552
5553 if (n > 0)
5554 {
5555 std::string sval (n, '\0');
5556
5557 for (octave_idx_type i = 0; i < n; i++)
5558 sval[i] = val(m_elt_idx++);
5559
5560 retval = sval;
5561 }
5562 else
5564 }
5565 }
5566 else
5567 {
5569
5570 if (type == 'c' && ! retval.is_string ())
5571 {
5572 double dval = retval.double_value ();
5573
5574 if (math::x_nint (dval) == dval && dval >= 0 && dval < 256)
5575 retval = static_cast<char> (dval);
5576 }
5577 }
5578
5579 if (m_elt_idx >= m_n_elts)
5580 {
5581 m_elt_idx = 0;
5582 m_val_idx++;
5583 m_have_data = false;
5584 }
5585
5586 break;
5587 }
5588 else
5589 {
5590 m_val_idx++;
5591 m_have_data = false;
5592
5593 if (m_n_elts == 0)
5594 {
5595 if (m_elt_idx == 0)
5596 {
5597 if (type == 's' || type == 'c')
5598 retval = "";
5599 else
5600 retval = Matrix ();
5601
5602 break;
5603 }
5604
5605 if (exhausted ())
5607 }
5608 }
5609 }
5610
5611 return retval;
5612 }
5613
5614 int
5616 {
5618
5619 double dval = val.double_value (true);
5620
5621 if (dval < 0 || dval > std::numeric_limits<int>::max ()
5622 || math::x_nint (dval) != dval)
5623 {
5625 return -1;
5626 }
5627
5628 return math::nint (dval);
5629 }
5630
5631 // Ugh again and again.
5632
5633 template <typename T>
5634 static int
5635 do_printf_conv (std::ostream& os, const std::string& encoding,
5636 const char *fmt, int nsa, int sa_1, int sa_2, T arg,
5637 const std::string& who)
5638 {
5639 int retval = 0;
5640
5641 switch (nsa)
5642 {
5643 case 2:
5644 retval = format (os, encoding, fmt, sa_1, sa_2, arg);
5645 break;
5646
5647 case 1:
5648 retval = format (os, encoding, fmt, sa_1, arg);
5649 break;
5650
5651 case 0:
5652 retval = format (os, encoding, fmt, arg);
5653 break;
5654
5655 default:
5656 ::error ("%s: internal error handling format", who.c_str ());
5657 break;
5658 }
5659
5660 return retval;
5661 }
5662
5663 static std::size_t
5664 do_printf_string (std::ostream& os, const printf_format_elt *elt,
5665 int nsa, int sa_1, int sa_2, const std::string& arg,
5666 const std::string& encoding, const std::string& who)
5667 {
5668 if (nsa > 2)
5669 ::error ("%s: internal error handling format", who.c_str ());
5670
5671 std::string flags = elt->flags;
5672
5673 bool left = flags.find ('-') != std::string::npos;
5674
5675 std::size_t len = arg.length ();
5676
5677 std::size_t prec = (nsa > 1 ? sa_2 : (elt->prec == -1 ? len : elt->prec));
5678
5679 std::string print_str = prec < arg.length () ? arg.substr (0, prec) : arg;
5680 if (encoding.compare ("utf-8"))
5681 {
5682 std::size_t src_len = print_str.length ();
5683 print_str = string::u8_to_encoding (who, print_str, encoding);
5684 len -= src_len - print_str.length ();
5685 }
5686
5687 std::size_t fw = (nsa > 0 ? sa_1 : (elt->fw == -1 ? len : elt->fw));
5688
5689 os << std::setw (fw) << (left ? std::left : std::right) << print_str;
5690
5691 return len > fw ? len : fw;
5692 }
5693
5694 static bool
5696 {
5697 octave_value ov_isnan = val.isnan ();
5698 octave_value ov_isinf = val.isinf ();
5699
5700 return (ov_isnan.is_true () || ov_isinf.is_true ());
5701 }
5702
5703 static bool
5705 {
5706 uint64_t limit = std::numeric_limits<int64_t>::max ();
5707
5708 if (val.is_string ())
5709 return true;
5710 else if (val.isinteger ())
5711 {
5712 if (val.is_uint64_type ())
5713 {
5714 octave_uint64 ival = val.uint64_scalar_value ();
5715
5716 if (ival.value () <= limit)
5717 return true;
5718 }
5719 else
5720 return true;
5721 }
5722 else
5723 {
5724 double dval = val.double_value (true);
5725
5726 if (dval == math::fix (dval) && dval <= limit)
5727 return true;
5728 }
5729
5730 return false;
5731 }
5732
5733 static bool
5735 {
5736 if (val.is_string ())
5737 return true;
5738 else if (val.isinteger ())
5739 {
5740 // Easier than dispatching here...
5741
5742 octave_value ov_is_ge_zero
5744
5745 return ov_is_ge_zero.is_true ();
5746 }
5747 else
5748 {
5749 double dval = val.double_value (true);
5750
5751 uint64_t limit = std::numeric_limits<uint64_t>::max ();
5752
5753 if (dval == math::fix (dval) && dval >= 0 && dval <= limit)
5754 return true;
5755 }
5756
5757 return false;
5758 }
5759
5760 static std::string
5762 {
5763 std::string tfmt = elt->text;
5764
5765 tfmt.replace (tfmt.rfind (elt->type), 1, "g");
5766
5767 return tfmt;
5768 }
5769
5770 int
5772 const printf_format_elt *elt,
5773 int nsa, int sa_1, int sa_2,
5774 const octave_value& val,
5775 const std::string& who)
5776 {
5777 int retval = 0;
5778
5779 std::string tfmt = elt->text;
5780
5781 if (is_nan_or_inf (val))
5782 {
5783 double dval = val.double_value ();
5784
5785 std::string::size_type i1, i2;
5786
5787 tfmt.replace ((i1 = tfmt.rfind (elt->type)), 1, 1, 's');
5788
5789 if ((i2 = tfmt.rfind ('.')) != std::string::npos && i2 < i1)
5790 {
5791 tfmt.erase (i2, i1-i2);
5792 if (elt->prec == -2)
5793 nsa--;
5794 }
5795
5796 const char *tval;
5797 if (lo_ieee_isinf (dval))
5798 {
5799 if (elt->flags.find ('+') != std::string::npos)
5800 tval = (dval < 0 ? "-Inf" : "+Inf");
5801 else
5802 tval = (dval < 0 ? "-Inf" : "Inf");
5803 }
5804 else
5805 {
5806 if (elt->flags.find ('+') != std::string::npos)
5807 tval = (lo_ieee_is_NA (dval) ? "+NA" : "+NaN");
5808 else
5809 tval = (lo_ieee_is_NA (dval) ? "NA" : "NaN");
5810 }
5811
5812 retval += do_printf_conv (os, encoding (), tfmt.c_str (), nsa, sa_1,
5813 sa_2, tval, who);
5814 }
5815 else
5816 {
5817 static std::string llmod
5818 = (sizeof (long) == sizeof (int64_t) ? "l" : "ll");
5819
5820 char type = elt->type;
5821
5822 switch (type)
5823 {
5824 case 'd': case 'i': case 'c':
5825 if (ok_for_signed_int_conv (val))
5826 {
5827 octave_int64 tval = val.int64_scalar_value ();
5828
5829 // Insert "long" modifier.
5830 tfmt.replace (tfmt.rfind (type), 1, llmod + type);
5831
5832 retval += do_printf_conv (os, encoding (), tfmt.c_str (), nsa,
5833 sa_1, sa_2, tval.value (), who);
5834 }
5835 else
5836 {
5837 tfmt = switch_to_g_format (elt);
5838
5839 double dval = val.double_value (true);
5840
5841 retval += do_printf_conv (os, encoding (), tfmt.c_str (), nsa,
5842 sa_1, sa_2, dval, who);
5843 }
5844 break;
5845
5846 case 'o': case 'x': case 'X': case 'u':
5847 if (ok_for_unsigned_int_conv (val))
5848 {
5849 octave_uint64 tval = val.uint64_scalar_value ();
5850
5851 // Insert "long" modifier.
5852 tfmt.replace (tfmt.rfind (type), 1, llmod + type);
5853
5854 retval += do_printf_conv (os, encoding (), tfmt.c_str (), nsa,
5855 sa_1, sa_2, tval.value (), who);
5856 }
5857 else
5858 {
5859 tfmt = switch_to_g_format (elt);
5860
5861 double dval = val.double_value (true);
5862
5863 retval += do_printf_conv (os, encoding (), tfmt.c_str (), nsa,
5864 sa_1, sa_2, dval, who);
5865 }
5866 break;
5867
5868 case 'f': case 'e': case 'E':
5869 case 'g': case 'G':
5870 {
5871 double dval = val.double_value (true);
5872
5873 retval += do_printf_conv (os, encoding (), tfmt.c_str (), nsa,
5874 sa_1, sa_2, dval, who);
5875 }
5876 break;
5877
5878 default:
5879 // Note: error is member fcn from base_stream, not ::error.
5880 // This error does not halt execution so "return ..." must exist.
5881 error (who, "invalid format specifier");
5882 return -1;
5883 break;
5884 }
5885 }
5886
5887 return retval;
5888 }
5889
5890 void
5891 base_stream::field_width_error (const std::string& who) const
5892 {
5893 ::error ("%s: invalid field width, must be integer >= 0 and <= INT_MAX",
5894 who.c_str ());
5895 }
5896
5897 int
5899 const octave_value_list& args,
5900 const std::string& who)
5901 {
5902 int retval = 0;
5903
5904 octave_idx_type m_nconv = fmt_list.num_conversions ();
5905
5906 std::ostream *osp = output_stream ();
5907
5908 if (! osp)
5909 invalid_operation (who, "writing");
5910 else
5911 {
5912 std::ostream& os = *osp;
5913
5914 preserve_stream_state stream_state (os);
5915
5916 const printf_format_elt *elt = fmt_list.first ();
5917
5918 printf_value_cache val_cache (args, who);
5919
5920 for (;;)
5921 {
5922 octave_quit ();
5923
5924 if (! elt)
5925 ::error ("%s: internal error handling format", who.c_str ());
5926
5927 // NSA is the number of 'star' args to convert.
5928 int nsa = (elt->fw == -2) + (elt->prec == -2);
5929
5930 int sa_1 = 0;
5931 int sa_2 = 0;
5932
5933 if (nsa > 0)
5934 {
5935 sa_1 = val_cache.int_value ();
5936
5937 if (! val_cache)
5938 {
5939 field_width_error (who);
5940 break;
5941 }
5942 else
5943 {
5944 if (nsa > 1)
5945 {
5946 sa_2 = val_cache.int_value ();
5947
5948 if (! val_cache)
5949 {
5950 field_width_error (who);
5951 break;
5952 }
5953 }
5954 }
5955 }
5956
5957 if (elt->type == '%')
5958 {
5959 if (encoding ().compare ("utf-8"))
5960 os << string::u8_to_encoding (who, "%", encoding ());
5961 else
5962 os << '%';
5963 retval++;
5964 }
5965 else if (elt->args == 0 && ! elt->text.empty ())
5966 {
5967 if (encoding ().compare ("utf-8"))
5968 os << string::u8_to_encoding (who, elt->text, encoding ());
5969 else
5970 os << elt->text;
5971 retval += (elt->text.length ());
5972 }
5973 else if (elt->type == 's' || elt->type == 'c')
5974 {
5975 octave_value val = val_cache.get_next_value (elt->type);
5976
5977 if (val_cache)
5978 {
5979 if (val.is_string ())
5980 {
5981 std::string sval = val.string_value ();
5982
5983 retval += do_printf_string (os, elt, nsa, sa_1,
5984 sa_2, sval, encoding (),
5985 who);
5986 }
5987 else
5988 retval += do_numeric_printf_conv (os, elt, nsa, sa_1,
5989 sa_2, val, who);
5990 }
5991 else
5992 break;
5993 }
5994 else
5995 {
5996 octave_value val = val_cache.get_next_value ();
5997
5998 if (val_cache)
5999 {
6000 if (! val.isempty ())
6001 retval += do_numeric_printf_conv (os, elt, nsa, sa_1,
6002 sa_2, val, who);
6003 }
6004 else
6005 break;
6006 }
6007
6008 if (! os)
6009 {
6010 error (who, "write error");
6011 break;
6012 }
6013
6014 elt = fmt_list.next (m_nconv > 0 && ! val_cache.exhausted ());
6015
6016 if (! elt || (val_cache.exhausted () && elt->args > 0))
6017 break;
6018 }
6019 }
6020
6021 return retval;
6022 }
6023
6024 int
6025 base_stream::printf (const std::string& fmt,
6026 const octave_value_list& args,
6027 const std::string& who)
6028 {
6029 printf_format_list fmt_list (fmt);
6030
6031 if (fmt_list.num_conversions () == -1)
6032 ::error ("%s: invalid format specified", who.c_str ());
6033
6034 return do_printf (fmt_list, args, who);
6035 }
6036
6037 int
6038 base_stream::puts (const std::string& s, const std::string& who)
6039 {
6040 int retval = -1;
6041
6042 std::ostream *osp = output_stream ();
6043
6044 if (! osp)
6045 invalid_operation (who, "writing");
6046 else
6047 {
6048 std::ostream& os = *osp;
6049
6050 os << s;
6051
6052 if (! os)
6053 error (who, "write error");
6054 else
6055 {
6056 // FIXME: why does this seem to be necessary?
6057 // Without it, output from a loop like
6058 //
6059 // for i = 1:100, fputs (stdout, "foo\n"); endfor
6060 //
6061 // doesn't seem to go to the pager immediately.
6062 os.flush ();
6063
6064 if (os)
6065 retval = 0;
6066 else
6067 error (who, "write error");
6068 }
6069 }
6070
6071 return retval;
6072 }
6073
6074 // Return current error message for this stream.
6075
6076 std::string
6077 base_stream::error (bool clear_err, int& err_num)
6078 {
6079 err_num = (m_fail ? -1 : 0);
6080
6081 std::string tmp = m_errmsg;
6082
6083 if (clear_err)
6084 clear ();
6085
6086 return tmp;
6087 }
6088
6089 void
6090 base_stream::invalid_operation (const std::string& who, const char *rw)
6091 {
6092 // Note: This calls the member fcn error, not ::error from error.h.
6093 error (who, std::string ("stream not open for ") + rw);
6094 }
6095
6096 int
6098 {
6099 int retval = -1;
6100
6101 if (stream_ok ())
6102 retval = m_rep->flush ();
6103
6104 return retval;
6105 }
6106
6107 std::string
6108 stream::getl (octave_idx_type max_len, bool& err, const std::string& who)
6109 {
6110 std::string retval;
6111
6112 if (stream_ok ())
6113 retval = m_rep->getl (max_len, err, who);
6114
6115 return retval;
6116 }
6117
6118 std::string
6119 stream::getl (const octave_value& tc_max_len, bool& err,
6120 const std::string& who)
6121 {
6122 err = false;
6123
6124 int conv_err = 0;
6125
6126 int max_len = -1;
6127
6128 if (tc_max_len.is_defined ())
6129 {
6130 max_len = convert_to_valid_int (tc_max_len, conv_err);
6131
6132 if (conv_err || max_len < 0)
6133 {
6134 err = true;
6135 ::error ("%s: invalid maximum length specified", who.c_str ());
6136 }
6137 }
6138
6139 return getl (max_len, err, who);
6140 }
6141
6142 std::string
6143 stream::gets (octave_idx_type max_len, bool& err, const std::string& who)
6144 {
6145 std::string retval;
6146
6147 if (stream_ok ())
6148 retval = m_rep->gets (max_len, err, who);
6149
6150 return retval;
6151 }
6152
6153 std::string
6154 stream::gets (const octave_value& tc_max_len, bool& err,
6155 const std::string& who)
6156 {
6157 err = false;
6158
6159 int conv_err = 0;
6160
6161 int max_len = -1;
6162
6163 if (tc_max_len.is_defined ())
6164 {
6165 max_len = convert_to_valid_int (tc_max_len, conv_err);
6166
6167 if (conv_err || max_len < 0)
6168 {
6169 err = true;
6170 ::error ("%s: invalid maximum length specified", who.c_str ());
6171 }
6172 }
6173
6174 return gets (max_len, err, who);
6175 }
6176
6177 off_t
6178 stream::skipl (off_t count, bool& err, const std::string& who)
6179 {
6180 off_t retval = -1;
6181
6182 if (stream_ok ())
6183 retval = m_rep->skipl (count, err, who);
6184
6185 return retval;
6186 }
6187
6188 off_t
6189 stream::skipl (const octave_value& tc_count, bool& err,
6190 const std::string& who)
6191 {
6192 err = false;
6193
6194 int conv_err = 0;
6195
6196 int count = 1;
6197
6198 if (tc_count.is_defined ())
6199 {
6200 if (tc_count.is_scalar_type ()
6201 && math::isinf (tc_count.scalar_value ()))
6202 count = -1;
6203 else
6204 {
6205 count = convert_to_valid_int (tc_count, conv_err);
6206
6207 if (conv_err || count < 0)
6208 {
6209 err = true;
6210 ::error ("%s: invalid number of lines specified",
6211 who.c_str ());
6212 }
6213 }
6214 }
6215
6216 return skipl (count, err, who);
6217 }
6218
6219 int
6220 stream::seek (off_t offset, int origin)
6221 {
6222 int status = -1;
6223
6224 if (stream_ok ())
6225 {
6226 clearerr ();
6227
6228 // Find current position so we can return to it if needed.
6229 off_t orig_pos = m_rep->tell ();
6230
6231 // Move to end of file. If successful, find the offset of the end.
6232 status = m_rep->seek (0, SEEK_END);
6233
6234 if (status == 0)
6235 {
6236 off_t eof_pos = m_rep->tell ();
6237
6238 if (origin == SEEK_CUR)
6239 {
6240 // Move back to original position, otherwise we will be seeking
6241 // from the end of file which is probably not the original
6242 // location.
6243 m_rep->seek (orig_pos, SEEK_SET);
6244 }
6245
6246 // Attempt to move to desired position; may be outside bounds of
6247 // existing file.
6248 status = m_rep->seek (offset, origin);
6249
6250 if (status == 0)
6251 {
6252 // Where are we after moving to desired position?
6253 off_t desired_pos = m_rep->tell ();
6254
6255 // I don't think save_pos can be less than zero,
6256 // but we'll check anyway...
6257 if (desired_pos > eof_pos || desired_pos < 0)
6258 {
6259 // Seek outside bounds of file.
6260 // Failure should leave position unchanged.
6261 m_rep->seek (orig_pos, SEEK_SET);
6262
6263 status = -1;
6264 }
6265 }
6266 else
6267 {
6268 // Seeking to the desired position failed.
6269 // Move back to original position and return failure status.
6270 m_rep->seek (orig_pos, SEEK_SET);
6271
6272 status = -1;
6273 }
6274 }
6275 }
6276
6277 return status;
6278 }
6279
6280 int
6281 stream::seek (const octave_value& tc_offset,
6282 const octave_value& tc_origin)
6283 {
6284 int retval = -1;
6285
6286 // FIXME: should we have octave_value methods that handle off_t explicitly?
6287 octave_int64 val = tc_offset.xint64_scalar_value ("fseek: invalid value for offset");
6288 off_t xoffset = val.value ();
6289
6290 int conv_err = 0;
6291
6292 int origin = SEEK_SET;
6293
6294 if (tc_origin.is_string ())
6295 {
6296 std::string xorigin = tc_origin.xstring_value ("fseek: invalid value for origin");
6297
6298 if (xorigin == "bof")
6299 origin = SEEK_SET;
6300 else if (xorigin == "cof")
6301 origin = SEEK_CUR;
6302 else if (xorigin == "eof")
6303 origin = SEEK_END;
6304 else
6305 conv_err = -1;
6306 }
6307 else
6308 {
6309 int xorigin = convert_to_valid_int (tc_origin, conv_err);
6310
6311 if (! conv_err)
6312 {
6313 if (xorigin == -1)
6314 origin = SEEK_SET;
6315 else if (xorigin == 0)
6316 origin = SEEK_CUR;
6317 else if (xorigin == 1)
6318 origin = SEEK_END;
6319 else
6320 conv_err = -1;
6321 }
6322 }
6323
6324 if (conv_err)
6325 ::error ("fseek: invalid value for origin");
6326
6327 retval = seek (xoffset, origin);
6328
6329 if (retval != 0)
6330 // Note: error is member fcn from stream, not ::error.
6331 error ("fseek: failed to seek to requested position");
6332
6333 return retval;
6334 }
6335
6336 off_t
6338 {
6339 off_t retval = -1;
6340
6341 if (stream_ok ())
6342 retval = m_rep->tell ();
6343
6344 return retval;
6345 }
6346
6347 int
6349 {
6350 return seek (0, SEEK_SET);
6351 }
6352
6353 bool
6354 stream::is_open (void) const
6355 {
6356 bool retval = false;
6357
6358 if (stream_ok ())
6359 retval = m_rep->is_open ();
6360
6361 return retval;
6362 }
6363
6364 void
6366 {
6367 if (stream_ok ())
6368 m_rep->close ();
6369 }
6370
6371 template <typename SRC_T, typename DST_T>
6372 static octave_value
6373 convert_and_copy (std::list<void *>& input_buf_list,
6374 octave_idx_type input_buf_elts,
6375 octave_idx_type elts_read,
6376 octave_idx_type nr, octave_idx_type nc, bool swap,
6377 bool do_float_fmt_conv, bool do_NA_conv,
6378 mach_info::float_format from_flt_fmt)
6379 {
6380 typedef typename DST_T::element_type dst_elt_type;
6381
6382 DST_T conv (dim_vector (nr, nc));
6383
6384 dst_elt_type *conv_data = conv.fortran_vec ();
6385
6386 octave_idx_type j = 0;
6387
6388 for (auto it = input_buf_list.cbegin (); it != input_buf_list.cend (); it++)
6389 {
6390 SRC_T *data = static_cast<SRC_T *> (*it);
6391
6392 if (swap || do_float_fmt_conv)
6393 {
6394 if (do_NA_conv)
6395 {
6396 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read;
6397 i++, j++)
6398 {
6399 if (swap)
6400 swap_bytes<sizeof (SRC_T)> (&data[i]);
6401 else if (do_float_fmt_conv)
6402 do_float_format_conversion (&data[i], sizeof (SRC_T),
6403 1, from_flt_fmt,
6405
6406 // FIXME: Potentially add conversion code for MIPS NA here
6407 // Bug #59830.
6408 // dst_elt_type tmp (data[i]);
6409 // if (is_MIPS_NA (tmp))
6410 // tmp = replace_MIPS_NA (tmp);
6411 // conv_data[j] = tmp;
6412
6413 conv_data[j] = data[i];
6414 }
6415 }
6416 else
6417 {
6418 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read;
6419 i++, j++)
6420 {
6421 if (swap)
6422 swap_bytes<sizeof (SRC_T)> (&data[i]);
6423 else if (do_float_fmt_conv)
6424 do_float_format_conversion (&data[i], sizeof (SRC_T),
6425 1, from_flt_fmt,
6427
6428 conv_data[j] = data[i];
6429 }
6430 }
6431 }
6432 else
6433 {
6434 if (do_NA_conv)
6435 {
6436 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read;
6437 i++, j++)
6438 {
6439 // FIXME: Potentially add conversion code for MIPS NA here
6440 conv_data[j] = data[i];
6441 }
6442 }
6443 else
6444 {
6445 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read;
6446 i++, j++)
6447 conv_data[j] = data[i];
6448 }
6449 }
6450
6451 delete [] data;
6452 }
6453
6454 input_buf_list.clear ();
6455
6456 for (octave_idx_type i = elts_read; i < nr * nc; i++)
6457 conv_data[i] = dst_elt_type (0);
6458
6459 return conv;
6460 }
6461
6463 (std::list<void *>& input_buf_list, octave_idx_type input_buf_elts,
6465 bool swap, bool do_float_fmt_conv, bool do_NA_conv,
6466 mach_info::float_format from_flt_fmt);
6467
6468#define TABLE_ELT(T, U, V, W) \
6469 conv_fptr_table[oct_data_conv::T][oct_data_conv::U] = convert_and_copy<V, W>
6470
6471#define FILL_TABLE_ROW(T, V) \
6472 TABLE_ELT (T, dt_int8, V, int8NDArray); \
6473 TABLE_ELT (T, dt_uint8, V, uint8NDArray); \
6474 TABLE_ELT (T, dt_int16, V, int16NDArray); \
6475 TABLE_ELT (T, dt_uint16, V, uint16NDArray); \
6476 TABLE_ELT (T, dt_int32, V, int32NDArray); \
6477 TABLE_ELT (T, dt_uint32, V, uint32NDArray); \
6478 TABLE_ELT (T, dt_int64, V, int64NDArray); \
6479 TABLE_ELT (T, dt_uint64, V, uint64NDArray); \
6480 TABLE_ELT (T, dt_single, V, FloatNDArray); \
6481 TABLE_ELT (T, dt_double, V, NDArray); \
6482 TABLE_ELT (T, dt_char, V, charNDArray); \
6483 TABLE_ELT (T, dt_schar, V, charNDArray); \
6484 TABLE_ELT (T, dt_uchar, V, charNDArray); \
6485 TABLE_ELT (T, dt_logical, V, boolNDArray);
6486
6488 stream::finalize_read (std::list<void *>& input_buf_list,
6489 octave_idx_type input_buf_elts,
6490 octave_idx_type elts_read,
6492 oct_data_conv::data_type input_type,
6493 oct_data_conv::data_type output_type,
6495 {
6496 octave_value retval;
6497
6498 static bool initialized = false;
6499
6500 // Table function pointers for return types x read types.
6501
6502 static conv_fptr conv_fptr_table[oct_data_conv::dt_unknown][14];
6503
6504 if (! initialized)
6505 {
6506 for (int i = 0; i < oct_data_conv::dt_unknown; i++)
6507 for (int j = 0; j < 14; j++)
6508 conv_fptr_table[i][j] = nullptr;
6509
6510 FILL_TABLE_ROW (dt_int8, int8_t);
6511 FILL_TABLE_ROW (dt_uint8, uint8_t);
6512 FILL_TABLE_ROW (dt_int16, int16_t);
6513 FILL_TABLE_ROW (dt_uint16, uint16_t);
6514 FILL_TABLE_ROW (dt_int32, int32_t);
6515 FILL_TABLE_ROW (dt_uint32, uint32_t);
6516 FILL_TABLE_ROW (dt_int64, int64_t);
6517 FILL_TABLE_ROW (dt_uint64, uint64_t);
6518 FILL_TABLE_ROW (dt_single, float);
6519 FILL_TABLE_ROW (dt_double, double);
6520 FILL_TABLE_ROW (dt_char, char);
6521 FILL_TABLE_ROW (dt_schar, signed char);
6522 FILL_TABLE_ROW (dt_uchar, unsigned char);
6523 FILL_TABLE_ROW (dt_logical, bool);
6524
6525 initialized = true;
6526 }
6527
6528 bool swap = false;
6529
6530 if (ffmt == mach_info::flt_fmt_unknown)
6531 ffmt = float_format ();
6532
6535 else
6536 swap = (ffmt == mach_info::flt_fmt_ieee_big_endian);
6537
6538 bool do_float_fmt_conv = ((input_type == oct_data_conv::dt_double
6539 || input_type == oct_data_conv::dt_single)
6540 && ffmt != float_format ());
6541
6542 bool do_NA_conv = (output_type == oct_data_conv::dt_double);
6543
6544 switch (output_type)
6545 {
6560 {
6561 conv_fptr fptr = conv_fptr_table[input_type][output_type];
6562
6563 retval = fptr (input_buf_list, input_buf_elts, elts_read,
6564 nr, nc, swap, do_float_fmt_conv, do_NA_conv, ffmt);
6565 }
6566 break;
6567
6568 default:
6569 ::error ("read: invalid type specification");
6570 }
6571
6572 return retval;
6573 }
6574
6577 oct_data_conv::data_type input_type,
6578 oct_data_conv::data_type output_type,
6580 octave_idx_type& count)
6581 {
6582 octave_value retval;
6583
6584 if (! stream_ok ())
6585 return retval;
6586
6587 octave_idx_type nr = -1;
6588 octave_idx_type nc = -1;
6589
6590 bool one_elt_size_spec = false;
6591
6592 // FIXME: We may eventually want to make this extensible.
6593
6594 // FIXME: We need a better way to ensure that this numbering stays
6595 // consistent with the order of the elements in the data_type enum in the
6596 // oct_data_conv class.
6597
6598 // Expose this in a future version?
6599 std::size_t char_count = 0;
6600
6601 std::ptrdiff_t tmp_count = 0;
6602
6603 try
6604 {
6605 get_size (size, nr, nc, one_elt_size_spec, "fread");
6606 }
6607 catch (const execution_exception&)
6608 {
6609 invalid_operation ("fread", "reading");
6610
6611 return retval;
6612 }
6613
6614 if (one_elt_size_spec)
6615 {
6616 // If NR == 0, Matlab returns [](0x0).
6617
6618 // If NR > 0, the result will be a column vector with the given
6619 // number of rows.
6620
6621 // If NR < 0, then we have Inf and the result will be a column
6622 // vector but we have to wait to see how big NR will be.
6623
6624 if (nr == 0)
6625 nr = nc = 0;
6626 else
6627 nc = 1;
6628 }
6629 else
6630 {
6631 // Matlab returns [] even if there are two elements in the size
6632 // specification and one is nonzero.
6633
6634 // If NC < 0 we have [NR, Inf] and we'll wait to decide how big NC
6635 // should be.
6636
6637 if (nr == 0 || nc == 0)
6638 nr = nc = 0;
6639 }
6640
6641 octave_idx_type elts_to_read = nr * nc;
6642
6643 bool read_to_eof = elts_to_read < 0;
6644
6645 octave_idx_type input_buf_elts = -1;
6646
6647 if (skip == 0)
6648 {
6649 if (read_to_eof)
6650 input_buf_elts = 1024 * 1024;
6651 else
6652 input_buf_elts = elts_to_read;
6653 }
6654 else
6655 input_buf_elts = block_size;
6656
6657 octave_idx_type input_elt_size
6658 = oct_data_conv::data_type_size (input_type);
6659
6660 std::ptrdiff_t input_buf_size
6661 = static_cast<std::ptrdiff_t> (input_buf_elts) * input_elt_size;
6662
6663 assert (input_buf_size >= 0);
6664
6665 // Must also work and return correct type object for 0 elements to read.
6666 std::istream *isp = input_stream ();
6667
6668 if (! isp)
6669 error ("fread: invalid input stream");
6670 else
6671 {
6672 std::istream& is = *isp;
6673
6674 // Initialize eof_pos variable just once per function call
6675 off_t eof_pos = 0;
6676 off_t cur_pos = 0;
6677 if (skip != 0 && is && ! is.eof ())
6678 {
6679 cur_pos = is.tellg ();
6680 is.seekg (0, is.end);
6681 eof_pos = is.tellg ();
6682 is.seekg (cur_pos, is.beg);
6683 }
6684
6685 std::list<void *> input_buf_list;
6686
6687 while (is && ! is.eof ()
6688 && (read_to_eof || tmp_count < elts_to_read))
6689 {
6690 if (! read_to_eof)
6691 {
6692 octave_idx_type remaining_elts = elts_to_read - tmp_count;
6693
6694 if (remaining_elts < input_buf_elts)
6695 input_buf_size = remaining_elts * input_elt_size;
6696 }
6697
6698 char *input_buf = new char [input_buf_size];
6699
6700 is.read (input_buf, input_buf_size);
6701
6702 std::size_t gcount = is.gcount ();
6703
6704 char_count += gcount;
6705 cur_pos += gcount;
6706
6707 octave_idx_type nel = gcount / input_elt_size;
6708
6709 tmp_count += nel;
6710
6711 input_buf_list.push_back (input_buf);
6712
6713 if (skip != 0 && nel == block_size && is)
6714 {
6715 // Attempt to skip.
6716 // If skip would move past EOF, position at EOF.
6717 off_t remaining = eof_pos - cur_pos;
6718
6719 if (remaining < skip)
6720 {
6721 is.seekg (0, is.end);
6722 cur_pos = eof_pos;
6723 }
6724 else
6725 {
6726 is.seekg (skip, is.cur);
6727 cur_pos += skip;
6728 }
6729 }
6730 }
6731
6732 if (read_to_eof)
6733 {
6734 if (nc < 0)
6735 {
6736 nc = tmp_count / nr;
6737
6738 if (tmp_count % nr != 0)
6739 nc++;
6740 }
6741 else
6742 nr = tmp_count;
6743 }
6744 else if (tmp_count == 0)
6745 {
6746 nr = 0;
6747 nc = 0;
6748 }
6749 else if (tmp_count != nr * nc)
6750 {
6751 if (tmp_count % nr != 0)
6752 nc = tmp_count / nr + 1;
6753 else
6754 nc = tmp_count / nr;
6755
6756 if (tmp_count < nr)
6757 nr = tmp_count;
6758 }
6759
6761 error ("fread: number of elements read exceeds max index size");
6762 else
6763 count = static_cast<octave_idx_type> (tmp_count);
6764
6765 retval = finalize_read (input_buf_list, input_buf_elts, count,
6766 nr, nc, input_type, output_type, ffmt);
6767 }
6768
6769 return retval;
6770 }
6771
6774 oct_data_conv::data_type output_type,
6776 {
6777 octave_idx_type retval = -1;
6778
6779 if (! stream_ok ())
6780 invalid_operation ("fwrite", "writing");
6781 else
6782 {
6783 if (flt_fmt == mach_info::flt_fmt_unknown)
6784 flt_fmt = float_format ();
6785
6786 octave_idx_type status = data.write (*this, block_size, output_type,
6787 skip, flt_fmt);
6788
6789 if (status < 0)
6790 error ("fwrite: write error");
6791 else
6792 retval = status;
6793 }
6794
6795 return retval;
6796 }
6797
6798 template <typename T, typename V>
6799 static void
6800 convert_chars (const void *data, void *conv_data, octave_idx_type n_elts)
6801 {
6802 const T *tt_data = static_cast<const T *> (data);
6803
6804 V *vt_data = static_cast<V *> (conv_data);
6805
6806 for (octave_idx_type i = 0; i < n_elts; i++)
6807 vt_data[i] = tt_data[i];
6808 }
6809
6810 template <typename T, typename V>
6811 static void
6812 convert_ints (const T *data, void *conv_data, octave_idx_type n_elts,
6813 bool swap)
6814 {
6815 typedef typename V::val_type val_type;
6816
6817 val_type *vt_data = static_cast<val_type *> (conv_data);
6818
6819 for (octave_idx_type i = 0; i < n_elts; i++)
6820 {
6821 // Yes, we want saturation semantics when converting to an integer type.
6822 V val (data[i]);
6823
6824 vt_data[i] = val.value ();
6825
6826 if (swap)
6827 swap_bytes<sizeof (val_type)> (&vt_data[i]);
6828 }
6829 }
6830
6831 template <typename T>
6833 {
6834 public:
6835 typedef T type;
6836 };
6837
6838 template <typename T>
6840 {
6841 public:
6842 typedef T type;
6843 };
6844
6845 template <typename T>
6846 static bool
6847 convert_data (const T *data, void *conv_data, octave_idx_type n_elts,
6848 oct_data_conv::data_type output_type,
6850 {
6851 bool retval = true;
6852
6853 bool swap = false;
6854
6856 swap = (flt_fmt == mach_info::flt_fmt_ieee_little_endian);
6857 else
6858 swap = (flt_fmt == mach_info::flt_fmt_ieee_big_endian);
6859
6860 bool do_float_conversion = flt_fmt != mach_info::float_format ();
6861
6862 typedef typename ultimate_element_type<T>::type ult_elt_type;
6863
6864 switch (output_type)
6865 {
6867 convert_chars<ult_elt_type, char> (data, conv_data, n_elts);
6868 break;
6869
6871 convert_chars<ult_elt_type, signed char> (data, conv_data, n_elts);
6872 break;
6873
6875 convert_chars<ult_elt_type, unsigned char> (data, conv_data, n_elts);
6876 break;
6877
6879 convert_ints<T, octave_int8> (data, conv_data, n_elts, swap);
6880 break;
6881
6883 convert_ints<T, octave_uint8> (data, conv_data, n_elts, swap);
6884 break;
6885
6887 convert_ints<T, octave_int16> (data, conv_data, n_elts, swap);
6888 break;
6889
6891 convert_ints<T, octave_uint16> (data, conv_data, n_elts, swap);
6892 break;
6893
6895 convert_ints<T, octave_int32> (data, conv_data, n_elts, swap);
6896 break;
6897
6899 convert_ints<T, octave_uint32> (data, conv_data, n_elts, swap);
6900 break;
6901
6903 convert_ints<T, octave_int64> (data, conv_data, n_elts, swap);
6904 break;
6905
6907 convert_ints<T, octave_uint64> (data, conv_data, n_elts, swap);
6908 break;
6909
6911 {
6912 float *vt_data = static_cast<float *> (conv_data);
6913
6914 for (octave_idx_type i = 0; i < n_elts; i++)
6915 {
6916 vt_data[i] = data[i];
6917
6918 if (do_float_conversion)
6919 do_float_format_conversion (&vt_data[i], 1, flt_fmt);
6920 }
6921 }
6922 break;
6923
6925 {
6926 double *vt_data = static_cast<double *> (conv_data);
6927
6928 for (octave_idx_type i = 0; i < n_elts; i++)
6929 {
6930 vt_data[i] = data[i];
6931
6932 if (do_float_conversion)
6933 do_double_format_conversion (&vt_data[i], 1, flt_fmt);
6934 }
6935 }
6936 break;
6937
6938 default:
6939 ::error ("write: invalid type specification");
6940 }
6941
6942 return retval;
6943 }
6944
6945 bool
6946 stream::write_bytes (const void *data, std::size_t nbytes)
6947 {
6948 bool status = false;
6949
6950 std::ostream *osp = output_stream ();
6951
6952 if (osp)
6953 {
6954 std::ostream& os = *osp;
6955
6956 if (os)
6957 {
6958 os.write (static_cast<const char *> (data), nbytes);
6959
6960 if (os)
6961 status = true;
6962 }
6963 }
6964
6965 return status;
6966 }
6967
6968 bool
6969 stream::skip_bytes (std::size_t skip)
6970 {
6971 bool status = false;
6972
6973 std::ostream *osp = output_stream ();
6974
6975 if (! osp)
6976 return false;
6977
6978 std::ostream& os = *osp;
6979
6980 // Seek to skip when inside bounds of existing file.
6981 // Otherwise, write NUL to skip.
6982 off_t orig_pos = tell ();
6983
6984 seek (0, SEEK_END);
6985
6986 off_t eof_pos = tell ();
6987
6988 // Is it possible for this to fail to return us to the original position?
6989 seek (orig_pos, SEEK_SET);
6990
6991 std::size_t remaining = eof_pos - orig_pos;
6992
6993 if (remaining < skip)
6994 {
6995 seek (0, SEEK_END);
6996
6997 // FIXME: probably should try to write larger blocks...
6998 unsigned char zero = 0;
6999 for (std::size_t j = 0; j < skip - remaining; j++)
7000 os.write (reinterpret_cast<const char *> (&zero), 1);
7001 }
7002 else
7003 seek (skip, SEEK_CUR);
7004
7005 if (os)
7006 status = true;
7007
7008 return status;
7009 }
7010
7011 template <typename T>
7013 stream::write (const Array<T>& data, octave_idx_type block_size,
7014 oct_data_conv::data_type output_type,
7015 octave_idx_type skip,
7017 {
7018 bool swap = false;
7019
7021 swap = (flt_fmt == mach_info::flt_fmt_ieee_little_endian);
7022 else
7023 swap = (flt_fmt == mach_info::flt_fmt_ieee_big_endian);
7024
7025 bool do_data_conversion = (swap || ! is_equivalent_type<T> (output_type)
7026 || flt_fmt != mach_info::float_format ());
7027
7028 octave_idx_type nel = data.numel ();
7029
7030 octave_idx_type chunk_size;
7031
7032 if (skip != 0)
7033 chunk_size = block_size;
7034 else if (do_data_conversion)
7035 chunk_size = 1024 * 1024;
7036 else
7037 chunk_size = nel;
7038
7039 octave_idx_type i = 0;
7040
7041 const T *pdata = data.data ();
7042
7043 while (i < nel)
7044 {
7045 if (skip != 0)
7046 {
7047 if (! skip_bytes (skip))
7048 return -1;
7049 }
7050
7051 octave_idx_type remaining_nel = nel - i;
7052
7053 if (chunk_size > remaining_nel)
7054 chunk_size = remaining_nel;
7055
7056 bool status = false;
7057
7058 if (do_data_conversion)
7059 {
7060 std::size_t output_size
7061 = chunk_size * oct_data_conv::data_type_size (output_type);
7062
7063 OCTAVE_LOCAL_BUFFER (unsigned char, conv_data, output_size);
7064
7065 status = convert_data (&pdata[i], conv_data, chunk_size,
7066 output_type, flt_fmt);
7067
7068 if (status)
7069 status = write_bytes (conv_data, output_size);
7070 }
7071 else
7072 status = write_bytes (pdata, sizeof (T) * chunk_size);
7073
7074 if (! status)
7075 return -1;
7076
7077 i += chunk_size;
7078 }
7079
7080 return nel;
7081 }
7082
7083#define INSTANTIATE_WRITE(T) \
7084 template \
7085 OCTINTERP_API octave_idx_type \
7086 stream::write (const Array<T>& data, octave_idx_type block_size, \
7087 oct_data_conv::data_type output_type, \
7088 octave_idx_type skip, \
7089 mach_info::float_format flt_fmt)
7090
7099 INSTANTIATE_WRITE (int8_t);
7100 INSTANTIATE_WRITE (uint8_t);
7101 INSTANTIATE_WRITE (int16_t);
7102 INSTANTIATE_WRITE (uint16_t);
7103 INSTANTIATE_WRITE (int32_t);
7104 INSTANTIATE_WRITE (uint32_t);
7105 INSTANTIATE_WRITE (int64_t);
7106 INSTANTIATE_WRITE (uint64_t);
7107 INSTANTIATE_WRITE (bool);
7108#if defined (OCTAVE_HAVE_OVERLOAD_CHAR_INT8_TYPES)
7109 INSTANTIATE_WRITE (char);
7110#endif
7111 INSTANTIATE_WRITE (float);
7112 INSTANTIATE_WRITE (double);
7113
7115 stream::scanf (const std::string& fmt, const Array<double>& size,
7116 octave_idx_type& count, const std::string& who)
7117 {
7118 octave_value retval;
7119
7120 if (stream_ok ())
7121 retval = m_rep->scanf (fmt, size, count, who);
7122
7123 return retval;
7124 }
7125
7127 stream::scanf (const octave_value& fmt, const Array<double>& size,
7128 octave_idx_type& count, const std::string& who)
7129 {
7130 octave_value retval = Matrix ();
7131
7132 if (fmt.is_string ())
7133 {
7134 std::string sfmt = fmt.string_value ();
7135
7136 if (fmt.is_sq_string ())
7137 sfmt = do_string_escapes (sfmt);
7138
7139 retval = scanf (sfmt, size, count, who);
7140 }
7141 else
7142 {
7143 // Note: error is member fcn from stream, not ::error.
7144 error (who + ": format must be a string");
7145 }
7146
7147 return retval;
7148 }
7149
7151 stream::oscanf (const std::string& fmt, const std::string& who)
7152 {
7153 octave_value_list retval;
7154
7155 if (stream_ok ())
7156 retval = m_rep->oscanf (fmt, who);
7157
7158 return retval;
7159 }
7160
7162 stream::oscanf (const octave_value& fmt, const std::string& who)
7163 {
7164 octave_value_list retval;
7165
7166 if (fmt.is_string ())
7167 {
7168 std::string sfmt = fmt.string_value ();
7169
7170 if (fmt.is_sq_string ())
7171 sfmt = do_string_escapes (sfmt);
7172
7173 retval = oscanf (sfmt, who);
7174 }
7175 else
7176 {
7177 // Note: error is member fcn from stream, not ::error.
7178 error (who + ": format must be a string");
7179 }
7180
7181 return retval;
7182 }
7183
7185 stream::textscan (const std::string& fmt, octave_idx_type ntimes,
7186 const octave_value_list& options,
7187 const std::string& who, octave_idx_type& count)
7188 {
7189 return (stream_ok ()
7190 ? m_rep->do_textscan (fmt, ntimes, options, who, count)
7191 : octave_value ());
7192 }
7193
7194 int
7195 stream::printf (const std::string& fmt, const octave_value_list& args,
7196 const std::string& who)
7197 {
7198 int retval = -1;
7199
7200 if (stream_ok ())
7201 retval = m_rep->printf (fmt, args, who);
7202
7203 return retval;
7204 }
7205
7206 int
7208 const std::string& who)
7209 {
7210 int retval = 0;
7211
7212 if (fmt.is_string ())
7213 {
7214 std::string sfmt = fmt.string_value ();
7215
7216 if (fmt.is_sq_string ())
7217 sfmt = do_string_escapes (sfmt);
7218
7219 retval = printf (sfmt, args, who);
7220 }
7221 else
7222 {
7223 // Note: error is member fcn from stream, not ::error.
7224 error (who + ": format must be a string");
7225 }
7226
7227 return retval;
7228 }
7229
7230 int
7231 stream::puts (const std::string& s, const std::string& who)
7232 {
7233 int retval = -1;
7234
7235 if (stream_ok ())
7236 retval = m_rep->puts (s, who);
7237
7238 return retval;
7239 }
7240
7241 // FIXME: maybe this should work for string arrays too.
7242
7243 int
7244 stream::puts (const octave_value& tc_s, const std::string& who)
7245 {
7246 int retval = -1;
7247
7248 if (tc_s.is_string ())
7249 {
7250 std::string s = tc_s.string_value ();
7251 retval = puts (s, who);
7252 }
7253 else
7254 {
7255 // Note: error is member fcn from stream, not ::error.
7256 error (who + ": argument must be a string");
7257 }
7258
7259 return retval;
7260 }
7261
7262 bool
7263 stream::eof (void) const
7264 {
7265 int retval = -1;
7266
7267 if (stream_ok ())
7268 retval = m_rep->eof ();
7269
7270 return retval;
7271 }
7272
7273 std::string
7274 stream::error (bool clear, int& err_num)
7275 {
7276 std::string retval = "invalid stream object";
7277
7278 if (stream_ok (false))
7279 retval = m_rep->error (clear, err_num);
7280
7281 return retval;
7282 }
7283
7284 std::string
7285 stream::name (void) const
7286 {
7287 std::string retval;
7288
7289 if (stream_ok ())
7290 retval = m_rep->name ();
7291
7292 return retval;
7293 }
7294
7295 int
7296 stream::mode (void) const
7297 {
7298 int retval = 0;
7299
7300 if (stream_ok ())
7301 retval = m_rep->mode ();
7302
7303 return retval;
7304 }
7305
7308 {
7310
7311 if (stream_ok ())
7312 retval = m_rep->float_format ();
7313
7314 return retval;
7315 }
7316
7317 std::string
7319 {
7320 std::string retval = "???";
7321 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode);
7322
7323 if (in_mode == std::ios::in)
7324 retval = "r";
7325 else if (in_mode == std::ios::out
7326 || in_mode == (std::ios::out | std::ios::trunc))
7327 retval = "w";
7328 else if (in_mode == (std::ios::out | std::ios::app))
7329 retval = "a";
7330 else if (in_mode == (std::ios::in | std::ios::out))
7331 retval = "r+";
7332 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc))
7333 retval = "w+";
7334 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate))
7335 retval = "a+";
7336 else if (in_mode == (std::ios::in | std::ios::binary))
7337 retval = "rb";
7338 else if (in_mode == (std::ios::out | std::ios::binary)
7339 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary))
7340 retval = "wb";
7341 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary))
7342 retval = "ab";
7343 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary))
7344 retval = "r+b";
7345 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc
7346 | std::ios::binary))
7347 retval = "w+b";
7348 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate
7349 | std::ios::binary))
7350 retval = "a+b";
7351
7352 return retval;
7353 }
7354
7356 : m_list (), m_lookup_cache (m_list.end ()), m_stdin_file (-1),
7357 m_stdout_file (-1), m_stderr_file (-1)
7358 {
7359 stream stdin_stream = istream::create (&std::cin, "stdin");
7360
7361 // This uses octave_stdout (see pager.h), not std::cout so that
7362 // Octave's standard output stream will pass through the pager.
7363
7364 // FIXME: we should be accessing octave_stdout from the interpreter.
7365
7366 output_system& output_sys = interp.get_output_system ();
7367
7368 stream stdout_stream
7369 = ostream::create (&(output_sys.__stdout__ ()), "stdout");
7370
7371 stream stderr_stream = ostream::create (&std::cerr, "stderr");
7372
7373 m_stdin_file = insert (stdin_stream);
7374 m_stdout_file = insert (stdout_stream);
7375 m_stderr_file = insert (stderr_stream);
7376 }
7377
7379 {
7380 clear ();
7381 }
7382
7384 {
7385 // Insert item with key corresponding to file-descriptor.
7386
7387 int stream_number = os.file_number ();
7388
7389 if (stream_number == -1)
7390 return stream_number;
7391
7392 // Should we test for
7393 //
7394 // (m_list.find (stream_number) != m_list.end ()
7395 // && m_list[stream_number].is_open ())
7396 //
7397 // and respond with "error ("internal error: ...")"? It should not
7398 // happen except for some bug or if the user has opened a stream with
7399 // an interpreted command, but closed it directly with a system call
7400 // in an oct-file; then the kernel knows the fd is free, but Octave
7401 // does not know. If it happens, it should not do harm here to simply
7402 // overwrite this entry, although the wrong entry might have done harm
7403 // before.
7404
7405 if (m_list.size () >= m_list.max_size ())
7406 ::error ("could not create file id");
7407
7408 m_list[stream_number] = os;
7409
7410 return stream_number;
7411 }
7412
7413 OCTAVE_NORETURN static
7414 void
7415 err_invalid_file_id (int fid, const std::string& who)
7416 {
7417 if (who.empty ())
7418 ::error ("invalid stream number = %d", fid);
7419 else
7420 ::error ("%s: invalid stream number = %d", who.c_str (), fid);
7421 }
7422
7423 stream stream_list::lookup (int fid, const std::string& who) const
7424 {
7425 stream retval;
7426
7427 if (fid < 0)
7428 err_invalid_file_id (fid, who);
7429
7430 if (m_lookup_cache != m_list.end () && m_lookup_cache->first == fid)
7431 retval = m_lookup_cache->second;
7432 else
7433 {
7434 ostrl_map::const_iterator iter = m_list.find (fid);
7435
7436 if (iter == m_list.end ())
7437 err_invalid_file_id (fid, who);
7438
7439 retval = iter->second;
7440 m_lookup_cache = iter;
7441 }
7442
7443 return retval;
7444 }
7445
7447 const std::string& who) const
7448 {
7449 int i = get_file_number (fid);
7450
7451 return lookup (i, who);
7452 }
7453
7454 int stream_list::remove (int fid, const std::string& who)
7455 {
7456 // Can't remove stdin (std::cin), stdout (std::cout), or stderr (std::cerr).
7457 if (fid < 3)
7458 err_invalid_file_id (fid, who);
7459
7460 auto iter = m_list.find (fid);
7461
7462 if (iter == m_list.end ())
7463 err_invalid_file_id (fid, who);
7464
7465 stream os = iter->second;
7466 m_list.erase (iter);
7467 m_lookup_cache = m_list.end ();
7468
7469 // FIXME: is this check redundant?
7470 if (! os.is_valid ())
7471 err_invalid_file_id (fid, who);
7472
7473 os.close ();
7474
7475 return 0;
7476 }
7477
7478 int stream_list::remove (const octave_value& fid, const std::string& who)
7479 {
7480 int retval = -1;
7481
7482 if (fid.is_string () && fid.string_value () == "all")
7483 {
7484 clear (false);
7485
7486 retval = 0;
7487 }
7488 else
7489 {
7490 int i = get_file_number (fid);
7491
7492 retval = remove (i, who);
7493 }
7494
7495 return retval;
7496 }
7497
7498 void stream_list::clear (bool flush)
7499 {
7500 if (flush)
7501 {
7502 // Flush stdout and stderr.
7503 m_list[1].flush ();
7504 m_list[2].flush ();
7505 }
7506
7507 for (auto iter = m_list.begin (); iter != m_list.end (); )
7508 {
7509 int fid = iter->first;
7510 if (fid < 3) // Don't delete stdin, stdout, stderr
7511 {
7512 iter++;
7513 continue;
7514 }
7515
7516 stream os = iter->second;
7517
7518 std::string name = os.name ();
7519 std::transform (name.begin (), name.end (), name.begin (), tolower);
7520
7521 // FIXME: This test for gnuplot is hardly foolproof.
7522 if (name.find ("gnuplot") != std::string::npos)
7523 {
7524 // Don't close down pipes to gnuplot
7525 iter++;
7526 continue;
7527 }
7528
7529 // Normal file handle. Close and delete from m_list.
7530 if (os.is_valid ())
7531 os.close ();
7532
7533 m_list.erase (iter++);
7534 }
7535
7536 m_lookup_cache = m_list.end ();
7537 }
7538
7540 {
7541 string_vector retval (4);
7542
7543 if (fid < 0)
7544 return retval;
7545
7546 stream os;
7547 if (m_lookup_cache != m_list.end () && m_lookup_cache->first == fid)
7548 os = m_lookup_cache->second;
7549 else
7550 {
7551 ostrl_map::const_iterator iter = m_list.find (fid);
7552
7553 if (iter == m_list.end ())
7554 return retval;
7555
7556 os = iter->second;
7557 m_lookup_cache = iter;
7558 }
7559
7560 if (! os.is_valid ())
7561 return retval;
7562
7563 retval(0) = os.name ();
7564 retval(1) = stream::mode_as_string (os.mode ());
7566 retval(3) = os.encoding ();
7567
7568 return retval;
7569 }
7570
7572 {
7573 int conv_err = 0;
7574
7575 if (fid.is_single_type ())
7576 ::error ("file id must be a file object or integer value");
7577
7578 int int_fid = convert_to_valid_int (fid, conv_err);
7579
7580 if (conv_err)
7581 ::error ("file id must be a file object or integer value");
7582
7583 return get_info (int_fid);
7584 }
7585
7586 std::string stream_list::list_open_files (void) const
7587 {
7588 std::ostringstream buf;
7589
7590 buf << "\n"
7591 << " number mode arch name\n"
7592 << " ------ ---- ---- ----\n";
7593
7594 for (const auto& fid_strm : m_list)
7595 {
7596 stream os = fid_strm.second;
7597
7598 buf << " "
7599 << std::setiosflags (std::ios::right)
7600 << std::setw (4) << fid_strm.first << " "
7601 // reset necessary in addition to setiosflags since this is one stmt.
7602 << std::resetiosflags (std::ios::adjustfield)
7603 << std::setiosflags (std::ios::left)
7604 << std::setw (3)
7605 << stream::mode_as_string (os.mode ())
7606 << " "
7607 << std::setw (9)
7609 << " "
7610 << os.name () << "\n";
7611 }
7612
7613 buf << "\n";
7614
7615 return buf.str ();
7616 }
7617
7619 {
7620 Matrix retval (1, m_list.size (), 0.0);
7621
7622 int num_open = 0;
7623
7624 for (const auto& fid_strm : m_list)
7625 {
7626 // Skip stdin, stdout, and stderr.
7627 if (fid_strm.first > 2 && fid_strm.second)
7628 retval(0, num_open++) = fid_strm.first;
7629 }
7630
7631 retval.resize ((num_open > 0), num_open);
7632
7633 return retval;
7634 }
7635
7637 {
7638 int retval = -1;
7639
7640 if (fid.is_string ())
7641 {
7642 std::string nm = fid.string_value ();
7643
7644 for (const auto& fid_strm : m_list)
7645 {
7646 // stdin, stdout, and stderr are unnamed.
7647 if (fid_strm.first > 2)
7648 {
7649 stream os = fid_strm.second;
7650
7651 if (os && os.name () == nm)
7652 {
7653 retval = fid_strm.first;
7654 break;
7655 }
7656 }
7657 }
7658 }
7659 else if (fid.is_single_type ())
7660 ::error ("file id must be a file object, std::string, or integer value");
7661 else
7662 {
7663 int conv_err = 0;
7664
7665 int int_fid = convert_to_valid_int (fid, conv_err);
7666
7667 if (conv_err)
7668 ::error ("file id must be a file object, std::string, or integer value");
7669
7670 retval = int_fid;
7671 }
7672
7673 return retval;
7674 }
7675
7677 {
7678 return octave_value (m_stdin_file);
7679 }
7680
7682 {
7683 return octave_value (m_stdout_file);
7684 }
7685
7687 {
7688 return octave_value (m_stderr_file);
7689 }
7690}
static F77_INT nn
Definition: DASPK.cc:66
#define Inf
Definition: Faddeeva.cc:260
#define NaN
Definition: Faddeeva.cc:261
static int elem
Definition: __contourc__.cc:54
static Matrix null(const Matrix &A, octave_idx_type &rank)
Definition: __qp__.cc:61
#define SEEK_SET
#define SEEK_CUR
#define SEEK_END
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:230
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
size_type size(const size_type d) const
Size of the specified dimension.
Definition: Array.h:476
bool isempty(void) const
Size of the specified dimension.
Definition: Array.h:607
const T * data(void) const
Size of the specified dimension.
Definition: Array.h:616
OCTARRAY_API T * fortran_vec(void)
Size of the specified dimension.
Definition: Array.cc:1744
Definition: Cell.h:43
Definition: dMatrix.h:42
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:158
static bool forced_interactive(void)
Definition: octave.cc:325
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
output_system & get_output_system(void)
Definition: interpreter.h:266
bool interactive(void) const
Definition: interpreter.h:171
void recover_from_exception(void)
static stream create(std::istream *arg=nullptr, const std::string &n="")
Definition: oct-iostrm.cc:81
static std::size_t data_type_size(data_type dt)
Definition: data-conv.cc:187
OCTINTERP_API int flush(void)
Definition: oct-stream.cc:5421
OCTINTERP_API std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
Definition: oct-stream.cc:4162
OCTINTERP_API int do_numeric_printf_conv(std::ostream &os, const printf_format_elt *elt, int nsa, int sa_1, int sa_2, const octave_value &val, const std::string &who)
Definition: oct-stream.cc:5771
OCTINTERP_API int do_printf(printf_format_list &fmt_list, const octave_value_list &args, const std::string &who)
Definition: oct-stream.cc:5898
OCTINTERP_API octave_value_list oscanf(const std::string &fmt, const std::string &who)
Definition: oct-stream.cc:5315
std::string m_errmsg
Definition: oct-stream.h:193
OCTINTERP_API std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
Definition: oct-stream.cc:4169
bool ok(void) const
Definition: oct-stream.h:147
OCTINTERP_API void clear(void)
Definition: oct-stream.cc:4035
OCTINTERP_API void invalid_operation(const std::string &who, const char *rw)
Definition: oct-stream.cc:6090
OCTINTERP_API void clearerr(void)
Definition: oct-stream.cc:4042
OCTINTERP_API std::string do_gets(octave_idx_type max_len, bool &err, bool strip_newline, const std::string &who)
Definition: oct-stream.cc:4058
OCTINTERP_API octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
Definition: oct-stream.cc:5065
OCTINTERP_API bool do_oscanf(const scanf_format_elt *elt, octave_value &, const std::string &who)
Definition: oct-stream.cc:5099
OCTINTERP_API int puts(const std::string &s, const std::string &who)
Definition: oct-stream.cc:6038
std::string error(bool clear, int &err_num)
Definition: oct-stream.cc:6077
OCTINTERP_API octave_value do_scanf(scanf_format_list &fmt_list, octave_idx_type nr, octave_idx_type nc, bool one_elt_size_spec, octave_idx_type &count, const std::string &who)
Definition: oct-stream.cc:4667
virtual std::istream * input_stream(void)
Definition: oct-stream.h:110
virtual std::ostream * output_stream(void)
Definition: oct-stream.h:116
OCTINTERP_API off_t skipl(off_t count, bool &err, const std::string &who)
Definition: oct-stream.cc:4176
virtual std::string name(void) const =0
OCTINTERP_API octave_value do_textscan(const std::string &fmt, octave_idx_type ntimes, const octave_value_list &options, const std::string &who, octave_idx_type &count)
Definition: oct-stream.cc:5389
OCTINTERP_API void field_width_error(const std::string &who) const
Definition: oct-stream.cc:5891
std::string encoding(void) const
Definition: oct-stream.h:159
OCTINTERP_API int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
Definition: oct-stream.cc:6025
virtual int file_number(void) const
Definition: oct-stream.h:133
const std::string m_delims
Definition: oct-stream.cc:1350
void clear(std::ios_base::iostate m=(std::ios_base::eofbit &~std::ios_base::eofbit))
Definition: oct-stream.cc:1309
std::istream & m_i_stream
Definition: oct-stream.cc:1328
std::ios_base::iostate rdstate(void)
Definition: oct-stream.cc:1305
std::streampos m_buf_in_file
Definition: oct-stream.cc:1353
delimited_stream(const delimited_stream &)=delete
void seekg(char *old_idx)
Definition: oct-stream.cc:1292
int getline(std::string &dest, char delim)
Definition: oct-stream.cc:1594
delimited_stream(std::istream &is, const std::string &delimiters, int longest_lookahead, octave_idx_type bsize=4096)
Definition: oct-stream.cc:1365
void setstate(std::ios_base::iostate m)
Definition: oct-stream.cc:1307
char * read(char *buffer, int size, char *&new_start)
Definition: oct-stream.cc:1527
std::ios_base::iostate m_flags
Definition: oct-stream.cc:1358
~printf_format_elt(void)=default
printf_format_elt(const printf_format_elt &)=default
printf_format_elt(const std::string &txt="", int n=0, int w=-1, int p=-1, const std::string &f="", char typ='\0', char mod='\0')
Definition: oct-stream.cc:758
void add_elt_to_list(int args, const std::string &flags, int fw, int prec, char type, char modifier)
Definition: oct-stream.cc:972
const printf_format_elt * first(void)
Definition: oct-stream.cc:811
const printf_format_elt * next(bool cycle=true)
Definition: oct-stream.cc:824
octave_idx_type m_nconv
Definition: oct-stream.cc:865
void printme(void) const
Definition: oct-stream.cc:1162
std::ostringstream m_buf
Definition: oct-stream.cc:874
const printf_format_elt * current(void) const
Definition: oct-stream.cc:817
printf_format_list(const std::string &fmt="")
Definition: oct-stream.cc:878
printf_format_list(const printf_format_list &)=delete
void finish_conversion(const std::string &s, std::size_t &i, int args, const std::string &flags, int fw, int prec, char modifier, char &type)
Definition: oct-stream.cc:1107
std::deque< printf_format_elt * > m_fmt_elts
Definition: oct-stream.cc:871
std::size_t length(void) const
Definition: oct-stream.cc:822
octave_idx_type num_conversions(void)
Definition: oct-stream.cc:809
void process_conversion(const std::string &s, std::size_t &i, std::size_t n, int &args, std::string &flags, int &fw, int &prec, char &modifier, char &type)
Definition: oct-stream.cc:992
printf_value_cache(const printf_value_cache &)=delete
octave_value get_next_value(char type=0)
Definition: oct-stream.cc:5500
octave_idx_type m_val_idx
Definition: oct-stream.cc:5490
const octave_value_list m_values
Definition: oct-stream.cc:5489
~printf_value_cache(void)=default
octave_idx_type m_n_vals
Definition: oct-stream.cc:5492
octave_idx_type m_elt_idx
Definition: oct-stream.cc:5491
printf_value_cache(const octave_value_list &args, const std::string &who)
Definition: oct-stream.cc:5447
octave_idx_type m_n_elts
Definition: oct-stream.cc:5493
scanf_format_elt(const std::string &txt="", int w=0, bool d=false, char typ='\0', char mod='\0', const std::string &ch_class="")
Definition: oct-stream.cc:253
scanf_format_elt(const scanf_format_elt &)=default
~scanf_format_elt(void)=default
bool all_character_conversions(void)
Definition: oct-stream.cc:695
scanf_format_list(const std::string &fmt="")
Definition: oct-stream.cc:379
std::deque< scanf_format_elt * > m_fmt_elts
Definition: oct-stream.cc:372
scanf_format_list(const scanf_format_list &)=delete
void printme(void) const
Definition: oct-stream.cc:667
std::ostringstream m_buf
Definition: oct-stream.cc:375
void add_elt_to_list(int width, bool discard, char type, char modifier, const std::string &char_class="")
Definition: oct-stream.cc:463
void process_conversion(const std::string &s, std::size_t &i, std::size_t n, int &width, bool &discard, char &type, char &modifier)
Definition: oct-stream.cc:483
octave_idx_type m_nconv
Definition: oct-stream.cc:366
bool all_numeric_conversions(void)
Definition: oct-stream.cc:725
const scanf_format_elt * next(bool cycle=true)
Definition: oct-stream.cc:321
const scanf_format_elt * first(void)
Definition: oct-stream.cc:310
int finish_conversion(const std::string &s, std::size_t &i, std::size_t n, int width, bool discard, char &type, char modifier)
Definition: oct-stream.cc:591
const scanf_format_elt * current(void) const
Definition: oct-stream.cc:316
octave_idx_type num_conversions(void)
Definition: oct-stream.cc:301
bool ok(void) const
Definition: oct-stream.cc:341
std::size_t length(void) const
Definition: oct-stream.cc:308
OCTINTERP_API int remove(int fid, const std::string &who="")
Definition: oct-stream.cc:7454
OCTINTERP_API std::string list_open_files(void) const
Definition: oct-stream.cc:7586
OCTINTERP_API octave_value stdin_file(void) const
Definition: oct-stream.cc:7676
OCTINTERP_API ~stream_list(void)
Definition: oct-stream.cc:7378
OCTINTERP_API int get_file_number(const octave_value &fid) const
Definition: oct-stream.cc:7636
ostrl_map::const_iterator m_lookup_cache
Definition: oct-stream.h:502
OCTINTERP_API void clear(bool flush=true)
Definition: oct-stream.cc:7498
OCTINTERP_API int insert(stream &os)
Definition: oct-stream.cc:7383
OCTINTERP_API octave_value stderr_file(void) const
Definition: oct-stream.cc:7686
OCTINTERP_API octave_value open_file_numbers(void) const
Definition: oct-stream.cc:7618
OCTINTERP_API stream lookup(int fid, const std::string &who="") const
Definition: oct-stream.cc:7423
OCTINTERP_API octave_value stdout_file(void) const
Definition: oct-stream.cc:7681
OCTINTERP_API stream_list(interpreter &interp)
Definition: oct-stream.cc:7355
OCTINTERP_API string_vector get_info(int fid) const
Definition: oct-stream.cc:7539
OCTINTERP_API void close(void)
Definition: oct-stream.cc:6365
OCTINTERP_API bool eof(void) const
Definition: oct-stream.cc:7263
void invalid_operation(const std::string &who, const char *rw)
Definition: oct-stream.h:443
OCTINTERP_API octave_value read(const Array< double > &size, octave_idx_type block_size, oct_data_conv::data_type input_type, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt, octave_idx_type &count)
Definition: oct-stream.cc:6576
std::istream * input_stream(void)
Definition: oct-stream.h:411
OCTINTERP_API int puts(const std::string &s, const std::string &who)
Definition: oct-stream.cc:7231
OCTINTERP_API mach_info::float_format float_format(void) const
Definition: oct-stream.cc:7307
OCTINTERP_API octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt)
Definition: oct-stream.cc:6773
OCTINTERP_API octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
Definition: oct-stream.cc:7115
OCTINTERP_API off_t skipl(off_t count, bool &err, const std::string &who)
Definition: oct-stream.cc:6178
OCTINTERP_API octave_value textscan(const std::string &fmt, octave_idx_type ntimes, const octave_value_list &options, const std::string &who, octave_idx_type &count)
Definition: oct-stream.cc:7185
OCTINTERP_API bool is_open(void) const
Definition: oct-stream.cc:6354
OCTINTERP_API int flush(void)
Definition: oct-stream.cc:6097
void clearerr(void)
Definition: oct-stream.h:421
OCTINTERP_API std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
Definition: oct-stream.cc:6143
std::ostream * output_stream(void)
Definition: oct-stream.h:416
OCTINTERP_API bool skip_bytes(std::size_t n_elts)
Definition: oct-stream.cc:6969
OCTINTERP_API std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
Definition: oct-stream.cc:6108
OCTINTERP_API bool write_bytes(const void *data, std::size_t n_elts)
Definition: oct-stream.cc:6946
bool stream_ok(bool clear=true) const
Definition: oct-stream.h:428
OCTINTERP_API int seek(off_t offset, int origin)
Definition: oct-stream.cc:6220
OCTINTERP_API int rewind(void)
Definition: oct-stream.cc:6348
OCTINTERP_API octave_value_list oscanf(const std::string &fmt, const std::string &who)
Definition: oct-stream.cc:7151
static OCTINTERP_API std::string mode_as_string(int mode)
Definition: oct-stream.cc:7318
OCTINTERP_API int mode(void) const
Definition: oct-stream.cc:7296
OCTINTERP_API int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
Definition: oct-stream.cc:7195
std::shared_ptr< base_stream > m_rep
Definition: oct-stream.h:426
OCTINTERP_API std::string name(void) const
Definition: oct-stream.cc:7285
bool is_valid(void) const
Definition: oct-stream.h:392
OCTINTERP_API octave_value finalize_read(std::list< void * > &input_buf_list, octave_idx_type input_buf_elts, octave_idx_type elts_read, octave_idx_type nr, octave_idx_type nc, oct_data_conv::data_type input_type, oct_data_conv::data_type output_type, mach_info::float_format ffmt)
Definition: oct-stream.cc:6488
std::string encoding(void)
Definition: oct-stream.h:406
OCTINTERP_API off_t tell(void)
Definition: oct-stream.cc:6337
OCTINTERP_API std::string error(bool clear, int &err_num)
Definition: oct-stream.cc:7274
int file_number(void)
Definition: oct-stream.h:390
textscan_format_elt(const std::string &txt, int w=0, int p=-1, int bw=0, bool dis=false, char typ='\0', const std::string &ch_class=std::string())
Definition: oct-stream.cc:1628
textscan_format_elt(const textscan_format_elt &e)
Definition: oct-stream.cc:1636
std::string parse_char_class(const std::string &pattern) const
Definition: oct-stream.cc:2288
void add_elt_to_list(unsigned int width, int prec, int bitwidth, octave_value val_type, bool discard, char type, const std::string &char_class=std::string())
Definition: oct-stream.cc:2068
std::list< octave_value > m_output_container
Definition: oct-stream.cc:1790
std::list< octave_value > out_buf(void) const
Definition: oct-stream.cc:1758
void process_conversion(const std::string &s, std::size_t &i, std::size_t n)
Definition: oct-stream.cc:2092
const textscan_format_elt * current(void) const
Definition: oct-stream.cc:1721
std::ostringstream m_buf
Definition: oct-stream.cc:1793
int finish_conversion(const std::string &s, std::size_t &i, std::size_t n, unsigned int width, int prec, int bitwidth, octave_value &val_type, bool discard, char &type)
Definition: oct-stream.cc:2375
const textscan_format_elt * next(bool cycle=true)
Definition: oct-stream.cc:1726
textscan_format_list(const textscan_format_list &)=delete
const textscan_format_elt * first(void)
Definition: oct-stream.cc:1715
textscan_format_list(const std::string &fmt=std::string(), const std::string &who="textscan")
Definition: oct-stream.cc:1956
int read_first_row(delimited_stream &is, textscan &ts)
Definition: oct-stream.cc:2474
std::size_t numel(void) const
Definition: oct-stream.cc:1713
std::deque< textscan_format_elt * > m_fmt_elts
Definition: oct-stream.cc:1787
octave_idx_type num_conversions(void) const
Definition: oct-stream.cc:1706
textscan(const textscan &)=delete
std::string m_who
Definition: oct-stream.cc:1895
void scan_one(delimited_stream &is, const textscan_format_elt &fmt, octave_value &ov, Array< octave_idx_type > row)
Definition: oct-stream.cc:3279
int scan_bracket(delimited_stream &is, const std::string &pattern, std::string &val) const
Definition: oct-stream.cc:3202
bool is_delim(unsigned char ch) const
Definition: oct-stream.cc:1879
bool multiple_delims_as_one
Definition: oct-stream.cc:1950
void scan_cstring(delimited_stream &is, const textscan_format_elt &fmt, std::string &val) const
Definition: oct-stream.cc:3254
textscan(const std::string &who_arg="textscan", const std::string &encoding="utf-8")
Definition: oct-stream.cc:2549
bool whitespace_delim(void) const
Definition: oct-stream.cc:1890
int skip_delim(delimited_stream &is)
Definition: oct-stream.cc:3923
bool isspace(unsigned int ch) const
Definition: oct-stream.cc:1886
~textscan(void)=default
void scan_complex(delimited_stream &is, const textscan_format_elt &fmt, Complex &val) const
Definition: oct-stream.cc:2939
std::string m_encoding
Definition: oct-stream.cc:1897
double read_double(delimited_stream &is, const textscan_format_elt &fmt) const
Definition: oct-stream.cc:2787
bool match_literal(delimited_stream &isp, const textscan_format_elt &elem)
Definition: oct-stream.cc:3999
std::string m_delim_table
Definition: oct-stream.cc:1909
octave_value m_empty_value
Definition: oct-stream.cc:1935
void scan_qstring(delimited_stream &is, const textscan_format_elt &fmt, std::string &val)
Definition: oct-stream.cc:3222
std::string m_whitespace
Definition: oct-stream.cc:1943
octave_idx_type m_buffer_size
Definition: oct-stream.cc:1922
void scan_string(delimited_stream &is, const textscan_format_elt &fmt, std::string &val) const
Definition: oct-stream.cc:3154
int scan_caret(delimited_stream &is, const std::string &pattern, std::string &val) const
Definition: oct-stream.cc:3080
std::string m_buf
Definition: oct-stream.cc:1899
octave_idx_type m_lines
Definition: oct-stream.cc:1953
int lookahead(delimited_stream &is, const Cell &targets, int max_len, bool case_sensitive=true) const
Definition: oct-stream.cc:3885
std::string m_date_locale
Definition: oct-stream.cc:1924
std::string m_delims
Definition: oct-stream.cc:1912
octave_value scan(std::istream &isp, const std::string &fmt, octave_idx_type ntimes, const octave_value_list &options, octave_idx_type &read_count)
Definition: oct-stream.cc:2562
int skip_whitespace(delimited_stream &is, bool EOLstop=true)
Definition: oct-stream.cc:3795
void parse_options(const octave_value_list &args, textscan_format_list &fmt_list)
Definition: oct-stream.cc:3571
std::string m_whitespace_table
Definition: oct-stream.cc:1906
int read_format_once(delimited_stream &isp, textscan_format_list &fmt_list, std::list< octave_value > &retval, Array< octave_idx_type > row, int &done_after)
Definition: oct-stream.cc:3446
octave_value do_scan(std::istream &isp, textscan_format_list &fmt_list, octave_idx_type ntimes)
Definition: oct-stream.cc:2585
std::string read_until(delimited_stream &is, const Cell &delimiters, const std::string &ends) const
Definition: oct-stream.cc:3105
std::string m_exp_chars
Definition: oct-stream.cc:1936
virtual bool fast_elem_insert(octave_idx_type n, const octave_value &x)
Definition: ov-base.cc:1406
T value(void) const
Definition: oct-inttypes.h:830
Cell cell_value(void) const
Definition: ovl.h:105
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition: ovl.h:117
octave_idx_type length(void) const
Definition: ovl.h:113
octave_value isnan(void) const
Definition: ov.h:1598
bool iscell(void) const
Definition: ov.h:649
octave_int64 int64_scalar_value(void) const
Definition: ov.h:980
bool isreal(void) const
Definition: ov.h:783
OCTINTERP_API std::string xstring_value(const char *fmt,...) const
OCTINTERP_API int write(octave::stream &os, int block_size, oct_data_conv::data_type output_type, int skip, octave::mach_info::float_format flt_fmt) const
bool is_true(void) const
Definition: ov.h:803
octave_idx_type numel(void) const
Definition: ov.h:604
bool is_scalar_type(void) const
Definition: ov.h:789
bool is_string(void) const
Definition: ov.h:682
bool is_defined(void) const
Definition: ov.h:637
bool isinteger(void) const
Definition: ov.h:775
bool isstruct(void) const
Definition: ov.h:694
octave_value reshape(const dim_vector &dv) const
Definition: ov.h:616
double scalar_value(bool frc_str_conv=false) const
Definition: ov.h:892
octave_value fast_elem_extract(octave_idx_type n) const
Extract the n-th element, aka 'val(n)'.
Definition: ov.h:1645
std::string string_value(bool force=false) const
Definition: ov.h:1019
bool is_uint64_type(void) const
Definition: ov.h:772
octave_value isinf(void) const
Definition: ov.h:1596
@ op_ge
Definition: ov.h:100
octave_uint64 uint64_scalar_value(void) const
Definition: ov.h:992
bool isempty(void) const
Definition: ov.h:646
NDArray array_value(bool frc_str_conv=false) const
Definition: ov.h:904
OCTINTERP_API octave_int64 xint64_scalar_value(const char *fmt,...) const
octave_base_value * internal_rep(void) const
Definition: ov.h:1508
bool is_single_type(void) const
Definition: ov.h:743
octave_value convert_to_str(bool pad=false, bool force=false, char type='\'') const
Definition: ov.h:1411
bool isobject(void) const
Definition: ov.h:709
bool is_sq_string(void) const
Definition: ov.h:685
double double_value(bool frc_str_conv=false) const
Definition: ov.h:886
static stream create(std::ostream *arg, const std::string &n="")
Definition: oct-iostrm.cc:95
std::ostream & __stdout__(void)
Definition: pager.h:260
void do_float_format_conversion(void *data, octave_idx_type len, octave::mach_info::float_format from_fmt, octave::mach_info::float_format to_fmt)
Definition: data-conv.cc:706
void do_double_format_conversion(void *data, octave_idx_type len, octave::mach_info::float_format from_fmt, octave::mach_info::float_format to_fmt)
Definition: data-conv.cc:659
void warning(const char *fmt,...)
Definition: error.cc:1055
void warning_with_id(const char *id, const char *fmt,...)
Definition: error.cc:1070
void error(const char *fmt,...)
Definition: error.cc:980
#define panic_impossible()
Definition: error.h:411
void err_wrong_type_arg(const char *name, const char *s)
Definition: errwarn.cc:166
ColumnVector transform(const Matrix &m, double x, double y, double z)
Definition: graphics.cc:5861
QString name
OCTAVE_API int octave_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: lo-cutils.c:49
#define lo_ieee_isnan(x)
Definition: lo-ieee.h:100
#define lo_ieee_isinf(x)
Definition: lo-ieee.h:108
#define lo_ieee_is_NA(x)
Definition: lo-ieee.h:112
F77_RET_T const F77_INT const F77_INT const F77_INT const F77_DBLE const F77_DBLE F77_INT F77_DBLE * V
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
class OCTAVE_API Matrix
Definition: mx-fwd.h:31
std::complex< double > w(std::complex< double > z, double relerr=0)
float_format native_float_format(void)
Definition: mach-info.cc:65
@ flt_fmt_ieee_little_endian
Definition: mach-info.h:43
bool words_big_endian(void)
Definition: mach-info.cc:72
std::string float_format_as_string(float_format flt_fmt)
Definition: mach-info.cc:105
double fix(double x)
Definition: lo-mappers.h:118
T x_nint(T x)
Definition: lo-mappers.h:269
int nint(double x)
Definition: lo-mappers.cc:212
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
Definition: lo-specfun.cc:1128
bool is_NaN_or_NA(const Complex &x)
Definition: lo-mappers.cc:71
bool isinf(double x)
Definition: lo-mappers.h:203
octave_idx_type nint_big(double x)
Definition: lo-mappers.cc:184
std::complex< T > trunc(const std::complex< T > &x)
Definition: lo-mappers.h:111
OCTAVE_API std::string u8_to_encoding(const std::string &who, const std::string &u8_string, const std::string &encoding)
Definition: oct-string.cc:497
OCTAVE_API std::string u8_from_encoding(const std::string &who, const std::string &native_string, const std::string &encoding)
Definition: oct-string.cc:529
OCTAVE_API bool strncmp(const T &str_a, const T &str_b, const typename T::size_type n)
True if the first N characters are the same.
static bool convert_data(const T *data, void *conv_data, octave_idx_type n_elts, oct_data_conv::data_type output_type, mach_info::float_format flt_fmt)
Definition: oct-stream.cc:6847
static Cell init_inf_nan(void)
Definition: oct-stream.cc:1200
static int convert_to_valid_int(const octave_value &tc, int &conv_err)
Definition: oct-stream.cc:88
static uint32_t state[624]
Definition: randmtzig.cc:192
static std::istream & octave_scan(std::istream &is, const scanf_format_elt &fmt, T *valptr)
Definition: oct-stream.cc:4323
static void convert_ints(const T *data, void *conv_data, octave_idx_type n_elts, bool swap)
Definition: oct-stream.cc:6812
static int do_printf_conv(std::ostream &os, const std::string &encoding, const char *fmt, int nsa, int sa_1, int sa_2, T arg, const std::string &who)
Definition: oct-stream.cc:5635
static double f(double k, double l_nu, double c_pm)
Definition: randpoisson.cc:118
static std::istream & octave_scan_1(std::istream &is, const scanf_format_elt &fmt, T *valptr)
Definition: oct-stream.cc:4234
static OCTAVE_NORETURN void err_invalid_file_id(int fid, const std::string &who)
Definition: oct-stream.cc:7415
static bool ok_for_unsigned_int_conv(const octave_value &val)
Definition: oct-stream.cc:5734
static void do_scanf_conv(std::istream &is, const scanf_format_elt &fmt, T valptr, Matrix &mval, double *data, octave_idx_type &idx, octave_idx_type &conversion_count, octave_idx_type nr, octave_idx_type max_size, bool discard)
Definition: oct-stream.cc:4409
static std::string expand_char_class(const std::string &s)
Definition: oct-stream.cc:206
static bool ok_for_signed_int_conv(const octave_value &val)
Definition: oct-stream.cc:5704
static uint32_t * next
Definition: randmtzig.cc:191
static int left
Definition: randmtzig.cc:193
static std::size_t do_printf_string(std::ostream &os, const printf_format_elt *elt, int nsa, int sa_1, int sa_2, const std::string &arg, const std::string &encoding, const std::string &who)
Definition: oct-stream.cc:5664
static bool is_nan_or_inf(const octave_value &val)
Definition: oct-stream.cc:5695
static octave_value convert_and_copy(std::list< void * > &input_buf_list, octave_idx_type input_buf_elts, octave_idx_type elts_read, octave_idx_type nr, octave_idx_type nc, bool swap, bool do_float_fmt_conv, bool do_NA_conv, mach_info::float_format from_flt_fmt)
Definition: oct-stream.cc:6373
interpreter & __get_interpreter__(const std::string &who)
octave_value(* conv_fptr)(std::list< void * > &input_buf_list, octave_idx_type input_buf_elts, octave_idx_type elts_read, octave_idx_type nr, octave_idx_type nc, bool swap, bool do_float_fmt_conv, bool do_NA_conv, mach_info::float_format from_flt_fmt)
Definition: oct-stream.cc:6463
static std::string switch_to_g_format(const printf_format_elt *elt)
Definition: oct-stream.cc:5761
static octave_idx_type get_size(double d, const std::string &who)
Definition: oct-stream.cc:128
static double pown(double x, unsigned int n)
Definition: oct-stream.cc:1185
static void convert_chars(const void *data, void *conv_data, octave_idx_type n_elts)
Definition: oct-stream.cc:6800
STL namespace.
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
octave_int< T > mod(const octave_int< T > &x, const octave_int< T > &y)
Definition: oct-inttypes.h:916
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:44
#define scanner
Definition: oct-parse.cc:147
#define BEGIN_S_CONVERSION()
Definition: oct-stream.cc:4524
#define BEGIN_CHAR_CLASS_CONVERSION()
Definition: oct-stream.cc:4573
#define BEGIN_C_CONVERSION()
Definition: oct-stream.cc:4503
#define DO_WHITESPACE_CONVERSION()
Definition: oct-stream.cc:4443
#define DO_LITERAL_CONVERSION()
Definition: oct-stream.cc:4457
#define FILL_TABLE_ROW(T, V)
Definition: oct-stream.cc:6471
#define INSTANTIATE_WRITE(T)
Definition: oct-stream.cc:7083
#define DO_PCT_CONVERSION()
Definition: oct-stream.cc:4485
#define FINISH_CHARACTER_CONVERSION()
Definition: oct-stream.cc:4620
T::size_type numel(const T &str)
Definition: oct-string.cc:71
const octave_base_value const Array< octave_idx_type > & ra_idx
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
OCTINTERP_API octave_value cat_op(type_info &ti, const octave_value &a, const octave_value &b, const Array< octave_idx_type > &ra_idx)
OCTINTERP_API octave_value binary_op(type_info &ti, octave_value::binary_op op, const octave_value &a, const octave_value &b)
static void parse_options(regexp::opts &options, const octave_value_list &args, const std::string &who, int skip, bool &extra_args)
Definition: regexp.cc:315
std::string do_string_escapes(const std::string &s)
Definition: utils.cc:801
std::string undo_string_escapes(const std::string &s)
Definition: utils.cc:1029
std::size_t format(std::ostream &os, const char *fmt,...)
Definition: utils.cc:1471
F77_RET_T len
Definition: xerbla.cc:61