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