AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
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
82 RECURSIVE = 1 << 3,
83
84 DEFAULT_FLAGS = AFileListFlags::DIRS | AFileListFlags::REGULAR_FILES
85};
86
107class API_AUI_CORE APath final: public AString {
108private:
109 APath ensureSlashEnding() const;
110 APath ensureNonSlashEnding() const;
111
112 void removeBackSlashes();
113
114
115#if AUI_PLATFORM_WIN
116 struct _stat64 stat() const;
117#else
118 struct stat stat() const;
119#endif
120
121public:
122 APath() = default;
123 APath(AString&& other) noexcept: AString(std::move(other)) {
124 removeBackSlashes();
125 }
126 APath(const AString& other) noexcept: AString(other) {
127 removeBackSlashes();
128 }
129 APath(const char* utf8) noexcept: AString(utf8) {
130 removeBackSlashes();
131 }
132
133 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
134 APath(const char* utf8, std::size_t length) noexcept: AString(utf8, utf8 + length) {
135 removeBackSlashes();
136 }
137 APath(const char16_t * str) noexcept: AString(str) {
138 removeBackSlashes();
139 }
140
141 APath(const char16_t * str, std::size_t length) noexcept: AString(str, str + length) {
142 removeBackSlashes();
143 }
144
149 const APath& touch() const;
150
154 AString systemSlashDirection() const;
155
160 APath absolute() const;
161
168 ADeque<APath> listDir(AFileListFlags f = AFileListFlags::DEFAULT_FLAGS) const;
169
175 [[nodiscard]] APath parent() const;
176
190 [[nodiscard]] APath file(const AString& fileName) const;
191
198 [[nodiscard]] APath filename() const;
199
206 [[nodiscard]] APath filenameWithoutExtension() const;
207
214 [[nodiscard]] APath withoutUppermostFolder() const;
215
224 bool exists() const;
225
226
232 bool isRegularFileExists() const;
233
239 bool isDirectoryExists() const;
240
245 const APath& removeFile() const;
246
251 const APath& removeFileRecursive() const;
252
257 const APath& makeDir() const;
258
263 const APath& makeDirs() const;
264
272 AString relativelyTo(const APath& dir) const;
273
277 APath extensionChanged(const AString& newExtension) const;
278
283 bool isAbsolute() const;
288 bool isRelative() const {
289 return !isAbsolute();
290 }
291
292 time_t fileModifyTime() const;
293 size_t fileSize() const;
294
295 const APath& chmod(int newMode) const;
296
306
315
323 };
324
330 static APath getDefaultPath(DefaultPath path);
331
337 static void copy(const APath& source, const APath& destination);
338
344 static void move(const APath& source, const APath& destination);
345
349 static APath workingDir();
350
351
359 static AVector<APath> find(const AString& filename, const AVector<APath>& locations, APathFinder flags = APathFinder::NONE);
360
370 [[nodiscard]]
371 APath operator/(const AString& filename) const {
372 return file(filename);
373 }
374
375};
376
377
378inline APath operator""_path(const char* str, std::size_t length) {
379 return APath(str, length);
380}
381
382template<>
383struct 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:107
AString systemSlashDirection() const
Definition APath.cpp:434
bool isAbsolute() const
Checks whether path absolute or not.
Definition APath.cpp:247
const APath & removeFileRecursive() const
Delete files recursively. Relevant for folders.
Definition APath.cpp:124
const APath & makeDirs() const
Create all nonexistent folders on the path.
Definition APath.cpp:234
const APath & makeDir() const
Create folder.
Definition APath.cpp:222
DefaultPath
Definition APath.h:297
@ HOME
User home directory.
Definition APath.h:322
@ TEMP
Folder for temporary data.
Definition APath.h:314
@ APPDATA
Folder for application data.
Definition APath.h:305
APath filenameWithoutExtension() const
File name without extension.
Definition APath.cpp:54
const APath & removeFile() const
Delete file. Relevant for empty folders and regular files.
Definition APath.cpp:103
AString relativelyTo(const APath &dir) const
Returns same path but without folder
Definition APath.cpp:81
bool exists() const
Definition APath.cpp:92
bool isRegularFileExists() const
Definition APath.cpp:95
APath absolute() const
Get the absolute (full) path to the file.
Definition APath.cpp:195
bool isDirectoryExists() const
Definition APath.cpp:99
APath operator/(const AString &filename) const
Path of the child element. Relevant only for folders.
Definition APath.h:371
const APath & touch() const
Definition APath.cpp:444
APath parent() const
Definition APath.cpp:38
bool isRelative() const
Checks whether path absolute or not.
Definition APath.h:288
APath withoutUppermostFolder() const
Remove the uppermost folder from this path.
Definition APath.cpp:312
APath file(const AString &fileName) const
Path of the child element. Relevant only for folders.
Definition APath.cpp:63
ADeque< APath > listDir(AFileListFlags f=AFileListFlags::DEFAULT_FLAGS) const
Get list of (by default) direct children of this folder. This function outputs paths including the pa...
Definition APath.cpp:135
APath extensionChanged(const AString &newExtension) const
Returns same path but with extension changed.
Definition APath.cpp:461
APath filename() const
File name.
Definition APath.cpp:46
Represents a Unicode character string.
Definition AString.h:37
A std::vector with AUI extensions.
Definition AVector.h:39
@ NONE
Definition AFloat.h:23
Definition serializable.h:26