73 bool have_data =
false;
82 if (c ==
'\n' || c ==
'\r')
89 if (c ==
'%' || c ==
'#')
97 if (! have_data && c !=
' ' && c !=
'\t')
104 while (! (have_data || is.eof ()));
112 const std::string& filename = std::string (),
113 bool quiet =
false,
bool check_numeric =
false)
115 std::streampos pos = is.tellg ();
117 int file_line_number = 0;
130 size_t beg = buf.find_first_not_of (
", \t");
136 if (beg != std::string::npos && buf[beg] ==
'\r'
137 && beg == buf.length () - 1)
141 beg = std::string::npos;
146 while (beg != std::string::npos)
150 size_t end = buf.find_first_of (
", \t", beg);
152 if (end != std::string::npos)
156 std::istringstream tmp_stream (buf.substr (beg, end-beg));
160 if (tmp_stream.fail ())
163 error (
"load: %s: non-numeric data found near line %d",
164 filename.c_str (), file_line_number);
173 beg = buf.find_first_not_of (
", \t", end);
175 if (beg == std::string::npos
176 || (buf[beg] ==
'\r' && beg == buf.length () - 1))
195 else if (nc == tmp_nc)
200 error (
"load: %s: inconsistent number of columns near line %d",
201 filename.c_str (), file_line_number);
211 if (! quiet && (nr == 0 || nc == 0))
212 error (
"load: file '%s' seems to be empty!", filename.c_str ());
239 size_t pos = filename.rfind (
'/');
241 if (pos != std::string::npos)
242 varname = filename.substr (pos+1);
246 pos = varname.rfind (
'.');
248 if (pos != std::string::npos)
249 varname = varname.substr (0, pos);
251 size_t len = varname.length ();
252 for (
size_t i = 0; i < len; i++)
255 if (! (isalnum (c) || c ==
'_'))
259 if (
is_keyword (varname) || ! isalpha (varname[0]))
260 varname.insert (0,
"X");
277 if (nr < 1 || nc < 1)
278 is.clear (std::ios::badbit);
286 std::istringstream tmp_stream (buf);
292 d = octave_read_value<double> (tmp_stream);
294 if (tmp_stream || tmp_stream.eof ())
308 if (! (c ==
' ' || c ==
'\t' || c ==
','))
310 tmp_stream.putback (c);
315 if (tmp_stream.eof ())
320 error (
"load: failed to read matrix from file '%s'",
339 if (expected == total_count)
345 error (
"load: expected %d elements, found %d",
346 expected, total_count);
349 error (
"load: failed to read matrix from file '%s'",
353 error (
"load: unable to extract matrix size from file '%s'",
357 error (
"load: unable to convert filename '%s' to valid identifier",
365 int precision,
bool tabs)
370 warning (
"save: omitting imaginary part for ASCII file");
382 long old_precision = os.precision ();
384 os.precision (precision);
386 std::ios::fmtflags oflags
387 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific));
396 if (j != 0) os <<
'\t';
407 os.precision (old_precision);
410 return (os && success);
421 retval = (nr != 0 && nc != 0);
void octave_write_double(std::ostream &os, double d)
double octave_read_double(std::istream &is)
std::string read_mat_ascii_data(std::istream &is, const std::string &filename, octave_value &tc)
void skip_until_newline(std::istream &is, bool keep_newline)
bool looks_like_mat_ascii_file(std::istream &is, const std::string &filename)
void error(const char *fmt,...)
void skip_preceeding_newline(std::istream &is)
T & elem(octave_idx_type n)
bool is_keyword(const std::string &s)
octave_idx_type rows(void) const
F77_RET_T const double const double double * d
bool save_mat_ascii_data(std::ostream &os, const octave_value &val, int precision, bool tabs)
bool valid_identifier(const char *s)
bool is_complex_type(void) const
Matrix matrix_value(bool frc_str_conv=false) const
static std::string get_mat_data_input_line(std::istream &is)
void warning(const char *fmt,...)
octave_idx_type cols(void) const
static void get_lines_and_columns(std::istream &is, octave_idx_type &nr, octave_idx_type &nc, const std::string &filename=std::string(), bool quiet=false, bool check_numeric=false)