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