File size: 4,111 Bytes
2492322 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | // Copyright (c) 2023, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.
#ifndef MAMBA_UTIL_PATH_MANIP_HPP
#define MAMBA_UTIL_PATH_MANIP_HPP
#include <optional>
#include <string>
#include <string_view>
namespace mamba::util
{
/**
* Lightweight file path manipulation.
*
* The purpose of this file is to provide a lightweight functions for manipulating paths
* for things that manipulate "path-like" objects, such as parsers and URLs.
* In general, users should prefer using the correct abstraction, such as @ref URL
* and @ref u8path.
* However some features provided here, such as @ref expand_home, are not available elsewhere.
*/
inline static constexpr char preferred_path_separator_posix = '/';
inline static constexpr char preferred_path_separator_win = '\\';
/**
* Return true is the input is explicitly a path.
*
* Explicit path are:
* - Absolute path
* - Path starting with '~'
* - Relative paths starting with "./" or "../"
*/
[[nodiscard]] auto is_explicit_path(std::string_view input) -> bool;
/**
* Return the path drive letter, if any, or none.
*/
[[nodiscard]] auto path_get_drive_letter(std::string_view path) -> std::optional<char>;
/**
* Check if a Windows path (not URL) starts with a drive letter.
*/
[[nodiscard]] auto path_has_drive_letter(std::string_view path) -> bool;
/**
* Detect the separator used in a path.
*/
[[nodiscard]] auto path_win_detect_sep(std::string_view path) -> std::optional<char>;
/**
* Convert the Windows path separators to Posix ones.
*/
[[nodiscard]] auto path_win_to_posix(std::string path) -> std::string;
/**
* Convert the Posix path separators to Windows ones.
*/
[[nodiscard]] auto path_posix_to_win(std::string path) -> std::string;
/**
* Convert the path separators to the desired one.
*/
[[nodiscard]] auto path_to_sep(std::string path, char sep) -> std::string;
/**
* Convert the Windows path separators to Posix ones on Windows only.
*/
[[nodiscard]] auto path_to_posix(std::string path) -> std::string;
/**
* Check that a path is a prefix of another path.
*/
[[nodiscard]] auto
path_is_prefix(std::string_view parent, std::string_view child, char sep = '/') -> bool;
/**
* Concatenate paths with the given separator.
*/
[[nodiscard]] auto path_concat(std::string_view parent, std::string_view child, char sep)
-> std::string;
/**
* Concatenate paths with '/' on Unix and detected separator on Windows.
*/
[[nodiscard]] auto path_concat(std::string_view parent, std::string_view child) -> std::string;
/**
* Expand a leading '~' with the given home directory, assuming the given separator.
*/
[[nodiscard]] auto expand_home(std::string_view path, std::string_view home, char sep)
-> std::string;
/**
* Expand a leading '~' with the given home directory.
*/
[[nodiscard]] auto expand_home(std::string_view path, std::string_view home) -> std::string;
/**
* Expand a leading '~' with the user home directory.
*/
[[nodiscard]] auto expand_home(std::string_view path) -> std::string;
/**
* If the path starts with the given home directory, replace it with a leading '~'.
*
* This assumes the given separator is used separate paths.
*/
[[nodiscard]] auto shrink_home(std::string_view path, std::string_view home, char sep)
-> std::string;
/**
* If the path starts with the given home directory, replace it with a leading '~'.
*/
[[nodiscard]] auto shrink_home(std::string_view path, std::string_view home) -> std::string;
/**
* If the path starts with the user home directory, replace it with a leading '~'.
*/
[[nodiscard]] auto shrink_home(std::string_view path) -> std::string;
}
#endif
|