26 #if defined (HAVE_CONFIG_H)
132 ::error (
"%s: NaN invalid as size specification", who.c_str ());
139 ::error (
"%s: negative value invalid as size specification",
143 ::error (
"%s: dimension too large for Octave's index type",
155 bool& one_elt_size_spec,
const std::string& who)
160 one_elt_size_spec =
false;
169 one_elt_size_spec =
true;
173 dnc = (dnr == 0.0) ? 0.0 : 1.0;
175 else if (sz_len == 2)
180 ::error (
"%s: infinite value invalid as size specification",
186 ::error (
"%s: invalid size specification (must be 2-D)", who.c_str ());
197 ::error (
"%s: size too large for Octave's index type", who.c_str ());
206 size_t len = s.length ();
212 unsigned char c = s[i++];
214 if (c ==
'-' && i > 1 && i <
len
215 && (
static_cast<unsigned char> (s[i-2])
216 <=
static_cast<unsigned char> (s[i])))
221 for (c = s[i-2]+1; c < s[i]; c++)
229 if (c !=
'-' || i ==
len)
244 whitespace_conversion = 1,
245 literal_conversion = 2,
250 char typ =
'\0',
char mod =
'\0',
251 const std::string& ch_class =
"")
252 :
text (txt), width (
w), discard (
d),
type (typ),
253 modifier (
mod), char_class (ch_class)
304 size_t length (
void)
const {
return fmt_elts.size (); }
314 return length () > 0 ? fmt_elts[curr_idx] :
nullptr;
324 if (curr_idx >=
length ())
335 void printme (
void)
const;
337 bool ok (
void)
const {
return (nconv >= 0); }
339 operator bool ()
const {
return ok (); }
341 bool all_character_conversions (
void);
343 bool all_numeric_conversions (
void);
360 void add_elt_to_list (
int width,
bool discard,
char type,
char modifier,
361 const std::string& char_class =
"");
363 void process_conversion (
const std::string& s,
size_t& i,
size_t n,
364 int& width,
bool& discard,
char&
type,
367 int finish_conversion (
const std::string& s,
size_t& i,
size_t n,
368 int width,
bool discard,
char&
type,
373 : nconv (0), curr_idx (0), fmt_elts (), buf ()
375 size_t n = s.length ();
380 bool discard =
false;
381 char modifier =
'\0';
384 bool have_more =
true;
396 have_more = (
buf.tellp () != 0);
398 else if (isspace (s[i]))
407 while (++i <
n && isspace (s[i]))
422 while (i <
n && ! isspace (s[i]) && s[i] !=
'%')
448 for (
size_t i = 0; i <
n; i++)
458 const std::string& char_class)
466 modifier, char_class);
477 size_t n,
int& width,
bool& discard,
478 char&
type,
char& modifier)
487 bool have_width =
false;
503 case '0':
case '1':
case '2':
case '3':
case '4':
504 case '5':
case '6':
case '7':
case '8':
case '9':
510 width = 10 * width + c -
'0';
513 while (i <
n && isdigit (s[i]))
516 width = 10 * width + c -
'0';
522 case 'h':
case 'l':
case 'L':
523 if (modifier !=
'\0')
530 case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
541 case 'e':
case 'f':
case 'g':
554 case 'c':
case 's':
case 'p':
case '%':
case '[':
555 if (modifier !=
'\0')
565 type, modifier) == 0)
584 size_t n,
int width,
bool discard,
585 char&
type,
char modifier)
589 std::string char_class;
591 size_t beg_idx = std::string::npos;
592 size_t end_idx = std::string::npos;
624 else if (s[i] ==
']')
628 while (i <
n && s[i] !=
']')
631 if (i <
n && s[i] ==
']')
648 if (beg_idx != std::string::npos && end_idx != std::string::npos)
650 end_idx - beg_idx + 1));
663 for (
size_t i = 0; i <
n; i++)
668 <<
"width: " << elt->
width <<
"\n"
669 <<
"discard: " << elt->
discard <<
"\n"
673 std::cerr <<
"literal text\n";
675 std::cerr <<
"whitespace\n";
677 std::cerr << elt->
type <<
"\n";
680 <<
"modifier: " << elt->
modifier <<
"\n"
693 for (
size_t i = 0; i <
n; i++)
699 case 'c':
case 's':
case '%':
case '[':
case '^':
723 for (
size_t i = 0; i <
n; i++)
729 case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
730 case 'e':
case 'f':
case 'g':
case 'E':
case 'G':
751 int p = -1,
const std::string&
f =
"",
752 char typ =
'\0',
char mod =
'\0')
753 :
text (txt), args (
n), fw (
w), prec (p), flags (
f),
811 return length () > 0 ? fmt_elts[curr_idx] :
nullptr;
814 size_t length (
void)
const {
return fmt_elts.size (); }
820 if (curr_idx >=
length ())
833 void printme (
void)
const;
835 bool ok (
void)
const {
return (nconv >= 0); }
837 operator bool ()
const {
return ok (); }
854 void add_elt_to_list (
int args,
const std::string& flags,
int fw,
855 int prec,
char type,
char modifier);
857 void process_conversion (
const std::string& s,
size_t& i,
size_t n,
858 int& args, std::string& flags,
int& fw,
859 int& prec,
char& modifier,
char&
type);
861 void finish_conversion (
const std::string& s,
size_t& i,
int args,
862 const std::string& flags,
int fw,
int prec,
863 char modifier,
char&
type);
867 : nconv (0), curr_idx (0), fmt_elts (), buf ()
869 size_t n = s.length ();
877 char modifier =
'\0';
880 bool have_more =
true;
881 bool empty_buf =
true;
896 empty_buf = (
buf.tellp () == 0);
913 have_more = (
buf.tellp () != 0);
952 for (
size_t i = 0; i <
n; i++)
961 int fw,
int prec,
char type,
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':
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)
1154 for (
size_t i = 0; i <
n; 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"
1177 for (
unsigned int d =
n;
d;
d >>= 1)
1239 int refresh_buf (
void);
1246 return eof () ? std::istream::traits_type::eof () : *idx++;
1248 return get_undelim ();
1252 int get_undelim (
void);
1258 int peek (
void) {
return eof () ? std::istream::traits_type::eof () : *idx; }
1261 int peek_undelim (
void);
1269 int getline (std::string& dest,
char delim);
1273 char * read (
char *buffer,
int size,
char* &new_start);
1279 void seekg (
char *old_idx) { idx = old_idx; }
1283 return (eob == buf && i_stream.eof ()) || (flags & std::ios_base::eofbit);
1286 operator const void* (void) {
return (! eof () && ! flags) ?
this :
nullptr; }
1288 bool fail (
void) {
return flags & std::ios_base::failbit; }
1290 std::ios_base::iostate
rdstate (
void) {
return flags; }
1295 = (std::ios_base::eofbit & ~std::ios_base::eofbit))
1351 const std::string& delimiters,
1352 int longest_lookahead,
1354 : bufsize (bsize), i_stream (is), longest (longest_lookahead),
1355 delims (delimiters),
1356 flags (std::ios::failbit & ~std::ios::failbit)
1394 return std::istream::traits_type::eof ();
1406 retval = std::istream::traits_type::eof ();
1439 return std::istream::traits_type::eof ();
1446 size_t old_remaining =
eob -
idx;
1450 if (old_remaining > 0)
1453 memmove (
buf,
idx, old_remaining);
1470 eob =
buf + old_remaining + gcount;
1480 retval = std::istream::traits_type::eof ();
1488 if (
delims.find (*
last) != std::string::npos)
1499 if (
retval == std::istream::traits_type::eof ())
1564 for (i = 0; i < size && !
eof (); i++)
1567 memset (buffer, 0, size - i);
1581 int len = out.length ();
1585 && ch != std::istream::traits_type::eof ())
1609 whitespace_conversion = 1,
1610 literal_conversion = 2
1614 int bw = 0,
bool dis =
false,
char typ =
'\0',
1615 const std::string& ch_class = std::string ())
1616 :
text (txt), width (
w), prec (p), bitwidth (bw),
1617 char_class (ch_class),
type (typ), discard (dis),
1618 numeric (typ ==
'd' || typ ==
'u' ||
type ==
'f' ||
type ==
'n')
1622 :
text (e.
text), width (e.width), prec (e.prec),
1623 bitwidth (e.bitwidth), char_class (e.char_class),
type (e.
type),
1624 discard (e.discard), numeric (e.numeric)
1682 const std::string& who =
"textscan");
1698 size_t numel (
void)
const {
return fmt_elts.size (); }
1708 return numel () > 0 ? fmt_elts[curr_idx] :
nullptr;
1715 if (curr_idx >=
numel ())
1726 void printme (
void)
const;
1728 bool ok (
void)
const {
return (nconv >= 0); }
1730 operator const void* (void)
const {
return ok () ? this :
nullptr; }
1743 std::list<octave_value>
out_buf (
void)
const {
return (output_container); }
1763 void add_elt_to_list (
unsigned int width,
int prec,
int bitwidth,
1766 const std::string& char_class = std::string ());
1768 void process_conversion (
const std::string& s,
size_t& i,
size_t n);
1770 std::string parse_char_class (
const std::string& pattern)
const;
1772 int finish_conversion (
const std::string& s,
size_t& i,
size_t n,
1773 unsigned int width,
int prec,
int bitwidth,
1775 bool discard,
char&
type);
1792 textscan (
const std::string& who_arg =
"textscan",
1793 const std::string& encoding =
"utf-8");
1803 octave_value scan (std::istream& isp,
const std::string& fmt,
1880 std::list<octave_value>&
retval,
1894 std::string& val)
const;
1897 std::string& val)
const;
1900 std::string& val)
const;
1903 std::string& val)
const;
1910 const std::string& ends)
const;
1913 bool case_sensitive =
true)
const;
1923 return ((delim_table.empty () && (isspace (ch) || ch == eol1 || ch == eol2))
1924 || delim_table[ch] !=
'\0');
1927 bool isspace (
unsigned int ch)
const {
return whitespace_table[ch & 0xff]; }
1934 const std::string& who_arg)
1935 : who (who_arg), set_from_first (false), has_string (false),
1936 nconv (0), curr_idx (0), fmt_elts (), buf ()
1938 size_t n = s.length ();
1942 unsigned int width = -1;
1945 bool discard =
false;
1948 bool have_more =
true;
1973 if (s[i] ==
'%' && (i+1 ==
n || s[i+1] !=
'%'))
1985 have_more = (
buf.tellp () != 0);
1987 else if (isspace (s[i]))
1989 while (++i <
n && isspace (s[i]))
2003 while (i <
n && ! isspace (s[i])
2004 && (s[i] !=
'%' || (i+1 <
n && s[i+1] ==
'%')))
2037 for (
size_t i = 0; i <
n; i++)
2047 bool discard,
char type,
2048 const std::string& char_class)
2050 std::string
text =
buf.str ();
2052 if (!
text.empty ())
2075 bool discard =
false;
2081 bool have_width =
false;
2097 case '0':
case '1':
case '2':
case '3':
case '4':
2098 case '5':
case '6':
case '7':
case '8':
case '9':
2104 width = width * 10 + c -
'0';
2107 while (i <
n && isdigit (s[i]))
2110 width = width * 10 + c -
'0';
2114 if (i <
n && s[i] ==
'.')
2118 while (i <
n && isdigit (s[i]))
2121 prec = prec * 10 + c -
'0';
2143 else if (s[i] ==
'1' && i+1 <
n && s[i+1] ==
'6')
2153 else if (s[i] ==
'3' && i+1 <
n && s[i+1] ==
'2')
2159 else if (s[i] ==
'6' && i+1 <
n && s[i+1] ==
'4')
2191 if (s[i] ==
'3' && i+1 <
n && s[i+1] ==
'2')
2198 else if (s[i] ==
'6' && i+1 <
n && s[i+1] ==
'4')
2217 case 's':
case 'q':
case '[':
case 'c':
2231 width =
static_cast<unsigned int> (-1);
2235 discard,
type) == 0)
2241 error (
"%s: '%%%c' is not a valid format specifier",
2242 who.c_str (), s[i]);
2267 int len = pattern.length ();
2271 std::string
retval (256,
'\0');
2272 std::string mask (256,
'\0');
2274 int in = 0, out = 0;
2275 unsigned char ch, prev = 0;
2284 mask[pattern[in]] =
'\1';
2285 retval[out++] = pattern[in++];
2287 bool prev_was_range =
false;
2288 bool prev_prev_was_range =
false;
2289 for (; in <
len; in++)
2291 bool was_range =
false;
2296 if (prev ==
'-' && in > 1 && isalnum (ch) && ! prev_prev_was_range)
2298 unsigned char start_of_range = pattern[in-2];
2299 if (start_of_range < ch
2300 && ((isupper (ch) && isupper (start_of_range))
2301 || (islower (ch) && islower (start_of_range))
2302 || (isdigit (ch) && isdigit (start_of_range))
2308 for (
int i = start_of_range; i <= ch; i++)
2310 if (mask[i] ==
'\0')
2320 if (mask[ch]++ == 0)
2324 "%s: [...] contains two '%c's",
2327 if (prev ==
'-' && mask[
'-'] >= 2)
2329 (
"Octave:textscan-pattern",
2330 "%s: [...] contains two '-'s outside range expressions",
2334 prev_prev_was_range = prev_was_range;
2335 prev_was_range = was_range;
2341 for (
int i = 0; i < 256; i++)
2353 size_t n,
unsigned int width,
2354 int prec,
int bitwidth,
2360 std::string char_class;
2362 size_t beg_idx = std::string::npos;
2363 size_t end_idx = std::string::npos;
2387 else if (s[i] ==
']')
2391 while (i <
n && s[i] !=
']')
2394 if (i <
n && s[i] ==
']')
2407 if (beg_idx != std::string::npos && end_idx != std::string::npos)
2409 end_idx - beg_idx + 1));
2423 for (
size_t i = 0; i <
n; i++)
2428 <<
"width: " << elt->
width <<
"\n"
2429 <<
"digits " << elt->
prec <<
"\n"
2430 <<
"bitwidth: " << elt->
bitwidth <<
"\n"
2431 <<
"discard: " << elt->
discard <<
"\n"
2435 std::cerr <<
"literal text\n";
2437 std::cerr <<
"whitespace\n";
2439 std::cerr << elt->
type <<
"\n";
2454 std::string first_line (20,
' ');
2456 is.
getline (first_line,
static_cast<char> (ts.
eol2));
2458 if (! first_line.empty () && first_line.back () == ts.
eol1)
2459 first_line.pop_back ();
2461 std::istringstream strstr (first_line);
2468 int max_empty = 1000;
2474 bool already_skipped_delim =
false;
2493 already_skipped_delim =
true;
2501 if (val.imag () == 0)
2508 if (! already_skipped_delim)
2527 : who (who_arg), m_encoding (encoding), buf (), whitespace_table (),
2528 delim_table (), delims (), comment_style (), comment_len (0),
2529 comment_char (-2), buffer_size (0), date_locale (),
2530 inf_nan (
init_inf_nan ()), empty_value (numeric_limits<double>::
NaN ()),
2531 exp_chars (
"edED"), header_lines (0), treat_as_empty (),
2532 treat_as_empty_len (0), whitespace (
" \b\t"), eol1 (
'\r'), eol2 (
'\n'),
2533 return_on_error (1), collect_output (false),
2534 multiple_delims_as_one (false), default_exp (true), lines (0)
2552 std::ios::iostate
state = isp.rdstate ();
2555 isp.setstate (
state);
2567 error (
"%s: invalid format specified",
who.c_str ());
2570 error (
"%s: no valid format conversion specifiers",
who.c_str ());
2575 getline (isp, dummy,
static_cast<char> (
eol2));
2587 else if (ntimes > 0)
2591 buf_size =
std::max (buf_size, ntimes);
2596 max_lookahead, buf_size);
2618 done_after = fmt_list.
numel () + 1;
2623 done_after = fmt_list.
out_buf ().size () + 1;
2625 std::list<octave_value> out = fmt_list.
out_buf ();
2633 bool merge_with_prev[fmt_list.
numel ()];
2638 for (
const auto& col : out)
2640 if (col.type_id () == prev_type
2642 merge_with_prev[conv++] =
true;
2644 merge_with_prev[conv++] =
false;
2646 prev_type = col.type_id ();
2653 error (
"%s: No conversions specified",
who.c_str ());
2658 for (; row < ntimes || ntimes == -1; row++)
2660 if (row == 0 || row >= size)
2663 for (
auto& col : out)
2670 if ((err & ~1) > 0 || ! is || (
lines >= ntimes && ntimes > -1))
2676 error (
"%s: Read error in field %d of row %" OCTAVE_IDX_TYPE_FORMAT,
2677 who.c_str (), done_after + 1, row + 1);
2680 bool uneven_columns =
false;
2682 uneven_columns =
true;
2683 else if (isp.eof ())
2686 isp.seekg (-1, std::ios_base::end);
2687 int last_char = isp.get ();
2688 isp.setstate (isp.eofbit);
2689 uneven_columns = (last_char !=
eol1 && last_char !=
eol2);
2698 done_after = out.size () + 1;
2700 int valid_rows = (row == ntimes
2702 : ((err & 1) && (err & 8)) ? row : row+1);
2710 for (
auto& col : out)
2713 if (i == done_after && uneven_columns)
2730 for (
auto& col : out)
2732 if (! merge_with_prev[conv++])
2734 if (prev_type != -1)
2742 prev_type = col.type_id ();
2746 ra_idx(1) = group_size++;
2766 unsigned int width_left = fmt.
width;
2770 int ch = is.
peek ();
2791 if (ch >=
'0' && ch <=
'9')
2793 while (width_left-- && is && (ch = is.
get ()) >=
'0' && ch <=
'9')
2799 if (ch ==
'.' && width_left)
2801 double multiplier = 1;
2802 int precision = fmt.
prec;
2807 if (precision == -1)
2813 for (i = 0; i < precision; i++)
2815 if (width_left-- && is && (ch = is.
get ()) >=
'0' && ch <=
'9')
2816 retval += (ch -
'0') * (multiplier *= 0.1);
2825 if ((i == precision || ! width_left) && (ch = is.
get ()) >=
'5'
2834 while (width_left-- && is && (ch = is.
get ()) >=
'0' && ch <=
'9')
2841 bool used_exp =
false;
2842 if (valid && width_left > 1 &&
exp_chars.find (ch) != std::string::npos)
2844 int ch1 = is.
peek ();
2845 if (ch1 ==
'-' || ch1 ==
'+' || (ch1 >=
'0' && ch1 <=
'9'))
2856 else if (ch1 ==
'-')
2863 while (width_left-- && is && (ch = is.
get ()) >=
'0' && ch <=
'9')
2865 exp = exp*10 + ch -
'0';
2869 if (ch != std::istream::traits_type::eof () && width_left)
2872 double multiplier =
pown (10, exp);
2882 if (! used_exp && ch != std::istream::traits_type::eof () && width_left)
2886 if (! valid && width_left >= 3)
2919 bool as_empty =
false;
2922 int ch = is.
peek ();
2923 if (ch ==
'+' || ch ==
'-')
2926 int ch2 = is.
peek ();
2927 if (ch2 ==
'i' || ch2 ==
'j')
2932 if (is.
peek () ==
'n')
2934 char *pos = is.
tellg ();
2953 im = (ch ==
'+') ? value : -value;
2961 char *pos = is.
tellg ();
2991 char *look = is.
read (&look_buf[0], look_buf.size (), pos);
3000 if (!
strncmp (s.c_str (), look, s.size ()))
3004 is.
read (&look_buf[0], s.size (), pos);
3011 if (! is.
eof () && ! as_empty)
3016 if (ch ==
'i' || ch ==
'j')
3022 else if (ch ==
'+' || ch ==
'-')
3033 if (is.
peek () ==
'i' || is.
peek () ==
'j')
3056 std::string& val)
const
3058 int c1 = std::istream::traits_type::eof ();
3059 std::ostringstream obuf;
3061 while (is && ((c1 = (is && ! is.
eof ())
3063 : std::istream::traits_type::eof ())
3064 != std::istream::traits_type::eof ())
3065 && pattern.find (c1) == std::string::npos)
3066 obuf << static_cast<char> (c1);
3070 if (c1 != std::istream::traits_type::eof ())
3081 const std::string& ends)
const
3092 int last = (! is.
eof ()
3093 ? is.
get_undelim () : std::istream::traits_type::eof ());
3095 if (last != std::istream::traits_type::eof ())
3101 for (
int i = 0; i < delimiters.
numel (); i++)
3103 std::string delim = delimiters(i).string_value ();
3104 size_t start = (
retval.length () > delim.length ()
3105 ?
retval.length () - delim.length ()
3107 std::string may_match =
retval.substr (start);
3108 if (may_match == delim)
3119 while (! done && is && ! is.
eof ());
3130 std::string& val)
const
3135 unsigned int width = fmt.
width;
3137 for (i = 0; i < width; i++)
3140 if (i >= val.length ())
3141 val.append (
std::max (val.length (),
3142 static_cast<size_t> (16)),
'\0');
3145 if (
is_delim (ch) || ch == std::istream::traits_type::eof ())
3153 val = val.substr (0, i);
3161 std::string tmp =
delim_list(i).string_value ();
3162 ends[i] = tmp.back ();
3178 std::string& val)
const
3180 int c1 = std::istream::traits_type::eof ();
3181 std::ostringstream obuf;
3183 while (is && pattern.find (c1 = is.
get_undelim ()) != std::string::npos)
3184 obuf << static_cast<char> (c1);
3187 if (c1 != std::istream::traits_type::eof ())
3202 if (is.
peek () !=
'"')
3215 val = val + '"' + val1;
3230 std::string& val)
const
3232 val.resize (fmt.
width);
3234 for (
unsigned int i = 0; is && i < fmt.
width; i++)
3237 if (ch != std::istream::traits_type::eof ())
3264 if (fmt.
type ==
'f' || fmt.
type ==
'n')
3274 if (ov.
isreal () && v.imag () == 0)
3286 if (ov.
isreal () && v.imag () == 0)
3349 if (fmt.
type ==
'd')
3362 if (fmt.
type ==
'd')
3381 std::string vv (
" ");
3423 std::list<octave_value>&
retval,
3427 auto out =
retval.begin ();
3428 bool no_conversions =
true;
3430 bool conversion_failed =
false;
3431 bool nothing_worked =
true;
3435 for (
size_t i = 0; i < fmt_list.
numel (); i++)
3437 bool this_conversion_failed =
false;
3446 warning (
"%s: conversion %c not yet implemented",
3467 error (
"Unknown format element '%c'",
elem->type);
3472 if (!
elem->discard)
3473 no_conversions =
false;
3484 this_conversion_failed =
true;
3488 char *pos = is.
tellg ();
3490 this_conversion_failed =
true;
3497 if (!
elem->discard)
3501 char *pos = is.
tellg ();
3519 if (this_conversion_failed)
3521 if (is.
tellg () == pos && ! conversion_failed)
3525 conversion_failed =
true;
3528 this_conversion_failed =
false;
3530 else if (! done && ! conversion_failed)
3531 nothing_worked =
false;
3537 return no_conversions
3538 + (is.
eof () ? 2 : 0)
3539 + (conversion_failed ? 4 : 0)
3540 + (nothing_worked ? 8 : 0);
3548 int last = args.
length ();
3552 error (
"%s: %d parameters given, but only %d values",
3553 who.c_str (),
n-
n/2,
n/2);
3556 bool have_delims =
false;
3557 for (
int i = 0; i < last; i += 2)
3559 std::string param = args(i).xstring_value (
"%s: Invalid parameter type <%s> for parameter %d",
3561 args(i).type_name ().c_str (),
3563 std::transform (param.begin (), param.end (), param.begin (), ::tolower);
3565 if (param ==
"delimiter")
3567 bool invalid =
true;
3568 if (args(i+1).is_string ())
3572 delims = args(i+1).string_value ();
3573 if (args(i+1).is_sq_string ())
3576 else if (args(i+1).iscell ())
3599 error (
"%s: Delimiters must be either a string or cell array of strings",
3602 else if (param ==
"commentstyle")
3604 if (args(i+1).is_string ())
3609 else if (args(i+1).iscell ())
3616 error (
"%s: CommentStyle must be either a string or cell array of one or two strings",
3620 error (
"%s: CommentStyle must be either a string or cell array of one or two strings",
3631 else if (param ==
"treatasempty")
3633 bool invalid =
false;
3634 if (args(i+1).is_string ())
3639 else if (args(i+1).iscell ())
3653 error (
"%s: TreatAsEmpty must be either a string or cell array of one or two strings",
3658 else if (param ==
"collectoutput")
3660 collect_output = args(i+1).xbool_value (
"%s: CollectOutput must be logical or numeric",
who.c_str ());
3662 else if (param ==
"emptyvalue")
3664 empty_value = args(i+1).xscalar_value (
"%s: EmptyValue must be numeric",
who.c_str ());
3666 else if (param ==
"headerlines")
3668 header_lines = args(i+1).xscalar_value (
"%s: HeaderLines must be numeric",
who.c_str ());
3670 else if (param ==
"bufsize")
3672 buffer_size = args(i+1).xscalar_value (
"%s: BufSize must be numeric",
who.c_str ());
3674 else if (param ==
"multipledelimsasone")
3678 else if (param ==
"returnonerror")
3680 return_on_error = args(i+1).xbool_value (
"%s: ReturnOnError must be logical or numeric",
who.c_str ());
3682 else if (param ==
"whitespace")
3684 whitespace = args(i+1).xstring_value (
"%s: Whitespace must be a character string",
who.c_str ());
3686 else if (param ==
"expchars")
3688 exp_chars = args(i+1).xstring_value (
"%s: ExpChars must be a character string",
who.c_str ());
3691 else if (param ==
"endofline")
3694 std::string s = args(i+1).xstring_value (R
"(%s: EndOfLine must be at most one character or '\r\n')", who.c_str ());
3695 if (args(i+1).is_sq_string ())
3697 int l = s.length ();
3704 eol1 = s.c_str ()[0];
3705 eol2 = s.c_str ()[1];
3713 error (R
"(%s: EndOfLine must be at most one character or '\r\n')",
3717 error (
"%s: unrecognized option '%s'",
who.c_str (), param.c_str ());
3721 for (
unsigned int j = 0; j <
delims.length (); j++)
3730 std::string delim =
delim_list(j).string_value ();
3731 if (delim.length () == 1)
3739 for (
unsigned int i = 0; i <
whitespace.length (); i++)
3754 for (
unsigned int i = 0; i < 256; i++)
3760 for (
unsigned int i = 0; i <
delims.length (); i++)
3770 int c1 = std::istream::traits_type::eof ();
3771 bool found_comment =
false;
3775 found_comment =
false;
3777 while (is && (c1 = is.
get_undelim ()) != std::istream::traits_type::eof ()
3789 char *pos = is.
tellg ();
3798 found_comment =
true;
3803 std::string eol (3,
'\0');
3817 std::string last = end_c.substr (end_c.size () - 1);
3818 std::string may_match (
"");
3825 may_match = may_match + dummy + last;
3826 if (may_match.length () > end_c.length ())
3828 size_t start = may_match.length () - end_c.length ();
3829 may_match = may_match.substr (start);
3832 while (may_match != end_c && is && ! is.
eof ());
3842 while (found_comment);
3844 if (c1 != std::istream::traits_type::eof ())
3856 bool case_sensitive)
const
3862 char *pos = is.
tellg ();
3864 std::string tmp (max_len,
'\0');
3865 char *look = is.
read (&tmp[0], tmp.size (), pos);
3872 int (*compare)(
const char *,
const char *, size_t);
3873 compare = (case_sensitive ?
strncmp : strncasecmp);
3875 for (i = 0; i < targets.
numel (); i++)
3877 std::string s = targets (i).string_value ();
3878 if (! (*compare) (s.c_str (), look, s.size ()))
3880 is.
read (&tmp[0], s.size (), pos);
3885 if (i == targets.
numel ())
3910 != std::istream::traits_type::eof ())
3918 if (c1 != std::istream::traits_type::eof ())
3936 else if (c1 ==
eol2)
3947 != std::istream::traits_type::eof ())
3973 for (
unsigned int i = 0; i < fmt.
width; i++)
3976 if (ch != fmt.
text[i])
3978 if (ch != std::istream::traits_type::eof ())
4026 bool strip_newline,
const std::string& who)
4031 ::error (
"%s: unable to read from stdin while running interactively",
4047 std::istream& is = *isp;
4049 std::ostringstream buf;
4056 while (is && (c = is.get ()) != std::istream::traits_type::eof ())
4063 if (! strip_newline)
4064 buf << static_cast<char> (c);
4068 if (c != std::istream::traits_type::eof ())
4074 if (! strip_newline)
4075 buf << static_cast<char> (c);
4085 if (! strip_newline)
4086 buf << static_cast<char> (c);
4091 buf << static_cast<char> (c);
4093 if (max_len > 0 && char_count == max_len)
4098 if (! is.eof () && char_count > 0)
4103 int disgusting_compatibility_hack = is.get ();
4105 is.putback (disgusting_compatibility_hack);
4108 if (is.good () || (is.eof () && char_count > 0))
4118 if (is.eof () && char_count == 0)
4119 error (who,
"at end of file");
4121 error (who,
"read error");
4130 const std::string& who)
4132 return do_gets (max_len, err,
true, who);
4137 const std::string& who)
4139 return do_gets (max_len, err,
false, who);
4148 ::error (
"%s: unable to read from stdin while running interactively",
4164 std::istream& is = *isp;
4170 while (is && (c = is.get ()) != std::istream::traits_type::eof ())
4173 if (c ==
'\r' || (c ==
'\n' && lastc !=
'\r'))
4183 if (c ==
'\r' && is.peek () ==
'\n')
4189 error (who,
"read error");
4199 template <
typename T>
4209 is >> std::oct >> value >> std::dec;
4214 is >> std::hex >> value >> std::dec;
4219 int c1 = std::istream::traits_type::eof ();
4221 while (is && (c1 = is.get ()) != std::istream::traits_type::eof ()
4225 if (c1 != std::istream::traits_type::eof ())
4229 int c2 = is.peek ();
4231 if (c2 ==
'x' || c2 ==
'X')
4234 if (std::isxdigit (is.peek ()))
4235 is >> std::hex >> value >> std::dec;
4241 if (c2 ==
'0' || c2 ==
'1' || c2 ==
'2'
4242 || c2 ==
'3' || c2 ==
'4' || c2 ==
'5'
4243 || c2 ==
'6' || c2 ==
'7')
4244 is >> std::oct >> value >> std::dec;
4245 else if (c2 ==
'8' || c2 ==
'9')
4277 if ((is.rdstate () & std::ios::failbit) && value != T ())
4278 is.clear (is.rdstate () & ~std::ios::failbit);
4282 if (! (is.rdstate () & std::ios::failbit))
4288 template <
typename T>
4298 auto orig_pos = is.tellg ();
4300 is.width (fmt.
width);
4303 std::istringstream ss (strbuf);
4316 is.seekg (orig_pos, is.beg);
4318 int chars_read = ss.tellg ();
4321 is.width (chars_read);
4328 is.setstate (std::ios::failbit);
4345 return is >> valptr;
4350 octave_scan<> (std::istream& is,
const scanf_format_elt& fmt,
double *valptr)
4352 double& ref = *valptr;
4362 int c1 = std::istream::traits_type::eof ();
4364 while (is && (c1 = is.get ()) != std::istream::traits_type::eof ()
4368 if (c1 != std::istream::traits_type::eof ())
4372 ref = octave_read_value<double> (is);
4385 template <
typename T>
4397 if (idx == max_size && ! discard)
4402 mval.
resize (nr, max_size / nr, 0.0);
4404 mval.
resize (max_size, 1, 0.0);
4412 data[idx++] = *(valptr);
4421 #define DO_WHITESPACE_CONVERSION() \
4424 int c = std::istream::traits_type::eof (); \
4426 while (is && (c = is.get ()) != std::istream::traits_type::eof () \
4430 if (c != std::istream::traits_type::eof ()) \
4435 #define DO_LITERAL_CONVERSION() \
4438 int c = std::istream::traits_type::eof (); \
4440 int n = fmt.length (); \
4443 while (i < n && is && (c = is.get ()) != std::istream::traits_type::eof ()) \
4445 if (c == static_cast<unsigned char> (fmt[i])) \
4458 is.setstate (std::ios::failbit); \
4462 #define DO_PCT_CONVERSION() \
4465 int c = is.get (); \
4467 if (c != std::istream::traits_type::eof ()) \
4472 is.setstate (std::ios::failbit); \
4476 is.setstate (std::ios::failbit); \
4480 #define BEGIN_C_CONVERSION() \
4481 is.unsetf (std::ios::skipws); \
4483 int width = (elt->width ? elt->width : 1); \
4485 std::string tmp (width, '\0'); \
4487 int c = std::istream::traits_type::eof (); \
4490 while (is && n < width \
4491 && (c = is.get ()) != std::istream::traits_type::eof ()) \
4492 tmp[n++] = static_cast<char> (c); \
4494 if (n > 0 && c == std::istream::traits_type::eof ()) \
4501 #define BEGIN_S_CONVERSION() \
4502 int width = elt->width; \
4510 tmp = std::string (width, '\0'); \
4512 int c = std::istream::traits_type::eof (); \
4516 while (is && (c = is.get ()) != std::istream::traits_type::eof ()) \
4518 if (! isspace (c)) \
4520 tmp[n++] = static_cast<char> (c); \
4525 while (is && n < width \
4526 && (c = is.get ()) != std::istream::traits_type::eof ()) \
4534 tmp[n++] = static_cast<char> (c); \
4537 if (n > 0 && c == std::istream::traits_type::eof ()) \
4544 is >> std::ws >> tmp; \
4550 #define BEGIN_CHAR_CLASS_CONVERSION() \
4551 int width = elt->width; \
4558 width = std::numeric_limits<int>::max (); \
4560 std::ostringstream buf; \
4562 std::string char_class = elt->char_class; \
4564 int c = std::istream::traits_type::eof (); \
4566 if (elt->type == '[') \
4568 int chars_read = 0; \
4569 while (is && chars_read++ < width \
4570 && (c = is.get ()) != std::istream::traits_type::eof () \
4571 && char_class.find (c) != std::string::npos) \
4572 buf << static_cast<char> (c); \
4576 int chars_read = 0; \
4577 while (is && chars_read++ < width \
4578 && (c = is.get ()) != std::istream::traits_type::eof () \
4579 && char_class.find (c) == std::string::npos) \
4580 buf << static_cast<char> (c); \
4583 if (width == std::numeric_limits<int>::max () \
4584 && c != std::istream::traits_type::eof ()) \
4590 is.setstate (std::ios::failbit); \
4591 else if (c == std::istream::traits_type::eof ()) \
4597 #define FINISH_CHARACTER_CONVERSION() \
4600 if (encoding ().compare ("utf-8")) \
4601 tmp = string::u8_from_encoding (who, tmp, encoding ()); \
4602 width = tmp.length (); \
4610 conversion_count++; \
4614 if (data_index == max_size) \
4618 if (all_char_conv) \
4620 if (one_elt_size_spec) \
4621 mval.resize (1, max_size, 0.0); \
4623 mval.resize (nr, max_size / nr, 0.0); \
4625 panic_impossible (); \
4628 mval.resize (nr, max_size / nr, 0.0); \
4630 mval.resize (max_size, 1, 0.0); \
4632 data = mval.fortran_vec (); \
4635 data[data_index++] = static_cast<unsigned char> \
4646 bool one_elt_size_spec,
4648 const std::string& who)
4653 ::error (
"%s: unable to read from stdin while running interactively",
4658 conversion_count = 0;
4664 if (nr == 0 || nc == 0)
4666 if (one_elt_size_spec)
4669 return Matrix (nr, nc, 0.0);
4687 if (one_elt_size_spec)
4690 mval.
resize (1, max_size, 0.0);
4699 mval.
resize (nr, nc, 0.0);
4700 max_size = max_conv = nr * nc;
4704 mval.
resize (nr, 32, 0.0);
4716 mval.
resize (nr, nc, 0.0);
4718 max_conv = max_size;
4723 mval.
resize (nr, 32, 0.0);
4730 mval.
resize (32, 1, 0.0);
4738 std::istream& is = *isp;
4742 std::ios::fmtflags flags = is.flags ();
4757 || elt->
type ==
'%')
4758 && max_conv > 0 && conversion_count == max_conv))
4765 if (all_char_conv && one_elt_size_spec)
4768 final_nc = data_index;
4773 final_nc = (data_index - 1) / nr + 1;
4778 else if (data_index == max_size)
4784 if (one_elt_size_spec)
4785 mval.
resize (1, max_size, 0.0);
4787 mval.
resize (nr, max_size / nr, 0.0);
4792 mval.
resize (nr, max_size / nr, 0.0);
4794 mval.
resize (max_size, 1, 0.0);
4799 std::string fmt = elt->
text;
4825 data_index, conversion_count,
4826 nr, max_size, discard);
4834 data_index, conversion_count,
4835 nr, max_size, discard);
4843 data_index, conversion_count,
4844 nr, max_size, discard);
4851 case 'o':
case 'u':
case 'x':
case 'X':
4859 data_index, conversion_count,
4860 nr, max_size, discard);
4868 data_index, conversion_count,
4869 nr, max_size, discard);
4877 data_index, conversion_count,
4878 nr, max_size, discard);
4885 case 'e':
case 'f':
case 'g':
4891 data_index, conversion_count,
4892 nr, max_size, discard);
4923 error (who,
"unsupported format specifier");
4927 error (who,
"internal format error");
4935 else if (is.eof () || ! is)
4939 if (one_elt_size_spec)
4942 final_nc = data_index;
4944 else if (data_index > nr)
4947 final_nc = (data_index - 1) / nr + 1;
4951 final_nr = data_index;
4957 if (data_index > nr)
4960 final_nc = (data_index - 1) / nr + 1;
4964 final_nr = data_index;
4970 final_nr = data_index;
4976 if (is.rdstate () & std::ios::failbit)
4978 error (who,
"format failed to match");
4979 is.clear (is.rdstate () & (~std::ios::failbit));
4985 &&
name () ==
"stdin")
4991 do_gets (-1, err,
false, who);
4999 error (who,
"internal format error");
5003 if (nconv == 0 && ++trips == num_fmt_elts)
5005 if (all_char_conv && one_elt_size_spec)
5008 final_nc = data_index;
5013 final_nc = (data_index - 1) / nr + 1;
5024 elt = fmt_list.
next (nconv > 0
5026 || conversion_count < max_conv));
5031 mval.
resize (final_nr, final_nc, 0.0);
5044 const std::string& who)
5048 conversion_count = 0;
5059 ::error (
"%s: invalid format specified", who.c_str ());
5064 bool one_elt_size_spec;
5066 get_size (size, nr, nc, one_elt_size_spec, who);
5069 conversion_count, who);
5086 std::istream& is = *isp;
5088 std::ios::fmtflags flags = is.flags ();
5092 std::string fmt = elt->
text;
5161 case 'o':
case 'u':
case 'x':
case 'X':
5207 case 'e':
case 'f':
case 'g':
5262 error (who,
"unsupported format specifier");
5266 error (who,
"internal format error");
5271 if (
ok () && is.fail ())
5273 error (
"%s: read error", who.c_str ());
5280 &&
name () ==
"stdin")
5284 do_gets (-1, err,
false, who);
5302 std::istream& is = *isp;
5309 ::error (
"%s: invalid format specified", who.c_str ());
5334 retval(num_values++) = tmp;
5339 elt = fmt_list.
next (nconv > 0);
5343 retval(nconv) = num_values;
5351 if (
ok () &&
len > nconv)
5355 elt = fmt_list.
next ();
5369 const std::string& who,
5375 ::error (
"%s: unable to read from stdin while running interactively",
5388 retval =
scanner.scan (*isp, fmt, ntimes, options, read_count);
5425 : values (args), val_idx (0), elt_idx (0),
5426 n_vals (values.
length ()), n_elts (0), have_data (false),
5453 int int_value (
void);
5455 operator bool ()
const {
return (curr_state == ok); }
5518 for (; idx <
n_elts; idx++)
5520 double dval = val(idx);
5522 if (
math::x_nint (dval) != dval || dval < 0 || dval > 255)
5530 std::string sval (
n,
'\0');
5547 double dval =
retval.double_value ();
5549 if (
math::x_nint (dval) == dval && dval >= 0 && dval < 256)
5550 retval =
static_cast<char> (dval);
5608 template <
typename T>
5611 const char *fmt,
int nsa,
int sa_1,
int sa_2, T arg,
5612 const std::string& who)
5631 ::error (
"%s: internal error handling format", who.c_str ());
5640 int nsa,
int sa_1,
int sa_2,
const std::string& arg,
5641 const std::string& encoding,
const std::string& who)
5644 ::error (
"%s: internal error handling format", who.c_str ());
5646 std::string flags = elt->
flags;
5648 bool left = flags.find (
'-') != std::string::npos;
5650 size_t len = arg.length ();
5652 size_t prec = (nsa > 1 ? sa_2 : (elt->
prec == -1 ?
len : elt->
prec));
5654 std::string print_str = prec < arg.length () ? arg.substr (0, prec) : arg;
5655 if (encoding.compare (
"utf-8"))
5657 size_t src_len = print_str.length ();
5659 len -= src_len - print_str.length ();
5662 size_t fw = (nsa > 0 ? sa_1 : (elt->
fw == -1 ?
len : elt->
fw));
5664 os << std::setw (fw) << (
left ?
std::left : std::right) << print_str;
5666 return len > fw ?
len : fw;
5691 if (ival.
value () <= limit)
5701 if (dval ==
math::fix (dval) && dval <= limit)
5720 return ov_is_ge_zero.
is_true ();
5728 if (dval ==
math::fix (dval) && dval >= 0 && dval <= limit)
5738 std::string tfmt = elt->
text;
5740 tfmt.replace (tfmt.rfind (elt->
type), 1,
"g");
5748 int nsa,
int sa_1,
int sa_2,
5750 const std::string& who)
5754 std::string tfmt = elt->
text;
5760 std::string::size_type i1, i2;
5762 tfmt.replace ((i1 = tfmt.rfind (elt->
type)), 1, 1,
's');
5764 if ((i2 = tfmt.rfind (
'.')) != std::string::npos && i2 < i1)
5766 tfmt.erase (i2, i1-i2);
5767 if (elt->
prec == -2)
5774 if (elt->
flags.find (
'+') != std::string::npos)
5775 tval = (dval < 0 ?
"-Inf" :
"+Inf");
5777 tval = (dval < 0 ?
"-Inf" :
"Inf");
5781 if (elt->
flags.find (
'+') != std::string::npos)
5792 static std::string llmod
5793 = (
sizeof (long) ==
sizeof (int64_t) ?
"l" :
"ll");
5799 case 'd':
case 'i':
case 'c':
5805 tfmt.replace (tfmt.rfind (
type), 1, llmod +
type);
5808 sa_1, sa_2, tval.
value (), who);
5817 sa_1, sa_2, dval, who);
5821 case 'o':
case 'x':
case 'X':
case 'u':
5827 tfmt.replace (tfmt.rfind (
type), 1, llmod +
type);
5830 sa_1, sa_2, tval.
value (), who);
5839 sa_1, sa_2, dval, who);
5843 case 'f':
case 'e':
case 'E':
5849 sa_1, sa_2, dval, who);
5856 error (who,
"invalid format specifier");
5868 ::error (
"%s: invalid field width, must be integer >= 0 and <= INT_MAX",
5875 const std::string& who)
5887 std::ostream& os = *osp;
5898 ::error (
"%s: internal error handling format", who.c_str ());
5901 int nsa = (elt->
fw == -2) + (elt->
prec == -2);
5930 if (elt->
type ==
'%')
5938 else if (elt->
args == 0 && ! elt->
text.empty ())
5946 else if (elt->
type ==
's' || elt->
type ==
'c')
5983 error (who,
"write error");
5987 elt = fmt_list.
next (nconv > 0 && ! val_cache.
exhausted ());
6000 const std::string& who)
6005 ::error (
"%s: invalid format specified", who.c_str ());
6021 std::ostream& os = *osp;
6026 error (who,
"write error");
6040 error (who,
"write error");
6052 err_num = (
m_fail ? -1 : 0);
6066 error (who, std::string (
"stream not open for ") + rw);
6093 const std::string& who)
6105 if (conv_err || max_len < 0)
6108 ::error (
"%s: invalid maximum length specified", who.c_str ());
6112 return getl (max_len, err, who);
6128 const std::string& who)
6140 if (conv_err || max_len < 0)
6143 ::error (
"%s: invalid maximum length specified", who.c_str ());
6147 return gets (max_len, err, who);
6163 const std::string& who)
6180 if (conv_err || count < 0)
6183 ::error (
"%s: invalid number of lines specified", who.c_str ());
6188 return skipl (count, err, who);
6201 off_t orig_pos =
m_rep->tell ();
6208 off_t eof_pos =
m_rep->tell ();
6220 status =
m_rep->seek (offset, origin);
6225 off_t desired_pos =
m_rep->tell ();
6229 if (desired_pos > eof_pos || desired_pos < 0)
6260 off_t xoffset = val.
value ();
6268 std::string xorigin = tc_origin.
xstring_value (
"fseek: invalid value for origin");
6270 if (xorigin ==
"bof")
6272 else if (xorigin ==
"cof")
6274 else if (xorigin ==
"eof")
6287 else if (xorigin == 0)
6289 else if (xorigin == 1)
6297 ::error (
"fseek: invalid value for origin");
6303 error (
"fseek: failed to seek to requested position");
6345 template <
typename T>
6359 template <
typename T>
6373 template <
typename SRC_T,
typename DST_T>
6379 bool do_float_fmt_conv,
bool do_NA_conv,
6382 typedef typename DST_T::element_type dst_elt_type;
6386 dst_elt_type *conv_data = conv.fortran_vec ();
6390 for (
auto it = input_buf_list.cbegin (); it != input_buf_list.cend (); it++)
6392 SRC_T *data =
static_cast<SRC_T *
> (*it);
6394 if (swap || do_float_fmt_conv)
6402 swap_bytes<sizeof (SRC_T)> (&data[i]);
6403 else if (do_float_fmt_conv)
6408 dst_elt_type tmp (data[i]);
6422 swap_bytes<sizeof (SRC_T)> (&data[i]);
6423 else if (do_float_fmt_conv)
6428 conv_data[j] = data[i];
6439 dst_elt_type tmp (data[i]);
6451 conv_data[j] = data[i];
6458 input_buf_list.clear ();
6461 conv_data[i] = dst_elt_type (0);
6469 bool swap,
bool do_float_fmt_conv,
bool do_NA_conv,
6472 #define TABLE_ELT(T, U, V, W) \
6473 conv_fptr_table[oct_data_conv::T][oct_data_conv::U] = convert_and_copy<V, W>
6475 #define FILL_TABLE_ROW(T, V) \
6476 TABLE_ELT (T, dt_int8, V, int8NDArray); \
6477 TABLE_ELT (T, dt_uint8, V, uint8NDArray); \
6478 TABLE_ELT (T, dt_int16, V, int16NDArray); \
6479 TABLE_ELT (T, dt_uint16, V, uint16NDArray); \
6480 TABLE_ELT (T, dt_int32, V, int32NDArray); \
6481 TABLE_ELT (T, dt_uint32, V, uint32NDArray); \
6482 TABLE_ELT (T, dt_int64, V, int64NDArray); \
6483 TABLE_ELT (T, dt_uint64, V, uint64NDArray); \
6484 TABLE_ELT (T, dt_single, V, FloatNDArray); \
6485 TABLE_ELT (T, dt_double, V, NDArray); \
6486 TABLE_ELT (T, dt_char, V, charNDArray); \
6487 TABLE_ELT (T, dt_schar, V, charNDArray); \
6488 TABLE_ELT (T, dt_uchar, V, charNDArray); \
6489 TABLE_ELT (T, dt_logical, V, boolNDArray);
6502 static bool initialized =
false;
6511 for (
int j = 0; j < 14; j++)
6512 conv_fptr_table[i][j] =
nullptr;
6548 switch (output_type)
6567 retval =
fptr (input_buf_list, input_buf_elts, elts_read,
6568 nr, nc, swap, do_float_fmt_conv, do_NA_conv, ffmt);
6573 ::error (
"read: invalid type specification");
6594 bool one_elt_size_spec =
false;
6603 size_t char_count = 0;
6605 ptrdiff_t tmp_count = 0;
6609 get_size (size, nr, nc, one_elt_size_spec,
"fread");
6618 if (one_elt_size_spec)
6641 if (nr == 0 || nc == 0)
6647 bool read_to_eof = elts_to_read < 0;
6654 input_buf_elts = 1024 * 1024;
6656 input_buf_elts = elts_to_read;
6659 input_buf_elts = block_size;
6664 ptrdiff_t input_buf_size
6665 =
static_cast<ptrdiff_t
> (input_buf_elts) * input_elt_size;
6667 assert (input_buf_size >= 0);
6673 error (
"fread: invalid input stream");
6676 std::istream& is = *isp;
6681 if (skip != 0 && is && ! is.eof ())
6683 cur_pos = is.tellg ();
6684 is.seekg (0, is.end);
6685 eof_pos = is.tellg ();
6686 is.seekg (cur_pos, is.beg);
6689 std::list<void *> input_buf_list;
6691 while (is && ! is.eof ()
6692 && (read_to_eof || tmp_count < elts_to_read))
6698 if (remaining_elts < input_buf_elts)
6699 input_buf_size = remaining_elts * input_elt_size;
6702 char *input_buf =
new char [input_buf_size];
6704 is.read (input_buf, input_buf_size);
6706 size_t gcount = is.gcount ();
6708 char_count += gcount;
6715 input_buf_list.push_back (input_buf);
6717 if (skip != 0 && nel == block_size && is)
6721 off_t remaining = eof_pos - cur_pos;
6723 if (remaining < skip)
6725 is.seekg (0, is.end);
6730 is.seekg (skip, is.cur);
6740 nc = tmp_count / nr;
6742 if (tmp_count % nr != 0)
6748 else if (tmp_count == 0)
6753 else if (tmp_count != nr * nc)
6755 if (tmp_count % nr != 0)
6756 nc = tmp_count / nr + 1;
6758 nc = tmp_count / nr;
6765 error (
"fread: number of elements read exceeds max index size");
6770 nr, nc, input_type, output_type, ffmt);
6794 error (
"fwrite: write error");
6802 template <
typename T,
typename V>
6806 const T *tt_data =
static_cast<const T *
> (data);
6808 V *vt_data =
static_cast<V *
> (conv_data);
6811 vt_data[i] = tt_data[i];
6814 template <
typename T,
typename V>
6819 typedef typename V::val_type val_type;
6821 val_type *vt_data =
static_cast<val_type *
> (conv_data);
6828 vt_data[i] = val.value ();
6831 swap_bytes<sizeof (val_type)> (&vt_data[i]);
6835 template <
typename T>
6842 template <
typename T>
6849 template <
typename T>
6868 switch (output_type)
6871 convert_chars<ult_elt_type, char> (data, conv_data, n_elts);
6875 convert_chars<ult_elt_type, signed char> (data, conv_data, n_elts);
6879 convert_chars<ult_elt_type, unsigned char> (data, conv_data, n_elts);
6883 convert_ints<T, octave_int8> (data, conv_data, n_elts, swap);
6887 convert_ints<T, octave_uint8> (data, conv_data, n_elts, swap);
6891 convert_ints<T, octave_int16> (data, conv_data, n_elts, swap);
6895 convert_ints<T, octave_uint16> (data, conv_data, n_elts, swap);
6899 convert_ints<T, octave_int32> (data, conv_data, n_elts, swap);
6903 convert_ints<T, octave_uint32> (data, conv_data, n_elts, swap);
6907 convert_ints<T, octave_int64> (data, conv_data, n_elts, swap);
6911 convert_ints<T, octave_uint64> (data, conv_data, n_elts, swap);
6916 float *vt_data =
static_cast<float *
> (conv_data);
6920 vt_data[i] = data[i];
6922 if (do_float_conversion)
6930 double *vt_data =
static_cast<double *
> (conv_data);
6934 vt_data[i] = data[i];
6936 if (do_float_conversion)
6943 ::error (
"write: invalid type specification");
6952 bool status =
false;
6958 std::ostream& os = *osp;
6962 os.write (
static_cast<const char *
> (data), nbytes);
6975 bool status =
false;
6982 std::ostream& os = *osp;
6986 off_t orig_pos =
tell ();
6990 off_t eof_pos =
tell ();
6995 size_t remaining = eof_pos - orig_pos;
6997 if (remaining < skip)
7002 unsigned char zero = 0;
7003 for (
size_t j = 0; j < skip - remaining; j++)
7004 os.write (
reinterpret_cast<const char *
> (&zero), 1);
7015 template <
typename T>
7029 bool do_data_conversion = (swap || ! is_equivalent_type<T> (output_type)
7037 chunk_size = block_size;
7038 else if (do_data_conversion)
7039 chunk_size = 1024 * 1024;
7045 const T *pdata = data.
data ();
7057 if (chunk_size > remaining_nel)
7058 chunk_size = remaining_nel;
7060 bool status =
false;
7062 if (do_data_conversion)
7069 status =
convert_data (&pdata[i], conv_data, chunk_size,
7070 output_type, flt_fmt);
7076 status =
write_bytes (pdata,
sizeof (T) * chunk_size);
7087 #define INSTANTIATE_WRITE(T) \
7090 stream::write (const Array<T>& data, octave_idx_type block_size, \
7091 oct_data_conv::data_type output_type, \
7092 octave_idx_type skip, \
7093 mach_info::float_format flt_fmt)
7112 #if defined (OCTAVE_HAVE_OVERLOAD_CHAR_INT8_TYPES)
7148 error (who +
": format must be a string");
7182 error (who +
": format must be a string");
7194 ?
m_rep->do_textscan (fmt, ntimes, options, who, count)
7200 const std::string& who)
7212 const std::string& who)
7228 error (who +
": format must be a string");
7260 error (who +
": argument must be a string");
7280 std::string
retval =
"invalid stream object";
7324 std::string
retval =
"???";
7325 std::ios::openmode in_mode =
static_cast<std::ios::openmode
> (
mode);
7327 if (in_mode == std::ios::in)
7329 else if (in_mode == std::ios::out
7332 else if (in_mode == (std::ios::out | std::ios::app))
7334 else if (in_mode == (std::ios::in | std::ios::out))
7338 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate))
7340 else if (in_mode == (std::ios::in | std::ios::binary))
7342 else if (in_mode == (std::ios::out | std::ios::binary)
7345 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary))
7347 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary))
7350 | std::ios::binary))
7352 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate
7353 | std::ios::binary))
7360 : m_list (), m_lookup_cache (m_list.end ()), m_stdin_file (-1),
7361 m_stdout_file (-1), m_stderr_file (-1)
7393 if (stream_number == -1)
7394 return stream_number;
7410 ::error (
"could not create file id");
7412 m_list[stream_number] = os;
7414 return stream_number;
7417 OCTAVE_NORETURN
static
7422 ::error (
"invalid stream number = %d", fid);
7424 ::error (
"%s: invalid stream number = %d", who.c_str (), fid);
7438 ostrl_map::const_iterator iter =
m_list.find (fid);
7440 if (iter ==
m_list.end ())
7451 const std::string& who)
const
7464 auto iter =
m_list.find (fid);
7466 if (iter ==
m_list.end ())
7469 stream os = iter->second;
7511 for (
auto iter =
m_list.begin (); iter !=
m_list.end (); )
7513 int fid = iter->first;
7520 stream os = iter->second;
7526 if (
name.find (
"gnuplot") != std::string::npos)
7555 ostrl_map::const_iterator iter =
m_list.find (fid);
7557 if (iter ==
m_list.end ())
7580 ::error (
"file id must be a file object or integer value");
7585 ::error (
"file id must be a file object or integer value");
7592 std::ostringstream buf;
7595 <<
" number mode arch name\n"
7596 <<
" ------ ---- ---- ----\n";
7598 for (
const auto& fid_strm :
m_list)
7600 stream os = fid_strm.second;
7603 << std::setiosflags (std::ios::right)
7604 << std::setw (4) << fid_strm.first <<
" "
7606 << std::resetiosflags (std::ios::adjustfield)
7614 << os.
name () <<
"\n";
7628 for (
const auto& fid_strm :
m_list)
7631 if (fid_strm.first > 2 && fid_strm.second)
7632 retval(0, num_open++) = fid_strm.first;
7648 for (
const auto& fid_strm :
m_list)
7651 if (fid_strm.first > 2)
7653 stream os = fid_strm.second;
7655 if (os && os.
name () == nm)
7664 ::error (
"file id must be a file object, std::string, or integer value");
7672 ::error (
"file id must be a file object, std::string, or integer value");
charNDArray max(char d, const charNDArray &m)
charNDArray min(char d, const charNDArray &m)
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
octave_idx_type numel(void) const
Number of elements in the array.
const T * data(void) const
Size of the specified dimension.
const T * fortran_vec(void) const
Size of the specified dimension.
size_type size(const size_type d) const
Size of the specified dimension.
bool isempty(void) const
Size of the specified dimension.
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Vector representing the dimensions (size) of an Array.
static size_t data_type_size(data_type dt)
static bool forced_interactive(void)
int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
void field_width_error(const std::string &who) const
virtual std::ostream * output_stream(void)
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)
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)
int do_printf(printf_format_list &fmt_list, const octave_value_list &args, const std::string &who)
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)
std::string do_gets(octave_idx_type max_len, bool &err, bool strip_newline, const std::string &who)
int puts(const std::string &s, const std::string &who)
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)
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
std::string error(bool clear, int &err_num)
virtual std::istream * input_stream(void)
bool do_oscanf(const scanf_format_elt *elt, octave_value &, const std::string &who)
virtual std::string name(void) const =0
std::string encoding(void) const
void invalid_operation(const std::string &who, const char *rw)
off_t skipl(off_t count, bool &err, const std::string &who)
virtual int file_number(void) const
octave_value_list oscanf(const std::string &fmt, const std::string &who)
void clear(std::ios_base::iostate m=(std::ios_base::eofbit &~std::ios_base::eofbit))
std::ios_base::iostate flags
std::streampos buf_in_file
std::ios_base::iostate rdstate(void)
delimited_stream(const delimited_stream &)=delete
void seekg(char *old_idx)
int getline(std::string &dest, char delim)
delimited_stream(std::istream &is, const std::string &delimiters, int longest_lookahead, octave_idx_type bsize=4096)
void setstate(std::ios_base::iostate m)
char * read(char *buffer, int size, char *&new_start)
void progress_benchmark(void)
output_system & get_output_system(void)
void recover_from_exception(void)
bool interactive(void) const
std::ostream & __stdout__(void)
printf_value_cache(const printf_value_cache &)=delete
octave_value get_next_value(char type=0)
~printf_value_cache(void)=default
const octave_value_list values
printf_value_cache(const octave_value_list &args, const std::string &who)
int remove(int fid, const std::string &who="")
std::string list_open_files(void) const
octave_value stdin_file(void) const
int get_file_number(const octave_value &fid) const
ostrl_map::const_iterator m_lookup_cache
void clear(bool flush=true)
octave_value stderr_file(void) const
octave_value open_file_numbers(void) const
stream lookup(int fid, const std::string &who="") const
octave_value stdout_file(void) const
stream_list(interpreter &interp)
string_vector get_info(int fid) const
void invalid_operation(const std::string &who, const char *rw)
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)
int puts(const std::string &s, const std::string &who)
mach_info::float_format float_format(void) const
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_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
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)
std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
bool skip_bytes(size_t n_elts)
std::istream * input_stream(void)
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
bool write_bytes(const void *data, size_t n_elts)
bool stream_ok(bool clear=true) const
int seek(off_t offset, int origin)
octave_value_list oscanf(const std::string &fmt, const std::string &who)
static std::string mode_as_string(int mode)
int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
std::shared_ptr< base_stream > m_rep
std::string name(void) const
std::ostream * output_stream(void)
bool is_valid(void) const
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)
std::string encoding(void)
std::string error(bool clear, int &err_num)
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())
textscan_format_elt(const textscan_format_elt &e)
std::string parse_char_class(const std::string &pattern) const
std::deque< textscan_format_elt * > fmt_elts
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())
const textscan_format_elt * first(void)
int finish_conversion(const std::string &s, size_t &i, size_t n, unsigned int width, int prec, int bitwidth, octave_value &val_type, bool discard, char &type)
std::list< octave_value > output_container
std::list< octave_value > out_buf(void) const
textscan_format_list(const textscan_format_list &)=delete
void process_conversion(const std::string &s, size_t &i, size_t n)
const textscan_format_elt * current(void) const
const textscan_format_elt * next(bool cycle=true)
textscan_format_list(const std::string &fmt=std::string(), const std::string &who="textscan")
~textscan_format_list(void)
int read_first_row(delimited_stream &is, textscan &ts)
octave_idx_type num_conversions(void) const
textscan(const textscan &)=delete
void scan_one(delimited_stream &is, const textscan_format_elt &fmt, octave_value &ov, Array< octave_idx_type > row)
int scan_bracket(delimited_stream &is, const std::string &pattern, std::string &val) const
bool is_delim(unsigned char ch) const
bool multiple_delims_as_one
void scan_cstring(delimited_stream &is, const textscan_format_elt &fmt, std::string &val) const
int skip_whitespace(delimited_stream &is, bool EOLstop=false)
textscan(const std::string &who_arg="textscan", const std::string &encoding="utf-8")
bool whitespace_delim(void) const
int skip_delim(delimited_stream &is)
bool isspace(unsigned int ch) const
void scan_complex(delimited_stream &is, const textscan_format_elt &fmt, Complex &val) const
double read_double(delimited_stream &is, const textscan_format_elt &fmt) const
bool match_literal(delimited_stream &isp, const textscan_format_elt &elem)
void scan_qstring(delimited_stream &is, const textscan_format_elt &fmt, std::string &val)
void scan_string(delimited_stream &is, const textscan_format_elt &fmt, std::string &val) const
int scan_caret(delimited_stream &is, const std::string &pattern, std::string &val) const
int lookahead(delimited_stream &is, const Cell &targets, int max_len, bool case_sensitive=true) const
octave_value scan(std::istream &isp, const std::string &fmt, octave_idx_type ntimes, const octave_value_list &options, octave_idx_type &read_count)
std::string whitespace_table
void parse_options(const octave_value_list &args, textscan_format_list &fmt_list)
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)
octave_idx_type buffer_size
octave_value do_scan(std::istream &isp, textscan_format_list &fmt_list, octave_idx_type ntimes)
std::string read_until(delimited_stream &is, const Cell &delimiters, const std::string &ends) const
virtual bool fast_elem_insert(octave_idx_type n, const octave_value &x)
static octave::stream create(std::istream *arg=nullptr, const std::string &n="")
static octave::stream create(std::ostream *arg, const std::string &n="")
Cell cell_value(void) const
octave_idx_type length(void) const
octave_value isnan(void) const
octave_int64 int64_scalar_value(void) const
octave_idx_type numel(void) const
bool is_scalar_type(void) const
octave_base_value * internal_rep(void) const
bool is_string(void) const
bool is_defined(void) const
bool isinteger(void) const
bool isstruct(void) 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
bool is_uint64_type(void) const
octave_value isinf(void) const
octave_uint64 uint64_scalar_value(void) 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
bool is_single_type(void) const
bool isobject(void) const
std::string xstring_value(const char *fmt,...) const
bool is_sq_string(void) const
double double_value(bool frc_str_conv=false) const
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,...)
#define panic_impossible()
void err_wrong_type_arg(const char *name, const char *s)
ColumnVector transform(const Matrix &m, double x, double y, double z)
int __lo_ieee_is_old_NA(double x)
double __lo_ieee_replace_old_NA(double x)
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
std::complex< double > w(std::complex< double > z, double relerr=0)
std::string float_format_as_string(float_format flt_fmt)
float_format native_float_format(void)
@ flt_fmt_ieee_big_endian
@ flt_fmt_ieee_little_endian
bool words_big_endian(void)
std::complex< T > trunc(const std::complex< T > &x)
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
bool is_NaN_or_NA(const Complex &x)
octave_idx_type nint_big(double x)
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.
OCTAVE_API std::string u8_to_encoding(const std::string &who, const std::string &u8_string, const std::string &encoding)
OCTAVE_API std::string u8_from_encoding(const std::string &who, const std::string &native_string, const std::string &encoding)
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)
static Cell init_inf_nan(void)
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)
static int convert_to_valid_int(const octave_value &tc, int &conv_err)
static uint32_t state[624]
static void convert_ints(const T *data, void *conv_data, octave_idx_type n_elts, bool swap)
std::istream & octave_scan(std::istream &is, const scanf_format_elt &fmt, T *valptr)
static double f(double k, double l_nu, double c_pm)
static OCTAVE_NORETURN void err_invalid_file_id(int fid, const std::string &who)
static bool ok_for_unsigned_int_conv(const octave_value &val)
interpreter & __get_interpreter__(const std::string &who)
std::string do_string_escapes(const std::string &s)
static std::string expand_char_class(const std::string &s)
static bool ok_for_signed_int_conv(const octave_value &val)
std::istream & octave_scan_1(std::istream &is, const scanf_format_elt &fmt, T *valptr)
static bool is_nan_or_inf(const octave_value &val)
double replace_old_NA< double >(double val)
bool is_old_NA< double >(double val)
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)
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)
static std::string switch_to_g_format(const printf_format_elt *elt)
static T replace_old_NA(T val)
size_t format(std::ostream &os, const char *fmt,...)
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)
static 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)
static octave_idx_type get_size(double d, const std::string &who)
static double pown(double x, unsigned int n)
static void convert_chars(const void *data, void *conv_data, octave_idx_type n_elts)
std::string undo_string_escapes(const std::string &s)
std::complex< double > Complex
std::complex< float > FloatComplex
octave_int< T > mod(const octave_int< T > &x, const octave_int< T > &y)
#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()
#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)
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()) ? '\'' :'"'))
octave_value::octave_value(const Array< char > &chm, char type) return retval
OCTINTERP_API octave_value do_cat_op(octave::type_info &ti, const octave_value &a, const octave_value &b, const Array< octave_idx_type > &ra_idx)
OCTINTERP_API octave_value do_binary_op(octave::type_info &ti, octave_value::binary_op op, const octave_value &a, const octave_value &b)
static void parse_options(octave::regexp::opts &options, const octave_value_list &args, const std::string &who, int skip, bool &extra_args)