path.h (3457B)
1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) 3 4 #pragma once 5 6 #include "types.h" 7 8 #include <string> 9 #include <string_view> 10 #include <vector> 11 12 namespace Path { 13 /// Converts any forward slashes to backslashes on Win32. 14 std::string ToNativePath(std::string_view path); 15 void ToNativePath(std::string* path); 16 17 /// Builds a path relative to the specified file 18 std::string BuildRelativePath(std::string_view filename, std::string_view new_filename); 19 20 /// Joins path components together, producing a new path. 21 std::string Combine(std::string_view base, std::string_view next); 22 23 /// Removes all .. and . components from a path. 24 std::string Canonicalize(std::string_view path); 25 void Canonicalize(std::string* path); 26 27 /// Sanitizes a filename for use in a filesystem. 28 std::string SanitizeFileName(std::string_view str, bool strip_slashes = true); 29 void SanitizeFileName(std::string* str, bool strip_slashes = true); 30 31 /// Mutates the path to remove any MAX_PATH limits (for Windows). 32 std::string RemoveLengthLimits(std::string_view str); 33 void RemoveLengthLimits(std::string* path); 34 35 /// Returns true if the specified path is an absolute path (C:\Path on Windows or /path on Unix). 36 bool IsAbsolute(std::string_view path); 37 38 /// Resolves any symbolic links in the specified path. 39 std::string RealPath(std::string_view path); 40 41 /// Makes the specified path relative to another (e.g. /a/b/c, /a/b -> ../c). 42 /// Both paths must be relative, otherwise this function will just return the input path. 43 std::string MakeRelative(std::string_view path, std::string_view relative_to); 44 45 /// Returns a view of the extension of a filename. 46 std::string_view GetExtension(std::string_view path); 47 48 /// Removes the extension of a filename. 49 std::string_view StripExtension(std::string_view path); 50 51 /// Replaces the extension of a filename with another. 52 std::string ReplaceExtension(std::string_view path, std::string_view new_extension); 53 54 /// Returns the directory component of a filename. 55 std::string_view GetDirectory(std::string_view path); 56 57 /// Returns the filename component of a filename. 58 std::string_view GetFileName(std::string_view path); 59 60 /// Returns the file title (less the extension and path) from a filename. 61 std::string_view GetFileTitle(std::string_view path); 62 63 /// Changes the filename in a path. 64 std::string ChangeFileName(std::string_view path, std::string_view new_filename); 65 void ChangeFileName(std::string* path, std::string_view new_filename); 66 67 /// Appends a directory to a path. 68 std::string AppendDirectory(std::string_view path, std::string_view new_dir); 69 void AppendDirectory(std::string* path, std::string_view new_dir); 70 71 /// Splits a path into its components, handling both Windows and Unix separators. 72 std::vector<std::string_view> SplitWindowsPath(std::string_view path); 73 std::string JoinWindowsPath(const std::vector<std::string_view>& components); 74 75 /// Splits a path into its components, only handling native separators. 76 std::vector<std::string_view> SplitNativePath(std::string_view path); 77 std::string JoinNativePath(const std::vector<std::string_view>& components); 78 79 /// URL encodes the specified string. 80 std::string URLEncode(std::string_view str); 81 82 /// Decodes the specified escaped string. 83 std::string URLDecode(std::string_view str); 84 85 /// Returns a URL for a given path. The path should be absolute. 86 std::string CreateFileURL(std::string_view path); 87 } // namespace Path