QMCPACK
hdf_path.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2023 QMCPACK developers.
6 //
7 // File developed by: Steven Hahn, hahnse@ornl.gov, Oak Ridge National Laboratory
8 //
9 // File created by: Steven Hahn, hahnse@ornl.gov, Oak Ridge National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include "hdf_path.h"
13 
14 namespace qmcplusplus
15 {
16 hdf_path::hdf_path() = default;
17 
18 hdf_path::hdf_path(std::string_view p) : path_{p} {};
19 
20 hdf_path& hdf_path::append(std::string_view p) { return append(hdf_path(p)); }
21 
23 {
24  if (p.has_root_directory())
25  path_.clear();
26  else if (!path_.empty() && path_.back() != '/')
27  path_.push_back('/');
28  return concat(p);
29 }
30 
31 hdf_path& hdf_path::concat(std::string_view p)
32 {
33  path_.append(p);
34  return *this;
35 }
36 
37 hdf_path& hdf_path::concat(const hdf_path& p) { return concat(p.string()); }
38 
39 hdf_path& hdf_path::operator/=(std::string_view p) { return append(p); }
40 
41 hdf_path& hdf_path::operator/=(const hdf_path& p) { return append(p); }
42 
43 hdf_path& hdf_path::operator+=(std::string_view p) { return concat(p); }
44 
45 hdf_path& hdf_path::operator+=(const hdf_path& p) { return concat(p); }
46 
48 {
49  // if path == '/' do nothing
50  if (path_.size() == 1 && has_root_directory())
51  return *this;
52 
53  auto pos = path_.find_last_of('/');
54 
55  // don't eliminate root group
56  if (pos == 0 && has_root_directory())
57  pos++;
58 
59  // if path is relative and only one group is present, clear the string
60  if (pos == std::string::npos)
61  path_.clear();
62  else
63  path_.erase(pos, std::string::npos);
64  return *this;
65 }
66 
68 {
70  return append(p);
71 }
72 
73 bool hdf_path::has_root_directory() const { return (!path_.empty() && path_[0] == '/'); }
74 
75 hdf_path operator/(const hdf_path& lhs, const hdf_path& rhs) { return hdf_path(lhs) /= rhs; }
76 
77 hdf_path operator/(const hdf_path& lhs, const std::string& rhs) { return hdf_path(lhs) /= std::string_view(rhs); }
78 
79 hdf_path operator/(const hdf_path& lhs, const char* rhs) { return hdf_path(lhs) /= std::string_view(rhs); }
80 
81 bool operator==(const hdf_path& lhs, const hdf_path& rhs) noexcept { return lhs.string() == rhs.string(); }
82 
83 } // namespace qmcplusplus
const std::string & string() const
Return std::string for use with HDF5.
Definition: hdf_path.h:67
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
bool operator==(const Matrix< T, Alloc > &lhs, const Matrix< T, Alloc > &rhs)
Definition: OhmmsMatrix.h:388
hdf_path & replace_subgroup(std::string_view p)
Replace the last subgroup in path.
Definition: hdf_path.cpp:67
std::string path_
Definition: hdf_path.h:74
hdf_path & concat(std::string_view p)
Concatenates two paths without introducing a directory separator.
Definition: hdf_path.cpp:31
hdf_path & append(std::string_view p)
Appends elements to the path with a directory separator.
Definition: hdf_path.cpp:20
hdf_path & remove_subgroup()
Remove last subgroup in path.
Definition: hdf_path.cpp:47
hdf_path & operator+=(std::string_view p)
Concatenates two paths without introducing a directory separator.
Definition: hdf_path.cpp:43
bool has_root_directory() const
Does the path absolute (true) or relative to the current group in hdf_archive (false)?
Definition: hdf_path.cpp:73
MakeReturn< BinaryNode< OpDivide, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t, typename CreateLeaf< Vector< T2, C2 > >::Leaf_t > >::Expression_t operator/(const Vector< T1, C1 > &l, const Vector< T2, C2 > &r)
hdf_path & operator/=(std::string_view p)
Appends elements to the path with a directory separator.
Definition: hdf_path.cpp:39
hdf_path()
Constructs an empty path.