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 "../Foundation/Internal/IGrowableBuffer.h"
5#include "../Strings/StringView.h"
6
7namespace SC
8{
9struct String;
10
13
15struct SC_COMPILER_EXPORT Path
16{
18 enum Type
19 {
20 AsPosix,
21 AsWindows,
22#if SC_PLATFORM_WINDOWS
23 AsNative = AsWindows
24#else
25 AsNative = AsPosix
26#endif
27 };
30 {
31 bool endsWithSeparator = false;
32
33 // TODO: ParsedView::directory and base are not defined consistently
34
35 Type type = AsPosix;
41
56 [[nodiscard]] bool parseWindows(StringView input);
57
72 [[nodiscard]] bool parsePosix(StringView input);
73 };
74
81 template <typename T>
82 [[nodiscard]] static bool join(T& output, Span<const StringView> inputs,
83 StringView separator = SeparatorStringView(), bool skipEmpty = false)
84 {
85 return join(GrowableBuffer<T>{output}, output.getEncoding(), inputs, separator, skipEmpty);
86 }
87
88 [[nodiscard]] static bool join(IGrowableBuffer&& output, StringEncoding encoding, Span<const StringView> inputs,
89 StringView separator = SeparatorStringView(), bool skipEmpty = false);
98 [[nodiscard]] static bool parseNameExtension(const StringView input, StringView& name, StringView& extension);
99
105 [[nodiscard]] static bool parse(StringView input, Path::ParsedView& pathView, Type type);
106
121 [[nodiscard]] static StringView dirname(StringView input, Type type, int repeat = 0);
122
134 [[nodiscard]] static StringView basename(StringView input, Type type);
135
146 [[nodiscard]] static StringView basename(StringView input, StringView suffix);
147
163 [[nodiscard]] static bool isAbsolute(StringView input, Type type);
164
165 struct Windows
166 {
167 static const char Separator = '\\';
168
169 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "\\"_a8; };
170 };
171
172 struct Posix
173 {
174 static const char Separator = '/';
175
176 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "/"_a8; }
177 };
178
180#if SC_PLATFORM_WINDOWS
181 static constexpr char Separator = '\\';
182#else
183 static constexpr char Separator = '/';
184#endif
185
187#if SC_PLATFORM_WINDOWS
188 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "\\"_a8; };
189#else
190 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "/"_a8; }
191#endif
192
205 template <typename T, int numComponents = 64>
206 [[nodiscard]] static bool normalize(T& output, StringView view, Type type)
207 {
208 StringView components[numComponents];
209 return normalize(GrowableBuffer<T>{output}, output.getEncoding(), view, type, components);
210 }
211
212 [[nodiscard]] static bool normalize(IGrowableBuffer&& output, StringEncoding encoding, StringView view, Type type,
213 Span<StringView> components);
214
229 template <typename T>
230 [[nodiscard]] static bool relativeFromTo(T& output, StringView source, StringView destination, Type type,
231 Type outputType = AsNative)
232 {
233 return relativeFromTo(GrowableBuffer<T>{output}, output.getEncoding(), source, destination, type, outputType);
234 }
235
236 [[nodiscard]] static bool relativeFromTo(IGrowableBuffer&& output, StringEncoding encoding, StringView source,
237 StringView destination, Type type, Type outputType = AsNative);
238
244 template <typename T>
245 [[nodiscard]] static bool append(T& output, Span<const StringView> paths, Type inputType)
246 {
247 return append(GrowableBuffer<T>{output}, output.getEncoding(), paths, inputType);
248 }
249
250 [[nodiscard]] static bool append(IGrowableBuffer&& output, StringEncoding encoding, Span<const StringView> paths,
251 Type inputType);
252
256 [[nodiscard]] static bool endsWithSeparator(StringView path);
257
262
265 template <typename T>
266 [[nodiscard]] static bool normalizeUNCAndTrimQuotes(T& outputPath, StringView fileLocation, Type type,
267 Span<StringView> components)
268 {
269 return normalizeUNCAndTrimQuotes(GrowableBuffer<T>{outputPath}, outputPath.getEncoding(), fileLocation, type,
270 components);
271 }
272 [[nodiscard]] static bool normalizeUNCAndTrimQuotes(IGrowableBuffer&& outputPath, StringEncoding encoding,
273 StringView fileLocation, Type type,
274 Span<StringView> components);
275
276 private:
277 [[nodiscard]] static StringView removeTrailingSeparator(StringView path);
278 struct Internal;
279};
280
282} // namespace SC
Holds the various parsed components of a path.
Definition Path.h:30
StringView root
Ex. "C:\\"</tt> on windows - <tt>"/" on posix.
Definition Path.h:36
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:38
StringView name
Ex. "name" for "C:\\dir\\name.ext" on windows or "/dir/name.ext" on posix.
Definition Path.h:39
StringView ext
Ex. "ext" for "C:\\dir\\name.ext" on windows or "/dir/name.ext" on posix.
Definition Path.h:40
StringView directory
Ex. "C:\\dir" on windows - "/dir" on posix.
Definition Path.h:37
bool parsePosix(StringView input)
Parses all components on posix input path.
Definition Path.h:173
Definition Path.h:166
Parse and compose filesystem paths for windows and posix.
Definition Path.h:16
static bool join(T &output, Span< const StringView > inputs, StringView separator=SeparatorStringView(), bool skipEmpty=false)
Joins multiple StringView with a Separator into an output String.
Definition Path.h:82
static StringView basename(StringView input, StringView suffix)
Returns the base name of a path.
static bool parseNameExtension(const StringView input, StringView &name, StringView &extension)
Splits a StringView of type "name.ext" into "name" and "ext".
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:190
static bool relativeFromTo(T &output, StringView source, StringView destination, Type type, Type outputType=AsNative)
Get relative path that appended to source resolves to destination.
Definition Path.h:230
static bool endsWithSeparator(StringView path)
Check if the path ends with a Windows or Posix separator.
static bool append(T &output, Span< const StringView > paths, Type inputType)
Append to an existing path a series of StringView with a separator.
Definition Path.h:245
static bool normalizeUNCAndTrimQuotes(T &outputPath, StringView fileLocation, Type type, Span< StringView > components)
An extended Path::normalize handling a bug with incorrect FILE backslash escape on Windows when using...
Definition Path.h:266
static StringView basename(StringView input, Type type)
Returns the base name of a path.
static bool normalize(T &output, StringView view, Type type)
Resolves all .. to output a normalized path String.
Definition Path.h:206
Type
Path type (windows or posix)
Definition Path.h:19
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:46