Mercurial > octave
annotate libinterp/corefcn/ls-oct-text.cc @ 23556:31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
* ls-oct-text.cc (Vsave_precision): Change default to 17 digits.
* ls-oct-text.cc (Fsave_precision): Add explanation of suitable values to docstring.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 07 Jun 2017 12:29:23 -0700 |
parents | a41fdb801db6 |
children | 194eb4bd202b |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
23219
3ac9f9ecfae5
maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents:
23083
diff
changeset
|
3 Copyright (C) 1996-2017 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 | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
8 under the terms of the GNU General Public License as published by |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
9 the Free Software Foundation; either version 3 of the License, or |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
10 (at your option) any later version. |
4634 | 11 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
12 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
13 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
15 GNU General Public License for more details. |
4634 | 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 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
25 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
26 # include "config.h" |
4634 | 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" | |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21021
diff
changeset
|
52 #include "errwarn.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" |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
55 #include "ls-oct-text.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
56 #include "ovl.h" |
4634 | 57 #include "oct-map.h" |
58 #include "ov-cell.h" | |
59 #include "pager.h" | |
60 #include "pt-exp.h" | |
61 #include "unwind-prot.h" | |
62 #include "utils.h" | |
63 #include "variables.h" | |
64 #include "version.h" | |
65 #include "dMatrix.h" | |
66 | |
23556
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
67 // The number of decimal digits to use when writing ASCII data. |
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
68 // 17 is the minimum necessary for lossless save/restore of IEEE-754 doubles. |
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
69 static int Vsave_precision = 17; |
4634 | 70 |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
71 // Functions for reading octave format text data. |
4634 | 72 |
73 // Extract a KEYWORD and its value from stream IS, returning the | |
74 // associated value in a new string. | |
75 // | |
76 // Input should look something like: | |
77 // | |
78 // [%#][ \t]*keyword[ \t]*:[ \t]*string-value[ \t]*\n | |
79 | |
80 std::string | |
4687 | 81 extract_keyword (std::istream& is, const char *keyword, const bool next_only) |
4634 | 82 { |
83 std::string retval; | |
84 | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
85 int ch = is.peek (); |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
86 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
|
87 return retval; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
88 |
4634 | 89 char c; |
90 while (is.get (c)) | |
91 { | |
92 if (c == '%' || c == '#') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
93 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
94 std::ostringstream buf; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
95 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
96 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
97 ; // Skip whitespace and comment characters. |
4634 | 98 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
99 if (isalpha (c)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
100 buf << c; |
4634 | 101 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
102 while (is.get (c) && isalpha (c)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
103 buf << c; |
4634 | 104 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
105 std::string tmp = buf.str (); |
21021
a5b99b09f8fd
maint: Use comparison operators rather than compare() for strings.
Rik <rik@octave.org>
parents:
21017
diff
changeset
|
106 bool match = (tmp.substr (0, strlen (keyword)) == keyword); |
4634 | 107 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
108 if (match) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
109 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
110 std::ostringstream value; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
111 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
112 ; // Skip whitespace and the colon. |
4634 | 113 |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
114 is.putback (c); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
115 retval = read_until_newline (is, false); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
116 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
117 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
118 else if (next_only) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
119 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
120 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
121 skip_until_newline (is, false); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
122 } |
4634 | 123 } |
124 | |
125 int len = retval.length (); | |
126 | |
127 if (len > 0) | |
128 { | |
129 while (len) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
130 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
131 c = retval[len-1]; |
4634 | 132 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
133 if (c == ' ' || c == '\t') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
134 len--; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
135 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
136 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
137 retval.resize (len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
138 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
139 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
140 } |
4634 | 141 } |
142 | |
143 return retval; | |
144 } | |
145 | |
146 // Extract one value (scalar, matrix, string, etc.) from stream IS and | |
147 // place it in TC, returning the name of the variable. If the value | |
148 // is tagged as global in the file, return TRUE in GLOBAL. | |
149 // | |
4687 | 150 // Each type supplies its own function to load the data, and so this |
151 // function is extensible. | |
152 // | |
4634 | 153 // FILENAME is used for error messages. |
154 // | |
155 // The data is expected to be in the following format: | |
156 // | |
157 // The input file must have a header followed by some data. | |
158 // | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
159 // All lines in the header must begin with a '#' character. |
4634 | 160 // |
161 // The header must contain a list of keyword and value pairs with the | |
162 // keyword and value separated by a colon. | |
163 // | |
164 // Keywords must appear in the following order: | |
165 // | |
166 // # name: <name> | |
167 // # type: <type> | |
168 // # <info> | |
169 // | |
4687 | 170 // Where, for the built in types are: |
4634 | 171 // |
172 // <name> : a valid identifier | |
173 // | |
174 // <type> : <typename> | |
175 // | global <typename> | |
176 // | |
177 // <typename> : scalar | |
178 // | complex scalar | |
179 // | matrix | |
180 // | complex matrix | |
4687 | 181 // | bool |
182 // | bool matrix | |
4634 | 183 // | string |
184 // | range | |
185 // | |
186 // <info> : <matrix info> | |
187 // | <string info> | |
188 // | |
189 // <matrix info> : # rows: <integer> | |
190 // : # columns: <integer> | |
191 // | |
4687 | 192 // <string info> : # elements: <integer> |
193 // : # length: <integer> (once before each string) | |
4634 | 194 // |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
195 // For backward compatibility the type "string array" is treated as a |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
196 // "string" type. Also "string" can have a single element with no elements |
4687 | 197 // line such that |
198 // | |
199 // <string info> : # length: <integer> | |
4634 | 200 // |
201 // Formatted ASCII data follows the header. | |
202 // | |
203 // Example: | |
204 // | |
205 // # name: foo | |
206 // # type: matrix | |
207 // # rows: 2 | |
208 // # columns: 2 | |
209 // 2 4 | |
210 // 1 3 | |
211 // | |
212 // Example: | |
213 // | |
214 // # name: foo | |
4687 | 215 // # type: string |
4634 | 216 // # elements: 5 |
217 // # length: 4 | |
218 // this | |
219 // # length: 2 | |
220 // is | |
221 // # length: 1 | |
222 // a | |
223 // # length: 6 | |
224 // string | |
225 // # length: 5 | |
226 // array | |
227 // | |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
228 // FIXME: This format is fairly rigid, and doesn't allow for arbitrary comments. |
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
229 // Someone should fix that. It does allow arbitrary types however. |
4634 | 230 |
231 // Ugh. The signature of the compare method is not standard in older | |
232 // versions of the GNU libstdc++. Do this instead: | |
233 | |
234 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t) | |
235 | |
236 std::string | |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
237 read_text_data (std::istream& is, const std::string& filename, bool& global, |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
238 octave_value& tc, octave_idx_type count) |
4634 | 239 { |
240 // Read name for this entry or break on EOF. | |
241 | |
242 std::string name = extract_keyword (is, "name"); | |
243 | |
244 if (name.empty ()) | |
245 { | |
246 if (count == 0) | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
247 error ("load: empty name keyword or no data found in file '%s'", |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
248 filename.c_str ()); |
4634 | 249 |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
21014
diff
changeset
|
250 return ""; |
4634 | 251 } |
252 | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
253 if (! (name == ".nargin." || name == ".nargout." |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
254 || name == CELL_ELT_TAG || valid_identifier (name))) |
20835
14cd86258b3d
use 'invalid' instead of 'bogus' in source files
John W. Eaton <jwe@octave.org>
parents:
20831
diff
changeset
|
255 error ("load: invalid identifier '%s' found in file '%s'", |
20741
a5ab31b52ae8
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20447
diff
changeset
|
256 name.c_str (), filename.c_str ()); |
4634 | 257 |
258 // Look for type keyword. | |
259 | |
260 std::string tag = extract_keyword (is, "type"); | |
261 | |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
262 if (tag.empty ()) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
263 error ("load: failed to extract keyword specifying value type"); |
4634 | 264 |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
265 std::string typ; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
266 size_t pos = tag.rfind (' '); |
4634 | 267 |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
268 if (pos != std::string::npos) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
269 { |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
270 global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global"); |
4634 | 271 |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
272 typ = (global ? tag.substr (7) : tag); |
4634 | 273 } |
274 else | |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
275 typ = tag; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
276 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
277 // Special case for backward compatibility. A small bit of cruft |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
278 if (SUBSTRING_COMPARE_EQ (typ, 0, 12, "string array")) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
279 tc = charMatrix (); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
280 else |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
281 tc = octave_value_typeinfo::lookup_type (typ); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
282 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
283 if (! tc.load_ascii (is)) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
284 error ("load: trouble reading ascii file '%s'", filename.c_str ()); |
4634 | 285 |
286 return name; | |
287 } | |
288 | |
289 // Save the data from TC along with the corresponding NAME, and global | |
290 // flag MARK_AS_GLOBAL on stream OS in the plain text format described | |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
291 // above for load_text_data. If NAME is empty, the name: line is not |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
292 // generated. PRECISION specifies the number of decimal digits to print. |
4634 | 293 // |
294 // Assumes ranges and strings cannot contain Inf or NaN values. | |
295 // | |
296 // Returns 1 for success and 0 for failure. | |
297 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
298 // FIXME: should probably write the help string here too. |
4634 | 299 |
300 bool | |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
301 save_text_data (std::ostream& os, const octave_value& val_arg, |
23470
a41fdb801db6
use mark_global and mark_persistent consistently
John W. Eaton <jwe@octave.org>
parents:
23450
diff
changeset
|
302 const std::string& name, bool mark_global, |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
303 int precision) |
4634 | 304 { |
305 bool success = true; | |
306 | |
307 if (! name.empty ()) | |
308 os << "# name: " << name << "\n"; | |
309 | |
310 octave_value val = val_arg; | |
311 | |
23470
a41fdb801db6
use mark_global and mark_persistent consistently
John W. Eaton <jwe@octave.org>
parents:
23450
diff
changeset
|
312 if (mark_global) |
4687 | 313 os << "# type: global " << val.type_name () << "\n"; |
314 else | |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
315 os << "# type: " << val.type_name () << "\n"; |
4634 | 316 |
5951 | 317 if (! precision) |
318 precision = Vsave_precision; | |
319 | |
320 long old_precision = os.precision (); | |
321 os.precision (precision); | |
322 | |
6974 | 323 success = val.save_ascii (os); |
4634 | 324 |
10417
ad29dbbc3f70
save: make that two newline characters
John W. Eaton <jwe@octave.org>
parents:
10416
diff
changeset
|
325 // Insert an extra pair of newline characters after the data so that |
ad29dbbc3f70
save: make that two newline characters
John W. Eaton <jwe@octave.org>
parents:
10416
diff
changeset
|
326 // multiple data elements may be handled separately by gnuplot (see |
ad29dbbc3f70
save: make that two newline characters
John W. Eaton <jwe@octave.org>
parents:
10416
diff
changeset
|
327 // the description of the index qualifier for the plot command in the |
ad29dbbc3f70
save: make that two newline characters
John W. Eaton <jwe@octave.org>
parents:
10416
diff
changeset
|
328 // gnuplot documentation). |
ad29dbbc3f70
save: make that two newline characters
John W. Eaton <jwe@octave.org>
parents:
10416
diff
changeset
|
329 os << "\n\n"; |
10416
f06ede00fd8f
save: separate variables by blank line in Octave text format files
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
330 |
4634 | 331 os.precision (old_precision); |
332 | |
333 return (os && success); | |
334 } | |
335 | |
336 bool | |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
337 save_text_data_for_plotting (std::ostream& os, const octave_value& t, |
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
338 const std::string& name) |
4634 | 339 { |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
340 return save_text_data (os, t, name, false, 6); |
4634 | 341 } |
342 | |
343 // Maybe this should be a static function in tree-plot.cc? | |
344 | |
345 // If TC is matrix, save it on stream OS in a format useful for | |
21014
b9d4c3615e89
maint: Capitalize D for dimension in messages and comments.
Rik <rik@octave.org>
parents:
20981
diff
changeset
|
346 // making a 3-D plot with gnuplot. If PARAMETRIC is TRUE, |
b9d4c3615e89
maint: Capitalize D for dimension in messages and comments.
Rik <rik@octave.org>
parents:
20981
diff
changeset
|
347 // assume a parametric 3-D plot will be generated. |
4634 | 348 |
349 bool | |
350 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) | |
351 { | |
352 bool fail = false; | |
353 | |
5275 | 354 octave_idx_type nr = tc.rows (); |
355 octave_idx_type nc = tc.columns (); | |
4634 | 356 |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
357 if (! tc.is_real_matrix ()) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
358 error ("for now, I can only save real matrices in 3-D format"); |
4634 | 359 |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
360 os << "# 3-D data...\n" |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
361 << "# type: matrix\n" |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
362 << "# total rows: " << nr << "\n" |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
363 << "# total columns: " << nc << "\n"; |
6171 | 364 |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
365 long old_precision = os.precision (); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
366 os.precision (6); |
4634 | 367 |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
368 if (parametric) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
369 { |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
370 octave_idx_type extras = nc % 3; |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
371 if (extras) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
372 warning ("ignoring last %d columns", extras); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
373 |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
374 Matrix tmp = tc.matrix_value (); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
375 nr = tmp.rows (); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
376 |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
377 for (octave_idx_type i = 0; i < nc-extras; i += 3) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
378 { |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
379 os << tmp.extract (0, i, nr-1, i+2); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
380 if (i+3 < nc-extras) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
381 os << "\n"; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
382 } |
4634 | 383 } |
384 else | |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
385 { |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
386 Matrix tmp = tc.matrix_value (); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
387 nr = tmp.rows (); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
388 |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
389 for (octave_idx_type i = 0; i < nc; i++) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
390 { |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
391 os << tmp.extract (0, i, nr-1, i); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
392 if (i+1 < nc) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
393 os << "\n"; |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
394 } |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
395 } |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
396 |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
397 os.precision (old_precision); |
4634 | 398 |
399 return (os && ! fail); | |
400 } | |
401 | |
5794 | 402 DEFUN (save_precision, args, nargout, |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
403 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
404 @deftypefn {} {@var{val} =} save_precision () |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
405 @deftypefnx {} {@var{old_val} =} save_precision (@var{new_val}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
406 @deftypefnx {} {} save_precision (@var{new_val}, "local") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
407 Query or set the internal variable that specifies the number of digits to |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
408 keep when saving data in text format. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
409 |
23556
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
410 The default value is 17 which is the minimum necessary for the lossless saving |
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
411 and restoring of IEEE-754 double values; For IEEE-754 single values the minimum |
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
412 value is 9. If file size is a concern, it is probably better to choose a |
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
413 binary format for saving data rather than to reduce the precision of the saved |
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
414 values. |
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
415 |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
416 When called from inside a function with the @qcode{"local"} option, the |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
417 variable is changed locally for the function and any subroutines it calls. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
418 The original variable value is restored when exiting the function. |
23556
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
419 |
31b1ef1ee585
Fix Octave's -text format for saving data to make it lossless for IEEE-754 doubles.
Rik <rik@octave.org>
parents:
23470
diff
changeset
|
420 @seealso{save_default_options} |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
421 @end deftypefn */) |
4634 | 422 { |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
423 return SET_INTERNAL_VARIABLE_WITH_LIMITS (save_precision, -1, |
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
424 std::numeric_limits<int>::max ()); |
4634 | 425 } |