29using namespace std::string_literals;
32#define CHECK_OR_THROW(Cond, Mess) \
33 if (!(Cond)) throw std::runtime_error("Error in h5::file: "s + (Mess));
40 case 'r':
id = H5Fopen(
name, H5F_ACC_RDONLY, H5P_DEFAULT);
break;
42 case 'w':
id = H5Fcreate(
name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
break;
46 herr_t (*old_func)(
void *) =
nullptr;
47 void *old_client_data =
nullptr;
48 H5Eget_auto1(&old_func, &old_client_data);
49 H5Eset_auto1(
nullptr,
nullptr);
52 id = H5Fcreate(
name, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
55 H5Eset_auto1(old_func, old_client_data);
58 if (
id < 0)
id = H5Fopen(
name, H5F_ACC_RDWR, H5P_DEFAULT);
62 case 'e':
id = H5Fcreate(
name, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
break;
63 default:
throw std::runtime_error(
"File mode is not one of r, w, a, e");
67 if (
id < 0)
throw std::runtime_error(
"Opening/Creating the file "s +
name +
" failed");
73 ssize_t size = H5Fget_name(
id,
nullptr, 1);
76 std::vector<char> buf(size + 1, 0x00);
77 H5Fget_name(
id, buf.data(), size + 1);
81 res.append(&(buf.front()));
87 auto err = H5Fflush(
id, H5F_SCOPE_GLOBAL);
88 CHECK_OR_THROW((err >= 0),
"Flushing the file failed");
93 proplist fapl = H5Pcreate(H5P_FILE_ACCESS);
94 CHECK_OR_THROW((fapl >= 0),
"Creating the fapl failed");
97 auto err = H5Pset_fapl_core(fapl, (
size_t)(64 * 1024),
false);
98 CHECK_OR_THROW((err >= 0),
"Setting the core file driver in fapl failed");
101 this->
id = H5Fcreate(
"MemoryBuffer", 0, H5P_DEFAULT, fapl);
102 CHECK_OR_THROW((this->
is_valid()),
"Creating a buffered memory file failed");
105 file::file(
const std::byte *buf,
size_t size) {
107 proplist fapl = H5Pcreate(H5P_FILE_ACCESS);
108 CHECK_OR_THROW((fapl >= 0),
"Creating the fapl failed");
111 auto err = H5Pset_fapl_core(fapl, (
size_t)(64 * 1024),
false);
112 CHECK_OR_THROW((err >= 0),
"Setting the core file driver in fapl failed");
115 err = H5Pset_file_image(fapl, (
void *)buf, size);
116 CHECK_OR_THROW((err >= 0),
"Setting the file image to a given memory buffer failed");
119 this->
id = H5Fopen(
"MemoryBuffer", H5F_ACC_RDWR, fapl);
120 CHECK_OR_THROW((this->
is_valid()),
"Creating a buffered memory file failed");
125 auto f =
hid_t(*
this);
126 auto err = H5Fflush(f, H5F_SCOPE_GLOBAL);
127 CHECK_OR_THROW((err >= 0),
"Flushing the buffered memory file failed");
130 ssize_t image_len = H5Fget_file_image(f,
nullptr, (
size_t)0);
131 CHECK_OR_THROW((image_len > 0),
"Getting the file image size failed");
134 std::vector<std::byte> buf(image_len, std::byte{0});
135 ssize_t bytes_read = H5Fget_file_image(f, (
void *)buf.data(), (
size_t)image_len);
136 CHECK_OR_THROW(bytes_read == image_len,
"Writing file image to buffer failed");
std::string name() const
Get the name of the file.
std::vector< std::byte > as_buffer() const
Get a copy of the associated byte buffer.
void flush()
Flush the file by calling H5Fflush.
file()
Default constructor creates a buffered memory file.
A generic handle for HDF5 objects.
bool is_valid() const
Ensure that the wrapped HDF5 ID is valid (by calling H5Iis_valid).
Provides a handle to an HDF5 file.
int64_t hid_t
ID type used in HDF5.