AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
APath.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include <iterator>
15#include "AUI/Reflect/AEnumerate.h"
16#include <AUI/Common/AString.h>
17#include <AUI/Common/ADeque.h>
18#include <AUI/Common/AVector.h>
19#include <AUI/Traits/serializable.h>
20
25AUI_ENUM_FLAG(APathFinder) {
26 NONE,
27
31 USE_SYSTEM_PATHS = 1 << 0,
32
36 RECURSIVE = 1 << 1,
37
41 SINGLE = 1 << 2
42};
43
48AUI_ENUM_FLAG(AFileListFlags) {
49 NONE = 0,
50
55 DONT_IGNORE_DOTS = 1 << 0,
56
60 DIRS = 1 << 1,
61
65 REGULAR_FILES = 1 << 2,
66
81 RECURSIVE = 1 << 3,
82
83 DEFAULT_FLAGS = AFileListFlags::DIRS | AFileListFlags::REGULAR_FILES
84};
85
106class API_AUI_CORE APath final: public AString {
107private:
108 APath ensureSlashEnding() const;
109 APath ensureNonSlashEnding() const;
110
111 void removeBackSlashes();
112
113
114#if AUI_PLATFORM_WIN
115 struct _stat64 stat() const;
116#else
117 struct stat stat() const;
118#endif
119
120public:
121 APath() = default;
122 APath(AString&& other) noexcept: AString(std::move(other)) {
123 removeBackSlashes();
124 }
125 APath(const AString& other) noexcept: AString(other) {
126 removeBackSlashes();
127 }
128 APath(const char* utf8) noexcept: AString(utf8) {
129 removeBackSlashes();
130 }
131
132 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
133 APath(const char* utf8, std::size_t length) noexcept: AString(utf8, utf8 + length) {
134 removeBackSlashes();
135 }
136 APath(const char16_t * str) noexcept: AString(str) {
137 removeBackSlashes();
138 }
139
140 APath(const char16_t * str, std::size_t length) noexcept: AString(str, str + length) {
141 removeBackSlashes();
142 }
143
148 const APath& touch() const;
149
153 AString systemSlashDirection() const;
154
159 APath absolute() const;
160
167 ADeque<APath> listDir(AFileListFlags f = AFileListFlags::DEFAULT_FLAGS) const;
168
173 [[nodiscard]] APath parent() const;
174
181 [[nodiscard]] APath file(const AString& fileName) const;
182
188 [[nodiscard]] APath filename() const;
189
195 [[nodiscard]] APath filenameWithoutExtension() const;
196
202 [[nodiscard]] APath withoutUppermostFolder() const;
203
212 bool exists() const;
213
214
220 bool isRegularFileExists() const;
221
227 bool isDirectoryExists() const;
228
233 const APath& removeFile() const;
234
239 const APath& removeFileRecursive() const;
240
245 const APath& makeDir() const;
246
251 const APath& makeDirs() const;
252
259 AString relativelyTo(const APath& dir) const;
260
264 APath extensionChanged(const AString& newExtension) const;
265
270 bool isAbsolute() const;
275 bool isRelative() const {
276 return !isAbsolute();
277 }
278
279 time_t fileModifyTime() const;
280 size_t fileSize() const;
281
282 const APath& chmod(int newMode) const;
283
292
300
307 };
308
314 static APath getDefaultPath(DefaultPath path);
315
321 static void copy(const APath& source, const APath& destination);
322
328 static void move(const APath& source, const APath& destination);
329
333 static APath workingDir();
334
335
342 static AVector<APath> find(const AString& filename, const AVector<APath>& locations, APathFinder flags = APathFinder::NONE);
343
353 [[nodiscard]]
354 APath operator/(const AString& filename) const {
355 return file(filename);
356 }
357
358};
359
360
361inline APath operator""_path(const char* str, std::size_t length) {
362 return APath(str, length);
363}
364
365template<>
366struct ASerializable<APath>: ASerializable<AString> {};
A std::deque with AUI extensions.
Definition: ADeque.h:27
An add-on to AString with functions for working with the path.
Definition: APath.h:106
DefaultPath
Definition: APath.h:284
@ HOME
User home directory. Windows: User's home folder (C:\Users\USERNAME) Linux: /home/$USER.
Definition: APath.h:306
@ TEMP
Folder for temporary data. Windows: User's temp folder (temp%) Linux: /tmp Android: <internal_storage...
Definition: APath.h:299
@ APPDATA
Folder for application data. Windows: C:/Users/user%/.appdata/Roaming/ Linux: homedir%/....
Definition: APath.h:291
APath operator/(const AString &filename) const
Path of the child element. Relevant only for folders.
Definition: APath.h:354
bool isRelative() const
Checks whether path absolute or not.
Definition: APath.h:275
Represents a Unicode character string.
Definition: AString.h:37
A std::vector with AUI extensions.
Definition: AVector.h:38
@ NONE
Image is kept in it's original size.
AUI_ENUM_FLAG(APathFinder)
Flag enum for APath::find.
Definition: APath.h:25
Definition: serializable.h:26