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