24 #if defined (HAVE_CONFIG_H) 94 catch (
const octave::execution_exception&)
125 ::error (
"%s: NaN is invalid as size specification", who.c_str ());
132 ::error (
"%s: negative value invalid as size specification",
136 ::
error (
"%s: dimension too large for Octave's index type",
153 one_elt_size_spec =
false;
162 one_elt_size_spec =
true;
166 dnc = (dnr == 0.0) ? 0.0 : 1.0;
168 else if (sz_len == 2)
173 ::error (
"%s: invalid size specification", who.c_str ());
178 ::error (
"%s: invalid size specification", who.c_str ());
191 size_t len =
s.length ();
197 unsigned char c =
s[
i++];
199 if (
c ==
'-' &&
i > 1 &&
i < len
200 && ( static_cast<unsigned char> (
s[
i-2])
201 <= static_cast<unsigned char> (
s[
i])))
206 for (
c =
s[
i-2]+1;
c <
s[
i];
c++)
214 if (
c !=
'-' ||
i == len)
229 whitespace_conversion = 1,
230 literal_conversion = 2,
235 char typ =
'\0',
char mod =
'\0',
237 :
text (txt), width (
w), discard (
d),
type (typ),
238 modifier (
mod), char_class (ch_class)
289 size_t length (
void)
const {
return fmt_elts.size (); }
299 return length () > 0 ? fmt_elts[curr_idx] :
nullptr;
309 if (curr_idx >=
length ())
320 void printme (
void)
const;
322 bool ok (
void)
const {
return (nconv >= 0); }
324 operator bool ()
const {
return ok (); }
326 bool all_character_conversions (
void);
328 bool all_numeric_conversions (
void);
345 void add_elt_to_list (
int width,
bool discard,
char type,
char modifier,
348 void process_conversion (
const std::string&
s,
size_t&
i,
size_t n,
349 int& width,
bool& discard,
char&
type,
352 int finish_conversion (
const std::string&
s,
size_t&
i,
size_t n,
353 int& width,
bool discard,
char&
type,
358 : nconv (0), curr_idx (0), fmt_elts (), buf ()
360 size_t n =
s.length ();
365 bool discard =
false;
366 char modifier =
'\0';
369 bool have_more =
true;
381 have_more = (
buf.tellp () != 0);
383 else if (isspace (
s[
i]))
392 while (++
i < n && isspace (
s[
i]))
407 while (
i < n && ! isspace (
s[
i]) &&
s[
i] !=
'%')
433 for (
size_t i = 0;
i < n;
i++)
451 modifier, char_class);
462 size_t n,
int& width,
bool& discard,
463 char&
type,
char& modifier)
472 bool have_width =
false;
488 case '0':
case '1':
case '2':
case '3':
case '4':
489 case '5':
case '6':
case '7':
case '8':
case '9':
495 width = 10 * width +
c -
'0';
498 while (
i < n && isdigit (
s[
i]))
501 width = 10 * width +
c -
'0';
507 case 'h':
case 'l':
case 'L':
508 if (modifier !=
'\0')
514 case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
522 case 'e':
case 'f':
case 'g':
534 case 'c':
case 's':
case 'p':
case '%':
case '[':
535 if (modifier !=
'\0')
545 type, modifier) == 0)
564 size_t n,
int& width,
bool discard,
565 char&
type,
char modifier)
571 size_t beg_idx = std::string::npos;
572 size_t end_idx = std::string::npos;
604 else if (
s[
i] ==
']')
608 while (
i < n &&
s[
i] !=
']')
611 if (
i < n &&
s[
i] ==
']')
628 if (beg_idx != std::string::npos && end_idx != std::string::npos)
630 end_idx - beg_idx + 1));
643 for (
size_t i = 0;
i < n;
i++)
648 <<
"width: " << elt->
width <<
"\n" 649 <<
"discard: " << elt->
discard <<
"\n" 653 std::cerr <<
"literal text\n";
655 std::cerr <<
"whitespace\n";
657 std::cerr << elt->
type <<
"\n";
660 <<
"modifier: " << elt->
modifier <<
"\n" 673 for (
size_t i = 0;
i < n;
i++)
679 case 'c':
case 's':
case '%':
case '[':
case '^':
703 for (
size_t i = 0;
i < n;
i++)
709 case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
710 case 'e':
case 'f':
case 'g':
732 char typ =
'\0',
char mod =
'\0')
733 :
text (txt), args (n), fw (
w), prec (
p), flags (
f),
791 return length () > 0 ? fmt_elts[curr_idx] :
nullptr;
794 size_t length (
void)
const {
return fmt_elts.size (); }
800 if (curr_idx >=
length ())
813 void printme (
void)
const;
815 bool ok (
void)
const {
return (nconv >= 0); }
817 operator bool ()
const {
return ok (); }
834 void add_elt_to_list (
int args,
const std::string& flags,
int fw,
835 int prec,
char type,
char modifier);
837 void process_conversion (
const std::string&
s,
size_t&
i,
size_t n,
839 int& prec,
char& modifier,
char&
type);
841 void finish_conversion (
const std::string&
s,
size_t&
i,
int args,
843 char modifier,
char&
type);
847 : nconv (0), curr_idx (0), fmt_elts (), buf ()
849 size_t n =
s.length ();
857 char modifier =
'\0';
860 bool have_more =
true;
861 bool empty_buf =
true;
876 empty_buf = (
buf.tellp () == 0);
893 have_more = (
buf.tellp () != 0);
933 for (
size_t i = 0;
i < n;
i++)
942 int fw,
int prec,
char type,
964 int& prec,
char& modifier,
982 case '-':
case '+':
case ' ':
case '0':
case '#':
1010 sscanf (
tmp.c_str (),
"%d%n", &fw, &
nn);
1013 while (
i < n && isdigit (
s[
i]))
1018 if (
i < n &&
s[
i] ==
'.')
1043 sscanf (
tmp.c_str (),
"%d%n", &prec, &
nn);
1046 while (
i < n && isdigit (
s[
i]))
1060 case 'h':
case 'l':
case 'L':
1078 int fw,
int prec,
char modifier,
1083 case 'd':
case 'i':
case 'o':
case 'x':
case 'X':
1085 if (modifier ==
'L')
1092 case 'f':
case 'e':
case 'E':
case 'g':
case 'G':
1093 if (modifier ==
'h' || modifier ==
'l')
1100 case 's':
case 'p':
case '%':
1101 if (modifier !=
'\0')
1114 if (
type !=
'%' || args != 0)
1135 for (
size_t i = 0;
i < n;
i++)
1140 <<
"args: " << elt->
args <<
"\n" 1141 <<
"flags: '" << elt->
flags <<
"'\n" 1142 <<
"width: " << elt->
fw <<
"\n" 1143 <<
"prec: " << elt->
prec <<
"\n" 1144 <<
"type: '" << elt->
type <<
"'\n" 1145 <<
"modifier: '" << elt->
modifier <<
"'\n" 1158 for (
unsigned int d = n;
d;
d >>= 1)
1220 int refresh_buf (
void);
1227 return eof () ? std::istream::traits_type::eof () : *idx++;
1229 return get_undelim ();
1233 int get_undelim (
void);
1236 int peek (
void) {
return eof () ? std::istream::traits_type::eof () : *idx; }
1239 int peek_undelim (
void);
1251 char * read (
char *buffer,
int size,
char* &new_start);
1257 void seekg (
char *old_idx) { idx = old_idx; }
1261 return (eob == buf && i_stream.eof ()) || (flags & std::ios_base::eofbit);
1264 operator const void* (void) {
return (! eof () && ! flags) ?
this :
nullptr; }
1266 bool fail (
void) {
return flags & std::ios_base::failbit; }
1268 std::ios_base::iostate
rdstate (
void) {
return flags; }
1270 void setstate (std::ios_base::iostate m) { flags = flags | m; }
1273 = (std::ios_base::eofbit & ~std::ios_base::eofbit))
1330 int longest_lookahead,
1332 : bufsize (bsize), i_stream (
is), longest (longest_lookahead),
1333 delims (delimiters),
1334 flags (
std::ios::failbit & ~
std::ios::failbit)
1372 return std::istream::traits_type::eof ();
1384 retval = std::istream::traits_type::eof ();
1417 return std::istream::traits_type::eof ();
1424 size_t old_remaining =
eob -
idx;
1428 if (old_remaining > 0)
1431 memmove (
buf,
idx, old_remaining);
1448 eob =
buf + old_remaining + gcount;
1458 retval = std::istream::traits_type::eof ();
1466 if (
delims.find (*
last) != std::string::npos)
1477 if (
retval == std::istream::traits_type::eof ())
1542 for (
i = 0;
i < size && !
eof ();
i++)
1545 memset (buffer, 0, size -
i);
1559 int len = out.length ();
1563 && ch != std::istream::traits_type::eof ())
1587 whitespace_conversion = 1,
1588 literal_conversion = 2
1592 int bw = 0,
bool dis =
false,
char typ =
'\0',
1594 :
text (txt), width (
w), prec (
p), bitwidth (bw),
1595 char_class (ch_class),
type (typ), discard (dis),
1596 numeric (typ ==
'd' || typ ==
'u' ||
type ==
'f' ||
type ==
'n')
1601 bitwidth (
e.bitwidth), char_class (
e.char_class),
type (
e.
type),
1602 discard (
e.discard), numeric (
e.numeric)
1612 bitwidth =
e.bitwidth;
1613 discard =
e.discard;
1615 numeric =
e.numeric;
1616 char_class =
e.char_class;
1676 size_t numel (
void)
const {
return fmt_elts.size (); }
1686 return numel () > 0 ? fmt_elts[curr_idx] :
nullptr;
1693 if (curr_idx >=
numel ())
1704 void printme (
void)
const;
1706 bool ok (
void)
const {
return (nconv >= 0); }
1708 operator const void* (void)
const {
return ok () ?
this :
nullptr; }
1721 std::list<octave_value>
out_buf (
void)
const {
return (output_container); }
1741 void add_elt_to_list (
unsigned int width,
int prec,
int bitwidth,
1746 void process_conversion (
const std::string&
s,
size_t&
i,
size_t n);
1750 int finish_conversion (
const std::string&
s,
size_t&
i,
size_t n,
1751 unsigned int& width,
int& prec,
int& bitwidth,
1753 bool discard,
char&
type);
1855 std::list<octave_value>&
retval,
1888 bool case_sensitive =
true)
const;
1898 return ((delim_table.empty () && (isspace (ch) || ch == eol1 || ch == eol2))
1899 || delim_table[ch] !=
'\0');
1902 bool isspace (
unsigned int ch)
const {
return whitespace_table[ch & 0xff]; }
1910 : who (who_arg), set_from_first (
false), has_string (
false),
1911 nconv (0), curr_idx (0), fmt_elts (), buf ()
1913 size_t n =
s.length ();
1917 unsigned int width = -1;
1920 bool discard =
false;
1923 bool have_more =
true;
1948 if (
s[
i] ==
'%' && (
i+1 == n ||
s[
i+1] !=
'%'))
1960 have_more = (
buf.tellp () != 0);
1962 else if (isspace (
s[
i]))
1964 while (++
i < n && isspace (
s[
i]))
1978 while (
i < n && ! isspace (
s[
i])
1979 && (
s[
i] !=
'%' || (
i+1 < n &&
s[
i+1] ==
'%')))
2010 size_t n =
numel ();
2012 for (
size_t i = 0;
i < n;
i++)
2022 bool discard,
char type,
2027 if (!
text.empty ())
2050 bool discard =
false;
2056 bool have_width =
false;
2072 case '0':
case '1':
case '2':
case '3':
case '4':
2073 case '5':
case '6':
case '7':
case '8':
case '9':
2079 width = width * 10 +
c -
'0';
2082 while (
i < n && isdigit (
s[
i]))
2085 width = width * 10 +
c -
'0';
2089 if (
i < n &&
s[
i] ==
'.')
2093 while (
i < n && isdigit (
s[
i]))
2096 prec = prec * 10 +
c -
'0';
2118 else if (
s[
i] ==
'1' &&
i+1 < n &&
s[
i+1] ==
'6')
2128 else if (
s[
i] ==
'3' &&
i+1 < n &&
s[
i+1] ==
'2')
2134 else if (
s[
i] ==
'6' &&
i+1 < n &&
s[
i+1] ==
'4')
2166 if (
s[
i] ==
'3' &&
i+1 < n &&
s[
i+1] ==
'2')
2173 else if (
s[
i] ==
'6' &&
i+1 < n &&
s[
i+1] ==
'4')
2192 case 's':
case 'q':
case '[':
case 'c':
2206 width =
static_cast<unsigned int> (-1);
2210 discard,
type) == 0)
2216 error (
"%s: '%%%c' is not a valid format specifier",
2217 who.c_str (),
s[
i]);
2249 int in = 0, out = 0;
2250 unsigned char ch, prev = 0;
2262 bool prev_was_range =
false;
2263 bool prev_prev_was_range =
false;
2264 for (; in < len; in++)
2266 bool was_range =
false;
2271 if (prev ==
'-' && in > 1 && isalnum (ch) && ! prev_prev_was_range)
2273 unsigned char start_of_range =
pattern[in-2];
2274 if (start_of_range < ch
2275 && ((isupper (ch) && isupper (start_of_range))
2276 || (islower (ch) && islower (start_of_range))
2277 || (isdigit (ch) && isdigit (start_of_range))
2283 for (
int i = start_of_range;
i <= ch;
i++)
2285 if (mask[
i] ==
'\0')
2295 if (mask[ch]++ == 0)
2299 "%s: [...] contains two '%c's",
2302 if (prev ==
'-' && mask[
'-'] >= 2)
2304 (
"octave:textscan-pattern",
2305 "%s: [...] contains two '-'s outside range expressions",
2309 prev_prev_was_range = prev_was_range;
2310 prev_was_range = was_range;
2316 for (
int i = 0;
i < 256;
i++)
2328 size_t n,
unsigned int& width,
2329 int& prec,
int& bitwidth,
2337 size_t beg_idx = std::string::npos;
2338 size_t end_idx = std::string::npos;
2362 else if (
s[
i] ==
']')
2366 while (
i < n &&
s[
i] !=
']')
2369 if (
i < n &&
s[
i] ==
']')
2382 if (beg_idx != std::string::npos && end_idx != std::string::npos)
2384 end_idx - beg_idx + 1));
2396 size_t n =
numel ();
2398 for (
size_t i = 0;
i < n;
i++)
2403 <<
"width: " << elt->
width <<
"\n" 2404 <<
"digits " << elt->
prec <<
"\n" 2405 <<
"bitwidth: " << elt->
bitwidth <<
"\n" 2406 <<
"discard: " << elt->
discard <<
"\n" 2410 std::cerr <<
"literal text\n";
2412 std::cerr <<
"whitespace\n";
2414 std::cerr << elt->
type <<
"\n";
2431 is.getline (first_line, static_cast<char> (ts.
eol2));
2433 if (! first_line.empty () && first_line.back () == ts.
eol1)
2434 first_line.pop_back ();
2436 std::istringstream strstr (first_line);
2443 int max_empty = 1000;
2449 bool already_skipped_delim =
false;
2452 bool progress =
false;
2469 already_skipped_delim =
true;
2477 if (
val.imag () == 0)
2484 if (! already_skipped_delim)
2503 : who (who_arg), buf (), whitespace_table (), delim_table (),
2504 delims (), comment_style (), comment_len (0), comment_char (-2),
2505 buffer_size (0), date_locale (), inf_nan (
init_inf_nan ()),
2506 empty_value (numeric_limits<
double>::
NaN ()), exp_chars (
"edED"),
2507 header_lines (0), treat_as_empty (), treat_as_empty_len (0),
2508 whitespace (
" \b\t"), eol1 (
'\r'), eol2 (
'\n'),
2509 return_on_error (1), collect_output (
false),
2510 multiple_delims_as_one (
false), default_exp (true), lines (0)
2528 std::ios::iostate
state = isp.rdstate ();
2531 isp.setstate (
state);
2543 error (
"%s: invalid format specified",
who.c_str ());
2546 error (
"%s: no valid format conversion specifiers",
who.c_str ());
2551 getline (isp, dummy, static_cast<char> (
eol2));
2563 else if (ntimes > 0)
2567 buf_size =
std::max (buf_size, ntimes);
2572 max_lookahead, buf_size);
2594 done_after = fmt_list.
numel () + 1;
2599 done_after = fmt_list.
out_buf ().size () + 1;
2601 std::list<octave_value> out = fmt_list.
out_buf ();
2609 bool merge_with_prev[fmt_list.
numel ()];
2614 for (
const auto& col : out)
2616 if (col.type_id () == prev_type
2618 merge_with_prev[conv++] =
true;
2620 merge_with_prev[conv++] =
false;
2622 prev_type = col.type_id ();
2629 error (
"%s: No conversions specified",
who.c_str ());
2634 for (;
row < ntimes || ntimes == -1;
row++)
2636 if (
row == 0 ||
row >= size)
2639 for (
auto& col : out)
2646 if ((
err & ~1) > 0 || !
is || (
lines >= ntimes && ntimes > -1))
2652 error (
"%s: Read error in field %d of row %d",
who.c_str (),
2653 done_after + 1,
row + 1);
2656 bool uneven_columns =
false;
2658 uneven_columns =
true;
2659 else if (isp.eof ())
2662 isp.seekg (-1, std::ios_base::end);
2663 int last_char = isp.get ();
2664 isp.setstate (isp.eofbit);
2665 uneven_columns = (last_char !=
eol1 && last_char !=
eol2);
2674 done_after = out.size () + 1;
2676 int valid_rows = (
row == ntimes
2686 for (
auto& col : out)
2689 if (
i == done_after && uneven_columns)
2706 for (
auto& col : out)
2708 if (! merge_with_prev[conv++])
2710 if (prev_type != -1)
2718 prev_type = col.type_id ();
2722 ra_idx(1) = group_size++;
2742 unsigned int width_left = fmt.
width;
2746 int ch =
is.peek ();
2767 if (ch >=
'0' && ch <=
'9')
2769 while (width_left-- &&
is && (ch =
is.get ()) >=
'0' && ch <=
'9')
2775 if (ch ==
'.' && width_left)
2777 double multiplier = 1;
2778 int precision = fmt.
prec;
2784 if (precision == -1)
2790 for (
i = 0;
i < precision;
i++)
2792 if (width_left-- &&
is && (ch =
is.get ()) >=
'0' && ch <=
'9')
2793 retval += (ch -
'0') * (multiplier *= 0.1);
2802 if ((
i == precision || ! width_left) && (ch =
is.get ()) >=
'5' 2811 while (width_left-- &&
is && (ch =
is.get ()) >=
'0' && ch <=
'9')
2818 bool used_exp =
false;
2819 if (valid && width_left > 1 &&
exp_chars.find (ch) != std::string::npos)
2821 int ch1 =
is.peek ();
2822 if (ch1 ==
'-' || ch1 ==
'+' || (ch1 >=
'0' && ch1 <=
'9'))
2834 else if (ch1 ==
'-')
2842 while (width_left-- &&
is && (ch =
is.get ()) >=
'0' && ch <=
'9')
2844 exp = exp*10 + ch -
'0';
2848 if (ch != std::istream::traits_type::eof () && width_left)
2851 double multiplier =
pown (10, exp);
2861 if (! used_exp && ch != std::istream::traits_type::eof () && width_left)
2865 if (! valid && width_left >= 3)
2881 is.setstate (std::ios::failbit);
2883 is.setstate (
is.rdstate () & ~
std::ios::failbit);
2898 bool as_empty =
false;
2901 int ch =
is.peek ();
2902 if (ch ==
'+' || ch ==
'-')
2905 int ch2 =
is.peek ();
2906 if (ch2 ==
'i' || ch2 ==
'j')
2911 if (
is.peek () ==
'n')
2913 char *pos =
is.tellg ();
2914 std::ios::iostate
state =
is.rdstate ();
2940 char *pos =
is.tellg ();
2941 std::ios::iostate
state =
is.rdstate ();
2970 char *look =
is.read (&look_buf[0], look_buf.size (), pos);
2979 if (!
strncmp (
s.c_str (), look,
s.size ()))
2983 is.read (&look_buf[0],
s.size (), pos);
2990 if (!
is.eof () && ! as_empty)
2995 if (ch ==
'i' || ch ==
'j')
3001 else if (ch ==
'+' || ch ==
'-')
3012 if (
is.peek () ==
'i' ||
is.peek () ==
'j')
3037 int c1 = std::istream::traits_type::eof ();
3038 std::ostringstream obuf;
3040 while (
is && ((c1 = (
is && !
is.eof ())
3042 : std::istream::traits_type::eof ())
3043 != std::istream::traits_type::eof ())
3044 &&
pattern.find (c1) == std::string::npos)
3045 obuf << static_cast<char> (c1);
3049 if (c1 != std::istream::traits_type::eof ())
3071 int last = (!
is.eof ()
3072 ?
is.get_undelim () : std::istream::traits_type::eof ());
3074 if (last != std::istream::traits_type::eof ())
3080 for (
int i = 0;
i < delimiters.
numel ();
i++)
3087 if (may_match == delim)
3098 while (!
done &&
is && !
is.eof ());
3114 unsigned int width = fmt.
width;
3116 for (
i = 0;
i < width;
i++)
3119 if (
i >=
val.length ())
3121 static_cast<size_t> (16)),
'\0');
3124 if (
is_delim (ch) || ch == std::istream::traits_type::eof ())
3141 ends[
i] =
tmp.back ();
3155 int c1 = std::istream::traits_type::eof ();
3156 std::ostringstream obuf;
3158 while (
is &&
pattern.find (c1 =
is.get_undelim ()) != std::string::npos)
3159 obuf << static_cast<char> (c1);
3162 if (c1 != std::istream::traits_type::eof ())
3177 if (
is.peek () !=
'"')
3185 while (
is &&
is.peek () ==
'"')
3205 for (
unsigned int i = 0;
is &&
i < fmt.
width;
i++)
3207 int ch =
is.get_undelim ();
3208 if (ch != std::istream::traits_type::eof ())
3231 if (fmt.
type ==
'f' || fmt.
type ==
'n')
3241 if (ov.
isreal () && v.imag () == 0)
3253 if (ov.
isreal () && v.imag () == 0)
3316 if (fmt.
type ==
'd')
3329 if (fmt.
type ==
'd')
3378 is.clear (
is.rdstate () & ~
std::ios_base::failbit);
3390 std::list<octave_value>&
retval,
3394 std::list<octave_value>::iterator out =
retval.begin ();
3395 bool no_conversions =
true;
3397 bool conversion_failed =
false;
3398 bool nothing_worked =
true;
3402 for (
size_t i = 0;
i < fmt_list.
numel ();
i++)
3404 bool this_conversion_failed =
false;
3413 warning (
"%s: conversion %c not yet implemented",
3434 error (
"Unknown format element '%c'",
elem->type);
3439 if (!
elem->discard)
3440 no_conversions =
false;
3444 is.clear (
is.rdstate () & ~
std::ios::failbit);
3451 this_conversion_failed =
true;
3455 char *pos =
is.tellg ();
3457 this_conversion_failed =
true;
3464 if (!
elem->discard)
3468 char *pos =
is.tellg ();
3486 if (this_conversion_failed)
3488 if (
is.tellg () == pos && ! conversion_failed)
3492 conversion_failed =
true;
3495 this_conversion_failed =
false;
3497 else if (!
done && ! conversion_failed)
3498 nothing_worked =
false;
3502 is.setstate (std::ios::eofbit);
3504 return no_conversions
3505 + (
is.eof () ? 2 : 0)
3506 + (conversion_failed ? 4 : 0)
3507 + (nothing_worked ? 8 : 0);
3515 int last = args.
length ();
3519 error (
"%s: %d parameters given, but only %d values",
3520 who.c_str (), n-n/2, n/2);
3523 bool have_delims =
false;
3524 for (
int i = 0;
i < last;
i += 2)
3526 std::string param = args(
i).xstring_value (
"%s: Invalid parameter type <%s> for parameter %d",
3528 args(
i).type_name ().c_str (),
3532 if (
param ==
"delimiter")
3534 bool invalid =
true;
3535 if (args(
i+1).is_string ())
3539 delims = args(
i+1).string_value ();
3540 if (args(
i+1).is_sq_string ())
3543 else if (args(
i+1).iscell ())
3566 error (
"%s: Delimiters must be either a string or cell array of strings",
3569 else if (
param ==
"commentstyle")
3571 if (args(
i+1).is_string ())
3576 else if (args(
i+1).iscell ())
3583 error (
"%s: CommentStyle must be either a string or cell array of one or two strings",
3587 error (
"%s: CommentStyle must be either a string or cell array of one or two strings",
3598 else if (
param ==
"treatasempty")
3600 bool invalid =
false;
3601 if (args(
i+1).is_string ())
3606 else if (args(
i+1).iscell ())
3620 error (
"%s: TreatAsEmpty must be either a string or cell array of one or two strings",
3625 else if (
param ==
"collectoutput")
3627 collect_output = args(
i+1).xbool_value (
"%s: CollectOutput must be logical or numeric",
who.c_str ());
3629 else if (
param ==
"emptyvalue")
3631 empty_value = args(
i+1).xscalar_value (
"%s: EmptyValue must be numeric",
who.c_str ());
3633 else if (
param ==
"headerlines")
3635 header_lines = args(
i+1).xscalar_value (
"%s: HeaderLines must be numeric",
who.c_str ());
3637 else if (
param ==
"bufsize")
3639 buffer_size = args(
i+1).xscalar_value (
"%s: BufSize must be numeric",
who.c_str ());
3641 else if (
param ==
"multipledelimsasone")
3645 else if (
param ==
"returnonerror")
3647 return_on_error = args(
i+1).xbool_value (
"%s: ReturnOnError must be logical or numeric",
who.c_str ());
3649 else if (
param ==
"whitespace")
3651 whitespace = args(
i+1).xstring_value (
"%s: Whitespace must be a character string",
who.c_str ());
3653 else if (
param ==
"expchars")
3655 exp_chars = args(
i+1).xstring_value (
"%s: ExpChars must be a character string",
who.c_str ());
3658 else if (
param ==
"endofline")
3661 std::string s = args(
i+1).xstring_value (R
"(%s: EndOfLine must be at most one character or '\r\n')", who.c_str ()); 3662 if (args(
i+1).is_sq_string ())
3664 int l =
s.length ();
3671 eol1 =
s.c_str ()[0];
3672 eol2 =
s.c_str ()[1];
3680 error (R
"(%s: EndOfLine must be at most one character or '\r\n')", 3684 error (
"%s: unrecognized option '%s'",
who.c_str (),
param.c_str ());
3688 for (
unsigned int j = 0; j <
delims.length (); j++)
3698 if (delim.length () == 1)
3721 for (
unsigned int i = 0;
i < 256;
i++)
3727 for (
unsigned int i = 0;
i <
delims.length ();
i++)
3737 int c1 = std::istream::traits_type::eof ();
3738 bool found_comment =
false;
3742 found_comment =
false;
3744 while (
is && (c1 =
is.get_undelim ()) != std::istream::traits_type::eof ()
3756 char *pos =
is.tellg ();
3757 std::ios::iostate
state =
is.rdstate ();
3765 found_comment =
true;
3775 c1 =
is.get_undelim ();
3784 std::string last = end_c.substr (end_c.size () - 1);
3792 may_match = may_match + dummy + last;
3793 if (may_match.length () > end_c.length ())
3795 size_t start = may_match.length () - end_c.length ();
3796 may_match = may_match.substr (
start);
3799 while (may_match != end_c &&
is && !
is.eof ());
3809 while (found_comment);
3811 if (c1 != std::istream::traits_type::eof ())
3822 bool case_sensitive)
const 3828 char *pos =
is.tellg ();
3831 char *look =
is.read (&
tmp[0],
tmp.size (), pos);
3838 int (*compare)(
const char *,
const char *,
size_t);
3839 compare = (case_sensitive ?
strncmp : strncasecmp);
3841 for (
i = 0;
i < targets.
numel ();
i++)
3844 if (! (*compare) (
s.c_str (), look,
s.size ()))
3846 is.read (&
tmp[0],
s.size (), pos);
3851 if (
i == targets.
numel ())
3867 if (c1 ==
eol1 &&
is.peek_undelim () ==
eol2)
3875 while (
is && ((c1 =
is.get_undelim ())
3876 != std::istream::traits_type::eof ())
3884 if (c1 != std::istream::traits_type::eof ())
3899 if (
is.peek_undelim () ==
eol2)
3902 else if (c1 ==
eol2)
3913 != std::istream::traits_type::eof ())
3939 for (
unsigned int i = 0;
i < fmt.
width;
i++)
3941 int ch =
is.get_undelim ();
3942 if (ch != fmt.
text[
i])
3944 if (ch != std::istream::traits_type::eof ())
3946 is.setstate (std::ios::failbit);
3964 errmsg = who +
": " + msg;
3994 if (application::interactive () &&
file_number () == 0)
3995 ::error (
"%s: unable to read from stdin while running interactively",
4011 std::istream&
is = *isp;
4013 std::ostringstream buf;
4020 while (
is && (
c =
is.get ()) != std::istream::traits_type::eof ())
4027 if (! strip_newline)
4028 buf << static_cast<char> (
c);
4032 if (
c != std::istream::traits_type::eof ())
4038 if (! strip_newline)
4039 buf << static_cast<char> (
c);
4049 if (! strip_newline)
4050 buf << static_cast<char> (
c);
4055 buf << static_cast<char> (
c);
4057 if (max_len > 0 && char_count == max_len)
4062 if (!
is.eof () && char_count > 0)
4067 int disgusting_compatibility_hack =
is.get ();
4069 is.putback (disgusting_compatibility_hack);
4072 if (
is.good () || (
is.eof () && char_count > 0))
4078 if (
is.eof () && char_count == 0)
4079 error (who,
"at end of file");
4081 error (who,
"read error");
4105 if (application::interactive () &&
file_number () == 0)
4106 ::error (
"%s: unable to read from stdin while running interactively",
4122 std::istream&
is = *isp;
4128 while (
is && (
c =
is.get ()) != std::istream::traits_type::eof ())
4131 if (
c ==
'\r' || (
c ==
'\n' && lastc !=
'\r'))
4141 if (
c ==
'\r' &&
is.peek () ==
'\n')
4147 error (who,
"read error");
4157 template <
typename T>
4167 is >> std::oct >>
value >> std::dec;
4171 is >> std::hex >>
value >> std::dec;
4176 int c1 = std::istream::traits_type::eof ();
4178 while (
is && (c1 =
is.get ()) != std::istream::traits_type::eof ()
4182 if (c1 != std::istream::traits_type::eof ())
4186 int c2 =
is.peek ();
4188 if (c2 ==
'x' || c2 ==
'X')
4191 if (std::isxdigit (
is.peek ()))
4192 is >> std::hex >>
value >> std::dec;
4198 if (c2 ==
'0' || c2 ==
'1' || c2 ==
'2' 4199 || c2 ==
'3' || c2 ==
'4' || c2 ==
'5' 4200 || c2 ==
'6' || c2 ==
'7')
4201 is >> std::oct >>
value >> std::dec;
4202 else if (c2 ==
'8' || c2 ==
'9')
4234 if ((
is.rdstate () & std::ios::failbit) &&
value != T ())
4235 is.clear (
is.rdstate () & ~
std::ios::failbit);
4239 if (! (
is.rdstate () & std::ios::failbit))
4245 template <
typename T>
4258 std::istringstream ss (
tmp);
4276 return is >> valptr;
4283 double& ref = *valptr;
4291 int c1 = std::istream::traits_type::eof ();
4293 while (
is && (c1 =
is.get ()) != std::istream::traits_type::eof ()
4297 if (c1 != std::istream::traits_type::eof ())
4301 ref = octave_read_value<double> (
is);
4314 template <
typename T>
4326 if (idx == max_size && ! discard)
4331 mval.
resize (nr, max_size / nr, 0.0);
4333 mval.
resize (max_size, 1, 0.0);
4341 data[idx++] = *(valptr);
4346 do_scanf_conv (std::istream&,
const scanf_format_elt&,
double*,
4350 #define DO_WHITESPACE_CONVERSION() \ 4353 int c = std::istream::traits_type::eof (); \ 4355 while (is && (c = is.get ()) != std::istream::traits_type::eof () \ 4359 if (c != std::istream::traits_type::eof ()) \ 4364 #define DO_LITERAL_CONVERSION() \ 4367 int c = std::istream::traits_type::eof (); \ 4369 int n = fmt.length (); \ 4372 while (i < n && is && (c = is.get ()) != std::istream::traits_type::eof ()) \ 4374 if (c == static_cast<unsigned char> (fmt[i])) \ 4387 is.setstate (std::ios::failbit); \ 4391 #define DO_PCT_CONVERSION() \ 4394 int c = is.get (); \ 4396 if (c != std::istream::traits_type::eof ()) \ 4401 is.setstate (std::ios::failbit); \ 4405 is.setstate (std::ios::failbit); \ 4409 #define BEGIN_C_CONVERSION() \ 4410 is.unsetf (std::ios::skipws); \ 4412 int width = (elt->width ? elt->width : 1); \ 4414 std::string tmp (width, '\0'); \ 4416 int c = std::istream::traits_type::eof (); \ 4419 while (is && n < width \ 4420 && (c = is.get ()) != std::istream::traits_type::eof ()) \ 4421 tmp[n++] = static_cast<char> (c); \ 4423 if (n > 0 && c == std::istream::traits_type::eof ()) \ 4430 #define BEGIN_S_CONVERSION() \ 4431 int width = elt->width; \ 4439 tmp = std::string (width, '\0'); \ 4441 int c = std::istream::traits_type::eof (); \ 4445 while (is && (c = is.get ()) != std::istream::traits_type::eof ()) \ 4447 if (! isspace (c)) \ 4449 tmp[n++] = static_cast<char> (c); \ 4454 while (is && n < width \ 4455 && (c = is.get ()) != std::istream::traits_type::eof ()) \ 4463 tmp[n++] = static_cast<char> (c); \ 4466 if (n > 0 && c == std::istream::traits_type::eof ()) \ 4473 is >> std::ws >> tmp; \ 4479 #define BEGIN_CHAR_CLASS_CONVERSION() \ 4480 int width = elt->width; \ 4487 width = std::numeric_limits<int>::max (); \ 4489 std::ostringstream buf; \ 4491 std::string char_class = elt->char_class; \ 4493 int c = std::istream::traits_type::eof (); \ 4495 if (elt->type == '[') \ 4497 int chars_read = 0; \ 4498 while (is && chars_read++ < width \ 4499 && (c = is.get ()) != std::istream::traits_type::eof () \ 4500 && char_class.find (c) != std::string::npos) \ 4501 buf << static_cast<char> (c); \ 4505 int chars_read = 0; \ 4506 while (is && chars_read++ < width \ 4507 && (c = is.get ()) != std::istream::traits_type::eof () \ 4508 && char_class.find (c) == std::string::npos) \ 4509 buf << static_cast<char> (c); \ 4512 if (width == std::numeric_limits<int>::max () \ 4513 && c != std::istream::traits_type::eof ()) \ 4519 is.setstate (std::ios::failbit); \ 4520 else if (c == std::istream::traits_type::eof ()) \ 4526 #define FINISH_CHARACTER_CONVERSION() \ 4529 width = tmp.length (); \ 4537 conversion_count++; \ 4541 if (data_index == max_size) \ 4545 if (all_char_conv) \ 4547 if (one_elt_size_spec) \ 4548 mval.resize (1, max_size, 0.0); \ 4550 mval.resize (nr, max_size / nr, 0.0); \ 4552 panic_impossible (); \ 4555 mval.resize (nr, max_size / nr, 0.0); \ 4557 mval.resize (max_size, 1, 0.0); \ 4559 data = mval.fortran_vec (); \ 4562 data[data_index++] = static_cast<unsigned char> \ 4573 bool one_elt_size_spec,
4577 if (octave::application::interactive () &&
file_number () == 0)
4578 ::error (
"%s: unable to read from stdin while running interactively",
4583 conversion_count = 0;
4589 if (nr == 0 || nc == 0)
4591 if (one_elt_size_spec)
4594 return Matrix (nr, nc, 0.0);
4602 double *data =
nullptr;
4613 if (one_elt_size_spec)
4616 mval.
resize (1, max_size, 0.0);
4625 mval.
resize (nr, nc, 0.0);
4626 max_size = max_conv = nr * nc;
4630 mval.
resize (nr, 32, 0.0);
4642 mval.
resize (nr, nc, 0.0);
4644 max_conv = max_size;
4649 mval.
resize (nr, 32, 0.0);
4656 mval.
resize (32, 1, 0.0);
4664 std::istream&
is = *isp;
4668 std::ios::fmtflags flags =
is.flags ();
4683 || elt->
type ==
'%')
4684 && max_conv > 0 && conversion_count == max_conv))
4691 if (all_char_conv && one_elt_size_spec)
4694 final_nc = data_index;
4699 final_nc = (data_index - 1) / nr + 1;
4704 else if (data_index == max_size)
4710 if (one_elt_size_spec)
4711 mval.
resize (1, max_size, 0.0);
4713 mval.
resize (nr, max_size / nr, 0.0);
4718 mval.
resize (nr, max_size / nr, 0.0);
4720 mval.
resize (max_size, 1, 0.0);
4751 data_index, conversion_count,
4752 nr, max_size, discard);
4760 data_index, conversion_count,
4761 nr, max_size, discard);
4769 data_index, conversion_count,
4770 nr, max_size, discard);
4777 case 'o':
case 'u':
case 'x':
4785 data_index, conversion_count,
4786 nr, max_size, discard);
4794 data_index, conversion_count,
4795 nr, max_size, discard);
4803 data_index, conversion_count,
4804 nr, max_size, discard);
4811 case 'e':
case 'f':
case 'g':
4816 data_index, conversion_count,
4817 nr, max_size, discard);
4848 error (
"%s: unsupported format specifier", who.c_str ());
4852 error (
"%s: internal format error", who.c_str ());
4864 if (one_elt_size_spec)
4867 final_nc = data_index;
4869 else if (data_index > nr)
4872 final_nc = (data_index - 1) / nr + 1;
4876 final_nr = data_index;
4882 if (data_index > nr)
4885 final_nc = (data_index - 1) / nr + 1;
4889 final_nr = data_index;
4895 final_nr = data_index;
4901 if (
is.rdstate () & std::ios::failbit)
4902 is.clear (
is.rdstate () & (~
std::ios::failbit));
4905 if (octave::application::interactive ()
4906 && ! octave::application::forced_interactive ()
4907 &&
name () ==
"stdin")
4921 error (
"%s: internal format error", who.c_str ());
4925 if (nconv == 0 && ++trips == num_fmt_elts)
4927 if (all_char_conv && one_elt_size_spec)
4930 final_nc = data_index;
4935 final_nc = (data_index - 1) / nr + 1;
4946 elt = fmt_list.
next (nconv > 0
4948 || conversion_count < max_conv));
4955 mval.
resize (final_nr, final_nc, 0.0);
4973 conversion_count = 0;
4984 ::
error (
"%s: invalid format specified", who.c_str ());
4989 bool one_elt_size_spec;
4991 get_size (size, nr, nc, one_elt_size_spec, who);
4994 conversion_count, who);
5011 std::istream&
is = *isp;
5013 std::ios::fmtflags flags =
is.flags ();
5086 case 'o':
case 'u':
case 'x':
5132 case 'e':
case 'f':
case 'g':
5186 error (
"%s: unsupported format specifier", who.c_str ());
5190 error (
"%s: internal format error", who.c_str ());
5195 if (
ok () &&
is.fail ())
5197 error (
"%s: read error", who.c_str ());
5201 if (octave::application::interactive ()
5202 && ! octave::application::forced_interactive ()
5203 &&
name () ==
"stdin")
5225 std::istream&
is = *isp;
5232 ::error (
"%s: invalid format specified", who.c_str ());
5256 if (
tmp.is_defined ())
5262 elt = fmt_list.
next (nconv > 0);
5266 retval(nconv) = num_values;
5274 if (
ok () && len > nconv)
5278 elt = fmt_list.
next ();
5295 if (octave::application::interactive () &&
file_number () == 0)
5296 ::error (
"%s: unable to read from stdin while running interactively",
5346 :
values (args), val_idx (0), elt_idx (0),
5354 if (
val.isstruct () ||
val.iscell () ||
val.isobject ())
5371 int int_value (
void);
5373 operator bool ()
const {
return (curr_state == ok); }
5436 for (; idx <
n_elts; idx++)
5438 double dval =
val(idx);
5468 retval =
static_cast<char> (dval);
5514 double dval =
val.double_value (
true);
5526 template <
typename T>
5548 ::error (
"%s: internal error handling format", who.c_str ());
5561 ::error (
"%s: internal error handling format", who.c_str ());
5565 bool left = flags.find (
'-') != std::string::npos;
5569 size_t fw = (nsa > 0 ? sa_1 : (elt->
fw == -1 ? len : elt->
fw));
5570 size_t prec = (nsa > 1 ? sa_2 : (elt->
prec == -1 ? len : elt->
prec));
5572 os << std::setw (fw)
5574 << (prec < len ?
arg.substr (0, prec) :
arg);
5576 return len > fw ? len : fw;
5593 if (
val.is_string ())
5595 else if (
val.isinteger ())
5597 if (
val.is_uint64_type ())
5601 if (ival.
value () <= limit)
5609 double dval =
val.double_value (
true);
5621 if (
val.is_string ())
5623 else if (
val.isinteger ())
5630 return ov_is_ge_zero.
is_true ();
5634 double dval =
val.double_value (
true);
5650 tfmt.replace (tfmt.rfind (elt->
type), 1,
"g");
5658 int nsa,
int sa_1,
int sa_2,
5668 double dval =
val.double_value ();
5670 std::string::size_type i1, i2;
5672 tfmt.replace ((i1 = tfmt.rfind (elt->
type)), 1, 1,
's');
5674 if ((i2 = tfmt.rfind (
'.')) != std::string::npos && i2 < i1)
5676 tfmt.erase (i2, i1-i2);
5677 if (elt->
prec == -2)
5684 if (elt->
flags.find (
'+') != std::string::npos)
5685 tval = (dval < 0 ?
"-Inf" :
"+Inf");
5687 tval = (dval < 0 ?
"-Inf" :
"Inf");
5691 if (elt->
flags.find (
'+') != std::string::npos)
5702 = (
sizeof (long) ==
sizeof (int64_t) ?
"l" :
"ll");
5708 case 'd':
case 'i':
case 'c':
5714 tfmt.replace (tfmt.rfind (
type), 1, llmod +
type);
5717 tval.
value (), who);
5723 double dval =
val.double_value (
true);
5726 sa_1, sa_2, dval, who);
5730 case 'o':
case 'x':
case 'X':
case 'u':
5736 tfmt.replace (tfmt.rfind (
type), 1, llmod +
type);
5739 tval.
value (), who);
5745 double dval =
val.double_value (
true);
5748 sa_1, sa_2, dval, who);
5752 case 'f':
case 'e':
case 'E':
5755 double dval =
val.double_value (
true);
5765 error (
"%s: invalid format specifier", who.c_str ());
5789 std::ostream&
os = *osp;
5800 ::error (
"%s: internal error handling format", who.c_str ());
5803 int nsa = (elt->
fw == -2) + (elt->
prec == -2);
5826 if (elt->
type ==
'%')
5831 else if (elt->
args == 0 && ! elt->
text.empty ())
5836 else if (elt->
type ==
's' || elt->
type ==
'c')
5842 if (
val.is_string ())
5862 if (!
val.isempty ())
5872 error (
"%s: write error", who.c_str ());
5876 elt = fmt_list.
next (nconv > 0 && ! val_cache.
exhausted ());
5894 ::
error (
"%s: invalid format specified", who.c_str ());
5910 std::ostream&
os = *osp;
5915 error (
"%s: write error", who.c_str ());
5929 error (
"%s: write error", who.c_str ());
5941 err_num = (
fail ? -1 : 0);
6031 if (conv_err || max_len < 0)
6034 ::error (
"%s: invalid maximum length specified", who.c_str ());
6038 return getl (max_len,
err, who);
6066 if (conv_err || max_len < 0)
6069 ::error (
"%s: invalid maximum length specified", who.c_str ());
6073 return gets (max_len,
err, who);
6106 if (conv_err || count < 0)
6109 ::error (
"%s: invalid number of lines specified", who.c_str ());
6127 off_t orig_pos =
rep->
tell ();
6146 status =
rep->
seek (offset, origin);
6151 off_t desired_pos =
rep->
tell ();
6155 if (desired_pos > eof_pos || desired_pos < 0)
6186 off_t xoffset =
val.value ();
6196 if (xorigin ==
"bof")
6198 else if (xorigin ==
"cof")
6200 else if (xorigin ==
"eof")
6213 else if (xorigin == 0)
6215 else if (xorigin == 1)
6223 ::error (
"fseek: invalid value for origin");
6229 error (
"fseek: failed to seek to requested position");
6271 template <
typename T>
6285 template <
typename T>
6299 template <
typename SRC_T,
typename DST_T>
6305 bool do_float_fmt_conv,
bool do_NA_conv,
6308 typedef typename DST_T::element_type dst_elt_type;
6312 dst_elt_type *conv_data = conv.fortran_vec ();
6316 for (std::list<void *>::const_iterator it = input_buf_list.begin ();
6317 it != input_buf_list.end (); it++)
6319 SRC_T *data =
static_cast<SRC_T *
> (*it);
6321 if (
swap || do_float_fmt_conv)
6329 swap_bytes<sizeof (SRC_T)> (&data[
i]);
6330 else if (do_float_fmt_conv)
6335 dst_elt_type
tmp (data[
i]);
6349 swap_bytes<sizeof (SRC_T)> (&data[
i]);
6350 else if (do_float_fmt_conv)
6355 conv_data[j] = data[
i];
6366 dst_elt_type
tmp (data[
i]);
6378 conv_data[j] = data[
i];
6385 input_buf_list.clear ();
6388 conv_data[
i] = dst_elt_type (0);
6396 bool swap,
bool do_float_fmt_conv,
bool do_NA_conv,
6399 #define TABLE_ELT(T, U, V, W) \ 6400 conv_fptr_table[oct_data_conv::T][oct_data_conv::U] = convert_and_copy<V, W> 6402 #define FILL_TABLE_ROW(T, V) \ 6403 TABLE_ELT (T, dt_int8, V, int8NDArray); \ 6404 TABLE_ELT (T, dt_uint8, V, uint8NDArray); \ 6405 TABLE_ELT (T, dt_int16, V, int16NDArray); \ 6406 TABLE_ELT (T, dt_uint16, V, uint16NDArray); \ 6407 TABLE_ELT (T, dt_int32, V, int32NDArray); \ 6408 TABLE_ELT (T, dt_uint32, V, uint32NDArray); \ 6409 TABLE_ELT (T, dt_int64, V, int64NDArray); \ 6410 TABLE_ELT (T, dt_uint64, V, uint64NDArray); \ 6411 TABLE_ELT (T, dt_single, V, FloatNDArray); \ 6412 TABLE_ELT (T, dt_double, V, NDArray); \ 6413 TABLE_ELT (T, dt_char, V, charNDArray); \ 6414 TABLE_ELT (T, dt_schar, V, charNDArray); \ 6415 TABLE_ELT (T, dt_uchar, V, charNDArray); \ 6416 TABLE_ELT (T, dt_logical, V, boolNDArray); 6438 for (
int j = 0; j < 14; j++)
6439 conv_fptr_table[
i][j] =
nullptr;
6475 switch (output_type)
6494 retval =
fptr (input_buf_list, input_buf_elts, elts_read,
6495 nr, nc,
swap, do_float_fmt_conv, do_NA_conv, ffmt);
6500 ::error (
"read: invalid type specification");
6518 bool one_elt_size_spec =
false;
6530 size_t char_count = 0;
6532 ptrdiff_t tmp_count = 0;
6536 get_size (size, nr, nc, one_elt_size_spec,
"fread");
6545 if (one_elt_size_spec)
6568 if (nr == 0 || nc == 0)
6577 bool read_to_eof = elts_to_read < 0;
6584 input_buf_elts = 1024 * 1024;
6586 input_buf_elts = elts_to_read;
6589 input_buf_elts = block_size;
6594 ptrdiff_t input_buf_size =
6595 static_cast<ptrdiff_t
> (input_buf_elts) * input_elt_size;
6597 assert (input_buf_size >= 0);
6604 error (
"fread: invalid input stream");
6607 std::istream&
is = *isp;
6609 std::list <void *> input_buf_list;
6611 while (
is && !
is.eof ()
6612 && (read_to_eof || tmp_count < elts_to_read))
6618 if (remaining_elts < input_buf_elts)
6619 input_buf_size = remaining_elts * input_elt_size;
6622 char *input_buf =
new char [input_buf_size];
6624 is.read (input_buf, input_buf_size);
6626 size_t gcount =
is.gcount ();
6628 char_count += gcount;
6634 input_buf_list.push_back (input_buf);
6636 if (
is && skip != 0 && nel == block_size)
6641 off_t orig_pos =
tell ();
6645 off_t eof_pos =
tell ();
6651 off_t remaining = eof_pos - orig_pos;
6653 if (remaining < skip)
6667 nc = tmp_count / nr;
6669 if (tmp_count % nr != 0)
6675 else if (tmp_count == 0)
6680 else if (tmp_count != nr * nc)
6682 if (tmp_count % nr != 0)
6683 nc = tmp_count / nr + 1;
6685 nc = tmp_count / nr;
6692 error (
"fread: number of elements read exceeds max index size");
6697 nr, nc, input_type, output_type, ffmt);
6721 error (
"fwrite: write error");
6729 template <
typename T,
typename V>
6733 const T *tt_data =
static_cast<const T *
> (data);
6735 V *vt_data =
static_cast<V *
> (conv_data);
6738 vt_data[
i] = tt_data[
i];
6741 template <
typename T,
typename V>
6746 typedef typename V::val_type val_type;
6748 val_type *vt_data =
static_cast<val_type *
> (conv_data);
6755 vt_data[
i] =
val.value ();
6758 swap_bytes<sizeof (val_type)> (&vt_data[
i]);
6762 template <
typename T>
6769 template <
typename T>
6776 template <
typename T>
6795 switch (output_type)
6798 convert_chars<ult_elt_type, char> (data, conv_data, n_elts);
6802 convert_chars<ult_elt_type, signed char> (data, conv_data, n_elts);
6806 convert_chars<ult_elt_type, unsigned char> (data, conv_data, n_elts);
6810 convert_ints<T, octave_int8> (data, conv_data, n_elts,
swap);
6814 convert_ints<T, octave_uint8> (data, conv_data, n_elts,
swap);
6818 convert_ints<T, octave_int16> (data, conv_data, n_elts,
swap);
6822 convert_ints<T, octave_uint16> (data, conv_data, n_elts,
swap);
6826 convert_ints<T, octave_int32> (data, conv_data, n_elts,
swap);
6830 convert_ints<T, octave_uint32> (data, conv_data, n_elts,
swap);
6834 convert_ints<T, octave_int64> (data, conv_data, n_elts,
swap);
6838 convert_ints<T, octave_uint64> (data, conv_data, n_elts,
swap);
6843 float *vt_data =
static_cast<float *
> (conv_data);
6847 vt_data[
i] = data[
i];
6849 if (do_float_conversion)
6857 double *vt_data =
static_cast<double *
> (conv_data);
6861 vt_data[
i] = data[
i];
6863 if (do_float_conversion)
6870 ::error (
"write: invalid type specification");
6879 bool status =
false;
6885 std::ostream&
os = *osp;
6889 os.
write (static_cast<const char *> (data), nbytes);
6902 bool status =
false;
6909 std::ostream&
os = *osp;
6913 off_t orig_pos =
tell ();
6917 off_t eof_pos =
tell ();
6922 size_t remaining = eof_pos - orig_pos;
6924 if (remaining < skip)
6929 unsigned char zero = 0;
6930 for (
size_t j = 0; j < skip - remaining; j++)
6931 os.
write (reinterpret_cast<const char *> (&
zero), 1);
6942 template <
typename T>
6956 bool do_data_conversion = (
swap || ! is_equivalent_type<T> (output_type)
6964 chunk_size = block_size;
6965 else if (do_data_conversion)
6966 chunk_size = 1024 * 1024;
6972 const T *pdata = data.
data ();
6984 if (chunk_size > remaining_nel)
6985 chunk_size = remaining_nel;
6987 bool status =
false;
6989 if (do_data_conversion)
7003 status =
write_bytes (pdata,
sizeof (T) * chunk_size);
7014 #define INSTANTIATE_WRITE(T) \ 7017 stream::write (const Array<T>& data, octave_idx_type block_size, \ 7018 oct_data_conv::data_type output_type, \ 7019 octave_idx_type skip, \ 7020 mach_info::float_format flt_fmt) 7039 #if defined (OCTAVE_HAVE_OVERLOAD_CHAR_INT8_TYPES) 7075 error (who +
": format must be a string");
7109 error (who +
": format must be a string");
7155 error (who +
": format must be a string");
7187 error (who +
": argument must be a string");
7252 std::ios::openmode in_mode =
static_cast<std::ios::openmode
> (
mode);
7254 if (in_mode == std::ios::in)
7256 else if (in_mode == std::ios::out
7259 else if (in_mode == (std::ios::out | std::ios::app))
7261 else if (in_mode == (std::ios::in | std::ios::out))
7265 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate))
7267 else if (in_mode == (std::ios::in | std::ios::binary))
7269 else if (in_mode == (std::ios::out | std::ios::binary)
7272 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary))
7274 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary))
7277 | std::ios::binary))
7279 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate
7280 | std::ios::binary))
7287 :
list (), lookup_cache (
list.end ()), m_stdin_file (-1),
7288 m_stdout_file (-1), m_stderr_file (-1)
7317 if (stream_number == -1)
7318 return stream_number;
7333 if (
list.size () >=
list.max_size ())
7334 ::
error (
"could not create file id");
7336 list[stream_number] =
os;
7338 return stream_number;
7341 OCTAVE_NORETURN
static 7346 ::
error (
"invalid stream number = %d",
fid);
7348 ::error (
"%s: invalid stream number = %d", who.c_str (),
fid);
7362 ostrl_map::const_iterator iter =
list.find (
fid);
7364 if (iter ==
list.end ())
7377 int i = get_file_number (
fid);
7388 ostrl_map::iterator iter =
list.find (
fid);
7390 if (iter ==
list.end ())
7395 lookup_cache =
list.end ();
7410 if (
fid.is_string () &&
fid.string_value () ==
"all")
7418 int i = get_file_number (
fid);
7426 void stream_list::clear (
bool flush)
7435 for (ostrl_map::iterator iter =
list.begin (); iter !=
list.end (); )
7437 int fid = iter->first;
7450 if (
name.find (
"gnuplot") != std::string::npos)
7461 list.erase (iter++);
7464 lookup_cache =
list.end ();
7475 if (lookup_cache !=
list.end () && lookup_cache->first ==
fid)
7476 os = lookup_cache->second;
7479 ostrl_map::const_iterator iter =
list.find (
fid);
7481 if (iter ==
list.end ())
7485 lookup_cache = iter;
7505 ::error (
"file id must be a file object or integer value");
7507 return get_info (int_fid);
7512 std::ostringstream buf;
7515 <<
" number mode arch name\n" 7516 <<
" ------ ---- ---- ----\n";
7518 for (
const auto& fid_strm :
list)
7524 << std::setw (4) << fid_strm.first <<
" " 7526 << std::resetiosflags (std::ios::adjustfield)
7529 << stream::mode_as_string (
os.
mode ())
7548 for (
const auto& fid_strm :
list)
7551 if (fid_strm.first > 2 && fid_strm.second)
7552 retval(0, num_open++) = fid_strm.first;
7564 if (
fid.is_string ())
7568 for (
const auto& fid_strm :
list)
7571 if (fid_strm.first > 2)
7590 ::error (
"file id must be a file object, std::string, or integer value");
static T replace_old_NA(T val)
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)
std::string error(bool clear, int &err_num)
std::string do_gets(octave_idx_type max_len, bool &err, bool strip_newline, const std::string &who)
off_t skipl(off_t count, bool &err, const std::string &who)
void warning_with_id(const char *id, const char *fmt,...)
std::istream & octave_scan(std::istream &is, const scanf_format_elt &fmt, T *valptr)
bool isspace(unsigned int ch) const
bool multiple_delims_as_one
bool stream_ok(bool clear=true) const
#define DO_LITERAL_CONVERSION()
F77_RET_T const F77_INT const F77_INT const F77_INT const F77_DBLE const F77_DBLE F77_INT F77_DBLE * V
#define DO_PCT_CONVERSION()
int do_printf(printf_format_list &fmt_list, const octave_value_list &args, const std::string &who)
octave_base_value * internal_rep(void) const
static std::string expand_char_class(const std::string &s)
std::list< octave_value > output_container
bool write_bytes(const void *data, size_t n_elts)
void scan_one(delimited_stream &is, const textscan_format_elt &fmt, octave_value &ov, Array< octave_idx_type > row)
const octave_value_list values
void clear(bool flush=true)
~textscan_format_list(void)
double __lo_ieee_replace_old_NA(double x)
const octave_base_value const Array< octave_idx_type > & ra_idx
std::string string_value(bool force=false) const
void parse_options(const octave_value_list &args, textscan_format_list &fmt_list)
octave_idx_type num_conversions(void) const
void scan_string(delimited_stream &is, const textscan_format_elt &fmt, std::string &val) const
The value of lines which begin with a space character are not saved in the history list A value of all commands are saved on the history list
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
const T * data(void) const
static void recover_from_exception(void)
bool whitespace_delim(void) const
const textscan_format_elt * current(void) const
void setstate(std::ios_base::iostate m)
octave_idx_type buffer_size
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)
void seekg(char *old_idx)
identity matrix If supplied two scalar respectively For allows like xample val
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
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)
OCTINTERP_API std::string undo_string_escapes(const std::string &s)
octave_value get_next_value(char type=0)
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the IEEE symbol zero divided by zero($0/0$)
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
textscan(const std::string &who_arg="textscan")
void clear(std::ios_base::iostate m=(std::ios_base::eofbit &~std::ios_base::eofbit))
octave_int< T > mod(const octave_int< T > &x, const octave_int< T > &y)
const T * fortran_vec(void) const
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)
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)
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())
void error(const char *fmt,...)
void invalid_operation(const std::string &who, const char *rw)
textscan_format_list(const std::string &fmt=std::string(), const std::string &who="textscan")
static Cell init_inf_nan(void)
static double pown(double x, unsigned int n)
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
textscan_format_elt(const textscan_format_elt &e)
octave_value textscan(const std::string &fmt, octave_idx_type ntimes, const octave_value_list &options, const std::string &who, octave_idx_type &count)
octave_int64 xint64_scalar_value(const char *fmt,...) const
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
octave::mach_info::float_format flt_fmt
virtual bool fast_elem_insert(octave_idx_type n, const octave_value &x)
bool is_defined(void) const
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
bool skip_bytes(size_t n_elts)
octave_value_list oscanf(const std::string &fmt, const std::string &who)
refcount< octave_idx_type > count
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
#define BEGIN_S_CONVERSION()
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)
#define DO_WHITESPACE_CONVERSION()
in this the arguments are accumulated from left to right
static OCTAVE_NORETURN void err_invalid_file_id(int fid, const std::string &who)
octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
int do_printf_conv(std::ostream &os, const char *fmt, int nsa, int sa_1, int sa_2, T arg, const std::string &who)
when function syntax is used
std::istream * input_stream(void)
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
void scan_complex(delimited_stream &is, const textscan_format_elt &fmt, Complex &val) const
std::streampos buf_in_file
cell array If invoked with two or more scalar integer or a vector of integer values
double read_double(delimited_stream &is, const textscan_format_elt &fmt) const
octave_value resize(const dim_vector &dv, bool fill=false) const
std::deque< textscan_format_elt * > fmt_elts
void scan_qstring(delimited_stream &is, const textscan_format_elt &fmt, std::string &val)
static octave_idx_type get_size(double d, const std::string &who)
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)
bool do_oscanf(const scanf_format_elt *elt, octave_value &, const std::string &who)
stream lookup(int fid, const std::string &who="") const
static Matrix null(const Matrix &A, octave_idx_type &rank)
std::ostream * output_stream(void)
virtual off_t tell(void)=0
int skip_delim(delimited_stream &is)
std::string error(bool clear, int &err_num)
nd deftypefn *std::string name
static void convert_ints(const T *data, void *conv_data, octave_idx_type n_elts, bool swap)
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
static bool ok_for_unsigned_int_conv(const octave_value &val)
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)
static size_t do_printf_string(std::ostream &os, const octave::printf_format_elt *elt, int nsa, int sa_1, int sa_2, const std::string &arg, const std::string &who)
virtual int file_number(void) const
octave_idx_type numel(const octave_value_list &idx)
bool is_valid(void) const
int skip_whitespace(delimited_stream &is, bool EOLstop=false)
printf_value_cache(const octave_value_list &args, const std::string &who)
std::list< octave_value > out_buf(void) const
OCTINTERP_API octave_value do_binary_op(octave::type_info &ti, octave_value::binary_op op, const octave_value &a, const octave_value &b)
mach_info::float_format float_format(void) const
int puts(const std::string &s, const std::string &who)
std::complex< double > w(std::complex< double > z, double relerr=0)
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, octave::mach_info::float_format from_flt_fmt)
int lookahead(delimited_stream &is, const Cell &targets, int max_len, bool case_sensitive=true) const
double scalar_value(bool frc_str_conv=false) const
float_format native_float_format(void)
void invalid_operation(const std::string &who, const char *rw)
off_t skipl(off_t count, bool &err, const std::string &who)
octave_value do_scan(std::istream &isp, textscan_format_list &fmt_list, octave_idx_type ntimes)
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)
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())
double replace_old_NA< double >(double val)
std::complex< T > trunc(const std::complex< T > &x)
size_type size(const size_type d) const
Size of the specified dimension.
#define panic_impossible()
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, octave::mach_info::float_format from_flt_fmt)
std::string read_until(delimited_stream &is, const Cell &delimiters, const std::string &ends) const
const textscan_format_elt * first(void)
std::istream & octave_scan_1(std::istream &is, const scanf_format_elt &fmt, T *valptr)
static void parse_options(octave::regexp::opts &options, const octave_value_list &args, const std::string &who, int skip, bool &extra_args)
void process_conversion(const std::string &s, size_t &i, size_t n)
virtual int seek(off_t offset, int origin)=0
returns the type of the matrix and caches it for future use Called with more than one the function will not attempt to guess the type if it is still unknown This is useful for debugging purposes The possible matrix types depend on whether the matrix is full or and can be one of the following able sis tem and mark type as unknown tem as the structure of the matrix explicitly gives this(Sparse matrices only) tem code
Array< std::string > param
std::string name(void) const
OCTINTERP_API size_t octave_format(std::ostream &os, const char *fmt,...)
int puts(const std::string &s, const std::string &who)
is longer than or if then or only for unique occurrences of the complete pattern(false). The default is true. If a cell array of strings ar
bool words_big_endian(void)
std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
std::ios_base::iostate rdstate(void)
octave_value convert_to_str(bool pad=false, bool force=false, char type='\'') const
void err_wrong_type_arg(const char *name, const char *s)
With real return the complex result
const textscan_format_elt * next(bool cycle=true)
static octave::stream create(std::istream *arg=nullptr, const std::string &n="")
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)
static uint32_t state[624]
virtual std::istream * input_stream(void)
int seek(off_t offset, int origin)
int read_first_row(delimited_stream &is, textscan &ts)
static octave::stream create(std::ostream *arg, const std::string &n="")
void warning(const char *fmt,...)
virtual bool eof(void) const =0
octave_value reshape(const dim_vector &dv) const
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
virtual std::string name(void) const =0
int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
charNDArray max(char d, const charNDArray &m)
int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
virtual std::ostream * output_stream(void)
T::size_type numel(const T &str)
bool is_NaN_or_NA(const Complex &x)
bool is_delim(unsigned char ch) const
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)
static bool ok_for_signed_int_conv(const octave_value &val)
static int convert_to_valid_int(const octave_value &tc, int &conv_err)
bool is_sq_string(void) 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)
static void convert_chars(const void *data, void *conv_data, octave_idx_type n_elts)
mach_info::float_format float_format(void) const
OCTINTERP_API std::string do_string_escapes(const std::string &s)
stream(base_stream *bs=nullptr)
octave_idx_type nint_big(double x)
delimited_stream(std::istream &is, const std::string &delimiters, int longest_lookahead, octave_idx_type bsize=4096)
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the IEEE symbol NaN(Not a Number). NaN is the result of operations which do not produce a well defined 0 result. Common operations which produce a NaN are arithmetic with infinity ex($\infty - \infty$)
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
int scan_bracket(delimited_stream &is, const std::string &pattern, std::string &val) const
octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
ostrl_map::const_iterator lookup_cache
octave_value_list oscanf(const std::string &fmt, const std::string &who)
OCTAVE_EXPORT octave_value_list only variables visible in the local scope are displayed The following are valid options
int __lo_ieee_is_old_NA(double x)
Cell cell_value(void) const
double double_value(bool frc_str_conv=false) const
octave_idx_type length(void) const
int scan_caret(delimited_stream &is, const std::string &pattern, std::string &val) const
#define BEGIN_C_CONVERSION()
static std::string switch_to_g_format(const octave::printf_format_elt *elt)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
#define FILL_TABLE_ROW(T, V)
static std::string mode_as_string(int mode)
ColumnVector transform(const Matrix &m, double x, double y, double z)
stream_list(interpreter &interp)
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)
static bool is_nan_or_inf(const octave_value &val)
int getline(std::string &dest, char delim)
bool is_string(void) const
void progress_benchmark(void)
std::complex< float > FloatComplex
OCTAVE_EXPORT octave_value_list error nd deftypefn *const octave_scalar_map err
#define FINISH_CHARACTER_CONVERSION()
std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
std::complex< double > Complex
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_idx_type numel(void) const
Number of elements in the array.
octave_idx_type length(void) const
void scan_cstring(delimited_stream &is, const textscan_format_elt &fmt, std::string &val) const
write the output to stdout if nargout is
bool match_literal(delimited_stream &isp, const textscan_format_elt &elem)
bool is_old_NA< double >(double val)
Vector representing the dimensions (size) of an Array.
static size_t data_type_size(data_type dt)
std::ios_base::iostate flags
#define BEGIN_CHAR_CLASS_CONVERSION()
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
nd group nd example For each display the value
char * read(char *buffer, int size, char *&new_start)
std::string whitespace_table
#define INSTANTIATE_WRITE(T)
stream & operator=(const stream &)
bool is_scalar_type(void) const
NDArray array_value(bool frc_str_conv=false) const
std::string float_format_as_string(float_format flt_fmt)
where the brackets indicate optional arguments and and character or cell array For character arrays the conversion is repeated for every row
octave_idx_type length(void) const
Number of elements in the array.
std::string parse_char_class(const std::string &pattern) const
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x
charNDArray min(char d, const charNDArray &m)
static bool convert_data(const T *data, void *conv_data, octave_idx_type n_elts, oct_data_conv::data_type output_type, octave::mach_info::float_format flt_fmt)
octave_value fast_elem_extract(octave_idx_type n) const
Extract the n-th element, aka val(n).