Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
StringPath.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/StringSpan.h"
5
6namespace SC
7{
8
9namespace detail
10{
12template <int N>
13struct StringNativeFixed
14{
15 size_t length = 0;
16 native_char_t buffer[N];
17
18 [[nodiscard]] operator StringSpan() const { return StringSpan({buffer, length}, true, StringEncoding::Native); }
19 [[nodiscard]] StringSpan view() const { return *this; }
20 [[nodiscard]] Span<native_char_t> writableSpan() const { return {buffer, N}; }
21
22 [[nodiscard]] bool assign(StringSpan str)
23 {
24 length = 0;
25 return append(str);
26 }
27
28 [[nodiscard]] bool append(StringSpan str)
29 {
30 StringSpan::NativeWritable string = {{buffer, N}, length};
31 if (not str.appendNullTerminatedTo(string))
32 return false;
33 length = string.length;
34 return true;
35 }
36};
37} // namespace detail
38
41{
43#if SC_PLATFORM_WINDOWS
44 static constexpr size_t MaxPath = 260; // Equal to 'MAX_PATH' on Windows
45#elif SC_PLATFORM_APPLE
46 static constexpr size_t MaxPath = 1024; // Equal to 'PATH_MAX' on macOS
47#else
48 static constexpr size_t MaxPath = 4096; // Equal to 'PATH_MAX' on Linux
49#endif
50 detail::StringNativeFixed<MaxPath> path;
51};
52} // namespace SC
char native_char_t
The native char for the platform (wchar_t (4 bytes) on Windows, char (1 byte) everywhere else )
Definition PrimitiveTypes.h:34
Pre-sized char array holding enough space to represent a file system path.
Definition StringPath.h:41
static constexpr size_t MaxPath
Maximum size of paths on current native platform.
Definition StringPath.h:48