Mercurial > hg > octave-nkf
annotate src/ov-fcn-inline.cc @ 10454:79a56d0a6a0d ss-3-3-51
version is now 3.3.51
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 24 Mar 2010 16:27:05 -0400 |
parents | 57a59eae83cc |
children | 604e13a89c7f |
rev | line source |
---|---|
4933 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 David Bateman |
4933 | 4 |
7016 | 5 This file is part of Octave. |
4933 | 6 |
7016 | 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 3 of the License, or (at your | |
10 option) any 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. | |
4933 | 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/>. | |
4933 | 20 |
21 In addition to the terms of the GPL, you are permitted to link | |
22 this program with any Open Source program, as defined by the | |
23 Open Source Initiative (www.opensource.org) | |
24 | |
25 */ | |
26 | |
27 #ifdef HAVE_CONFIG_H | |
28 #include <config.h> | |
29 #endif | |
30 | |
4988 | 31 #include <istream> |
4933 | 32 #include <iostream> |
5765 | 33 #include <sstream> |
5164 | 34 #include <vector> |
4933 | 35 |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8045
diff
changeset
|
36 #include "oct-locbuf.h" |
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8045
diff
changeset
|
37 |
4933 | 38 #include "defun.h" |
39 #include "error.h" | |
40 #include "gripes.h" | |
41 #include "oct-map.h" | |
42 #include "ov-base.h" | |
43 #include "ov-fcn-inline.h" | |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
44 #include "ov-usr-fcn.h" |
4933 | 45 #include "pr-output.h" |
46 #include "variables.h" | |
47 #include "parse.h" | |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
48 #include "toplev.h" |
4933 | 49 |
4972 | 50 #include "byte-swap.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
|
51 #include "ls-ascii-helper.h" |
4972 | 52 #include "ls-oct-ascii.h" |
53 #include "ls-hdf5.h" | |
54 #include "ls-utils.h" | |
55 | |
4933 | 56 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_inline); |
57 | |
58 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_inline, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
59 "inline function", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
60 "function_handle"); |
4933 | 61 |
62 octave_fcn_inline::octave_fcn_inline (const std::string& f, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
63 const string_vector& a, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
64 const std::string& n) |
5007 | 65 : octave_fcn_handle (n), iftext (f), ifargs (a) |
4933 | 66 { |
4973 | 67 // Form a string representing the function. |
4933 | 68 |
5765 | 69 std::ostringstream buf; |
4933 | 70 |
5007 | 71 buf << "@("; |
4933 | 72 |
73 for (int i = 0; i < ifargs.length (); i++) | |
74 { | |
75 if (i > 0) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
76 buf << ", "; |
4933 | 77 |
78 buf << ifargs(i); | |
79 } | |
80 | |
5765 | 81 buf << ") " << iftext; |
4933 | 82 |
5007 | 83 int parse_status; |
5765 | 84 octave_value anon_fcn_handle = eval_string (buf.str (), true, parse_status); |
4933 | 85 |
5007 | 86 if (parse_status == 0) |
4933 | 87 { |
5007 | 88 octave_fcn_handle *fh = anon_fcn_handle.fcn_handle_value (); |
4933 | 89 |
5007 | 90 if (fh) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
91 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
92 fcn = fh->fcn_val (); |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
93 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
94 octave_user_function *uf = fcn.user_function_value (); |
8032
2fd4a5ef6b59
stash parent function scope for inline functions and anonymous function handles.
John W. Eaton <jwe@octave.org>
parents:
7908
diff
changeset
|
95 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
96 if (uf) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
97 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
98 octave_function *curr_fcn = octave_call_stack::current (); |
8045
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
99 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
100 if (curr_fcn) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
101 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
102 symbol_table::scope_id parent_scope |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
103 = curr_fcn->parent_fcn_scope (); |
8045
24701aa75ecb
scope fixes for anonymous and inline functions that appear inside subfunctions
John W. Eaton <jwe@octave.org>
parents:
8032
diff
changeset
|
104 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
105 if (parent_scope < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
106 parent_scope = curr_fcn->scope (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
107 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
108 uf->stash_parent_fcn_scope (parent_scope); |
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 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
111 } |
4933 | 112 } |
5007 | 113 |
114 if (fcn.is_undefined ()) | |
4933 | 115 error ("inline: unable to define function"); |
116 } | |
117 | |
6625 | 118 // This function is supplied to allow a Matlab style class structure |
119 // to be returned.. | |
120 Octave_map | |
121 octave_fcn_inline::map_value (void) const | |
122 { | |
123 Octave_map m; | |
124 string_vector args = fcn_arg_names (); | |
125 m.assign ("version", octave_value (1.0)); | |
126 m.assign ("isEmpty", octave_value (0.0)); | |
127 m.assign ("expr", octave_value (fcn_text ())); | |
128 m.assign ("numArgs", octave_value (args.length ())); | |
129 m.assign ("args", octave_value (args)); | |
130 std::ostringstream buf; | |
131 for (int i = 0; i < args.length (); i++) | |
132 buf << args(i) << " = INLINE_INPUTS_{" << i + 1 << "}; "; | |
133 m.assign ("inputExpr", octave_value (buf.str ())); | |
134 | |
135 return m; | |
136 } | |
137 | |
4972 | 138 bool |
6974 | 139 octave_fcn_inline::save_ascii (std::ostream& os) |
4972 | 140 { |
4973 | 141 os << "# nargs: " << ifargs.length () << "\n"; |
4972 | 142 for (int i = 0; i < ifargs.length (); i++) |
4973 | 143 os << ifargs(i) << "\n"; |
144 if (nm.length () < 1) | |
145 // Write an invalid value to flag empty fcn handle name. | |
4972 | 146 os << "0\n"; |
147 else | |
148 os << nm << "\n"; | |
149 os << iftext << "\n"; | |
150 return true; | |
151 } | |
152 | |
153 bool | |
154 octave_fcn_inline::load_ascii (std::istream& is) | |
155 { | |
156 int nargs; | |
157 if (extract_keyword (is, "nargs", nargs, true)) | |
158 { | |
159 ifargs.resize (nargs); | |
160 for (int i = 0; i < nargs; i++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
161 is >> ifargs(i); |
4972 | 162 is >> nm; |
163 if (nm == "0") | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
164 nm = ""; |
4988 | 165 |
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
|
166 skip_preceeding_newline (is); |
4988 | 167 |
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
|
168 std::string buf; |
4988 | 169 |
170 if (is) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
171 { |
4988 | 172 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
173 // Get a line of text whitespace characters included, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
174 // leaving newline in the stream. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
175 buf = read_until_newline (is, true); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
176 } |
4988 | 177 |
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
|
178 iftext = buf; |
4972 | 179 |
180 octave_fcn_inline tmp (iftext, ifargs, nm); | |
181 fcn = tmp.fcn; | |
182 | |
183 return true; | |
184 } | |
185 else | |
186 return false; | |
187 } | |
188 | |
4973 | 189 bool |
4972 | 190 octave_fcn_inline::save_binary (std::ostream& os, bool&) |
191 { | |
5828 | 192 int32_t tmp = ifargs.length (); |
5760 | 193 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 194 for (int i = 0; i < ifargs.length (); i++) |
4972 | 195 { |
4973 | 196 tmp = ifargs(i).length (); |
5760 | 197 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 198 os.write (ifargs(i).c_str (), ifargs(i).length ()); |
4972 | 199 } |
4973 | 200 tmp = nm.length (); |
5760 | 201 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 202 os.write (nm.c_str (), nm.length ()); |
203 tmp = iftext.length (); | |
5760 | 204 os.write (reinterpret_cast<char *> (&tmp), 4); |
4973 | 205 os.write (iftext.c_str (), iftext.length ()); |
4972 | 206 return true; |
207 } | |
208 | |
4973 | 209 bool |
4972 | 210 octave_fcn_inline::load_binary (std::istream& is, bool swap, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
211 oct_mach_info::float_format) |
4972 | 212 { |
5828 | 213 int32_t nargs; |
5760 | 214 if (! is.read (reinterpret_cast<char *> (&nargs), 4)) |
4972 | 215 return false; |
216 if (swap) | |
217 swap_bytes<4> (&nargs); | |
218 | |
219 if (nargs < 1) | |
220 return false; | |
221 else | |
222 { | |
5828 | 223 int32_t tmp; |
4973 | 224 ifargs.resize (nargs); |
4972 | 225 for (int i = 0; i < nargs; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
226 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
227 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
228 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
229 if (swap) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
230 swap_bytes<4> (&tmp); |
4972 | 231 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
232 OCTAVE_LOCAL_BUFFER (char, ctmp, tmp+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
233 is.read (ctmp, tmp); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
234 ifargs(i) = std::string (ctmp); |
4972 | 235 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
236 if (! is) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
237 return false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
238 } |
4972 | 239 |
5760 | 240 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
241 return false; |
4972 | 242 if (swap) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
243 swap_bytes<4> (&tmp); |
4972 | 244 |
245 OCTAVE_LOCAL_BUFFER (char, ctmp1, tmp+1); | |
246 is.read (ctmp1, tmp); | |
247 nm = std::string (ctmp1); | |
248 | |
249 if (! is) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
250 return false; |
4972 | 251 |
5760 | 252 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
253 return false; |
4972 | 254 if (swap) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
255 swap_bytes<4> (&tmp); |
4972 | 256 |
257 OCTAVE_LOCAL_BUFFER (char, ctmp2, tmp+1); | |
258 is.read (ctmp2, tmp); | |
259 iftext = std::string (ctmp2); | |
260 | |
261 if (! is) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
262 return false; |
4972 | 263 |
264 octave_fcn_inline ftmp (iftext, ifargs, nm); | |
265 fcn = ftmp.fcn; | |
266 } | |
267 return true; | |
268 } | |
269 | |
270 #if defined (HAVE_HDF5) | |
271 bool | |
272 octave_fcn_inline::save_hdf5 (hid_t loc_id, const char *name, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
273 bool /* save_as_floats */) |
4972 | 274 { |
275 hid_t group_hid = -1; | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
276 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
277 group_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
278 #else |
4972 | 279 group_hid = H5Gcreate (loc_id, name, 0); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
280 #endif |
4972 | 281 if (group_hid < 0 ) return false; |
282 | |
283 size_t len = 0; | |
4973 | 284 for (int i = 0; i < ifargs.length (); i++) |
285 if (len < ifargs(i).length ()) | |
286 len = ifargs(i).length (); | |
4972 | 287 |
288 hid_t space_hid = -1, data_hid = -1, type_hid = -1;; | |
289 bool retval = true; | |
290 | |
5775 | 291 // FIXME Is there a better way of saving string vectors, than a |
4972 | 292 // null padded matrix? |
293 | |
294 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2); | |
295 | |
296 // Octave uses column-major, while HDF5 uses row-major ordering | |
4973 | 297 hdims[1] = ifargs.length (); |
4972 | 298 hdims[0] = len + 1; |
299 | |
300 space_hid = H5Screate_simple (2, hdims, 0); | |
301 if (space_hid < 0) | |
302 { | |
303 H5Gclose (group_hid); | |
304 return false; | |
305 } | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
306 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
307 data_hid = H5Dcreate (group_hid, "args", H5T_NATIVE_CHAR, space_hid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
308 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
309 #else |
4973 | 310 data_hid = H5Dcreate (group_hid, "args", H5T_NATIVE_CHAR, space_hid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
311 H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
312 #endif |
4972 | 313 if (data_hid < 0) |
314 { | |
315 H5Sclose (space_hid); | |
316 H5Gclose (group_hid); | |
317 return false; | |
318 } | |
319 | |
4973 | 320 OCTAVE_LOCAL_BUFFER (char, s, ifargs.length () * (len + 1)); |
4972 | 321 |
322 // Save the args as a null teminated list | |
4973 | 323 for (int i = 0; i < ifargs.length (); i++) |
4972 | 324 { |
4973 | 325 const char * cptr = ifargs(i).c_str (); |
326 for (size_t j = 0; j < ifargs(i).length (); j++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
327 s[i*(len+1)+j] = *cptr++; |
4973 | 328 s[ifargs(i).length ()] = '\0'; |
4972 | 329 } |
330 | |
4973 | 331 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
332 H5P_DEFAULT, s) >= 0; |
4972 | 333 |
334 H5Dclose (data_hid); | |
335 H5Sclose (space_hid); | |
336 | |
337 if (!retval) | |
338 { | |
339 H5Gclose (group_hid); | |
340 return false; | |
4973 | 341 } |
4972 | 342 |
343 // attach the type of the variable | |
4973 | 344 type_hid = H5Tcopy (H5T_C_S1); |
4972 | 345 H5Tset_size (type_hid, nm.length () + 1); |
346 if (type_hid < 0) | |
347 { | |
348 H5Gclose (group_hid); | |
349 return false; | |
4973 | 350 } |
4972 | 351 |
352 hdims[0] = 0; | |
5760 | 353 space_hid = H5Screate_simple (0 , hdims, 0); |
4972 | 354 if (space_hid < 0) |
355 { | |
356 H5Tclose (type_hid); | |
357 H5Gclose (group_hid); | |
358 return false; | |
4973 | 359 } |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
360 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
361 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
362 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
363 #else |
4972 | 364 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
365 #endif |
4973 | 366 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
367 H5P_DEFAULT, nm.c_str ()) < 0) |
4972 | 368 { |
369 H5Sclose (space_hid); | |
370 H5Tclose (type_hid); | |
371 H5Gclose (group_hid); | |
372 return false; | |
4973 | 373 } |
4972 | 374 H5Dclose (data_hid); |
375 | |
376 // attach the type of the variable | |
377 H5Tset_size (type_hid, iftext.length () + 1); | |
378 if (type_hid < 0) | |
379 { | |
380 H5Gclose (group_hid); | |
381 return false; | |
4973 | 382 } |
4972 | 383 |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
384 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
385 data_hid = H5Dcreate (group_hid, "iftext", type_hid, space_hid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
386 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
387 #else |
4973 | 388 data_hid = H5Dcreate (group_hid, "iftext", type_hid, space_hid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
389 H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
390 #endif |
4973 | 391 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
392 H5P_DEFAULT, iftext.c_str ()) < 0) |
4972 | 393 { |
394 H5Sclose (space_hid); | |
395 H5Tclose (type_hid); | |
396 H5Gclose (group_hid); | |
397 return false; | |
4973 | 398 } |
4972 | 399 |
400 H5Dclose (data_hid); | |
4988 | 401 H5Sclose (space_hid); |
402 H5Tclose (type_hid); | |
403 H5Gclose (group_hid); | |
4972 | 404 |
405 return retval; | |
406 } | |
407 | |
408 bool | |
9881
b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
Kacper Kowalik
parents:
9038
diff
changeset
|
409 octave_fcn_inline::load_hdf5 (hid_t loc_id, const char *name) |
4972 | 410 { |
411 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; | |
412 hsize_t rank; | |
413 int slen; | |
414 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
415 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
416 group_hid = H5Gopen (loc_id, name, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
417 #else |
4972 | 418 group_hid = H5Gopen (loc_id, name); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
419 #endif |
4972 | 420 if (group_hid < 0 ) return false; |
421 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
422 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
423 data_hid = H5Dopen (group_hid, "args", H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
424 #else |
4972 | 425 data_hid = H5Dopen (group_hid, "args"); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
426 #endif |
4972 | 427 space_hid = H5Dget_space (data_hid); |
428 rank = H5Sget_simple_extent_ndims (space_hid); | |
429 | |
430 if (rank != 2) | |
4973 | 431 { |
4972 | 432 H5Dclose (data_hid); |
433 H5Sclose (space_hid); | |
434 H5Gclose (group_hid); | |
435 return false; | |
436 } | |
437 | |
438 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
439 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
440 | |
441 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
442 | |
4973 | 443 ifargs.resize (hdims[1]); |
4972 | 444 |
445 OCTAVE_LOCAL_BUFFER (char, s1, hdims[0] * hdims[1]); | |
446 | |
4973 | 447 if (H5Dread (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
448 H5P_DEFAULT, s1) < 0) |
4973 | 449 { |
4972 | 450 H5Dclose (data_hid); |
451 H5Sclose (space_hid); | |
452 H5Gclose (group_hid); | |
453 return false; | |
454 } | |
455 | |
456 H5Dclose (data_hid); | |
457 H5Sclose (space_hid); | |
458 | |
459 for (size_t i = 0; i < hdims[1]; i++) | |
460 ifargs(i) = std::string (s1 + i*hdims[0]); | |
461 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
462 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
463 data_hid = H5Dopen (group_hid, "nm", H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
464 #else |
4972 | 465 data_hid = H5Dopen (group_hid, "nm"); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
466 #endif |
4972 | 467 |
468 if (data_hid < 0) | |
469 { | |
470 H5Gclose (group_hid); | |
471 return false; | |
472 } | |
473 | |
474 type_hid = H5Dget_type (data_hid); | |
475 type_class_hid = H5Tget_class (type_hid); | |
476 | |
477 if (type_class_hid != H5T_STRING) | |
478 { | |
479 H5Tclose (type_hid); | |
480 H5Dclose (data_hid); | |
481 H5Gclose (group_hid); | |
482 return false; | |
483 } | |
4973 | 484 |
4972 | 485 space_hid = H5Dget_space (data_hid); |
486 rank = H5Sget_simple_extent_ndims (space_hid); | |
487 | |
488 if (rank != 0) | |
489 { | |
490 H5Sclose (space_hid); | |
491 H5Tclose (type_hid); | |
492 H5Dclose (data_hid); | |
493 H5Gclose (group_hid); | |
494 return false; | |
495 } | |
496 | |
497 slen = H5Tget_size (type_hid); | |
498 if (slen < 0) | |
499 { | |
500 H5Sclose (space_hid); | |
501 H5Tclose (type_hid); | |
502 H5Dclose (data_hid); | |
503 H5Gclose (group_hid); | |
504 return false; | |
505 } | |
506 | |
507 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen); | |
508 | |
509 // create datatype for (null-terminated) string to read into: | |
510 st_id = H5Tcopy (H5T_C_S1); | |
511 H5Tset_size (st_id, slen); | |
512 | |
5760 | 513 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, nm_tmp) < 0) |
4972 | 514 { |
515 H5Sclose (space_hid); | |
516 H5Tclose (type_hid); | |
517 H5Gclose (group_hid); | |
518 return false; | |
519 } | |
520 H5Tclose (st_id); | |
521 H5Dclose (data_hid); | |
522 nm = nm_tmp; | |
523 | |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
524 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
525 data_hid = H5Dopen (group_hid, "iftext", H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
526 #else |
4972 | 527 data_hid = H5Dopen (group_hid, "iftext"); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
528 #endif |
4972 | 529 |
530 if (data_hid < 0) | |
531 { | |
532 H5Gclose (group_hid); | |
533 return false; | |
534 } | |
535 | |
536 type_hid = H5Dget_type (data_hid); | |
537 type_class_hid = H5Tget_class (type_hid); | |
538 | |
539 if (type_class_hid != H5T_STRING) | |
540 { | |
541 H5Tclose (type_hid); | |
542 H5Dclose (data_hid); | |
543 H5Gclose (group_hid); | |
544 return false; | |
545 } | |
4973 | 546 |
4972 | 547 space_hid = H5Dget_space (data_hid); |
548 rank = H5Sget_simple_extent_ndims (space_hid); | |
549 | |
550 if (rank != 0) | |
551 { | |
552 H5Sclose (space_hid); | |
553 H5Tclose (type_hid); | |
554 H5Dclose (data_hid); | |
555 H5Gclose (group_hid); | |
556 return false; | |
557 } | |
558 | |
559 slen = H5Tget_size (type_hid); | |
560 if (slen < 0) | |
561 { | |
562 H5Sclose (space_hid); | |
563 H5Tclose (type_hid); | |
564 H5Dclose (data_hid); | |
565 H5Gclose (group_hid); | |
566 return false; | |
567 } | |
568 | |
569 OCTAVE_LOCAL_BUFFER (char, iftext_tmp, slen); | |
570 | |
571 // create datatype for (null-terminated) string to read into: | |
572 st_id = H5Tcopy (H5T_C_S1); | |
573 H5Tset_size (st_id, slen); | |
574 | |
5760 | 575 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, iftext_tmp) < 0) |
4972 | 576 { |
577 H5Sclose (space_hid); | |
578 H5Tclose (type_hid); | |
579 H5Gclose (group_hid); | |
580 return false; | |
581 } | |
582 H5Tclose (st_id); | |
583 H5Dclose (data_hid); | |
584 iftext = iftext_tmp; | |
585 | |
586 octave_fcn_inline ftmp (iftext, ifargs, nm); | |
587 fcn = ftmp.fcn; | |
588 | |
589 return true; | |
590 } | |
591 #endif | |
592 | |
4933 | 593 void |
594 octave_fcn_inline::print (std::ostream& os, bool pr_as_read_syntax) const | |
595 { | |
596 print_raw (os, pr_as_read_syntax); | |
597 newline (os); | |
598 } | |
599 | |
600 void | |
601 octave_fcn_inline::print_raw (std::ostream& os, bool pr_as_read_syntax) const | |
602 { | |
5765 | 603 std::ostringstream buf; |
4933 | 604 |
605 if (nm.empty ()) | |
606 buf << "f("; | |
607 else | |
608 buf << nm << "("; | |
609 | |
610 for (int i = 0; i < ifargs.length (); i++) | |
611 { | |
612 if (i) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
613 buf << ", "; |
4933 | 614 |
615 buf << ifargs(i); | |
616 } | |
617 | |
5765 | 618 buf << ") = " << iftext; |
4933 | 619 |
5765 | 620 octave_print_internal (os, buf.str (), pr_as_read_syntax, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
621 current_print_indent_level ()); |
4933 | 622 } |
623 | |
624 octave_value | |
5279 | 625 octave_fcn_inline::convert_to_str_internal (bool, bool, char type) const |
4933 | 626 { |
5279 | 627 return octave_value (fcn_text (), type); |
4933 | 628 } |
629 | |
6973 | 630 DEFUNX ("inline", Finline, args, , |
4933 | 631 "-*- texinfo -*-\n\ |
632 @deftypefn {Built-in Function} {} inline (@var{str})\n\ | |
6547 | 633 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, @dots{})\n\ |
4933 | 634 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\ |
635 Create an inline function from the character string @var{str}.\n\ | |
5034 | 636 If called with a single argument, the arguments of the generated\n\ |
9038
fca0dc2fb042
Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents:
8946
diff
changeset
|
637 function are extracted from the function itself. The generated\n\ |
fca0dc2fb042
Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents:
8946
diff
changeset
|
638 function arguments will then be in alphabetical order. It should\n\ |
5034 | 639 be noted that i, and j are ignored as arguments due to the\n\ |
640 ambiguity between their use as a variable or their use as an inbuilt\n\ | |
9038
fca0dc2fb042
Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents:
8946
diff
changeset
|
641 constant. All arguments followed by a parenthesis are considered\n\ |
5034 | 642 to be functions.\n\ |
4933 | 643 \n\ |
644 If the second and subsequent arguments are character strings,\n\ | |
645 they are the names of the arguments of the function.\n\ | |
646 \n\ | |
647 If the second argument is an integer @var{n}, the arguments are\n\ | |
648 @code{\"x\"}, @code{\"P1\"}, @dots{}, @code{\"P@var{N}\"}.\n\ | |
5642 | 649 @seealso{argnames, formula, vectorize}\n\ |
650 @end deftypefn") | |
4933 | 651 { |
652 octave_value retval; | |
653 | |
654 int nargin = args.length (); | |
655 | |
656 if (nargin > 0) | |
657 { | |
658 std::string fun = args(0).string_value (); | |
659 | |
660 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
661 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
662 string_vector fargs; |
4933 | 663 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
664 if (nargin == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
665 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
666 bool is_arg = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
667 bool in_string = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
668 std::string tmp_arg; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
669 size_t i = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
670 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
671 while (i < fun.length ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
672 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
673 bool terminate_arg = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
674 char c = fun[i++]; |
4933 | 675 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
676 if (in_string) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
677 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
678 if (c == '\'' || c == '\"') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
679 in_string = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
680 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
681 else if (c == '\'' || c == '\"') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
682 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
683 in_string = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
684 if (is_arg) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
685 terminate_arg = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
686 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
687 else if (! isalpha (c) && c != '_') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
688 if (! is_arg) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
689 continue; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
690 else if (isdigit (c)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
691 tmp_arg.append (1, c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
692 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
693 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
694 // Before we do anything remove trailing whitespaces. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
695 while (i < fun.length () && isspace (c)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
696 c = fun[i++]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
697 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
698 // Do we have a variable or a function? |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
699 if (c != '(') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
700 terminate_arg = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
701 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
702 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
703 tmp_arg = std::string (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
704 is_arg = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
705 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
706 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
707 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
708 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
709 tmp_arg.append (1, c); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
710 is_arg = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
711 } |
5034 | 712 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
713 if (terminate_arg || (i == fun.length () && is_arg)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
714 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
715 bool have_arg = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
716 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
717 for (int j = 0; j < fargs.length (); j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
718 if (tmp_arg == fargs (j)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
719 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
720 have_arg = true; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
721 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
722 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
723 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
724 if (! have_arg && tmp_arg != "i" && tmp_arg != "j" && |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
725 tmp_arg != "NaN" && tmp_arg != "nan" && |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
726 tmp_arg != "Inf" && tmp_arg != "inf" && |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
727 tmp_arg != "NA" && tmp_arg != "pi" && |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
728 tmp_arg != "eps") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
729 fargs.append (tmp_arg); |
5037 | 730 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
731 tmp_arg = std::string (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
732 is_arg = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
733 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
734 } |
5034 | 735 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
736 // Sort the arguments into ascii order. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
737 fargs.sort (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
738 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
739 else if (nargin == 2 && args(1).is_numeric_type ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
740 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
741 int n = args(1).int_value (); |
4933 | 742 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
743 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
744 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
745 if (n >= 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
746 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
747 fargs.resize (n+1); |
4933 | 748 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
749 fargs(0) = "x"; |
4933 | 750 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
751 for (int i = 1; i < n+1; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
752 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
753 std::ostringstream buf; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
754 buf << "P" << i; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
755 fargs(i) = buf.str (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
756 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
757 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
758 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
759 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
760 error ("inline: numeric argument must be nonnegative"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
761 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
762 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
763 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
764 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
765 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
766 error ("inline: expecting second argument to be an integer"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
767 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
768 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
769 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
770 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
771 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
772 fargs.resize (nargin - 1); |
4933 | 773 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
774 for (int i = 1; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
775 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
776 std::string s = args(i).string_value (); |
4933 | 777 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
778 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
779 fargs(i-1) = s; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
780 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
781 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
782 error ("inline: expecting string arguments"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
783 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
784 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
785 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
786 } |
4933 | 787 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
788 retval = octave_value (new octave_fcn_inline (fun, fargs)); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
789 } |
4933 | 790 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
791 error ("inline: first argument must be a string"); |
4933 | 792 } |
793 else | |
5823 | 794 print_usage (); |
4933 | 795 |
796 return retval; | |
797 } | |
798 | |
7394 | 799 /* |
800 %!shared fn | |
801 %! fn = inline ("x.^2 + 1","x"); | |
802 %!assert (feval (fn, 6), 37) | |
803 %!assert (fn (6), 37) | |
804 */ | |
805 | |
4933 | 806 DEFUN (formula, args, , |
807 "-*- texinfo -*-\n\ | |
808 @deftypefn {Built-in Function} {} formula (@var{fun})\n\ | |
809 Return a character string representing the inline function @var{fun}.\n\ | |
810 Note that @code{char (@var{fun})} is equivalent to\n\ | |
811 @code{formula (@var{fun})}.\n\ | |
5642 | 812 @seealso{argnames, inline, vectorize}\n\ |
813 @end deftypefn") | |
4933 | 814 { |
815 octave_value retval; | |
816 | |
817 int nargin = args.length (); | |
818 | |
819 if (nargin == 1) | |
820 { | |
821 octave_fcn_inline* fn = args(0).fcn_inline_value (true); | |
822 | |
823 if (fn) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
824 retval = octave_value (fn->fcn_text ()); |
4933 | 825 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
826 error ("formula: must be an inline function"); |
4933 | 827 } |
828 else | |
5823 | 829 print_usage (); |
4933 | 830 |
831 return retval; | |
832 } | |
833 | |
834 DEFUN (argnames, args, , | |
835 "-*- texinfo -*-\n\ | |
836 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ | |
837 Return a cell array of character strings containing the names of\n\ | |
838 the arguments of the inline function @var{fun}.\n\ | |
6529 | 839 @seealso{inline, formula, vectorize}\n\ |
5642 | 840 @end deftypefn") |
4933 | 841 { |
842 octave_value retval; | |
843 | |
844 int nargin = args.length (); | |
845 | |
846 if (nargin == 1) | |
847 { | |
848 octave_fcn_inline *fn = args(0).fcn_inline_value (true); | |
849 | |
850 if (fn) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
851 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
852 string_vector t1 = fn->fcn_arg_names (); |
4933 | 853 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
854 Cell t2 (dim_vector (t1.length (), 1)); |
4933 | 855 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
856 for (int i = 0; i < t1.length (); i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
857 t2(i) = t1(i); |
4933 | 858 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
859 retval = t2; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
860 } |
4933 | 861 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
862 error ("argnames: argument must be an inline function"); |
4933 | 863 } |
864 else | |
5823 | 865 print_usage (); |
4933 | 866 |
867 return retval; | |
868 } | |
869 | |
870 DEFUN (vectorize, args, , | |
871 "-*- texinfo -*-\n\ | |
6635 | 872 @deftypefn {Built-in Function} {} vectorize (@var{fun})\n\ |
4933 | 873 Create a vectorized version of the inline function @var{fun}\n\ |
874 by replacing all occurrences of @code{*}, @code{/}, etc., with\n\ | |
875 @code{.*}, @code{./}, etc.\n\ | |
5642 | 876 @end deftypefn") |
4933 | 877 { |
878 octave_value retval; | |
879 | |
880 int nargin = args.length (); | |
881 | |
882 if (nargin == 1) | |
883 { | |
5409 | 884 std::string old_func; |
885 octave_fcn_inline* old = 0; | |
886 bool func_is_string = true; | |
4933 | 887 |
5409 | 888 if (args(0).is_string ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
889 old_func = args(0).string_value (); |
5409 | 890 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
891 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
892 old = args(0).fcn_inline_value (true); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
893 func_is_string = false; |
5409 | 894 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
895 if (old) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
896 old_func = old->fcn_text (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
897 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
898 error ("vectorize: must be a string or inline function"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
899 } |
5409 | 900 |
901 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
902 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
903 std::string new_func; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
904 size_t i = 0; |
4933 | 905 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
906 while (i < old_func.length ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
907 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
908 std::string t1 = old_func.substr (i, 1); |
4933 | 909 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
910 if (t1 == "*" || t1 == "/" || t1 == "\\" || t1 == "^") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
911 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
912 if (i && old_func.substr (i-1, 1) != ".") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
913 new_func.append ("."); |
4933 | 914 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
915 // Special case for ** operator. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
916 if (t1 == "*" && i < (old_func.length () - 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
917 && old_func.substr (i+1, 1) == "*") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
918 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
919 new_func.append ("*"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
920 i++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
921 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
922 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
923 new_func.append (t1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
924 i++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
925 } |
4933 | 926 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
927 if (func_is_string) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
928 retval = octave_value (new_func); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
929 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
930 retval = octave_value (new octave_fcn_inline |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
931 (new_func, old->fcn_arg_names ())); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
932 } |
4933 | 933 } |
934 else | |
5823 | 935 print_usage (); |
4933 | 936 |
937 return retval; | |
938 } |