HighFive 2.3.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#ifndef H5FILE_MISC_HPP
10#define H5FILE_MISC_HPP
11
12#include <string>
13
14#include <H5Fpublic.h>
15
16#include "../H5Utility.hpp"
17#include "H5Utils.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
41
42inline File::File(const std::string& filename, unsigned openFlags,
43 const FileAccessProps& fileAccessProps) {
44 openFlags = convert_open_flag(openFlags);
45
46 unsigned createMode = openFlags & (H5F_ACC_TRUNC | H5F_ACC_EXCL);
47 unsigned openMode = openFlags & (H5F_ACC_RDWR | H5F_ACC_RDONLY);
48 bool mustCreate = createMode > 0;
49 bool openOrCreate = (openFlags & H5F_ACC_CREAT) > 0;
50
51 // open is default. It's skipped only if flags require creation
52 // If open fails it will try create() if H5F_ACC_CREAT is set
53 if (!mustCreate) {
54 // Silence open errors if create is allowed
55 std::unique_ptr<SilenceHDF5> silencer;
56 if (openOrCreate) silencer.reset(new SilenceHDF5());
57
58 _hid = H5Fopen(filename.c_str(), openMode, fileAccessProps.getId());
59
60 if (isValid()) return; // Done
61
62 if (openOrCreate) {
63 // Will attempt to create ensuring wont clobber any file
64 createMode = H5F_ACC_EXCL;
65 } else {
66 HDF5ErrMapper::ToException<FileException>(
67 std::string("Unable to open file " + filename));
68 }
69 }
70
71 if ((_hid = H5Fcreate(filename.c_str(), createMode, H5P_DEFAULT,
72 fileAccessProps.getId())) < 0) {
73 HDF5ErrMapper::ToException<FileException>(
74 std::string("Unable to create file " + filename));
75 }
76}
77
78inline const std::string& File::getName() const noexcept {
79 if (_filename.empty()) {
80 _filename = details::get_name([this](char* buffer, hsize_t length) {
81 return H5Fget_name(getId(), buffer, static_cast<size_t>(length));
82 });
83 }
84 return _filename;
85}
86
87inline void File::flush() {
88 if (H5Fflush(_hid, H5F_SCOPE_GLOBAL) < 0) {
89 HDF5ErrMapper::ToException<FileException>(
90 std::string("Unable to flush file " + getName()));
91 }
92}
93
94} // namespace HighFive
95
96#endif // H5FILE_MISC_HPP
void flush()
flush
Definition: H5File_misc.hpp:87
File(const std::string &filename, unsigned openFlags=ReadOnly, const FileAccessProps &fileAccessProps=FileAccessProps::Default())
File.
Definition: H5File_misc.hpp:42
@ Truncate
Open flag: Truncate a file if already existing.
Definition: H5File.hpp:37
@ Create
Open flag: Create non existing file.
Definition: H5File.hpp:43
@ ReadOnly
Open flag: Read only access.
Definition: H5File.hpp:33
@ ReadWrite
Open flag: Read Write access.
Definition: H5File.hpp:35
@ Excl
Open flag: Open will fail if file already exist.
Definition: H5File.hpp:39
const std::string & getName() const noexcept
Return the name of the file.
Definition: H5File_misc.hpp:78
hid_t getId() const noexcept
getId
Definition: H5Object_misc.hpp:55
bool isValid() const noexcept
isValid
Definition: H5Object_misc.hpp:51
hid_t _hid
Definition: H5Object.hpp:87
HDF5 property Lists.
Definition: H5PropertyList.hpp:62
Utility class to disable HDF5 stack printing inside a scope.
Definition: H5Utility.hpp:20
Definition: H5_definitions.hpp:15