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 # if defined (HAVE_HDF5_UTF8)
148 const char *fname = name;
150 std::string fname_str (name);
152 const char *fname = ascii_fname_str.c_str ();
155 if (mode & std::ios::in)
157 else if (mode & std::ios::out)
159 if (mode & std::ios::app && H5Fis_hdf5 (fname) > 0)
162 # if defined (HAVE_HDF5_UTF8)
168 std::string abs_ascii_fname
170 if (! abs_ascii_fname.empty ())
173 file_id = H5Fcreate (fname, H5F_ACC_TRUNC,
176 std::ios::setstate (std::ios::badbit);
182 std::string::const_iterator first_non_ASCII
183 = std::find_if (fname_str.begin (), fname_str.end (),
184 [](
char c) { return (c < 0 || c >= 128); });
185 if (first_non_ASCII == fname_str.end ())
191 std::ios::setstate (std::ios::badbit);
198 octave_hdf5_id hdf5_fid = H5Fcreate (tmp_name.c_str (), H5F_ACC_TRUNC,
204 std::ios::setstate (std::ios::badbit);
216 std::ios::setstate (std::ios::badbit);
223 ascii_fname = ascii_fname_str.c_str ();
229 std::ios::setstate (std::ios::badbit);
246 std::size_t nm_len = nm.length ();
250 if (! isalpha (nm[0]))
253 for (std::size_t i = 0; i < nm_len; i++)
256 retval += (isalnum (c) || c ==
'_') ? c :
'_';
271 #if defined (HAVE_HDF5)
274 if ((
n = H5Tget_nmembers (t1)) != H5Tget_nmembers (t2))
277 for (
int i = 0; i <
n; ++i)
279 hid_t mt1 = H5Tget_member_type (t1, i);
280 hid_t mt2 = H5Tget_member_type (t2, i);
282 if (H5Tget_class (mt1) != H5Tget_class (mt2))
302 #if defined (HAVE_HDF5)
316 #if defined (HAVE_HDF5_18)
320 H5Eget_auto (&err_fcn, &err_fcn_data);
321 H5Eset_auto (
nullptr,
nullptr);
324 hid_t attr_id = H5Aopen_name (loc_id, attr_name);
334 #if defined (HAVE_HDF5_18)
337 H5Eset_auto (err_fcn, err_fcn_data);
348 const char *attr_name,
void *buf)
350 #if defined (HAVE_HDF5)
364 #if defined (HAVE_HDF5_18)
368 H5Eget_auto (&err_fcn, &err_fcn_data);
369 H5Eset_auto (
nullptr,
nullptr);
372 hid_t attr_id = H5Aopen_name (loc_id, attr_name);
376 hid_t space_id = H5Aget_space (attr_id);
378 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
381 retval = H5Aread (attr_id, type_id, buf) >= 0;
386 #if defined (HAVE_HDF5_18)
389 H5Eset_auto (err_fcn, err_fcn_data);
407 #if defined (HAVE_HDF5)
409 hid_t type_id = H5Tcreate (H5T_COMPOUND,
sizeof (
double) * 2);
411 H5Tinsert (type_id,
"real", 0 *
sizeof (
double), num_type);
412 H5Tinsert (type_id,
"imag", 1 *
sizeof (
double), num_type);
421 #if defined (HAVE_HDF5)
432 hid_t type_id = H5Tcreate (H5T_COMPOUND,
sizeof (
double) * 3);
434 H5Tinsert (type_id,
"base", 0 *
sizeof (
double), num_type);
435 H5Tinsert (type_id,
"limit", 1 *
sizeof (
double), num_type);
436 H5Tinsert (type_id,
"increment", 2 *
sizeof (
double), num_type);
444 #if defined (HAVE_HDF5)
446 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id;
450 #if defined (HAVE_HDF5_18)
453 group_hid = H5Gopen (loc_id, name);
455 if (group_hid < 0)
return -1;
457 #if defined (HAVE_HDF5_18)
460 data_hid = H5Dopen (group_hid,
"args");
462 space_hid = H5Dget_space (data_hid);
463 rank = H5Sget_simple_extent_ndims (space_hid);
468 H5Sclose (space_hid);
469 H5Gclose (group_hid);
476 H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
486 H5Sclose (space_hid);
487 H5Gclose (group_hid);
492 H5Sclose (space_hid);
494 for (std::size_t i = 0; i < hdims[1]; i++)
495 args(i+1) = std::string (s1 + i*hdims[0]);
497 #if defined (HAVE_HDF5_18)
500 data_hid = H5Dopen (group_hid,
"nm");
505 H5Gclose (group_hid);
509 type_hid = H5Dget_type (data_hid);
510 type_class_hid = H5Tget_class (type_hid);
512 if (type_class_hid != H5T_STRING)
516 H5Gclose (group_hid);
520 space_hid = H5Dget_space (data_hid);
521 rank = H5Sget_simple_extent_ndims (space_hid);
525 H5Sclose (space_hid);
528 H5Gclose (group_hid);
532 slen = H5Tget_size (type_hid);
535 H5Sclose (space_hid);
538 H5Gclose (group_hid);
545 st_id = H5Tcopy (H5T_C_S1);
546 H5Tset_size (st_id, slen);
551 H5Sclose (space_hid);
553 H5Gclose (group_hid);
561 #if defined (HAVE_HDF5_18)
564 data_hid = H5Dopen (group_hid,
"iftext");
569 H5Gclose (group_hid);
573 type_hid = H5Dget_type (data_hid);
574 type_class_hid = H5Tget_class (type_hid);
576 if (type_class_hid != H5T_STRING)
580 H5Gclose (group_hid);
584 space_hid = H5Dget_space (data_hid);
585 rank = H5Sget_simple_extent_ndims (space_hid);
589 H5Sclose (space_hid);
592 H5Gclose (group_hid);
596 slen = H5Tget_size (type_hid);
599 H5Sclose (space_hid);
602 H5Gclose (group_hid);
609 st_id = H5Tcopy (H5T_C_S1);
610 H5Tset_size (st_id, slen);
615 H5Sclose (space_hid);
617 H5Gclose (group_hid);
623 args(0) = std::string (iftext_tmp);
636 octave_unused_parameter (loc_id);
637 octave_unused_parameter (name);
638 octave_unused_parameter (retval);
663 hid_t type_class_id = -1;
665 hid_t subgroup_id = -1;
666 hid_t space_id = -1;;
672 std::string vname = name;
688 H5Gget_objinfo (group_id, name, 1, &info);
690 if (info.type == H5G_GROUP && ident_valid)
692 #if defined (HAVE_HDF5_18)
695 subgroup_id = H5Gopen (group_id, name);
700 retval = subgroup_id;
706 #if defined (HAVE_HDF5_18)
709 data_id = H5Dopen (subgroup_id,
"type");
718 type_id = H5Dget_type (data_id);
720 type_class_id = H5Tget_class (type_id);
722 if (type_class_id != H5T_STRING)
725 space_id = H5Dget_space (data_id);
726 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
731 int slen = H5Tget_size (type_id);
738 hid_t st_id = H5Tcopy (H5T_C_S1);
739 H5Tset_size (st_id, slen);
748 if (std::string (typ, slen-1) ==
"inline function")
758 retval = (
d->tc.load_hdf5 (subgroup_id,
"value") ? 1 : -1);
760 catch (
const octave::execution_exception& ee)
769 H5Gclose (subgroup_id);
789 H5Gclose (subgroup_id);
793 retval = (
d->tc.load_hdf5 (group_id, name) ? 1 : -1);
795 catch (
const octave::execution_exception& ee)
802 else if (info.type == H5G_DATASET && ident_valid)
809 #if defined (HAVE_HDF5_18)
812 data_id = H5Dopen (group_id, name);
821 type_id = H5Dget_type (data_id);
823 type_class_id = H5Tget_class (type_id);
825 if (type_class_id == H5T_FLOAT)
827 space_id = H5Dget_space (data_id);
829 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
838 else if (type_class_id == H5T_INTEGER)
842 #if defined (HAVE_H5T_GET_NATIVE_TYPE)
846 switch (H5Tget_native_type (type_id, H5T_DIR_ASCEND))
848 case H5T_NATIVE_CHAR:
852 case H5T_NATIVE_SHORT:
857 case H5T_NATIVE_LONG:
861 case H5T_NATIVE_LLONG:
865 case H5T_NATIVE_UCHAR:
869 case H5T_NATIVE_USHORT:
873 case H5T_NATIVE_UINT:
874 case H5T_NATIVE_ULONG:
878 case H5T_NATIVE_ULLONG:
883 hid_t int_sign = H5Tget_sign (type_id);
885 if (int_sign == H5T_SGN_ERROR)
886 warning (
"load: can't read '%s' (unknown datatype)", name);
889 if (int_sign == H5T_SGN_NONE)
890 int_typ.push_back (
'u');
891 int_typ.append (
"int");
893 int slen = H5Tget_size (type_id);
895 warning (
"load: can't read '%s' (unknown datatype)", name);
901 int_typ.append (
"8 ");
905 int_typ.append (
"16 ");
909 int_typ.append (
"32 ");
913 int_typ.append (
"64 ");
917 warning (
"load: can't read '%s' (unknown datatype)",
926 warning (
"load: can't read '%s' (unknown datatype)", name);
930 space_id = H5Dget_space (data_id);
932 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
935 int_typ.append (
"scalar");
937 int_typ.append (
"matrix");
943 else if (type_class_id == H5T_STRING)
945 else if (type_class_id == H5T_COMPOUND)
953 space_id = H5Dget_space (data_id);
954 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
970 warning (
"load: can't read '%s' (unknown datatype)", name);
975 H5Tclose (range_type);
976 H5Tclose (complex_type);
980 warning (
"load: can't read '%s' (unknown datatype)", name);
990 retval = (
d->tc.load_hdf5 (group_id, name) ? 1 : -1);
992 catch (
const octave::execution_exception& ee)
1005 warning (
"load: skipping invalid identifier '%s' in hdf5 file",
1014 warning (
"load: error while reading hdf5 item '%s'", name);
1020 int comment_length = H5Gget_comment (group_id, name, 0,
nullptr);
1022 if (comment_length > 1)
1025 H5Gget_comment (group_id, name, comment_length, tdoc);
1028 else if (vname != name)
1047 #if defined (HAVE_HDF5)
1060 void *operator_data)
1062 #if defined (HAVE_HDF5)
1083 #if defined (HAVE_HDF5)
1094 herr_t H5Giterate_retval = -1;
1096 hsize_t num_obj = 0;
1097 #if defined (HAVE_HDF5_18)
1100 hid_t group_id = H5Gopen (hs.
file_id,
"/");
1102 H5Gget_num_objs (group_id, &num_obj);
1103 H5Gclose (group_id);
1107 bool load_named_vars = argv_idx < argc;
1108 while (load_named_vars && hs.
current_item <
static_cast<int> (num_obj))
1110 std::vector<char> var_name;
1112 std::size_t
len = 0;
1115 var_name.resize (
len+1);
1118 for (
int i = argv_idx; i < argc; i++)
1121 if (pattern.
match (std::string (&var_name[0])))
1138 if (H5Giterate_retval > 0)
1151 if (!
d.name.empty ())
1166 #if defined (HAVE_HDF5)
1170 hid_t as_id = H5Screate (H5S_SCALAR);
1174 #if defined (HAVE_HDF5_18)
1175 hid_t a_id = H5Acreate (loc_id, attr_name, H5T_NATIVE_UCHAR,
1178 hid_t a_id = H5Acreate (loc_id, attr_name,
1183 unsigned char attr_val = 1;
1185 retval = H5Awrite (a_id, H5T_NATIVE_UCHAR, &attr_val);
1206 const char *attr_name,
void *buf)
1208 #if defined (HAVE_HDF5)
1212 hid_t as_id = H5Screate (H5S_SCALAR);
1216 #if defined (HAVE_HDF5_18)
1217 hid_t a_id = H5Acreate (loc_id, attr_name, type_id,
1220 hid_t a_id = H5Acreate (loc_id, attr_name,
1225 retval = H5Awrite (a_id, type_id, buf);
1251 #if defined (HAVE_HDF5)
1253 hsize_t sz =
d.length ();
1256 hid_t space_hid = -1;
1257 hid_t data_hid = -1;
1259 for (hsize_t i = 0; i < sz; i++)
1269 space_hid = H5Screate_simple (1, &sz,
nullptr);
1270 if (space_hid < 0)
return space_hid;
1271 #if defined (HAVE_HDF5_18)
1281 H5Sclose (space_hid);
1288 H5Dclose (data_hid);
1289 H5Sclose (space_hid);
1294 return (retval == 0 ? 1 : retval);
1308 #if defined (HAVE_HDF5)
1313 hsize_t hdims, maxdims;
1314 #if defined (HAVE_HDF5_18)
1317 hid_t data_hid = H5Dopen (loc_id, name);
1319 hid_t space_id = H5Dget_space (data_hid);
1320 H5Sget_simple_extent_dims (space_id, &hdims, &maxdims);
1330 for (hsize_t i = 0; i < hdims; i++)
1334 H5Sclose (space_id);
1335 H5Dclose (data_hid);
1337 return (retval == 0 ? hdims : retval);
1352 #if defined (HAVE_HDF5)
1353 # if defined (HAVE_HDF5_INT2FLOAT_CONVERSIONS)
1358 return H5T_NATIVE_UCHAR;
1361 return H5T_NATIVE_USHORT;
1364 return H5T_NATIVE_UINT;
1367 return H5T_NATIVE_CHAR;
1370 return H5T_NATIVE_SHORT;
1373 return H5T_NATIVE_INT;
1376 return H5T_NATIVE_FLOAT;
1380 return H5T_NATIVE_DOUBLE;
1385 octave_unused_parameter (st);
1393 octave_unused_parameter (st);
1407 const std::string& name,
const std::string& doc,
1408 bool mark_global,
bool save_as_floats)
1410 #if defined (HAVE_HDF5)
1413 hid_t type_id, space_id, data_id, data_type_id;
1414 type_id = space_id = data_id = data_type_id = -1;
1416 bool retval =
false;
1426 #if defined (HAVE_HDF5_18)
1430 data_id = H5Gcreate (loc_id, name.c_str (), 0);
1436 type_id = H5Tcopy (H5T_C_S1); H5Tset_size (type_id, t.length () + 1);
1441 space_id = H5Screate_simple (0, dims,
nullptr);
1444 #if defined (HAVE_HDF5_18)
1445 data_type_id = H5Dcreate (data_id,
"type", type_id, space_id,
1449 data_type_id = H5Dcreate (data_id,
"type", type_id, space_id,
1452 if (data_type_id < 0
1458 retval = val.
save_hdf5 (data_id,
"value", save_as_floats);
1461 if (retval && doc.length () > 0
1462 && H5Gset_comment (loc_id, name.c_str (), doc.c_str ()) < 0)
1466 if (retval && mark_global)
1475 if (data_type_id >= 0)
1476 H5Dclose (data_type_id);
1482 H5Sclose (space_id);
1488 error (
"save: error while writing '%s' to hdf5 file", name.c_str ());
1502 const std::string& name,
const std::string& doc,
1503 bool mark_global,
bool save_as_floats)
1505 #if defined (HAVE_HDF5)
1512 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
OCTINTERP_API void open(const char *name, int mode, int)
OCTINTERP_API void open_create(const char *name, int mode)
OCTINTERP_API void close(void)
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
octave_value lookup_type(const std::string &nm)
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)
int rename(const std::string &from, const std::string &to)
std::string canonicalize_file_name(const std::string &name)
std::string tempnam(const std::string &dir, const std::string &pfx)
interpreter & __get_interpreter__(void)
type_info & __get_type_info__(void)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
std::string get_ASCII_filename(const std::string &orig_file_name, const bool allow_locale)
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)
bool check_hdf5_types(bool warn)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
octave_value lookup_type(const std::string &nm)
bool valid_identifier(const char *s)