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