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> |
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 (); |
4972
|
160 os.write (X_CAST (char *, &tmp), 4); |
4973
|
161 for (int i = 0; i < ifargs.length (); i++) |
4972
|
162 { |
4973
|
163 tmp = ifargs(i).length (); |
4972
|
164 os.write (X_CAST (char *, &tmp), 4); |
4973
|
165 os.write (ifargs(i).c_str (), ifargs(i).length ()); |
4972
|
166 } |
4973
|
167 tmp = nm.length (); |
4972
|
168 os.write (X_CAST (char *, &tmp), 4); |
4973
|
169 os.write (nm.c_str (), nm.length ()); |
|
170 tmp = iftext.length (); |
4972
|
171 os.write (X_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; |
|
181 if (! is.read (X_CAST (char *, &nargs), 4)) |
|
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 { |
|
194 if (! is.read (X_CAST (char *, &tmp), 4)) |
|
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 |
|
207 if (! is.read (X_CAST (char *, &tmp), 4)) |
|
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 |
|
219 if (! is.read (X_CAST (char *, &tmp), 4)) |
|
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; |
|
312 space_hid = H5Screate_simple (0 , hdims, (hsize_t*) 0); |
|
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, |
|
322 H5P_DEFAULT, (void*) 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, |
|
342 H5P_DEFAULT, (void*) 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 |
4973
|
452 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
453 X_CAST (void *, nm_tmp)) < 0) |
4972
|
454 { |
|
455 H5Sclose (space_hid); |
|
456 H5Tclose (type_hid); |
|
457 H5Gclose (group_hid); |
|
458 return false; |
|
459 } |
|
460 H5Tclose (st_id); |
|
461 H5Dclose (data_hid); |
|
462 nm = nm_tmp; |
|
463 |
|
464 data_hid = H5Dopen (group_hid, "iftext"); |
|
465 |
|
466 if (data_hid < 0) |
|
467 { |
|
468 H5Gclose (group_hid); |
|
469 return false; |
|
470 } |
|
471 |
|
472 type_hid = H5Dget_type (data_hid); |
|
473 type_class_hid = H5Tget_class (type_hid); |
|
474 |
|
475 if (type_class_hid != H5T_STRING) |
|
476 { |
|
477 H5Tclose (type_hid); |
|
478 H5Dclose (data_hid); |
|
479 H5Gclose (group_hid); |
|
480 return false; |
|
481 } |
4973
|
482 |
4972
|
483 space_hid = H5Dget_space (data_hid); |
|
484 rank = H5Sget_simple_extent_ndims (space_hid); |
|
485 |
|
486 if (rank != 0) |
|
487 { |
|
488 H5Sclose (space_hid); |
|
489 H5Tclose (type_hid); |
|
490 H5Dclose (data_hid); |
|
491 H5Gclose (group_hid); |
|
492 return false; |
|
493 } |
|
494 |
|
495 slen = H5Tget_size (type_hid); |
|
496 if (slen < 0) |
|
497 { |
|
498 H5Sclose (space_hid); |
|
499 H5Tclose (type_hid); |
|
500 H5Dclose (data_hid); |
|
501 H5Gclose (group_hid); |
|
502 return false; |
|
503 } |
|
504 |
|
505 OCTAVE_LOCAL_BUFFER (char, iftext_tmp, slen); |
|
506 |
|
507 // create datatype for (null-terminated) string to read into: |
|
508 st_id = H5Tcopy (H5T_C_S1); |
|
509 H5Tset_size (st_id, slen); |
|
510 |
4973
|
511 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
|
512 X_CAST (void *, iftext_tmp)) < 0) |
4972
|
513 { |
|
514 H5Sclose (space_hid); |
|
515 H5Tclose (type_hid); |
|
516 H5Gclose (group_hid); |
|
517 return false; |
|
518 } |
|
519 H5Tclose (st_id); |
|
520 H5Dclose (data_hid); |
|
521 iftext = iftext_tmp; |
|
522 |
|
523 octave_fcn_inline ftmp (iftext, ifargs, nm); |
|
524 fcn = ftmp.fcn; |
|
525 |
|
526 return true; |
|
527 } |
|
528 #endif |
|
529 |
4933
|
530 void |
|
531 octave_fcn_inline::print (std::ostream& os, bool pr_as_read_syntax) const |
|
532 { |
|
533 print_raw (os, pr_as_read_syntax); |
|
534 newline (os); |
|
535 } |
|
536 |
|
537 void |
|
538 octave_fcn_inline::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
539 { |
|
540 OSSTREAM buf; |
|
541 |
|
542 if (nm.empty ()) |
|
543 buf << "f("; |
|
544 else |
|
545 buf << nm << "("; |
|
546 |
|
547 for (int i = 0; i < ifargs.length (); i++) |
|
548 { |
|
549 if (i) |
|
550 buf << ", "; |
|
551 |
|
552 buf << ifargs(i); |
|
553 } |
|
554 |
|
555 buf << ") = " << iftext << OSSTREAM_ENDS; |
|
556 |
|
557 octave_print_internal (os, OSSTREAM_STR (buf), pr_as_read_syntax, |
|
558 current_print_indent_level ()); |
|
559 OSSTREAM_FREEZE (buf); |
|
560 } |
|
561 |
|
562 octave_value |
|
563 octave_fcn_inline::convert_to_str_internal (bool, bool) const |
|
564 { |
|
565 return octave_value (fcn_text ()); |
|
566 } |
|
567 |
|
568 DEFUN (inline, args, , |
|
569 "-*- texinfo -*-\n\ |
|
570 @deftypefn {Built-in Function} {} inline (@var{str})\n\ |
|
571 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, ...)\n\ |
|
572 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\ |
|
573 Create an inline function from the character string @var{str}.\n\ |
5034
|
574 If called with a single argument, the arguments of the generated\n\ |
|
575 function are extracted from the function itself. The generated\n\ |
|
576 function arguments will then be in alphabetical order. It should\n\ |
|
577 be noted that i, and j are ignored as arguments due to the\n\ |
|
578 ambiguity between their use as a variable or their use as an inbuilt\n\ |
|
579 constant. All arguments followed by a parentheses are considered\n\ |
|
580 to be functions.\n\ |
4933
|
581 \n\ |
|
582 If the second and subsequent arguments are character strings,\n\ |
|
583 they are the names of the arguments of the function.\n\ |
|
584 \n\ |
|
585 If the second argument is an integer @var{n}, the arguments are\n\ |
|
586 @code{\"x\"}, @code{\"P1\"}, @dots{}, @code{\"P@var{N}\"}.\n\ |
|
587 @end deftypefn\n\ |
|
588 @seealso{argnames, formula, vectorize}") |
|
589 { |
|
590 octave_value retval; |
|
591 |
|
592 int nargin = args.length (); |
|
593 |
|
594 if (nargin > 0) |
|
595 { |
|
596 std::string fun = args(0).string_value (); |
|
597 |
|
598 if (! error_state) |
|
599 { |
|
600 string_vector fargs; |
|
601 |
|
602 if (nargin == 1) |
|
603 { |
5034
|
604 bool is_arg = false; |
5037
|
605 bool in_string = false; |
5034
|
606 std::string tmp_arg; |
|
607 size_t i = 0; |
|
608 |
|
609 while (i < fun.length ()) |
|
610 { |
5037
|
611 bool terminate_arg = false; |
5034
|
612 char c = fun[i++]; |
4933
|
613 |
5037
|
614 if (in_string) |
|
615 { |
|
616 if (c == '\'' || c == '\"') |
|
617 in_string = false; |
|
618 } |
|
619 else if (c == '\'' || c == '\"') |
|
620 { |
|
621 in_string = true; |
|
622 if (is_arg) |
|
623 terminate_arg = true; |
|
624 } |
|
625 else if (! isalpha (c) && c != '_') |
5034
|
626 if (! is_arg) |
|
627 continue; |
|
628 else if (isdigit (c)) |
5037
|
629 tmp_arg.append (1, c); |
5034
|
630 else |
|
631 { |
5037
|
632 // Before we do anything remove trailing whitespaces. |
5034
|
633 while (i < fun.length () && isspace (c)) |
|
634 c = fun[i++]; |
5037
|
635 |
5034
|
636 // Do we have a variable or a function? |
|
637 if (c != '(') |
5037
|
638 terminate_arg = true; |
|
639 else |
5034
|
640 { |
5037
|
641 tmp_arg = std::string (); |
|
642 is_arg = false; |
5034
|
643 } |
|
644 } |
|
645 else |
|
646 { |
|
647 tmp_arg.append (1, c); |
|
648 is_arg = true; |
5037
|
649 } |
5034
|
650 |
5037
|
651 if (terminate_arg || (i == fun.length () && is_arg)) |
|
652 { |
|
653 bool have_arg = false; |
|
654 |
|
655 for (int j = 0; j < fargs.length (); j++) |
|
656 if (tmp_arg == fargs (j)) |
|
657 { |
|
658 have_arg = true; |
|
659 break; |
|
660 } |
5034
|
661 |
5037
|
662 if (! have_arg && tmp_arg != "i" && tmp_arg != "j") |
|
663 fargs.append (tmp_arg); |
|
664 |
|
665 tmp_arg = std::string (); |
|
666 is_arg = false; |
5022
|
667 } |
|
668 } |
5034
|
669 |
5037
|
670 // Sort the arguments into ascii order. |
5034
|
671 fargs.qsort (); |
4933
|
672 } |
|
673 else if (nargin == 2 && args(1).is_numeric_type ()) |
|
674 { |
|
675 int n = args(1).int_value (); |
|
676 |
|
677 if (! error_state) |
|
678 { |
|
679 if (n >= 0) |
|
680 { |
|
681 fargs.resize (n+1); |
|
682 |
|
683 fargs(0) = "x"; |
|
684 |
|
685 for (int i = 1; i < n+1; i++) |
|
686 { |
|
687 OSSTREAM buf; |
|
688 buf << "P" << i << OSSTREAM_ENDS; |
|
689 fargs(i) = OSSTREAM_STR (buf); |
|
690 OSSTREAM_FREEZE (buf); |
|
691 } |
|
692 } |
|
693 else |
|
694 { |
|
695 error ("inline: numeric argument must be nonnegative"); |
|
696 return retval; |
|
697 } |
|
698 } |
|
699 else |
|
700 { |
|
701 error ("inline: expecting second argument to be an integer"); |
|
702 return retval; |
|
703 } |
|
704 } |
|
705 else |
|
706 { |
|
707 fargs.resize (nargin - 1); |
|
708 |
|
709 for (int i = 1; i < nargin; i++) |
|
710 { |
|
711 std::string s = args(i).string_value (); |
|
712 |
|
713 if (! error_state) |
|
714 fargs(i-1) = s; |
|
715 else |
|
716 { |
|
717 error ("inline: expecting string arguments"); |
|
718 return retval; |
|
719 } |
|
720 } |
|
721 } |
|
722 |
|
723 retval = octave_value (new octave_fcn_inline (fun, fargs)); |
|
724 } |
|
725 else |
|
726 error ("inline: first argument must be a string"); |
|
727 } |
|
728 else |
|
729 print_usage ("inline"); |
|
730 |
|
731 return retval; |
|
732 } |
|
733 |
|
734 DEFUN (formula, args, , |
|
735 "-*- texinfo -*-\n\ |
|
736 @deftypefn {Built-in Function} {} formula (@var{fun})\n\ |
|
737 Return a character string representing the inline function @var{fun}.\n\ |
|
738 Note that @code{char (@var{fun})} is equivalent to\n\ |
|
739 @code{formula (@var{fun})}.\n\ |
|
740 @end deftypefn\n\ |
|
741 @seealso{argnames, inline, vectorize}") |
|
742 { |
|
743 octave_value retval; |
|
744 |
|
745 int nargin = args.length (); |
|
746 |
|
747 if (nargin == 1) |
|
748 { |
|
749 octave_fcn_inline* fn = args(0).fcn_inline_value (true); |
|
750 |
|
751 if (fn) |
|
752 retval = octave_value (fn->fcn_text ()); |
|
753 else |
|
754 error ("formula: must be an inline function"); |
|
755 } |
|
756 else |
|
757 print_usage ("formula"); |
|
758 |
|
759 return retval; |
|
760 } |
|
761 |
|
762 DEFUN (argnames, args, , |
|
763 "-*- texinfo -*-\n\ |
|
764 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ |
|
765 Return a cell array of character strings containing the names of\n\ |
|
766 the arguments of the inline function @var{fun}.\n\ |
|
767 @end deftypefn\n\ |
|
768 @seealso{argnames, inline, formula, vectorize}") |
|
769 { |
|
770 octave_value retval; |
|
771 |
|
772 int nargin = args.length (); |
|
773 |
|
774 if (nargin == 1) |
|
775 { |
|
776 octave_fcn_inline *fn = args(0).fcn_inline_value (true); |
|
777 |
|
778 if (fn) |
|
779 { |
|
780 string_vector t1 = fn->fcn_arg_names (); |
|
781 |
|
782 Cell t2 (dim_vector (t1.length (), 1)); |
|
783 |
|
784 for (int i = 0; i < t1.length (); i++) |
|
785 t2(i) = t1(i); |
|
786 |
|
787 retval = t2; |
|
788 } |
|
789 else |
|
790 error ("argnames: argument must be an inline function"); |
|
791 } |
|
792 else |
|
793 print_usage ("argnames"); |
|
794 |
|
795 return retval; |
|
796 } |
|
797 |
|
798 DEFUN (vectorize, args, , |
|
799 "-*- texinfo -*-\n\ |
|
800 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ |
|
801 Create a vectorized version of the inline function @var{fun}\n\ |
|
802 by replacing all occurrences of @code{*}, @code{/}, etc., with\n\ |
|
803 @code{.*}, @code{./}, etc.\n\ |
|
804 @end deftypefn\n\ |
|
805 @seealso{argnames, inline, formula, vectorize}") |
|
806 { |
|
807 octave_value retval; |
|
808 |
|
809 int nargin = args.length (); |
|
810 |
|
811 if (nargin == 1) |
|
812 { |
|
813 octave_fcn_inline* old = args(0).fcn_inline_value (true); |
|
814 |
|
815 if (old) |
|
816 { |
|
817 std::string old_func = old->fcn_text (); |
|
818 std::string new_func; |
|
819 |
|
820 size_t i = 0; |
|
821 |
|
822 while (i < old_func.length ()) |
|
823 { |
|
824 std::string t1 = old_func.substr (i, 1); |
|
825 |
|
826 if (t1 == "*" || t1 == "/" || t1 == "\\" || t1 == "^") |
|
827 { |
|
828 if (i && old_func.substr (i-1, 1) != ".") |
|
829 new_func.append ("."); |
|
830 |
|
831 // Special case for ** operator. |
4973
|
832 if (t1 == "*" && i < (old_func.length () - 1) |
4933
|
833 && old_func.substr (i+1, 1) == "*") |
|
834 { |
|
835 new_func.append ("*"); |
|
836 i++; |
|
837 } |
|
838 } |
|
839 new_func.append (t1); |
|
840 i++; |
|
841 } |
|
842 |
|
843 retval = octave_value (new octave_fcn_inline (new_func, old->fcn_arg_names ())); |
|
844 } |
|
845 else |
|
846 error ("vectorize: must be an inline function"); |
|
847 } |
|
848 else |
|
849 print_usage ("vectorize"); |
|
850 |
|
851 return retval; |
|
852 } |
|
853 |
|
854 /* |
|
855 ;;; Local Variables: *** |
|
856 ;;; mode: C++ *** |
|
857 ;;; End: *** |
|
858 */ |