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