📂 golxzn::os::filesystem 📂 1.6.5
golxzn filesystem submodule
Loading...
Searching...
No Matches
filesystem.hpp
Go to the documentation of this file.
1/**
2 * @file golxzn/os/filesystem.hpp
3 * @author Ruslan Golovinskii (golxzn@gmail.com)
4 * @brief Header file with the declaration of the Golxzn Filesystem class
5 * @date 2023-10-25
6 *
7 * @copyright Copyright (c) 2023
8 */
9#pragma once
10
11#if !defined(GOLXZN_OS_FILESYSTEM)
12#define GOLXZN_OS_FILESYSTEM
13#endif // !defined(GOLXZN_OS_FILESYSTEM)
14
15#include <span>
16#include <string>
17#include <memory>
18#include <string_view>
19#include <unordered_map>
20
21#if defined(GOLXZN_OS_ALIASES)
22#include <golxzn/os/aliases.hpp>
23#endif // defined(GOLXZN_OS_ALIASES)
24
25namespace golxzn::os {
26
27#if !defined(GOLXZN_OS_ALIASES)
28
29using u16 = uint16_t;
30using byte = std::byte;
31using usize = std::size_t;
32
33#endif // defined(GOLXZN_OS_ALIASES)
34
35namespace details {
36
37template<class T>
38struct data_view {
39public:
40 using value_type = T;
41 using const_pointer = const T *;
42
43 template<class Iterator>
44 constexpr data_view(Iterator begin, Iterator end) noexcept : m_data{ &*begin }, m_length{ end - begin } {}
45
46 template<class Container>
47 constexpr data_view(const Container &container) noexcept
48 : data_view{ std::begin(container), std::end(container) } {}
49
50 [[nodiscard]] constexpr const_pointer data() const noexcept{ return m_data; }
51 [[nodiscard]] constexpr usize size() const noexcept { return m_length; }
52
53 [[nodiscard]] constexpr const_pointer begin() const noexcept{ return m_data; }
54 [[nodiscard]] constexpr const_pointer end() const noexcept{ return std::next(m_data, m_length); }
55
56private:
57 const_pointer m_data{};
58 const usize m_length{};
59};
60
61} // namespace
62
63/**
64 * @brief Golxzn Resource Manager
65 * @details Use this class to load resources from the program's directory.
66 * @warning Every methods with string arguments instead of wstring cause a memory allocation.
67 */
68class filesystem final {
69public:
70 /** @brief Map of the protocol extensions and their prefixes */
71 using associations_type = std::unordered_map<std::wstring, std::wstring>;
72
73 static constexpr std::wstring_view none{ L"" }; ///< Empty wide string
74 static constexpr std::wstring::value_type separator{ L'/' }; ///< Path separator
75 static constexpr std::wstring_view default_application_name{ L"unknown_application" }; ///< Default application name
76 static constexpr std::wstring_view default_assets_directory_name{ L"assets" }; ///< Default assets directory name
77 static constexpr std::wstring_view protocol_separator{ L"://" }; ///< Protocol separator
78
79 static constexpr std::string_view none_narrow{ "" }; ///< Narrow version of golxzn::os::filesystem::none
80 static constexpr std::string::value_type separator_narrow{ '/' }; ///< Narrow version of golxzn::os::filesystem::separator
81 static constexpr std::string_view default_application_name_narrow{ "unknown_application" }; ///< Narrow version of golxzn::os::filesystem::default_application_name
82 static constexpr std::string_view default_assets_directory_name_narrow{ "assets" }; ///< Narrow version of golxzn::os::filesystem::default_assets_directory_name
83 static constexpr std::string_view protocol_separator_narrow{ "://" }; ///< Narrow version of golxzn::os::filesystem::protocol_separator
84
85
86
87 /**
88 * @brief Struct returned by some methods to tell if there's an error
89 * @warning This struct has unexplicit conversion to bool! Use carefully!
90 */
91 struct error {
92 std::wstring message; ///< Error message. If it's golxzn::os::filesystem::none, everything is OK.
93
94 /**
95 * @brief Checks if there's an error
96 *
97 * @return `true` - Something wrong (error message is not empty)
98 * @return `false` - All right (error message is empty)
99 */
100 bool has_error() const noexcept;
101
102 /**
103 * @brief Non-implicit conversion to bool
104 *
105 * @return `true` - All right. There's no error
106 * @return `false` - An error occurred
107 */
108 operator bool() const noexcept;
109 };
110 static inline const error OK{ std::wstring{ none } }; ///< OK struct
111
112 filesystem() = delete;
113
114 /** @addtogroup initialization Initialization and setting up
115 * @{
116 */
117
118 /**
119 * @brief Initialize the Resource Manager
120 *
121 * @param application_name Your application name or the name of the user assets directory
122 * @param assets_path The name of the assets directory or the full path to the directory.
123 * If there's [LETTER]:/ or / at the beginning of the string, the path will be used as it is.
124 * Otherwise the path is relative to the program's directory.
125 * @return golxzn::os::filesystem::error - The error message or an empty string if there's no error
126 */
127 [[nodiscard]] static error initialize(const std::wstring_view application_name,
128 const std::wstring_view assets_path = default_assets_directory_name);
129
130 /**
131 * @brief Sets the application name object
132 * @details Sets the name of the application which is used in the directory path for user data.
133 * If the name is empty, the default name will be used.
134 * @warning This method doesn't change the name of the user data directory!
135 * @param application_name application name
136 */
137 static void set_application_name(const std::wstring_view application_name) noexcept;
138
139 /**
140 * @brief Add the association between the protocol and the prefix. (ex. L"res://" and L"user://")
141 * @details Add custom protocol to your application. For example, you could have a L"ab-test://"
142 * association with your own separated assets directory.
143 * @warning L"res://", L"user://", L"temp://" are reserved and cannot be used.
144 * @param protocol Protocol extension (ex. "res://"). Has to end with "://"
145 * @param prefix Prefix of the path. Has to end with a slash!
146 */
147 static void associate(std::wstring_view protocol, std::wstring &&prefix) noexcept;
148
149 /** @} */
150
151 /** @addtogroup read Reading files
152 * @{
153 */
154
155 /**
156 * @brief Read whole binary file
157 *
158 * @warning This method throws an exception `std::invalid_argument` if the path has no protocol!
159 * @param path Path to the file
160 * @return `std::vector<byte>` - The data or an empty vector if there's an reading error.
161 */
162 [[nodiscard]] static std::vector<byte> read_binary(const std::wstring_view path);
163
164 /**
165 * @brief Read whole text file
166 *
167 * @warning This method throws an exception `std::invalid_argument` if the path has no protocol!
168 * @param path Path to the file
169 * @return `std::string` - The data or an empty string if there's an reading error.
170 */
171 [[nodiscard]] static std::string read_text(const std::wstring_view path);
172
173 /**
174 * @brief Construct @p Custom class by binary data from file
175 *
176 * @tparam Custom Class to construct. Has to have a constructor with std::vector<byte> argument
177 * @param path Path to the file
178 * @return `Custom` - Constructed class
179 */
180 template<class Custom>
181 [[nodiscard]] static auto read_binary(const std::wstring_view path)
182 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, Custom>;
183
184 /**
185 * @brief Construct @p Custom class by text data from file
186 *
187 * @tparam Custom Class to construct. Has to have a constructor with std::string argument
188 * @param path Path to the file
189 * @return `Custom` - Constructed class
190 */
191 template<class Custom>
192 [[nodiscard]] static auto read_text(const std::wstring_view path)
193 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, Custom>;
194
195 /**
196 * @brief Construct @p std::shared_ptr<Custom> by binary data from file
197 *
198 * @tparam Custom Class to construct. Has to have a public constructor with std::vector<byte> argument
199 * @param path Path to the file
200 * @return `std::shared_ptr<Custom>` - Constructed shared class
201 */
202 template<class Custom>
203 [[nodiscard]] static auto read_shared_binary(const std::wstring_view path)
204 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::shared_ptr<Custom>>;
205
206 /**
207 * @brief Construct @p std::shared_ptr<Custom> by binary data from file
208 *
209 * @tparam Custom Class to construct. Has to have a public constructor with std::string argument
210 * @param path Path to the file
211 * @return `std::shared_ptr<Custom>` - Constructed shared class
212 */
213 template<class Custom>
214 [[nodiscard]] static auto read_shared_text(const std::wstring_view path)
215 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::shared_ptr<Custom>>;
216
217 /**
218 * @brief Construct @p std::shared_ptr<Custom> by binary data from file
219 *
220 * @tparam Custom Class to construct. Has to have a public constructor with std::vector<byte> argument
221 * @param path Path to the file
222 * @return `std::unique_ptr<Custom>` - Constructed unique class
223 */
224 template<class Custom>
225 [[nodiscard]] static auto read_unique_binary(const std::wstring_view path)
226 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::unique_ptr<Custom>>;
227
228 /**
229 * @brief Construct @p std::shared_ptr<Custom> by binary data from file
230 *
231 * @tparam Custom Class to construct. Has to have a public constructor with std::string argument
232 * @param path Path to the file
233 * @return `std::unique_ptr<Custom>` - Constructed unique class
234 */
235 template<class Custom>
236 [[nodiscard]] static auto read_unique_text(const std::wstring_view path)
237 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::unique_ptr<Custom>>;
238
239 /** @} */
240
241 /** @addtogroup write Writing files
242 * @{
243 */
244
245 /**
246 * @brief Write binary data to a file
247 *
248 * @param path Path to the file
249 * @param data Data to write to the file
250 * @return golxzn::os::filesystem::error - The error message or an empty string if there's no error
251 */
252 [[nodiscard]] static error write_binary(const std::wstring_view path, const details::data_view<byte> &data);
253
254 /**
255 * @brief Write binary data to a file
256 *
257 * @param path Path to the file
258 * @param data Data to write to the file
259 * @return golxzn::os::filesystem::error - The error message or an empty string if there's no error
260 */
261 [[nodiscard]] static error write_binary(const std::wstring_view path, const std::initializer_list<byte> data);
262
263 /**
264 * @brief Append binary data to a file
265 *
266 * @param path Path to the file
267 * @param data Data to write to the file
268 * @return golxzn::os::filesystem::error - The error message or an empty string if there's no error
269 */
270 [[nodiscard]] static error append_binary(const std::wstring_view path, const details::data_view<byte> &data);
271
272 /**
273 * @brief Append binary data to a file
274 *
275 * @param path Path to the file
276 * @param data Data to write to the file
277 * @return golxzn::os::filesystem::error - The error message or an empty string if there's no error
278 */
279 [[nodiscard]] static error append_binary(const std::wstring_view path, const std::initializer_list<byte> data);
280
281 /**
282 * @brief Write text data to a file
283 *
284 * @param path Path to the file
285 * @param text Text to write to the file
286 * @return golxzn::os::filesystem::error - The error message or an empty string if there's no error
287 */
288 [[nodiscard]] static error write_text(const std::wstring_view path, const std::string_view text);
289
290 /**
291 * @brief Append text data to a file
292 *
293 * @param path Path to the file
294 * @param text Text to write to the file
295 * @return golxzn::os::filesystem::error - The error message or an empty string if there's no error
296 */
297 [[nodiscard]] static error append_text(const std::wstring_view path, const std::string_view text);
298
299 /**
300 * @brief Write text data to a file
301 *
302 * @param path Path to the file
303 * @param text Text to write to the file
304 * @return golxzn::os::filesystem::error - The error message or an empty string if there's no error
305 */
306 [[nodiscard]] static error write_text(const std::wstring_view path, const std::wstring_view text);
307
308 /**
309 * @brief Append text data to a file
310 *
311 * @param path Path to the file
312 * @param text Text to write to the file
313 * @return golxzn::os::filesystem::error - The error message or an empty string if there's no error
314 */
315 [[nodiscard]] static error append_text(const std::wstring_view path, const std::wstring_view text);
316
317 /** @} */
318
319 /**
320 * @brief Get the association object
321 *
322 * @param protocol The protocol. Has to end with "://". If the prefix is not set, it returns an
323 * golxzn::os::filesystem::none
324 * @return `std::wstring_view` - Prefix or golxzn::os::filesystem::none
325 */
326 [[nodiscard]] static std::wstring_view get_association(const std::wstring_view protocol) noexcept;
327
328 /**
329 * @brief Get the application name
330 *
331 * @return `std::string_view` - application name
332 */
333 [[nodiscard]] static std::wstring_view application_name() noexcept;
334
335 /**
336 * @brief Get the user data directory object
337 * @details Same as golxzn::os::filesystem::get_association(L"user://")
338 * Gets the full path of the user data directory:
339 * - __Windows__: `%USERPROFILE%/AppData/Roaming/<application name>`
340 * - __MacOS__: `$HOME/Library/Application Support/<application name>`
341 * - __Linux__: `$XDG_CONFIG_HOME/<application name>` or `$HOME/.config/<application name>`
342 *
343 * @return User directory or golxzn::os::filesystem::none if it's not set
344 */
345 [[nodiscard]] static std::wstring_view user_data_directory() noexcept;
346
347 /**
348 * @brief Get the assets directories
349 * @details Same as golxzn::os::filesystem::get_association(L"res://")
350 *
351 * @return assets directory or golxzn::os::filesystem::none if it's not set
352 */
353 [[nodiscard]] static std::wstring_view assets_directory() noexcept;
354
355 /**
356 * @brief Get the associations
357 *
358 * @return `const associations_type& associations` - Map of the protocol extensions and their prefixes
359 */
360 [[nodiscard]] static const associations_type &associations() noexcept;
361
362 /**
363 * @brief Join two paths with a slash.
364 *
365 * @param left Path that will be changed
366 * @param right Path to append
367 */
368 static void join(std::wstring &left, std::wstring_view right) noexcept;
369
370 /**
371 * @brief Join two paths with a slash.
372 *
373 * @param left left path
374 * @param right right path
375 * @return `std::wstring` - Joined path
376 */
377 [[nodiscard]] static std::wstring join(std::wstring_view left, std::wstring_view right) noexcept;
378
379 /**
380 * @brief Remove the last path component.
381 *
382 * @param path path
383 */
384 static void parent_directory(std::wstring &path) noexcept;
385
386 /**
387 * @brief Remove the last path component.
388 *
389 * @param path path
390 * @return `std::wstring` - Parent directory
391 */
392 [[nodiscard]] static std::wstring parent_directory(std::wstring_view path) noexcept;
393
394 /**
395 * @brief Fix the path separators to the '/' and remove the trailing slash.
396 *
397 * @param path std::wstring_view path
398 * @return `std::wstring` - Canonical path
399 */
400 [[nodiscard]] static std::wstring normalize(std::wstring_view path);
401
402 /** @addtogroup fdmanip Files and directories manipulation
403 * @{
404 */
405
406 /**
407 * @brief Check if an entry exists.
408 *
409 * @param path directory or file path
410 * @return `true` if exists, `false` otherwise
411 */
412 [[nodiscard]] static bool exists(const std::wstring_view path) noexcept;
413
414 /**
415 * @brief Check if an entry is a file.
416 *
417 * @param path path to the file entry
418 * @return `true` if is file, `false` otherwise
419 */
420 [[nodiscard]] static bool is_file(const std::wstring_view path);
421
422 /**
423 * @brief Check if an entry is a directory.
424 *
425 * @param path path to the directory entry
426 * @return `true` if is directory, `false` otherwise
427 */
428 [[nodiscard]] static bool is_directory(const std::wstring_view path);
429
430 /**
431 * @brief Create a directory (recursively).
432 *
433 * @param path path to the directory
434 * @return golxzn::os::filesystem::error - filesystem::OK or the error message
435 */
436 [[nodiscard]] static error make_directory(const std::wstring_view path);
437
438 /**
439 * @brief Remove a directory (recursively).
440 *
441 * @param path path to the directory
442 * @return golxzn::os::filesystem::error - filesystem::OK or the error message
443 */
444 [[nodiscard]] static error remove_directory(const std::wstring_view path);
445
446 /**
447 * @brief Remove a file.
448 *
449 * @param path path to the file
450 * @return golxzn::os::filesystem::error - filesystem::OK or the error message
451 */
452 [[nodiscard]] static error remove_file(const std::wstring_view path);
453
454 /**
455 * @brief List an entry (file or directory).
456 *
457 * @param path path to the entry
458 * @return golxzn::os::filesystem::error - filesystem::OK or the error message
459 */
460 [[nodiscard]] static error remove(const std::wstring_view path);
461
462 // [[nodiscard]] static error move_file(const std::wstring_view path, const std::wstring_view destination);
463
464 // [[nodiscard]] static error copy_file(const std::wstring_view path, const std::wstring_view destination);
465
466 /** @} */
467
468 /**
469 * @brief Get the absolute path of this application.
470 * @returns std::wstring Absolute path of the application or, in the failure case, a "./".
471 */
472 [[nodiscard]] static std::wstring current_directory();
473
474 /**
475 * @brief Get all entries in a directory.
476 *
477 * @param path path to the directory
478 * @return `std::vector<std::wstring>` - vector of entries
479 */
480 [[nodiscard]] static std::vector<std::wstring> entries(const std::wstring_view path);
481
482 /**
483 * @brief Convert a string to wide string
484 *
485 * @param str string to convert
486 * @return `std::wstring` - converted wide string
487 */
488 [[nodiscard]] static std::wstring to_wide(const std::string_view str) noexcept;
489
490 /**
491 * @brief Convert a wide string to string
492 *
493 * @param wstr wide string to convert
494 * @return `std::string` - converted string
495 */
496 [[nodiscard]] static std::string to_narrow(const std::wstring_view wstr) noexcept;
497
498
499
500 /** @addtogroup aliases Narrow string aliases
501 * @warning Methods with a narrow string arguments instead of wide string could cause a memory allocation.
502 * @{
503 */
504
505 /// @brief Narrow string alias for golxzn::os::filesystem::initialize(const std::wstring_view, const std::wstring_view)
506 [[nodiscard]] static error initialize(const std::string_view application_name,
507 const std::string_view assets_path = default_assets_directory_name_narrow);
508
509 /// @brief Narrow string alias for golxzn::os::filesystem::set_application_name(const std::wstring_view)
510 static void set_application_name(const std::string_view application_name) noexcept;
511
512 /// @brief Narrow string alias for golxzn::os::filesystem::associate(const std::wstring_view, const std::wstring_view)
513 static void associate(const std::string_view protocol, const std::string_view prefix) noexcept;
514
515 /// @brief Narrow string alias for golxzn::os::filesystem::read_binary(const std::wstring_view path)
516 [[nodiscard]] static std::vector<byte> read_binary(const std::string_view path);
517
518 /// @brief Narrow string alias for golxzn::os::filesystem::read_text(const std::wstring_view path)
519 [[nodiscard]] static std::string read_text(const std::string_view path);
520
521 /// @brief Narrow string alias for golxzn::os::filesystem::read_binary(const std::wstring_view path)
522 template<class Custom>
523 [[nodiscard]] static auto read_binary(const std::string_view path)
524 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, Custom>;
525
526 /// @brief Narrow string alias for golxzn::os::filesystem::read_text(const std::wstring_view path)
527 template<class Custom>
528 [[nodiscard]] static auto read_text(const std::string_view path)
529 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, Custom>;
530
531 /// @brief Narrow string alias for golxzn::os::filesystem::read_shared_binary(const std::wstring_view path)
532 template<class Custom>
533 [[nodiscard]] static auto read_shared_binary(const std::string_view path)
534 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::shared_ptr<Custom>>;
535
536 /// @brief Narrow string alias for golxzn::os::filesystem::read_shared_text(const std::wstring_view path)
537 template<class Custom>
538 [[nodiscard]] static auto read_shared_text(const std::string_view path)
539 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::shared_ptr<Custom>>;
540
541 /// @brief Narrow string alias for golxzn::os::filesystem::read_unique_binary(const std::wstring_view path)
542 template<class Custom>
543 [[nodiscard]] static auto read_unique_binary(const std::string_view path)
544 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::unique_ptr<Custom>>;
545
546 /// @brief Narrow string alias for golxzn::os::filesystem::read_unique_text(const std::wstring_view path)
547 template<class Custom>
548 [[nodiscard]] static auto read_unique_text(const std::string_view path)
549 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::unique_ptr<Custom>>;
550
551 /// @brief Narrow string alias for golxzn::os::filesystem::write_binary(const std::wstring_view path, const details::data_view<byte> &data)
552 [[nodiscard]] static error write_binary(const std::string_view path, const details::data_view<byte> &data);
553
554 /// @brief Narrow string alias for golxzn::os::filesystem::write_binary(const std::wstring_view path, const std::initializer_list<byte> data)
555 [[nodiscard]] static error write_binary(const std::string_view path, const std::initializer_list<byte> data);
556
557 /// @brief Narrow string alias for golxzn::os::filesystem::append_binary(const std::wstring_view path, const details::data_view<byte> &data)
558 [[nodiscard]] static error append_binary(const std::string_view path, const details::data_view<byte> &data);
559
560 /// @brief Narrow string alias for golxzn::os::filesystem::append_binary(const std::wstring_view path, const std::initializer_list<byte> data)
561 [[nodiscard]] static error append_binary(const std::string_view path, const std::initializer_list<byte> data);
562
563 /// @brief Narrow string alias for golxzn::os::filesystem::write_text(const std::wstring_view path, const std::string_view text)
564 [[nodiscard]] static error write_text(const std::string_view path, const std::string_view text);
565
566 /// @brief Narrow string alias for golxzn::os::filesystem::append_text(const std::wstring_view path, const std::string_view text)
567 [[nodiscard]] static error append_text(const std::string_view path, const std::string_view text);
568
569 /// @brief Narrow string alias for golxzn::os::filesystem::write_text(const std::wstring_view path, const std::wstring_view text)
570 [[nodiscard]] static error write_text(const std::string_view path, const std::wstring_view text);
571
572 /// @brief Narrow string alias for golxzn::os::filesystem::append_text(const std::wstring_view path, const std::wstring_view text)
573 [[nodiscard]] static error append_text(const std::string_view path, const std::wstring_view text);
574
575 /// @brief Narrow string alias for golxzn::os::filesystem::get_association(const std::wstring_view protocol)
576 [[nodiscard]] static std::wstring_view get_association(const std::string_view protocol) noexcept;
577
578 /// @brief Narrow string alias for golxzn::os::filesystem::join(std::wstring &left, std::wstring_view right)
579 static void join(std::string &left, std::string_view right) noexcept;
580
581 /// @brief Narrow string alias for golxzn::os::filesystem::join(std::wstring_view left, std::wstring_view right)
582 [[nodiscard]] static std::string join(std::string_view left, std::string_view right) noexcept;
583
584 /// @brief Narrow string alias for golxzn::os::filesystem::parent_directory(std::wstring &path)
585 static void parent_directory(std::string &path) noexcept;
586
587 /// @brief Narrow string alias for golxzn::os::filesystem::parent_directory(std::wstring_view path)
588 [[nodiscard]] static std::string parent_directory(std::string_view path) noexcept;
589
590 /// @brief Narrow string alias for golxzn::os::filesystem::normalize(const std::wstring_view path)
591 [[nodiscard]] static std::wstring normalize(const std::string_view path);
592
593 /// @brief Narrow string alias for golxzn::os::filesystem::exists(const std::wstring_view path)
594 [[nodiscard]] static bool exists(const std::string_view path) noexcept;
595
596 /// @brief Narrow string alias for golxzn::os::filesystem::is_file(const std::wstring_view path)
597 [[nodiscard]] static bool is_file(const std::string_view path);
598
599 /// @brief Narrow string alias for golxzn::os::filesystem::is_directory(const std::wstring_view path)
600 [[nodiscard]] static bool is_directory(const std::string_view path);
601
602 /// @brief Narrow string alias for golxzn::os::filesystem::make_directory(const std::wstring_view path)
603 [[nodiscard]] static error make_directory(const std::string_view path);
604
605 /// @brief Narrow string alias for golxzn::os::filesystem::remove_directory(const std::wstring_view path)
606 [[nodiscard]] static error remove_directory(const std::string_view path);
607
608 /// @brief Narrow string alias for golxzn::os::filesystem::remove_file(const std::wstring_view path)
609 [[nodiscard]] static error remove_file(const std::string_view path);
610
611 /// @brief Narrow string alias for golxzn::os::filesystem::remove(const std::wstring_view path)
612 [[nodiscard]] static error remove(const std::string_view path);
613
614 /// @brief Narrow stirng alias for golxzn::os::filesystem::entries(const std::wstring_view path)
615 [[nodiscard]] static std::vector<std::string> entries(const std::string_view path);
616
617
618 /** @} */
619
620private:
621 static std::wstring appname;
622 static associations_type associations_map;
623
624 static std::wstring_view get_protocol(const std::wstring_view path) noexcept;
625 static std::wstring replace_association_prefix(std::wstring_view path) noexcept;
626 static std::wstring setup_assets_directories(const std::wstring_view assets_path);
627 static std::wstring setup_user_data_directory();
628};
629
630using fs = filesystem;
631
632//======================================== Implementation ========================================//
633
634template<class Custom>
635auto filesystem::read_binary(const std::wstring_view path)
636 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, Custom> {
637 return Custom{ read_binary(path) };
638}
639template<class Custom>
640auto filesystem::read_text(const std::wstring_view path)
641 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, Custom> {
642 return Custom{ read_text(path) };
643}
644
645template<class Custom>
646auto filesystem::read_shared_binary(const std::wstring_view path)
647 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::shared_ptr<Custom>> {
648 return std::make_shared<Custom>(read_binary(path));
649}
650
651template<class Custom>
652auto filesystem::read_shared_text(const std::wstring_view path)
653 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::shared_ptr<Custom>> {
654 return std::make_shared<Custom>(read_text(path));
655}
656
657template<class Custom>
658auto filesystem::read_unique_binary(const std::wstring_view path)
659 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::unique_ptr<Custom>> {
660 return std::make_unique<Custom>(read_binary(path));
661}
662
663template<class Custom>
664auto filesystem::read_unique_text(const std::wstring_view path)
665 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::unique_ptr<Custom>> {
666 return std::make_unique<Custom>(read_text(path));
667}
668
669
670template<class Custom>
671auto filesystem::read_binary(const std::string_view path)
672 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, Custom> {
673 return Custom{ read_binary(path) };
674}
675template<class Custom>
676auto filesystem::read_text(const std::string_view path)
677 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, Custom> {
678 return Custom{ read_text(path) };
679}
680
681template<class Custom>
682auto filesystem::read_shared_binary(const std::string_view path)
683 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::shared_ptr<Custom>> {
684 return std::make_shared<Custom>(read_binary(path));
685}
686
687template<class Custom>
688auto filesystem::read_shared_text(const std::string_view path)
689 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::shared_ptr<Custom>> {
690 return std::make_shared<Custom>(read_text(path));
691}
692
693template<class Custom>
694auto filesystem::read_unique_binary(const std::string_view path)
695 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::unique_ptr<Custom>> {
696 return std::make_unique<Custom>(read_binary(path));
697}
698
699template<class Custom>
700auto filesystem::read_unique_text(const std::string_view path)
701 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::unique_ptr<Custom>> {
702 return std::make_unique<Custom>(read_text(path));
703}
704
705
706
707} // namespace golxzn::os
708
709namespace gxzn = golxzn;
Golxzn Resource Manager.
Definition filesystem.hpp:68
static void join(std::wstring &left, std::wstring_view right) noexcept
Join two paths with a slash.
static const associations_type & associations() noexcept
Get the associations.
static std::wstring to_wide(const std::string_view str) noexcept
Convert a string to wide string.
static std::wstring_view get_association(const std::wstring_view protocol) noexcept
Get the association object.
static std::wstring normalize(std::wstring_view path)
Fix the path separators to the '/' and remove the trailing slash.
static constexpr std::wstring::value_type separator
Path separator.
Definition filesystem.hpp:74
static constexpr std::wstring_view none
Empty wide string.
Definition filesystem.hpp:73
static std::string to_narrow(const std::wstring_view wstr) noexcept
Convert a wide string to string.
static constexpr std::string::value_type separator_narrow
Narrow version of golxzn::os::filesystem::separator.
Definition filesystem.hpp:80
static constexpr std::wstring_view protocol_separator
Protocol separator.
Definition filesystem.hpp:77
std::unordered_map< std::wstring, std::wstring > associations_type
Map of the protocol extensions and their prefixes.
Definition filesystem.hpp:71
static constexpr std::wstring_view default_assets_directory_name
Default assets directory name.
Definition filesystem.hpp:76
static std::wstring_view user_data_directory() noexcept
Get the user data directory object.
static constexpr std::string_view none_narrow
Narrow version of golxzn::os::filesystem::none.
Definition filesystem.hpp:79
static std::wstring_view application_name() noexcept
Get the application name.
static void parent_directory(std::wstring &path) noexcept
Remove the last path component.
static std::wstring_view assets_directory() noexcept
Get the assets directories.
static constexpr std::string_view protocol_separator_narrow
Narrow version of golxzn::os::filesystem::protocol_separator.
Definition filesystem.hpp:83
static constexpr std::wstring_view default_application_name
Default application name.
Definition filesystem.hpp:75
static std::vector< std::wstring > entries(const std::wstring_view path)
Get all entries in a directory.
static std::wstring current_directory()
Get the absolute path of this application.
static const error OK
OK struct.
Definition filesystem.hpp:110
static constexpr std::string_view default_assets_directory_name_narrow
Narrow version of golxzn::os::filesystem::default_assets_directory_name.
Definition filesystem.hpp:82
static constexpr std::string_view default_application_name_narrow
Narrow version of golxzn::os::filesystem::default_application_name.
Definition filesystem.hpp:81
static bool is_file(const std::wstring_view path)
Check if an entry is a file.
static error remove_file(const std::wstring_view path)
Remove a file.
static error remove(const std::wstring_view path)
List an entry (file or directory).
static bool is_directory(const std::wstring_view path)
Check if an entry is a directory.
static error remove_directory(const std::wstring_view path)
Remove a directory (recursively).
static error make_directory(const std::wstring_view path)
Create a directory (recursively).
static bool exists(const std::wstring_view path) noexcept
Check if an entry exists.
static error initialize(const std::wstring_view application_name, const std::wstring_view assets_path=default_assets_directory_name)
Initialize the Resource Manager.
static void associate(std::wstring_view protocol, std::wstring &&prefix) noexcept
Add the association between the protocol and the prefix. (ex. L"res://" and L"user://")
static void set_application_name(const std::wstring_view application_name) noexcept
Sets the application name object.
static std::vector< byte > read_binary(const std::wstring_view path)
Read whole binary file.
static auto read_shared_binary(const std::wstring_view path) -> std::enable_if_t< std::is_constructible_v< Custom, std::vector< byte > >, std::shared_ptr< Custom > >
Construct std::shared_ptr<Custom> by binary data from file.
Definition filesystem.hpp:646
static auto read_unique_text(const std::wstring_view path) -> std::enable_if_t< std::is_constructible_v< Custom, std::string >, std::unique_ptr< Custom > >
Construct std::shared_ptr<Custom> by binary data from file.
Definition filesystem.hpp:664
static auto read_unique_binary(const std::wstring_view path) -> std::enable_if_t< std::is_constructible_v< Custom, std::vector< byte > >, std::unique_ptr< Custom > >
Construct std::shared_ptr<Custom> by binary data from file.
Definition filesystem.hpp:658
static std::string read_text(const std::wstring_view path)
Read whole text file.
static auto read_shared_text(const std::wstring_view path) -> std::enable_if_t< std::is_constructible_v< Custom, std::string >, std::shared_ptr< Custom > >
Construct std::shared_ptr<Custom> by binary data from file.
Definition filesystem.hpp:652
static error append_text(const std::wstring_view path, const std::string_view text)
Append text data to a file.
static error append_text(const std::wstring_view path, const std::wstring_view text)
Append text data to a file.
static error write_text(const std::wstring_view path, const std::wstring_view text)
Write text data to a file.
static error append_binary(const std::wstring_view path, const details::data_view< byte > &data)
Append binary data to a file.
static error write_binary(const std::wstring_view path, const std::initializer_list< byte > data)
Write binary data to a file.
static error write_binary(const std::wstring_view path, const details::data_view< byte > &data)
Write binary data to a file.
static error write_text(const std::wstring_view path, const std::string_view text)
Write text data to a file.
static error append_binary(const std::wstring_view path, const std::initializer_list< byte > data)
Append binary data to a file.
Struct returned by some methods to tell if there's an error.
Definition filesystem.hpp:91
bool has_error() const noexcept
Checks if there's an error.
std::wstring message
Error message. If it's golxzn::os::filesystem::none, everything is OK.
Definition filesystem.hpp:92