void H5SoftLink::getAccessibleAttribute(const std::string & _name, const int pos, void * pvApiCtx) const { SciErr err; std::string lower(_name); std::transform(_name.begin(), _name.end(), lower.begin(), tolower); if (lower == "type") { const std::string linkType = getLinkType(); const char * _type = linkType.c_str(); err = createMatrixOfString(pvApiCtx, pos, 1, 1, &_type); if (err.iErr) { throw H5Exception(__LINE__, __FILE__, _("Cannot create a string on the stack.")); } return; } else if (lower == "target") { const std::string linkValue = getLinkValue(); const char * _target = linkValue.c_str(); err = createMatrixOfString(pvApiCtx, pos, 1, 1, &_target); if (err.iErr) { throw H5Exception(__LINE__, __FILE__, _("Cannot create a string on the stack.")); } return; } H5Object::getAccessibleAttribute(_name, pos, pvApiCtx); }
H5File::H5File(const std::string & _filename, const std::string & _path, const std::string & access, const hsize_t memberSize) : H5Object(H5Object::getRoot()), filename(_filename), path(_path), flags(getFlags(access)) { herr_t err; hid_t fapl; hid_t fapl2; if (filename.find("%d") == std::string::npos) { throw H5Exception(__LINE__, __FILE__, _("Invalid filename: must contain a '%d'.")); } fapl = H5Pcreate(H5P_FILE_ACCESS); H5Pset_fclose_degree(fapl, H5F_CLOSE_STRONG); fapl2 = H5Pcreate(H5P_FILE_ACCESS); H5Pset_fclose_degree(fapl2, H5F_CLOSE_STRONG); err = H5Pset_fapl_family(fapl, memberSize, fapl2); H5Pclose(fapl2); if (err < 0) { H5Pclose(fapl); throw H5Exception(__LINE__, __FILE__, _("Cannot set \'family\' as driver.")); } try { init(fapl); H5Pclose(fapl); } catch (const H5Exception & /*e*/) { H5Pclose(fapl); throw; } }
H5Object & H5ReferenceData::getData(const unsigned int size, const unsigned int * index) const { char * cdata = static_cast<char *>(data) + offset; void ** ref = 0; unsigned int pos = 0; unsigned int cumprod = 1; H5O_info_t info; H5Object * hobj; hid_t obj; hid_t file; ssize_t ssize; char * name = 0; std::string _name; for (unsigned int i = 0; i < size; i++) { pos += cumprod * index[i]; } if (pos >= totalSize) { throw H5Exception(__LINE__, __FILE__, _("Invalid index.")); } cdata += pos * (stride ? stride : dataSize); file = getFile().getH5Id(); ref = &(((void **)cdata)[0]); obj = H5Rdereference(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref); if (obj < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot open object at the given position.")); } ssize = H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, 0, 0); name = new char[ssize + 1]; H5Rget_name(file, datasetReference ? H5R_DATASET_REGION : H5R_OBJECT, ref, name, ssize + 1); _name = std::string(name); delete[] name; H5Oget_info(obj, &info); switch (info.type) { case H5O_TYPE_GROUP: hobj = new H5Group(getParent(), obj, _name); break; case H5O_TYPE_DATASET: hobj = new H5Dataset(getParent(), obj, _name); break; case H5O_TYPE_NAMED_DATATYPE: hobj = new H5Type(getParent(), obj, _name); break; case H5O_TYPE_UNKNOWN: default: H5Oclose(obj); throw H5Exception(__LINE__, __FILE__, _("Unknown HDF5 object")); } return *hobj; }
H5Link & H5Link::getLink(H5Object & _parent, const char * _name) { herr_t err; H5L_info_t info; H5Link * link = 0; err = H5Lget_info(_parent.getH5Id(), _name, &info, H5P_DEFAULT); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the link info")); } switch (info.type) { case H5L_TYPE_HARD: link = new H5HardLink(_parent, _name); break; case H5L_TYPE_SOFT: link = new H5SoftLink(_parent, _name); break; case H5L_TYPE_EXTERNAL: link = new H5ExternalLink(_parent, _name); break; case H5L_TYPE_ERROR: default: throw H5Exception(__LINE__, __FILE__, _("Invalid link type: %s."), _name); } return *link; }
std::string H5SoftLink::getLinkValue() const { herr_t err; H5L_info_t info; char * buf = 0; std::string ret; err = H5Lget_info(getParent().getH5Id(), name.c_str(), &info, H5P_DEFAULT); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the link info")); } buf = new char[info.u.val_size]; err = H5Lget_val(getParent().getH5Id(), name.c_str(), static_cast<void *>(buf), info.u.val_size, H5P_DEFAULT); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the link target")); } ret = std::string(buf); delete[] buf; return ret; }
H5Attribute::H5Attribute(H5Object & _parent, const std::string & _name) : H5Object(_parent, _name) { if (H5Aexists(getParent().getH5Id(), name.c_str()) <= 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot open attribute: %s"), name.c_str()); } attr = H5Aopen(getParent().getH5Id(), name.c_str(), H5P_DEFAULT); if (attr < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot open attribute: %s"), name.c_str()); } }
string FileHDF5::location() const { ssize_t size = H5Fget_name(hid, nullptr, 0); if (size < 0) { throw H5Exception("H5Fget_name failed"); } std::vector<char> buf(static_cast<size_t>(size + 1), 0); size = H5Fget_name(hid, buf.data(), buf.size()); if (size < 0) { throw H5Exception("H5Fget_name failed"); } return std::string(buf.data()); }
H5Link::H5Link(H5Object & _parent, const std::string & _name) : H5Object(_parent, _name) { if (H5Lexists(_parent.getH5Id(), name.c_str(), H5P_DEFAULT) <= 0) { throw H5Exception(__LINE__, __FILE__, _("The link %s does not exist."), name.c_str()); } }
H5StringData::~H5StringData() { if (transformedData) { delete[] transformedData; } else { char ** _data = reinterpret_cast<char **>(getData()); hid_t space = H5Screate_simple(1, &totalSize, 0); hid_t type = H5Tcopy(H5T_C_S1); H5Tset_size(type, H5T_VARIABLE); H5Tset_strpad(type, H5T_STR_NULLTERM); herr_t err = H5Dvlen_reclaim(type, space, H5P_DEFAULT, _data); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot free the memory associated with String data")); } H5Tclose(type); H5Sclose(space); } }
H5Dataset::H5Layout & H5Dataset::getLayout() { hid_t plist = H5Dget_create_plist(dataset); H5D_layout_t layout = H5Pget_layout(plist); H5Dataset::H5Layout * obj = 0; switch (layout) { case H5D_COMPACT: obj = new H5Dataset::H5CompactLayout(*this); break; case H5D_CONTIGUOUS: obj = new H5Dataset::H5ContiguousLayout(*this); break; case H5D_CHUNKED: obj = new H5Dataset::H5ChunkedLayout(*this); break; default: H5Pclose(plist); throw H5Exception(__LINE__, __FILE__, _("Invalid layout")); } H5Pclose(plist); return *obj; }
unsigned int DataType::member_count() const { int res = H5Tget_nmembers(hid); if (res < 0) { throw H5Exception("DataType::member_count(): H5Tget_nmembers faild"); } return static_cast<unsigned int>(res); }
void H5Dataset::init() { dataset = H5Dopen2(getParent().getH5Id(), name.c_str(), H5P_DEFAULT); if (dataset < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot open the given dataset %s."), name.c_str()); } }
void H5Group::init() { group = H5Gopen(getParent().getH5Id(), name.c_str(), H5P_DEFAULT); if (group < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot open the group %s."), name.c_str()); } }
void FileHDF5::createHeader() const { try { root.setAttr("format", FILE_FORMAT); root.setAttr("version", std::vector<int>{my_version.x(), my_version.y(), my_version.z()}); } catch ( ... ) { throw H5Exception("Could not open/create file"); } }
void H5File::flush(const bool local) const { herr_t err = H5Fflush(file, local ? H5F_SCOPE_LOCAL : H5F_SCOPE_GLOBAL); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Error in flushing the file.")); } }
void H5File::getFileHDF5Version(unsigned int * out) const { herr_t err = H5get_libversion(out, out + 1, out + 2); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot retrieve file version: %s"), filename.c_str()); } }
H5Dataspace & H5Attribute::getSpace() { hid_t space = H5Aget_space(attr); if (space < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the attribute dataspace")); } return *new H5Dataspace(*this, space); }
H5Dataspace & H5Dataset::getSpace() { hid_t space = H5Dget_space(dataset); if (space < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the dataspace associated with dataset named %s."), name.c_str()); } return *new H5Dataspace(*this, space); }
H5Type & H5Dataset::getDataType() { hid_t type = H5Dget_type(dataset); if (type < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the dataspace associated with dataset named %s."), name.c_str()); } return *new H5Type(*this, type); }
H5Type & H5Attribute::getDataType() { hid_t type = H5Aget_type(attr); if (type < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the attribute type")); } return *new H5Type(*this, type); }
void H5Group::ls(std::vector<std::string> & name, std::vector<std::string> & type) const { herr_t err; OpDataGetLs opdata(const_cast<H5Group *>(this), &name, &type); hsize_t idx = 0; err = H5Literate(group, H5_INDEX_NAME, H5_ITER_INC, &idx, getLsInfo, &opdata); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot list group links.")); } idx = 0; err = H5Aiterate(group, H5_INDEX_NAME, H5_ITER_INC, &idx, H5Object::getLsAttributes, &opdata); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot list group attributes.")); } }
H5Data & H5CompoundData::getData(const std::string fieldname) const { std::map<std::string, FieldInfo *>::const_iterator it = infos->find(fieldname); if (it != infos->end()) { return H5DataFactory::getObjectData(*const_cast<H5CompoundData *>(this), totalSize, it->second->size, it->second->type, ndims, dims, data, stride ? stride : dataSize, offset + it->second->offset, false); } throw H5Exception(__LINE__, __FILE__, _("Invalid field name: %s"), fieldname.c_str()); }
void H5Group::createGroup(H5Object & parent, const int size, const char ** names) { hid_t obj; hid_t loc = parent.getH5Id(); for (unsigned int i = 0; i < (unsigned int)size; i++) { if (H5Lexists(loc, names[i], H5P_DEFAULT) > 0) { throw H5Exception(__LINE__, __FILE__, _("The group already exists: %s."), names[i]); } obj = H5Gcreate(loc, names[i], H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (obj < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot create the group: %s."), names[i]); } H5Gclose(obj); } }
int * H5DataConverter::getHypermatrix(void * pvApiCtx, const int position, const int ndims, const hsize_t * dims) { static const char * hypermat[3] = {"hm", "dims", "entries"}; int * list = 0; SciErr err = createMList(pvApiCtx, position, 3, &list); if (err.iErr) { throw H5Exception(__LINE__, __FILE__, _("Cannot create an hypermatrix on the stack")); } err = createMatrixOfStringInList(pvApiCtx, position, list, 1, 1, 3, hypermat); if (err.iErr) { throw H5Exception(__LINE__, __FILE__, _("Cannot create an hypermatrix on the stack")); } if (sizeof(int) == sizeof(hsize_t)) { err = createMatrixOfInteger32InList(pvApiCtx, position, list, 2, 1, ndims, (int *)dims); if (err.iErr) { throw H5Exception(__LINE__, __FILE__, _("Cannot create an hypermatrix on the stack")); } } else { int * _dims = 0; err = allocMatrixOfInteger32InList(pvApiCtx, position, list, 2, 1, ndims, &_dims); if (err.iErr) { throw H5Exception(__LINE__, __FILE__, _("Cannot create an hypermatrix on the stack")); } for (int i = 0; i < ndims; i++) { _dims[i] = (int)dims[i]; } } return list; }
const unsigned int H5LinksList::getSize() const { H5G_info_t info; herr_t err = H5Gget_info(getParent().getH5Id(), &info); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the number of links.")); } return (unsigned int)info.nlinks; }
hsize_t H5File::getFileSize() const { herr_t err; hsize_t size = 0; err = H5Fget_filesize(file, &size); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot retrieve file size: %s"), filename.c_str()); } return size; }
const unsigned int H5Group::getLinksSize() const { herr_t err; H5G_info_t info; err = H5Gget_info(group, &info); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the links number")); } return (unsigned int)info.nlinks; }
void H5Dataset::label(const unsigned int size, const unsigned int * dim, const char ** names) const { hsize_t dims[__SCILAB_HDF5_MAX_DIMS__]; unsigned int ndims; hid_t space = H5Dget_space(dataset); if (space < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot get the dataspace associated with dataset named %s."), name.c_str()); } ndims = H5Sget_simple_extent_dims(space, (hsize_t *)dims, 0); H5Sclose(space); for (unsigned int i = 0; i < size; i++) { if (dim[i] > ndims) { throw H5Exception(__LINE__, __FILE__, _("Only %d dimensions."), ndims); } H5DSset_label(dataset, dim[i], names[i]); } }
hid_t H5Attribute::create(const hid_t loc, const std::string & name, const hid_t type, const hid_t targettype, const hid_t srcspace, const hid_t targetspace, void * data) { herr_t err; if (H5Aexists(loc, name.c_str()) > 0) { throw H5Exception(__LINE__, __FILE__, _("Attribute %s already exists."), name.c_str()); } hid_t attr = H5Acreate(loc, name.c_str(), targettype, targetspace == -1 ? srcspace : targetspace, H5P_DEFAULT, H5P_DEFAULT); if (attr < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot create a new attribute.")); } err = H5Awrite(attr, type, data); if (err < 0) { H5Aclose(attr); throw H5Exception(__LINE__, __FILE__, _("Cannot write data in the attribute.")); } return attr; }
void FileHDF5::close() { if (!isOpen()) return; data.close(); metadata.close(); root.close(); unsigned types = H5F_OBJ_GROUP|H5F_OBJ_DATASET|H5F_OBJ_DATATYPE; ssize_t obj_count = H5Fget_obj_count(hid, types); if (obj_count < 0) { throw H5Exception("FileHDF5::close(): Could not get object count"); } std::vector<hid_t> objs(static_cast<size_t>(obj_count)); if (obj_count > 0) { obj_count = H5Fget_obj_ids(hid, types, objs.size(), objs.data()); if (obj_count < 0) { throw H5Exception("FileHDF5::close(): Could not get objs"); } } for (auto obj : objs) { int ref_count = H5Iget_ref(obj); for (int j = 0; j < ref_count; j++) { H5Oclose(obj); } } H5Object::close(); }