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