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