Mercurial > hg > octave-nkf
annotate src/ls-oct-ascii.cc @ 8087:7d19f4f70c61
ls-oct-ascii.{h,cc} (extract_keyword): accept \r as line ending character
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 08 Sep 2008 12:45:53 -0400 |
parents | 85184151822e |
children | eb63fbe60fab |
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 // Author: John W. Eaton. | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
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" | |
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 "unwind-prot.h" | |
60 #include "utils.h" | |
61 #include "variables.h" | |
62 #include "version.h" | |
63 #include "dMatrix.h" | |
64 | |
65 #include "ls-oct-ascii.h" | |
66 | |
67 // The number of decimal digits to use when writing ascii data. | |
5951 | 68 static int Vsave_precision = 16; |
4634 | 69 |
70 // Functions for reading ascii data. | |
71 | |
72 // Extract a KEYWORD and its value from stream IS, returning the | |
73 // associated value in a new string. | |
74 // | |
75 // Input should look something like: | |
76 // | |
77 // [%#][ \t]*keyword[ \t]*:[ \t]*string-value[ \t]*\n | |
78 | |
79 std::string | |
4687 | 80 extract_keyword (std::istream& is, const char *keyword, const bool next_only) |
4634 | 81 { |
82 std::string retval; | |
83 | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
84 int ch = is.peek (); |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
85 if (next_only && ch != '%' && ch != '#') |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
86 return retval; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
87 |
4634 | 88 char c; |
89 while (is.get (c)) | |
90 { | |
91 if (c == '%' || c == '#') | |
92 { | |
5765 | 93 std::ostringstream buf; |
4634 | 94 |
95 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) | |
96 ; // Skip whitespace and comment characters. | |
97 | |
98 if (isalpha (c)) | |
99 buf << c; | |
100 | |
101 while (is.get (c) && isalpha (c)) | |
102 buf << c; | |
103 | |
5765 | 104 std::string tmp = buf.str (); |
5679 | 105 bool match = (tmp.compare (0, strlen (keyword), keyword) == 0); |
4634 | 106 |
107 if (match) | |
108 { | |
5765 | 109 std::ostringstream value; |
4634 | 110 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
111 ; // Skip whitespace and the colon. | |
112 | |
8087
7d19f4f70c61
ls-oct-ascii.{h,cc} (extract_keyword): accept \r as line ending character
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
113 if (c != '\n' && c != '\r') |
4634 | 114 { |
115 value << c; | |
8087
7d19f4f70c61
ls-oct-ascii.{h,cc} (extract_keyword): accept \r as line ending character
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
116 while (is.get (c) && c != '\n' && c != '\r') |
4634 | 117 value << c; |
118 } | |
5765 | 119 |
120 retval = value.str (); | |
4634 | 121 break; |
122 } | |
4687 | 123 else if (next_only) |
124 break; | |
6202 | 125 else |
126 { | |
8087
7d19f4f70c61
ls-oct-ascii.{h,cc} (extract_keyword): accept \r as line ending character
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
127 while (is.get (c) && c != '\n' && c != '\r') |
6202 | 128 ; // Skip to end of line. |
129 } | |
4634 | 130 } |
131 } | |
132 | |
133 int len = retval.length (); | |
134 | |
135 if (len > 0) | |
136 { | |
137 while (len) | |
138 { | |
139 c = retval[len-1]; | |
140 | |
141 if (c == ' ' || c == '\t') | |
142 len--; | |
143 else | |
144 { | |
145 retval.resize (len); | |
146 break; | |
147 } | |
148 } | |
149 } | |
150 | |
151 return retval; | |
152 } | |
153 | |
154 // Extract one value (scalar, matrix, string, etc.) from stream IS and | |
155 // place it in TC, returning the name of the variable. If the value | |
156 // is tagged as global in the file, return TRUE in GLOBAL. | |
157 // | |
4687 | 158 // Each type supplies its own function to load the data, and so this |
159 // function is extensible. | |
160 // | |
4634 | 161 // FILENAME is used for error messages. |
162 // | |
163 // The data is expected to be in the following format: | |
164 // | |
165 // The input file must have a header followed by some data. | |
166 // | |
167 // All lines in the header must begin with a `#' character. | |
168 // | |
169 // The header must contain a list of keyword and value pairs with the | |
170 // keyword and value separated by a colon. | |
171 // | |
172 // Keywords must appear in the following order: | |
173 // | |
174 // # name: <name> | |
175 // # type: <type> | |
176 // # <info> | |
177 // | |
4687 | 178 // Where, for the built in types are: |
4634 | 179 // |
180 // <name> : a valid identifier | |
181 // | |
182 // <type> : <typename> | |
183 // | global <typename> | |
184 // | |
185 // <typename> : scalar | |
186 // | complex scalar | |
187 // | matrix | |
188 // | complex matrix | |
4687 | 189 // | bool |
190 // | bool matrix | |
4634 | 191 // | string |
192 // | range | |
193 // | |
194 // <info> : <matrix info> | |
195 // | <string info> | |
196 // | |
197 // <matrix info> : # rows: <integer> | |
198 // : # columns: <integer> | |
199 // | |
4687 | 200 // <string info> : # elements: <integer> |
201 // : # length: <integer> (once before each string) | |
4634 | 202 // |
4687 | 203 // For backward compatibility the type "string array" is treated as a |
204 // "string" type. Also "string" can have a single element with no elements | |
205 // line such that | |
206 // | |
207 // <string info> : # length: <integer> | |
4634 | 208 // |
209 // Formatted ASCII data follows the header. | |
210 // | |
211 // Example: | |
212 // | |
213 // # name: foo | |
214 // # type: matrix | |
215 // # rows: 2 | |
216 // # columns: 2 | |
217 // 2 4 | |
218 // 1 3 | |
219 // | |
220 // Example: | |
221 // | |
222 // # name: foo | |
4687 | 223 // # type: string |
4634 | 224 // # elements: 5 |
225 // # length: 4 | |
226 // this | |
227 // # length: 2 | |
228 // is | |
229 // # length: 1 | |
230 // a | |
231 // # length: 6 | |
232 // string | |
233 // # length: 5 | |
234 // array | |
235 // | |
5775 | 236 // FIXME -- this format is fairly rigid, and doesn't allow for |
4687 | 237 // arbitrary comments. Someone should fix that. It does allow arbitrary |
238 // types however. | |
4634 | 239 |
240 // Ugh. The signature of the compare method is not standard in older | |
241 // versions of the GNU libstdc++. Do this instead: | |
242 | |
243 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t) | |
244 | |
245 std::string | |
246 read_ascii_data (std::istream& is, const std::string& filename, bool& global, | |
5754 | 247 octave_value& tc, octave_idx_type count) |
4634 | 248 { |
249 // Read name for this entry or break on EOF. | |
250 | |
251 std::string name = extract_keyword (is, "name"); | |
252 | |
253 if (name.empty ()) | |
254 { | |
255 if (count == 0) | |
256 error ("load: empty name keyword or no data found in file `%s'", | |
257 filename.c_str ()); | |
258 | |
259 return std::string (); | |
260 } | |
261 | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
262 if (! (name == ".nargin." || name == ".nargout." |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
263 || name == CELL_ELT_TAG || valid_identifier (name))) |
4634 | 264 { |
265 error ("load: bogus identifier `%s' found in file `%s'", | |
266 name.c_str (), filename.c_str ()); | |
267 return std::string (); | |
268 } | |
269 | |
270 // Look for type keyword. | |
271 | |
272 std::string tag = extract_keyword (is, "type"); | |
273 | |
274 if (! tag.empty ()) | |
275 { | |
276 std::string typ; | |
277 size_t pos = tag.rfind (' '); | |
278 | |
8021 | 279 if (pos != std::string::npos) |
4634 | 280 { |
281 global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global"); | |
282 | |
283 typ = global ? tag.substr (7) : tag; | |
284 } | |
285 else | |
286 typ = tag; | |
287 | |
4687 | 288 // Special case for backward compatiablity. A small bit of cruft |
289 if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array")) | |
290 tc = octave_value (charMatrix (), true); | |
291 else | |
292 tc = octave_value_typeinfo::lookup_type (typ); | |
4634 | 293 |
4988 | 294 if (! tc.load_ascii (is)) |
295 error ("load: trouble reading ascii file `%s'", filename.c_str ()); | |
4634 | 296 } |
297 else | |
298 error ("load: failed to extract keyword specifying value type"); | |
299 | |
300 if (error_state) | |
301 { | |
302 error ("load: reading file %s", filename.c_str ()); | |
303 return std::string (); | |
304 } | |
305 | |
306 return name; | |
307 } | |
308 | |
309 // Save the data from TC along with the corresponding NAME, and global | |
310 // flag MARK_AS_GLOBAL on stream OS in the plain text format described | |
311 // above for load_ascii_data. If NAME is empty, the name: line is not | |
312 // generated. PRECISION specifies the number of decimal digits to print. | |
313 // | |
314 // Assumes ranges and strings cannot contain Inf or NaN values. | |
315 // | |
316 // Returns 1 for success and 0 for failure. | |
317 | |
5775 | 318 // FIXME -- should probably write the help string here too. |
4634 | 319 |
320 bool | |
321 save_ascii_data (std::ostream& os, const octave_value& val_arg, | |
6974 | 322 const std::string& name, bool mark_as_global, |
323 int precision) | |
4634 | 324 { |
325 bool success = true; | |
326 | |
327 if (! name.empty ()) | |
328 os << "# name: " << name << "\n"; | |
329 | |
330 octave_value val = val_arg; | |
331 | |
4687 | 332 if (mark_as_global) |
333 os << "# type: global " << val.type_name () << "\n"; | |
334 else | |
335 os << "# type: " << val.type_name() << "\n"; | |
4634 | 336 |
5951 | 337 if (! precision) |
338 precision = Vsave_precision; | |
339 | |
340 long old_precision = os.precision (); | |
341 os.precision (precision); | |
342 | |
6974 | 343 success = val.save_ascii (os); |
4634 | 344 |
345 os.precision (old_precision); | |
346 | |
347 return (os && success); | |
348 } | |
349 | |
350 bool | |
351 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, | |
352 const std::string& name) | |
353 { | |
6974 | 354 return save_ascii_data (os, t, name, false, 6); |
4634 | 355 } |
356 | |
357 // Maybe this should be a static function in tree-plot.cc? | |
358 | |
359 // If TC is matrix, save it on stream OS in a format useful for | |
360 // making a 3-dimensional plot with gnuplot. If PARAMETRIC is | |
361 // TRUE, assume a parametric 3-dimensional plot will be generated. | |
362 | |
363 bool | |
364 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) | |
365 { | |
366 bool fail = false; | |
367 | |
5275 | 368 octave_idx_type nr = tc.rows (); |
369 octave_idx_type nc = tc.columns (); | |
4634 | 370 |
371 if (tc.is_real_matrix ()) | |
372 { | |
373 os << "# 3D data...\n" | |
374 << "# type: matrix\n" | |
375 << "# total rows: " << nr << "\n" | |
376 << "# total columns: " << nc << "\n"; | |
377 | |
6171 | 378 long old_precision = os.precision (); |
6257 | 379 os.precision (6); |
6171 | 380 |
4634 | 381 if (parametric) |
382 { | |
5275 | 383 octave_idx_type extras = nc % 3; |
4634 | 384 if (extras) |
385 warning ("ignoring last %d columns", extras); | |
386 | |
387 Matrix tmp = tc.matrix_value (); | |
388 nr = tmp.rows (); | |
389 | |
5275 | 390 for (octave_idx_type i = 0; i < nc-extras; i += 3) |
4634 | 391 { |
392 os << tmp.extract (0, i, nr-1, i+2); | |
393 if (i+3 < nc-extras) | |
394 os << "\n"; | |
395 } | |
396 } | |
397 else | |
398 { | |
399 Matrix tmp = tc.matrix_value (); | |
400 nr = tmp.rows (); | |
401 | |
5275 | 402 for (octave_idx_type i = 0; i < nc; i++) |
4634 | 403 { |
404 os << tmp.extract (0, i, nr-1, i); | |
405 if (i+1 < nc) | |
406 os << "\n"; | |
407 } | |
408 } | |
6171 | 409 |
410 os.precision (old_precision); | |
4634 | 411 } |
412 else | |
413 { | |
414 ::error ("for now, I can only save real matrices in 3D format"); | |
415 fail = true; | |
416 } | |
417 | |
418 return (os && ! fail); | |
419 } | |
420 | |
5794 | 421 DEFUN (save_precision, args, nargout, |
422 "-*- texinfo -*-\n\ | |
423 @deftypefn {Built-in Function} {@var{val} =} save_precision ()\n\ | |
424 @deftypefnx {Built-in Function} {@var{old_val} =} save_precision (@var{new_val})\n\ | |
425 Query or set the internal variable that specifies the number of\n\ | |
426 digits to keep when saving data in text format.\n\ | |
427 @end deftypefn") | |
4634 | 428 { |
5794 | 429 return SET_INTERNAL_VARIABLE_WITH_LIMITS (save_precision, -1, INT_MAX); |
4634 | 430 } |
431 | |
432 /* | |
433 ;;; Local Variables: *** | |
434 ;;; mode: C++ *** | |
435 ;;; End: *** | |
436 */ | |
437 |