74 int ival =
NINT (dval);
102 ::error (
"%s: negative value invalid as size specification",
109 ::error (
"%s: NaN is invalid as size specification", who.c_str ());
116 bool& one_elt_size_spec,
const std::string& who)
121 one_elt_size_spec =
false;
130 one_elt_size_spec =
true;
134 dnc = (dnr == 0.0) ? 0.0 : 1.0;
136 else if (sz_len == 2)
143 ::error (
"%s: invalid size specification", who.c_str ());
146 ::error (
"%s: invalid size specification", who.c_str ());
158 : nconv (0), curr_idx (0), list (
dim_vector (16, 1)), buf (0)
162 size_t n = s.length ();
167 bool discard =
false;
168 char modifier =
'\0';
171 bool have_more =
true;
178 buf =
new std::ostringstream ();
187 have_more = (
buf != 0);
189 else if (isspace (s[i]))
198 while (++i < n && isspace (s[i]))
213 while (i < n && ! isspace (s[i]) && s[i] !=
'%')
250 const std::string& char_class)
254 std::string
text =
buf->str ();
260 modifier, char_class);
265 list(num_elts++) = elt;
278 size_t len = s.length ();
284 unsigned char c = s[i++];
286 if (c ==
'-' && i > 1 && i < len
287 && ( static_cast<unsigned char> (s[i-2])
288 <= static_cast<unsigned char> (s[i])))
293 for (c = s[i-2]+1; c < s[i]; c++)
301 if (c !=
'-' || i == len)
311 size_t n,
int& width,
bool& discard,
312 char&
type,
char& modifier,
322 bool have_width =
false;
338 case '0':
case '1':
case '2':
case '3':
case '4':
339 case '5':
case '6':
case '7':
case '8':
case '9':
345 width = width * 10 + c -
'0';
348 while (i < n && isdigit (s[i]))
351 width = width * 10 + c -
'0';
357 case 'h':
case 'l':
case 'L':
358 if (modifier !=
'\0')
364 case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
372 case 'e':
case 'f':
case 'g':
384 case 'c':
case 's':
case 'p':
case '%':
case '[':
385 if (modifier !=
'\0')
395 modifier, num_elts) == 0)
414 size_t n,
int& width,
bool discard,
415 char&
type,
char modifier,
420 std::string char_class;
422 size_t beg_idx = std::string::npos;
423 size_t end_idx = std::string::npos;
455 else if (s[i] ==
']')
459 while (i < n && s[i] !=
']')
462 if (i < n && s[i] ==
']')
479 if (beg_idx != std::string::npos && end_idx != std::string::npos)
481 end_idx - beg_idx + 1));
483 add_elt_to_list (width, discard, type, modifier, num_elts, char_class);
499 <<
"width: " << elt->
width <<
"\n"
500 <<
"discard: " << elt->
discard <<
"\n"
504 std::cerr <<
"literal text\n";
506 std::cerr <<
"whitespace\n";
508 std::cerr << elt->
type <<
"\n";
511 <<
"modifier: " << elt->
modifier <<
"\n"
530 case 'c':
case 's':
case '%':
case '[':
case '^':
560 case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
561 case 'e':
case 'f':
case 'g':
579 : nconv (0), curr_idx (0), list (
dim_vector (16, 1)), buf (0)
583 size_t n = s.length ();
591 char modifier =
'\0';
594 bool have_more =
true;
595 bool empty_buf =
true;
602 list(num_elts++) = elt;
614 buf =
new std::ostringstream ();
625 type, modifier, num_elts);
627 have_more = (
buf != 0);
678 int fw,
int prec,
char type,
683 std::string
text =
buf->str ();
694 list(num_elts++) = elt;
704 size_t n,
int& args, std::string& flags,
705 int& fw,
int& prec,
char& modifier,
723 case '-':
case '+':
case ' ':
case '0':
case '#':
750 std::string tmp = s.substr (i);
751 sscanf (tmp.c_str (),
"%d%n", &fw, &
nn);
754 while (i < n && isdigit (s[i]))
759 if (i < n && s[i] ==
'.')
783 std::string tmp = s.substr (i);
784 sscanf (tmp.c_str (),
"%d%n", &prec, &
nn);
787 while (i < n && isdigit (s[i]))
801 case 'h':
case 'l':
case 'L':
818 int args,
const std::string& flags,
819 int fw,
int prec,
char modifier,
824 case 'd':
case 'i':
case 'o':
case 'x':
case 'X':
833 case 'f':
case 'e':
case 'E':
case 'g':
case 'G':
834 if (modifier ==
'h' || modifier ==
'l')
841 case 's':
case 'p':
case '%':
842 if (modifier !=
'\0')
855 if (type !=
'%' || args != 0)
876 for (
int i = 0; i < n; i++)
881 <<
"args: " << elt->
args <<
"\n"
882 <<
"flags: '" << elt->
flags <<
"'\n"
883 <<
"width: " << elt->
fw <<
"\n"
884 <<
"prec: " << elt->
prec <<
"\n"
885 <<
"type: '" << elt->
type <<
"'\n"
886 <<
"modifier: '" << elt->
modifier <<
"'\n"
902 errmsg = who +
": " + msg;
930 bool strip_newline,
const std::string& who)
936 ::error (
"%s: unable to read from stdin while running interactively",
948 std::istream& is = *isp;
950 std::ostringstream buf;
957 while (is && (c = is.get ()) != EOF)
966 buf << static_cast<char> (c);
977 buf << static_cast<char> (c);
988 buf << static_cast<char> (c);
993 buf << static_cast<char> (c);
995 if (max_len > 0 && char_count == max_len)
1000 if (! is.eof () && char_count > 0)
1005 int disgusting_compatibility_hack = is.get ();
1007 is.putback (disgusting_compatibility_hack);
1010 if (is.good () || (is.eof () && char_count > 0))
1011 retval = buf.str ();
1016 if (is.eof () && char_count == 0)
1017 error (who,
"at end of file");
1019 error (who,
"read error");
1033 const std::string& who)
1035 return do_gets (max_len, err,
true, who);
1040 const std::string& who)
1042 return do_gets (max_len, err,
false, who);
1052 ::error (
"%s: unable to read from stdin while running interactively",
1064 std::istream& is = *isp;
1070 while (is && (c = is.get ()) != EOF)
1074 if (c ==
'\r' || (c ==
'\n' && lastc !=
'\r'))
1084 if (c ==
'\r' && is.peek () ==
'\n')
1090 error (who,
"read error");
1105 #define OCTAVE_SCAN(is, fmt, arg) octave_scan (is, fmt, arg)
1116 is >> std::oct >> ref >> std::dec;
1120 is >> std::hex >> ref >> std::dec;
1127 while (is && (c1 = is.get ()) != EOF && isspace (c1))
1134 int c2 = is.peek ();
1136 if (c2 ==
'x' || c2 ==
'X')
1139 if (std::isxdigit (is.peek ()))
1140 is >> std::hex >> ref >> std::dec;
1146 if (c2 ==
'0' || c2 ==
'1' || c2 ==
'2'
1147 || c2 ==
'3' || c2 ==
'4' || c2 ==
'5'
1148 || c2 ==
'6' || c2 ==
'7')
1149 is >> std::oct >> ref >> std::dec;
1183 is.width (fmt.
width);
1186 std::istringstream ss (tmp);
1204 return is >> valptr;
1211 double& ref = *valptr;
1221 while (is && (c1 = is.get ()) != EOF && isspace (c1))
1228 ref = octave_read_value<double> (is);
1252 if (idx == max_size && ! discard)
1257 mval.
resize (nr, max_size / nr, 0.0);
1259 mval.
resize (max_size, 1, 0.0);
1267 data[idx++] = *(valptr);
1277 #define DO_WHITESPACE_CONVERSION() \
1282 while (is && (c = is.get ()) != EOF && isspace (c)) \
1290 #define DO_LITERAL_CONVERSION() \
1295 int n = strlen (fmt); \
1298 while (i < n && is && (c = is.get ()) != EOF) \
1300 if (c == static_cast<unsigned char> (fmt[i])) \
1313 is.setstate (std::ios::failbit); \
1317 #define DO_PCT_CONVERSION() \
1320 int c = is.get (); \
1327 is.setstate (std::ios::failbit); \
1331 is.setstate (std::ios::failbit); \
1335 #define BEGIN_C_CONVERSION() \
1336 is.unsetf (std::ios::skipws); \
1338 int width = elt->width ? elt->width : 1; \
1340 std::string tmp (width, '\0'); \
1345 while (is && n < width && (c = is.get ()) != EOF) \
1346 tmp[n++] = static_cast<char> (c); \
1348 if (n > 0 && c == EOF) \
1355 #define BEGIN_S_CONVERSION() \
1356 int width = elt->width; \
1364 tmp = std::string (width, '\0'); \
1370 while (is && (c = is.get ()) != EOF) \
1372 if (! isspace (c)) \
1374 tmp[n++] = static_cast<char> (c); \
1379 while (is && n < width && (c = is.get ()) != EOF) \
1387 tmp[n++] = static_cast<char> (c); \
1390 if (n > 0 && c == EOF) \
1397 is >> std::ws >> tmp; \
1403 #define BEGIN_CHAR_CLASS_CONVERSION() \
1404 int width = elt->width; \
1411 width = std::numeric_limits<int>::max (); \
1413 std::ostringstream buf; \
1415 std::string char_class = elt->char_class; \
1419 if (elt->type == '[') \
1421 int chars_read = 0; \
1422 while (is && chars_read++ < width && (c = is.get ()) != EOF \
1423 && char_class.find (c) != std::string::npos) \
1424 buf << static_cast<char> (c); \
1428 int chars_read = 0; \
1429 while (is && chars_read++ < width && (c = is.get ()) != EOF \
1430 && char_class.find (c) == std::string::npos) \
1431 buf << static_cast<char> (c); \
1434 if (width == std::numeric_limits<int>::max () && c != EOF) \
1440 is.setstate (std::ios::failbit); \
1441 else if (c == EOF) \
1447 #define FINISH_CHARACTER_CONVERSION() \
1450 width = tmp.length (); \
1458 conversion_count++; \
1462 if (data_index == max_size) \
1466 if (all_char_conv) \
1468 if (one_elt_size_spec) \
1469 mval.resize (1, max_size, 0.0); \
1471 mval.resize (nr, max_size / nr, 0.0); \
1473 panic_impossible (); \
1476 mval.resize (nr, max_size / nr, 0.0); \
1478 mval.resize (max_size, 1, 0.0); \
1480 data = mval.fortran_vec (); \
1483 data[data_index++] = tmp[i++]; \
1493 bool one_elt_size_spec,
1495 const std::string& who)
1501 ::error (
"%s: unable to read from stdin while running interactively",
1507 conversion_count = 0;
1513 if (nr == 0 || nc == 0)
1515 if (one_elt_size_spec)
1518 return Matrix (nr, nc, 0.0);
1539 if (one_elt_size_spec)
1542 mval.
resize (1, max_size, 0.0);
1551 mval.
resize (nr, nc, 0.0);
1552 max_size = max_conv = nr * nc;
1556 mval.
resize (nr, 32, 0.0);
1568 mval.
resize (nr, nc, 0.0);
1570 max_conv = max_size;
1575 mval.
resize (nr, 32, 0.0);
1582 mval.
resize (32, 1, 0.0);
1590 std::istream& is = *isp;
1594 std::ios::fmtflags flags = is.flags ();
1608 || elt->
type ==
'%')
1609 && max_conv > 0 && conversion_count == max_conv)
1611 if (all_char_conv && one_elt_size_spec)
1614 final_nc = data_index;
1619 final_nc = (data_index - 1) / nr + 1;
1624 else if (data_index == max_size)
1630 if (one_elt_size_spec)
1631 mval.
resize (1, max_size, 0.0);
1633 mval.
resize (nr, max_size / nr, 0.0);
1638 mval.
resize (nr, max_size / nr, 0.0);
1640 mval.
resize (max_size, 1, 0.0);
1645 const char *fmt = elt->
text;
1671 data_index, conversion_count,
1672 nr, max_size, discard);
1680 data_index, conversion_count,
1681 nr, max_size, discard);
1689 data_index, conversion_count,
1690 nr, max_size, discard);
1697 case 'o':
case 'u':
case 'x':
1703 unsigned short int tmp;
1705 data_index, conversion_count,
1706 nr, max_size, discard);
1712 unsigned long int tmp;
1714 data_index, conversion_count,
1715 nr, max_size, discard);
1723 data_index, conversion_count,
1724 nr, max_size, discard);
1731 case 'e':
case 'f':
case 'g':
1736 data_index, conversion_count,
1737 nr, max_size, discard);
1768 error (
"%s: unsupported format specifier", who.c_str ());
1772 error (
"%s: internal format error", who.c_str ());
1784 if (one_elt_size_spec)
1787 final_nc = data_index;
1789 else if (data_index > nr)
1792 final_nc = (data_index - 1) / nr + 1;
1796 final_nr = data_index;
1802 if (data_index > nr)
1805 final_nc = (data_index - 1) / nr + 1;
1809 final_nr = data_index;
1815 final_nr = data_index;
1822 if (is.rdstate () & std::ios::failbit)
1823 is.clear (is.rdstate () & (~
std::ios::failbit));
1834 do_gets (-1, err,
false, who);
1842 error (
"%s: internal format error", who.c_str ());
1846 if (nconv == 0 && ++trips == num_fmt_elts)
1848 if (all_char_conv && one_elt_size_spec)
1851 final_nc = data_index;
1856 final_nc = (data_index - 1) / nr + 1;
1862 elt = fmt_list.
next (nconv > 0);
1868 mval.
resize (final_nr, final_nc, 0.0);
1882 const std::string& who)
1886 conversion_count = 0;
1895 ::
error (
"%s: invalid format specified", who.c_str ());
1901 bool one_elt_size_spec;
1903 get_size (size, nr, nc, one_elt_size_spec, who);
1906 retval =
do_scanf (fmt_list, nr, nc, one_elt_size_spec,
1907 conversion_count, who);
1926 std::istream& is = *isp;
1928 std::ios::fmtflags flags = is.flags ();
1932 const char *fmt = elt->
text;
1970 case 'o':
case 'u':
case 'x':
1984 case 'e':
case 'f':
case 'g':
2037 error (
"%s: unsupported format specifier", who.c_str ());
2041 error (
"%s: internal format error", who.c_str ());
2046 if (
ok () && is.fail ())
2048 error (
"%s: read error", who.c_str ());
2057 do_gets (-1, err,
false, who);
2074 std::istream& is = *isp;
2081 ::error (
"%s: invalid format specified", who.c_str ());
2107 retval(num_values++) = tmp;
2112 elt = fmt_list.
next (nconv > 0);
2116 retval(nconv) = num_values;
2119 retval(nconv+1) =
error (
false, err_num);
2124 if (
ok () && len > nconv)
2128 elt = fmt_list.
next ();
2172 : values (args), val_idx (0), elt_idx (0),
2173 n_vals (values.
length ()), n_elts (0), have_data (false),
2194 int int_value (
void);
2196 operator bool ()
const {
return (curr_state == ok); }
2259 retval = sval.substr (
elt_idx);
2273 for (; idx <
n_elts; idx++)
2275 double dval = val(idx);
2277 if (
D_NINT (dval) != dval || dval < 0 || dval > 255)
2285 std::string sval (n,
'\0');
2288 sval[i] = val(elt_idx++);
2300 if (type ==
'c' && ! retval.
is_string ())
2304 if (
D_NINT (dval) == dval && dval >= 0 && dval < 256)
2305 retval =
static_cast<char> (dval);
2325 if (
elt_idx == 0 && (type ==
's' || type ==
'c'))
2353 if (
D_NINT (dval) == dval)
2354 retval =
NINT (dval);
2368 int sa_2, T
arg,
const std::string& who)
2387 ::error (
"%s: internal error handling format", who.c_str ());
2396 int nsa,
int sa_1,
int sa_2,
const std::string&
arg,
2397 const std::string& who)
2403 ::error (
"%s: internal error handling format", who.c_str ());
2407 std::string flags = elt->
flags;
2409 bool left = flags.find (
'-') != std::string::npos;
2411 size_t len = arg.length ();
2413 size_t fw = nsa > 0 ? sa_1 : (elt->
fw == -1 ? len : elt->
fw);
2414 size_t prec = nsa > 1 ? sa_2 : (elt->
prec == -1 ? len : elt->
prec);
2416 os << std::setw (fw)
2418 << (prec < len ? arg.substr (0, prec) :
arg);
2420 return len > fw ? len : fw;
2445 if (ival.
value () <= limit)
2455 if (dval ==
xround (dval) && dval <= limit)
2474 return ov_is_ge_zero.
is_true ();
2482 if (dval ==
xround (dval) && dval >= 0 && dval <= limit)
2492 std::string tfmt = elt->
text;
2494 tfmt.replace (tfmt.rfind (elt->
type), 1,
"g");
2502 int nsa,
int sa_1,
int sa_2,
2504 const std::string& who)
2508 const char *fmt = elt->
text;
2514 std::string tfmt = fmt;
2515 std::string::size_type i1, i2;
2517 tfmt.replace ((i1 = tfmt.rfind (elt->
type)),
2520 if ((i2 = tfmt.rfind (
'.')) != std::string::npos
2523 tfmt.erase (i2, i1-i2);
2524 if (elt->
prec == -2)
2531 if (elt->
flags.find (
'+') != std::string::npos)
2532 tval = (dval < 0 ?
"-Inf" :
"+Inf");
2534 tval = (dval < 0 ?
"-Inf" :
"Inf");
2538 if (elt->
flags.find (
'+') != std::string::npos)
2544 retval +=
do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2, tval, who);
2548 static std::string llmod
2549 =
sizeof (long) ==
sizeof (int64_t) ?
"l" :
"ll";
2555 case 'd':
case 'i':
case 'c':
2561 std::string tfmt = fmt;
2562 tfmt.replace (tfmt.rfind (type), 1, llmod +
type);
2565 tval.
value (), who);
2575 sa_1, sa_2, dval, who);
2579 case 'o':
case 'x':
case 'X':
case 'u':
2585 std::string tfmt = fmt;
2586 tfmt.replace (tfmt.rfind (type), 1, llmod +
type);
2589 tval.
value (), who);
2599 sa_1, sa_2, dval, who);
2603 case 'f':
case 'e':
case 'E':
2614 error (
"%s: invalid format specifier",
2627 const std::string& who)
2637 std::ostream& os = *osp;
2654 int nsa = (elt->
fw == -2) + (elt->
prec == -2);
2677 if (elt->
type ==
'%')
2682 else if (elt->
args == 0 && elt->
text)
2685 retval += strlen (elt->
text);
2687 else if (elt->
type ==
's' || elt->
type ==
'c')
2720 error (
"%s: write error", who.c_str ());
2726 ::error (
"%s: internal error handling format", who.c_str ());
2731 elt = fmt_list.
next (nconv > 0 && ! val_cache.
exhausted ());
2746 const std::string& who)
2753 ::
error (
"%s: invalid format specified", who.c_str ());
2755 retval =
do_printf (fmt_list, args, who);
2769 std::ostream& os = *osp;
2787 error (
"%s: write error", who.c_str ());
2790 error (
"%s: write error", who.c_str ());
2803 err_num =
fail ? -1 : 0;
2805 std::string tmp =
errmsg;
2818 error (who, std::string (
"stream not open for ") + rw);
2875 retval =
rep->
getl (max_len, err, who);
2882 const std::string& who)
2896 if (conv_err || max_len < 0)
2899 ::error (
"%s: invalid maximum length specified", who.c_str ());
2904 retval =
getl (max_len, err, who);
2915 retval =
rep->
gets (max_len, err, who);
2922 const std::string& who)
2936 if (conv_err || max_len < 0)
2939 ::error (
"%s: invalid maximum length specified", who.c_str ());
2944 retval =
gets (max_len, err, who);
2955 retval =
rep->
skipl (count, err, who);
2962 const std::string& who)
2980 if (conv_err || count < 0)
2983 ::error (
"%s: invalid number of lines specified", who.c_str ());
2989 retval =
skipl (count, err, who);
3005 off_t orig_pos =
rep->
tell ();
3027 status =
rep->
seek (offset, origin);
3033 off_t desired_pos =
rep->
tell ();
3038 if (desired_pos > eof_pos || desired_pos < 0)
3071 off_t xoffset = val.
value ();
3083 if (xorigin ==
"bof")
3085 else if (xorigin ==
"cof")
3087 else if (xorigin ==
"eof")
3100 else if (xorigin == 0)
3102 else if (xorigin == 1)
3111 retval =
seek (xoffset, origin);
3114 error (
"fseek: failed to seek to requested position");
3117 error (
"fseek: invalid value for origin");
3120 error (
"fseek: invalid value for offset");
3145 bool retval =
false;
3160 template <
class SRC_T,
class DST_T>
3166 bool do_float_fmt_conv,
bool do_NA_conv,
3169 typedef typename DST_T::element_type dst_elt_type;
3173 dst_elt_type *conv_data = conv.fortran_vec ();
3177 for (std::list<void *>::const_iterator it = input_buf_list.begin ();
3178 it != input_buf_list.end (); it++)
3180 SRC_T *data =
static_cast<SRC_T *
> (*it);
3182 if (swap || do_float_fmt_conv)
3188 swap_bytes<sizeof (SRC_T)> (&data[i]);
3189 else if (do_float_fmt_conv)
3194 dst_elt_type tmp (data[i]);
3209 dst_elt_type tmp (data[i]);
3221 conv_data[j] = data[i];
3228 input_buf_list.clear ();
3231 conv_data[i] = dst_elt_type (0);
3239 bool swap,
bool do_float_fmt_conv,
bool do_NA_conv,
3242 #define TABLE_ELT(T, U, V, W) \
3243 conv_fptr_table[oct_data_conv::T][oct_data_conv::U] = convert_and_copy<V, W>
3245 #define FILL_TABLE_ROW(T, V) \
3246 TABLE_ELT (T, dt_int8, V, int8NDArray); \
3247 TABLE_ELT (T, dt_uint8, V, uint8NDArray); \
3248 TABLE_ELT (T, dt_int16, V, int16NDArray); \
3249 TABLE_ELT (T, dt_uint16, V, uint16NDArray); \
3250 TABLE_ELT (T, dt_int32, V, int32NDArray); \
3251 TABLE_ELT (T, dt_uint32, V, uint32NDArray); \
3252 TABLE_ELT (T, dt_int64, V, int64NDArray); \
3253 TABLE_ELT (T, dt_uint64, V, uint64NDArray); \
3254 TABLE_ELT (T, dt_single, V, FloatNDArray); \
3255 TABLE_ELT (T, dt_double, V, NDArray); \
3256 TABLE_ELT (T, dt_char, V, charNDArray); \
3257 TABLE_ELT (T, dt_schar, V, charNDArray); \
3258 TABLE_ELT (T, dt_uchar, V, charNDArray); \
3259 TABLE_ELT (T, dt_logical, V, boolNDArray);
3272 static bool initialized =
false;
3281 for (
int j = 0; j < 14; j++)
3282 conv_fptr_table[i][j] = 0;
3318 switch (output_type)
3337 retval =
fptr (input_buf_list, input_buf_elts, elts_read,
3338 nr, nc, swap, do_float_fmt_conv, do_NA_conv, ffmt);
3344 (*current_liboctave_error_handler)
3345 (
"read: invalid type specification");
3365 bool one_elt_size_spec =
false;
3380 get_size (size, nr, nc, one_elt_size_spec,
"fread");
3387 if (one_elt_size_spec)
3410 if (nr == 0 || nc == 0)
3418 elts_to_read = nr * nc;
3420 bool read_to_eof = elts_to_read < 0;
3427 input_buf_elts = 1024 * 1024;
3429 input_buf_elts = elts_to_read;
3432 input_buf_elts = block_size;
3439 assert (input_buf_size >= 0);
3448 std::istream& is = *isp;
3450 std::list <void *> input_buf_list;
3452 while (is && ! is.eof ()
3453 && (read_to_eof || count < elts_to_read))
3459 if (remaining_elts < input_buf_elts)
3460 input_buf_size = remaining_elts * input_elt_size;
3463 char *input_buf =
new char [input_buf_size];
3465 is.read (input_buf, input_buf_size);
3467 size_t gcount = is.gcount ();
3469 char_count += gcount;
3475 input_buf_list.push_back (input_buf);
3477 if (is && skip != 0 && nel == block_size)
3482 off_t orig_pos =
tell ();
3486 off_t eof_pos =
tell ();
3492 off_t remaining = eof_pos - orig_pos;
3494 if (remaining < skip)
3510 if (count % nr != 0)
3516 else if (count == 0)
3521 else if (count != nr * nc)
3523 if (count % nr != 0)
3524 nc = count / nr + 1;
3532 retval =
finalize_read (input_buf_list, input_buf_elts, count,
3533 nr, nc, input_type, output_type, ffmt);
3536 error (
"fread: invalid input stream");
3563 error (
"fwrite: write error");
3574 template <
class T,
class V>
3578 const T *tt_data =
static_cast<const T *
> (data);
3580 V *vt_data =
static_cast<V *
> (conv_data);
3583 vt_data[i] = tt_data[i];
3586 template <
class T,
class V>
3591 typedef typename V::val_type val_type;
3593 val_type *vt_data =
static_cast<val_type *
> (conv_data);
3602 vt_data[i] = val.value ();
3605 swap_bytes<sizeof (val_type)> (&vt_data[i]);
3640 switch (output_type)
3643 convert_chars<ult_elt_type, char> (data, conv_data, n_elts);
3647 convert_chars<ult_elt_type, signed char> (data, conv_data, n_elts);
3651 convert_chars<ult_elt_type, unsigned char> (data, conv_data, n_elts);
3655 convert_ints<T, octave_int8> (data, conv_data, n_elts, swap);
3659 convert_ints<T, octave_uint8> (data, conv_data, n_elts, swap);
3663 convert_ints<T, octave_int16> (data, conv_data, n_elts, swap);
3667 convert_ints<T, octave_uint16> (data, conv_data, n_elts, swap);
3671 convert_ints<T, octave_int32> (data, conv_data, n_elts, swap);
3675 convert_ints<T, octave_uint32> (data, conv_data, n_elts, swap);
3679 convert_ints<T, octave_int64> (data, conv_data, n_elts, swap);
3683 convert_ints<T, octave_uint64> (data, conv_data, n_elts, swap);
3688 float *vt_data =
static_cast<float *
> (conv_data);
3692 vt_data[i] = data[i];
3694 if (do_float_conversion)
3702 double *vt_data =
static_cast<double *
> (conv_data);
3706 vt_data[i] = data[i];
3708 if (do_float_conversion)
3716 (*current_liboctave_error_handler)
3717 (
"write: invalid type specification");
3727 bool status =
false;
3733 std::ostream& os = *osp;
3737 os.write (static_cast<const char *> (data), nbytes);
3750 bool status =
false;
3756 std::ostream& os = *osp;
3761 off_t orig_pos =
tell ();
3765 off_t eof_pos =
tell ();
3771 size_t remaining = eof_pos - orig_pos;
3773 if (remaining < skip)
3779 unsigned char zero = 0;
3780 for (
size_t j = 0; j < skip - remaining; j++)
3781 os.write (reinterpret_cast<const char *> (&zero), 1);
3804 bool do_data_conversion = (swap || ! is_equivalent_type<T> (output_type)
3812 chunk_size = block_size;
3813 else if (do_data_conversion)
3814 chunk_size = 1024 * 1024;
3820 const T *pdata = data.
data ();
3832 if (chunk_size > remaining_nel)
3833 chunk_size = remaining_nel;
3835 bool status =
false;
3837 if (do_data_conversion)
3844 status =
convert_data (&pdata[i], conv_data, chunk_size,
3845 output_type, flt_fmt);
3851 status =
write_bytes (pdata,
sizeof (T) * chunk_size);
3862 #define INSTANTIATE_WRITE(T) \
3865 octave_stream::write (const Array<T>& data, octave_idx_type block_size, \
3866 oct_data_conv::data_type output_type, \
3867 octave_idx_type skip, \
3868 oct_mach_info::float_format flt_fmt)
3898 retval =
rep->
scanf (fmt, size, count, who);
3916 retval =
scanf (sfmt, size, count, who);
3922 error (who +
": format must be a string");
3951 retval =
oscanf (sfmt, who);
3957 error (who +
": format must be a string");
3965 const std::string& who)
3977 const std::string& who)
3988 retval =
printf (sfmt, args, who);
3994 error (who +
": format must be a string");
4021 retval =
puts (s, who);
4027 error (who +
": argument must be a string");
4047 std::string retval =
"invalid stream object";
4050 retval =
rep->
error (clear, err_num);
4091 std::string retval =
"???";
4092 std::ios::openmode in_mode =
static_cast<std::ios::openmode
> (
mode);
4094 if (in_mode == std::ios::in)
4096 else if (in_mode == std::ios::out
4097 || in_mode == (std::ios::out | std::ios::trunc))
4099 else if (in_mode == (std::ios::out | std::ios::app))
4101 else if (in_mode == (std::ios::in | std::ios::out))
4103 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc))
4105 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate))
4107 else if (in_mode == (std::ios::in | std::ios::binary))
4109 else if (in_mode == (std::ios::out | std::ios::binary)
4110 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary))
4112 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary))
4114 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary))
4116 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc
4117 | std::ios::binary))
4119 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate
4120 | std::ios::binary))
4143 ::error (
"unable to create stream list object!");
4227 return stream_number;
4242 if (
list.size () <
list.max_size ())
4243 list[stream_number] = os;
4247 error (
"could not create file id");
4250 return stream_number;
4258 ::
error (
"invalid stream number = %d", fid);
4260 ::error (
"%s: invalid stream number = %d", who.c_str (), fid);
4274 ostrl_map::const_iterator iter =
list.find (fid);
4276 if (iter !=
list.end ())
4278 retval = iter->second;
4293 const std::string& who)
const
4315 ostrl_map::iterator iter =
list.find (fid);
4317 if (iter !=
list.end ())
4376 for (ostrl_map::iterator iter =
list.begin (); iter !=
list.end (); iter++)
4378 int fid = iter->first;
4386 for (
int fid = 0; fid < 3; fid++)
list[fid] = saved_os[fid];
4403 retval(0) = os.
name ();
4406 ::error (
"invalid file id = %d", fid);
4423 ::error (
"file id must be a file object or integer value");
4433 std::ostringstream buf;
4436 <<
" number mode arch name\n"
4437 <<
" ------ ---- ---- ----\n";
4439 for (ostrl_map::const_iterator p =
list.begin (); p !=
list.end (); p++)
4444 << std::setiosflags (std::ios::right)
4445 << std::setw (4) << p->first <<
" "
4447 << std::resetiosflags (std::ios::adjustfield)
4455 << os.
name () <<
"\n";
4460 retval = buf.str ();
4472 for (ostrl_map::const_iterator p =
list.begin (); p !=
list.end (); p++)
4476 if (p->first > 2 && p->second)
4477 retval(0,num_open++) = p->first;
4480 retval.
resize ((num_open > 0), num_open);
4494 for (ostrl_map::const_iterator p =
list.begin (); p !=
list.end (); p++)
4503 if (os && os.
name () == nm)
4518 ::error (
"file id must be a file object, std::string, or integer value");
bool is_object(void) const
void invalid_operation(const std::string &who, const char *rw)
static void clear(octave_shlib &oct_file)
bool skip_bytes(size_t n_elts)
static octave_stream lookup(int fid, const std::string &who=std::string())
#define DO_LITERAL_CONVERSION()
#define DO_PCT_CONVERSION()
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, oct_mach_info::float_format flt_fmt, octave_idx_type &count)
static std::string expand_char_class(const std::string &s)
void gripe_wrong_type_arg(const char *name, const char *s, bool is_error)
static std::string float_format_as_string(float_format)
void do_float_format_conversion(void *data, octave_idx_type len, oct_mach_info::float_format from_fmt, oct_mach_info::float_format to_fmt)
double __lo_ieee_replace_old_NA(double x)
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
virtual off_t tell(void)=0
static string_vector get_info(int fid)
octave_idx_type numel(void) const
Number of elements in the array.
static octave_idx_type nn
static octave_stream_list * instance
std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
bool is_scalar_type(void) const
int seek(off_t offset, int origin)
void do_clear(bool flush=true)
bool is_defined(void) const
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, oct_mach_info::float_format from_flt_fmt)
virtual std::istream * input_stream(void)
static std::string switch_to_g_format(const printf_format_elt *elt)
octave_stream do_lookup(int fid, const std::string &who=std::string()) const
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
static bool convert_data(const T *data, void *conv_data, octave_idx_type n_elts, oct_data_conv::data_type output_type, oct_mach_info::float_format flt_fmt)
void error(const char *fmt,...)
octave_value get_next_value(char type=0)
static bool instance_ok(void)
virtual std::string name(void) const =0
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)
printf_value_cache(const octave_value_list &args, 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)
octave_value isinf(void) const
int do_remove(int fid, const std::string &who=std::string())
double scalar_value(bool frc_str_conv=false) const
#define BEGIN_S_CONVERSION()
#define DO_WHITESPACE_CONVERSION()
int do_get_file_number(const octave_value &fid) const
#define OCTAVE_SCAN(is, fmt, arg)
const octave_value_list values
std::istream * input_stream(void)
void do_double_format_conversion(void *data, octave_idx_type len, oct_mach_info::float_format from_fmt, oct_mach_info::float_format to_fmt)
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 &who)
static bool ok_for_signed_int_conv(const octave_value &val)
F77_RET_T const double const double double * d
size_t octave_format(std::ostream &os, const char *fmt,...)
virtual std::ostream * output_stream(void)
int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
static int insert(octave_stream &os)
std::istream & octave_scan_1(std::istream &is, const scanf_format_elt &fmt, T *valptr)
std::istream & octave_scan(std::istream &is, const scanf_format_elt &fmt, T *valptr)
static int remove(int fid, const std::string &who=std::string())
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, oct_mach_info::float_format from_flt_fmt)
static void gripe_invalid_file_id(int fid, const std::string &who)
octave_value convert_to_str(bool pad=false, bool force=false, char type= '\'') const
octave_idx_type numel(const octave_value_list &idx)
static int convert_to_valid_int(const octave_value &tc, int &conv_err)
std::string string_value(bool force=false) const
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 resize(octave_idx_type n, const std::string &rfv=std::string())
bool write_bytes(const void *data, size_t n_elts)
static void convert_chars(const void *data, void *conv_data, octave_idx_type n_elts)
bool is_string(void) const
std::string do_gets(octave_idx_type max_len, bool &err, bool strip_newline, const std::string &who)
const T * data(void) const
int do_printf_conv(std::ostream &os, const char *fmt, int nsa, int sa_1, int sa_2, T arg, const std::string &who)
bool do_oscanf(const scanf_format_elt *elt, octave_value &, const std::string &who)
void resize(const dim_vector &dv, const T &rfv)
int puts(const std::string &s, const std::string &who)
static void clear(bool flush=true)
#define panic_impossible()
int do_printf(printf_format_list &fmt_list, const octave_value_list &args, const std::string &who)
int puts(const std::string &s, const std::string &who)
std::string error(bool clear, int &err_num)
std::ostream * output_stream(void)
virtual int file_number(void) const
static void cleanup_instance(void)
octave_value_list oscanf(const std::string &fmt, const std::string &who)
static bool words_big_endian(void)
static bool ok_for_unsigned_int_conv(const octave_value &val)
size_t size(T const (&)[z])
OCTAVE_API double D_NINT(double x)
octave_value scanf(const std::string &fmt, const Array< double > &size, octave_idx_type &count, const std::string &who)
oct_mach_info::float_format float_format(void) const
int write(octave_stream &os, int block_size, oct_data_conv::data_type output_type, int skip, oct_mach_info::float_format flt_fmt) const
~printf_value_cache(void)
std::string error(bool clear, int &err_num)
bool stream_ok(bool clear=true) const
static bool is_nan_or_inf(const octave_value &val)
int do_insert(octave_stream &os)
Handles the reference counting for all the derived classes.
charNDArray max(char d, const charNDArray &m)
octave_stream(octave_base_stream *bs=0)
octave_idx_type length(void) const
Number of elements in the array.
bool is_sq_string(void) const
NDArray array_value(bool frc_str_conv=false) const
std::string undo_string_escapes(const std::string &s)
octave_value_list oscanf(const std::string &fmt, const std::string &who)
F77_RET_T const octave_idx_type const octave_idx_type const octave_idx_type const double const double octave_idx_type double * V
bool is_valid(void) const
octave_stream & operator=(const octave_stream &)
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
static std::string list_open_files(void)
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, oct_mach_info::float_format flt_fmt)
static octave_value open_file_numbers(void)
int __lo_ieee_is_old_NA(double x)
octave_refcount< octave_idx_type > count
std::string do_string_escapes(const std::string &s)
#define BEGIN_C_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, oct_mach_info::float_format ffmt)
octave_int64 int64_scalar_value(void) const
string_vector do_get_info(int fid) const
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
oct_mach_info::float_format float_format(void) const
static int get_size(double d, const std::string &who)
bool is_uint64_type(void) const
ostrl_map::const_iterator lookup_cache
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
#define FILL_TABLE_ROW(T, V)
octave_uint64 uint64_scalar_value(void) const
octave_value fast_elem_extract(octave_idx_type n) const
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 MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type octave_idx_type c1
#define FINISH_CHARACTER_CONVERSION()
const T * fortran_vec(void) const
std::string name(void) const
std::string gets(octave_idx_type max_len, bool &err, const std::string &who)
virtual bool eof(void) const =0
void invalid_operation(const std::string &who, const char *rw)
int printf(const std::string &fmt, const octave_value_list &args, const std::string &who)
double double_value(bool frc_str_conv=false) const
virtual int seek(off_t offset, int origin)=0
std::string getl(octave_idx_type max_len, bool &err, const std::string &who)
static int get_file_number(const octave_value &fid)
static size_t data_type_size(data_type dt)
octave_value isnan(void) const
static std::string mode_as_string(int mode)
std::string do_list_open_files(void) const
#define BEGIN_CHAR_CLASS_CONVERSION()
#define INSTANTIATE_WRITE(T)
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
static void convert_ints(const T *data, void *conv_data, octave_idx_type n_elts, bool swap)
octave_value do_binary_op(octave_value::binary_op op, const octave_value &v1, const octave_value &v2)
octave_value do_open_file_numbers(void) const
off_t skipl(off_t count, bool &err, const std::string &who)
bool is_integer_type(void) const