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