comparison src/load-save.cc @ 4231:1032fb9ec0d1

[project @ 2002-12-19 19:58:29 by jwe]
author jwe
date Thu, 19 Dec 2002 19:59:13 +0000
parents 23d06c9e1edd
children b032ebd54586
comparison
equal deleted inserted replaced
4230:1feaee8df4ff 4231:1032fb9ec0d1
1897 1897
1898 std::string buf = get_mat_data_input_line (is); 1898 std::string buf = get_mat_data_input_line (is);
1899 1899
1900 file_line_number++; 1900 file_line_number++;
1901 1901
1902 size_t beg = buf.find_first_not_of (" \t"); 1902 size_t beg = buf.find_first_not_of (", \t");
1903 1903
1904 // If we see a CR as the last character in the buffer, we had a 1904 // If we see a CR as the last character in the buffer, we had a
1905 // CRLF pair as the line separator. Any other CR in the text 1905 // CRLF pair as the line separator. Any other CR in the text
1906 // will not be considered as whitespace. 1906 // will not be considered as whitespace.
1907 1907
1916 1916
1917 while (beg != NPOS) 1917 while (beg != NPOS)
1918 { 1918 {
1919 tmp_nc++; 1919 tmp_nc++;
1920 1920
1921 size_t end = buf.find_first_of (" \t", beg); 1921 size_t end = buf.find_first_of (", \t", beg);
1922 1922
1923 if (end != NPOS) 1923 if (end != NPOS)
1924 { 1924 {
1925 beg = buf.find_first_not_of (" \t", end); 1925 beg = buf.find_first_not_of (", \t", end);
1926 1926
1927 if (buf[beg] == '\r' && beg == buf.length () - 1) 1927 if (buf[beg] == '\r' && beg == buf.length () - 1)
1928 { 1928 {
1929 // We had a line with trailing spaces and 1929 // We had a line with trailing spaces and
1930 // ending with a CRLF, so this should look like EOL, 1930 // ending with a CRLF, so this should look like EOL,
1992 if (valid_identifier (varname)) 1992 if (valid_identifier (varname))
1993 { 1993 {
1994 int nr = 0; 1994 int nr = 0;
1995 int nc = 0; 1995 int nc = 0;
1996 1996
1997 int total_count = 0;
1998
1997 get_lines_and_columns (is, filename, nr, nc); 1999 get_lines_and_columns (is, filename, nr, nc);
1998 2000
1999 OCTAVE_QUIT; 2001 OCTAVE_QUIT;
2000 2002
2001 if (! error_state && nr > 0 && nc > 0) 2003 if (! error_state && nr > 0 && nc > 0)
2008 { 2010 {
2009 double d; 2011 double d;
2010 for (int i = 0; i < nr; i++) 2012 for (int i = 0; i < nr; i++)
2011 { 2013 {
2012 std::string buf = get_mat_data_input_line (is); 2014 std::string buf = get_mat_data_input_line (is);
2015 std::cerr << buf<< std::endl;
2013 2016
2014 #ifdef HAVE_SSTREAM 2017 #ifdef HAVE_SSTREAM
2015 std::istringstream tmp_stream (buf); 2018 std::istringstream tmp_stream (buf);
2016 #else 2019 #else
2017 std::istrstream tmp_stream (buf.c_str ()); 2020 std::istrstream tmp_stream (buf.c_str ());
2021 { 2024 {
2022 OCTAVE_QUIT; 2025 OCTAVE_QUIT;
2023 2026
2024 d = octave_read_double (tmp_stream); 2027 d = octave_read_double (tmp_stream);
2025 2028
2026 if (tmp_stream) 2029 if (tmp_stream || tmp_stream.eof ())
2027 tmp.elem (i, j) = d; 2030 {
2031 tmp.elem (i, j) = d;
2032 total_count++;
2033
2034 // Skip whitespace and commas.
2035 char c;
2036 while (1)
2037 {
2038 tmp_stream >> c;
2039
2040 if (! tmp_stream)
2041 break;
2042
2043 if (! (c == ' ' || c == '\t' || c == ','))
2044 {
2045 tmp_stream.putback (c);
2046 break;
2047 }
2048 }
2049
2050 if (tmp_stream.eof ())
2051 break;
2052 }
2028 else 2053 else
2029 { 2054 {
2030 error ("load: failed to read matrix from file `%s'", 2055 error ("load: failed to read matrix from file `%s'",
2031 filename.c_str ()); 2056 filename.c_str ());
2032 2057
2035 2060
2036 } 2061 }
2037 } 2062 }
2038 } 2063 }
2039 2064
2040 if (is) 2065 if (is || is.eof ())
2041 { 2066 {
2042 tc = tmp; 2067 // XXX FIXME XXX -- not sure this is best, but it works.
2043 2068
2044 retval = varname; 2069 if (is.eof ())
2070 is.clear ();
2071
2072 int expected = nr * nc;
2073
2074 if (expected == total_count)
2075 {
2076 tc = tmp;
2077 retval = varname;
2078 }
2079 else
2080 error ("load: expected %d elements, found %d",
2081 expected, total_count);
2045 } 2082 }
2046 else 2083 else
2047 error ("load: failed to read matrix from file `%s'", 2084 error ("load: failed to read matrix from file `%s'",
2048 filename.c_str ()); 2085 filename.c_str ());
2049 } 2086 }