TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches
format.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
22#include "./format.hpp"
23#include "./group.hpp"
24#include "./object.hpp"
25#include "./stl/string.hpp"
26
27#include <stdexcept>
28#include <string>
29
30namespace h5 {
31
32 void read_hdf5_format(object obj, std::string &s) {
33 h5_read_attribute(obj, "Format", s);
34
35 // backward compatibility
36 if (s == "") { h5_read_attribute(obj, "TRIQS_HDF5_data_scheme", s); }
37 }
38
39 std::string read_hdf5_format(group g) {
40 std::string s;
41 read_hdf5_format(g, s); // NOLINT (slicing is intended)
42 return s;
43 }
44
45 void read_hdf5_format_from_key(group g, std::string const &key, std::string &s) {
46 h5_read_attribute_from_key(g, key, "Format", s);
47
48 // backward compatibility
49 if (s == "") { h5_read_attribute_from_key(g, key, "TRIQS_HDF5_data_scheme", s); }
50 }
51
52 void assert_hdf5_format_as_string(group g, const char *tag_expected, bool ignore_if_absent) {
53 auto tag = read_hdf5_format(g);
54 if (ignore_if_absent and tag.empty()) return;
55 if (tag != tag_expected)
56 throw std::runtime_error("Error in assert_hdf5_format_as_string: hdf5_format tag mistmatch: " + tag + " != " + tag_expected);
57 }
58
59} // namespace h5
A handle to an HDF5 group.
Definition group.hpp:44
Provides utilities for reading and writing hdf5_format tags.
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
void assert_hdf5_format_as_string(group g, const char *tag_expected, bool ignore_if_absent)
Assert that the hdf5_format tag attached to the given group is the same as the given tag.
Definition format.cpp:52
void read_hdf5_format(object obj, std::string &s)
Read an hdf5_format tag from an HDF5 attribute with the name 'Format'.
Definition format.cpp:32
void read_hdf5_format_from_key(group g, std::string const &key, std::string &s)
Read an hdf5_format tag from an HDF5 attribute with the name 'Format'.
Definition format.cpp:45
T h5_read_attribute_from_key(group g, std::string const &key, std::string const &name)
Generic implementation for reading an HDF5 attribute.
Definition generic.hpp:184
T h5_read_attribute(object obj, std::string const &name)
Generic implementation for reading an HDF5 attribute.
Definition generic.hpp:120
Provides a generic handle for HDF5 objects.
Provides functions to read/write std::string, char* and h5::char_buf objects from/to HDF5.