Mercurial > octave
annotate libinterp/corefcn/ls-oct-text.cc @ 21021:a5b99b09f8fd
maint: Use comparison operators rather than compare() for strings.
* graphics.cc, graphics.in.h, load-path.cc, ls-oct-text.cc, ls-oct-text.h,
__init_fltk__.cc, oct-parse.in.yy, file-ops.cc:
Use comparison operators rather than compare() for strings.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 31 Dec 2015 13:39:25 -0800 |
parents | 93748bcaec17 |
children | e39e05d90788 |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
3 Copyright (C) 1996-2015 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" | |
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 | |
67 // The number of decimal digits to use when writing ascii data. | |
5951 | 68 static int Vsave_precision = 16; |
4634 | 69 |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
70 // Functions for reading octave format text data. |
4634 | 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 == '#') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
92 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
93 std::ostringstream buf; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
94 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
95 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
|
96 ; // Skip whitespace and comment characters. |
4634 | 97 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
98 if (isalpha (c)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
99 buf << c; |
4634 | 100 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
101 while (is.get (c) && isalpha (c)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
102 buf << c; |
4634 | 103 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
104 std::string tmp = buf.str (); |
21021
a5b99b09f8fd
maint: Use comparison operators rather than compare() for strings.
Rik <rik@octave.org>
parents:
21017
diff
changeset
|
105 bool match = (tmp.substr (0, strlen (keyword)) == keyword); |
4634 | 106 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
107 if (match) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
108 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
109 std::ostringstream value; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
110 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
111 ; // Skip whitespace and the colon. |
4634 | 112 |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14846
diff
changeset
|
113 is.putback (c); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
114 retval = read_until_newline (is, false); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
115 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
116 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
117 else if (next_only) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
118 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
119 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
120 skip_until_newline (is, false); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
121 } |
4634 | 122 } |
123 | |
124 int len = retval.length (); | |
125 | |
126 if (len > 0) | |
127 { | |
128 while (len) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
129 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
130 c = retval[len-1]; |
4634 | 131 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
132 if (c == ' ' || c == '\t') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
133 len--; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
134 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
135 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
136 retval.resize (len); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
137 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
138 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
139 } |
4634 | 140 } |
141 | |
142 return retval; | |
143 } | |
144 | |
145 // Extract one value (scalar, matrix, string, etc.) from stream IS and | |
146 // place it in TC, returning the name of the variable. If the value | |
147 // is tagged as global in the file, return TRUE in GLOBAL. | |
148 // | |
4687 | 149 // Each type supplies its own function to load the data, and so this |
150 // function is extensible. | |
151 // | |
4634 | 152 // FILENAME is used for error messages. |
153 // | |
154 // The data is expected to be in the following format: | |
155 // | |
156 // The input file must have a header followed by some data. | |
157 // | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
158 // All lines in the header must begin with a '#' character. |
4634 | 159 // |
160 // The header must contain a list of keyword and value pairs with the | |
161 // keyword and value separated by a colon. | |
162 // | |
163 // Keywords must appear in the following order: | |
164 // | |
165 // # name: <name> | |
166 // # type: <type> | |
167 // # <info> | |
168 // | |
4687 | 169 // Where, for the built in types are: |
4634 | 170 // |
171 // <name> : a valid identifier | |
172 // | |
173 // <type> : <typename> | |
174 // | global <typename> | |
175 // | |
176 // <typename> : scalar | |
177 // | complex scalar | |
178 // | matrix | |
179 // | complex matrix | |
4687 | 180 // | bool |
181 // | bool matrix | |
4634 | 182 // | string |
183 // | range | |
184 // | |
185 // <info> : <matrix info> | |
186 // | <string info> | |
187 // | |
188 // <matrix info> : # rows: <integer> | |
189 // : # columns: <integer> | |
190 // | |
4687 | 191 // <string info> : # elements: <integer> |
192 // : # length: <integer> (once before each string) | |
4634 | 193 // |
4687 | 194 // For backward compatibility the type "string array" is treated as a |
195 // "string" type. Also "string" can have a single element with no elements | |
196 // line such that | |
197 // | |
198 // <string info> : # length: <integer> | |
4634 | 199 // |
200 // Formatted ASCII data follows the header. | |
201 // | |
202 // Example: | |
203 // | |
204 // # name: foo | |
205 // # type: matrix | |
206 // # rows: 2 | |
207 // # columns: 2 | |
208 // 2 4 | |
209 // 1 3 | |
210 // | |
211 // Example: | |
212 // | |
213 // # name: foo | |
4687 | 214 // # type: string |
4634 | 215 // # elements: 5 |
216 // # length: 4 | |
217 // this | |
218 // # length: 2 | |
219 // is | |
220 // # length: 1 | |
221 // a | |
222 // # length: 6 | |
223 // string | |
224 // # length: 5 | |
225 // array | |
226 // | |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
227 // 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
|
228 // Someone should fix that. It does allow arbitrary types however. |
4634 | 229 |
230 // Ugh. The signature of the compare method is not standard in older | |
231 // versions of the GNU libstdc++. Do this instead: | |
232 | |
233 #define SUBSTRING_COMPARE_EQ(s, pos, n, t) (s.substr (pos, n) == t) | |
234 | |
235 std::string | |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
236 read_text_data (std::istream& is, const std::string& filename, bool& global, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
237 octave_value& tc, octave_idx_type count) |
4634 | 238 { |
239 // Read name for this entry or break on EOF. | |
240 | |
241 std::string name = extract_keyword (is, "name"); | |
242 | |
243 if (name.empty ()) | |
244 { | |
245 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
|
246 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
|
247 filename.c_str ()); |
4634 | 248 |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
21014
diff
changeset
|
249 return ""; |
4634 | 250 } |
251 | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
252 if (! (name == ".nargin." || name == ".nargout." |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
253 || 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
|
254 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
|
255 name.c_str (), filename.c_str ()); |
4634 | 256 |
257 // Look for type keyword. | |
258 | |
259 std::string tag = extract_keyword (is, "type"); | |
260 | |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
261 if (tag.empty ()) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
262 error ("load: failed to extract keyword specifying value type"); |
4634 | 263 |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
264 std::string typ; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
265 size_t pos = tag.rfind (' '); |
4634 | 266 |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
267 if (pos != std::string::npos) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
268 { |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
269 global = SUBSTRING_COMPARE_EQ (tag, 0, 6, "global"); |
4634 | 270 |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
271 typ = global ? tag.substr (7) : tag; |
4634 | 272 } |
273 else | |
20981
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
274 typ = tag; |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
275 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
276 // 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
|
277 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
|
278 tc = charMatrix (); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
279 else |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
280 tc = octave_value_typeinfo::lookup_type (typ); |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
281 |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
282 if (! tc.load_ascii (is)) |
c11cea70b638
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20962
diff
changeset
|
283 error ("load: trouble reading ascii file '%s'", filename.c_str ()); |
4634 | 284 |
285 return name; | |
286 } | |
287 | |
288 // Save the data from TC along with the corresponding NAME, and global | |
289 // 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
|
290 // 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
|
291 // generated. PRECISION specifies the number of decimal digits to print. |
4634 | 292 // |
293 // Assumes ranges and strings cannot contain Inf or NaN values. | |
294 // | |
295 // Returns 1 for success and 0 for failure. | |
296 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
297 // FIXME: should probably write the help string here too. |
4634 | 298 |
299 bool | |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
300 save_text_data (std::ostream& os, const octave_value& val_arg, |
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
301 const std::string& name, bool mark_as_global, |
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
302 int precision) |
4634 | 303 { |
304 bool success = true; | |
305 | |
306 if (! name.empty ()) | |
307 os << "# name: " << name << "\n"; | |
308 | |
309 octave_value val = val_arg; | |
310 | |
4687 | 311 if (mark_as_global) |
312 os << "# type: global " << val.type_name () << "\n"; | |
313 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
|
314 os << "# type: " << val.type_name () << "\n"; |
4634 | 315 |
5951 | 316 if (! precision) |
317 precision = Vsave_precision; | |
318 | |
319 long old_precision = os.precision (); | |
320 os.precision (precision); | |
321 | |
6974 | 322 success = val.save_ascii (os); |
4634 | 323 |
10417
ad29dbbc3f70
save: make that two newline characters
John W. Eaton <jwe@octave.org>
parents:
10416
diff
changeset
|
324 // 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
|
325 // 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
|
326 // 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
|
327 // gnuplot documentation). |
ad29dbbc3f70
save: make that two newline characters
John W. Eaton <jwe@octave.org>
parents:
10416
diff
changeset
|
328 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
|
329 |
4634 | 330 os.precision (old_precision); |
331 | |
332 return (os && success); | |
333 } | |
334 | |
335 bool | |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
336 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
|
337 const std::string& name) |
4634 | 338 { |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
339 return save_text_data (os, t, name, false, 6); |
4634 | 340 } |
341 | |
342 // Maybe this should be a static function in tree-plot.cc? | |
343 | |
344 // 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
|
345 // 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
|
346 // assume a parametric 3-D plot will be generated. |
4634 | 347 |
348 bool | |
349 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) | |
350 { | |
351 bool fail = false; | |
352 | |
5275 | 353 octave_idx_type nr = tc.rows (); |
354 octave_idx_type nc = tc.columns (); | |
4634 | 355 |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
356 if (! tc.is_real_matrix ()) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
357 error ("for now, I can only save real matrices in 3-D format"); |
4634 | 358 |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
359 os << "# 3-D data...\n" |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
360 << "# type: matrix\n" |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
361 << "# total rows: " << nr << "\n" |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
362 << "# total columns: " << nc << "\n"; |
6171 | 363 |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
364 long old_precision = os.precision (); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
365 os.precision (6); |
4634 | 366 |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
367 if (parametric) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
368 { |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
369 octave_idx_type extras = nc % 3; |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
370 if (extras) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
371 warning ("ignoring last %d columns", extras); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
372 |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
373 Matrix tmp = tc.matrix_value (); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
374 nr = tmp.rows (); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
375 |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
376 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
|
377 { |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
378 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
|
379 if (i+3 < nc-extras) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
380 os << "\n"; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
381 } |
4634 | 382 } |
383 else | |
20962
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
384 { |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
385 Matrix tmp = tc.matrix_value (); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
386 nr = tmp.rows (); |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
387 |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
388 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
|
389 { |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
390 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
|
391 if (i+1 < nc) |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
392 os << "\n"; |
3aa293be0e8d
maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents:
20940
diff
changeset
|
393 } |
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 os.precision (old_precision); |
4634 | 397 |
398 return (os && ! fail); | |
399 } | |
400 | |
5794 | 401 DEFUN (save_precision, args, nargout, |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
402 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20837
diff
changeset
|
403 @deftypefn {} {@var{val} =} save_precision ()\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20837
diff
changeset
|
404 @deftypefnx {} {@var{old_val} =} save_precision (@var{new_val})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20837
diff
changeset
|
405 @deftypefnx {} {} save_precision (@var{new_val}, \"local\")\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
406 Query or set the internal variable that specifies the number of digits to\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
407 keep when saving data in text format.\n\ |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
408 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17199
diff
changeset
|
409 When called from inside a function with the @qcode{\"local\"} option, the\n\ |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17199
diff
changeset
|
410 variable is changed locally for the function and any subroutines it calls.\n\ |
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17199
diff
changeset
|
411 The original variable value is restored when exiting the function.\n\ |
5794 | 412 @end deftypefn") |
4634 | 413 { |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
414 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
|
415 std::numeric_limits<int>::max ()); |
4634 | 416 } |