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