Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 08:55:33

0001 /*
0002  *  Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
0003  *
0004  *  Distributed under the Boost Software License, Version 1.0.
0005  *    (See accompanying file LICENSE_1_0.txt or copy at
0006  *          http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  */
0009 #pragma once
0010 
0011 #include <string>
0012 
0013 #include <H5Fpublic.h>
0014 
0015 #include "../H5Utility.hpp"
0016 #include "H5Utils.hpp"
0017 #include "h5f_wrapper.hpp"
0018 
0019 namespace HighFive {
0020 
0021 namespace {  // unnamed
0022 
0023 // libhdf5 uses a preprocessor trick on their oflags
0024 // we can not declare them constant without a mapper
0025 inline unsigned convert_open_flag(unsigned openFlags) {
0026     unsigned res_open = 0;
0027     if (openFlags & File::ReadOnly)
0028         res_open |= H5F_ACC_RDONLY;
0029     if (openFlags & File::ReadWrite)
0030         res_open |= H5F_ACC_RDWR;
0031     if (openFlags & File::Create)
0032         res_open |= H5F_ACC_CREAT;
0033     if (openFlags & File::Truncate)
0034         res_open |= H5F_ACC_TRUNC;
0035     if (openFlags & File::Excl)
0036         res_open |= H5F_ACC_EXCL;
0037     return res_open;
0038 }
0039 }  // namespace
0040 
0041 inline File::File(const std::string& filename,
0042                   unsigned openFlags,
0043                   const FileAccessProps& fileAccessProps)
0044     : File(filename, openFlags, FileCreateProps::Default(), fileAccessProps) {}
0045 
0046 
0047 inline File::File(const std::string& filename,
0048                   unsigned openFlags,
0049                   const FileCreateProps& fileCreateProps,
0050                   const FileAccessProps& fileAccessProps) {
0051     openFlags = convert_open_flag(openFlags);
0052 
0053     unsigned createMode = openFlags & (H5F_ACC_TRUNC | H5F_ACC_EXCL);
0054     unsigned openMode = openFlags & (H5F_ACC_RDWR | H5F_ACC_RDONLY);
0055     bool mustCreate = createMode > 0;
0056     bool openOrCreate = (openFlags & H5F_ACC_CREAT) > 0;
0057 
0058     // open is default. It's skipped only if flags require creation
0059     // If open fails it will try create() if H5F_ACC_CREAT is set
0060     if (!mustCreate) {
0061         // Silence open errors if create is allowed
0062         std::unique_ptr<SilenceHDF5> silencer;
0063         if (openOrCreate)
0064             silencer.reset(new SilenceHDF5());
0065 
0066         _hid = detail::nothrow::h5f_open(filename.c_str(), openMode, fileAccessProps.getId());
0067 
0068         if (isValid())
0069             return;  // Done
0070 
0071         if (openOrCreate) {
0072             // Will attempt to create ensuring wont clobber any file
0073             createMode = H5F_ACC_EXCL;
0074         } else {
0075             HDF5ErrMapper::ToException<FileException>(
0076                 std::string("Unable to open file " + filename));
0077         }
0078     }
0079 
0080     auto fcpl = fileCreateProps.getId();
0081     auto fapl = fileAccessProps.getId();
0082     _hid = detail::h5f_create(filename.c_str(), createMode, fcpl, fapl);
0083 }
0084 
0085 inline const std::string& File::getName() const noexcept {
0086     if (_filename.empty()) {
0087         _filename = details::get_name([this](char* buffer, size_t length) {
0088             return detail::h5f_get_name(getId(), buffer, length);
0089         });
0090     }
0091     return _filename;
0092 }
0093 
0094 inline hsize_t File::getMetadataBlockSize() const {
0095     auto fapl = getAccessPropertyList();
0096     return MetadataBlockSize(fapl).getSize();
0097 }
0098 
0099 inline std::pair<H5F_libver_t, H5F_libver_t> File::getVersionBounds() const {
0100     auto fapl = getAccessPropertyList();
0101     auto fileVer = FileVersionBounds(fapl);
0102     return fileVer.getVersion();
0103 }
0104 
0105 #if H5_VERSION_GE(1, 10, 1)
0106 inline H5F_fspace_strategy_t File::getFileSpaceStrategy() const {
0107     auto fcpl = getCreatePropertyList();
0108     FileSpaceStrategy spaceStrategy(fcpl);
0109     return spaceStrategy.getStrategy();
0110 }
0111 
0112 inline hsize_t File::getFileSpacePageSize() const {
0113     auto fcpl = getCreatePropertyList();
0114 
0115     if (getFileSpaceStrategy() != H5F_FSPACE_STRATEGY_PAGE) {
0116         HDF5ErrMapper::ToException<FileException>(
0117             std::string("Cannot obtain page size as paged allocation is not used."));
0118     }
0119 
0120     return FileSpacePageSize(fcpl).getPageSize();
0121 }
0122 #endif
0123 
0124 inline void File::flush() {
0125     detail::h5f_flush(_hid, H5F_SCOPE_GLOBAL);
0126 }
0127 
0128 inline size_t File::getFileSize() const {
0129     hsize_t sizeValue = 0;
0130     detail::h5f_get_filesize(_hid, &sizeValue);
0131     return static_cast<size_t>(sizeValue);
0132 }
0133 
0134 inline size_t File::getFreeSpace() const {
0135     return static_cast<size_t>(detail::h5f_get_freespace(_hid));
0136 }
0137 
0138 }  // namespace HighFive