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