Mercurial > jwe > octave
annotate libinterp/corefcn/ls-oct-text.h @ 28166:ef349f5c320b
Load structures with arbitrary string fieldnames (bug #50831, bug #46645).
* ls-oct-text.h, ls-oct-text.cc (read_text_data): Consider additional optional
bool argument to skip validation of input name.
* ov-struct.cc (octave_struct::load_ascii, octave_scalar_struct::load_ascii):
Call read_text_data with additional argument (skip_validation), set to true.
* test/bug-50831/bug-50831.tst, test/bug-50831/module.mk: New test files.
author | Olaf Till <i7tiol@t-online.de> |
---|---|
date | Wed, 19 Apr 2017 15:06:31 +0200 |
parents | bd51beb6205e |
children | 0a5b15007766 |
rev | line source |
---|---|
27928
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
2 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
3 // Copyright (C) 2003-2020 The Octave Project Developers |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27924
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
4634 | 25 |
20791
f7084eae3318
maint: Use Octave coding conventions for #if statements.
Rik <rik@octave.org>
parents:
20447
diff
changeset
|
26 #if ! defined (octave_ls_oct_text_h) |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20232
diff
changeset
|
27 #define octave_ls_oct_text_h 1 |
4634 | 28 |
21244
1473547f50f5
include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
29 #include "octave-config.h" |
1473547f50f5
include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents:
21200
diff
changeset
|
30 |
23455
73ff72d3d603
maint: Eliminate <cfloat.h> header from libinterp files
Rik <rik@octave.org>
parents:
23442
diff
changeset
|
31 #include <limits> |
5765 | 32 #include <sstream> |
5099 | 33 #include <string> |
34 | |
35 #include "str-vec.h" | |
36 | |
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
|
37 #include "ls-ascii-helper.h" |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
38 |
23442
53f5f8231c37
allow most header files to be compiled separately
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
39 class octave_value; |
53f5f8231c37
allow most header files to be compiled separately
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
40 |
4687 | 41 // Flag for cell elements |
42 #define CELL_ELT_TAG "<cell-element>" | |
43 | |
44 // Used when converting Inf to something that gnuplot can read. | |
45 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21244
diff
changeset
|
46 #if ! defined (OCT_RBV) |
21200
fcac5dbbf9ed
maint: Indent #ifdef blocks in libinterp.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
47 # define OCT_RBV (std::numeric_limits<double>::max () / 100.0) |
4687 | 48 #endif |
49 | |
6109 | 50 extern OCTINTERP_API std::string |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
51 extract_keyword (std::istream& is, const char *keyword, |
10313 | 52 const bool next_only = false); |
4687 | 53 |
6109 | 54 extern OCTINTERP_API std::string |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20232
diff
changeset
|
55 read_text_data (std::istream& is, const std::string& filename, bool& global, |
28166
ef349f5c320b
Load structures with arbitrary string fieldnames (bug #50831, bug #46645).
Olaf Till <i7tiol@t-online.de>
parents:
27928
diff
changeset
|
56 octave_value& tc, octave_idx_type count, |
ef349f5c320b
Load structures with arbitrary string fieldnames (bug #50831, bug #46645).
Olaf Till <i7tiol@t-online.de>
parents:
27928
diff
changeset
|
57 const bool do_name_validation = true); |
4634 | 58 |
6109 | 59 extern OCTINTERP_API bool |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20232
diff
changeset
|
60 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:
23455
diff
changeset
|
61 const std::string& name, bool mark_global, int precision); |
4634 | 62 |
6109 | 63 extern OCTINTERP_API bool |
20447
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20232
diff
changeset
|
64 save_text_data_for_plotting (std::ostream& os, const octave_value& t, |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
65 const std::string& name); |
4634 | 66 |
6109 | 67 extern OCTINTERP_API bool |
5958 | 68 save_three_d (std::ostream& os, const octave_value& t, |
10313 | 69 bool parametric = false); |
5958 | 70 |
5275 | 71 // Match KEYWORD on stream IS, placing the associated value in VALUE, |
72 // returning TRUE if successful and FALSE otherwise. | |
73 // | |
74 // Input should look something like: | |
75 // | |
76 // [%#][ \t]*keyword[ \t]*int-value.*\n | |
77 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21021
diff
changeset
|
78 template <typename T> |
5275 | 79 bool |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
80 extract_keyword (std::istream& is, const char *keyword, T& value, |
10313 | 81 const bool next_only = false) |
5275 | 82 { |
83 bool status = false; | |
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
|
84 value = T (); |
5275 | 85 |
86 char c; | |
87 while (is.get (c)) | |
88 { | |
89 if (c == '%' || c == '#') | |
10313 | 90 { |
91 std::ostringstream buf; | |
5275 | 92 |
10313 | 93 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
94 ; // Skip whitespace and comment characters. | |
5275 | 95 |
10313 | 96 if (isalpha (c)) |
97 buf << c; | |
5275 | 98 |
10313 | 99 while (is.get (c) && isalpha (c)) |
100 buf << c; | |
5275 | 101 |
10313 | 102 std::string tmp = buf.str (); |
21021
a5b99b09f8fd
maint: Use comparison operators rather than compare() for strings.
Rik <rik@octave.org>
parents:
20791
diff
changeset
|
103 bool match = (tmp.substr (0, strlen (keyword)) == keyword); |
5275 | 104 |
10313 | 105 if (match) |
106 { | |
107 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) | |
108 ; // Skip whitespace and the colon. | |
5275 | 109 |
10313 | 110 is.putback (c); |
111 if (c != '\n' && c != '\r') | |
112 is >> value; | |
113 if (is) | |
114 status = true; | |
115 skip_until_newline (is, false); | |
116 break; | |
117 } | |
118 else if (next_only) | |
119 break; | |
120 } | |
5275 | 121 } |
122 return status; | |
123 } | |
124 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21021
diff
changeset
|
125 template <typename T> |
9852
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
126 bool |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
127 extract_keyword (std::istream& is, const std::string& kw, T& value, |
10313 | 128 const bool next_only = false) |
9852
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
129 { |
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
130 return extract_keyword (is, kw.c_str (), value, next_only); |
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
131 } |
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
132 |
5275 | 133 // Match one of the elements in KEYWORDS on stream IS, placing the |
134 // matched keyword in KW and the associated value in VALUE, | |
135 // returning TRUE if successful and FALSE otherwise. | |
136 // | |
137 // Input should look something like: | |
138 // | |
139 // [%#][ \t]*keyword[ \t]*int-value.*\n | |
140 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21021
diff
changeset
|
141 template <typename T> |
5275 | 142 bool |
143 extract_keyword (std::istream& is, const string_vector& keywords, | |
10313 | 144 std::string& kw, T& value, const bool next_only = false) |
5275 | 145 { |
146 bool status = false; | |
147 kw = ""; | |
148 value = 0; | |
149 | |
150 char c; | |
151 while (is.get (c)) | |
152 { | |
153 if (c == '%' || c == '#') | |
10313 | 154 { |
155 std::ostringstream buf; | |
5275 | 156 |
10313 | 157 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
158 ; // Skip whitespace and comment characters. | |
5275 | 159 |
10313 | 160 if (isalpha (c)) |
161 buf << c; | |
5275 | 162 |
10313 | 163 while (is.get (c) && isalpha (c)) |
164 buf << c; | |
5275 | 165 |
10313 | 166 std::string tmp = buf.str (); |
5275 | 167 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
168 for (int i = 0; i < keywords.numel (); i++) |
10313 | 169 { |
170 int match = (tmp == keywords[i]); | |
5275 | 171 |
10313 | 172 if (match) |
173 { | |
174 kw = keywords[i]; | |
5275 | 175 |
10313 | 176 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
177 ; // Skip whitespace and the colon. | |
5275 | 178 |
10313 | 179 is.putback (c); |
180 if (c != '\n' && c != '\r') | |
181 is >> value; | |
182 if (is) | |
183 status = true; | |
184 skip_until_newline (is, false); | |
185 return status; | |
186 } | |
187 } | |
5275 | 188 |
10313 | 189 if (next_only) |
190 break; | |
191 } | |
5275 | 192 } |
193 return status; | |
194 } | |
195 | |
4634 | 196 #endif |