Mercurial > octave-nkf
annotate src/ov-fcn-inline.cc @ 9881:b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
author | Kacper Kowalik |
---|---|
date | Sat, 28 Nov 2009 14:00:56 +0100 |
parents | fca0dc2fb042 |
children | ac69e6f4b33d |
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 | |
9881
b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
Kacper Kowalik
parents:
9038
diff
changeset
|
392 octave_fcn_inline::load_hdf5 (hid_t loc_id, const char *name) |
4972 | 393 { |
394 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; | |
395 hsize_t rank; | |
396 int slen; | |
397 | |
398 group_hid = H5Gopen (loc_id, name); | |
399 if (group_hid < 0 ) return false; | |
400 | |
401 data_hid = H5Dopen (group_hid, "args"); | |
402 space_hid = H5Dget_space (data_hid); | |
403 rank = H5Sget_simple_extent_ndims (space_hid); | |
404 | |
405 if (rank != 2) | |
4973 | 406 { |
4972 | 407 H5Dclose (data_hid); |
408 H5Sclose (space_hid); | |
409 H5Gclose (group_hid); | |
410 return false; | |
411 } | |
412 | |
413 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
414 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
415 | |
416 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
417 | |
4973 | 418 ifargs.resize (hdims[1]); |
4972 | 419 |
420 OCTAVE_LOCAL_BUFFER (char, s1, hdims[0] * hdims[1]); | |
421 | |
4973 | 422 if (H5Dread (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL, |
4972 | 423 H5P_DEFAULT, s1) < 0) |
4973 | 424 { |
4972 | 425 H5Dclose (data_hid); |
426 H5Sclose (space_hid); | |
427 H5Gclose (group_hid); | |
428 return false; | |
429 } | |
430 | |
431 H5Dclose (data_hid); | |
432 H5Sclose (space_hid); | |
433 | |
434 for (size_t i = 0; i < hdims[1]; i++) | |
435 ifargs(i) = std::string (s1 + i*hdims[0]); | |
436 | |
437 data_hid = H5Dopen (group_hid, "nm"); | |
438 | |
439 if (data_hid < 0) | |
440 { | |
441 H5Gclose (group_hid); | |
442 return false; | |
443 } | |
444 | |
445 type_hid = H5Dget_type (data_hid); | |
446 type_class_hid = H5Tget_class (type_hid); | |
447 | |
448 if (type_class_hid != H5T_STRING) | |
449 { | |
450 H5Tclose (type_hid); | |
451 H5Dclose (data_hid); | |
452 H5Gclose (group_hid); | |
453 return false; | |
454 } | |
4973 | 455 |
4972 | 456 space_hid = H5Dget_space (data_hid); |
457 rank = H5Sget_simple_extent_ndims (space_hid); | |
458 | |
459 if (rank != 0) | |
460 { | |
461 H5Sclose (space_hid); | |
462 H5Tclose (type_hid); | |
463 H5Dclose (data_hid); | |
464 H5Gclose (group_hid); | |
465 return false; | |
466 } | |
467 | |
468 slen = H5Tget_size (type_hid); | |
469 if (slen < 0) | |
470 { | |
471 H5Sclose (space_hid); | |
472 H5Tclose (type_hid); | |
473 H5Dclose (data_hid); | |
474 H5Gclose (group_hid); | |
475 return false; | |
476 } | |
477 | |
478 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen); | |
479 | |
480 // create datatype for (null-terminated) string to read into: | |
481 st_id = H5Tcopy (H5T_C_S1); | |
482 H5Tset_size (st_id, slen); | |
483 | |
5760 | 484 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, nm_tmp) < 0) |
4972 | 485 { |
486 H5Sclose (space_hid); | |
487 H5Tclose (type_hid); | |
488 H5Gclose (group_hid); | |
489 return false; | |
490 } | |
491 H5Tclose (st_id); | |
492 H5Dclose (data_hid); | |
493 nm = nm_tmp; | |
494 | |
495 data_hid = H5Dopen (group_hid, "iftext"); | |
496 | |
497 if (data_hid < 0) | |
498 { | |
499 H5Gclose (group_hid); | |
500 return false; | |
501 } | |
502 | |
503 type_hid = H5Dget_type (data_hid); | |
504 type_class_hid = H5Tget_class (type_hid); | |
505 | |
506 if (type_class_hid != H5T_STRING) | |
507 { | |
508 H5Tclose (type_hid); | |
509 H5Dclose (data_hid); | |
510 H5Gclose (group_hid); | |
511 return false; | |
512 } | |
4973 | 513 |
4972 | 514 space_hid = H5Dget_space (data_hid); |
515 rank = H5Sget_simple_extent_ndims (space_hid); | |
516 | |
517 if (rank != 0) | |
518 { | |
519 H5Sclose (space_hid); | |
520 H5Tclose (type_hid); | |
521 H5Dclose (data_hid); | |
522 H5Gclose (group_hid); | |
523 return false; | |
524 } | |
525 | |
526 slen = H5Tget_size (type_hid); | |
527 if (slen < 0) | |
528 { | |
529 H5Sclose (space_hid); | |
530 H5Tclose (type_hid); | |
531 H5Dclose (data_hid); | |
532 H5Gclose (group_hid); | |
533 return false; | |
534 } | |
535 | |
536 OCTAVE_LOCAL_BUFFER (char, iftext_tmp, slen); | |
537 | |
538 // create datatype for (null-terminated) string to read into: | |
539 st_id = H5Tcopy (H5T_C_S1); | |
540 H5Tset_size (st_id, slen); | |
541 | |
5760 | 542 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, iftext_tmp) < 0) |
4972 | 543 { |
544 H5Sclose (space_hid); | |
545 H5Tclose (type_hid); | |
546 H5Gclose (group_hid); | |
547 return false; | |
548 } | |
549 H5Tclose (st_id); | |
550 H5Dclose (data_hid); | |
551 iftext = iftext_tmp; | |
552 | |
553 octave_fcn_inline ftmp (iftext, ifargs, nm); | |
554 fcn = ftmp.fcn; | |
555 | |
556 return true; | |
557 } | |
558 #endif | |
559 | |
4933 | 560 void |
561 octave_fcn_inline::print (std::ostream& os, bool pr_as_read_syntax) const | |
562 { | |
563 print_raw (os, pr_as_read_syntax); | |
564 newline (os); | |
565 } | |
566 | |
567 void | |
568 octave_fcn_inline::print_raw (std::ostream& os, bool pr_as_read_syntax) const | |
569 { | |
5765 | 570 std::ostringstream buf; |
4933 | 571 |
572 if (nm.empty ()) | |
573 buf << "f("; | |
574 else | |
575 buf << nm << "("; | |
576 | |
577 for (int i = 0; i < ifargs.length (); i++) | |
578 { | |
579 if (i) | |
580 buf << ", "; | |
581 | |
582 buf << ifargs(i); | |
583 } | |
584 | |
5765 | 585 buf << ") = " << iftext; |
4933 | 586 |
5765 | 587 octave_print_internal (os, buf.str (), pr_as_read_syntax, |
4933 | 588 current_print_indent_level ()); |
589 } | |
590 | |
591 octave_value | |
5279 | 592 octave_fcn_inline::convert_to_str_internal (bool, bool, char type) const |
4933 | 593 { |
5279 | 594 return octave_value (fcn_text (), type); |
4933 | 595 } |
596 | |
6973 | 597 DEFUNX ("inline", Finline, args, , |
4933 | 598 "-*- texinfo -*-\n\ |
599 @deftypefn {Built-in Function} {} inline (@var{str})\n\ | |
6547 | 600 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{arg1}, @dots{})\n\ |
4933 | 601 @deftypefnx {Built-in Function} {} inline (@var{str}, @var{n})\n\ |
602 Create an inline function from the character string @var{str}.\n\ | |
5034 | 603 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
|
604 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
|
605 function arguments will then be in alphabetical order. It should\n\ |
5034 | 606 be noted that i, and j are ignored as arguments due to the\n\ |
607 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
|
608 constant. All arguments followed by a parenthesis are considered\n\ |
5034 | 609 to be functions.\n\ |
4933 | 610 \n\ |
611 If the second and subsequent arguments are character strings,\n\ | |
612 they are the names of the arguments of the function.\n\ | |
613 \n\ | |
614 If the second argument is an integer @var{n}, the arguments are\n\ | |
615 @code{\"x\"}, @code{\"P1\"}, @dots{}, @code{\"P@var{N}\"}.\n\ | |
5642 | 616 @seealso{argnames, formula, vectorize}\n\ |
617 @end deftypefn") | |
4933 | 618 { |
619 octave_value retval; | |
620 | |
621 int nargin = args.length (); | |
622 | |
623 if (nargin > 0) | |
624 { | |
625 std::string fun = args(0).string_value (); | |
626 | |
627 if (! error_state) | |
628 { | |
629 string_vector fargs; | |
630 | |
631 if (nargin == 1) | |
632 { | |
5034 | 633 bool is_arg = false; |
5037 | 634 bool in_string = false; |
5034 | 635 std::string tmp_arg; |
636 size_t i = 0; | |
637 | |
638 while (i < fun.length ()) | |
639 { | |
5037 | 640 bool terminate_arg = false; |
5034 | 641 char c = fun[i++]; |
4933 | 642 |
5037 | 643 if (in_string) |
644 { | |
645 if (c == '\'' || c == '\"') | |
646 in_string = false; | |
647 } | |
648 else if (c == '\'' || c == '\"') | |
649 { | |
650 in_string = true; | |
651 if (is_arg) | |
652 terminate_arg = true; | |
653 } | |
654 else if (! isalpha (c) && c != '_') | |
5034 | 655 if (! is_arg) |
656 continue; | |
657 else if (isdigit (c)) | |
5037 | 658 tmp_arg.append (1, c); |
5034 | 659 else |
660 { | |
5037 | 661 // Before we do anything remove trailing whitespaces. |
5034 | 662 while (i < fun.length () && isspace (c)) |
663 c = fun[i++]; | |
5037 | 664 |
5034 | 665 // Do we have a variable or a function? |
666 if (c != '(') | |
5037 | 667 terminate_arg = true; |
668 else | |
5034 | 669 { |
5037 | 670 tmp_arg = std::string (); |
671 is_arg = false; | |
5034 | 672 } |
673 } | |
674 else | |
675 { | |
676 tmp_arg.append (1, c); | |
677 is_arg = true; | |
5037 | 678 } |
5034 | 679 |
5037 | 680 if (terminate_arg || (i == fun.length () && is_arg)) |
681 { | |
682 bool have_arg = false; | |
683 | |
684 for (int j = 0; j < fargs.length (); j++) | |
685 if (tmp_arg == fargs (j)) | |
686 { | |
687 have_arg = true; | |
688 break; | |
689 } | |
5034 | 690 |
7764
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
691 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
|
692 tmp_arg != "NaN" && tmp_arg != "nan" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
693 tmp_arg != "Inf" && tmp_arg != "inf" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
694 tmp_arg != "NA" && tmp_arg != "pi" && |
aaa808ed5d53
Also ignore other constants in Finline
David Bateman <dbateman@free.fr>
parents:
7394
diff
changeset
|
695 tmp_arg != "eps") |
5037 | 696 fargs.append (tmp_arg); |
697 | |
698 tmp_arg = std::string (); | |
699 is_arg = false; | |
5022 | 700 } |
701 } | |
5034 | 702 |
5037 | 703 // Sort the arguments into ascii order. |
8503
8ba2ee57c594
remove qsort in favor of sort
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
704 fargs.sort (); |
4933 | 705 } |
706 else if (nargin == 2 && args(1).is_numeric_type ()) | |
707 { | |
708 int n = args(1).int_value (); | |
709 | |
710 if (! error_state) | |
711 { | |
712 if (n >= 0) | |
713 { | |
714 fargs.resize (n+1); | |
715 | |
716 fargs(0) = "x"; | |
717 | |
718 for (int i = 1; i < n+1; i++) | |
719 { | |
5765 | 720 std::ostringstream buf; |
721 buf << "P" << i; | |
722 fargs(i) = buf.str (); | |
4933 | 723 } |
724 } | |
725 else | |
726 { | |
727 error ("inline: numeric argument must be nonnegative"); | |
728 return retval; | |
729 } | |
730 } | |
731 else | |
732 { | |
733 error ("inline: expecting second argument to be an integer"); | |
734 return retval; | |
735 } | |
736 } | |
737 else | |
738 { | |
739 fargs.resize (nargin - 1); | |
740 | |
741 for (int i = 1; i < nargin; i++) | |
742 { | |
743 std::string s = args(i).string_value (); | |
744 | |
745 if (! error_state) | |
746 fargs(i-1) = s; | |
747 else | |
748 { | |
749 error ("inline: expecting string arguments"); | |
750 return retval; | |
751 } | |
752 } | |
753 } | |
754 | |
755 retval = octave_value (new octave_fcn_inline (fun, fargs)); | |
756 } | |
757 else | |
758 error ("inline: first argument must be a string"); | |
759 } | |
760 else | |
5823 | 761 print_usage (); |
4933 | 762 |
763 return retval; | |
764 } | |
765 | |
7394 | 766 /* |
767 %!shared fn | |
768 %! fn = inline ("x.^2 + 1","x"); | |
769 %!assert (feval (fn, 6), 37) | |
770 %!assert (fn (6), 37) | |
771 */ | |
772 | |
4933 | 773 DEFUN (formula, args, , |
774 "-*- texinfo -*-\n\ | |
775 @deftypefn {Built-in Function} {} formula (@var{fun})\n\ | |
776 Return a character string representing the inline function @var{fun}.\n\ | |
777 Note that @code{char (@var{fun})} is equivalent to\n\ | |
778 @code{formula (@var{fun})}.\n\ | |
5642 | 779 @seealso{argnames, inline, vectorize}\n\ |
780 @end deftypefn") | |
4933 | 781 { |
782 octave_value retval; | |
783 | |
784 int nargin = args.length (); | |
785 | |
786 if (nargin == 1) | |
787 { | |
788 octave_fcn_inline* fn = args(0).fcn_inline_value (true); | |
789 | |
790 if (fn) | |
791 retval = octave_value (fn->fcn_text ()); | |
792 else | |
793 error ("formula: must be an inline function"); | |
794 } | |
795 else | |
5823 | 796 print_usage (); |
4933 | 797 |
798 return retval; | |
799 } | |
800 | |
801 DEFUN (argnames, args, , | |
802 "-*- texinfo -*-\n\ | |
803 @deftypefn {Built-in Function} {} argnames (@var{fun})\n\ | |
804 Return a cell array of character strings containing the names of\n\ | |
805 the arguments of the inline function @var{fun}.\n\ | |
6529 | 806 @seealso{inline, formula, vectorize}\n\ |
5642 | 807 @end deftypefn") |
4933 | 808 { |
809 octave_value retval; | |
810 | |
811 int nargin = args.length (); | |
812 | |
813 if (nargin == 1) | |
814 { | |
815 octave_fcn_inline *fn = args(0).fcn_inline_value (true); | |
816 | |
817 if (fn) | |
818 { | |
819 string_vector t1 = fn->fcn_arg_names (); | |
820 | |
821 Cell t2 (dim_vector (t1.length (), 1)); | |
822 | |
823 for (int i = 0; i < t1.length (); i++) | |
824 t2(i) = t1(i); | |
825 | |
826 retval = t2; | |
827 } | |
828 else | |
829 error ("argnames: argument must be an inline function"); | |
830 } | |
831 else | |
5823 | 832 print_usage (); |
4933 | 833 |
834 return retval; | |
835 } | |
836 | |
837 DEFUN (vectorize, args, , | |
838 "-*- texinfo -*-\n\ | |
6635 | 839 @deftypefn {Built-in Function} {} vectorize (@var{fun})\n\ |
4933 | 840 Create a vectorized version of the inline function @var{fun}\n\ |
841 by replacing all occurrences of @code{*}, @code{/}, etc., with\n\ | |
842 @code{.*}, @code{./}, etc.\n\ | |
5642 | 843 @end deftypefn") |
4933 | 844 { |
845 octave_value retval; | |
846 | |
847 int nargin = args.length (); | |
848 | |
849 if (nargin == 1) | |
850 { | |
5409 | 851 std::string old_func; |
852 octave_fcn_inline* old = 0; | |
853 bool func_is_string = true; | |
4933 | 854 |
5409 | 855 if (args(0).is_string ()) |
856 old_func = args(0).string_value (); | |
857 else | |
4933 | 858 { |
5409 | 859 old = args(0).fcn_inline_value (true); |
860 func_is_string = false; | |
861 | |
862 if (old) | |
863 old_func = old->fcn_text (); | |
864 else | |
865 error ("vectorize: must be a string or inline function"); | |
866 } | |
867 | |
868 if (! error_state) | |
869 { | |
4933 | 870 std::string new_func; |
871 size_t i = 0; | |
872 | |
873 while (i < old_func.length ()) | |
874 { | |
875 std::string t1 = old_func.substr (i, 1); | |
876 | |
877 if (t1 == "*" || t1 == "/" || t1 == "\\" || t1 == "^") | |
878 { | |
879 if (i && old_func.substr (i-1, 1) != ".") | |
880 new_func.append ("."); | |
881 | |
882 // Special case for ** operator. | |
4973 | 883 if (t1 == "*" && i < (old_func.length () - 1) |
4933 | 884 && old_func.substr (i+1, 1) == "*") |
885 { | |
886 new_func.append ("*"); | |
887 i++; | |
888 } | |
889 } | |
890 new_func.append (t1); | |
891 i++; | |
892 } | |
893 | |
5409 | 894 if (func_is_string) |
895 retval = octave_value (new_func); | |
896 else | |
897 retval = octave_value (new octave_fcn_inline | |
898 (new_func, old->fcn_arg_names ())); | |
4933 | 899 } |
900 } | |
901 else | |
5823 | 902 print_usage (); |
4933 | 903 |
904 return retval; | |
905 } | |
906 | |
907 /* | |
908 ;;; Local Variables: *** | |
909 ;;; mode: C++ *** | |
910 ;;; End: *** | |
911 */ |