26#if defined (HAVE_CONFIG_H)
89convert_to_valid_int (
const octave_value& tc,
int& conv_err)
112 if (! math::isnan (dval))
114 int ival = math::nint (dval);
129get_size (
double d,
const std::string& who)
134 ::error (
"%s: NaN invalid as size specification", who.c_str ());
141 ::error (
"%s: negative value invalid as size specification",
144 static constexpr double out_of_range_top
145 =
static_cast<double> (std::numeric_limits<octave_idx_type>::max ()) + 1.0;
146 if (
d >= out_of_range_top)
147 ::error (
"%s: dimension too large for Octave's index type",
150 retval = math::nint_big (
d);
159 bool& one_elt_size_spec,
const std::string& who)
164 one_elt_size_spec =
false;
173 one_elt_size_spec =
true;
177 dnc = (dnr == 0.0) ? 0.0 : 1.0;
179 else if (sz_len == 2)
183 if (math::isinf (dnr))
184 ::error (
"%s: infinite value invalid as size specification",
190 ::error (
"%s: invalid size specification (must be 2-D)", who.c_str ());
192 nr = get_size (dnr, who);
196 nc = get_size (dnc, who);
200 && nc > std::numeric_limits<octave_idx_type>::max () / nr)
201 ::error (
"%s: size too large for Octave's index type", who.c_str ());
206expand_char_class (
const std::string& s)
210 std::size_t
len = s.length ();
216 unsigned char c = s[i++];
218 if (c ==
'-' && i > 1 && i <
len
219 && (
static_cast<unsigned char> (s[i-2])
220 <=
static_cast<unsigned char> (s[i])))
225 for (c = s[i-2]+1; c < s[i]; c++)
233 if (c !=
'-' || i ==
len)
241class scanf_format_elt
245 enum special_conversion
247 whitespace_conversion = 1,
248 literal_conversion = 2,
252 scanf_format_elt (
const std::string& txt =
"",
int w = 0,
bool d =
false,
253 char typ =
'\0',
char mod =
'\0',
254 const std::string& ch_class =
"")
255 : text (txt), width (
w), discard (
d), type (typ),
256 modifier (
mod), char_class (ch_class)
259 scanf_format_elt (
const scanf_format_elt&) =
default;
261 scanf_format_elt& operator = (
const scanf_format_elt&) =
default;
263 ~scanf_format_elt () =
default;
282 std::string char_class;
285class scanf_format_list
289 scanf_format_list (
const std::string& fmt =
"");
291 OCTAVE_DISABLE_COPY_MOVE (scanf_format_list)
293 ~scanf_format_list ();
302 std::size_t length ()
const {
return m_fmt_elts.size (); }
304 const scanf_format_elt * first ()
310 const scanf_format_elt * current ()
const
312 return length () > 0 ? m_fmt_elts[m_curr_idx] :
nullptr;
315 const scanf_format_elt * next (
bool cycle =
true)
317 static scanf_format_elt dummy
318 (
"", 0,
false, scanf_format_elt::null,
'\0',
"");
322 if (m_curr_idx >= length ())
333 void printme ()
const;
335 bool ok ()
const {
return (m_nconv >= 0); }
337 operator bool ()
const {
return ok (); }
339 bool all_character_conversions ();
341 bool all_numeric_conversions ();
345 void add_elt_to_list (
int width,
bool discard,
char type,
char modifier,
346 const std::string& char_class =
"");
348 void process_conversion (
const std::string& s, std::size_t& i,
349 std::size_t n,
int& width,
bool& discard,
350 char& type,
char& modifier);
352 int finish_conversion (
const std::string& s, std::size_t& i, std::size_t n,
353 int width,
bool discard,
char& type,
363 std::size_t m_curr_idx;
366 std::deque<scanf_format_elt *> m_fmt_elts;
369 std::ostringstream m_buf;
373scanf_format_list::scanf_format_list (
const std::string& s)
374 : m_nconv (0), m_curr_idx (0), m_fmt_elts (), m_buf ()
376 std::size_t n = s.length ();
381 bool discard =
false;
382 char modifier =
'\0';
385 bool have_more =
true;
395 process_conversion (s, i, n, width, discard, type, modifier);
397 have_more = (m_buf.tellp () != 0);
399 else if (isspace (s[i]))
401 type = scanf_format_elt::whitespace_conversion;
408 while (++i < n && isspace (s[i]))
411 add_elt_to_list (width, discard, type, modifier);
417 type = scanf_format_elt::literal_conversion;
423 while (i < n && ! isspace (s[i]) && s[i] !=
'%')
426 add_elt_to_list (width, discard, type, modifier);
439 add_elt_to_list (width, discard, type, modifier);
445scanf_format_list::~scanf_format_list ()
447 std::size_t n = m_fmt_elts.size ();
449 for (std::size_t i = 0; i < n; i++)
451 scanf_format_elt *elt = m_fmt_elts[i];
457scanf_format_list::add_elt_to_list (
int width,
bool discard,
char type,
459 const std::string& char_class)
461 std::string text = m_buf.str ();
465 scanf_format_elt *elt
466 =
new scanf_format_elt (text, width, discard, type,
467 modifier, char_class);
469 m_fmt_elts.push_back (elt);
477scanf_format_list::process_conversion (
const std::string& s, std::size_t& i,
478 std::size_t n,
int& width,
479 bool& discard,
char& type,
489 bool have_width =
false;
505 case '0':
case '1':
case '2':
case '3':
case '4':
506 case '5':
case '6':
case '7':
case '8':
case '9':
512 width = 10 * width + c -
'0';
515 while (i < n && isdigit (s[i]))
518 width = 10 * width + c -
'0';
524 case 'h':
case 'l':
case 'L':
525 if (modifier !=
'\0')
532 case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
543 case 'e':
case 'f':
case 'g':
556 case 'c':
case 's':
case 'p':
case '%':
case '[':
557 if (modifier !=
'\0')
566 if (finish_conversion (s, i, n, width, discard,
567 type, modifier) == 0)
585scanf_format_list::finish_conversion (
const std::string& s, std::size_t& i,
586 std::size_t n,
int width,
bool discard,
587 char& type,
char modifier)
591 std::string char_class;
593 std::size_t beg_idx = std::string::npos;
594 std::size_t end_idx = std::string::npos;
626 else if (s[i] ==
']')
630 while (i < n && s[i] !=
']')
633 if (i < n && s[i] ==
']')
640 retval = m_nconv = -1;
650 if (beg_idx != std::string::npos && end_idx != std::string::npos)
651 char_class = expand_char_class (s.substr (beg_idx,
652 end_idx - beg_idx + 1));
654 add_elt_to_list (width, discard, type, modifier, char_class);
661scanf_format_list::printme ()
const
663 std::size_t n = m_fmt_elts.size ();
665 for (std::size_t i = 0; i < n; i++)
667 scanf_format_elt *elt = m_fmt_elts[i];
670 <<
"width: " << elt->width <<
"\n"
671 <<
"discard: " << elt->discard <<
"\n"
674 if (elt->type == scanf_format_elt::literal_conversion)
675 std::cerr <<
"literal text\n";
676 else if (elt->type == scanf_format_elt::whitespace_conversion)
677 std::cerr <<
"whitespace\n";
679 std::cerr << elt->type <<
"\n";
682 <<
"modifier: " << elt->modifier <<
"\n"
689scanf_format_list::all_character_conversions ()
691 std::size_t n = m_fmt_elts.size ();
695 for (std::size_t i = 0; i < n; i++)
697 scanf_format_elt *elt = m_fmt_elts[i];
701 case 'c':
case 's':
case '%':
case '[':
case '^':
702 case scanf_format_elt::literal_conversion:
703 case scanf_format_elt::whitespace_conversion:
719scanf_format_list::all_numeric_conversions ()
721 std::size_t n = m_fmt_elts.size ();
725 for (std::size_t i = 0; i < n; i++)
727 scanf_format_elt *elt = m_fmt_elts[i];
731 case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
732 case 'e':
case 'f':
case 'g':
case 'E':
case 'G':
747class printf_format_elt
751 printf_format_elt (
const std::string& txt =
"",
int n = 0,
int w = -1,
752 int p = -1,
const std::string&
f =
"",
753 char typ =
'\0',
char mod =
'\0')
754 : text (txt), args (n), fw (
w), prec (p), flags (
f),
755 type (typ), modifier (
mod)
758 printf_format_elt (
const printf_format_elt&) =
default;
760 printf_format_elt& operator = (
const printf_format_elt&) =
default;
762 ~printf_format_elt () =
default;
787class printf_format_list
791 printf_format_list (
const std::string& fmt =
"");
793 OCTAVE_DISABLE_COPY_MOVE (printf_format_list)
795 ~printf_format_list ();
799 const printf_format_elt * first ()
805 const printf_format_elt * current ()
const
807 return length () > 0 ? m_fmt_elts[m_curr_idx] :
nullptr;
810 std::size_t length ()
const {
return m_fmt_elts.size (); }
812 const printf_format_elt * next (
bool cycle =
true)
816 if (m_curr_idx >= length ())
827 bool last_elt_p () {
return (m_curr_idx + 1 == length ()); }
829 void printme ()
const;
831 bool ok ()
const {
return (m_nconv >= 0); }
833 operator bool ()
const {
return ok (); }
837 void add_elt_to_list (
int args,
const std::string& flags,
int fw,
838 int prec,
char type,
char modifier);
840 void process_conversion (
const std::string& s, std::size_t& i,
842 int& args, std::string& flags,
int& fw,
843 int& prec,
char& modifier,
char& type);
845 void finish_conversion (
const std::string& s, std::size_t& i,
int args,
846 const std::string& flags,
int fw,
int prec,
847 char modifier,
char& type);
856 std::size_t m_curr_idx;
859 std::deque<printf_format_elt *> m_fmt_elts;
862 std::ostringstream m_buf;
866printf_format_list::printf_format_list (
const std::string& s)
867 : m_nconv (0), m_curr_idx (0), m_fmt_elts (), m_buf ()
869 std::size_t n = s.length ();
877 char modifier =
'\0';
880 bool have_more =
true;
881 bool empty_buf =
true;
885 printf_format_elt *elt
886 =
new printf_format_elt (
"", args, fw, prec, flags, type, modifier);
888 m_fmt_elts.push_back (elt);
896 empty_buf = (m_buf.tellp () == 0);
904 process_conversion (s, i, n, args, flags, fw, prec,
913 have_more = (m_buf.tellp () != 0);
916 add_elt_to_list (args, flags, fw, prec, type, modifier);
941 add_elt_to_list (args, flags, fw, prec, type, modifier);
948printf_format_list::~printf_format_list ()
950 std::size_t n = m_fmt_elts.size ();
952 for (std::size_t i = 0; i < n; i++)
954 printf_format_elt *elt = m_fmt_elts[i];
960printf_format_list::add_elt_to_list (
int args,
const std::string& flags,
961 int fw,
int prec,
char type,
964 std::string text = m_buf.str ();
968 printf_format_elt *elt
969 =
new printf_format_elt (text, args, fw, prec, flags,
972 m_fmt_elts.push_back (elt);
980printf_format_list::process_conversion (
const std::string& s, std::size_t& i,
981 std::size_t n,
int& args,
982 std::string& flags,
int& fw,
983 int& prec,
char& modifier,
1001 case '-':
case '+':
case ' ':
case '0':
case '#':
1028 std::string tmp = s.substr (i);
1029 sscanf (tmp.c_str (),
"%d%n", &fw, &nn);
1032 while (i < n && isdigit (s[i]))
1037 if (i < n && s[i] ==
'.')
1061 std::string tmp = s.substr (i);
1062 sscanf (tmp.c_str (),
"%d%n", &prec, &nn);
1065 while (i < n && isdigit (s[i]))
1079 case 'h':
case 'l':
case 'L':
1089 finish_conversion (s, i, args, flags, fw, prec, modifier, type);
1095printf_format_list::finish_conversion (
const std::string& s, std::size_t& i,
1096 int args,
const std::string& flags,
1097 int fw,
int prec,
char modifier,
1102 case 'd':
case 'i':
case 'o':
case 'x':
case 'X':
1104 if (modifier ==
'L')
1111 case 'f':
case 'e':
case 'E':
case 'g':
case 'G':
1112 if (modifier ==
'h' || modifier ==
'l')
1119 case 's':
case 'p':
case '%':
1120 if (modifier !=
'\0')
1133 if (type !=
'%' || args != 0)
1139 add_elt_to_list (args, flags, fw, prec, type, modifier);
1150printf_format_list::printme ()
const
1152 std::size_t n = m_fmt_elts.size ();
1154 for (std::size_t i = 0; i < n; i++)
1156 printf_format_elt *elt = m_fmt_elts[i];
1159 <<
"args: " << elt->args <<
"\n"
1160 <<
"flags: '" << elt->flags <<
"'\n"
1161 <<
"width: " << elt->fw <<
"\n"
1162 <<
"prec: " << elt->prec <<
"\n"
1163 <<
"type: '" << elt->type <<
"'\n"
1164 <<
"modifier: '" << elt->modifier <<
"'\n"
1173pown (
double x,
unsigned int n)
1177 for (
unsigned int d = n;
d;
d >>= 1)
1211class delimited_stream
1215 delimited_stream (std::istream& is,
const std::string& delimiters,
1218 delimited_stream (std::istream& is,
const delimited_stream& ds);
1220 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (delimited_stream)
1222 ~delimited_stream ();
1228 if (m_idx >= m_last)
1234 int refresh_buf (
bool initialize =
false);
1241 return eof () ? std::istream::traits_type::eof () : *m_idx++;
1243 return get_undelim ();
1254 {
return eof () ? std::istream::traits_type::eof () : *m_idx; }
1257 int peek_undelim ();
1263 void putback (
char = 0) {
if (! eof ()) --m_idx; }
1265 int getline (std::string& dest,
char delim);
1269 char * read (
char *buffer,
int size,
char *&new_start);
1273 char * tellg () {
return m_idx; }
1275 void seekg (
char *old_idx) { m_idx = old_idx; }
1279 return (m_eob == m_buf + m_overlap && m_i_stream.eof ())
1280 || (m_flags & std::ios_base::eofbit);
1283 operator const void *()
1284 {
return (! eof () && ! m_flags) ?
this : nullptr; }
1286 bool fail () {
return m_flags & std::ios_base::failbit; }
1288 std::ios_base::iostate rdstate () {
return m_flags; }
1290 void setstate (std::ios_base::iostate m) { m_flags = m_flags | m; }
1292 void clear (std::ios_base::iostate m
1293 = (std::ios_base::eofbit & ~std::ios_base::eofbit))
1295 m_flags = m_flags & m;
1301 void progress_benchmark () { m_progress_marker = m_idx; }
1303 bool no_progress () {
return m_progress_marker == m_idx; }
1308 std::ptrdiff_t remaining ()
1310 if (m_eob < m_buf + m_bufsize)
1311 return m_eob - m_idx;
1313 return std::numeric_limits<std::ptrdiff_t>::max ();
1322 std::istream& m_i_stream;
1338 std::ptrdiff_t m_overlap;
1347 const std::string m_delims;
1350 std::streampos m_buf_in_file;
1353 char *m_progress_marker;
1355 std::ios_base::iostate m_flags;
1362delimited_stream::delimited_stream (std::istream& is,
1363 const std::string& delimiters,
1364 int longest_lookahead,
1366 : m_bufsize (bsize), m_i_stream (is), m_longest (longest_lookahead),
1367 m_delims (delimiters),
1368 m_flags (std::ios::failbit & ~std::ios::failbit)
1370 m_buf =
new char[m_bufsize];
1371 m_eob = m_buf + m_bufsize;
1373 m_progress_marker = m_idx;
1378delimited_stream::delimited_stream (std::istream& is,
1379 const delimited_stream& ds)
1380 : delimited_stream (is, ds.m_delims, ds.m_longest, ds.m_bufsize)
1383delimited_stream::~delimited_stream ()
1388 m_i_stream.clear ();
1389 m_i_stream.seekg (m_buf_in_file);
1390 m_i_stream.read (m_buf, m_idx - m_buf - m_overlap);
1400delimited_stream::get_undelim ()
1405 setstate (std::ios_base::failbit);
1406 return std::istream::traits_type::eof ();
1417 setstate (std::ios_base::eofbit);
1418 retval = std::istream::traits_type::eof ();
1424 if (m_idx >= m_last)
1425 m_delimited =
false;
1434delimited_stream::peek_undelim ()
1436 int retval = get_undelim ();
1448delimited_stream::refresh_buf (
bool initialize)
1451 return std::istream::traits_type::eof ();
1458 std::size_t old_remaining = m_eob - m_idx;
1459 std::size_t old_overlap = 0;
1461 if (initialize || (m_idx - m_buf <= 0))
1465 old_overlap = m_overlap;
1472 m_overlap = std::min (m_overlap, m_idx - m_buf - 1);
1477 if (old_remaining + m_overlap > 0)
1479 m_buf_in_file += (m_idx - old_overlap - m_buf);
1480 std::memmove (m_buf, m_idx - m_overlap, m_overlap + old_remaining);
1483 m_buf_in_file = m_i_stream.tellg ();
1486 m_progress_marker -= m_idx - m_overlap - m_buf;
1487 m_idx = m_buf + m_overlap;
1490 if (! m_i_stream.eof ())
1492 m_i_stream.read (m_buf + m_overlap + old_remaining,
1493 m_bufsize - m_overlap - old_remaining);
1494 gcount = m_i_stream.gcount ();
1499 m_eob = m_buf + m_overlap + old_remaining + gcount;
1503 m_delimited =
false;
1505 if (m_eob != m_buf + m_overlap)
1510 retval = std::istream::traits_type::eof ();
1516 for (m_last = m_eob - m_longest; m_last - m_buf - m_overlap >= 0;
1519 if (m_delims.find (*m_last) != std::string::npos)
1523 if (m_last < m_buf + m_overlap)
1524 m_delimited =
false;
1530 if (retval == std::istream::traits_type::eof ())
1543delimited_stream::read (
char *buffer,
int size,
char *&prior_tell)
1547 if (m_eob - m_idx >= size)
1552 m_delimited =
false;
1561 if (m_eob - prior_tell + size < m_bufsize)
1575 if (m_eob - m_idx > size)
1580 m_delimited =
false;
1584 if (size <= m_bufsize)
1587 memset (m_eob, 0, size + (m_idx - m_buf));
1595 for (i = 0; i < size && ! eof (); i++)
1596 *buffer++ = get_undelim ();
1598 memset (buffer, 0, size - i);
1610delimited_stream::getline (std::string& out,
char delim)
1612 int len = out.length ();
1615 while ((ch = get_undelim ()) != delim
1616 && ch != std::istream::traits_type::eof ())
1633class textscan_format_elt
1637 enum special_conversion
1639 whitespace_conversion = 1,
1640 literal_conversion = 2
1643 textscan_format_elt () =
delete;
1645 textscan_format_elt (
const std::string& txt,
int w = 0,
int p = -1,
1646 int bw = 0,
bool dis =
false,
char typ =
'\0',
1647 const std::string& ch_class = std::string ())
1648 : text (txt), width (
w), prec (p), bitwidth (bw),
1649 char_class (ch_class), type (typ), discard (dis),
1650 numeric (typ ==
'd' || typ ==
'u' || type ==
'f' || type ==
'n')
1653 OCTAVE_DEFAULT_COPY_MOVE_DELETE (textscan_format_elt)
1670 std::string char_class;
1687class textscan_format_list
1691 textscan_format_list (
const std::string& fmt = std::string (),
1692 const std::string& who =
"textscan");
1693 OCTAVE_DISABLE_COPY_MOVE (textscan_format_list)
1695 ~textscan_format_list ();
1704 std::size_t
numel ()
const {
return m_fmt_elts.size (); }
1706 const textscan_format_elt * first ()
1712 const textscan_format_elt * current ()
const
1714 return numel () > 0 ? m_fmt_elts[m_curr_idx] :
nullptr;
1717 const textscan_format_elt * next (
bool cycle =
true)
1721 if (m_curr_idx >= numel ())
1732 void printme ()
const;
1734 bool ok ()
const {
return (m_nconv >= 0); }
1736 operator const void *()
const {
return ok () ? this :
nullptr; }
1742 bool set_from_first;
1747 int read_first_row (delimited_stream& is, textscan& ts);
1749 std::list<octave_value> out_buf ()
const
1750 {
return (m_output_container); }
1754 void add_elt_to_list (
unsigned int width,
int prec,
int bitwidth,
1757 const std::string& char_class = std::string ());
1759 void process_conversion (
const std::string& s, std::size_t& i,
1762 std::string parse_char_class (
const std::string& pattern)
const;
1764 int finish_conversion (
const std::string& s, std::size_t& i, std::size_t n,
1765 unsigned int width,
int prec,
int bitwidth,
1767 bool discard,
char& type);
1776 std::size_t m_curr_idx;
1779 std::deque<textscan_format_elt *> m_fmt_elts;
1782 std::list<octave_value> m_output_container;
1785 std::ostringstream m_buf;
1797class OCTINTERP_API textscan
1801 textscan (
const std::string& who_arg =
"textscan",
1802 const std::string& encoding =
"utf-8");
1804 OCTAVE_DISABLE_COPY_MOVE (textscan)
1806 ~textscan () =
default;
1808 octave_value scan (std::istream& isp,
const std::string& fmt,
1815 friend class textscan_format_list;
1817 octave_value do_scan (std::istream& isp, textscan_format_list& fmt_list,
1821 textscan_format_list& fmt_list);
1823 int read_format_once (delimited_stream& isp, textscan_format_list& fmt_list,
1824 std::list<octave_value>& retval,
1827 void scan_one (delimited_stream& is,
const textscan_format_elt& fmt,
1831 double read_double (delimited_stream& is,
1832 const textscan_format_elt& fmt)
const;
1834 void scan_complex (delimited_stream& is,
const textscan_format_elt& fmt,
1837 int scan_bracket (delimited_stream& is,
const std::string& pattern,
1838 std::string& val)
const;
1840 int scan_caret (delimited_stream& is,
const std::string& pattern,
1841 std::string& val)
const;
1843 void scan_string (delimited_stream& is,
const textscan_format_elt& fmt,
1844 std::string& val)
const;
1846 void scan_cstring (delimited_stream& is,
const textscan_format_elt& fmt,
1847 std::string& val)
const;
1849 void scan_qstring (delimited_stream& is,
const textscan_format_elt& fmt,
1853 std::string read_until (delimited_stream& is,
const Cell& delimiters,
1854 const std::string& ends)
const;
1856 int lookahead (delimited_stream& is,
const Cell& targets,
int max_len,
1857 bool case_sensitive =
true)
const;
1859 bool match_literal (delimited_stream& isp,
const textscan_format_elt& elem);
1861 int skip_whitespace (delimited_stream& is,
bool EOLstop =
true);
1863 int skip_delim (delimited_stream& is);
1865 bool is_delim (
unsigned char ch)
const
1867 return ((m_delim_table.empty ()
1868 && (isspace (ch) || ch == m_eol1 || ch == m_eol2))
1869 || m_delim_table[ch] !=
'\0');
1872 bool isspace (
unsigned int ch)
const
1873 {
return m_whitespace_table[ch & 0xff]; }
1876 bool whitespace_delim ()
const {
return m_delim_table.empty (); }
1883 std::string m_encoding;
1892 std::string m_whitespace_table;
1895 std::string m_delim_table;
1898 std::string m_delims;
1900 Cell m_comment_style;
1910 std::string m_date_locale;
1922 std::string m_exp_chars;
1924 Cell m_treat_as_empty;
1927 int m_treat_as_empty_len;
1929 std::string m_whitespace;
1933 short m_return_on_error;
1935 bool m_collect_output;
1936 bool m_multiple_delims_as_one;
1942textscan_format_list::textscan_format_list (
const std::string& s,
1943 const std::string& who_arg)
1944 : who (who_arg), set_from_first (false), has_string (false),
1945 m_nconv (0), m_curr_idx (0), m_fmt_elts (), m_buf ()
1947 std::size_t n = s.length ();
1951 unsigned int width = -1;
1954 bool discard =
false;
1957 bool have_more =
true;
1971 set_from_first =
true;
1976 set_from_first =
false;
1982 if (s[i] ==
'%' && (i+1 == n || s[i+1] !=
'%'))
1986 process_conversion (s, i, n);
1994 have_more = (m_buf.tellp () != 0);
1996 else if (isspace (s[i]))
1998 while (++i < n && isspace (s[i]))
2005 type = textscan_format_elt::literal_conversion;
2012 while (i < n && ! isspace (s[i])
2013 && (s[i] !=
'%' || (i+1 < n && s[i+1] ==
'%')))
2021 add_elt_to_list (width, prec, bitwidth,
octave_value (),
2036 add_elt_to_list (width, prec, bitwidth,
octave_value (), discard, type);
2042textscan_format_list::~textscan_format_list ()
2044 std::size_t n =
numel ();
2046 for (std::size_t i = 0; i < n; i++)
2048 textscan_format_elt *elt = m_fmt_elts[i];
2054textscan_format_list::add_elt_to_list (
unsigned int width,
int prec,
2056 bool discard,
char type,
2057 const std::string& char_class)
2059 std::string text = m_buf.str ();
2061 if (! text.empty ())
2063 textscan_format_elt *elt
2064 =
new textscan_format_elt (text, width, prec, bitwidth, discard,
2068 m_output_container.push_back (val_type);
2070 m_fmt_elts.push_back (elt);
2078textscan_format_list::process_conversion (
const std::string& s,
2079 std::size_t& i, std::size_t n)
2084 bool discard =
false;
2090 bool have_width =
false;
2106 case '0':
case '1':
case '2':
case '3':
case '4':
2107 case '5':
case '6':
case '7':
case '8':
case '9':
2113 width = width * 10 + c -
'0';
2116 while (i < n && isdigit (s[i]))
2119 width = width * 10 + c -
'0';
2123 if (i < n && s[i] ==
'.')
2127 while (i < n && isdigit (s[i]))
2130 prec = prec * 10 + c -
'0';
2140 m_buf << (type = s[i++]);
2152 else if (s[i] ==
'1' && i+1 < n && s[i+1] ==
'6')
2162 else if (s[i] ==
'3' && i+1 < n && s[i+1] ==
'2')
2168 else if (s[i] ==
'6' && i+1 < n && s[i+1] ==
'4')
2196 m_buf << (type = s[i++]);
2200 if (s[i] ==
'3' && i+1 < n && s[i+1] ==
'2')
2207 else if (s[i] ==
'6' && i+1 < n && s[i+1] ==
'4')
2221 m_buf << (type = s[i++]);
2226 case 's':
case 'q':
case '[':
case 'c':
2229 m_buf << (type = s[i++]);
2240 width =
static_cast<unsigned int> (-1);
2243 if (finish_conversion (s, i, n, width, prec, bitwidth, val_type,
2244 discard, type) == 0)
2250 error (
"%s: '%%%c' is not a valid format specifier",
2251 who.c_str (), s[i]);
2274textscan_format_list::parse_char_class (
const std::string& pattern)
const
2276 int len = pattern.length ();
2280 std::string retval (256,
'\0');
2281 std::string mask (256,
'\0');
2283 int in = 0, out = 0;
2284 unsigned char ch, prev = 0;
2293 mask[pattern[in]] =
'\1';
2294 retval[out++] = pattern[in++];
2296 bool prev_was_range =
false;
2297 bool prev_prev_was_range =
false;
2298 for (; in <
len; in++)
2300 bool was_range =
false;
2305 if (prev ==
'-' && in > 1 && isalnum (ch) && ! prev_prev_was_range)
2307 unsigned char start_of_range = pattern[in-2];
2308 if (start_of_range < ch
2309 && ((isupper (ch) && isupper (start_of_range))
2310 || (islower (ch) && islower (start_of_range))
2311 || (isdigit (ch) && isdigit (start_of_range))
2317 for (
int i = start_of_range; i <= ch; i++)
2319 if (mask[i] ==
'\0')
2329 if (mask[ch]++ == 0)
2333 "%s: [...] contains two '%c's",
2336 if (prev ==
'-' && mask[
'-'] >= 2)
2338 (
"Octave:textscan-pattern",
2339 "%s: [...] contains two '-'s outside range expressions",
2343 prev_prev_was_range = prev_was_range;
2344 prev_was_range = was_range;
2350 for (
int i = 0; i < 256; i++)
2355 retval.resize (out);
2361textscan_format_list::finish_conversion (
const std::string& s, std::size_t& i,
2362 std::size_t n,
unsigned int width,
2363 int prec,
int bitwidth,
2369 std::string char_class;
2371 std::size_t beg_idx = std::string::npos;
2372 std::size_t end_idx = std::string::npos;
2396 else if (s[i] ==
']')
2400 while (i < n && s[i] !=
']')
2403 if (i < n && s[i] ==
']')
2410 retval = m_nconv = -1;
2416 if (beg_idx != std::string::npos && end_idx != std::string::npos)
2417 char_class = parse_char_class (s.substr (beg_idx,
2418 end_idx - beg_idx + 1));
2420 add_elt_to_list (width, prec, bitwidth, val_type, discard, type,
2428textscan_format_list::printme ()
const
2430 std::size_t n =
numel ();
2432 for (std::size_t i = 0; i < n; i++)
2434 textscan_format_elt *elt = m_fmt_elts[i];
2437 <<
"width: " << elt->width <<
"\n"
2438 <<
"digits " << elt->prec <<
"\n"
2439 <<
"bitwidth: " << elt->bitwidth <<
"\n"
2440 <<
"discard: " << elt->discard <<
"\n"
2443 if (elt->type == textscan_format_elt::literal_conversion)
2444 std::cerr <<
"literal text\n";
2445 else if (elt->type == textscan_format_elt::whitespace_conversion)
2446 std::cerr <<
"whitespace\n";
2448 std::cerr << elt->type <<
"\n";
2460textscan_format_list::read_first_row (delimited_stream& is, textscan& ts)
2463 std::string first_line (20,
' ');
2465 is.getline (first_line,
static_cast<char> (ts.m_eol2));
2467 if (! first_line.empty () && first_line.back () == ts.m_eol1)
2468 first_line.pop_back ();
2470 std::istringstream strstr (first_line);
2471 delimited_stream ds (strstr, is);
2477 int max_empty = 1000;
2483 bool already_skipped_delim =
false;
2484 ts.skip_whitespace (ds,
false);
2485 ds.progress_benchmark ();
2486 ts.scan_complex (ds, *m_fmt_elts[0], val);
2489 ds.clear (ds.rdstate () & ~std::ios::failbit);
2497 if (ds.no_progress ())
2502 already_skipped_delim =
true;
2504 val = ts.m_empty_value.scalar_value ();
2510 if (val.imag () == 0)
2515 m_output_container.push_back (val_type);
2517 if (! already_skipped_delim)
2520 if (ds.no_progress ())
2526 m_output_container.pop_front ();
2530 m_fmt_elts.push_back (
new textscan_format_elt (*m_fmt_elts[0]));
2535textscan::textscan (
const std::string& who_arg,
const std::string& encoding)
2536 : m_who (who_arg), m_encoding (encoding), m_buf (), m_whitespace_table (),
2537 m_delim_table (), m_delims (), m_comment_style (), m_comment_len (0),
2538 m_comment_char (-2), m_buffer_size (0), m_date_locale (),
2539 m_inf_nan (init_inf_nan ()),
2540 m_empty_value (numeric_limits<
double>::
NaN ()),
2541 m_exp_chars (
"edED"), m_header_lines (0), m_treat_as_empty (),
2542 m_treat_as_empty_len (0), m_whitespace (
" \b\t"), m_eol1 (
'\r'),
2543 m_eol2 (
'\n'), m_return_on_error (1), m_collect_output (false),
2544 m_multiple_delims_as_one (false), m_default_exp (true), m_lines (0)
2548textscan::scan (std::istream& isp,
const std::string& fmt,
2552 textscan_format_list fmt_list (fmt);
2554 parse_options (options, fmt_list);
2562 std::ios::iostate state = isp.rdstate ();
2565 isp.setstate (state);
2571textscan::do_scan (std::istream& isp, textscan_format_list& fmt_list,
2576 if (fmt_list.num_conversions () == -1)
2577 error (
"%s: invalid format specified", m_who.c_str ());
2579 if (fmt_list.num_conversions () == 0)
2580 error (
"%s: no valid format conversion specifiers", m_who.c_str ());
2584 for (
int i = 0; i < m_header_lines && isp; i++)
2585 getline (isp, dummy,
static_cast<char> (m_eol2));
2590 int max_lookahead = std::max ({m_comment_len, m_treat_as_empty_len,
2596 buf_size = m_buffer_size;
2597 else if (ntimes > 0)
2600 buf_size = std::min (buf_size, std::max (ntimes, 80 * ntimes));
2601 buf_size = std::max (buf_size, ntimes);
2604 delimited_stream is (isp,
2605 (m_delims.empty () ? m_whitespace +
"\r\n"
2607 max_lookahead, buf_size);
2618 if (m_multiple_delims_as_one)
2624 if (fmt_list.set_from_first)
2626 err = fmt_list.read_first_row (is, *
this);
2629 done_after = fmt_list.numel () + 1;
2634 done_after = fmt_list.out_buf ().size () + 1;
2636 std::list<octave_value> out = fmt_list.out_buf ();
2644 std::vector<bool> merge_with_prev (fmt_list.numel ());
2646 if (m_collect_output)
2649 for (
const auto& col : out)
2651 if (col.type_id () == prev_type
2652 || (fmt_list.set_from_first && prev_type != -1))
2653 merge_with_prev[conv++] =
true;
2655 merge_with_prev[conv++] =
false;
2657 prev_type = col.type_id ();
2663 if (fmt_list.num_conversions () == 0)
2664 error (
"%s: No conversions specified", m_who.c_str ());
2670 row < ntimes || ntimes == -1;
2673 if (row == 0 || row >= size)
2676 for (
auto& col : out)
2681 err = read_format_once (is, fmt_list, out, row_idx, done_after);
2683 if ((err & ~1) > 0 || ! is || (m_lines >= ntimes && ntimes > -1))
2688 if ((err & 4) && ! m_return_on_error)
2689 error (
"%s: Read error in field %d of row %" OCTAVE_IDX_TYPE_FORMAT,
2690 m_who.c_str (), done_after + 1, row + 1);
2693 bool uneven_columns =
false;
2695 uneven_columns =
true;
2696 else if (isp.eof ())
2699 isp.seekg (-1, std::ios_base::end);
2700 int last_char = isp.get ();
2701 isp.setstate (isp.eofbit);
2702 uneven_columns = (last_char != m_eol1 && last_char != m_eol2);
2711 done_after = out.size () + 1;
2713 int valid_rows = (row == ntimes
2715 : ((err & 1) && (err & 8)) ? row : row+1);
2720 if (! m_collect_output)
2723 for (
auto& col : out)
2726 if (i == done_after && uneven_columns)
2727 dv =
dim_vector (std::max (valid_rows - 1, 0), 1);
2743 for (
auto& col : out)
2745 if (! merge_with_prev[conv++])
2747 if (prev_type != -1)
2754 prev_type = col.type_id ();
2758 ra_idx(1) = group_size++;
2773textscan::read_double (delimited_stream& is,
2774 const textscan_format_elt& fmt)
const
2777 unsigned int width_left = fmt.width;
2781 int ch = is.peek_undelim ();
2786 ch = is.peek_undelim ();
2794 ch = is.peek_undelim ();
2802 if (ch >=
'0' && ch <=
'9')
2804 while (width_left-- && is && (ch = is.get ()) >=
'0' && ch <=
'9')
2805 retval = retval * 10 + (ch -
'0');
2810 if (ch ==
'.' && width_left)
2812 double multiplier = 1;
2813 int precision = fmt.prec;
2818 if (precision == -1)
2824 for (i = 0; i < precision; i++)
2826 if (width_left-- && is && (ch = is.get ()) >=
'0' && ch <=
'9')
2827 retval += (ch -
'0') * (multiplier *= 0.1);
2836 if ((i == precision || ! width_left) && (ch = is.get ()) >=
'5'
2838 retval += multiplier;
2850 while (width_left-- && is && (ch = is.get ()) >=
'0' && ch <=
'9')
2857 bool used_exp =
false;
2858 if (valid && width_left > 1 && m_exp_chars.find (ch) != std::string::npos)
2860 int ch1 = is.peek_undelim ();
2861 if (ch1 ==
'-' || ch1 ==
'+' || (ch1 >=
'0' && ch1 <=
'9'))
2872 else if (ch1 ==
'-')
2879 while (width_left-- && is && (ch = is.get_undelim ()) >=
'0' && ch <=
'9')
2881 exp = exp*10 + ch -
'0';
2885 if (ch != std::istream::traits_type::eof () && width_left)
2888 double multiplier = pown (10, exp);
2890 retval *= multiplier;
2892 retval /= multiplier;
2898 if (! used_exp && ch != std::istream::traits_type::eof () && width_left)
2902 if (! valid && width_left >= 3 && is.remaining () >= 3)
2904 int i = lookahead (is, m_inf_nan, 3,
false);
2907 retval = numeric_limits<double>::Inf ();
2912 retval = numeric_limits<double>::NaN ();
2918 is.setstate (std::ios::failbit);
2920 is.setstate (is.rdstate () & ~std::ios::failbit);
2922 return retval * sign;
2930textscan::scan_complex (delimited_stream& is,
const textscan_format_elt& fmt,
2935 bool as_empty =
false;
2938 int ch = is.peek_undelim ();
2939 if (ch ==
'+' || ch ==
'-')
2942 int ch2 = is.peek_undelim ();
2943 if (ch2 ==
'i' || ch2 ==
'j')
2948 if (is.peek_undelim () ==
'n')
2950 char *pos = is.tellg ();
2951 std::ios::iostate state = is.rdstate ();
2954 ch2 = is.get_undelim ();
2958 re = (ch ==
'+' ? numeric_limits<double>::Inf ()
2971 im = (ch ==
'+') ? value : -value;
2979 char *pos = is.tellg ();
2980 std::ios::iostate state = is.rdstate ();
2983 re = read_double (is, fmt);
2986 if (m_treat_as_empty.numel ()
2987 && (is.fail () || math::is_NaN_or_NA (
Complex (re))
2988 || re == numeric_limits<double>::Inf ()))
2991 for (
int i = 0; i < m_treat_as_empty.numel (); i++)
2993 if (ch == m_treat_as_empty (i).string_value ()[0])
3009 std::string look_buf (m_treat_as_empty_len,
'\0');
3010 char *look = is.read (&look_buf[0], look_buf.size (), pos);
3016 for (
int i = 0; i < m_treat_as_empty.numel (); i++)
3018 std::string s = m_treat_as_empty (i).string_value ();
3019 if (!
strncmp (s.c_str (), look, s.size ()))
3023 is.read (&look_buf[0], s.size (), pos);
3030 if (! is.eof () && ! as_empty)
3032 state = is.rdstate ();
3034 ch = is.peek_undelim ();
3036 if (ch ==
'i' || ch ==
'j')
3042 else if (ch ==
'+' || ch ==
'-')
3046 state = is.rdstate ();
3051 im = read_double (is, fmt);
3055 if (is.peek_undelim () ==
'i' || is.peek_undelim () ==
'j')
3069 val = m_empty_value.scalar_value ();
3077textscan::scan_caret (delimited_stream& is,
const std::string& pattern,
3078 std::string& val)
const
3080 int c1 = std::istream::traits_type::eof ();
3081 std::ostringstream obuf;
3083 while (is && ((c1 = (is && ! is.eof ())
3085 : std::
istream::traits_type::eof ())
3086 != std::
istream::traits_type::eof ())
3087 && pattern.find (c1) == std::string::npos)
3088 obuf << static_cast<char> (c1);
3092 if (c1 != std::istream::traits_type::eof ())
3102textscan::read_until (delimited_stream& is,
const Cell& delimiters,
3103 const std::string& ends)
const
3105 std::string retval (
"");
3111 scan_caret (is, ends.c_str (), next);
3112 retval = retval + next;
3114 int last = (! is.eof ()
3115 ? is.get_undelim () : std::istream::traits_type::eof ());
3117 if (last != std::istream::traits_type::eof ())
3119 if (last == m_eol1 || last == m_eol2)
3122 retval = retval +
static_cast<char> (last);
3123 for (
int i = 0; i < delimiters.
numel (); i++)
3125 std::string delim = delimiters(i).string_value ();
3126 std::size_t start = (retval.length () > delim.length ()
3127 ? retval.length () - delim.length ()
3129 std::string may_match = retval.substr (start);
3130 if (may_match == delim)
3133 retval = retval.substr (0, start);
3141 while (! done && is && ! is.eof ());
3151textscan::scan_string (delimited_stream& is,
const textscan_format_elt& fmt,
3152 std::string& val)
const
3154 if (m_delim_list.isempty ())
3157 unsigned int width = fmt.width;
3159 for (i = 0; i < width; i++)
3162 if (i >= val.length ())
3163 val.append (std::max (val.length (),
3164 static_cast<std::size_t
> (16)),
'\0');
3166 int ch = is.get_undelim ();
3167 if (is_delim (ch) || ch == std::istream::traits_type::eof ())
3175 val = val.substr (0, i);
3179 std::string ends (m_delim_list.numel () + 2,
'\0');
3181 for (i = 0; i < m_delim_list.numel (); i++)
3183 std::string tmp = m_delim_list(i).string_value ();
3184 ends[i] = tmp.back ();
3188 val = textscan::read_until (is, m_delim_list, ends);
3192 if (m_encoding.compare (
"utf-8"))
3193 val = string::u8_from_encoding (
"textscan", val, m_encoding);
3199textscan::scan_bracket (delimited_stream& is,
const std::string& pattern,
3200 std::string& val)
const
3202 int c1 = std::istream::traits_type::eof ();
3203 std::ostringstream obuf;
3205 while (is && pattern.find (c1 = is.get_undelim ()) != std::string::npos)
3206 obuf << static_cast<char> (c1);
3209 if (c1 != std::istream::traits_type::eof ())
3219textscan::scan_qstring (delimited_stream& is,
const textscan_format_elt& fmt,
3222 skip_whitespace (is);
3224 if (is.peek_undelim () !=
'"')
3225 scan_string (is, fmt, val);
3229 scan_caret (is, R
"(")", val);
3232 while (is && is.peek_undelim () ==
'"')
3237 scan_caret (is, R
"(")", val1);
3238 val = val + '"' + val1;
3244 if (m_encoding.compare (
"utf-8"))
3245 val = string::u8_from_encoding (
"textscan", val, m_encoding);
3252textscan::scan_cstring (delimited_stream& is,
const textscan_format_elt& fmt,
3253 std::string& val)
const
3255 val.resize (fmt.width);
3257 for (
unsigned int i = 0; is && i < fmt.width; i++)
3259 int ch = is.get_undelim ();
3260 if (ch != std::istream::traits_type::eof ())
3270 if (m_encoding.compare (
"utf-8"))
3271 val = string::u8_from_encoding (
"textscan", val, m_encoding);
3277textscan::scan_one (delimited_stream& is,
const textscan_format_elt& fmt,
3280 skip_whitespace (is);
3287 if (fmt.type ==
'f' || fmt.type ==
'n')
3290 skip_whitespace (is);
3291 scan_complex (is, fmt, v);
3293 if (! fmt.discard && ! is.fail ())
3295 if (fmt.bitwidth == 64)
3297 if (ov.
isreal () && v.imag () == 0)
3309 if (ov.
isreal () && v.imag () == 0)
3328 skip_whitespace (is);
3329 v = read_double (is, fmt);
3330 if (! fmt.discard && ! is.fail ())
3331 switch (fmt.bitwidth)
3372 if (fmt.type ==
'd')
3385 if (fmt.type ==
'd')
3399 if (is.fail () & ! fmt.discard)
3400 ov =
cat_op (ov, m_empty_value, row);
3404 std::string vv (
" ");
3408 scan_string (is, fmt, vv);
3412 scan_qstring (is, fmt, vv);
3416 scan_cstring (is, fmt, vv);
3420 scan_bracket (is, fmt.char_class.c_str (), vv);
3424 scan_caret (is, fmt.char_class.c_str (), vv);
3434 is.clear (is.rdstate () & ~std::ios_base::failbit);
3444textscan::read_format_once (delimited_stream& is,
3445 textscan_format_list& fmt_list,
3446 std::list<octave_value>& retval,
3449 const textscan_format_elt *elem = fmt_list.first ();
3450 auto out = retval.begin ();
3451 bool no_conversions =
true;
3453 bool conversion_failed =
false;
3454 bool nothing_worked =
true;
3458 for (std::size_t i = 0; i < fmt_list.numel (); i++)
3460 bool this_conversion_failed =
false;
3469 warning (
"%s: conversion %c not yet implemented",
3470 m_who.c_str (), elem->type);
3482 scan_one (is, *elem, *out, row);
3485 case textscan_format_elt::literal_conversion :
3486 match_literal (is, *elem);
3490 error (
"Unknown format element '%c'", elem->type);
3495 if (! elem->discard)
3496 no_conversions =
false;
3500 is.clear (is.rdstate () & ~std::ios::failbit);
3504 if (m_delim_list.isempty ())
3506 if (! is_delim (is.peek_undelim ()))
3507 this_conversion_failed =
true;
3511 char *pos = is.tellg ();
3512 if (-1 == lookahead (is, m_delim_list, m_delim_len))
3513 this_conversion_failed =
true;
3520 if (! elem->discard)
3523 elem = fmt_list.next ();
3524 char *pos = is.tellg ();
3529 if (elem->type != textscan_format_elt::literal_conversion)
3531 else if (! is_delim (elem->text[0]))
3543 if (this_conversion_failed)
3545 if (is.tellg () == pos && ! conversion_failed)
3549 conversion_failed =
true;
3552 this_conversion_failed =
false;
3554 else if (! done && ! conversion_failed)
3555 nothing_worked =
false;
3559 is.setstate (std::ios::eofbit);
3561 return no_conversions
3562 + (is.eof () ? 2 : 0)
3563 + (conversion_failed ? 4 : 0)
3564 + (nothing_worked ? 8 : 0);
3570 textscan_format_list& fmt_list)
3572 int last = args.
length ();
3576 error (
"%s: %d parameters given, but only %d values",
3577 m_who.c_str (), n-n/2, n/2);
3580 bool have_delims =
false;
3581 for (
int i = 0; i < last; i += 2)
3583 std::string param = args(i).xstring_value (
"%s: Invalid parameter type <%s> for parameter %d",
3585 args(i).type_name ().c_str (),
3587 std::transform (param.begin (), param.end (), param.begin (), ::tolower);
3589 if (param ==
"delimiter")
3591 bool invalid =
true;
3592 if (args(i+1).is_string ())
3596 m_delims = args(i+1).string_value ();
3597 if (args(i+1).is_sq_string ())
3600 else if (args(i+1).iscell ())
3604 m_delim_table =
" ";
3607 for (
int j = 0; j < m_delim_list.numel (); j++)
3609 if (! m_delim_list(j).is_string ())
3613 if (m_delim_list(j).is_sq_string ())
3618 m_delim_len = std::max (
static_cast<int> (
len),
3624 error (
"%s: Delimiters must be either a string or cell array of strings",
3627 else if (param ==
"commentstyle")
3629 if (args(i+1).is_string ())
3632 m_comment_style =
Cell (args(i+1));
3634 else if (args(i+1).iscell ())
3637 int len = m_comment_style.
numel ();
3638 if ((
len >= 1 && ! m_comment_style (0).is_string ())
3639 || (
len >= 2 && ! m_comment_style (1).is_string ())
3641 error (
"%s: CommentStyle must be either a string or cell array of one or two strings",
3645 error (
"%s: CommentStyle must be either a string or cell array of one or two strings",
3650 if (m_comment_style.numel () >= 1)
3652 m_comment_len = m_comment_style (0).string_value ().size ();
3653 m_comment_char = m_comment_style (0).string_value ()[0];
3656 else if (param ==
"treatasempty")
3658 bool invalid =
false;
3659 if (args(i+1).is_string ())
3661 m_treat_as_empty =
Cell (args(i+1));
3662 m_treat_as_empty_len = args(i+1).string_value ().size ();
3664 else if (args(i+1).iscell ())
3667 for (
int j = 0; j < m_treat_as_empty.numel (); j++)
3668 if (! m_treat_as_empty (j).is_string ())
3672 int k = m_treat_as_empty (j).string_value ().size ();
3673 if (k > m_treat_as_empty_len)
3674 m_treat_as_empty_len = k;
3678 error (
"%s: TreatAsEmpty must be either a string or cell array of one or two strings",
3683 else if (param ==
"collectoutput")
3685 m_collect_output = args(i+1).strict_bool_value (
"%s: CollectOutput must be logical or numeric", m_who.c_str ());
3687 else if (param ==
"emptyvalue")
3689 m_empty_value = args(i+1).xscalar_value (
"%s: EmptyValue must be numeric", m_who.c_str ());
3691 else if (param ==
"headerlines")
3693 m_header_lines = args(i+1).xscalar_value (
"%s: HeaderLines must be numeric", m_who.c_str ());
3695 else if (param ==
"bufsize")
3697 m_buffer_size = args(i+1).xscalar_value (
"%s: BufSize must be numeric", m_who.c_str ());
3699 else if (param ==
"multipledelimsasone")
3701 m_multiple_delims_as_one = args(i+1).strict_bool_value (
"%s: MultipleDelimsAsOne must be logical or numeric", m_who.c_str ());
3703 else if (param ==
"returnonerror")
3705 m_return_on_error = args(i+1).strict_bool_value (
"%s: ReturnOnError must be logical or numeric", m_who.c_str ());
3707 else if (param ==
"whitespace")
3709 m_whitespace = args(i+1).xstring_value (
"%s: Whitespace must be a character string", m_who.c_str ());
3711 else if (param ==
"expchars")
3713 m_exp_chars = args(i+1).xstring_value (
"%s: ExpChars must be a character string", m_who.c_str ());
3714 m_default_exp =
false;
3716 else if (param ==
"endofline")
3719 std::string s = args(i+1).xstring_value (R
"(%s: EndOfLine must be at most one character or '\r\n')",
3721 if (args(i+1).is_sq_string ())
3723 int l = s.length ();
3725 m_eol1 = m_eol2 = -2;
3727 m_eol1 = m_eol2 = s.c_str ()[0];
3730 m_eol1 = s.c_str ()[0];
3731 m_eol2 = s.c_str ()[1];
3732 if (m_eol1 !=
'\r' || m_eol2 !=
'\n')
3739 error (R
"(%s: EndOfLine must be at most one character or '\r\n')",
3743 error (
"%s: unrecognized option '%s'", m_who.c_str (), param.c_str ());
3747 for (
unsigned int j = 0; j < m_delims.length (); j++)
3749 m_whitespace.erase (std::remove (m_whitespace.begin (),
3750 m_whitespace.end (),
3752 m_whitespace.end ());
3754 for (
int j = 0; j < m_delim_list.numel (); j++)
3756 std::string delim = m_delim_list(j).string_value ();
3757 if (delim.length () == 1)
3758 m_whitespace.erase (std::remove (m_whitespace.begin (),
3759 m_whitespace.end (),
3761 m_whitespace.end ());
3764 m_whitespace_table = std::string (256,
'\0');
3765 for (
unsigned int i = 0; i < m_whitespace.length (); i++)
3766 m_whitespace_table[m_whitespace[i]] =
'1';
3770 if (! (m_whitespace.empty () && fmt_list.has_string))
3771 m_whitespace_table[
' '] =
'1';
3774 m_delim_table = std::string (256,
'\0');
3775 if (m_eol1 >= 0 && m_eol1 < 256)
3776 m_delim_table[m_eol1] =
'1';
3777 if (m_eol2 >= 0 && m_eol2 < 256)
3778 m_delim_table[m_eol2] =
'1';
3780 for (
unsigned int i = 0; i < 256; i++)
3783 m_delim_table[i] =
'1';
3786 for (
unsigned int i = 0; i < m_delims.length (); i++)
3787 m_delim_table[m_delims[i]] =
'1';
3794textscan::skip_whitespace (delimited_stream& is,
bool EOLstop)
3796 int c1 = std::istream::traits_type::eof ();
3797 bool found_comment =
false;
3801 found_comment =
false;
3804 && (c1 = is.get_undelim ()) != std::istream::traits_type::eof ()
3805 && ( ( (c1 == m_eol1 || c1 == m_eol2) && ++m_lines && ! EOLstop)
3808 if (prev == m_eol1 && m_eol1 != m_eol2 && c1 == m_eol2)
3813 if (c1 == m_comment_char)
3816 char *pos = is.tellg ();
3817 std::ios::iostate state = is.rdstate ();
3819 std::string tmp (m_comment_len,
'\0');
3820 char *look = is.read (&tmp[0], m_comment_len-1, pos);
3821 if (is && m_comment_style.numel () > 0
3822 && !
strncmp (m_comment_style(0).string_value ().substr (1).c_str (),
3823 look, m_comment_len-1))
3825 found_comment =
true;
3828 if (m_comment_style.numel () == 1)
3830 std::string eol (3,
'\0');
3834 scan_caret (is, eol, dummy);
3835 c1 = is.get_undelim ();
3836 if (c1 == m_eol1 && m_eol1 != m_eol2
3837 && is.peek_undelim () == m_eol2)
3843 std::string end_c = m_comment_style(1).string_value ();
3845 std::string last = end_c.substr (end_c.size () - 1);
3846 std::string may_match (
"");
3850 scan_caret (is, last, dummy);
3853 may_match = may_match + dummy + last;
3854 if (may_match.length () > end_c.length ())
3856 std::size_t start = may_match.length ()
3858 may_match = may_match.substr (start);
3861 while (may_match != end_c && is && ! is.eof ());
3871 while (found_comment);
3873 if (c1 != std::istream::traits_type::eof ())
3884textscan::lookahead (delimited_stream& is,
const Cell& targets,
int max_len,
3885 bool case_sensitive)
const
3891 char *pos = is.tellg ();
3893 std::string tmp (max_len,
'\0');
3894 char *look = is.read (&tmp[0], tmp.size (), pos);
3901 int (*compare)(
const char *,
const char *, std::size_t);
3904 for (i = 0; i < targets.
numel (); i++)
3906 std::string s = targets (i).string_value ();
3907 if (! (*compare) (s.c_str (), look, s.size ()))
3909 is.read (&tmp[0], s.size (), pos);
3914 if (i == targets.
numel ())
3922textscan::skip_delim (delimited_stream& is)
3924 int c1 = skip_whitespace (is);
3925 if (m_delim_list.numel () == 0)
3927 if (is_delim (c1) || c1 == m_eol1 || c1 == m_eol2)
3930 if (c1 == m_eol1 && is.peek_undelim () == m_eol2)
3933 if (m_multiple_delims_as_one)
3939 while (is && ((c1 = is.get_undelim ())
3940 != std::istream::traits_type::eof ())
3941 && (((c1 == m_eol1 || c1 == m_eol2) && ++m_lines)
3942 || isspace (c1) || is_delim (c1)))
3944 if (prev == m_eol1 && m_eol1 != m_eol2 && c1 == m_eol2)
3948 if (c1 != std::istream::traits_type::eof ())
3957 if (c1 == m_eol1 || c1 == m_eol2
3958 || (-1 != (first_match = lookahead (is, m_delim_list, m_delim_len))))
3963 if (is.peek_undelim () == m_eol2)
3966 else if (c1 == m_eol2)
3971 if (m_multiple_delims_as_one)
3977 while (is && ((c1 = skip_whitespace (is))
3978 != std::istream::traits_type::eof ())
3979 && (((c1 == m_eol1 || c1 == m_eol2) && ++m_lines)
3980 || -1 != lookahead (is, m_delim_list, m_delim_len)))
3982 if (prev == m_eol1 && m_eol1 != m_eol2 && c1 == m_eol2)
3998textscan::match_literal (delimited_stream& is,
3999 const textscan_format_elt& fmt)
4003 skip_whitespace (is,
false);
4005 for (
unsigned int i = 0; i < fmt.width; i++)
4007 int ch = is.get_undelim ();
4008 if (ch != fmt.text[i])
4010 if (ch != std::istream::traits_type::eof ())
4012 is.setstate (std::ios::failbit);
4030 m_errmsg = who +
": " + msg;
4058 bool strip_newline,
const std::string& who)
4063 ::error (
"%s: unable to read from stdin while running interactively",
4075 invalid_operation (who,
"reading");
4079 std::istream& is = *isp;
4081 std::ostringstream buf;
4088 while (is && (c = is.get ()) != std::istream::traits_type::eof ())
4095 if (! strip_newline)
4096 buf << static_cast<char> (c);
4100 if (c != std::istream::traits_type::eof ())
4106 if (! strip_newline)
4107 buf << static_cast<char> (c);
4117 if (! strip_newline)
4118 buf << static_cast<char> (c);
4123 buf << static_cast<char> (c);
4125 if (max_len > 0 && char_count == max_len)
4130 if (! is.eof () && char_count > 0)
4135 int disgusting_compatibility_hack = is.get ();
4137 is.putback (disgusting_compatibility_hack);
4140 if (is.good () || (is.eof () && char_count > 0))
4142 retval = buf.str ();
4144 retval = string::u8_from_encoding (who, retval,
encoding ());
4150 if (is.eof () && char_count == 0)
4151 error (who,
"at end of file");
4153 error (who,
"read error");
4162 const std::string& who)
4164 return do_gets (max_len, err,
true, who);
4169 const std::string& who)
4171 return do_gets (max_len, err,
false, who);
4175base_stream::skipl (off_t num,
bool& err,
const std::string& who)
4180 ::error (
"%s: unable to read from stdin while running interactively",
4192 invalid_operation (who,
"reading");
4196 std::istream& is = *isp;
4202 while (is && (c = is.get ()) != std::istream::traits_type::eof ())
4205 if (c ==
'\r' || (c ==
'\n' && lastc !=
'\r'))
4215 if (c ==
'\r' && is.peek () ==
'\n')
4221 error (who,
"read error");
4231template <
typename T>
4233octave_scan_1 (std::istream& is,
const scanf_format_elt& fmt,
4239 std::streampos pos = is.tellg ();
4244 is >> std::oct >> value >> std::dec;
4249 is >> std::hex >> value >> std::dec;
4256 if (c1 != std::istream::traits_type::eof ())
4260 int c2 = is.peek ();
4262 if (c2 ==
'x' || c2 ==
'X')
4265 if (std::isxdigit (is.peek ()))
4266 is >> std::hex >> value >> std::dec;
4272 if (c2 ==
'0' || c2 ==
'1' || c2 ==
'2'
4273 || c2 ==
'3' || c2 ==
'4' || c2 ==
'5'
4274 || c2 ==
'6' || c2 ==
'7')
4275 is >> std::oct >> value >> std::dec;
4276 else if (c2 ==
'8' || c2 ==
'9')
4303 std::ios::iostate status = is.rdstate ();
4304 if (! (status & std::ios::failbit))
4318 is.clear (status & ~std::ios::failbit);
4327 is.setstate (status & ~std::ios_base::eofbit);
4334template <
typename T>
4336octave_scan (std::istream& is,
const scanf_format_elt& fmt, T *valptr)
4344 auto orig_pos = is.tellg ();
4346 is.width (fmt.width);
4349 std::istringstream ss (strbuf);
4351 octave_scan_1 (ss, fmt, valptr);
4362 is.seekg (orig_pos, is.beg);
4364 int chars_read = ss.tellg ();
4367 is.width (chars_read);
4374 is.setstate (std::ios::failbit);
4378 octave_scan_1 (is, fmt, valptr);
4386(std::istream& is,
const scanf_format_elt& fmt,
double *valptr)
4398 std::streampos pos = is.tellg ();
4400 double value = read_value<double> (is);
4402 std::ios::iostate status = is.rdstate ();
4403 if (! (status & std::ios::failbit))
4414 is.setstate (status & ~std::ios_base::eofbit);
4420 error (
"expecting format type to be one of 'e', 'f', 'g', 'E', or 'G' but found '%c' - please report this bug", fmt.type);
4427template <
typename T>
4429do_scanf_conv (std::istream& is,
const scanf_format_elt& fmt,
4434 octave_scan (is, fmt, valptr);
4439 if (idx == max_size && ! discard)
4444 mval.
resize (nr, max_size / nr, 0.0);
4446 mval.
resize (max_size, 1, 0.0);
4454 data[idx++] = *(valptr);
4459do_scanf_conv (std::istream&,
const scanf_format_elt&,
double *,
4463#define DO_WHITESPACE_CONVERSION() \
4466 int c = std::istream::traits_type::eof (); \
4469 while (is && (c = is.get ()) != std::istream::traits_type::eof () \
4473 if (c == std::istream::traits_type::eof ()) \
4475 is.clear (is.rdstate () & (~std::ios::failbit)); \
4482#define DO_LITERAL_CONVERSION() \
4485 int c = std::istream::traits_type::eof (); \
4487 int n = fmt.length (); \
4490 while (i < n && is \
4491 && (c = is.get ()) != std::istream::traits_type::eof ()) \
4493 if (c == static_cast<unsigned char> (fmt[i])) \
4506 is.setstate (std::ios::failbit); \
4510#define DO_PCT_CONVERSION() \
4513 int c = is.get (); \
4515 if (c != std::istream::traits_type::eof ()) \
4520 is.setstate (std::ios::failbit); \
4524 is.setstate (std::ios::failbit); \
4528#define BEGIN_C_CONVERSION() \
4529 is.unsetf (std::ios::skipws); \
4531 int width = (elt->width ? elt->width : 1); \
4533 std::string tmp (width, '\0'); \
4535 int c = std::istream::traits_type::eof (); \
4538 while (is && n < width \
4539 && (c = is.get ()) != std::istream::traits_type::eof ()) \
4540 tmp[n++] = static_cast<char> (c); \
4542 if (c == std::istream::traits_type::eof ()) \
4543 is.clear (is.rdstate () & (~std::ios::failbit)); \
4549#define BEGIN_S_CONVERSION() \
4550 int width = elt->width; \
4558 tmp = std::string (width, '\0'); \
4560 int c = std::istream::traits_type::eof (); \
4564 while (is && (c = is.get ()) != std::istream::traits_type::eof ()) \
4566 if (! isspace (c)) \
4568 tmp[n++] = static_cast<char> (c); \
4573 while (is && n < width \
4574 && (c = is.get ()) != std::istream::traits_type::eof ()) \
4582 tmp[n++] = static_cast<char> (c); \
4585 if (c == std::istream::traits_type::eof ()) \
4586 is.clear (is.rdstate () & (~std::ios::failbit)); \
4592 is >> std::ws >> tmp; \
4598#define BEGIN_CHAR_CLASS_CONVERSION() \
4599 int width = (elt->width ? elt->width \
4600 : std::numeric_limits<int>::max ()); \
4606 std::ostringstream buf; \
4608 std::string char_class = elt->char_class; \
4610 int c = std::istream::traits_type::eof (); \
4612 if (elt->type == '[') \
4614 int chars_read = 0; \
4615 while (is && chars_read++ < width \
4616 && (c = is.get ()) != std::istream::traits_type::eof ()) \
4618 if (char_class.find (c) != std::string::npos) \
4619 buf << static_cast<char> (c); \
4629 int chars_read = 0; \
4630 while (is && chars_read++ < width \
4631 && (c = is.get ()) != std::istream::traits_type::eof ()) \
4633 if (char_class.find (c) == std::string::npos) \
4634 buf << static_cast<char> (c); \
4646 is.setstate (std::ios::failbit); \
4647 else if (c == std::istream::traits_type::eof ()) \
4648 is.clear (is.rdstate () & (~std::ios::failbit)); \
4653#define FINISH_CHARACTER_CONVERSION() \
4656 if (encoding ().compare ("utf-8")) \
4657 tmp = string::u8_from_encoding (who, tmp, encoding ()); \
4658 width = tmp.length (); \
4660 if (is && width > 0) \
4666 conversion_count++; \
4670 if (data_index == max_size) \
4674 if (all_char_conv) \
4676 if (one_elt_size_spec) \
4677 mval.resize (1, max_size, 0.0); \
4679 mval.resize (nr, max_size / nr, 0.0); \
4681 error ("unexpected size in character conversion - please report this bug"); \
4684 mval.resize (nr, max_size / nr, 0.0); \
4686 mval.resize (max_size, 1, 0.0); \
4688 data = mval.rwdata (); \
4691 data[data_index++] = static_cast<unsigned char> \
4700base_stream::do_scanf (scanf_format_list& fmt_list,
4702 bool one_elt_size_spec,
4704 const std::string& who)
4709 ::error (
"%s: unable to read from stdin while running interactively",
4714 conversion_count = 0;
4720 if (nr == 0 || nc == 0)
4722 if (one_elt_size_spec)
4725 return Matrix (nr, nc, 0.0);
4730 bool all_char_conv = fmt_list.all_character_conversions ();
4743 if (one_elt_size_spec)
4746 mval.
resize (1, max_size, 0.0);
4755 mval.
resize (nr, nc, 0.0);
4756 max_size = max_conv = nr * nc;
4760 mval.
resize (nr, 32, 0.0);
4765 error (
"unexpected size in character conversion - please report this bug");
4772 mval.
resize (nr, nc, 0.0);
4774 max_conv = max_size;
4779 mval.
resize (nr, 32, 0.0);
4786 mval.
resize (32, 1, 0.0);
4790 double *data = mval.
rwdata ();
4794 std::istream& is = *isp;
4796 const scanf_format_elt *elt = fmt_list.first ();
4798 std::ios::fmtflags flags = is.flags ();
4810 if (elt->type == scanf_format_elt::null
4811 || (! (elt->type == scanf_format_elt::whitespace_conversion
4812 || elt->type == scanf_format_elt::literal_conversion
4813 || elt->type ==
'%')
4814 && max_conv > 0 && conversion_count == max_conv))
4821 if (all_char_conv && one_elt_size_spec)
4824 final_nc = data_index;
4829 final_nc = (data_index - 1) / nr + 1;
4834 else if (data_index == max_size)
4840 if (one_elt_size_spec)
4841 mval.
resize (1, max_size, 0.0);
4843 mval.
resize (nr, max_size / nr, 0.0);
4845 error (
"unexpected size in character conversion - please report this bug");
4848 mval.
resize (nr, max_size / nr, 0.0);
4850 mval.
resize (max_size, 1, 0.0);
4855 std::string fmt = elt->text;
4857 bool discard = elt->discard;
4861 case scanf_format_elt::whitespace_conversion:
4865 case scanf_format_elt::literal_conversion:
4875 switch (elt->modifier)
4880 do_scanf_conv (is, *elt, &tmp, mval, data,
4881 data_index, conversion_count,
4882 nr, max_size, discard);
4889 do_scanf_conv (is, *elt, &tmp, mval, data,
4890 data_index, conversion_count,
4891 nr, max_size, discard);
4898 do_scanf_conv (is, *elt, &tmp, mval, data,
4899 data_index, conversion_count,
4900 nr, max_size, discard);
4907 case 'o':
case 'u':
case 'x':
case 'X':
4909 switch (elt->modifier)
4914 do_scanf_conv (is, *elt, &tmp, mval, data,
4915 data_index, conversion_count,
4916 nr, max_size, discard);
4923 do_scanf_conv (is, *elt, &tmp, mval, data,
4924 data_index, conversion_count,
4925 nr, max_size, discard);
4932 do_scanf_conv (is, *elt, &tmp, mval, data,
4933 data_index, conversion_count,
4934 nr, max_size, discard);
4941 case 'e':
case 'f':
case 'g':
4946 do_scanf_conv (is, *elt, &tmp, mval, data,
4947 data_index, conversion_count,
4948 nr, max_size, discard);
4979 error (who,
"unsupported format specifier");
4983 error (who,
"internal format error");
4991 else if (is.eof () || ! is)
4995 if (one_elt_size_spec)
4998 final_nc = data_index;
5000 else if (data_index > nr)
5003 final_nc = (data_index - 1) / nr + 1;
5007 final_nr = data_index;
5013 if (data_index > nr)
5016 final_nc = (data_index - 1) / nr + 1;
5020 final_nr = data_index;
5026 final_nr = data_index;
5032 if (is.rdstate () & std::ios::failbit)
5034 is.clear (is.rdstate () & (~std::ios::failbit));
5035 error (who,
"format failed to match");
5041 &&
name () ==
"stdin")
5047 do_gets (-1, err,
false, who);
5055 error (who,
"internal format error");
5059 if (m_nconv == 0 && ++trips == num_fmt_elts)
5061 if (all_char_conv && one_elt_size_spec)
5064 final_nc = data_index;
5069 final_nc = (data_index - 1) / nr + 1;
5080 elt = fmt_list.next (m_nconv > 0
5082 || conversion_count < max_conv));
5087 mval.
resize (final_nr, final_nc, 0.0);
5098base_stream::scanf (
const std::string& fmt,
const Array<double>& size,
5100 const std::string& who)
5104 conversion_count = 0;
5109 invalid_operation (who,
"reading");
5112 scanf_format_list fmt_list (fmt);
5114 if (fmt_list.num_conversions () == -1)
5115 ::error (
"%s: invalid format specified", who.c_str ());
5120 bool one_elt_size_spec;
5122 get_size (size, nr, nc, one_elt_size_spec, who);
5124 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec,
5125 conversion_count, who);
5132base_stream::do_oscanf (
const scanf_format_elt *elt,
5142 std::istream& is = *isp;
5144 std::ios::fmtflags flags = is.flags ();
5148 std::string fmt = elt->text;
5150 bool discard = elt->discard;
5154 case scanf_format_elt::whitespace_conversion:
5158 case scanf_format_elt::literal_conversion:
5173 switch (elt->modifier)
5178 if (octave_scan (is, *elt, &tmp))
5191 if (octave_scan (is, *elt, &tmp))
5204 if (octave_scan (is, *elt, &tmp))
5217 case 'o':
case 'u':
case 'x':
case 'X':
5219 switch (elt->modifier)
5224 if (octave_scan (is, *elt, &tmp))
5237 if (octave_scan (is, *elt, &tmp))
5250 if (octave_scan (is, *elt, &tmp))
5263 case 'e':
case 'f':
case 'g':
5268 if (octave_scan (is, *elt, &tmp))
5318 error (who,
"unsupported format specifier");
5322 error (who,
"internal format error");
5327 if (
ok () && is.fail ())
5329 error (
"%s: read error", who.c_str ());
5336 &&
name () ==
"stdin")
5340 do_gets (-1, err,
false, who);
5348base_stream::oscanf (
const std::string& fmt,
const std::string& who)
5355 invalid_operation (who,
"reading");
5358 std::istream& is = *isp;
5360 scanf_format_list fmt_list (fmt);
5365 ::error (
"%s: invalid format specified", who.c_str ());
5373 const scanf_format_elt *elt = fmt_list.first ();
5383 quit = do_oscanf (elt, tmp, who);
5390 retval(num_values++) = tmp;
5395 elt = fmt_list.next (m_nconv > 0);
5399 retval(m_nconv) = num_values;
5402 retval(m_nconv+1) =
error (
false, err_num);
5407 if (
ok () &&
len > m_nconv)
5411 elt = fmt_list.next ();
5413 do_oscanf (elt, tmp, who);
5422base_stream::do_textscan (
const std::string& fmt,
5425 const std::string& who,
5431 ::error (
"%s: unable to read from stdin while running interactively",
5439 invalid_operation (who,
"reading");
5444 retval =
scanner.scan (*isp, fmt, ntimes, options, read_count);
5454base_stream::flush ()
5461 invalid_operation (
"fflush",
"writing");
5473class printf_value_cache
5477 enum state { ok, conversion_error };
5480 : m_values (args), m_val_idx (0), m_elt_idx (0),
5481 m_n_vals (m_values.length ()), m_n_elts (0), m_have_data (false),
5493 OCTAVE_DISABLE_COPY_MOVE (printf_value_cache)
5495 ~printf_value_cache () =
default;
5506 operator bool ()
const {
return (m_curr_state == ok); }
5508 bool exhausted () {
return (m_val_idx >= m_n_vals); }
5513 printf_value_cache ();
5528printf_value_cache::get_next_value (
char type)
5533 m_curr_state = conversion_error;
5535 while (! exhausted ())
5539 m_curr_val = m_values (m_val_idx);
5542 m_n_elts = m_curr_val.numel ();
5546 if (m_elt_idx < m_n_elts)
5550 if (m_curr_val.is_string ())
5557 retval = sval.substr (m_elt_idx);
5560 m_elt_idx = m_n_elts;
5570 for (; idx < m_n_elts; idx++)
5572 double dval = val(idx);
5574 if (math::x_nint (dval) != dval
5575 || dval < 0 || dval > 255)
5583 std::string sval (n,
'\0');
5586 sval[i] = val(m_elt_idx++);
5598 if (type ==
'c' && ! retval.
is_string ())
5602 if (math::x_nint (dval) == dval && dval >= 0 && dval < 256)
5603 retval =
static_cast<char> (dval);
5607 if (m_elt_idx >= m_n_elts)
5611 m_have_data =
false;
5619 m_have_data =
false;
5625 if (type ==
's' || type ==
'c')
5634 m_curr_state = conversion_error;
5643printf_value_cache::int_value ()
5649 if (dval < 0 || dval > std::numeric_limits<int>::max ()
5650 || math::x_nint (dval) != dval)
5652 m_curr_state = conversion_error;
5656 return math::nint (dval);
5661template <
typename T>
5663do_printf_conv (std::ostream& os,
const char *fmt,
int nsa,
int sa_1,
5664 int sa_2, T arg,
const std::string& who)
5671 retval =
format (os, fmt, sa_1, sa_2, arg);
5675 retval =
format (os, fmt, sa_1, arg);
5679 retval =
format (os, fmt, arg);
5683 ::error (
"%s: internal error handling format", who.c_str ());
5691do_printf_string (std::ostream& os,
const printf_format_elt *elt,
5692 int nsa,
int sa_1,
int sa_2,
const std::string& arg,
5693 const std::string& who)
5696 ::error (
"%s: internal error handling format", who.c_str ());
5698 std::string flags = elt->flags;
5700 bool left = flags.find (
'-') != std::string::npos;
5702 std::size_t
len = arg.length ();
5704 std::size_t prec = (nsa > 1 ? sa_2 : (elt->prec == -1 ?
len : elt->prec));
5706 std::string print_str = prec < arg.length () ? arg.substr (0, prec) : arg;
5708 std::size_t fw = (nsa > 0 ? sa_1 : (elt->fw == -1 ?
len : elt->fw));
5710 os << std::setw (fw) << (left ? std::left : std::right) << print_str;
5712 return len > fw ?
len : fw;
5727 uint64_t limit = std::numeric_limits<int64_t>::max ();
5737 if (ival.
value () <= limit)
5747 if (dval == math::fix (dval) && dval <= limit)
5766 return ov_is_ge_zero.
is_true ();
5772 uint64_t limit = std::numeric_limits<uint64_t>::max ();
5774 if (dval == math::fix (dval) && dval >= 0 && dval <= limit)
5782switch_to_g_format (
const printf_format_elt *elt)
5784 std::string tfmt = elt->text;
5786 tfmt.replace (tfmt.rfind (elt->type), 1,
"g");
5792base_stream::do_numeric_printf_conv (std::ostream& os,
5793 const printf_format_elt *elt,
5794 int nsa,
int sa_1,
int sa_2,
5796 const std::string& who)
5800 std::string tfmt = elt->text;
5802 if (is_nan_or_inf (val))
5806 std::string::size_type i1, i2;
5808 tfmt.replace ((i1 = tfmt.rfind (elt->type)), 1, 1,
's');
5810 if ((i2 = tfmt.rfind (
'.')) != std::string::npos && i2 < i1)
5812 tfmt.erase (i2, i1-i2);
5813 if (elt->prec == -2)
5818 if (math::isinf (dval))
5820 if (elt->flags.find (
'+') != std::string::npos)
5821 tval = (dval < 0 ?
"-Inf" :
"+Inf");
5823 tval = (dval < 0 ?
"-Inf" :
"Inf");
5827 if (elt->flags.find (
'+') != std::string::npos)
5833 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2, tval,
5838 static std::string llmod
5839 = (
sizeof (long) ==
sizeof (int64_t) ?
"l" :
"ll");
5841 char type = elt->type;
5845 case 'd':
case 'i':
case 'c':
5846 if (ok_for_signed_int_conv (val))
5851 tfmt.replace (tfmt.rfind (type), 1, llmod + type);
5853 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2,
5854 tval.
value (), who);
5858 tfmt = switch_to_g_format (elt);
5862 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2,
5867 case 'o':
case 'x':
case 'X':
case 'u':
5868 if (ok_for_unsigned_int_conv (val))
5873 tfmt.replace (tfmt.rfind (type), 1, llmod + type);
5875 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2,
5876 tval.
value (), who);
5880 tfmt = switch_to_g_format (elt);
5884 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2,
5889 case 'f':
case 'e':
case 'E':
5894 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2,
5902 error (who,
"invalid format specifier");
5912base_stream::field_width_error (
const std::string& who)
const
5914 ::error (
"%s: invalid field width, must be integer >= 0 and <= INT_MAX",
5919base_stream::do_printf (printf_format_list& fmt_list,
5921 const std::string& who)
5930 invalid_operation (who,
"writing");
5933 std::ostream& os = *osp;
5937 const printf_format_elt *elt = fmt_list.first ();
5939 printf_value_cache val_cache (args, who);
5946 ::error (
"%s: internal error handling format", who.c_str ());
5949 int nsa = (elt->fw == -2) + (elt->prec == -2);
5956 sa_1 = val_cache.int_value ();
5960 field_width_error (who);
5967 sa_2 = val_cache.int_value ();
5971 field_width_error (who);
5978 if (elt->type ==
'%')
5983 else if (elt->args == 0 && ! elt->text.empty ())
5986 retval += (elt->text.length ());
5988 else if (elt->type ==
's' || elt->type ==
'c')
5990 octave_value val = val_cache.get_next_value (elt->type);
5998 retval += do_printf_string (os, elt, nsa, sa_1,
6002 retval += do_numeric_printf_conv (os, elt, nsa, sa_1,
6015 retval += do_numeric_printf_conv (os, elt, nsa, sa_1,
6024 error (who,
"write error");
6028 elt = fmt_list.next (m_nconv > 0 && ! val_cache.exhausted ());
6030 if (! elt || (val_cache.exhausted () && elt->args > 0))
6039base_stream::printf (
const std::string& fmt,
6041 const std::string& who)
6043 printf_format_list fmt_list (fmt);
6045 if (fmt_list.num_conversions () == -1)
6046 ::error (
"%s: invalid format specified", who.c_str ());
6048 return do_printf (fmt_list, args, who);
6052base_stream::puts (
const std::string& s,
const std::string& who)
6059 invalid_operation (who,
"writing");
6062 std::ostream& os = *osp;
6067 error (who,
"write error");
6081 error (who,
"write error");
6093 err_num = (m_fail ? -1 : 0);
6095 std::string tmp = m_errmsg;
6104base_stream::invalid_operation (
const std::string& who,
const char *rw)
6107 error (who, std::string (
"stream not open for ") + rw);
6116 retval = m_rep->flush ();
6127 retval = m_rep->getl (max_len, err, who);
6134 const std::string& who)
6144 max_len = convert_to_valid_int (tc_max_len, conv_err);
6146 if (conv_err || max_len < 0)
6149 ::error (
"%s: invalid maximum length specified", who.c_str ());
6153 return getl (max_len, err, who);
6162 retval = m_rep->gets (max_len, err, who);
6169 const std::string& who)
6179 max_len = convert_to_valid_int (tc_max_len, conv_err);
6181 if (conv_err || max_len < 0)
6184 ::error (
"%s: invalid maximum length specified", who.c_str ());
6188 return gets (max_len, err, who);
6197 retval = m_rep->skipl (count, err, who);
6204 const std::string& who)
6219 count = convert_to_valid_int (tc_count, conv_err);
6221 if (conv_err || count < 0)
6224 ::error (
"%s: invalid number of lines specified",
6230 return skipl (count, err, who);
6243 off_t orig_pos = m_rep->tell ();
6246 status = m_rep->seek (0,
SEEK_END);
6250 off_t eof_pos = m_rep->tell ();
6262 status = m_rep->seek (offset, origin);
6267 off_t desired_pos = m_rep->tell ();
6271 if (desired_pos > eof_pos || desired_pos < 0)
6302 off_t xoffset = val.
value ();
6310 std::string xorigin = tc_origin.
xstring_value (
"fseek: invalid value for origin");
6312 if (xorigin ==
"bof")
6314 else if (xorigin ==
"cof")
6316 else if (xorigin ==
"eof")
6323 int xorigin = convert_to_valid_int (tc_origin, conv_err);
6329 else if (xorigin == 0)
6331 else if (xorigin == 1)
6339 ::error (
"fseek: invalid value for origin");
6341 retval =
seek (xoffset, origin);
6345 error (
"fseek: failed to seek to requested position");
6356 retval = m_rep->tell ();
6370 bool retval =
false;
6373 retval = m_rep->is_open ();
6388template <
typename SRC_T,
typename DST_T>
6390convert_and_copy (std::list<void *>& input_buf_list,
6394 bool do_float_fmt_conv,
bool do_NA_conv,
6395 mach_info::float_format from_flt_fmt)
6397 typedef typename DST_T::element_type dst_elt_type;
6401 dst_elt_type *conv_data = conv.rwdata ();
6405 for (
auto it = input_buf_list.cbegin (); it != input_buf_list.cend (); it++)
6407 SRC_T *data =
static_cast<SRC_T *
> (*it);
6409 if (swap || do_float_fmt_conv)
6417 swap_bytes<sizeof (SRC_T)> (&data[i]);
6418 else if (do_float_fmt_conv)
6421 mach_info::native_float_format ());
6430 conv_data[j] = data[i];
6439 swap_bytes<sizeof (SRC_T)> (&data[i]);
6440 else if (do_float_fmt_conv)
6443 mach_info::native_float_format ());
6445 conv_data[j] = data[i];
6457 conv_data[j] = data[i];
6464 conv_data[j] = data[i];
6471 input_buf_list.clear ();
6474 conv_data[i] = dst_elt_type (0);
6482 bool swap,
bool do_float_fmt_conv,
bool do_NA_conv,
6483 mach_info::float_format from_flt_fmt);
6485#define TABLE_ELT(T, U, V, W) \
6486 conv_fptr_table[oct_data_conv::T][oct_data_conv::U] = convert_and_copy<V, W>
6488#define FILL_TABLE_ROW(T, V) \
6489 TABLE_ELT (T, dt_int8, V, int8NDArray); \
6490 TABLE_ELT (T, dt_uint8, V, uint8NDArray); \
6491 TABLE_ELT (T, dt_int16, V, int16NDArray); \
6492 TABLE_ELT (T, dt_uint16, V, uint16NDArray); \
6493 TABLE_ELT (T, dt_int32, V, int32NDArray); \
6494 TABLE_ELT (T, dt_uint32, V, uint32NDArray); \
6495 TABLE_ELT (T, dt_int64, V, int64NDArray); \
6496 TABLE_ELT (T, dt_uint64, V, uint64NDArray); \
6497 TABLE_ELT (T, dt_single, V, FloatNDArray); \
6498 TABLE_ELT (T, dt_double, V, NDArray); \
6499 TABLE_ELT (T, dt_char, V, charNDArray); \
6500 TABLE_ELT (T, dt_schar, V, charNDArray); \
6501 TABLE_ELT (T, dt_uchar, V, charNDArray); \
6502 TABLE_ELT (T, dt_logical, V, boolNDArray);
6505stream::finalize_read (std::list<void *>& input_buf_list,
6511 mach_info::float_format ffmt)
6515 static bool initialized =
false;
6524 for (
int j = 0; j < 14; j++)
6525 conv_fptr_table[i][j] =
nullptr;
6547 if (ffmt == mach_info::flt_fmt_unknown)
6550 if (mach_info::words_big_endian ())
6551 swap = (ffmt == mach_info::flt_fmt_ieee_little_endian);
6553 swap = (ffmt == mach_info::flt_fmt_ieee_big_endian);
6561 switch (output_type)
6580 retval =
fptr (input_buf_list, input_buf_elts, elts_read,
6581 nr, nc, swap, do_float_fmt_conv, do_NA_conv, ffmt);
6586 ::error (
"read: invalid type specification");
6607 bool one_elt_size_spec =
false;
6615 std::ptrdiff_t tmp_count = 0;
6619 get_size (size, nr, nc, one_elt_size_spec,
"fread");
6623 invalid_operation (
"fread",
"reading");
6628 if (one_elt_size_spec)
6651 if (nr == 0 || nc == 0)
6657 bool read_to_eof = elts_to_read < 0;
6664 input_buf_elts = 1024 * 1024;
6666 input_buf_elts = elts_to_read;
6669 input_buf_elts = block_size;
6674 std::ptrdiff_t input_buf_size
6675 =
static_cast<std::ptrdiff_t
> (input_buf_elts) * input_elt_size;
6683 error (
"fread: invalid input stream");
6686 std::istream& is = *isp;
6691 if (skip != 0 && is && ! is.eof ())
6693 cur_pos = is.tellg ();
6694 is.seekg (0, is.end);
6695 eof_pos = is.tellg ();
6696 is.seekg (cur_pos, is.beg);
6699 std::list<void *> input_buf_list;
6701 while (is && ! is.eof ()
6702 && (read_to_eof || tmp_count < elts_to_read))
6708 if (remaining_elts < input_buf_elts)
6709 input_buf_size = remaining_elts * input_elt_size;
6712 char *input_buf =
new char [input_buf_size];
6714 is.read (input_buf, input_buf_size);
6716 std::size_t gcount = is.gcount ();
6724 input_buf_list.push_back (input_buf);
6726 if (skip != 0 && nel == block_size && is)
6730 off_t remaining = eof_pos - cur_pos;
6732 if (remaining < skip)
6734 is.seekg (0, is.end);
6739 is.seekg (skip, is.cur);
6749 nc = tmp_count / nr;
6751 if (tmp_count % nr != 0)
6757 else if (tmp_count == 0)
6762 else if (tmp_count != nr * nc)
6764 if (tmp_count % nr != 0)
6765 nc = tmp_count / nr + 1;
6767 nc = tmp_count / nr;
6773 if (tmp_count > std::numeric_limits<octave_idx_type>::max ())
6774 error (
"fread: number of elements read exceeds max index size");
6778 retval = finalize_read (input_buf_list, input_buf_elts, count,
6779 nr, nc, input_type, output_type, ffmt);
6793 invalid_operation (
"fwrite",
"writing");
6796 if (flt_fmt == mach_info::flt_fmt_unknown)
6803 error (
"fwrite: write error");
6811template <
typename T,
typename V>
6813convert_chars (
const void *data,
void *conv_data,
octave_idx_type n_elts)
6815 const T *tt_data =
static_cast<const T *
> (data);
6817 V *vt_data =
static_cast<V *
> (conv_data);
6820 vt_data[i] = tt_data[i];
6823template <
typename T,
typename V>
6828 typedef typename V::val_type val_type;
6830 val_type *vt_data =
static_cast<val_type *
> (conv_data);
6837 vt_data[i] = val.value ();
6840 swap_bytes<sizeof (val_type)> (&vt_data[i]);
6844template <
typename T>
6845class ultimate_element_type
6851template <
typename T>
6858template <
typename T>
6862 mach_info::float_format flt_fmt)
6868 if (mach_info::words_big_endian ())
6869 swap = (flt_fmt == mach_info::flt_fmt_ieee_little_endian);
6871 swap = (flt_fmt == mach_info::flt_fmt_ieee_big_endian);
6873 bool do_float_conversion = flt_fmt != mach_info::float_format ();
6875 typedef typename ultimate_element_type<T>::type ult_elt_type;
6877 switch (output_type)
6880 convert_chars<ult_elt_type, char> (data, conv_data, n_elts);
6884 convert_chars<ult_elt_type, signed char> (data, conv_data, n_elts);
6888 convert_chars<ult_elt_type, unsigned char> (data, conv_data, n_elts);
6892 convert_ints<T, octave_int8> (data, conv_data, n_elts, swap);
6896 convert_ints<T, octave_uint8> (data, conv_data, n_elts, swap);
6900 convert_ints<T, octave_int16> (data, conv_data, n_elts, swap);
6904 convert_ints<T, octave_uint16> (data, conv_data, n_elts, swap);
6908 convert_ints<T, octave_int32> (data, conv_data, n_elts, swap);
6912 convert_ints<T, octave_uint32> (data, conv_data, n_elts, swap);
6916 convert_ints<T, octave_int64> (data, conv_data, n_elts, swap);
6920 convert_ints<T, octave_uint64> (data, conv_data, n_elts, swap);
6925 float *vt_data =
static_cast<float *
> (conv_data);
6929 vt_data[i] = data[i];
6931 if (do_float_conversion)
6939 double *vt_data =
static_cast<double *
> (conv_data);
6943 vt_data[i] = data[i];
6945 if (do_float_conversion)
6952 ::error (
"write: invalid type specification");
6961 bool status =
false;
6967 std::ostream& os = *osp;
6971 os.write (
static_cast<const char *
> (data), nbytes);
6984 bool status =
false;
6991 std::ostream& os = *osp;
6995 off_t orig_pos =
tell ();
6999 off_t eof_pos =
tell ();
7004 std::size_t remaining = eof_pos - orig_pos;
7006 if (remaining < skip)
7011 unsigned char zero = 0;
7012 for (std::size_t j = 0; j < skip - remaining; j++)
7013 os.write (
reinterpret_cast<const char *
> (&zero), 1);
7024template <
typename T>
7029 mach_info::float_format flt_fmt)
7033 if (mach_info::words_big_endian ())
7034 swap = (flt_fmt == mach_info::flt_fmt_ieee_little_endian);
7036 swap = (flt_fmt == mach_info::flt_fmt_ieee_big_endian);
7038 bool do_data_conversion = (swap || ! is_equivalent_type<T> (output_type)
7039 || flt_fmt != mach_info::float_format ());
7046 chunk_size = block_size;
7047 else if (do_data_conversion)
7048 chunk_size = 1024 * 1024;
7054 const T *pdata = data.
data ();
7066 if (chunk_size > remaining_nel)
7067 chunk_size = remaining_nel;
7069 bool status =
false;
7071 if (do_data_conversion)
7073 std::size_t output_size
7078 status = convert_data (&pdata[i], conv_data, chunk_size,
7079 output_type, flt_fmt);
7085 status =
write_bytes (pdata,
sizeof (T) * chunk_size);
7096#define INSTANTIATE_WRITE(T) \
7098 OCTINTERP_API octave_idx_type \
7099 stream::write (const Array<T>& data, octave_idx_type block_size, \
7100 oct_data_conv::data_type output_type, \
7101 octave_idx_type skip, \
7102 mach_info::float_format flt_fmt)
7121#if defined (OCTAVE_HAVE_OVERLOAD_CHAR_INT8_TYPES)
7134 retval = m_rep->scanf (fmt, size, count, who);
7152 retval =
scanf (sfmt, size, count, who);
7157 error (who +
": format must be a string");
7169 retval = m_rep->oscanf (fmt, who);
7186 retval =
oscanf (sfmt, who);
7191 error (who +
": format must be a string");
7202 return (stream_ok ()
7203 ? m_rep->do_textscan (fmt, ntimes, options, who, count)
7209 const std::string& who)
7214 retval = m_rep->printf (fmt, args, who);
7221 const std::string& who)
7232 retval =
printf (sfmt, args, who);
7237 error (who +
": format must be a string");
7249 retval = m_rep->puts (s, who);
7264 retval =
puts (s, who);
7269 error (who +
": argument must be a string");
7281 retval = m_rep->eof ();
7289 std::string retval =
"invalid stream object";
7291 if (stream_ok (
false))
7292 retval = m_rep->error (clear, err_num);
7303 retval = m_rep->name ();
7314 retval = m_rep->mode ();
7319mach_info::float_format
7322 mach_info::float_format retval = mach_info::flt_fmt_unknown;
7325 retval = m_rep->float_format ();
7333 std::string retval =
"???";
7334 std::ios::openmode in_mode =
static_cast<std::ios::openmode
> (
mode);
7336 if (in_mode == std::ios::in)
7338 else if (in_mode == std::ios::out
7339 || in_mode == (std::ios::out | std::ios::trunc))
7341 else if (in_mode == (std::ios::out | std::ios::app))
7343 else if (in_mode == (std::ios::in | std::ios::out))
7345 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc))
7347 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate))
7349 else if (in_mode == (std::ios::in | std::ios::binary))
7351 else if (in_mode == (std::ios::out | std::ios::binary)
7352 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary))
7354 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary))
7356 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary))
7358 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc
7359 | std::ios::binary))
7361 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate
7362 | std::ios::binary))
7369 : m_list (), m_lookup_cache (m_list.end ()), m_stdin_file (-1),
7370 m_stdout_file (-1), m_stderr_file (-1)
7386 m_stdin_file =
insert (stdin_stream);
7387 m_stdout_file =
insert (stdout_stream);
7388 m_stderr_file =
insert (stderr_stream);
7403 if (stream_number == -1)
7404 return stream_number;
7419 if (m_list.size () >= m_list.max_size ())
7420 ::error (
"could not create file id");
7422 m_list[stream_number] = os;
7424 return stream_number;
7427OCTAVE_NORETURN
static
7429err_invalid_file_id (
int fid,
const std::string& who)
7432 ::error (
"invalid stream number = %d", fid);
7434 ::error (
"%s: invalid stream number = %d", who.c_str (), fid);
7443 err_invalid_file_id (fid, who);
7445 if (m_lookup_cache != m_list.end () && m_lookup_cache->first == fid)
7446 retval = m_lookup_cache->second;
7449 ostrl_map::const_iterator iter = m_list.find (fid);
7451 if (iter == m_list.end ())
7452 err_invalid_file_id (fid, who);
7454 retval = iter->second;
7455 m_lookup_cache = iter;
7463 const std::string& who)
const
7475 err_invalid_file_id (fid, who);
7477 auto iter = m_list.find (fid);
7479 if (iter == m_list.end ())
7480 err_invalid_file_id (fid, who);
7482 stream os = iter->second;
7483 m_list.erase (iter);
7484 m_lookup_cache = m_list.end ();
7488 err_invalid_file_id (fid, who);
7510 retval =
remove (i, who);
7526 for (
auto iter = m_list.begin (); iter != m_list.end (); )
7528 int fid = iter->first;
7535 stream os = iter->second;
7537 std::string name = os.
name ();
7538 std::transform (name.begin (), name.end (), name.begin (), tolower);
7541 if (name.find (
"gnuplot") != std::string::npos)
7552 m_list.erase (iter++);
7555 m_lookup_cache = m_list.end ();
7567 if (m_lookup_cache != m_list.end () && m_lookup_cache->first == fid)
7568 os = m_lookup_cache->second;
7571 ostrl_map::const_iterator iter = m_list.find (fid);
7573 if (iter == m_list.end ())
7577 m_lookup_cache = iter;
7583 retval(0) = os.
name ();
7585 retval(2) = mach_info::float_format_as_string (os.
float_format ());
7597 ::error (
"file id must be a file object or integer value");
7599 int int_fid = convert_to_valid_int (fid, conv_err);
7602 ::error (
"file id must be a file object or integer value");
7610 std::ostringstream buf;
7613 <<
" number mode arch name\n"
7614 <<
" ------ ---- ---- ----\n";
7616 for (
const auto& fid_strm : m_list)
7618 stream os = fid_strm.second;
7621 << std::setiosflags (std::ios::right)
7622 << std::setw (4) << fid_strm.first <<
" "
7624 << std::resetiosflags (std::ios::adjustfield)
7625 << std::setiosflags (std::ios::left)
7630 << mach_info::float_format_as_string (os.
float_format ())
7632 << os.
name () <<
"\n";
7643 Matrix retval (1, m_list.size (), 0.0);
7647 for (
const auto& fid_strm : m_list)
7650 if (fid_strm.first > 2 && fid_strm.second)
7651 retval(0, num_open++) = fid_strm.first;
7654 retval.
resize ((num_open > 0), num_open);
7668 for (
const auto& fid_strm : m_list)
7671 if (fid_strm.first > 2)
7673 stream os = fid_strm.second;
7675 if (os && os.
name () == nm)
7677 retval = fid_strm.first;
7684 ::error (
"file id must be a file object, std::string, or integer value");
7689 int int_fid = convert_to_valid_int (fid, conv_err);
7692 ::error (
"file id must be a file object, std::string, or integer value");
7718OCTAVE_END_NAMESPACE(octave)
N Dimensional Array with copy-on-write semantics.
const T * data() const
Size of the specified dimension.
T * rwdata()
Size of the specified dimension.
octave_idx_type numel() const
Number of elements in the array.
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
static bool forced_interactive()
std::ostream * preferred_output_stream()
virtual std::string name() const =0
std::string encoding() const
virtual std::istream * input_stream()
std::string error(bool clear, int &err_num)
virtual int file_number() const
Vector representing the dimensions (size) of an Array.
output_system & get_output_system()
void recover_from_exception()
static stream create(std::istream *arg=nullptr, const std::string &n="")
static std::size_t data_type_size(data_type dt)
virtual bool fast_elem_insert(octave_idx_type n, const octave_value &x)
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
octave_idx_type length() const
octave_value isinf() const
octave_uint64 uint64_scalar_value() const
octave_int64 int64_scalar_value() const
bool is_scalar_type() const
bool is_sq_string() const
bool is_single_type() const
octave_value isnan() const
bool is_uint64_type() const
octave_value reshape(const dim_vector &dv) const
octave_int64 xint64_scalar_value(const char *fmt,...) const
double scalar_value(bool frc_str_conv=false) const
octave_value fast_elem_extract(octave_idx_type n) const
Extract the n-th element, aka 'val(n)'.
std::string string_value(bool force=false) const
NDArray array_value(bool frc_str_conv=false) const
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
octave_value convert_to_str(bool pad=false, bool force=false, char type='\'') const
std::string xstring_value(const char *fmt,...) const
double double_value(bool frc_str_conv=false) const
octave_base_value * internal_rep() const
static stream create(std::ostream *arg, const std::string &n="")
std::ostream & __stdout__()
octave_value stdin_file() const
int get_file_number(const octave_value &fid) const
std::string list_open_files() const
octave_value stdout_file() const
stream_list(interpreter &interp)
stream lookup(int fid, const std::string &who="") const
octave_value open_file_numbers() const
string_vector get_info(int fid) const
int remove(int fid, const std::string &who="")
void clear(bool flush=true)
octave_value stderr_file() const
std::ostream * output_stream()
std::istream * input_stream()
static std::string mode_as_string(int mode)
octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
bool skip_bytes(std::size_t n_elts)
off_t skipl(off_t count, bool &err, const std::string &who)
octave_value textscan(const std::string &fmt, octave_idx_type ntimes, const octave_value_list &options, const std::string &who, octave_idx_type &count)
int puts(const std::string &s, const std::string &who)
int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
bool write_bytes(const void *data, std::size_t n_elts)
mach_info::float_format float_format() const
int seek(off_t offset, int origin)
octave_value_list oscanf(const std::string &fmt, const std::string &who)
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
std::string error(bool clear, int &err_num)
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)
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)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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)
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)
void warning(const char *fmt,...)
void warning_with_id(const char *id, const char *fmt,...)
void error(const char *fmt,...)
void err_wrong_type_arg(const char *name, const char *s)
interpreter & __get_interpreter__()
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
F77_RET_T const F77_DBLE const F77_DBLE * f
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
std::complex< double > w(std::complex< double > z, double relerr=0)
std::complex< double > Complex
std::complex< float > FloatComplex
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
#define BEGIN_S_CONVERSION()
#define BEGIN_CHAR_CLASS_CONVERSION()
#define BEGIN_C_CONVERSION()
#define DO_WHITESPACE_CONVERSION()
#define DO_LITERAL_CONVERSION()
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)
#define FILL_TABLE_ROW(T, V)
#define INSTANTIATE_WRITE(T)
#define DO_PCT_CONVERSION()
#define FINISH_CHARACTER_CONVERSION()
T::size_type numel(const T &str)
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.
const octave_base_value const Array< octave_idx_type > & ra_idx
octave_value cat_op(type_info &ti, const octave_value &a, const octave_value &b, const Array< octave_idx_type > &ra_idx)
octave_value binary_op(type_info &ti, octave_value::binary_op op, const octave_value &a, const octave_value &b)
int octave_strncasecmp(const char *s1, const char *s2, size_t n)
std::string do_string_escapes(const std::string &s)
std::string undo_string_escapes(const std::string &s)
std::size_t format(std::ostream &os, const char *fmt,...)