4634
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 2003, 2004, 2005, 2006, 2007 John W. Eaton |
4634
|
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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
4634
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
4634
|
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> |
5765
|
34 #include <sstream> |
4634
|
35 #include <string> |
|
36 |
|
37 #include "byte-swap.h" |
|
38 #include "data-conv.h" |
|
39 #include "file-ops.h" |
|
40 #include "glob-match.h" |
|
41 #include "lo-mappers.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 "sysdep.h" |
|
60 #include "unwind-prot.h" |
|
61 #include "utils.h" |
|
62 #include "variables.h" |
|
63 #include "version.h" |
|
64 #include "dMatrix.h" |
|
65 |
|
66 #include "ls-mat-ascii.h" |
|
67 |
|
68 static std::string |
|
69 get_mat_data_input_line (std::istream& is) |
|
70 { |
|
71 std::string retval; |
|
72 |
|
73 bool have_data = false; |
|
74 |
|
75 do |
|
76 { |
|
77 retval = ""; |
|
78 |
|
79 char c; |
|
80 while (is.get (c)) |
|
81 { |
|
82 if (c == '\n' || c == '\r') |
|
83 break; |
|
84 |
|
85 if (c == '%' || c == '#') |
|
86 { |
|
87 // skip to end of line |
|
88 while (is.get (c)) |
|
89 if (c == '\n' || c == '\r') |
|
90 break; |
|
91 |
|
92 break; |
|
93 } |
|
94 |
|
95 if (! is.eof ()) |
|
96 { |
|
97 if (! have_data && c != ' ' && c != '\t') |
|
98 have_data = true; |
|
99 |
|
100 retval += c; |
|
101 } |
|
102 } |
|
103 } |
|
104 while (! (have_data || is.eof ())); |
|
105 |
|
106 return retval; |
|
107 } |
|
108 |
|
109 static void |
5275
|
110 get_lines_and_columns (std::istream& is, const std::string& filename, octave_idx_type& nr, octave_idx_type& nc) |
4634
|
111 { |
|
112 std::streampos pos = is.tellg (); |
|
113 |
|
114 int file_line_number = 0; |
|
115 |
|
116 nr = 0; |
|
117 nc = 0; |
|
118 |
|
119 while (is && ! error_state) |
|
120 { |
|
121 OCTAVE_QUIT; |
|
122 |
|
123 std::string buf = get_mat_data_input_line (is); |
|
124 |
|
125 file_line_number++; |
|
126 |
|
127 size_t beg = buf.find_first_not_of (", \t"); |
|
128 |
|
129 // If we see a CR as the last character in the buffer, we had a |
|
130 // CRLF pair as the line separator. Any other CR in the text |
|
131 // will not be considered as whitespace. |
|
132 |
8021
|
133 if (beg != std::string::npos && buf[beg] == '\r' && beg == buf.length () - 1) |
4634
|
134 { |
|
135 // We had a blank line ending with a CRLF. Handle it the |
|
136 // same as an empty line. |
8021
|
137 beg = std::string::npos; |
4634
|
138 } |
|
139 |
5275
|
140 octave_idx_type tmp_nc = 0; |
4634
|
141 |
8021
|
142 while (beg != std::string::npos) |
4634
|
143 { |
|
144 tmp_nc++; |
|
145 |
|
146 size_t end = buf.find_first_of (", \t", beg); |
|
147 |
8021
|
148 if (end != std::string::npos) |
4634
|
149 { |
|
150 beg = buf.find_first_not_of (", \t", end); |
|
151 |
8021
|
152 if (beg == std::string::npos || (buf[beg] == '\r' && |
6856
|
153 beg == buf.length () - 1)) |
4634
|
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 |
8021
|
208 if (pos != std::string::npos) |
4634
|
209 varname = filename.substr (pos+1); |
|
210 else |
|
211 varname = filename; |
|
212 |
4867
|
213 pos = varname.rfind ('.'); |
4634
|
214 |
8021
|
215 if (pos != std::string::npos) |
4634
|
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 { |
5275
|
231 octave_idx_type nr = 0; |
|
232 octave_idx_type nc = 0; |
4634
|
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; |
5275
|
249 for (octave_idx_type i = 0; i < nr; i++) |
4634
|
250 { |
|
251 std::string buf = get_mat_data_input_line (is); |
|
252 |
|
253 std::istringstream tmp_stream (buf); |
|
254 |
5275
|
255 for (octave_idx_type j = 0; j < nc; j++) |
4634
|
256 { |
|
257 OCTAVE_QUIT; |
|
258 |
|
259 d = octave_read_double (tmp_stream); |
|
260 |
|
261 if (tmp_stream || tmp_stream.eof ()) |
|
262 { |
|
263 tmp.elem (i, j) = d; |
|
264 total_count++; |
|
265 |
|
266 // Skip whitespace and commas. |
|
267 char c; |
|
268 while (1) |
|
269 { |
|
270 tmp_stream >> c; |
|
271 |
|
272 if (! tmp_stream) |
|
273 break; |
|
274 |
|
275 if (! (c == ' ' || c == '\t' || c == ',')) |
|
276 { |
|
277 tmp_stream.putback (c); |
|
278 break; |
|
279 } |
|
280 } |
|
281 |
|
282 if (tmp_stream.eof ()) |
|
283 break; |
|
284 } |
|
285 else |
|
286 { |
|
287 error ("load: failed to read matrix from file `%s'", |
|
288 filename.c_str ()); |
|
289 |
|
290 return retval; |
|
291 } |
|
292 |
|
293 } |
|
294 } |
|
295 } |
|
296 |
|
297 if (is || is.eof ()) |
|
298 { |
5775
|
299 // FIXME -- not sure this is best, but it works. |
4634
|
300 |
|
301 if (is.eof ()) |
|
302 is.clear (); |
|
303 |
5275
|
304 octave_idx_type expected = nr * nc; |
4634
|
305 |
|
306 if (expected == total_count) |
|
307 { |
|
308 tc = tmp; |
|
309 retval = varname; |
|
310 } |
|
311 else |
|
312 error ("load: expected %d elements, found %d", |
|
313 expected, total_count); |
|
314 } |
|
315 else |
|
316 error ("load: failed to read matrix from file `%s'", |
|
317 filename.c_str ()); |
|
318 } |
|
319 else |
|
320 error ("load: unable to extract matrix size from file `%s'", |
|
321 filename.c_str ()); |
|
322 } |
|
323 else |
|
324 error ("load: unable to convert filename `%s' to valid identifier", |
|
325 filename.c_str ()); |
|
326 |
|
327 return retval; |
|
328 } |
|
329 |
5938
|
330 bool |
|
331 save_mat_ascii_data (std::ostream& os, const octave_value& val, |
|
332 int precision) |
|
333 { |
|
334 bool success = true; |
|
335 |
|
336 if (val.is_complex_type ()) |
|
337 warning ("save: omitting imaginary part for ASCII file"); |
|
338 |
|
339 Matrix m = val.matrix_value (true); |
|
340 |
|
341 if (error_state) |
|
342 { |
|
343 success = false; |
|
344 |
|
345 error_state = 0; |
|
346 } |
|
347 else |
5951
|
348 { |
|
349 long old_precision = os.precision (); |
|
350 |
|
351 os.precision (precision); |
5938
|
352 |
5951
|
353 std::ios::fmtflags oflags |
|
354 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific)); |
|
355 |
|
356 os << m; |
|
357 |
|
358 os.flags (oflags); |
|
359 |
|
360 os.precision (old_precision); |
|
361 } |
5938
|
362 |
|
363 return (os && success); |
|
364 } |
|
365 |
4634
|
366 /* |
|
367 ;;; Local Variables: *** |
|
368 ;;; mode: C++ *** |
|
369 ;;; End: *** |
|
370 */ |
|
371 |