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 || (buf[beg] ==
'\r' &&
176 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);