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#include "StringsExport.h"
7
8namespace SC
9{
10struct String;
11
14
16struct SC_STRINGS_EXPORT Path
17{
19 enum Type
20 {
21 AsPosix,
22 AsWindows,
23#if SC_PLATFORM_WINDOWS
24 AsNative = AsWindows
25#else
26 AsNative = AsPosix
27#endif
28 };
31 {
32 bool endsWithSeparator = false;
33
34 // TODO: ParsedView::directory and base are not defined consistently
35
36 Type type = AsPosix;
42
57 [[nodiscard]] bool parseWindows(StringView input);
58
73 [[nodiscard]] bool parsePosix(StringView input);
74 };
75
82 template <typename T>
83 [[nodiscard]] static bool join(T& output, Span<const StringView> inputs,
84 StringView separator = SeparatorStringView(), bool skipEmpty = false)
85 {
86 return join(GrowableBuffer<T>{output}, GrowableBuffer<T>::getEncodingFor(output), inputs, separator, skipEmpty);
87 }
88
89 [[nodiscard]] static bool join(IGrowableBuffer&& output, StringEncoding encoding, Span<const StringView> inputs,
90 StringView separator = SeparatorStringView(), bool skipEmpty = false);
99 [[nodiscard]] static bool parseNameExtension(const StringView input, StringView& name, StringView& extension);
100
106 [[nodiscard]] static bool parse(StringView input, Path::ParsedView& pathView, Type type);
107
122 [[nodiscard]] static StringView dirname(StringView input, Type type, int repeat = 0);
123
135 [[nodiscard]] static StringView basename(StringView input, Type type);
136
147 [[nodiscard]] static StringView basename(StringView input, StringView suffix);
148
164 [[nodiscard]] static bool isAbsolute(StringView input, Type type);
165
166 struct Windows
167 {
168 static const char Separator = '\\';
169
170 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "\\"_a8; };
171 };
172
173 struct Posix
174 {
175 static const char Separator = '/';
176
177 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "/"_a8; }
178 };
179
181#if SC_PLATFORM_WINDOWS
182 static constexpr char Separator = '\\';
183#else
184 static constexpr char Separator = '/';
185#endif
186
188#if SC_PLATFORM_WINDOWS
189 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "\\"_a8; };
190#else
191 [[nodiscard]] static constexpr StringView SeparatorStringView() { return "/"_a8; }
192#endif
193
206 template <typename T, int numComponents = 64>
207 [[nodiscard]] static bool normalize(T& output, StringView view, Type type)
208 {
209 StringView components[numComponents];
210 return normalize(GrowableBuffer<T>{output}, output.getEncoding(), view, type, components);
211 }
212
213 [[nodiscard]] static bool normalize(IGrowableBuffer&& output, StringEncoding encoding, StringView view, Type type,
214 Span<StringView> components);
215
230 template <typename T>
231 [[nodiscard]] static bool relativeFromTo(T& output, StringView source, StringView destination, Type type,
232 Type outputType = AsNative)
233 {
234 return relativeFromTo(GrowableBuffer<T>{output}, output.getEncoding(), source, destination, type, outputType);
235 }
236
237 [[nodiscard]] static bool relativeFromTo(IGrowableBuffer&& output, StringEncoding encoding, StringView source,
238 StringView destination, Type type, Type outputType = AsNative);
239
245 template <typename T>
246 [[nodiscard]] static bool append(T& output, Span<const StringView> paths, Type inputType)
247 {
248 return append(GrowableBuffer<T>{output}, GrowableBuffer<T>::getEncodingFor(output), paths, inputType);
249 }
250
251 [[nodiscard]] static bool append(IGrowableBuffer&& output, StringEncoding encoding, Span<const StringView> paths,
252 Type inputType);
253
257 [[nodiscard]] static bool endsWithSeparator(StringView path);
258
263
264 private:
265 [[nodiscard]] static StringView removeTrailingSeparator(StringView path);
266 struct Internal;
267};
268
270} // namespace SC
Holds the various parsed components of a path.
Definition Path.h:31
StringView root
Ex. "C:\\"</tt> on windows - <tt>"/" on posix.
Definition Path.h:37
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:39
StringView name
Ex. "name" for "C:\\dir\\name.ext" on windows or "/dir/name.ext" on posix.
Definition Path.h:40
StringView ext
Ex. "ext" for "C:\\dir\\name.ext" on windows or "/dir/name.ext" on posix.
Definition Path.h:41
StringView directory
Ex. "C:\\dir" on windows - "/dir" on posix.
Definition Path.h:38
bool parsePosix(StringView input)
Parses all components on posix input path.
Definition Path.h:174
Definition Path.h:167
Parse and compose filesystem paths for windows and posix.
Definition Path.h:17
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:83
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:191
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:231
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:246
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:207
Type
Path type (windows or posix)
Definition Path.h:20
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:47