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 |
|
154 if (buf[beg] == '\r' && beg == buf.length () - 1) |
|
155 { |
|
156 // We had a line with trailing spaces and |
|
157 // ending with a CRLF, so this should look like EOL, |
|
158 // not a new colum. |
|
159 break; |
|
160 } |
|
161 } |
|
162 else |
|
163 break; |
|
164 } |
|
165 |
|
166 if (tmp_nc > 0) |
|
167 { |
|
168 if (nc == 0) |
|
169 { |
|
170 nc = tmp_nc; |
|
171 nr++; |
|
172 } |
|
173 else if (nc == tmp_nc) |
|
174 nr++; |
|
175 else |
|
176 error ("load: %s: inconsistent number of columns near line %d", |
|
177 filename.c_str (), file_line_number); |
|
178 } |
|
179 } |
|
180 |
|
181 if (nr == 0 || nc == 0) |
|
182 error ("load: file `%s' seems to be empty!", filename.c_str ()); |
|
183 |
|
184 is.clear (); |
4643
|
185 is.seekg (pos); |
4634
|
186 } |
|
187 |
|
188 // Extract a matrix from a file of numbers only. |
|
189 // |
|
190 // Comments are not allowed. The file should only have numeric values. |
|
191 // |
|
192 // Reads the file twice. Once to find the number of rows and columns, |
|
193 // and once to extract the matrix. |
|
194 // |
|
195 // FILENAME is used for error messages. |
|
196 // |
|
197 // This format provides no way to tag the data as global. |
|
198 |
|
199 std::string |
|
200 read_mat_ascii_data (std::istream& is, const std::string& filename, |
|
201 octave_value& tc) |
|
202 { |
|
203 std::string retval; |
|
204 |
|
205 std::string varname; |
|
206 |
|
207 size_t pos = filename.rfind ('/'); |
|
208 |
|
209 if (pos != NPOS) |
|
210 varname = filename.substr (pos+1); |
|
211 else |
|
212 varname = filename; |
|
213 |
4867
|
214 pos = varname.rfind ('.'); |
4634
|
215 |
|
216 if (pos != NPOS) |
|
217 varname = varname.substr (0, pos); |
|
218 |
|
219 size_t len = varname.length (); |
|
220 for (size_t i = 0; i < len; i++) |
|
221 { |
|
222 char c = varname[i]; |
|
223 if (! (isalnum (c) || c == '_')) |
|
224 varname[i] = '_'; |
|
225 } |
|
226 |
4867
|
227 if (is_keyword (varname) || ! isalpha (varname[0])) |
4634
|
228 varname.insert (0, "X"); |
|
229 |
|
230 if (valid_identifier (varname)) |
|
231 { |
5275
|
232 octave_idx_type nr = 0; |
|
233 octave_idx_type nc = 0; |
4634
|
234 |
|
235 int total_count = 0; |
|
236 |
|
237 get_lines_and_columns (is, filename, nr, nc); |
|
238 |
|
239 OCTAVE_QUIT; |
|
240 |
|
241 if (! error_state && nr > 0 && nc > 0) |
|
242 { |
|
243 Matrix tmp (nr, nc); |
|
244 |
|
245 if (nr < 1 || nc < 1) |
|
246 is.clear (std::ios::badbit); |
|
247 else |
|
248 { |
|
249 double d; |
5275
|
250 for (octave_idx_type i = 0; i < nr; i++) |
4634
|
251 { |
|
252 std::string buf = get_mat_data_input_line (is); |
|
253 |
|
254 std::istringstream tmp_stream (buf); |
|
255 |
5275
|
256 for (octave_idx_type j = 0; j < nc; j++) |
4634
|
257 { |
|
258 OCTAVE_QUIT; |
|
259 |
|
260 d = octave_read_double (tmp_stream); |
|
261 |
|
262 if (tmp_stream || tmp_stream.eof ()) |
|
263 { |
|
264 tmp.elem (i, j) = d; |
|
265 total_count++; |
|
266 |
|
267 // Skip whitespace and commas. |
|
268 char c; |
|
269 while (1) |
|
270 { |
|
271 tmp_stream >> c; |
|
272 |
|
273 if (! tmp_stream) |
|
274 break; |
|
275 |
|
276 if (! (c == ' ' || c == '\t' || c == ',')) |
|
277 { |
|
278 tmp_stream.putback (c); |
|
279 break; |
|
280 } |
|
281 } |
|
282 |
|
283 if (tmp_stream.eof ()) |
|
284 break; |
|
285 } |
|
286 else |
|
287 { |
|
288 error ("load: failed to read matrix from file `%s'", |
|
289 filename.c_str ()); |
|
290 |
|
291 return retval; |
|
292 } |
|
293 |
|
294 } |
|
295 } |
|
296 } |
|
297 |
|
298 if (is || is.eof ()) |
|
299 { |
5775
|
300 // FIXME -- not sure this is best, but it works. |
4634
|
301 |
|
302 if (is.eof ()) |
|
303 is.clear (); |
|
304 |
5275
|
305 octave_idx_type expected = nr * nc; |
4634
|
306 |
|
307 if (expected == total_count) |
|
308 { |
|
309 tc = tmp; |
|
310 retval = varname; |
|
311 } |
|
312 else |
|
313 error ("load: expected %d elements, found %d", |
|
314 expected, total_count); |
|
315 } |
|
316 else |
|
317 error ("load: failed to read matrix from file `%s'", |
|
318 filename.c_str ()); |
|
319 } |
|
320 else |
|
321 error ("load: unable to extract matrix size from file `%s'", |
|
322 filename.c_str ()); |
|
323 } |
|
324 else |
|
325 error ("load: unable to convert filename `%s' to valid identifier", |
|
326 filename.c_str ()); |
|
327 |
|
328 return retval; |
|
329 } |
|
330 |
5938
|
331 bool |
|
332 save_mat_ascii_data (std::ostream& os, const octave_value& val, |
|
333 int precision) |
|
334 { |
|
335 bool success = true; |
|
336 |
|
337 if (val.is_complex_type ()) |
|
338 warning ("save: omitting imaginary part for ASCII file"); |
|
339 |
|
340 Matrix m = val.matrix_value (true); |
|
341 |
|
342 if (error_state) |
|
343 { |
|
344 success = false; |
|
345 |
|
346 error_state = 0; |
|
347 } |
|
348 else |
5951
|
349 { |
|
350 long old_precision = os.precision (); |
|
351 |
|
352 os.precision (precision); |
5938
|
353 |
5951
|
354 std::ios::fmtflags oflags |
|
355 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific)); |
|
356 |
|
357 os << m; |
|
358 |
|
359 os.flags (oflags); |
|
360 |
|
361 os.precision (old_precision); |
|
362 } |
5938
|
363 |
|
364 return (os && success); |
|
365 } |
|
366 |
4634
|
367 /* |
|
368 ;;; Local Variables: *** |
|
369 ;;; mode: C++ *** |
|
370 ;;; End: *** |
|
371 */ |
|
372 |