Mercurial > hg > octave-lyh
comparison libinterp/interp-core/ls-mat-ascii.cc @ 15195:2fc554ffbc28
split libinterp from src
* libinterp: New directory. Move all files from src directory here
except Makefile.am, main.cc, main-cli.cc, mkoctfile.in.cc,
mkoctfilr.in.sh, octave-config.in.cc, octave-config.in.sh.
* libinterp/Makefile.am: New file, extracted from src/Makefile.am.
* src/Makefile.am: Delete everything except targets and definitions
needed to build and link main and utility programs.
* Makefile.am (SUBDIRS): Include libinterp in the list.
* autogen.sh: Run config-module.sh in libinterp/dldfcn directory, not
src/dldfcn directory.
* configure.ac (AC_CONFIG_SRCDIR): Use libinterp/octave.cc, not
src/octave.cc.
(DL_LDFLAGS, LIBOCTINTERP): Use libinterp, not src.
(AC_CONFIG_FILES): Include libinterp/Makefile in the list.
* find-docstring-files.sh: Look in libinterp, not src.
* gui/src/Makefile.am (liboctgui_la_CPPFLAGS): Find header files in
libinterp, not src.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 18 Aug 2012 16:23:39 -0400 |
parents | src/interp-core/ls-mat-ascii.cc@909a2797935b |
children | 049e8bbff782 |
comparison
equal
deleted
inserted
replaced
15194:0f0b795044c3 | 15195:2fc554ffbc28 |
---|---|
1 /* | |
2 | |
3 Copyright (C) 1996-2012 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 3 of the License, or (at your | |
10 option) any 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, see | |
19 <http://www.gnu.org/licenses/>. | |
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 <sstream> | |
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" | |
52 #include "lex.h" | |
53 #include "load-save.h" | |
54 #include "ls-ascii-helper.h" | |
55 #include "ls-mat-ascii.h" | |
56 #include "oct-obj.h" | |
57 #include "oct-map.h" | |
58 #include "ov-cell.h" | |
59 #include "pager.h" | |
60 #include "pt-exp.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 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 { | |
84 is.putback (c); | |
85 skip_preceeding_newline (is); | |
86 break; | |
87 } | |
88 | |
89 if (c == '%' || c == '#') | |
90 { | |
91 skip_until_newline (is, false); | |
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 | |
110 get_lines_and_columns (std::istream& is, const std::string& filename, octave_idx_type& nr, octave_idx_type& nc) | |
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 | |
133 if (beg != std::string::npos && buf[beg] == '\r' && beg == buf.length () - 1) | |
134 { | |
135 // We had a blank line ending with a CRLF. Handle it the | |
136 // same as an empty line. | |
137 beg = std::string::npos; | |
138 } | |
139 | |
140 octave_idx_type tmp_nc = 0; | |
141 | |
142 while (beg != std::string::npos) | |
143 { | |
144 tmp_nc++; | |
145 | |
146 size_t end = buf.find_first_of (", \t", beg); | |
147 | |
148 if (end != std::string::npos) | |
149 { | |
150 beg = buf.find_first_not_of (", \t", end); | |
151 | |
152 if (beg == std::string::npos || (buf[beg] == '\r' && | |
153 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 (); | |
184 is.seekg (pos); | |
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 != std::string::npos) | |
209 varname = filename.substr (pos+1); | |
210 else | |
211 varname = filename; | |
212 | |
213 pos = varname.rfind ('.'); | |
214 | |
215 if (pos != std::string::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 | |
226 if (is_keyword (varname) || ! isalpha (varname[0])) | |
227 varname.insert (0, "X"); | |
228 | |
229 if (valid_identifier (varname)) | |
230 { | |
231 octave_idx_type nr = 0; | |
232 octave_idx_type 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 (octave_idx_type i = 0; i < nr; i++) | |
250 { | |
251 std::string buf = get_mat_data_input_line (is); | |
252 | |
253 std::istringstream tmp_stream (buf); | |
254 | |
255 for (octave_idx_type j = 0; j < nc; j++) | |
256 { | |
257 octave_quit (); | |
258 | |
259 d = octave_read_value<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 { | |
299 // FIXME -- not sure this is best, but it works. | |
300 | |
301 if (is.eof ()) | |
302 is.clear (); | |
303 | |
304 octave_idx_type expected = nr * nc; | |
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 | |
330 bool | |
331 save_mat_ascii_data (std::ostream& os, const octave_value& val, | |
332 int precision, bool tabs) | |
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 | |
348 { | |
349 long old_precision = os.precision (); | |
350 | |
351 os.precision (precision); | |
352 | |
353 std::ios::fmtflags oflags | |
354 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific)); | |
355 | |
356 if (tabs) | |
357 { | |
358 for (octave_idx_type i = 0; i < m.rows (); i++) | |
359 { | |
360 for (octave_idx_type j = 0; j < m.cols (); j++) | |
361 { | |
362 // Omit leading tabs. | |
363 if (j != 0) os << '\t'; | |
364 octave_write_double (os, m (i, j)); | |
365 } | |
366 os << "\n"; | |
367 } | |
368 } | |
369 else | |
370 os << m; | |
371 | |
372 os.flags (oflags); | |
373 | |
374 os.precision (old_precision); | |
375 } | |
376 | |
377 return (os && success); | |
378 } |