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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4634
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cfloat> |
|
29 #include <cstring> |
|
30 #include <cctype> |
|
31 |
|
32 #include <fstream> |
|
33 #include <iomanip> |
|
34 #include <iostream> |
5765
|
35 #include <sstream> |
4634
|
36 #include <string> |
|
37 |
|
38 #include "byte-swap.h" |
|
39 #include "data-conv.h" |
|
40 #include "file-ops.h" |
|
41 #include "glob-match.h" |
|
42 #include "lo-mappers.h" |
|
43 #include "mach-info.h" |
|
44 #include "oct-env.h" |
|
45 #include "oct-time.h" |
|
46 #include "quit.h" |
|
47 #include "str-vec.h" |
|
48 |
|
49 #include "Cell.h" |
|
50 #include "defun.h" |
|
51 #include "error.h" |
|
52 #include "gripes.h" |
4867
|
53 #include "lex.h" |
4634
|
54 #include "load-save.h" |
|
55 #include "oct-obj.h" |
|
56 #include "oct-map.h" |
|
57 #include "ov-cell.h" |
|
58 #include "pager.h" |
|
59 #include "pt-exp.h" |
|
60 #include "symtab.h" |
|
61 #include "sysdep.h" |
|
62 #include "unwind-prot.h" |
|
63 #include "utils.h" |
|
64 #include "variables.h" |
|
65 #include "version.h" |
|
66 #include "dMatrix.h" |
|
67 |
|
68 #include "ls-mat-ascii.h" |
|
69 |
|
70 static std::string |
|
71 get_mat_data_input_line (std::istream& is) |
|
72 { |
|
73 std::string retval; |
|
74 |
|
75 bool have_data = false; |
|
76 |
|
77 do |
|
78 { |
|
79 retval = ""; |
|
80 |
|
81 char c; |
|
82 while (is.get (c)) |
|
83 { |
|
84 if (c == '\n' || c == '\r') |
|
85 break; |
|
86 |
|
87 if (c == '%' || c == '#') |
|
88 { |
|
89 // skip to end of line |
|
90 while (is.get (c)) |
|
91 if (c == '\n' || c == '\r') |
|
92 break; |
|
93 |
|
94 break; |
|
95 } |
|
96 |
|
97 if (! is.eof ()) |
|
98 { |
|
99 if (! have_data && c != ' ' && c != '\t') |
|
100 have_data = true; |
|
101 |
|
102 retval += c; |
|
103 } |
|
104 } |
|
105 } |
|
106 while (! (have_data || is.eof ())); |
|
107 |
|
108 return retval; |
|
109 } |
|
110 |
|
111 static void |
5275
|
112 get_lines_and_columns (std::istream& is, const std::string& filename, octave_idx_type& nr, octave_idx_type& nc) |
4634
|
113 { |
|
114 std::streampos pos = is.tellg (); |
|
115 |
|
116 int file_line_number = 0; |
|
117 |
|
118 nr = 0; |
|
119 nc = 0; |
|
120 |
|
121 while (is && ! error_state) |
|
122 { |
|
123 OCTAVE_QUIT; |
|
124 |
|
125 std::string buf = get_mat_data_input_line (is); |
|
126 |
|
127 file_line_number++; |
|
128 |
|
129 size_t beg = buf.find_first_not_of (", \t"); |
|
130 |
|
131 // If we see a CR as the last character in the buffer, we had a |
|
132 // CRLF pair as the line separator. Any other CR in the text |
|
133 // will not be considered as whitespace. |
|
134 |
|
135 if (beg != NPOS && buf[beg] == '\r' && beg == buf.length () - 1) |
|
136 { |
|
137 // We had a blank line ending with a CRLF. Handle it the |
|
138 // same as an empty line. |
|
139 beg = NPOS; |
|
140 } |
|
141 |
5275
|
142 octave_idx_type tmp_nc = 0; |
4634
|
143 |
|
144 while (beg != NPOS) |
|
145 { |
|
146 tmp_nc++; |
|
147 |
|
148 size_t end = buf.find_first_of (", \t", beg); |
|
149 |
|
150 if (end != NPOS) |
|
151 { |
|
152 beg = buf.find_first_not_of (", \t", end); |
|
153 |
6856
|
154 if (beg == NPOS || (buf[beg] == '\r' && |
|
155 beg == buf.length () - 1)) |
4634
|
156 { |
|
157 // We had a line with trailing spaces and |
|
158 // ending with a CRLF, so this should look like EOL, |
|
159 // not a new colum. |
|
160 break; |
|
161 } |
|
162 } |
|
163 else |
|
164 break; |
|
165 } |
|
166 |
|
167 if (tmp_nc > 0) |
|
168 { |
|
169 if (nc == 0) |
|
170 { |
|
171 nc = tmp_nc; |
|
172 nr++; |
|
173 } |
|
174 else if (nc == tmp_nc) |
|
175 nr++; |
|
176 else |
|
177 error ("load: %s: inconsistent number of columns near line %d", |
|
178 filename.c_str (), file_line_number); |
|
179 } |
|
180 } |
|
181 |
|
182 if (nr == 0 || nc == 0) |
|
183 error ("load: file `%s' seems to be empty!", filename.c_str ()); |
|
184 |
|
185 is.clear (); |
4643
|
186 is.seekg (pos); |
4634
|
187 } |
|
188 |
|
189 // Extract a matrix from a file of numbers only. |
|
190 // |
|
191 // Comments are not allowed. The file should only have numeric values. |
|
192 // |
|
193 // Reads the file twice. Once to find the number of rows and columns, |
|
194 // and once to extract the matrix. |
|
195 // |
|
196 // FILENAME is used for error messages. |
|
197 // |
|
198 // This format provides no way to tag the data as global. |
|
199 |
|
200 std::string |
|
201 read_mat_ascii_data (std::istream& is, const std::string& filename, |
|
202 octave_value& tc) |
|
203 { |
|
204 std::string retval; |
|
205 |
|
206 std::string varname; |
|
207 |
|
208 size_t pos = filename.rfind ('/'); |
|
209 |
|
210 if (pos != NPOS) |
|
211 varname = filename.substr (pos+1); |
|
212 else |
|
213 varname = filename; |
|
214 |
4867
|
215 pos = varname.rfind ('.'); |
4634
|
216 |
|
217 if (pos != NPOS) |
|
218 varname = varname.substr (0, pos); |
|
219 |
|
220 size_t len = varname.length (); |
|
221 for (size_t i = 0; i < len; i++) |
|
222 { |
|
223 char c = varname[i]; |
|
224 if (! (isalnum (c) || c == '_')) |
|
225 varname[i] = '_'; |
|
226 } |
|
227 |
4867
|
228 if (is_keyword (varname) || ! isalpha (varname[0])) |
4634
|
229 varname.insert (0, "X"); |
|
230 |
|
231 if (valid_identifier (varname)) |
|
232 { |
5275
|
233 octave_idx_type nr = 0; |
|
234 octave_idx_type nc = 0; |
4634
|
235 |
|
236 int total_count = 0; |
|
237 |
|
238 get_lines_and_columns (is, filename, nr, nc); |
|
239 |
|
240 OCTAVE_QUIT; |
|
241 |
|
242 if (! error_state && nr > 0 && nc > 0) |
|
243 { |
|
244 Matrix tmp (nr, nc); |
|
245 |
|
246 if (nr < 1 || nc < 1) |
|
247 is.clear (std::ios::badbit); |
|
248 else |
|
249 { |
|
250 double d; |
5275
|
251 for (octave_idx_type i = 0; i < nr; i++) |
4634
|
252 { |
|
253 std::string buf = get_mat_data_input_line (is); |
|
254 |
|
255 std::istringstream tmp_stream (buf); |
|
256 |
5275
|
257 for (octave_idx_type j = 0; j < nc; j++) |
4634
|
258 { |
|
259 OCTAVE_QUIT; |
|
260 |
|
261 d = octave_read_double (tmp_stream); |
|
262 |
|
263 if (tmp_stream || tmp_stream.eof ()) |
|
264 { |
|
265 tmp.elem (i, j) = d; |
|
266 total_count++; |
|
267 |
|
268 // Skip whitespace and commas. |
|
269 char c; |
|
270 while (1) |
|
271 { |
|
272 tmp_stream >> c; |
|
273 |
|
274 if (! tmp_stream) |
|
275 break; |
|
276 |
|
277 if (! (c == ' ' || c == '\t' || c == ',')) |
|
278 { |
|
279 tmp_stream.putback (c); |
|
280 break; |
|
281 } |
|
282 } |
|
283 |
|
284 if (tmp_stream.eof ()) |
|
285 break; |
|
286 } |
|
287 else |
|
288 { |
|
289 error ("load: failed to read matrix from file `%s'", |
|
290 filename.c_str ()); |
|
291 |
|
292 return retval; |
|
293 } |
|
294 |
|
295 } |
|
296 } |
|
297 } |
|
298 |
|
299 if (is || is.eof ()) |
|
300 { |
5775
|
301 // FIXME -- not sure this is best, but it works. |
4634
|
302 |
|
303 if (is.eof ()) |
|
304 is.clear (); |
|
305 |
5275
|
306 octave_idx_type expected = nr * nc; |
4634
|
307 |
|
308 if (expected == total_count) |
|
309 { |
|
310 tc = tmp; |
|
311 retval = varname; |
|
312 } |
|
313 else |
|
314 error ("load: expected %d elements, found %d", |
|
315 expected, total_count); |
|
316 } |
|
317 else |
|
318 error ("load: failed to read matrix from file `%s'", |
|
319 filename.c_str ()); |
|
320 } |
|
321 else |
|
322 error ("load: unable to extract matrix size from file `%s'", |
|
323 filename.c_str ()); |
|
324 } |
|
325 else |
|
326 error ("load: unable to convert filename `%s' to valid identifier", |
|
327 filename.c_str ()); |
|
328 |
|
329 return retval; |
|
330 } |
|
331 |
5938
|
332 bool |
|
333 save_mat_ascii_data (std::ostream& os, const octave_value& val, |
|
334 int precision) |
|
335 { |
|
336 bool success = true; |
|
337 |
|
338 if (val.is_complex_type ()) |
|
339 warning ("save: omitting imaginary part for ASCII file"); |
|
340 |
|
341 Matrix m = val.matrix_value (true); |
|
342 |
|
343 if (error_state) |
|
344 { |
|
345 success = false; |
|
346 |
|
347 error_state = 0; |
|
348 } |
|
349 else |
5951
|
350 { |
|
351 long old_precision = os.precision (); |
|
352 |
|
353 os.precision (precision); |
5938
|
354 |
5951
|
355 std::ios::fmtflags oflags |
|
356 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific)); |
|
357 |
|
358 os << m; |
|
359 |
|
360 os.flags (oflags); |
|
361 |
|
362 os.precision (old_precision); |
|
363 } |
5938
|
364 |
|
365 return (os && success); |
|
366 } |
|
367 |
4634
|
368 /* |
|
369 ;;; Local Variables: *** |
|
370 ;;; mode: C++ *** |
|
371 ;;; End: *** |
|
372 */ |
|
373 |