3
0
mirror of https://github.com/triqs/dft_tools synced 2024-12-25 13:53:40 +01:00
dft_tools/triqs/h5/file.cpp

53 lines
1.5 KiB
C++
Raw Normal View History

#include "./file.hpp"
#include "./base.hpp"
namespace triqs {
namespace h5 {
file::file(const char* name, unsigned flags) {
if ((flags == H5F_ACC_RDWR) || (flags == H5F_ACC_RDONLY)) {
id = H5Fopen(name, flags, H5P_DEFAULT);
if (id < 0) TRIQS_RUNTIME_ERROR << "HDF5 : cannot open file " << name;
return;
}
if (flags == H5F_ACC_TRUNC) {
id = H5Fcreate(name, flags, H5P_DEFAULT, H5P_DEFAULT);
if (id < 0) TRIQS_RUNTIME_ERROR << "HDF5 : cannot create file " << name;
return;
}
if (flags == H5F_ACC_EXCL) {
id = H5Fcreate(name, flags, H5P_DEFAULT, H5P_DEFAULT);
if (id < 0) TRIQS_RUNTIME_ERROR << "HDF5 : cannot create file " << name << ". Does it exists ?";
return;
}
TRIQS_RUNTIME_ERROR << "HDF5 file opening : flag not recognized";
}
//---------------------------------------------
file::file (hid_t id_) : h5_object(h5_object(id_)){}
file::file(h5_object obj) : h5_object(std::move(obj)) {
if (!H5Iis_valid(this->id)) TRIQS_RUNTIME_ERROR << "Invalid input in h5::file constructor from id";
if (H5Iget_type(this->id) != H5I_FILE) TRIQS_RUNTIME_ERROR << "h5::file constructor must take the id of a file ";
}
//---------------------------------------------
std::string file::name() const { // same function as for group
char _n[1];
ssize_t size = H5Fget_name(id, _n, 1); // first call, get the size only
std::vector<char> buf(size + 1, 0x00);
H5Fget_name(id, buf.data(), 1); // now get the name
std::string res = "";
res.append(&(buf.front()));
return res;
}
}
}