Mercurial > octave-nkf
annotate src/ls-mat-ascii.cc @ 11944:278afaecddd4 release-3-0-x
fix leaving stray '\r' in stream when reading from CRLF data file
* * *
fix CRLF issues with text-mode reading in windows when loading ascii data
author | Benjamin Lindner <lindnerb@users.sourceforge.net> |
---|---|
date | Wed, 18 Mar 2009 15:23:14 +0100 |
parents | a1dbe9d80eee |
children | 21f8e9c91b77 |
rev | line source |
---|---|
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 "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" | |
11944
278afaecddd4
fix leaving stray '\r' in stream when reading from CRLF data file
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
7017
diff
changeset
|
68 #include "ls-ascii-helper.h" |
4634 | 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') | |
11944
278afaecddd4
fix leaving stray '\r' in stream when reading from CRLF data file
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
7017
diff
changeset
|
85 { |
278afaecddd4
fix leaving stray '\r' in stream when reading from CRLF data file
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
7017
diff
changeset
|
86 // Let skip_until_newline handle CR/LF issues... |
278afaecddd4
fix leaving stray '\r' in stream when reading from CRLF data file
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
7017
diff
changeset
|
87 skip_until_newline (is, false); |
278afaecddd4
fix leaving stray '\r' in stream when reading from CRLF data file
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
7017
diff
changeset
|
88 break; |
278afaecddd4
fix leaving stray '\r' in stream when reading from CRLF data file
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
7017
diff
changeset
|
89 } |
4634 | 90 |
91 if (c == '%' || c == '#') | |
92 { | |
93 // skip to end of line | |
11944
278afaecddd4
fix leaving stray '\r' in stream when reading from CRLF data file
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
7017
diff
changeset
|
94 skip_until_newline (is, false); |
4634 | 95 |
96 break; | |
97 } | |
98 | |
99 if (! is.eof ()) | |
100 { | |
101 if (! have_data && c != ' ' && c != '\t') | |
102 have_data = true; | |
103 | |
104 retval += c; | |
105 } | |
106 } | |
107 } | |
108 while (! (have_data || is.eof ())); | |
109 | |
110 return retval; | |
111 } | |
112 | |
113 static void | |
5275 | 114 get_lines_and_columns (std::istream& is, const std::string& filename, octave_idx_type& nr, octave_idx_type& nc) |
4634 | 115 { |
116 std::streampos pos = is.tellg (); | |
117 | |
118 int file_line_number = 0; | |
119 | |
120 nr = 0; | |
121 nc = 0; | |
122 | |
123 while (is && ! error_state) | |
124 { | |
125 OCTAVE_QUIT; | |
126 | |
127 std::string buf = get_mat_data_input_line (is); | |
128 | |
129 file_line_number++; | |
130 | |
131 size_t beg = buf.find_first_not_of (", \t"); | |
132 | |
133 // If we see a CR as the last character in the buffer, we had a | |
134 // CRLF pair as the line separator. Any other CR in the text | |
135 // will not be considered as whitespace. | |
136 | |
137 if (beg != NPOS && buf[beg] == '\r' && beg == buf.length () - 1) | |
138 { | |
139 // We had a blank line ending with a CRLF. Handle it the | |
140 // same as an empty line. | |
141 beg = NPOS; | |
142 } | |
143 | |
5275 | 144 octave_idx_type tmp_nc = 0; |
4634 | 145 |
146 while (beg != NPOS) | |
147 { | |
148 tmp_nc++; | |
149 | |
150 size_t end = buf.find_first_of (", \t", beg); | |
151 | |
152 if (end != NPOS) | |
153 { | |
154 beg = buf.find_first_not_of (", \t", end); | |
155 | |
6856 | 156 if (beg == NPOS || (buf[beg] == '\r' && |
157 beg == buf.length () - 1)) | |
4634 | 158 { |
159 // We had a line with trailing spaces and | |
160 // ending with a CRLF, so this should look like EOL, | |
161 // not a new colum. | |
162 break; | |
163 } | |
164 } | |
165 else | |
166 break; | |
167 } | |
168 | |
169 if (tmp_nc > 0) | |
170 { | |
171 if (nc == 0) | |
172 { | |
173 nc = tmp_nc; | |
174 nr++; | |
175 } | |
176 else if (nc == tmp_nc) | |
177 nr++; | |
178 else | |
179 error ("load: %s: inconsistent number of columns near line %d", | |
180 filename.c_str (), file_line_number); | |
181 } | |
182 } | |
183 | |
184 if (nr == 0 || nc == 0) | |
185 error ("load: file `%s' seems to be empty!", filename.c_str ()); | |
186 | |
187 is.clear (); | |
4643 | 188 is.seekg (pos); |
4634 | 189 } |
190 | |
191 // Extract a matrix from a file of numbers only. | |
192 // | |
193 // Comments are not allowed. The file should only have numeric values. | |
194 // | |
195 // Reads the file twice. Once to find the number of rows and columns, | |
196 // and once to extract the matrix. | |
197 // | |
198 // FILENAME is used for error messages. | |
199 // | |
200 // This format provides no way to tag the data as global. | |
201 | |
202 std::string | |
203 read_mat_ascii_data (std::istream& is, const std::string& filename, | |
204 octave_value& tc) | |
205 { | |
206 std::string retval; | |
207 | |
208 std::string varname; | |
209 | |
210 size_t pos = filename.rfind ('/'); | |
211 | |
212 if (pos != NPOS) | |
213 varname = filename.substr (pos+1); | |
214 else | |
215 varname = filename; | |
216 | |
4867 | 217 pos = varname.rfind ('.'); |
4634 | 218 |
219 if (pos != NPOS) | |
220 varname = varname.substr (0, pos); | |
221 | |
222 size_t len = varname.length (); | |
223 for (size_t i = 0; i < len; i++) | |
224 { | |
225 char c = varname[i]; | |
226 if (! (isalnum (c) || c == '_')) | |
227 varname[i] = '_'; | |
228 } | |
229 | |
4867 | 230 if (is_keyword (varname) || ! isalpha (varname[0])) |
4634 | 231 varname.insert (0, "X"); |
232 | |
233 if (valid_identifier (varname)) | |
234 { | |
5275 | 235 octave_idx_type nr = 0; |
236 octave_idx_type nc = 0; | |
4634 | 237 |
238 int total_count = 0; | |
239 | |
240 get_lines_and_columns (is, filename, nr, nc); | |
241 | |
242 OCTAVE_QUIT; | |
243 | |
244 if (! error_state && nr > 0 && nc > 0) | |
245 { | |
246 Matrix tmp (nr, nc); | |
247 | |
248 if (nr < 1 || nc < 1) | |
249 is.clear (std::ios::badbit); | |
250 else | |
251 { | |
252 double d; | |
5275 | 253 for (octave_idx_type i = 0; i < nr; i++) |
4634 | 254 { |
255 std::string buf = get_mat_data_input_line (is); | |
256 | |
257 std::istringstream tmp_stream (buf); | |
258 | |
5275 | 259 for (octave_idx_type j = 0; j < nc; j++) |
4634 | 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 { | |
5775 | 303 // FIXME -- not sure this is best, but it works. |
4634 | 304 |
305 if (is.eof ()) | |
306 is.clear (); | |
307 | |
5275 | 308 octave_idx_type expected = nr * nc; |
4634 | 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 | |
5938 | 334 bool |
335 save_mat_ascii_data (std::ostream& os, const octave_value& val, | |
336 int precision) | |
337 { | |
338 bool success = true; | |
339 | |
340 if (val.is_complex_type ()) | |
341 warning ("save: omitting imaginary part for ASCII file"); | |
342 | |
343 Matrix m = val.matrix_value (true); | |
344 | |
345 if (error_state) | |
346 { | |
347 success = false; | |
348 | |
349 error_state = 0; | |
350 } | |
351 else | |
5951 | 352 { |
353 long old_precision = os.precision (); | |
354 | |
355 os.precision (precision); | |
5938 | 356 |
5951 | 357 std::ios::fmtflags oflags |
358 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific)); | |
359 | |
360 os << m; | |
361 | |
362 os.flags (oflags); | |
363 | |
364 os.precision (old_precision); | |
365 } | |
5938 | 366 |
367 return (os && success); | |
368 } | |
369 | |
4634 | 370 /* |
371 ;;; Local Variables: *** | |
372 ;;; mode: C++ *** | |
373 ;;; End: *** | |
374 */ | |
375 |