4634
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cfloat> |
|
28 #include <cstring> |
|
29 #include <cctype> |
|
30 |
|
31 #include <fstream> |
|
32 #include <iomanip> |
|
33 #include <iostream> |
|
34 #include <string> |
|
35 |
|
36 #include "byte-swap.h" |
|
37 #include "data-conv.h" |
|
38 #include "file-ops.h" |
|
39 #include "glob-match.h" |
|
40 #include "lo-mappers.h" |
|
41 #include "lo-sstream.h" |
|
42 #include "mach-info.h" |
|
43 #include "oct-env.h" |
|
44 #include "oct-time.h" |
|
45 #include "quit.h" |
|
46 #include "str-vec.h" |
|
47 |
|
48 #include "Cell.h" |
|
49 #include "defun.h" |
|
50 #include "error.h" |
|
51 #include "gripes.h" |
4867
|
52 #include "lex.h" |
4634
|
53 #include "load-save.h" |
|
54 #include "oct-obj.h" |
|
55 #include "oct-map.h" |
|
56 #include "ov-cell.h" |
|
57 #include "pager.h" |
|
58 #include "pt-exp.h" |
|
59 #include "symtab.h" |
|
60 #include "sysdep.h" |
|
61 #include "unwind-prot.h" |
|
62 #include "utils.h" |
|
63 #include "variables.h" |
|
64 #include "version.h" |
|
65 #include "dMatrix.h" |
|
66 |
|
67 #include "ls-mat-ascii.h" |
|
68 |
|
69 static std::string |
|
70 get_mat_data_input_line (std::istream& is) |
|
71 { |
|
72 std::string retval; |
|
73 |
|
74 bool have_data = false; |
|
75 |
|
76 do |
|
77 { |
|
78 retval = ""; |
|
79 |
|
80 char c; |
|
81 while (is.get (c)) |
|
82 { |
|
83 if (c == '\n' || c == '\r') |
|
84 break; |
|
85 |
|
86 if (c == '%' || c == '#') |
|
87 { |
|
88 // skip to end of line |
|
89 while (is.get (c)) |
|
90 if (c == '\n' || c == '\r') |
|
91 break; |
|
92 |
|
93 break; |
|
94 } |
|
95 |
|
96 if (! is.eof ()) |
|
97 { |
|
98 if (! have_data && c != ' ' && c != '\t') |
|
99 have_data = true; |
|
100 |
|
101 retval += c; |
|
102 } |
|
103 } |
|
104 } |
|
105 while (! (have_data || is.eof ())); |
|
106 |
|
107 return retval; |
|
108 } |
|
109 |
|
110 static void |
|
111 get_lines_and_columns (std::istream& is, const std::string& filename, int& nr, int& nc) |
|
112 { |
|
113 std::streampos pos = is.tellg (); |
|
114 |
|
115 int file_line_number = 0; |
|
116 |
|
117 nr = 0; |
|
118 nc = 0; |
|
119 |
|
120 while (is && ! error_state) |
|
121 { |
|
122 OCTAVE_QUIT; |
|
123 |
|
124 std::string buf = get_mat_data_input_line (is); |
|
125 |
|
126 file_line_number++; |
|
127 |
|
128 size_t beg = buf.find_first_not_of (", \t"); |
|
129 |
|
130 // If we see a CR as the last character in the buffer, we had a |
|
131 // CRLF pair as the line separator. Any other CR in the text |
|
132 // will not be considered as whitespace. |
|
133 |
|
134 if (beg != NPOS && buf[beg] == '\r' && beg == buf.length () - 1) |
|
135 { |
|
136 // We had a blank line ending with a CRLF. Handle it the |
|
137 // same as an empty line. |
|
138 beg = NPOS; |
|
139 } |
|
140 |
|
141 int tmp_nc = 0; |
|
142 |
|
143 while (beg != NPOS) |
|
144 { |
|
145 tmp_nc++; |
|
146 |
|
147 size_t end = buf.find_first_of (", \t", beg); |
|
148 |
|
149 if (end != NPOS) |
|
150 { |
|
151 beg = buf.find_first_not_of (", \t", end); |
|
152 |
|
153 if (buf[beg] == '\r' && beg == buf.length () - 1) |
|
154 { |
|
155 // We had a line with trailing spaces and |
|
156 // ending with a CRLF, so this should look like EOL, |
|
157 // not a new colum. |
|
158 break; |
|
159 } |
|
160 } |
|
161 else |
|
162 break; |
|
163 } |
|
164 |
|
165 if (tmp_nc > 0) |
|
166 { |
|
167 if (nc == 0) |
|
168 { |
|
169 nc = tmp_nc; |
|
170 nr++; |
|
171 } |
|
172 else if (nc == tmp_nc) |
|
173 nr++; |
|
174 else |
|
175 error ("load: %s: inconsistent number of columns near line %d", |
|
176 filename.c_str (), file_line_number); |
|
177 } |
|
178 } |
|
179 |
|
180 if (nr == 0 || nc == 0) |
|
181 error ("load: file `%s' seems to be empty!", filename.c_str ()); |
|
182 |
|
183 is.clear (); |
4643
|
184 is.seekg (pos); |
4634
|
185 } |
|
186 |
|
187 // Extract a matrix from a file of numbers only. |
|
188 // |
|
189 // Comments are not allowed. The file should only have numeric values. |
|
190 // |
|
191 // Reads the file twice. Once to find the number of rows and columns, |
|
192 // and once to extract the matrix. |
|
193 // |
|
194 // FILENAME is used for error messages. |
|
195 // |
|
196 // This format provides no way to tag the data as global. |
|
197 |
|
198 std::string |
|
199 read_mat_ascii_data (std::istream& is, const std::string& filename, |
|
200 octave_value& tc) |
|
201 { |
|
202 std::string retval; |
|
203 |
|
204 std::string varname; |
|
205 |
|
206 size_t pos = filename.rfind ('/'); |
|
207 |
|
208 if (pos != NPOS) |
|
209 varname = filename.substr (pos+1); |
|
210 else |
|
211 varname = filename; |
|
212 |
4867
|
213 pos = varname.rfind ('.'); |
4634
|
214 |
|
215 if (pos != NPOS) |
|
216 varname = varname.substr (0, pos); |
|
217 |
|
218 size_t len = varname.length (); |
|
219 for (size_t i = 0; i < len; i++) |
|
220 { |
|
221 char c = varname[i]; |
|
222 if (! (isalnum (c) || c == '_')) |
|
223 varname[i] = '_'; |
|
224 } |
|
225 |
4867
|
226 if (is_keyword (varname) || ! isalpha (varname[0])) |
4634
|
227 varname.insert (0, "X"); |
|
228 |
|
229 if (valid_identifier (varname)) |
|
230 { |
|
231 int nr = 0; |
|
232 int nc = 0; |
|
233 |
|
234 int total_count = 0; |
|
235 |
|
236 get_lines_and_columns (is, filename, nr, nc); |
|
237 |
|
238 OCTAVE_QUIT; |
|
239 |
|
240 if (! error_state && nr > 0 && nc > 0) |
|
241 { |
|
242 Matrix tmp (nr, nc); |
|
243 |
|
244 if (nr < 1 || nc < 1) |
|
245 is.clear (std::ios::badbit); |
|
246 else |
|
247 { |
|
248 double d; |
|
249 for (int i = 0; i < nr; i++) |
|
250 { |
|
251 std::string buf = get_mat_data_input_line (is); |
|
252 |
|
253 #ifdef HAVE_SSTREAM |
|
254 std::istringstream tmp_stream (buf); |
|
255 #else |
|
256 std::istrstream tmp_stream (buf.c_str ()); |
|
257 #endif |
|
258 |
|
259 for (int j = 0; j < nc; j++) |
|
260 { |
|
261 OCTAVE_QUIT; |
|
262 |
|
263 d = octave_read_double (tmp_stream); |
|
264 |
|
265 if (tmp_stream || tmp_stream.eof ()) |
|
266 { |
|
267 tmp.elem (i, j) = d; |
|
268 total_count++; |
|
269 |
|
270 // Skip whitespace and commas. |
|
271 char c; |
|
272 while (1) |
|
273 { |
|
274 tmp_stream >> c; |
|
275 |
|
276 if (! tmp_stream) |
|
277 break; |
|
278 |
|
279 if (! (c == ' ' || c == '\t' || c == ',')) |
|
280 { |
|
281 tmp_stream.putback (c); |
|
282 break; |
|
283 } |
|
284 } |
|
285 |
|
286 if (tmp_stream.eof ()) |
|
287 break; |
|
288 } |
|
289 else |
|
290 { |
|
291 error ("load: failed to read matrix from file `%s'", |
|
292 filename.c_str ()); |
|
293 |
|
294 return retval; |
|
295 } |
|
296 |
|
297 } |
|
298 } |
|
299 } |
|
300 |
|
301 if (is || is.eof ()) |
|
302 { |
|
303 // XXX FIXME XXX -- not sure this is best, but it works. |
|
304 |
|
305 if (is.eof ()) |
|
306 is.clear (); |
|
307 |
|
308 int expected = nr * nc; |
|
309 |
|
310 if (expected == total_count) |
|
311 { |
|
312 tc = tmp; |
|
313 retval = varname; |
|
314 } |
|
315 else |
|
316 error ("load: expected %d elements, found %d", |
|
317 expected, total_count); |
|
318 } |
|
319 else |
|
320 error ("load: failed to read matrix from file `%s'", |
|
321 filename.c_str ()); |
|
322 } |
|
323 else |
|
324 error ("load: unable to extract matrix size from file `%s'", |
|
325 filename.c_str ()); |
|
326 } |
|
327 else |
|
328 error ("load: unable to convert filename `%s' to valid identifier", |
|
329 filename.c_str ()); |
|
330 |
|
331 return retval; |
|
332 } |
|
333 |
|
334 /* |
|
335 ;;; Local Variables: *** |
|
336 ;;; mode: C++ *** |
|
337 ;;; End: *** |
|
338 */ |
|
339 |