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