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