26 #if defined (HAVE_CONFIG_H)
30 #if defined (HAVE_HDF5)
76 #if defined (HAVE_HDF5)
82 error (
"%s: internal error: ID too large for hid_t", who);
84 return static_cast<hid_t
> (id);
90 : file_id (-1), current_item (-1)
92 #if defined (HAVE_HDF5)
105 #if defined (HAVE_HDF5)
110 std::ios::setstate (std::ios::badbit);
125 #if defined (HAVE_HDF5)
144 #if defined (HAVE_HDF5)
147 std::string fname_str (
name);
149 const char *ascii_fname = ascii_fname_str.c_str ();
151 if (mode & std::ios::in)
153 else if (mode & std::ios::out)
155 if (mode & std::ios::app && H5Fis_hdf5 (ascii_fname) > 0)
158 # if defined (OCTAVE_USE_WINDOWS_API)
161 std::string abs_ascii_fname
163 if (! abs_ascii_fname.empty ())
166 file_id = H5Fcreate (ascii_fname, H5F_ACC_TRUNC,
169 std::ios::setstate (std::ios::badbit);
175 std::string::const_iterator first_non_ASCII
176 = std::find_if (fname_str.begin (), fname_str.end (),
177 [](
char c) { return (c < 0 || c >= 128); });
178 if (first_non_ASCII == fname_str.end ())
184 std::ios::setstate (std::ios::badbit);
191 octave_hdf5_id hdf5_fid = H5Fcreate (tmp_name.c_str (), H5F_ACC_TRUNC,
197 std::ios::setstate (std::ios::badbit);
209 std::ios::setstate (std::ios::badbit);
216 ascii_fname = ascii_fname_str.c_str ();
225 std::ios::setstate (std::ios::badbit);
242 size_t nm_len = nm.length ();
246 if (! isalpha (nm[0]))
249 for (
size_t i = 0; i < nm_len; i++)
252 retval += (isalnum (c) || c ==
'_') ? c :
'_';
267 #if defined (HAVE_HDF5)
270 if ((
n = H5Tget_nmembers (t1)) != H5Tget_nmembers (t2))
273 for (
int i = 0; i <
n; ++i)
275 hid_t mt1 = H5Tget_member_type (t1, i);
276 hid_t mt2 = H5Tget_member_type (t2, i);
278 if (H5Tget_class (mt1) != H5Tget_class (mt2))
298 #if defined (HAVE_HDF5)
312 #if defined (HAVE_HDF5_18)
316 H5Eget_auto (&err_func, &err_func_data);
317 H5Eset_auto (
nullptr,
nullptr);
320 hid_t attr_id = H5Aopen_name (loc_id, attr_name);
330 #if defined (HAVE_HDF5_18)
333 H5Eset_auto (err_func, err_func_data);
344 const char *attr_name,
void *buf)
346 #if defined (HAVE_HDF5)
360 #if defined (HAVE_HDF5_18)
364 H5Eget_auto (&err_func, &err_func_data);
365 H5Eset_auto (
nullptr,
nullptr);
368 hid_t attr_id = H5Aopen_name (loc_id, attr_name);
372 hid_t space_id = H5Aget_space (attr_id);
374 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
377 retval = H5Aread (attr_id, type_id, buf) >= 0;
382 #if defined (HAVE_HDF5_18)
385 H5Eset_auto (err_func, err_func_data);
403 #if defined (HAVE_HDF5)
405 hid_t type_id = H5Tcreate (H5T_COMPOUND,
sizeof (
double) * 2);
407 H5Tinsert (type_id,
"real", 0 *
sizeof (
double), num_type);
408 H5Tinsert (type_id,
"imag", 1 *
sizeof (
double), num_type);
417 #if defined (HAVE_HDF5)
428 hid_t type_id = H5Tcreate (H5T_COMPOUND,
sizeof (
double) * 3);
430 H5Tinsert (type_id,
"base", 0 *
sizeof (
double), num_type);
431 H5Tinsert (type_id,
"limit", 1 *
sizeof (
double), num_type);
432 H5Tinsert (type_id,
"increment", 2 *
sizeof (
double), num_type);
440 #if defined (HAVE_HDF5)
442 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id;
446 #if defined (HAVE_HDF5_18)
449 group_hid = H5Gopen (loc_id,
name);
451 if (group_hid < 0)
return -1;
453 #if defined (HAVE_HDF5_18)
456 data_hid = H5Dopen (group_hid,
"args");
458 space_hid = H5Dget_space (data_hid);
459 rank = H5Sget_simple_extent_ndims (space_hid);
464 H5Sclose (space_hid);
465 H5Gclose (group_hid);
472 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
482 H5Sclose (space_hid);
483 H5Gclose (group_hid);
488 H5Sclose (space_hid);
490 for (
size_t i = 0; i < hdims[1]; i++)
491 args(i+1) = std::string (s1 + i*hdims[0]);
493 #if defined (HAVE_HDF5_18)
496 data_hid = H5Dopen (group_hid,
"nm");
501 H5Gclose (group_hid);
505 type_hid = H5Dget_type (data_hid);
506 type_class_hid = H5Tget_class (type_hid);
508 if (type_class_hid != H5T_STRING)
512 H5Gclose (group_hid);
516 space_hid = H5Dget_space (data_hid);
517 rank = H5Sget_simple_extent_ndims (space_hid);
521 H5Sclose (space_hid);
524 H5Gclose (group_hid);
528 slen = H5Tget_size (type_hid);
531 H5Sclose (space_hid);
534 H5Gclose (group_hid);
541 st_id = H5Tcopy (H5T_C_S1);
542 H5Tset_size (st_id, slen);
547 H5Sclose (space_hid);
549 H5Gclose (group_hid);
557 #if defined (HAVE_HDF5_18)
560 data_hid = H5Dopen (group_hid,
"iftext");
565 H5Gclose (group_hid);
569 type_hid = H5Dget_type (data_hid);
570 type_class_hid = H5Tget_class (type_hid);
572 if (type_class_hid != H5T_STRING)
576 H5Gclose (group_hid);
580 space_hid = H5Dget_space (data_hid);
581 rank = H5Sget_simple_extent_ndims (space_hid);
585 H5Sclose (space_hid);
588 H5Gclose (group_hid);
592 slen = H5Tget_size (type_hid);
595 H5Sclose (space_hid);
598 H5Gclose (group_hid);
605 st_id = H5Tcopy (H5T_C_S1);
606 H5Tset_size (st_id, slen);
611 H5Sclose (space_hid);
613 H5Gclose (group_hid);
619 args(0) = std::string (iftext_tmp);
633 octave_unused_parameter (loc_id);
634 octave_unused_parameter (
name);
635 octave_unused_parameter (
retval);
658 hid_t type_class_id = -1;
660 hid_t subgroup_id = -1;
661 hid_t space_id = -1;;
667 std::string vname =
name;
684 H5Gget_objinfo (group_id,
name, 1, &info);
686 if (info.type == H5G_GROUP && ident_valid)
688 #if defined (HAVE_HDF5_18)
691 subgroup_id = H5Gopen (group_id,
name);
702 #if defined (HAVE_HDF5_18)
705 data_id = H5Dopen (subgroup_id,
"type");
714 type_id = H5Dget_type (data_id);
716 type_class_id = H5Tget_class (type_id);
718 if (type_class_id != H5T_STRING)
721 space_id = H5Dget_space (data_id);
722 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
727 int slen = H5Tget_size (type_id);
734 hid_t st_id = H5Tcopy (H5T_C_S1);
735 H5Tset_size (st_id, slen);
744 if (std::string (typ) ==
"inline function")
752 retval = (
d->tc.load_hdf5 (subgroup_id,
"value") ? 1 : -1);
758 H5Gclose (subgroup_id);
778 H5Gclose (subgroup_id);
780 retval = (
d->tc.load_hdf5 (group_id,
name) ? 1 : -1);
784 else if (info.type == H5G_DATASET && ident_valid)
791 #if defined (HAVE_HDF5_18)
794 data_id = H5Dopen (group_id,
name);
803 type_id = H5Dget_type (data_id);
805 type_class_id = H5Tget_class (type_id);
807 if (type_class_id == H5T_FLOAT)
809 space_id = H5Dget_space (data_id);
811 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
820 else if (type_class_id == H5T_INTEGER)
824 #if defined (HAVE_H5T_GET_NATIVE_TYPE)
828 switch (H5Tget_native_type (type_id, H5T_DIR_ASCEND))
830 case H5T_NATIVE_CHAR:
834 case H5T_NATIVE_SHORT:
839 case H5T_NATIVE_LONG:
843 case H5T_NATIVE_LLONG:
847 case H5T_NATIVE_UCHAR:
851 case H5T_NATIVE_USHORT:
855 case H5T_NATIVE_UINT:
856 case H5T_NATIVE_ULONG:
860 case H5T_NATIVE_ULLONG:
865 hid_t int_sign = H5Tget_sign (type_id);
867 if (int_sign == H5T_SGN_ERROR)
868 warning (
"load: can't read '%s' (unknown datatype)",
name);
871 if (int_sign == H5T_SGN_NONE)
872 int_typ.push_back (
'u');
873 int_typ.append (
"int");
875 int slen = H5Tget_size (type_id);
877 warning (
"load: can't read '%s' (unknown datatype)",
name);
883 int_typ.append (
"8 ");
887 int_typ.append (
"16 ");
891 int_typ.append (
"32 ");
895 int_typ.append (
"64 ");
899 warning (
"load: can't read '%s' (unknown datatype)",
908 warning (
"load: can't read '%s' (unknown datatype)",
name);
912 space_id = H5Dget_space (data_id);
914 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
917 int_typ.append (
"scalar");
919 int_typ.append (
"matrix");
925 else if (type_class_id == H5T_STRING)
927 else if (type_class_id == H5T_COMPOUND)
935 space_id = H5Dget_space (data_id);
936 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
952 warning (
"load: can't read '%s' (unknown datatype)",
name);
957 H5Tclose (range_type);
958 H5Tclose (complex_type);
962 warning (
"load: can't read '%s' (unknown datatype)",
name);
970 retval = (
d->tc.load_hdf5 (group_id,
name) ? 1 : -1);
980 warning (
"load: skipping invalid identifier '%s' in hdf5 file",
989 warning (
"load: error while reading hdf5 item '%s'",
name);
995 int comment_length = H5Gget_comment (group_id,
name, 0,
nullptr);
997 if (comment_length > 1)
1000 H5Gget_comment (group_id,
name, comment_length, tdoc);
1003 else if (vname !=
name)
1022 #if defined (HAVE_HDF5)
1035 void *operator_data)
1037 #if defined (HAVE_HDF5)
1058 #if defined (HAVE_HDF5)
1069 herr_t H5Giterate_retval = -1;
1071 hsize_t num_obj = 0;
1072 #if defined (HAVE_HDF5_18)
1075 hid_t group_id = H5Gopen (hs.
file_id,
"/");
1077 H5Gget_num_objs (group_id, &num_obj);
1078 H5Gclose (group_id);
1082 bool load_named_vars = argv_idx < argc;
1083 while (load_named_vars && hs.
current_item <
static_cast<int> (num_obj))
1085 std::vector<char> var_name;
1090 var_name.resize (
len+1);
1093 for (
int i = argv_idx; i < argc; i++)
1096 if (pattern.
match (std::string (&var_name[0])))
1113 if (H5Giterate_retval > 0)
1126 if (!
d.name.empty ())
1141 #if defined (HAVE_HDF5)
1145 hid_t as_id = H5Screate (H5S_SCALAR);
1149 #if defined (HAVE_HDF5_18)
1150 hid_t a_id = H5Acreate (loc_id, attr_name, H5T_NATIVE_UCHAR,
1153 hid_t a_id = H5Acreate (loc_id, attr_name,
1158 unsigned char attr_val = 1;
1160 retval = H5Awrite (a_id, H5T_NATIVE_UCHAR, &attr_val);
1181 const char *attr_name,
void *buf)
1183 #if defined (HAVE_HDF5)
1187 hid_t as_id = H5Screate (H5S_SCALAR);
1191 #if defined (HAVE_HDF5_18)
1192 hid_t a_id = H5Acreate (loc_id, attr_name, type_id,
1195 hid_t a_id = H5Acreate (loc_id, attr_name,
1200 retval = H5Awrite (a_id, type_id, buf);
1226 #if defined (HAVE_HDF5)
1228 hsize_t sz =
d.length ();
1231 hid_t space_hid = -1;
1232 hid_t data_hid = -1;
1234 for (hsize_t i = 0; i < sz; i++)
1244 space_hid = H5Screate_simple (1, &sz,
nullptr);
1245 if (space_hid < 0)
return space_hid;
1246 #if defined (HAVE_HDF5_18)
1255 H5Sclose (space_hid);
1262 H5Dclose (data_hid);
1263 H5Sclose (space_hid);
1282 #if defined (HAVE_HDF5)
1287 hsize_t hdims, maxdims;
1288 #if defined (HAVE_HDF5_18)
1291 hid_t data_hid = H5Dopen (loc_id,
name);
1293 hid_t space_id = H5Dget_space (data_hid);
1294 H5Sget_simple_extent_dims (space_id, &hdims, &maxdims);
1304 for (hsize_t i = 0; i < hdims; i++)
1308 H5Sclose (space_id);
1309 H5Dclose (data_hid);
1326 #if defined (HAVE_HDF5)
1327 # if defined (HAVE_HDF5_INT2FLOAT_CONVERSIONS)
1332 return H5T_NATIVE_UCHAR;
1335 return H5T_NATIVE_USHORT;
1338 return H5T_NATIVE_UINT;
1341 return H5T_NATIVE_CHAR;
1344 return H5T_NATIVE_SHORT;
1347 return H5T_NATIVE_INT;
1350 return H5T_NATIVE_FLOAT;
1354 return H5T_NATIVE_DOUBLE;
1359 octave_unused_parameter (st);
1367 octave_unused_parameter (st);
1381 const std::string&
name,
const std::string& doc,
1382 bool mark_global,
bool save_as_floats)
1384 #if defined (HAVE_HDF5)
1387 hid_t type_id, space_id, data_id, data_type_id;
1388 type_id = space_id = data_id = data_type_id = -1;
1400 #if defined (HAVE_HDF5_18)
1404 data_id = H5Gcreate (loc_id,
name.c_str (), 0);
1410 type_id = H5Tcopy (H5T_C_S1); H5Tset_size (type_id, t.length () + 1);
1415 space_id = H5Screate_simple (0, dims,
nullptr);
1418 #if defined (HAVE_HDF5_18)
1419 data_type_id = H5Dcreate (data_id,
"type", type_id, space_id,
1423 data_type_id = H5Dcreate (data_id,
"type", type_id, space_id,
1426 if (data_type_id < 0
1435 if (
retval && doc.length () > 0
1436 && H5Gset_comment (loc_id,
name.c_str (), doc.c_str ()) < 0)
1440 if (
retval && mark_global)
1449 if (data_type_id >= 0)
1450 H5Dclose (data_type_id);
1456 H5Sclose (space_id);
1462 error (
"save: error while writing '%s' to hdf5 file",
name.c_str ());
1476 const std::string&
name,
const std::string& doc,
1477 bool mark_global,
bool save_as_floats)
1479 #if defined (HAVE_HDF5)
1486 mark_global, save_as_floats);
charNDArray max(char d, const charNDArray &m)
Vector representing the dimensions (size) of an Array.
bool match(const std::string &str) const
void open(const char *name, int mode, int)
void open_create(const char *name, int mode)
octave_value_list feval(const char *name, const octave_value_list &args=octave_value_list(), int nargout=0)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
octave_value lookup_type(const std::string &nm)
static int static_type_id(void)
octave_idx_type length(void) const
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
octave_value full_value(void) const
bool is_diag_matrix(void) const
bool is_perm_matrix(void) const
std::string type_name(void) const
const octave_hdf5_id octave_H5P_DEFAULT
const octave_hdf5_id octave_H5E_DEFAULT
const octave_hdf5_id octave_H5S_ALL
void warning(const char *fmt,...)
void error(const char *fmt,...)
#define panic_impossible()
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
bool save_hdf5_data(std::ostream &os, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_global, bool save_as_floats)
static herr_t hdf5_read_next_data_internal(hid_t group_id, const char *name, void *dv)
int save_hdf5_empty(octave_hdf5_id loc_id, const char *name, const dim_vector &d)
std::string read_hdf5_data(std::istream &is, const std::string &, bool &global, octave_value &tc, std::string &doc, const string_vector &argv, int argv_idx, int argc)
octave_hdf5_err hdf5_add_attr(octave_hdf5_id loc_id, const char *attr_name)
bool hdf5_get_scalar_attr(octave_hdf5_id loc_id, octave_hdf5_id type_id, const char *attr_name, void *buf)
int load_hdf5_empty(octave_hdf5_id loc_id, const char *name, dim_vector &d)
octave_hdf5_id save_type_to_hdf5(save_type st)
octave_hdf5_id hdf5_make_complex_type(octave_hdf5_id num_type)
static hid_t hdf5_make_range_type(hid_t num_type)
static herr_t load_inline_fcn(hid_t loc_id, const char *name, octave_value &retval)
octave_hdf5_err hdf5_read_next_data(octave_hdf5_id group_id, const char *name, void *dv)
octave_hdf5_err hdf5_h5g_iterate(octave_hdf5_id loc_id, const char *name, int *idx, void *operator_data)
bool hdf5_types_compatible(octave_hdf5_id t1, octave_hdf5_id t2)
octave_hdf5_err hdf5_add_scalar_attr(octave_hdf5_id loc_id, octave_hdf5_id type_id, const char *attr_name, void *buf)
bool add_hdf5_data(octave_hdf5_id loc_id, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_global, bool save_as_floats)
static std::string make_valid_identifier(const std::string &nm)
static hid_t check_hdf5_id_value(octave_hdf5_id id, const char *who)
bool hdf5_check_attr(octave_hdf5_id loc_id, const char *attr_name)
std::string get_ASCII_filename(const std::string &orig_file_name)
std::string tempnam(const std::string &dir, const std::string &pfx)
std::string canonicalize_file_name(const std::string &name)
int rename(const std::string &from, const std::string &to)
octave_value lookup_type(const std::string &nm)
bool valid_identifier(const char *s)
type_info & __get_type_info__(const std::string &who)
interpreter & __get_interpreter__(const std::string &who)
bool check_hdf5_types(bool warn)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
octave_value::octave_value(const Array< char > &chm, char type) return retval