Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Path.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Strings/StringView.h"
5
6namespace SC
7{
8struct String;
9
12
15{
17 enum Type
18 {
19 AsPosix,
20 AsWindows,
21#if SC_PLATFORM_WINDOWS
22 AsNative = AsWindows
23#else
24 AsNative = AsPosix
25#endif
26 };
29 {
30 bool endsWithSeparator = false;
31
32 // TODO: ParsedView::directory and base are not defined consistently
33
34 Type type = AsPosix;
40
55 [[nodiscard]] bool parseWindows(StringView input);
56
71 [[nodiscard]] bool parsePosix(StringView input);
72 };
73
80 [[nodiscard]] static bool join(String& output, Span<const StringView> inputs,
81 StringView separator = SeparatorStringView(), bool skipEmpty = false);
82
91 [[nodiscard]] static bool parseNameExtension(const StringView input, StringView& name, StringView& extension);
92
98 [[nodiscard]] static bool parse(StringView input, Path::ParsedView& pathView, Type type);
99
114 [[nodiscard]] static StringView dirname(StringView input, Type type, int repeat = 0);
115
127 [[nodiscard]] static StringView basename(StringView input, Type type);
128
139 [[nodiscard]] static StringView basename(StringView input, StringView suffix);
140
156 [[nodiscard]] static bool isAbsolute(StringView input, Type type);
157
158 struct Windows
159 {
160 static const char Separator = '\\';
161
162 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "\\"_a8; };
163 };
164
165 struct Posix
166 {
167 static const char Separator = '/';
168
169 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "/"_a8; }
170 };
171
173#if SC_PLATFORM_WINDOWS
174 static constexpr char Separator = '\\';
175#else
176 static constexpr char Separator = '/';
177#endif
178
180#if SC_PLATFORM_WINDOWS
181 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "\\"_a8; };
182#else
183 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "/"_a8; }
184#endif
185
198 template <int numComponents = 64>
199 [[nodiscard]] static bool normalize(String& output, StringView view, Type type)
200 {
201 StringView components[numComponents];
202 return normalize(output, view, type, components);
203 }
204
206 [[nodiscard]] static bool normalize(String& output, StringView view, Type type, Span<StringView> components);
207
222 [[nodiscard]] static bool relativeFromTo(StringView source, StringView destination, String& output, Type type,
223 Type outputType = AsNative);
224
230 [[nodiscard]] static bool append(String& output, Span<const StringView> paths, Type inputType);
231
235 [[nodiscard]] static bool endsWithSeparator(StringView path);
236
241
244 [[nodiscard]] static bool normalizeUNCAndTrimQuotes(String& outputPath, StringView fileLocation, Type type,
245 Span<StringView> components);
246
247 private:
248 [[nodiscard]] static bool appendTrailingSeparator(String& path, Type type);
249
250 [[nodiscard]] static StringView removeTrailingSeparator(StringView path);
251 struct Internal;
252};
253
255} // namespace SC
#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:29
StringView root
Ex. "C:\\"</tt> on windows - <tt>"/" on posix.
Definition Path.h:35
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:37
StringView name
Ex. "name" for "C:\\dir\\name.ext" on windows or "/dir/name.ext" on posix.
Definition Path.h:38
StringView ext
Ex. "ext" for "C:\\dir\\name.ext" on windows or "/dir/name.ext" on posix.
Definition Path.h:39
StringView directory
Ex. "C:\\dir" on windows - "/dir" on posix.
Definition Path.h:36
bool parsePosix(StringView input)
Parses all components on posix input path.
Definition Path.h:166
Definition Path.h:159
Parse and compose filesystem paths for windows and posix.
Definition Path.h:15
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 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:183
static bool normalize(String &output, StringView view, Type type)
Resolves all .. to output a normalized path String.
Definition Path.h:199
static bool endsWithSeparator(StringView path)
Check if the path ends with a Windows or Posix separator.
static bool normalizeUNCAndTrimQuotes(String &outputPath, StringView fileLocation, Type type, Span< StringView > components)
An extended Path::normalize handling a bug with incorrect FILE backslash escape on Windows when using...
static bool normalize(String &output, StringView view, Type type, Span< StringView > components)
Resolves all .. to output a normalized path String (allows custom span of components)
static StringView basename(StringView input, Type type)
Returns the base name of a path.
Type
Path type (windows or posix)
Definition Path.h:18
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:29
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:48
A non-modifiable owning string with associated encoding.
Definition String.h:29