26 #if defined (HAVE_CONFIG_H)
72 bool have_data =
false;
81 if (c ==
'\n' || c ==
'\r')
88 if (c ==
'%' || c ==
'#')
96 if (! have_data && c !=
' ' && c !=
'\t')
103 while (! (have_data || is.eof () || is.fail ()));
111 const std::string& filename =
"",
112 bool quiet =
false,
bool check_numeric =
false)
114 std::streampos pos = is.tellg ();
116 int file_line_number = 0;
129 std::size_t beg = buf.find_first_not_of (
", \t");
135 if (beg != std::string::npos && buf[beg] ==
'\r'
136 && beg == buf.length () - 1)
140 beg = std::string::npos;
145 while (beg != std::string::npos)
149 std::size_t end = buf.find_first_of (
", \t", beg);
151 if (end != std::string::npos)
155 std::istringstream tmp_stream (buf.substr (beg, end-beg));
157 octave::read_value<double> (tmp_stream);
159 if (tmp_stream.fail ())
162 error (
"load: %s: non-numeric data found near line %d",
163 filename.c_str (), file_line_number);
172 beg = buf.find_first_not_of (
", \t", end);
174 if (beg == std::string::npos
175 || (buf[beg] ==
'\r' && beg == buf.length () - 1))
193 else if (nc == tmp_nc)
198 error (
"load: %s: inconsistent number of columns near line %d",
199 filename.c_str (), file_line_number);
209 if (! quiet && (nr == 0 || nc == 0))
210 error (
"load: file '%s' seems to be empty!", filename.c_str ());
235 std::size_t pos = filename.rfind (
'/');
237 if (pos != std::string::npos)
238 varname = filename.substr (pos+1);
242 pos = varname.rfind (
'.');
244 if (pos != std::string::npos)
245 varname = varname.substr (0, pos);
247 std::size_t
len = varname.length ();
248 for (std::size_t i = 0; i <
len; i++)
251 if (! (isalnum (c) || c ==
'_'))
256 varname.insert (0,
"X");
259 error (
"load: unable to convert filename '%s' to valid identifier",
271 if (nr <= 0 || nc <= 0)
272 error (
"load: unable to extract matrix size from file '%s'",
282 std::istringstream tmp_stream (buf);
288 d = octave::read_value<double> (tmp_stream);
290 if (! tmp_stream && ! tmp_stream.eof ())
291 error (
"load: failed to read matrix from file '%s'",
306 if (! (c ==
' ' || c ==
'\t' || c ==
','))
308 tmp_stream.putback (c);
313 if (tmp_stream.eof ())
318 if (! is && ! is.eof ())
319 error (
"load: failed to read matrix from file '%s'", filename.c_str ());
327 if (expected != total_count)
328 error (
"load: expected %" OCTAVE_IDX_TYPE_FORMAT
" elements, found "
329 "%" OCTAVE_IDX_TYPE_FORMAT, expected, total_count);
338 int precision,
bool tabs)
343 warning (
"save: omitting imaginary part for ASCII file");
345 if (val.
ndims () > 2)
347 warning (
"save: skipping variable which is not a 2-D matrix");
357 catch (
const octave::execution_exception&)
361 interp.recover_from_exception ();
368 long old_precision = os.precision ();
370 os.precision (precision);
372 std::ios::fmtflags oflags
373 = os.flags (
static_cast<std::ios::fmtflags
> (std::ios::scientific));
382 if (j != 0) os <<
'\t';
383 octave::write_value<double> (os,
m(i, j));
393 os.precision (old_precision);
396 return (os && success);
407 retval = (nr != 0 && nc != 0);
OCTARRAY_OVERRIDABLE_FUNC_API T & elem(octave_idx_type n)
Size of the specified dimension.
Matrix matrix_value(bool frc_str_conv=false) const
bool iscomplex(void) const
void warning(const char *fmt,...)
void error(const char *fmt,...)
interpreter & __get_interpreter__(void)
bool iskeyword(const std::string &s)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
void skip_until_newline(std::istream &is, bool keep_newline)
void skip_preceeding_newline(std::istream &is)
bool save_mat_ascii_data(std::ostream &os, const octave_value &val, int precision, bool tabs)
static void get_lines_and_columns(std::istream &is, octave_idx_type &nr, octave_idx_type &nc, const std::string &filename="", bool quiet=false, bool check_numeric=false)
static std::string get_mat_data_input_line(std::istream &is)
std::string read_mat_ascii_data(std::istream &is, const std::string &filename, octave_value &tc)
bool looks_like_mat_ascii_file(std::istream &is, const std::string &filename)
bool valid_identifier(const char *s)