Sane C++ Libraries
C++ Platform Abstraction Libraries
Path.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3//
4// Path - Parse filesystem paths for windows and posix
5#pragma once
6#include "../Strings/StringView.h"
7
8namespace SC
9{
10struct SC_COMPILER_EXPORT Path;
11struct String;
12template <typename T>
13struct Vector;
14} // namespace SC
15
18
21{
23 enum Type
24 {
25 AsPosix,
26 AsWindows,
27#if SC_PLATFORM_WINDOWS
28 AsNative = AsWindows
29#else
30 AsNative = AsPosix
31#endif
32 };
35 {
36 bool endsWithSeparator = false;
37
38 // TODO: ParsedView::directory and base are not defined consistently
39
40 Type type = AsPosix;
46
61 [[nodiscard]] bool parseWindows(StringView input);
62
77 [[nodiscard]] bool parsePosix(StringView input);
78 };
79
86 [[nodiscard]] static bool join(String& output, Span<const StringView> inputs,
87 StringView separator = SeparatorStringView(), bool skipEmpty = false);
88
97 [[nodiscard]] static bool parseNameExtension(const StringView input, StringView& name, StringView& extension);
98
104 [[nodiscard]] static bool parse(StringView input, Path::ParsedView& pathView, Type type);
105
120 [[nodiscard]] static StringView dirname(StringView input, Type type, int repeat = 0);
121
133 [[nodiscard]] static StringView basename(StringView input, Type type);
134
145 [[nodiscard]] static StringView basename(StringView input, StringView suffix);
146
162 [[nodiscard]] static bool isAbsolute(StringView input, Type type);
163
164 struct Windows
165 {
166 static const char Separator = '\\';
167
168 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "\\"_a8; };
169 };
170
171 struct Posix
172 {
173 static const char Separator = '/';
174
175 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "/"_a8; }
176 };
177
179#if SC_PLATFORM_WINDOWS
180 static constexpr char Separator = '\\';
181#else
182 static constexpr char Separator = '/';
183#endif
184
186#if SC_PLATFORM_WINDOWS
187 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "\\"_a8; };
188#else
189 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "/"_a8; }
190#endif
191
205 [[nodiscard]] static bool normalize(StringView view, Vector<StringView>& components, String* output, Type type);
206
221 [[nodiscard]] static bool relativeFromTo(StringView source, StringView destination, String& output, Type type,
222 Type outputType = AsNative);
223
229 [[nodiscard]] static bool append(String& output, Span<const StringView> paths, Type inputType);
230
234 [[nodiscard]] static bool endsWithSeparator(StringView path);
235
240
241 [[nodiscard]] static bool normalizeUNCAndTrimQuotes(StringView fileLocation, Vector<StringView>& components,
242 String& outputPath, Type type);
243
244 private:
245 [[nodiscard]] static bool appendTrailingSeparator(String& path, Type type);
246
247 [[nodiscard]] static StringView removeTrailingSeparator(StringView path);
248 struct Internal;
249};
250
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
Holds the various parsed components of a path.
Definition: Path.h:35
StringView root
Ex. "C:\\" on windows - "/" on posix.
Definition: Path.h:41
bool parseWindows(StringView input)
Parses all components on windows input path.
StringView base
Ex. "base" for "C:\\dir\\base" on windows or "/dir/base" on posix.
Definition: Path.h:43
StringView name
Ex. "name" for "C:\\dir\\name.ext" on windows or "/dir/name.ext" on posix.
Definition: Path.h:44
StringView ext
Ex. "ext" for "C:\\dir\\name.ext" on windows or "/dir/name.ext" on posix.
Definition: Path.h:45
Type type
Indicates if this is a windows or posix path.
Definition: Path.h:40
StringView directory
Ex. "C:\\dir" on windows - "/dir" on posix.
Definition: Path.h:42
bool parsePosix(StringView input)
Parses all components on posix input path.
Definition: Path.h:172
Definition: Path.h:165
Represents a posix or windows file system path.
Definition: Path.h:21
static constexpr char Separator
Path separator char for current platform.
Definition: Path.h:182
static StringView basename(StringView input, StringView suffix)
Returns the base name of a path.
static bool relativeFromTo(StringView source, StringView destination, String &output, Type type, Type outputType=AsNative)
Get relative path that appended to source resolves to destination.
static bool parseNameExtension(const StringView input, StringView &name, StringView &extension)
Splits a StringView of type "name.ext" into "name" and "ext".
static bool append(String &output, Span< const StringView > paths, Type inputType)
Append to an existing path a series of StringView with a separator.
static bool join(String &output, Span< const StringView > inputs, StringView separator=SeparatorStringView(), bool skipEmpty=false)
Joins multiple StringView with a Separator into an output String.
static StringView removeStartingSeparator(StringView path)
Return a path without its (potential) starting separator.
static bool normalize(StringView view, Vector< StringView > &components, String *output, Type type)
Resolves all .. to output a normalized path String.
static bool parse(StringView input, Path::ParsedView &pathView, Type type)
Splits a Posix or Windows path into a ParsedView.
static StringView dirname(StringView input, Type type, int repeat=0)
Returns the directory name of a path.
static constexpr StringView SeparatorStringView()
Path separator StringView for current platform.
Definition: Path.h:189
static bool endsWithSeparator(StringView path)
Check if the path ends with a Windows or Posix separator.
static StringView basename(StringView input, Type type)
Returns the base name of a path.
Type
Path type (windows or posix)
Definition: Path.h:24
static bool isAbsolute(StringView input, Type type)
Checks if a path is absolute.
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:20
A non-modifiable owning string with associated encoding.
Definition: String.h:30
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51