xlnt
path.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2024-2025 xlnt-community
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE
21 //
22 // @license: http://www.opensource.org/licenses/mit-license.php
23 // @author: see AUTHORS file
24 
25 #pragma once
26 
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include <xlnt/xlnt_config.hpp>
32 #include <xlnt/internal/features.hpp>
33 
34 #if XLNT_HAS_INCLUDE(<string_view>) && XLNT_HAS_FEATURE(U8_STRING_VIEW)
35  #include <string_view>
36 #endif
37 
38 namespace xlnt {
39 
43 class XLNT_API path
44 {
45 public:
49  static char system_separator();
50 
54  path();
55 
59  explicit path(const std::string &path_string);
60 
64  path(const std::string &path_string, char sep);
65 
66 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
67  explicit path(std::u8string_view path_string);
71 
75  path(std::u8string_view path_string, char sep);
76 #endif
77 
78  // general attributes
79 
83  bool is_relative() const;
84 
88  bool is_absolute() const;
89 
93  bool is_root() const;
94 
99  path parent() const;
100 
104  std::string filename() const;
105 
109  std::string extension() const;
110 
114  std::pair<std::string, std::string> split_extension() const;
115 
116  // conversion
117 
122  std::vector<std::string> split() const;
123 
128  const std::string &string() const;
129 
130 #ifdef _MSC_VER
131  std::wstring wstring() const;
136 #endif
137 
143  path resolve(const path &base_path) const;
144 
150  path relative_to(const path &base_path) const;
151 
152  // filesystem attributes
153 
158  bool exists() const;
159 
164  bool is_directory() const;
165 
170  bool is_file() const;
171 
172  // filesystem
173 
178  std::string read_contents() const;
179 
180  // mutators
181 
185  path append(const std::string &to_append) const;
186 
187 #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
188  path append(std::u8string_view to_append) const;
192 #endif
193 
197  path append(const path &to_append) const;
198 
202  bool operator==(const path &other) const;
203 
207  bool operator!=(const path &other) const;
208 
209 private:
218  char guess_separator() const;
219 
223  std::string internal_;
224 };
225 
226 } // namespace xlnt
227 
228 namespace std {
229 
233 template <>
234 struct hash<xlnt::path>
235 {
239  size_t operator()(const xlnt::path &p) const
240  {
241  static hash<string> hasher;
242  return hasher(p.string());
243  }
244 };
245 
246 } // namespace std
const std::string & string() const
Create a string representing this path separated by the provided separator or the system-default sepa...
Definition: cell_reference.hpp:262
size_t operator()(const xlnt::path &p) const
Returns a hashed represenation of the given path.
Definition: path.hpp:239
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37
bool operator==(std::nullptr_t, const cell &cell)
Returns true if this cell is uninitialized.
Encapsulates a path that points to location in a filesystem.
Definition: path.hpp:43
bool operator!=(const std::string &reference_string, const range_reference &ref)
Returns true if the string representation of the range is not equivalent to ref.