3
0
mirror of https://github.com/triqs/dft_tools synced 2024-11-01 11:43:47 +01:00
dft_tools/triqs/h5/file.cpp
Olivier Parcollet 4af1afbdaf hdf5 : clean up
- For users : only change is :
   H5::H5File in apps. to be replaced by triqs::h5::file, same API.

- using only the C API because :
   - it is cleaner, better documented, more examples.
   - it is the native hdf5 interface.
   - simplify the installation e.g. on mac. Indeed, hdf5 is
     usually installed without C++ interface, which is optional.
     E.g. EPD et al., brew by default.
     Also the infamous mpi+ hdf5_cpp bug, for which we have no clean solution.

- clean the notion of parent of a group. Not needed, better iterate function in C LT API.
- modified doc : no need for C++ bindings any more.
- modified cmake to avoid requiring CPP bindings.
2014-06-22 13:57:47 +02:00

53 lines
1.5 KiB
C++

#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 : can not open file" << name;
return;
}
if (flags == H5F_ACC_TRUNC) {
id = H5Fcreate(name, flags, H5P_DEFAULT, H5P_DEFAULT);
if (id < 0) TRIQS_RUNTIME_ERROR << "HDF5 : can not create file " << name;
return;
}
if (flags == H5F_ACC_EXCL) {
id = H5Fcreate(name, flags, H5P_DEFAULT, H5P_DEFAULT);
if (id < 0) TRIQS_RUNTIME_ERROR << "HDF5 : can not 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;
}
}
}