Mantid
Loading...
Searching...
No Matches
NexusAddress.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7
9
10namespace Mantid::Nexus {
11
12namespace {
13std::filesystem::path const nxroot("/");
14
15std::filesystem::path cleanup(std::string const &s) {
16 std::filesystem::path ret(s);
17 if (ret == nxroot) {
18 // the root path is already clean
19 } else if (s.back() == '/') {
20 // make sure no entries end in "/" -- this confuses path location
21 ret = s.substr(0, s.size() - 1);
22 }
23 if (s.starts_with("//")) {
24 ret = s.substr(1);
25 }
26 // cast as lexically normal
27 return ret.lexically_normal();
28}
29
30std::filesystem::path cleanup(std::filesystem::path const &p) { return cleanup(p.string()); }
31
32} // namespace
33
34NexusAddress::NexusAddress(std::filesystem::path const &path)
35 : m_path(cleanup(path)), m_resolved_path(m_path.generic_string()) {}
36
37NexusAddress::NexusAddress(std::string const &path) : m_path(cleanup(path)), m_resolved_path(m_path.generic_string()) {}
38
39NexusAddress::NexusAddress(char const *const path) : NexusAddress(std::string(path)) {}
40
41NexusAddress::NexusAddress() : m_path(nxroot), m_resolved_path(m_path.generic_string()) {}
42
43NexusAddress &NexusAddress::operator=(std::string const &s) {
44 m_path = cleanup(s);
45 m_resolved_path = std::string(m_path.generic_string());
46 return *this;
47}
48
49bool NexusAddress::operator==(NexusAddress const &p) const { return m_path == p.m_path; }
50
51bool NexusAddress::operator==(std::string const &s) const { return m_resolved_path == s; }
52
53bool NexusAddress::operator==(char const *const s) const { return m_resolved_path == std::string(s); }
54
55bool NexusAddress::operator!=(NexusAddress const &p) const { return m_path != p.m_path; }
56
57bool NexusAddress::operator!=(std::string const &s) const { return m_resolved_path != s; }
58
59bool NexusAddress::operator!=(char const *const s) const { return m_resolved_path != std::string(s); }
60
61NexusAddress NexusAddress::operator/(std::string const &s) const { return *this / NexusAddress(s); }
62
63NexusAddress NexusAddress::operator/(char const *const s) const { return *this / NexusAddress(s); }
64
66 return NexusAddress(m_path / p.m_path.relative_path());
67}
68
69NexusAddress &NexusAddress::operator/=(std::string const &s) { return *this /= NexusAddress(s); }
70
71NexusAddress &NexusAddress::operator/=(char const *const s) { return *this /= NexusAddress(s); }
72
74 m_path = cleanup(m_path / p.m_path);
75 m_resolved_path = std::string(m_path.generic_string());
76 return *this;
77}
78
79bool NexusAddress::isAbsolute() const { return *m_path.begin() == nxroot; }
80
81bool NexusAddress::isRoot() const { return (m_path == nxroot); }
82
84
86
88
90
91std::string NexusAddress::operator+(std::string const &s) const { return m_resolved_path + s; }
92
93std::string NexusAddress::operator+(char const s[]) const { return m_resolved_path + s; }
94
95std::vector<std::string> NexusAddress::parts() const {
96 std::vector<std::string> names;
97 for (auto it = m_path.begin(); it != m_path.end(); it++) {
98 if (*it != nxroot) {
99 names.push_back(it->generic_string());
100 }
101 }
102 return names;
103}
104} // namespace Mantid::Nexus
105
106bool operator==(std::string const &s, Mantid::Nexus::NexusAddress const &p) { return s == p.string(); }
107
108bool operator!=(std::string const &s, Mantid::Nexus::NexusAddress const &p) { return s != p.string(); }
109
110std::string operator+(std::string const &s, Mantid::Nexus::NexusAddress const &p) { return s + p.string(); }
111
112std::string operator+(char const s[], Mantid::Nexus::NexusAddress const &p) { return s + p.string(); }
113
114std::ostream &operator<<(std::ostream &os, Mantid::Nexus::NexusAddress const &p) {
115 os << p.string();
116 return os;
117}
This simple class encapsulates some methods for working with paths inside a Nexus file.
std::string operator+(std::string const &s) const
bool operator==(NexusAddress const &p) const
std::vector< std::string > parts() const
std::filesystem::path m_path
standard filesystem path
NexusAddress & operator/=(std::string const &s)
NexusAddress parent_path() const
NexusAddress & operator=(NexusAddress const &nd)=default
NexusAddress operator/(std::string const &s) const
static NexusAddress root()
NexusAddress fromRoot() const
NexusAddress stem() const
bool operator!=(NexusAddress const &p) const
std::string const & string() const
MANTID_API_DLL std::ostream & operator<<(std::ostream &, const AlgorithmHistory &)
Prints a text representation.
MatrixWorkspace_sptr MANTID_API_DLL operator+(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Adds two workspaces.
Header for a base Nexus::Exception.
STL namespace.
constexpr bool operator==(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)
constexpr bool operator!=(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)