4933
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 David Bateman |
|
4 |
|
5 This program is free software; you can redistribute it and/or modify it |
|
6 under the terms of the GNU General Public License as published by |
|
7 the Free Software Foundation; either version 2, or (at your option) |
|
8 any later version. |
|
9 |
|
10 This program is distributed in the hope that it will be useful, but |
|
11 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 General Public License for more details. |
|
14 |
|
15 You should have received a copy of the GNU General Public License |
|
16 along with Octave; see the file COPYING. If not, write to the Free |
|
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 02111-1307, USA. |
|
19 |
|
20 In addition to the terms of the GPL, you are permitted to link |
|
21 this program with any Open Source program, as defined by the |
|
22 Open Source Initiative (www.opensource.org) |
|
23 |
|
24 */ |
|
25 |
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
27 #pragma implementation |
|
28 #endif |
|
29 |
|
30 #ifdef HAVE_CONFIG_H |
|
31 #include <config.h> |
|
32 #endif |
|
33 |
|
34 #include <iostream> |
|
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" |
|
42 #include "pr-output.h" |
|
43 #include "variables.h" |
|
44 #include "parse.h" |
|
45 |
4972
|
46 #include "byte-swap.h" |
|
47 #include "ls-oct-ascii.h" |
|
48 #include "ls-hdf5.h" |
|
49 #include "ls-utils.h" |
|
50 |
4933
|
51 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_inline); |
|
52 |
|
53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_inline, |
|
54 "inline function", |
|
55 "inline function"); |
|
56 |
|
57 octave_fcn_inline::octave_fcn_inline (const std::string& f, |
4973
|
58 const string_vector& a, |
4933
|
59 const std::string& n) |
4973
|
60 : octave_fcn_handle (0, n), iftext (f), ifargs (a) |
4933
|
61 { |
|
62 // Find a function name that isn't already in the symbol table. |
4962
|
63 std::string fname = unique_symbol_name ("__inline_"); |
4933
|
64 |
4973
|
65 // Form a string representing the function. |
4933
|
66 |
|
67 OSSTREAM buf; |
|
68 |
|
69 buf << "function __retval__ = " << fname << "("; |
|
70 |
|
71 for (int i = 0; i < ifargs.length (); i++) |
|
72 { |
|
73 if (i > 0) |
|
74 buf << ", "; |
|
75 |
|
76 buf << ifargs(i); |
|
77 } |
|
78 |
|
79 buf << ")\n __retval__ = " << iftext << ";\nendfunction" << OSSTREAM_ENDS; |
4973
|
80 |
4933
|
81 // Parse this function and create a user function. |
|
82 |
4973
|
83 octave_value eval_args (OSSTREAM_STR (buf)); |
4933
|
84 |
|
85 feval ("eval", eval_args, 0); |
|
86 |
|
87 OSSTREAM_FREEZE (buf); |
|
88 |
|
89 octave_value tmp = lookup_function (fname); |
|
90 |
|
91 if (tmp.is_function ()) |
|
92 { |
|
93 fcn = tmp; |
|
94 |
4954
|
95 clear_function (fname); |
4933
|
96 } |
|
97 else |
|
98 error ("inline: unable to define function"); |
|
99 } |
|
100 |
4972
|
101 bool |
|
102 octave_fcn_inline::save_ascii (std::ostream& os, bool&, bool) |
|
103 { |
4973
|
104 os << "# nargs: " << ifargs.length () << "\n"; |
4972
|
105 for (int i = 0; i < ifargs.length (); i++) |
4973
|
106 os << ifargs(i) << "\n"; |
|
107 if (nm.length () < 1) |
|
108 // Write an invalid value to flag empty fcn handle name. |
4972
|
109 os << "0\n"; |
|
110 else |
|
111 os << nm << "\n"; |
|
112 os << iftext << "\n"; |
|
113 return true; |
|
114 } |
|
115 |
|
116 bool |
|
117 octave_fcn_inline::load_ascii (std::istream& is) |
|
118 { |
|
119 int nargs; |
|
120 if (extract_keyword (is, "nargs", nargs, true)) |
|
121 { |
|
122 ifargs.resize (nargs); |
|
123 for (int i = 0; i < nargs; i++) |
4973
|
124 is >> ifargs(i); |
4972
|
125 is >> nm; |
|
126 if (nm == "0") |
|
127 nm = ""; |
|
128 is >> iftext; |
|
129 |
|
130 octave_fcn_inline tmp (iftext, ifargs, nm); |
|
131 fcn = tmp.fcn; |
|
132 |
|
133 return true; |
|
134 } |
|
135 else |
|
136 return false; |
|
137 } |
|
138 |
4973
|
139 bool |
4972
|
140 octave_fcn_inline::save_binary (std::ostream& os, bool&) |
|
141 { |
4973
|
142 FOUR_BYTE_INT tmp = ifargs.length (); |
4972
|
143 os.write (X_CAST (char *, &tmp), 4); |
4973
|
144 for (int i = 0; i < ifargs.length (); i++) |
4972
|
145 { |
4973
|
146 tmp = ifargs(i).length (); |
4972
|
147 os.write (X_CAST (char *, &tmp), 4); |
4973
|
148 os.write (ifargs(i).c_str (), ifargs(i).length ()); |
4972
|
149 } |
4973
|
150 tmp = nm.length (); |
4972
|
151 os.write (X_CAST (char *, &tmp), 4); |
4973
|
152 os.write (nm.c_str (), nm.length ()); |
|
153 tmp = iftext.length (); |
4972
|
154 os.write (X_CAST (char *, &tmp), 4); |
4973
|
155 os.write (iftext.c_str (), iftext.length ()); |
4972
|
156 return true; |
|
157 } |
|
158 |
4973
|
159 bool |
4972
|
160 octave_fcn_inline::load_binary (std::istream& is, bool swap, |
|
161 oct_mach_info::float_format) |
|
162 { |
|
163 FOUR_BYTE_INT nargs; |
|
164 if (! is.read (X_CAST (char *, &nargs), 4)) |
|
165 return false; |
|
166 if (swap) |
|
167 swap_bytes<4> (&nargs); |
|
168 |
|
169 if (nargs < 1) |
|
170 return false; |
|
171 else |
|
172 { |
|
173 FOUR_BYTE_INT tmp; |
4973
|
174 ifargs.resize (nargs); |
4972
|
175 for (int i = 0; i < nargs; i++) |
|
176 { |
|
177 if (! is.read (X_CAST (char *, &tmp), 4)) |
|
178 return false; |
|
179 if (swap) |
|
180 swap_bytes<4> (&tmp); |
|
181 |
|
182 OCTAVE_LOCAL_BUFFER (char, ctmp, tmp+1); |
|
183 is.read (ctmp, tmp); |
|
184 ifargs(i) = std::string (ctmp); |
|
185 |
|
186 if (! is) |
|
187 return false; |
4973
|
188 } |
4972
|
189 |
|
190 if (! is.read (X_CAST (char *, &tmp), 4)) |
|
191 return false; |
|
192 if (swap) |
|
193 swap_bytes<4> (&tmp); |
|
194 |
|
195 OCTAVE_LOCAL_BUFFER (char, ctmp1, tmp+1); |
|
196 is.read (ctmp1, tmp); |
|
197 nm = std::string (ctmp1); |
|
198 |
|
199 if (! is) |
|
200 return false; |
|
201 |
|
202 if (! is.read (X_CAST (char *, &tmp), 4)) |
|
203 return false; |
|
204 if (swap) |
|
205 swap_bytes<4> (&tmp); |
|
206 |
|
207 OCTAVE_LOCAL_BUFFER (char, ctmp2, tmp+1); |
|
208 is.read (ctmp2, tmp); |
|
209 iftext = std::string (ctmp2); |
|
210 |
|
211 if (! is) |
|
212 return false; |
|
213 |
|
214 octave_fcn_inline ftmp (iftext, ifargs, nm); |
|
215 fcn = ftmp.fcn; |
|
216 } |
|
217 return true; |
|
218 } |
|
219 |
|
220 #if defined (HAVE_HDF5) |
|
221 bool |
|
222 octave_fcn_inline::save_hdf5 (hid_t loc_id, const char *name, |
|
223 bool /* save_as_floats */) |
|
224 { |
|
225 hid_t group_hid = -1; |
|
226 group_hid = H5Gcreate (loc_id, name, 0); |
|
227 if (group_hid < 0 ) return false; |
|
228 |
|
229 size_t len = 0; |
4973
|
230 for (int i = 0; i < ifargs.length (); i++) |
|
231 if (len < ifargs(i).length ()) |
|
232 len = ifargs(i).length (); |
4972
|
233 |
|
234 hid_t space_hid = -1, data_hid = -1, type_hid = -1;; |
|
235 bool retval = true; |
|
236 |
|
237 // XXX FIXME XXX Is there a better way of saving string vectors, than a |
|
238 // null padded matrix? |
|
239 |
|
240 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2); |
|
241 |
|
242 // Octave uses column-major, while HDF5 uses row-major ordering |
4973
|
243 hdims[1] = ifargs.length (); |
4972
|
244 hdims[0] = len + 1; |
|
245 |
|
246 space_hid = H5Screate_simple (2, hdims, 0); |
|
247 if (space_hid < 0) |
|
248 { |
|
249 H5Gclose (group_hid); |
|
250 return false; |
|
251 } |
|
252 |
4973
|
253 data_hid = H5Dcreate (group_hid, "args", H5T_NATIVE_CHAR, space_hid, |
4972
|
254 H5P_DEFAULT); |
|
255 if (data_hid < 0) |
|
256 { |
|
257 H5Sclose (space_hid); |
|
258 H5Gclose (group_hid); |
|
259 return false; |
|
260 } |
|
261 |
4973
|
262 OCTAVE_LOCAL_BUFFER (char, s, ifargs.length () * (len + 1)); |
4972
|
263 |
|
264 // Save the args as a null teminated list |
4973
|
265 for (int i = 0; i < ifargs.length (); i++) |
4972
|
266 { |
4973
|
267 const char * cptr = ifargs(i).c_str (); |
|
268 for (size_t j = 0; j < ifargs(i).length (); j++) |
4972
|
269 s[i*(len+1)+j] = *cptr++; |
4973
|
270 s[ifargs(i).length ()] = '\0'; |
4972
|
271 } |
|
272 |
4973
|
273 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, |
4972
|
274 H5P_DEFAULT, s) >= 0; |
|
275 |
|
276 H5Dclose (data_hid); |
|
277 H5Sclose (space_hid); |
|
278 |
|
279 if (!retval) |
|
280 { |
|
281 H5Gclose (group_hid); |
|
282 return false; |
4973
|
283 } |
4972
|
284 |
|
285 // attach the type of the variable |
4973
|
286 type_hid = H5Tcopy (H5T_C_S1); |
4972
|
287 H5Tset_size (type_hid, nm.length () + 1); |
|
288 if (type_hid < 0) |
|
289 { |
|
290 H5Gclose (group_hid); |
|
291 return false; |
4973
|
292 } |
4972
|
293 |
|
294 hdims[0] = 0; |
|
295 space_hid = H5Screate_simple (0 , hdims, (hsize_t*) 0); |
|
296 if (space_hid < 0) |
|
297 { |
|
298 H5Tclose (type_hid); |
|
299 H5Gclose (group_hid); |
|
300 return false; |
4973
|
301 } |
4972
|
302 |
|
303 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, H5P_DEFAULT); |
4973
|
304 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
|
305 H5P_DEFAULT, (void*) nm.c_str ()) < 0) |
4972
|
306 { |
|
307 H5Sclose (space_hid); |
|
308 H5Tclose (type_hid); |
|
309 H5Gclose (group_hid); |
|
310 return false; |
4973
|
311 } |
4972
|
312 H5Dclose (data_hid); |
|
313 |
|
314 // attach the type of the variable |
|
315 H5Tset_size (type_hid, iftext.length () + 1); |
|
316 if (type_hid < 0) |
|
317 { |
|
318 H5Gclose (group_hid); |
|
319 return false; |
4973
|
320 } |
4972
|
321 |
4973
|
322 data_hid = H5Dcreate (group_hid, "iftext", type_hid, space_hid, |
4972
|
323 H5P_DEFAULT); |
4973
|
324 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, |
|
325 H5P_DEFAULT, (void*) iftext.c_str ()) < 0) |
4972
|
326 { |
|
327 H5Sclose (space_hid); |
|
328 H5Tclose (type_hid); |
|
329 H5Gclose (group_hid); |
|
330 return false; |
4973
|
331 } |
4972
|
332 |
|
333 H5Dclose (data_hid); |
|
334 |
|
335 return retval; |
|
336 } |
|
337 |
|
338 bool |
|
339 octave_fcn_inline::load_hdf5 (hid_t loc_id, const char *name, |
4973
|
340 bool /* have_h5giterate_bug */) |
4972
|
341 { |
|
342 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; |
|
343 hsize_t rank; |
|
344 int slen; |
|
345 |
|
346 group_hid = H5Gopen (loc_id, name); |
|
347 if (group_hid < 0 ) return false; |
|
348 |
|
349 data_hid = H5Dopen (group_hid, "args"); |
|
350 space_hid = H5Dget_space (data_hid); |
|
351 rank = H5Sget_simple_extent_ndims (space_hid); |
|
352 |
|
353 if (rank != 2) |
4973
|
354 { |
4972
|
355 H5Dclose (data_hid); |
|
356 H5Sclose (space_hid); |
|
357 H5Gclose (group_hid); |
|
358 return false; |
|
359 } |
|
360 |
|
361 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
|
362 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
|
363 |
|
364 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
|
365 |
4973
|
366 ifargs.resize (hdims[1]); |
4972
|
367 |
|
368 OCTAVE_LOCAL_BUFFER (char, s1, hdims[0] * hdims[1]); |
|
369 |
4973
|
370 if (H5Dread (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL, |
4972
|
371 H5P_DEFAULT, s1) < 0) |
4973
|
372 { |
4972
|
373 H5Dclose (data_hid); |
|
374 H5Sclose (space_hid); |
|
375 H5Gclose (group_hid); |
|
376 return false; |
|
377 } |
|
378 |
|
379 H5Dclose (data_hid); |
|
380 H5Sclose (space_hid); |
|
381 |
|
382 for (size_t i = 0; i < hdims[1]; i++) |
|
383 ifargs(i) = std::string (s1 + i*hdims[0]); |
|
384 |
|
385 data_hid = H5Dopen (group_hid, "nm"); |
|
386 |
|
387 if (data_hid < 0) |
|
388 { |
|
389 H5Gclose (group_hid); |
|
390 return false; |
|
391 } |
|
392 |
|
393 type_hid = H5Dget_type (data_hid); |
|
394 type_class_hid = H5Tget_class (type_hid); |
|
395 |
|
396 if (type_class_hid != H5T_STRING) |
|
397 { |
|
398 H5Tclose (type_hid); |
|
399 H5Dclose (data_hid); |
|
400 H5Gclose (group_hid); |
|
401 return false; |
|
402 } |
4973
|
403 |
4972
|
404 space_hid = H5Dget_space (data_hid); |
|
405 rank = H5Sget_simple_extent_ndims (space_hid); |
|
406 |
|
407 if (rank != 0) |
|
408 { |
|
409 H5Sclose (space_hid); |
|
410 H5Tclose (type_hid); |
|
411 H5Dclose (data_hid); |
|
412 H5Gclose (group_hid); |
|
413 return false; |
|
414 } |
|
415 |
|
416 slen = H5Tget_size (type_hid); |
|
417 if (slen < 0) |
|
418 { |
|
419 H5Sclose (space_hid); |
|
420 H5Tclose (type_hid); |
|
421 H5Dclose (data_hid); |
|
422 H5Gclose (group_hid); |
|
423 return false; |
|
424 } |
|
425 |
|
426 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen); |
|
427 |
|
428 // create datatype for (null-terminated) string to read into: |
|
429 st_id = H5Tcopy (H5T_C_S1); |
|
430 H5Tset_size (st_id, slen); |
|
431 |
4973
|
432 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
433 X_CAST (void *, nm_tmp)) < 0) |
4972
|
434 { |
|
435 H5Sclose (space_hid); |
|
436 H5Tclose (type_hid); |
|
437 H5Gclose (group_hid); |
|
438 return false; |
|
439 } |
|
440 H5Tclose (st_id); |
|
441 H5Dclose (data_hid); |
|
442 nm = nm_tmp; |
|
443 |
|
444 data_hid = H5Dopen (group_hid, "iftext"); |
|
445 |
|
446 if (data_hid < 0) |
|
447 { |
|
448 H5Gclose (group_hid); |
|
449 return false; |
|
450 } |
|
451 |
|
452 type_hid = H5Dget_type (data_hid); |
|
453 type_class_hid = H5Tget_class (type_hid); |
|
454 |
|
455 if (type_class_hid != H5T_STRING) |
|
456 { |
|
457 H5Tclose (type_hid); |
|
458 H5Dclose (data_hid); |
|
459 H5Gclose (group_hid); |
|
460 return false; |
|
461 } |
4973
|
462 |
4972
|
463 space_hid = H5Dget_space (data_hid); |
|
464 rank = H5Sget_simple_extent_ndims (space_hid); |
|
465 |
|
466 if (rank != 0) |
|
467 { |
|
468 H5Sclose (space_hid); |
|
469 H5Tclose (type_hid); |
|
470 H5Dclose (data_hid); |
|
471 H5Gclose (group_hid); |
|
472 return false; |
|
473 } |
|
474 |
|
475 slen = H5Tget_size (type_hid); |
|
476 if (slen < 0) |
|
477 { |
|
478 H5Sclose (space_hid); |
|
479 H5Tclose (type_hid); |
|
480 H5Dclose (data_hid); |
|
481 H5Gclose (group_hid); |
|
482 return false; |
|
483 } |
|
484 |
|
485 OCTAVE_LOCAL_BUFFER (char, iftext_tmp, slen); |
|
486 |
|
487 // create datatype for (null-terminated) string to read into: |
|
488 st_id = H5Tcopy (H5T_C_S1); |
|
489 H5Tset_size (st_id, slen); |
|
490 |
4973
|
491 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
492 X_CAST (void *, iftext_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 iftext = iftext_tmp; |
|
502 |
|
503 octave_fcn_inline ftmp (iftext, ifargs, nm); |
|
504 fcn = ftmp.fcn; |
|
505 |
|
506 return true; |
|
507 } |
|
508 #endif |
|
509 |
4933
|
510 void |
|
511 octave_fcn_inline::print (std::ostream& os, bool pr_as_read_syntax) const |
|
512 { |
|
513 print_raw (os, pr_as_read_syntax); |
|
514 newline (os); |
|
515 } |
|
516 |
|
517 void |
|
518 octave_fcn_inline::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
519 { |
|
520 OSSTREAM buf; |
|
521 |
|
522 if (nm.empty ()) |
|
523 buf << "f("; |
|
524 else |
|
525 buf << nm << "("; |
|
526 |
|
527 for (int i = 0; i < ifargs.length (); i++) |
|
528 { |
|
529 if (i) |
|
530 buf << ", "; |
|
531 |
|
532 buf << ifargs(i); |
|
533 } |
|
534 |
|
535 buf << ") = " << iftext << OSSTREAM_ENDS; |
|
536 |
|
537 octave_print_internal (os, OSSTREAM_STR (buf), pr_as_read_syntax, |
|
538 current_print_indent_level ()); |
|
539 OSSTREAM_FREEZE (buf); |
|
540 } |
|
541 |
|
542 octave_value |
|
543 octave_fcn_inline::convert_to_str_internal (bool, bool) const |
|
544 { |
|
545 return octave_value (fcn_text ()); |
|
546 } |
|
547 |
|
548 DEFUN (inline, args, , |
|
549 "-*- texinfo -*-\n\ |
|
550 @deftypefn {Built-in Function} {} inline (@var{str})\n\ |
|
551 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, ...)\n\ |
|
552 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\ |
|
553 Create an inline function from the character string @var{str}.\n\ |
|
554 If called with a single argument, the generated function is\n\ |
|
555 assumed to have a single argument and will be defined\n\ |
|
556 as the first isolated lower case character, except i or j.\n\ |
|
557 \n\ |
|
558 If the second and subsequent arguments are character strings,\n\ |
|
559 they are the names of the arguments of the function.\n\ |
|
560 \n\ |
|
561 If the second argument is an integer @var{n}, the arguments are\n\ |
|
562 @code{\"x\"}, @code{\"P1\"}, @dots{}, @code{\"P@var{N}\"}.\n\ |
|
563 @end deftypefn\n\ |
|
564 @seealso{argnames, formula, vectorize}") |
|
565 { |
|
566 octave_value retval; |
|
567 |
|
568 int nargin = args.length (); |
|
569 |
|
570 if (nargin > 0) |
|
571 { |
|
572 std::string fun = args(0).string_value (); |
|
573 |
|
574 if (! error_state) |
|
575 { |
|
576 string_vector fargs; |
|
577 |
|
578 if (nargin == 1) |
|
579 { |
|
580 fargs.resize (1); |
|
581 |
|
582 // Find the first isolated string as the argument of the |
|
583 // function. |
|
584 |
|
585 // XXX FIXME XXX -- use just "x" for now. |
|
586 fargs(0) = "x"; |
|
587 } |
|
588 else if (nargin == 2 && args(1).is_numeric_type ()) |
|
589 { |
|
590 int n = args(1).int_value (); |
|
591 |
|
592 if (! error_state) |
|
593 { |
|
594 if (n >= 0) |
|
595 { |
|
596 fargs.resize (n+1); |
|
597 |
|
598 fargs(0) = "x"; |
|
599 |
|
600 for (int i = 1; i < n+1; i++) |
|
601 { |
|
602 OSSTREAM buf; |
|
603 buf << "P" << i << OSSTREAM_ENDS; |
|
604 fargs(i) = OSSTREAM_STR (buf); |
|
605 OSSTREAM_FREEZE (buf); |
|
606 } |
|
607 } |
|
608 else |
|
609 { |
|
610 error ("inline: numeric argument must be nonnegative"); |
|
611 return retval; |
|
612 } |
|
613 } |
|
614 else |
|
615 { |
|
616 error ("inline: expecting second argument to be an integer"); |
|
617 return retval; |
|
618 } |
|
619 } |
|
620 else |
|
621 { |
|
622 fargs.resize (nargin - 1); |
|
623 |
|
624 for (int i = 1; i < nargin; i++) |
|
625 { |
|
626 std::string s = args(i).string_value (); |
|
627 |
|
628 if (! error_state) |
|
629 fargs(i-1) = s; |
|
630 else |
|
631 { |
|
632 error ("inline: expecting string arguments"); |
|
633 return retval; |
|
634 } |
|
635 } |
|
636 } |
|
637 |
|
638 retval = octave_value (new octave_fcn_inline (fun, fargs)); |
|
639 } |
|
640 else |
|
641 error ("inline: first argument must be a string"); |
|
642 } |
|
643 else |
|
644 print_usage ("inline"); |
|
645 |
|
646 return retval; |
|
647 } |
|
648 |
|
649 DEFUN (formula, args, , |
|
650 "-*- texinfo -*-\n\ |
|
651 @deftypefn {Built-in Function} {} formula (@var{fun})\n\ |
|
652 Return a character string representing the inline function @var{fun}.\n\ |
|
653 Note that @code{char (@var{fun})} is equivalent to\n\ |
|
654 @code{formula (@var{fun})}.\n\ |
|
655 @end deftypefn\n\ |
|
656 @seealso{argnames, inline, vectorize}") |
|
657 { |
|
658 octave_value retval; |
|
659 |
|
660 int nargin = args.length (); |
|
661 |
|
662 if (nargin == 1) |
|
663 { |
|
664 octave_fcn_inline* fn = args(0).fcn_inline_value (true); |
|
665 |
|
666 if (fn) |
|
667 retval = octave_value (fn->fcn_text ()); |
|
668 else |
|
669 error ("formula: must be an inline function"); |
|
670 } |
|
671 else |
|
672 print_usage ("formula"); |
|
673 |
|
674 return retval; |
|
675 } |
|
676 |
|
677 DEFUN (argnames, args, , |
|
678 "-*- texinfo -*-\n\ |
|
679 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ |
|
680 Return a cell array of character strings containing the names of\n\ |
|
681 the arguments of the inline function @var{fun}.\n\ |
|
682 @end deftypefn\n\ |
|
683 @seealso{argnames, inline, formula, vectorize}") |
|
684 { |
|
685 octave_value retval; |
|
686 |
|
687 int nargin = args.length (); |
|
688 |
|
689 if (nargin == 1) |
|
690 { |
|
691 octave_fcn_inline *fn = args(0).fcn_inline_value (true); |
|
692 |
|
693 if (fn) |
|
694 { |
|
695 string_vector t1 = fn->fcn_arg_names (); |
|
696 |
|
697 Cell t2 (dim_vector (t1.length (), 1)); |
|
698 |
|
699 for (int i = 0; i < t1.length (); i++) |
|
700 t2(i) = t1(i); |
|
701 |
|
702 retval = t2; |
|
703 } |
|
704 else |
|
705 error ("argnames: argument must be an inline function"); |
|
706 } |
|
707 else |
|
708 print_usage ("argnames"); |
|
709 |
|
710 return retval; |
|
711 } |
|
712 |
|
713 DEFUN (vectorize, args, , |
|
714 "-*- texinfo -*-\n\ |
|
715 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ |
|
716 Create a vectorized version of the inline function @var{fun}\n\ |
|
717 by replacing all occurrences of @code{*}, @code{/}, etc., with\n\ |
|
718 @code{.*}, @code{./}, etc.\n\ |
|
719 @end deftypefn\n\ |
|
720 @seealso{argnames, inline, formula, vectorize}") |
|
721 { |
|
722 octave_value retval; |
|
723 |
|
724 int nargin = args.length (); |
|
725 |
|
726 if (nargin == 1) |
|
727 { |
|
728 octave_fcn_inline* old = args(0).fcn_inline_value (true); |
|
729 |
|
730 if (old) |
|
731 { |
|
732 std::string old_func = old->fcn_text (); |
|
733 std::string new_func; |
|
734 |
|
735 size_t i = 0; |
|
736 |
|
737 while (i < old_func.length ()) |
|
738 { |
|
739 std::string t1 = old_func.substr (i, 1); |
|
740 |
|
741 if (t1 == "*" || t1 == "/" || t1 == "\\" || t1 == "^") |
|
742 { |
|
743 if (i && old_func.substr (i-1, 1) != ".") |
|
744 new_func.append ("."); |
|
745 |
|
746 // Special case for ** operator. |
4973
|
747 if (t1 == "*" && i < (old_func.length () - 1) |
4933
|
748 && old_func.substr (i+1, 1) == "*") |
|
749 { |
|
750 new_func.append ("*"); |
|
751 i++; |
|
752 } |
|
753 } |
|
754 new_func.append (t1); |
|
755 i++; |
|
756 } |
|
757 |
|
758 retval = octave_value (new octave_fcn_inline (new_func, old->fcn_arg_names ())); |
|
759 } |
|
760 else |
|
761 error ("vectorize: must be an inline function"); |
|
762 } |
|
763 else |
|
764 print_usage ("vectorize"); |
|
765 |
|
766 return retval; |
|
767 } |
|
768 |
|
769 /* |
|
770 ;;; Local Variables: *** |
|
771 ;;; mode: C++ *** |
|
772 ;;; End: *** |
|
773 */ |