HighFive 2.10.1
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5File_misc.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
3 *
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 */
9#pragma once
10
11#include <string>
12
13#include <H5Fpublic.h>
14
15#include "../H5Utility.hpp"
16#include "H5Utils.hpp"
17#include "h5f_wrapper.hpp"
18
19namespace HighFive {
20
21namespace { // unnamed
22
23// libhdf5 uses a preprocessor trick on their oflags
24// we can not declare them constant without a mapper
25inline unsigned convert_open_flag(unsigned openFlags) {
26 unsigned res_open = 0;
27 if (openFlags & File::ReadOnly)
28 res_open |= H5F_ACC_RDONLY;
29 if (openFlags & File::ReadWrite)
30 res_open |= H5F_ACC_RDWR;
31 if (openFlags & File::Create)
32 res_open |= H5F_ACC_CREAT;
33 if (openFlags & File::Truncate)
34 res_open |= H5F_ACC_TRUNC;
35 if (openFlags & File::Excl)
36 res_open |= H5F_ACC_EXCL;
37 return res_open;
38}
39} // namespace
40
41inline File::File(const std::string& filename,
42 unsigned openFlags,
43 const FileAccessProps& fileAccessProps)
44 : File(filename, openFlags, FileCreateProps::Default(), fileAccessProps) {}
45
46
47inline File::File(const std::string& filename,
48 unsigned openFlags,
49 const FileCreateProps& fileCreateProps,
50 const FileAccessProps& fileAccessProps) {
51 openFlags = convert_open_flag(openFlags);
52
53 unsigned createMode = openFlags & (H5F_ACC_TRUNC | H5F_ACC_EXCL);
54 unsigned openMode = openFlags & (H5F_ACC_RDWR | H5F_ACC_RDONLY);
55 bool mustCreate = createMode > 0;
56 bool openOrCreate = (openFlags & H5F_ACC_CREAT) > 0;
57
58 // open is default. It's skipped only if flags require creation
59 // If open fails it will try create() if H5F_ACC_CREAT is set
60 if (!mustCreate) {
61 // Silence open errors if create is allowed
62 std::unique_ptr<SilenceHDF5> silencer;
63 if (openOrCreate)
64 silencer.reset(new SilenceHDF5());
65
66 _hid = detail::nothrow::h5f_open(filename.c_str(), openMode, fileAccessProps.getId());
67
68 if (isValid())
69 return; // Done
70
71 if (openOrCreate) {
72 // Will attempt to create ensuring wont clobber any file
73 createMode = H5F_ACC_EXCL;
74 } else {
76 std::string("Unable to open file " + filename));
77 }
78 }
79
80 auto fcpl = fileCreateProps.getId();
81 auto fapl = fileAccessProps.getId();
82 _hid = detail::h5f_create(filename.c_str(), createMode, fcpl, fapl);
83}
84
85inline const std::string& File::getName() const noexcept {
86 if (_filename.empty()) {
87 _filename = details::get_name([this](char* buffer, size_t length) {
88 return detail::h5f_get_name(getId(), buffer, length);
89 });
90 }
91 return _filename;
92}
93
94inline hsize_t File::getMetadataBlockSize() const {
95 auto fapl = getAccessPropertyList();
96 return MetadataBlockSize(fapl).getSize();
97}
98
99inline std::pair<H5F_libver_t, H5F_libver_t> File::getVersionBounds() const {
100 auto fapl = getAccessPropertyList();
101 auto fileVer = FileVersionBounds(fapl);
102 return fileVer.getVersion();
103}
104
105#if H5_VERSION_GE(1, 10, 1)
106inline H5F_fspace_strategy_t File::getFileSpaceStrategy() const {
107 auto fcpl = getCreatePropertyList();
108 FileSpaceStrategy spaceStrategy(fcpl);
109 return spaceStrategy.getStrategy();
110}
111
112inline hsize_t File::getFileSpacePageSize() const {
113 auto fcpl = getCreatePropertyList();
114
115 if (getFileSpaceStrategy() != H5F_FSPACE_STRATEGY_PAGE) {
117 std::string("Cannot obtain page size as paged allocation is not used."));
118 }
119
120 return FileSpacePageSize(fcpl).getPageSize();
121}
122#endif
123
124inline void File::flush() {
125 detail::h5f_flush(_hid, H5F_SCOPE_GLOBAL);
126}
127
128inline size_t File::getFileSize() const {
129 hsize_t sizeValue = 0;
130 detail::h5f_get_filesize(_hid, &sizeValue);
131 return static_cast<size_t>(sizeValue);
132}
133
134inline size_t File::getFreeSpace() const {
135 return static_cast<size_t>(detail::h5f_get_freespace(_hid));
136}
137
138} // namespace HighFive
File class.
Definition H5File.hpp:24
FileCreateProps getCreatePropertyList() const
Get the list of properties for creation of this file.
Definition H5File.hpp:104
size_t getFileSize() const
Get the size of this file in bytes.
Definition H5File_misc.hpp:128
std::pair< H5F_libver_t, H5F_libver_t > getVersionBounds() const
Returns the HDF5 version compatibility bounds.
Definition H5File_misc.hpp:99
void flush()
flush
Definition H5File_misc.hpp:124
size_t getFreeSpace() const
Get the amount of tracked, unused space in bytes.
Definition H5File_misc.hpp:134
FileAccessProps getAccessPropertyList() const
Get the list of properties for accession of this file.
Definition H5File.hpp:109
hsize_t getMetadataBlockSize() const
Returns the block size for metadata in bytes.
Definition H5File_misc.hpp:94
@ Truncate
Open flag: Truncate a file if already existing.
Definition H5File.hpp:34
@ Create
Open flag: Create non existing file.
Definition H5File.hpp:40
@ ReadOnly
Open flag: Read only access.
Definition H5File.hpp:30
@ ReadWrite
Open flag: Read Write access.
Definition H5File.hpp:32
@ Excl
Open flag: Open will fail if file already exist.
Definition H5File.hpp:36
File()=default
const std::string & getName() const noexcept
Return the name of the file.
Definition H5File_misc.hpp:85
Configure the version bounds for the file.
Definition H5PropertyList.hpp:342
Configure the metadata block size to use writing to files.
Definition H5PropertyList.hpp:362
hsize_t getSize() const
Definition H5PropertyList_misc.hpp:257
hid_t getId() const noexcept
getId
Definition H5Object_misc.hpp:69
bool isValid() const noexcept
isValid
Definition H5Object_misc.hpp:65
hid_t _hid
Definition H5Object.hpp:105
HDF5 property Lists.
Definition H5PropertyList.hpp:160
Utility class to disable HDF5 stack printing inside a scope.
Definition H5Utility.hpp:24
Definition H5_definitions.hpp:22
static void ToException(const std::string &prefix_msg)
Definition H5Exception_misc.hpp:43