Mercurial > octave-nkf
annotate src/ls-mat-ascii.cc @ 8946:e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
author | Benjamin Lindner <lindnerb@users.sourceforge.net> |
---|---|
date | Tue, 10 Mar 2009 01:01:50 -0400 |
parents | eb63fbe60fab |
children | 985792c9e0da |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1996, 1997, 2003, 2004, 2005, 2006, 2007, 2008 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" |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
54 #include "ls-ascii-helper.h" |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
55 #include "ls-mat-ascii.h" |
4634 | 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') | |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
83 { |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
84 skip_until_newline (is, false); |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
85 break; |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
86 } |
4634 | 87 |
88 if (c == '%' || c == '#') | |
89 { | |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
90 skip_until_newline (is, false); |
4634 | 91 break; |
92 } | |
93 | |
94 if (! is.eof ()) | |
95 { | |
96 if (! have_data && c != ' ' && c != '\t') | |
97 have_data = true; | |
98 | |
99 retval += c; | |
100 } | |
101 } | |
102 } | |
103 while (! (have_data || is.eof ())); | |
104 | |
105 return retval; | |
106 } | |
107 | |
108 static void | |
5275 | 109 get_lines_and_columns (std::istream& is, const std::string& filename, octave_idx_type& nr, octave_idx_type& nc) |
4634 | 110 { |
111 std::streampos pos = is.tellg (); | |
112 | |
113 int file_line_number = 0; | |
114 | |
115 nr = 0; | |
116 nc = 0; | |
117 | |
118 while (is && ! error_state) | |
119 { | |
120 OCTAVE_QUIT; | |
121 | |
122 std::string buf = get_mat_data_input_line (is); | |
123 | |
124 file_line_number++; | |
125 | |
126 size_t beg = buf.find_first_not_of (", \t"); | |
127 | |
128 // If we see a CR as the last character in the buffer, we had a | |
129 // CRLF pair as the line separator. Any other CR in the text | |
130 // will not be considered as whitespace. | |
131 | |
8021 | 132 if (beg != std::string::npos && buf[beg] == '\r' && beg == buf.length () - 1) |
4634 | 133 { |
134 // We had a blank line ending with a CRLF. Handle it the | |
135 // same as an empty line. | |
8021 | 136 beg = std::string::npos; |
4634 | 137 } |
138 | |
5275 | 139 octave_idx_type tmp_nc = 0; |
4634 | 140 |
8021 | 141 while (beg != std::string::npos) |
4634 | 142 { |
143 tmp_nc++; | |
144 | |
145 size_t end = buf.find_first_of (", \t", beg); | |
146 | |
8021 | 147 if (end != std::string::npos) |
4634 | 148 { |
149 beg = buf.find_first_not_of (", \t", end); | |
150 | |
8021 | 151 if (beg == std::string::npos || (buf[beg] == '\r' && |
6856 | 152 beg == buf.length () - 1)) |
4634 | 153 { |
154 // We had a line with trailing spaces and | |
155 // ending with a CRLF, so this should look like EOL, | |
156 // not a new colum. | |
157 break; | |
158 } | |
159 } | |
160 else | |
161 break; | |
162 } | |
163 | |
164 if (tmp_nc > 0) | |
165 { | |
166 if (nc == 0) | |
167 { | |
168 nc = tmp_nc; | |
169 nr++; | |
170 } | |
171 else if (nc == tmp_nc) | |
172 nr++; | |
173 else | |
174 error ("load: %s: inconsistent number of columns near line %d", | |
175 filename.c_str (), file_line_number); | |
176 } | |
177 } | |
178 | |
179 if (nr == 0 || nc == 0) | |
180 error ("load: file `%s' seems to be empty!", filename.c_str ()); | |
181 | |
182 is.clear (); | |
4643 | 183 is.seekg (pos); |
4634 | 184 } |
185 | |
186 // Extract a matrix from a file of numbers only. | |
187 // | |
188 // Comments are not allowed. The file should only have numeric values. | |
189 // | |
190 // Reads the file twice. Once to find the number of rows and columns, | |
191 // and once to extract the matrix. | |
192 // | |
193 // FILENAME is used for error messages. | |
194 // | |
195 // This format provides no way to tag the data as global. | |
196 | |
197 std::string | |
198 read_mat_ascii_data (std::istream& is, const std::string& filename, | |
199 octave_value& tc) | |
200 { | |
201 std::string retval; | |
202 | |
203 std::string varname; | |
204 | |
205 size_t pos = filename.rfind ('/'); | |
206 | |
8021 | 207 if (pos != std::string::npos) |
4634 | 208 varname = filename.substr (pos+1); |
209 else | |
210 varname = filename; | |
211 | |
4867 | 212 pos = varname.rfind ('.'); |
4634 | 213 |
8021 | 214 if (pos != std::string::npos) |
4634 | 215 varname = varname.substr (0, pos); |
216 | |
217 size_t len = varname.length (); | |
218 for (size_t i = 0; i < len; i++) | |
219 { | |
220 char c = varname[i]; | |
221 if (! (isalnum (c) || c == '_')) | |
222 varname[i] = '_'; | |
223 } | |
224 | |
4867 | 225 if (is_keyword (varname) || ! isalpha (varname[0])) |
4634 | 226 varname.insert (0, "X"); |
227 | |
228 if (valid_identifier (varname)) | |
229 { | |
5275 | 230 octave_idx_type nr = 0; |
231 octave_idx_type nc = 0; | |
4634 | 232 |
233 int total_count = 0; | |
234 | |
235 get_lines_and_columns (is, filename, nr, nc); | |
236 | |
237 OCTAVE_QUIT; | |
238 | |
239 if (! error_state && nr > 0 && nc > 0) | |
240 { | |
241 Matrix tmp (nr, nc); | |
242 | |
243 if (nr < 1 || nc < 1) | |
244 is.clear (std::ios::badbit); | |
245 else | |
246 { | |
247 double d; | |
5275 | 248 for (octave_idx_type i = 0; i < nr; i++) |
4634 | 249 { |
250 std::string buf = get_mat_data_input_line (is); | |
251 | |
252 std::istringstream tmp_stream (buf); | |
253 | |
5275 | 254 for (octave_idx_type j = 0; j < nc; j++) |
4634 | 255 { |
256 OCTAVE_QUIT; | |
257 | |
258 d = octave_read_double (tmp_stream); | |
259 | |
260 if (tmp_stream || tmp_stream.eof ()) | |
261 { | |
262 tmp.elem (i, j) = d; | |
263 total_count++; | |
264 | |
265 // Skip whitespace and commas. | |
266 char c; | |
267 while (1) | |
268 { | |
269 tmp_stream >> c; | |
270 | |
271 if (! tmp_stream) | |
272 break; | |
273 | |
274 if (! (c == ' ' || c == '\t' || c == ',')) | |
275 { | |
276 tmp_stream.putback (c); | |
277 break; | |
278 } | |
279 } | |
280 | |
281 if (tmp_stream.eof ()) | |
282 break; | |
283 } | |
284 else | |
285 { | |
286 error ("load: failed to read matrix from file `%s'", | |
287 filename.c_str ()); | |
288 | |
289 return retval; | |
290 } | |
291 | |
292 } | |
293 } | |
294 } | |
295 | |
296 if (is || is.eof ()) | |
297 { | |
5775 | 298 // FIXME -- not sure this is best, but it works. |
4634 | 299 |
300 if (is.eof ()) | |
301 is.clear (); | |
302 | |
5275 | 303 octave_idx_type expected = nr * nc; |
4634 | 304 |
305 if (expected == total_count) | |
306 { | |
307 tc = tmp; | |
308 retval = varname; | |
309 } | |
310 else | |
311 error ("load: expected %d elements, found %d", | |
312 expected, total_count); | |
313 } | |
314 else | |
315 error ("load: failed to read matrix from file `%s'", | |
316 filename.c_str ()); | |
317 } | |
318 else | |
319 error ("load: unable to extract matrix size from file `%s'", | |
320 filename.c_str ()); | |
321 } | |
322 else | |
323 error ("load: unable to convert filename `%s' to valid identifier", | |
324 filename.c_str ()); | |
325 | |
326 return retval; | |
327 } | |
328 | |
5938 | 329 bool |
330 save_mat_ascii_data (std::ostream& os, const octave_value& val, | |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
331 int precision, bool tabs) |
5938 | 332 { |
333 bool success = true; | |
334 | |
335 if (val.is_complex_type ()) | |
336 warning ("save: omitting imaginary part for ASCII file"); | |
337 | |
338 Matrix m = val.matrix_value (true); | |
339 | |
340 if (error_state) | |
341 { | |
342 success = false; | |
343 | |
344 error_state = 0; | |
345 } | |
346 else | |
5951 | 347 { |
348 long old_precision = os.precision (); | |
349 | |
350 os.precision (precision); | |
5938 | 351 |
5951 | 352 std::ios::fmtflags oflags |
353 = os.flags (static_cast<std::ios::fmtflags> (std::ios::scientific)); | |
354 | |
8425
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
355 if (tabs) |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
356 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
357 for (octave_idx_type i = 0; i < m.rows (); i++) |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
358 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
359 for (octave_idx_type j = 0; j < m.cols (); j++) |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
360 { |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
361 // Omit leading tabs. |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
362 if (j != 0) os << '\t'; |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
363 octave_write_double (os, m (i, j)); |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
364 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
365 os << "\n"; |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
366 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
367 } |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
368 else |
2e777f5135a3
support -tabs option for save -ascii
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
369 os << m; |
5951 | 370 |
371 os.flags (oflags); | |
372 | |
373 os.precision (old_precision); | |
374 } | |
5938 | 375 |
376 return (os && success); | |
377 } | |
378 | |
4634 | 379 /* |
380 ;;; Local Variables: *** | |
381 ;;; mode: C++ *** | |
382 ;;; End: *** | |
383 */ | |
384 |