h5gt 0.2.0
C++ wrapper for HDF5 library (based on HighFive project)
Loading...
Searching...
No Matches
H5File_misc.hpp
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
18namespace h5gt {
19
20namespace { // unnamed
21
22// libhdf5 uses a preprocessor trick on their oflags
23// we can not declare them constant without a mapper
24inline unsigned convert_open_flag(unsigned openFlags) {
25 unsigned res_open = 0;
26 if (openFlags & File::ReadOnly)
27 res_open |= H5F_ACC_RDONLY;
28 if (openFlags & File::ReadWrite)
29 res_open |= H5F_ACC_RDWR;
30 if (openFlags & File::Create)
31 res_open |= H5F_ACC_CREAT;
32 if (openFlags & File::Truncate)
33 res_open |= H5F_ACC_TRUNC;
34 if (openFlags & File::Excl)
35 res_open |= H5F_ACC_EXCL;
36 return res_open;
37}
38} // namespace
39
40
41inline File::File(const std::string& filename, unsigned openFlags,
42 const FileAccessProps& fileAccessProps){
43
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(false));
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(false))) < 0) {
73 HDF5ErrMapper::ToException<FileException>(
74 std::string("Unable to create file " + filename +
75 ". Probably you don't have permissions, incorrect directory or the file is busy by another process"));
76 }
77}
78
79inline bool File::operator==(const File& other) const {
80#if (H5_VERS_MAJOR >= 1 && H5_VERS_MINOR >= 12)
81 unsigned long fileNumLeft, fileNumRight;
82
83 if (H5Fget_fileno(_hid, &fileNumLeft) < 0){
84 HDF5ErrMapper::ToException<FileException>(
85 std::string("Unable to get file number for " + getFileName()));
86 }
87
88 if (H5Fget_fileno(other.getId(false), &fileNumRight) < 0){
89 HDF5ErrMapper::ToException<FileException>(
90 std::string("Unable to get file number for " + other.getFileName()));
91 }
92 return fileNumLeft == fileNumRight;
93#else
94 return getObjectInfo().getFileNumber() == other.getObjectInfo().getFileNumber();
95#endif
96}
97
98inline bool File::operator!=(const File& other) const {
99 return !(*this == other);
100}
101
102} // namespace h5gt
103
104#endif // H5FILE_MISC_HPP
Definition H5PropertyList.hpp:152
File class.
Definition H5File.hpp:25
@ Truncate
Open flag: Truncate a file if already existing.
Definition H5File.hpp:41
@ Excl
Open flag: Open will fail if file already exist.
Definition H5File.hpp:43
@ ReadWrite
Open flag: Read Write access.
Definition H5File.hpp:39
@ ReadOnly
Open flag: Read only access.
Definition H5File.hpp:37
@ Create
Open flag: Create non existing file.
Definition H5File.hpp:47
ObjectInfo getObjectInfo() const
Retrieve several infos about the current object (address, dates, etc)
Definition H5Object_misc.hpp:204
hid_t getId(const bool &increaseRefCount=false) const noexcept
getId
Definition H5Object_misc.hpp:172
bool isValid() const noexcept
isValid
Definition H5Object_misc.hpp:162
Utility class to disable HDF5 stack printing inside a scope.
Definition H5Utility.hpp:20