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