Duckstation/src/common/path.h

88 lines
3.4 KiB
C
Raw Normal View History

2024-05-05 10:21:54 +00:00
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
2022-07-08 11:57:06 +00:00
#pragma once
#include "types.h"
#include <string>
#include <string_view>
#include <vector>
namespace Path {
/// Converts any forward slashes to backslashes on Win32.
2024-05-05 10:21:54 +00:00
std::string ToNativePath(std::string_view path);
2022-07-08 11:57:06 +00:00
void ToNativePath(std::string* path);
/// Builds a path relative to the specified file
2024-05-05 10:21:54 +00:00
std::string BuildRelativePath(std::string_view filename, std::string_view new_filename);
2022-07-08 11:57:06 +00:00
/// Joins path components together, producing a new path.
2024-05-05 10:21:54 +00:00
std::string Combine(std::string_view base, std::string_view next);
2022-07-08 11:57:06 +00:00
/// Removes all .. and . components from a path.
2024-05-05 10:21:54 +00:00
std::string Canonicalize(std::string_view path);
2022-07-08 11:57:06 +00:00
void Canonicalize(std::string* path);
/// Sanitizes a filename for use in a filesystem.
2024-05-05 10:21:54 +00:00
std::string SanitizeFileName(std::string_view str, bool strip_slashes = true);
void SanitizeFileName(std::string* str, bool strip_slashes = true);
2022-07-08 11:57:06 +00:00
2024-03-15 05:21:06 +00:00
/// Mutates the path to remove any MAX_PATH limits (for Windows).
std::string RemoveLengthLimits(std::string_view str);
void RemoveLengthLimits(std::string* path);
2022-07-08 11:57:06 +00:00
/// Returns true if the specified path is an absolute path (C:\Path on Windows or /path on Unix).
2024-05-05 10:21:54 +00:00
bool IsAbsolute(std::string_view path);
2022-07-08 11:57:06 +00:00
2023-12-05 05:21:37 +00:00
/// Resolves any symbolic links in the specified path.
2024-05-05 10:21:54 +00:00
std::string RealPath(std::string_view path);
2023-12-05 05:21:37 +00:00
2022-07-08 11:57:06 +00:00
/// Makes the specified path relative to another (e.g. /a/b/c, /a/b -> ../c).
/// Both paths must be relative, otherwise this function will just return the input path.
2024-05-05 10:21:54 +00:00
std::string MakeRelative(std::string_view path, std::string_view relative_to);
2022-07-08 11:57:06 +00:00
/// Returns a view of the extension of a filename.
2024-05-05 10:21:54 +00:00
std::string_view GetExtension(std::string_view path);
2022-07-08 11:57:06 +00:00
/// Removes the extension of a filename.
2024-05-05 10:21:54 +00:00
std::string_view StripExtension(std::string_view path);
2022-07-08 11:57:06 +00:00
/// Replaces the extension of a filename with another.
2024-05-05 10:21:54 +00:00
std::string ReplaceExtension(std::string_view path, std::string_view new_extension);
2022-07-08 11:57:06 +00:00
/// Returns the directory component of a filename.
2024-05-05 10:21:54 +00:00
std::string_view GetDirectory(std::string_view path);
2022-07-08 11:57:06 +00:00
/// Returns the filename component of a filename.
2024-05-05 10:21:54 +00:00
std::string_view GetFileName(std::string_view path);
2022-07-08 11:57:06 +00:00
/// Returns the file title (less the extension and path) from a filename.
2024-05-05 10:21:54 +00:00
std::string_view GetFileTitle(std::string_view path);
2022-07-08 11:57:06 +00:00
/// Changes the filename in a path.
2024-05-05 10:21:54 +00:00
std::string ChangeFileName(std::string_view path, std::string_view new_filename);
void ChangeFileName(std::string* path, std::string_view new_filename);
2022-07-08 11:57:06 +00:00
/// Appends a directory to a path.
2024-05-05 10:21:54 +00:00
std::string AppendDirectory(std::string_view path, std::string_view new_dir);
void AppendDirectory(std::string* path, std::string_view new_dir);
2022-07-08 11:57:06 +00:00
/// Splits a path into its components, handling both Windows and Unix separators.
2024-05-05 10:21:54 +00:00
std::vector<std::string_view> SplitWindowsPath(std::string_view path);
2022-07-08 11:57:06 +00:00
std::string JoinWindowsPath(const std::vector<std::string_view>& components);
/// Splits a path into its components, only handling native separators.
2024-05-05 10:21:54 +00:00
std::vector<std::string_view> SplitNativePath(std::string_view path);
2022-07-08 11:57:06 +00:00
std::string JoinNativePath(const std::vector<std::string_view>& components);
2024-03-13 09:39:10 +00:00
/// URL encodes the specified string.
std::string URLEncode(std::string_view str);
/// Decodes the specified escaped string.
std::string URLDecode(std::string_view str);
/// Returns a URL for a given path. The path should be absolute.
std::string CreateFileURL(std::string_view path);
2022-07-08 11:57:06 +00:00
} // namespace Path