Mercurial > octave-nkf
comparison src/ov-fcn-handle.cc @ 4988:352d228d409b
[project @ 2004-09-11 13:05:38 by jwe]
author | jwe |
---|---|
date | Sat, 11 Sep 2004 13:05:39 +0000 |
parents | 003bbf6c13d8 |
children | 19b73a80e1d9 |
comparison
equal
deleted
inserted
replaced
4987:bad4898b468e | 4988:352d228d409b |
---|---|
43 #include "pt-stmt.h" | 43 #include "pt-stmt.h" |
44 #include "pt-cmd.h" | 44 #include "pt-cmd.h" |
45 #include "pt-exp.h" | 45 #include "pt-exp.h" |
46 #include "pt-assign.h" | 46 #include "pt-assign.h" |
47 #include "variables.h" | 47 #include "variables.h" |
48 #include "parse.h" | |
49 | |
50 #include "byte-swap.h" | |
51 #include "ls-oct-ascii.h" | |
52 #include "ls-hdf5.h" | |
53 #include "ls-utils.h" | |
48 | 54 |
49 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); | 55 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); |
50 | 56 |
51 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, | 57 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, |
52 "function handle", | 58 "function handle", |
88 if (idx.size () > 1) | 94 if (idx.size () > 1) |
89 retval = retval(0).next_subsref (type, idx); | 95 retval = retval(0).next_subsref (type, idx); |
90 | 96 |
91 return retval; | 97 return retval; |
92 } | 98 } |
99 | |
100 bool | |
101 octave_fcn_handle::save_ascii (std::ostream& os, bool&, bool) | |
102 { | |
103 os << nm << "\n"; | |
104 if (nm == "@<anonymous>") | |
105 { | |
106 OSSTREAM buf; | |
107 print_raw (buf, true); | |
108 os << OSSTREAM_STR (buf) << "\n" << OSSTREAM_ENDS; | |
109 OSSTREAM_FREEZE (buf); | |
110 } | |
111 | |
112 return true; | |
113 } | |
114 | |
115 bool | |
116 octave_fcn_handle::load_ascii (std::istream& is) | |
117 { | |
118 is >> nm; | |
119 if (nm == "@<anonymous>") | |
120 { | |
121 char c; | |
122 OSSTREAM buf; | |
123 | |
124 // Skip preceeding newline(s) | |
125 while (is.get (c) && c == '\n'); | |
126 | |
127 if (is) | |
128 { | |
129 buf << c; | |
130 | |
131 // Get a line of text whitespace characters included, leaving | |
132 // newline in the stream | |
133 while (is.peek () != '\n') | |
134 { | |
135 is.get (c); | |
136 if (! is) | |
137 break; | |
138 buf << c; | |
139 } | |
140 } | |
141 | |
142 buf << OSSTREAM_ENDS; | |
143 | |
144 int parse_status; | |
145 octave_value anon_fcn_handle = eval_string (OSSTREAM_C_STR (buf), | |
146 true, parse_status); | |
147 OSSTREAM_FREEZE (buf); | |
148 | |
149 fcn = anon_fcn_handle.fcn_handle_value () -> fcn; | |
150 } | |
151 else | |
152 { | |
153 fcn = lookup_function (nm); | |
154 if (! fcn.is_function ()) | |
155 return false; | |
156 } | |
157 | |
158 return true; | |
159 } | |
160 | |
161 bool | |
162 octave_fcn_handle::save_binary (std::ostream& os, bool&) | |
163 { | |
164 FOUR_BYTE_INT tmp = nm.length (); | |
165 os.write (X_CAST (char *, &tmp), 4); | |
166 os.write (nm.c_str (), nm.length ()); | |
167 if (nm == "@<anonymous>") | |
168 { | |
169 OSSTREAM buf; | |
170 print_raw (buf, true); | |
171 std::string stmp = OSSTREAM_STR (buf); | |
172 OSSTREAM_FREEZE (buf); | |
173 tmp = stmp.length (); | |
174 os.write (X_CAST (char *, &tmp), 4); | |
175 os.write (stmp.c_str (), stmp.length ()); | |
176 } | |
177 return true; | |
178 } | |
179 | |
180 bool | |
181 octave_fcn_handle::load_binary (std::istream& is, bool swap, | |
182 oct_mach_info::float_format) | |
183 { | |
184 FOUR_BYTE_INT tmp; | |
185 if (! is.read (X_CAST (char *, &tmp), 4)) | |
186 return false; | |
187 if (swap) | |
188 swap_bytes<4> (&tmp); | |
189 | |
190 OCTAVE_LOCAL_BUFFER (char, ctmp1, tmp+1); | |
191 is.read (ctmp1, tmp); | |
192 nm = std::string (ctmp1); | |
193 | |
194 if (! is) | |
195 return false; | |
196 | |
197 if (nm == "@<anonymous>") | |
198 { | |
199 if (! is.read (X_CAST (char *, &tmp), 4)) | |
200 return false; | |
201 if (swap) | |
202 swap_bytes<4> (&tmp); | |
203 | |
204 OCTAVE_LOCAL_BUFFER (char, ctmp2, tmp+1); | |
205 is.read (ctmp2, tmp); | |
206 | |
207 int parse_status; | |
208 octave_value anon_fcn_handle = eval_string (ctmp2, true, parse_status); | |
209 | |
210 fcn = anon_fcn_handle.fcn_handle_value () -> fcn; | |
211 } | |
212 else | |
213 { | |
214 fcn = lookup_function (nm); | |
215 if (! fcn.is_function ()) | |
216 return false; | |
217 } | |
218 return true; | |
219 } | |
220 | |
221 #if defined (HAVE_HDF5) | |
222 bool | |
223 octave_fcn_handle::save_hdf5 (hid_t loc_id, const char *name, | |
224 bool /* save_as_floats */) | |
225 { | |
226 hid_t group_hid = -1; | |
227 group_hid = H5Gcreate (loc_id, name, 0); | |
228 if (group_hid < 0 ) return false; | |
229 | |
230 hid_t space_hid = -1, data_hid = -1, type_hid = -1;; | |
231 bool retval = true; | |
232 | |
233 // attach the type of the variable | |
234 type_hid = H5Tcopy (H5T_C_S1); | |
235 H5Tset_size (type_hid, nm.length () + 1); | |
236 if (type_hid < 0) | |
237 { | |
238 H5Gclose (group_hid); | |
239 return false; | |
240 } | |
241 | |
242 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2); | |
243 hdims[0] = 0; | |
244 hdims[1] = 0; | |
245 space_hid = H5Screate_simple (0 , hdims, (hsize_t*) 0); | |
246 if (space_hid < 0) | |
247 { | |
248 H5Tclose (type_hid); | |
249 H5Gclose (group_hid); | |
250 return false; | |
251 } | |
252 | |
253 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, H5P_DEFAULT); | |
254 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, | |
255 H5P_DEFAULT, (void*) nm.c_str ()) < 0) | |
256 { | |
257 H5Sclose (space_hid); | |
258 H5Tclose (type_hid); | |
259 H5Gclose (group_hid); | |
260 return false; | |
261 } | |
262 H5Dclose (data_hid); | |
263 | |
264 if (nm == "@<anonymous>") | |
265 { | |
266 OSSTREAM buf; | |
267 print_raw (buf, true); | |
268 std::string stmp = OSSTREAM_STR (buf); | |
269 OSSTREAM_FREEZE (buf); | |
270 | |
271 // attach the type of the variable | |
272 H5Tset_size (type_hid, stmp.length () + 1); | |
273 if (type_hid < 0) | |
274 { | |
275 H5Gclose (group_hid); | |
276 return false; | |
277 } | |
278 | |
279 data_hid = H5Dcreate (group_hid, "fcn", type_hid, space_hid, | |
280 H5P_DEFAULT); | |
281 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, | |
282 H5P_DEFAULT, (void*) stmp.c_str ()) < 0) | |
283 { | |
284 H5Sclose (space_hid); | |
285 H5Tclose (type_hid); | |
286 H5Gclose (group_hid); | |
287 return false; | |
288 } | |
289 | |
290 H5Dclose (data_hid); | |
291 } | |
292 | |
293 H5Sclose (space_hid); | |
294 H5Tclose (type_hid); | |
295 H5Gclose (group_hid); | |
296 | |
297 return retval; | |
298 } | |
299 | |
300 bool | |
301 octave_fcn_handle::load_hdf5 (hid_t loc_id, const char *name, | |
302 bool /* have_h5giterate_bug */) | |
303 { | |
304 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; | |
305 hsize_t rank; | |
306 int slen; | |
307 | |
308 group_hid = H5Gopen (loc_id, name); | |
309 if (group_hid < 0 ) return false; | |
310 | |
311 data_hid = H5Dopen (group_hid, "nm"); | |
312 | |
313 if (data_hid < 0) | |
314 { | |
315 H5Gclose (group_hid); | |
316 return false; | |
317 } | |
318 | |
319 type_hid = H5Dget_type (data_hid); | |
320 type_class_hid = H5Tget_class (type_hid); | |
321 | |
322 if (type_class_hid != H5T_STRING) | |
323 { | |
324 H5Tclose (type_hid); | |
325 H5Dclose (data_hid); | |
326 H5Gclose (group_hid); | |
327 return false; | |
328 } | |
329 | |
330 space_hid = H5Dget_space (data_hid); | |
331 rank = H5Sget_simple_extent_ndims (space_hid); | |
332 | |
333 if (rank != 0) | |
334 { | |
335 H5Sclose (space_hid); | |
336 H5Tclose (type_hid); | |
337 H5Dclose (data_hid); | |
338 H5Gclose (group_hid); | |
339 return false; | |
340 } | |
341 | |
342 slen = H5Tget_size (type_hid); | |
343 if (slen < 0) | |
344 { | |
345 H5Sclose (space_hid); | |
346 H5Tclose (type_hid); | |
347 H5Dclose (data_hid); | |
348 H5Gclose (group_hid); | |
349 return false; | |
350 } | |
351 | |
352 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen); | |
353 | |
354 // create datatype for (null-terminated) string to read into: | |
355 st_id = H5Tcopy (H5T_C_S1); | |
356 H5Tset_size (st_id, slen); | |
357 | |
358 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, | |
359 X_CAST (void *, nm_tmp)) < 0) | |
360 { | |
361 H5Sclose (space_hid); | |
362 H5Tclose (type_hid); | |
363 H5Gclose (group_hid); | |
364 return false; | |
365 } | |
366 H5Tclose (st_id); | |
367 H5Dclose (data_hid); | |
368 nm = nm_tmp; | |
369 | |
370 if (nm == "@<anonymous>") | |
371 { | |
372 data_hid = H5Dopen (group_hid, "fcn"); | |
373 | |
374 if (data_hid < 0) | |
375 { | |
376 H5Gclose (group_hid); | |
377 return false; | |
378 } | |
379 | |
380 type_hid = H5Dget_type (data_hid); | |
381 type_class_hid = H5Tget_class (type_hid); | |
382 | |
383 if (type_class_hid != H5T_STRING) | |
384 { | |
385 H5Tclose (type_hid); | |
386 H5Dclose (data_hid); | |
387 H5Gclose (group_hid); | |
388 return false; | |
389 } | |
390 | |
391 space_hid = H5Dget_space (data_hid); | |
392 rank = H5Sget_simple_extent_ndims (space_hid); | |
393 | |
394 if (rank != 0) | |
395 { | |
396 H5Sclose (space_hid); | |
397 H5Tclose (type_hid); | |
398 H5Dclose (data_hid); | |
399 H5Gclose (group_hid); | |
400 return false; | |
401 } | |
402 | |
403 slen = H5Tget_size (type_hid); | |
404 if (slen < 0) | |
405 { | |
406 H5Sclose (space_hid); | |
407 H5Tclose (type_hid); | |
408 H5Dclose (data_hid); | |
409 H5Gclose (group_hid); | |
410 return false; | |
411 } | |
412 | |
413 OCTAVE_LOCAL_BUFFER (char, fcn_tmp, slen); | |
414 | |
415 // create datatype for (null-terminated) string to read into: | |
416 st_id = H5Tcopy (H5T_C_S1); | |
417 H5Tset_size (st_id, slen); | |
418 | |
419 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, | |
420 X_CAST (void *, fcn_tmp)) < 0) | |
421 { | |
422 H5Sclose (space_hid); | |
423 H5Tclose (type_hid); | |
424 H5Gclose (group_hid); | |
425 return false; | |
426 } | |
427 H5Dclose (data_hid); | |
428 H5Tclose (st_id); | |
429 | |
430 int parse_status; | |
431 octave_value anon_fcn_handle = eval_string (fcn_tmp, true, parse_status); | |
432 | |
433 fcn = anon_fcn_handle.fcn_handle_value () -> fcn; | |
434 } | |
435 else | |
436 { | |
437 fcn = lookup_function (nm); | |
438 if (! fcn.is_function ()) | |
439 return false; | |
440 } | |
441 | |
442 return true; | |
443 } | |
444 #endif | |
93 | 445 |
94 void | 446 void |
95 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const | 447 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const |
96 { | 448 { |
97 print_raw (os, pr_as_read_syntax); | 449 print_raw (os, pr_as_read_syntax); |