comparison src/ov-fcn-inline.cc @ 4973:c969a018c928

[project @ 2004-09-08 17:07:11 by jwe]
author jwe
date Wed, 08 Sep 2004 17:07:11 +0000
parents 724675f7f7cb
children 352d228d409b
comparison
equal deleted inserted replaced
4972:724675f7f7cb 4973:c969a018c928
53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_inline, 53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_inline,
54 "inline function", 54 "inline function",
55 "inline function"); 55 "inline function");
56 56
57 octave_fcn_inline::octave_fcn_inline (const std::string& f, 57 octave_fcn_inline::octave_fcn_inline (const std::string& f,
58 const string_vector& a, 58 const string_vector& a,
59 const std::string& n) 59 const std::string& n)
60 : octave_fcn_handle (0, n), iftext (f), ifargs (a) 60 : octave_fcn_handle (0, n), iftext (f), ifargs (a)
61 { 61 {
62 // Find a function name that isn't already in the symbol table. 62 // Find a function name that isn't already in the symbol table.
63 std::string fname = unique_symbol_name ("__inline_"); 63 std::string fname = unique_symbol_name ("__inline_");
64 64
65 // Form a string representing the function. 65 // Form a string representing the function.
66 66
67 OSSTREAM buf; 67 OSSTREAM buf;
68 68
69 buf << "function __retval__ = " << fname << "("; 69 buf << "function __retval__ = " << fname << "(";
70 70
75 75
76 buf << ifargs(i); 76 buf << ifargs(i);
77 } 77 }
78 78
79 buf << ")\n __retval__ = " << iftext << ";\nendfunction" << OSSTREAM_ENDS; 79 buf << ")\n __retval__ = " << iftext << ";\nendfunction" << OSSTREAM_ENDS;
80 80
81 // Parse this function and create a user function. 81 // Parse this function and create a user function.
82 82
83 octave_value eval_args (OSSTREAM_STR (buf)); 83 octave_value eval_args (OSSTREAM_STR (buf));
84 84
85 feval ("eval", eval_args, 0); 85 feval ("eval", eval_args, 0);
86 86
87 OSSTREAM_FREEZE (buf); 87 OSSTREAM_FREEZE (buf);
88 88
99 } 99 }
100 100
101 bool 101 bool
102 octave_fcn_inline::save_ascii (std::ostream& os, bool&, bool) 102 octave_fcn_inline::save_ascii (std::ostream& os, bool&, bool)
103 { 103 {
104 os << "# nargs: " << ifargs.length() << "\n"; 104 os << "# nargs: " << ifargs.length () << "\n";
105 for (int i = 0; i < ifargs.length (); i++) 105 for (int i = 0; i < ifargs.length (); i++)
106 os << ifargs (i) << "\n"; 106 os << ifargs(i) << "\n";
107 if (nm.length() < 1) 107 if (nm.length () < 1)
108 // Write an illegal value to flag empty fcn handle name 108 // Write an invalid value to flag empty fcn handle name.
109 os << "0\n"; 109 os << "0\n";
110 else 110 else
111 os << nm << "\n"; 111 os << nm << "\n";
112 os << iftext << "\n"; 112 os << iftext << "\n";
113 return true; 113 return true;
119 int nargs; 119 int nargs;
120 if (extract_keyword (is, "nargs", nargs, true)) 120 if (extract_keyword (is, "nargs", nargs, true))
121 { 121 {
122 ifargs.resize (nargs); 122 ifargs.resize (nargs);
123 for (int i = 0; i < nargs; i++) 123 for (int i = 0; i < nargs; i++)
124 is >> ifargs (i); 124 is >> ifargs(i);
125 is >> nm; 125 is >> nm;
126 if (nm == "0") 126 if (nm == "0")
127 nm = ""; 127 nm = "";
128 is >> iftext; 128 is >> iftext;
129 129
134 } 134 }
135 else 135 else
136 return false; 136 return false;
137 } 137 }
138 138
139 bool 139 bool
140 octave_fcn_inline::save_binary (std::ostream& os, bool&) 140 octave_fcn_inline::save_binary (std::ostream& os, bool&)
141 { 141 {
142 FOUR_BYTE_INT tmp = ifargs.length(); 142 FOUR_BYTE_INT tmp = ifargs.length ();
143 os.write (X_CAST (char *, &tmp), 4); 143 os.write (X_CAST (char *, &tmp), 4);
144 for (int i=0; i < ifargs.length (); i++) 144 for (int i = 0; i < ifargs.length (); i++)
145 { 145 {
146 tmp = ifargs(i).length(); 146 tmp = ifargs(i).length ();
147 os.write (X_CAST (char *, &tmp), 4); 147 os.write (X_CAST (char *, &tmp), 4);
148 os.write (ifargs(i).c_str(), ifargs(i).length()); 148 os.write (ifargs(i).c_str (), ifargs(i).length ());
149 } 149 }
150 tmp = nm.length(); 150 tmp = nm.length ();
151 os.write (X_CAST (char *, &tmp), 4); 151 os.write (X_CAST (char *, &tmp), 4);
152 os.write (nm.c_str(), nm.length()); 152 os.write (nm.c_str (), nm.length ());
153 tmp = iftext.length(); 153 tmp = iftext.length ();
154 os.write (X_CAST (char *, &tmp), 4); 154 os.write (X_CAST (char *, &tmp), 4);
155 os.write (iftext.c_str(), iftext.length()); 155 os.write (iftext.c_str (), iftext.length ());
156 return true; 156 return true;
157 } 157 }
158 158
159 bool 159 bool
160 octave_fcn_inline::load_binary (std::istream& is, bool swap, 160 octave_fcn_inline::load_binary (std::istream& is, bool swap,
161 oct_mach_info::float_format) 161 oct_mach_info::float_format)
162 { 162 {
163 FOUR_BYTE_INT nargs; 163 FOUR_BYTE_INT nargs;
164 if (! is.read (X_CAST (char *, &nargs), 4)) 164 if (! is.read (X_CAST (char *, &nargs), 4))
169 if (nargs < 1) 169 if (nargs < 1)
170 return false; 170 return false;
171 else 171 else
172 { 172 {
173 FOUR_BYTE_INT tmp; 173 FOUR_BYTE_INT tmp;
174 ifargs.resize(nargs); 174 ifargs.resize (nargs);
175 for (int i = 0; i < nargs; i++) 175 for (int i = 0; i < nargs; i++)
176 { 176 {
177 if (! is.read (X_CAST (char *, &tmp), 4)) 177 if (! is.read (X_CAST (char *, &tmp), 4))
178 return false; 178 return false;
179 if (swap) 179 if (swap)
183 is.read (ctmp, tmp); 183 is.read (ctmp, tmp);
184 ifargs(i) = std::string (ctmp); 184 ifargs(i) = std::string (ctmp);
185 185
186 if (! is) 186 if (! is)
187 return false; 187 return false;
188 } 188 }
189 189
190 if (! is.read (X_CAST (char *, &tmp), 4)) 190 if (! is.read (X_CAST (char *, &tmp), 4))
191 return false; 191 return false;
192 if (swap) 192 if (swap)
193 swap_bytes<4> (&tmp); 193 swap_bytes<4> (&tmp);
225 hid_t group_hid = -1; 225 hid_t group_hid = -1;
226 group_hid = H5Gcreate (loc_id, name, 0); 226 group_hid = H5Gcreate (loc_id, name, 0);
227 if (group_hid < 0 ) return false; 227 if (group_hid < 0 ) return false;
228 228
229 size_t len = 0; 229 size_t len = 0;
230 for (int i = 0; i < ifargs.length(); i++) 230 for (int i = 0; i < ifargs.length (); i++)
231 if (len < ifargs(i).length()) 231 if (len < ifargs(i).length ())
232 len = ifargs(i).length(); 232 len = ifargs(i).length ();
233 233
234 hid_t space_hid = -1, data_hid = -1, type_hid = -1;; 234 hid_t space_hid = -1, data_hid = -1, type_hid = -1;;
235 bool retval = true; 235 bool retval = true;
236 236
237 // XXX FIXME XXX Is there a better way of saving string vectors, than a 237 // XXX FIXME XXX Is there a better way of saving string vectors, than a
238 // null padded matrix? 238 // null padded matrix?
239 239
240 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2); 240 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2);
241 241
242 // Octave uses column-major, while HDF5 uses row-major ordering 242 // Octave uses column-major, while HDF5 uses row-major ordering
243 hdims[1] = ifargs.length(); 243 hdims[1] = ifargs.length ();
244 hdims[0] = len + 1; 244 hdims[0] = len + 1;
245 245
246 space_hid = H5Screate_simple (2, hdims, 0); 246 space_hid = H5Screate_simple (2, hdims, 0);
247 if (space_hid < 0) 247 if (space_hid < 0)
248 { 248 {
249 H5Gclose (group_hid); 249 H5Gclose (group_hid);
250 return false; 250 return false;
251 } 251 }
252 252
253 data_hid = H5Dcreate (group_hid, "args", H5T_NATIVE_CHAR, space_hid, 253 data_hid = H5Dcreate (group_hid, "args", H5T_NATIVE_CHAR, space_hid,
254 H5P_DEFAULT); 254 H5P_DEFAULT);
255 if (data_hid < 0) 255 if (data_hid < 0)
256 { 256 {
257 H5Sclose (space_hid); 257 H5Sclose (space_hid);
258 H5Gclose (group_hid); 258 H5Gclose (group_hid);
259 return false; 259 return false;
260 } 260 }
261 261
262 OCTAVE_LOCAL_BUFFER (char, s, ifargs.length() * (len + 1)); 262 OCTAVE_LOCAL_BUFFER (char, s, ifargs.length () * (len + 1));
263 263
264 // Save the args as a null teminated list 264 // Save the args as a null teminated list
265 for (int i = 0; i < ifargs.length(); i++) 265 for (int i = 0; i < ifargs.length (); i++)
266 { 266 {
267 const char * cptr = ifargs(i).c_str(); 267 const char * cptr = ifargs(i).c_str ();
268 for (size_t j = 0; j < ifargs(i).length(); j++) 268 for (size_t j = 0; j < ifargs(i).length (); j++)
269 s[i*(len+1)+j] = *cptr++; 269 s[i*(len+1)+j] = *cptr++;
270 s[ifargs(i).length()] = '\0'; 270 s[ifargs(i).length ()] = '\0';
271 } 271 }
272 272
273 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, 273 retval = H5Dwrite (data_hid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL,
274 H5P_DEFAULT, s) >= 0; 274 H5P_DEFAULT, s) >= 0;
275 275
276 H5Dclose (data_hid); 276 H5Dclose (data_hid);
277 H5Sclose (space_hid); 277 H5Sclose (space_hid);
278 278
279 if (!retval) 279 if (!retval)
280 { 280 {
281 H5Gclose (group_hid); 281 H5Gclose (group_hid);
282 return false; 282 return false;
283 } 283 }
284 284
285 // attach the type of the variable 285 // attach the type of the variable
286 type_hid = H5Tcopy (H5T_C_S1); 286 type_hid = H5Tcopy (H5T_C_S1);
287 H5Tset_size (type_hid, nm.length () + 1); 287 H5Tset_size (type_hid, nm.length () + 1);
288 if (type_hid < 0) 288 if (type_hid < 0)
289 { 289 {
290 H5Gclose (group_hid); 290 H5Gclose (group_hid);
291 return false; 291 return false;
292 } 292 }
293 293
294 hdims[0] = 0; 294 hdims[0] = 0;
295 space_hid = H5Screate_simple (0 , hdims, (hsize_t*) 0); 295 space_hid = H5Screate_simple (0 , hdims, (hsize_t*) 0);
296 if (space_hid < 0) 296 if (space_hid < 0)
297 { 297 {
298 H5Tclose (type_hid); 298 H5Tclose (type_hid);
299 H5Gclose (group_hid); 299 H5Gclose (group_hid);
300 return false; 300 return false;
301 } 301 }
302 302
303 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, H5P_DEFAULT); 303 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, H5P_DEFAULT);
304 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, 304 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL,
305 H5P_DEFAULT, (void*) nm.c_str ()) < 0) 305 H5P_DEFAULT, (void*) nm.c_str ()) < 0)
306 { 306 {
307 H5Sclose (space_hid); 307 H5Sclose (space_hid);
308 H5Tclose (type_hid); 308 H5Tclose (type_hid);
309 H5Gclose (group_hid); 309 H5Gclose (group_hid);
310 return false; 310 return false;
311 } 311 }
312 H5Dclose (data_hid); 312 H5Dclose (data_hid);
313 313
314 // attach the type of the variable 314 // attach the type of the variable
315 H5Tset_size (type_hid, iftext.length () + 1); 315 H5Tset_size (type_hid, iftext.length () + 1);
316 if (type_hid < 0) 316 if (type_hid < 0)
317 { 317 {
318 H5Gclose (group_hid); 318 H5Gclose (group_hid);
319 return false; 319 return false;
320 } 320 }
321 321
322 data_hid = H5Dcreate (group_hid, "iftext", type_hid, space_hid, 322 data_hid = H5Dcreate (group_hid, "iftext", type_hid, space_hid,
323 H5P_DEFAULT); 323 H5P_DEFAULT);
324 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, 324 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL,
325 H5P_DEFAULT, (void*) iftext.c_str ()) < 0) 325 H5P_DEFAULT, (void*) iftext.c_str ()) < 0)
326 { 326 {
327 H5Sclose (space_hid); 327 H5Sclose (space_hid);
328 H5Tclose (type_hid); 328 H5Tclose (type_hid);
329 H5Gclose (group_hid); 329 H5Gclose (group_hid);
330 return false; 330 return false;
331 } 331 }
332 332
333 H5Dclose (data_hid); 333 H5Dclose (data_hid);
334 334
335 return retval; 335 return retval;
336 } 336 }
337 337
338 bool 338 bool
339 octave_fcn_inline::load_hdf5 (hid_t loc_id, const char *name, 339 octave_fcn_inline::load_hdf5 (hid_t loc_id, const char *name,
340 bool /* have_h5giterate_bug */) 340 bool /* have_h5giterate_bug */)
341 { 341 {
342 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; 342 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id;
343 hsize_t rank; 343 hsize_t rank;
344 int slen; 344 int slen;
345 345
349 data_hid = H5Dopen (group_hid, "args"); 349 data_hid = H5Dopen (group_hid, "args");
350 space_hid = H5Dget_space (data_hid); 350 space_hid = H5Dget_space (data_hid);
351 rank = H5Sget_simple_extent_ndims (space_hid); 351 rank = H5Sget_simple_extent_ndims (space_hid);
352 352
353 if (rank != 2) 353 if (rank != 2)
354 { 354 {
355 H5Dclose (data_hid); 355 H5Dclose (data_hid);
356 H5Sclose (space_hid); 356 H5Sclose (space_hid);
357 H5Gclose (group_hid); 357 H5Gclose (group_hid);
358 return false; 358 return false;
359 } 359 }
361 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); 361 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank);
362 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); 362 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank);
363 363
364 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); 364 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
365 365
366 ifargs.resize(hdims[1]); 366 ifargs.resize (hdims[1]);
367 367
368 OCTAVE_LOCAL_BUFFER (char, s1, hdims[0] * hdims[1]); 368 OCTAVE_LOCAL_BUFFER (char, s1, hdims[0] * hdims[1]);
369 369
370 if (H5Dread (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL, 370 if (H5Dread (data_hid, H5T_NATIVE_UCHAR, H5S_ALL, H5S_ALL,
371 H5P_DEFAULT, s1) < 0) 371 H5P_DEFAULT, s1) < 0)
372 { 372 {
373 H5Dclose (data_hid); 373 H5Dclose (data_hid);
374 H5Sclose (space_hid); 374 H5Sclose (space_hid);
375 H5Gclose (group_hid); 375 H5Gclose (group_hid);
376 return false; 376 return false;
377 } 377 }
398 H5Tclose (type_hid); 398 H5Tclose (type_hid);
399 H5Dclose (data_hid); 399 H5Dclose (data_hid);
400 H5Gclose (group_hid); 400 H5Gclose (group_hid);
401 return false; 401 return false;
402 } 402 }
403 403
404 space_hid = H5Dget_space (data_hid); 404 space_hid = H5Dget_space (data_hid);
405 rank = H5Sget_simple_extent_ndims (space_hid); 405 rank = H5Sget_simple_extent_ndims (space_hid);
406 406
407 if (rank != 0) 407 if (rank != 0)
408 { 408 {
427 427
428 // create datatype for (null-terminated) string to read into: 428 // create datatype for (null-terminated) string to read into:
429 st_id = H5Tcopy (H5T_C_S1); 429 st_id = H5Tcopy (H5T_C_S1);
430 H5Tset_size (st_id, slen); 430 H5Tset_size (st_id, slen);
431 431
432 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, 432 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT,
433 (void *) nm_tmp) < 0) 433 X_CAST (void *, nm_tmp)) < 0)
434 { 434 {
435 H5Sclose (space_hid); 435 H5Sclose (space_hid);
436 H5Tclose (type_hid); 436 H5Tclose (type_hid);
437 H5Gclose (group_hid); 437 H5Gclose (group_hid);
438 return false; 438 return false;
457 H5Tclose (type_hid); 457 H5Tclose (type_hid);
458 H5Dclose (data_hid); 458 H5Dclose (data_hid);
459 H5Gclose (group_hid); 459 H5Gclose (group_hid);
460 return false; 460 return false;
461 } 461 }
462 462
463 space_hid = H5Dget_space (data_hid); 463 space_hid = H5Dget_space (data_hid);
464 rank = H5Sget_simple_extent_ndims (space_hid); 464 rank = H5Sget_simple_extent_ndims (space_hid);
465 465
466 if (rank != 0) 466 if (rank != 0)
467 { 467 {
486 486
487 // create datatype for (null-terminated) string to read into: 487 // create datatype for (null-terminated) string to read into:
488 st_id = H5Tcopy (H5T_C_S1); 488 st_id = H5Tcopy (H5T_C_S1);
489 H5Tset_size (st_id, slen); 489 H5Tset_size (st_id, slen);
490 490
491 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, 491 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT,
492 (void *) iftext_tmp) < 0) 492 X_CAST (void *, iftext_tmp)) < 0)
493 { 493 {
494 H5Sclose (space_hid); 494 H5Sclose (space_hid);
495 H5Tclose (type_hid); 495 H5Tclose (type_hid);
496 H5Gclose (group_hid); 496 H5Gclose (group_hid);
497 return false; 497 return false;
742 { 742 {
743 if (i && old_func.substr (i-1, 1) != ".") 743 if (i && old_func.substr (i-1, 1) != ".")
744 new_func.append ("."); 744 new_func.append (".");
745 745
746 // Special case for ** operator. 746 // Special case for ** operator.
747 if (t1 == "*" && i < (old_func.length () - 1) 747 if (t1 == "*" && i < (old_func.length () - 1)
748 && old_func.substr (i+1, 1) == "*") 748 && old_func.substr (i+1, 1) == "*")
749 { 749 {
750 new_func.append ("*"); 750 new_func.append ("*");
751 i++; 751 i++;
752 } 752 }