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-2025 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
   91AUI_ENUM_FLAG(AFileAccess) {
   95    R = 0b100,
   96
  100    W = 0b010,
  101
  105    X = 0b001,
  106};
  107
  128class API_AUI_CORE APath final: public AString {
  129private:
  130    APath ensureSlashEnding() const;
  131    APath ensureNonSlashEnding() const;
  132
  133    void removeBackSlashes();
  134
  135
  136#if AUI_PLATFORM_WIN
  137    struct _stat64 stat() const;
  138#else
  139    struct stat stat() const;
  140#endif
  141
  142public:
  143    APath() = default;
  144    APath(AString&& other) noexcept: AString(std::move(other)) {
  145        removeBackSlashes();
  146    }
  147    APath(const AString& other) noexcept: AString(other) {
  148        removeBackSlashes();
  149    }
  150    APath(const char* utf8) noexcept: AString(utf8) {
  151        removeBackSlashes();
  152    }
  153
  154    // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
  155    APath(const char* utf8, std::size_t length) noexcept: AString(utf8, utf8 + length) {
  156        removeBackSlashes();
  157    }
  158    APath(const char16_t * str) noexcept: AString(str) {
  159        removeBackSlashes();
  160    }
  161
  162    APath(const char16_t * str, std::size_t length) noexcept: AString(str, str + length) {
  163        removeBackSlashes();
  164    }
  165
  178    [[nodiscard]]
  179    static const APath& processTemporaryDir();
  180
  190    [[nodiscard]]
  191    static APath nextRandomTemporary();
  192
  197    const APath& touch() const;
  198
  207    AString systemSlashDirection() const;
  208
  213    APath absolute() const;
  214
  222    ADeque<APath> listDir(AFileListFlags f = AFileListFlags::DEFAULT_FLAGS) const;
  223
  229    [[nodiscard]] APath parent() const;
  230
  244    [[nodiscard]] APath file(const AString& fileName) const;
  245
  252    [[nodiscard]] APath filename() const;
  253
  260    [[nodiscard]] APath filenameWithoutExtension() const;
  261
  268    [[nodiscard]] AString extension() const;
  269
  276    [[nodiscard]] APath withoutUppermostFolder() const;
  277
  286    bool exists() const;
  287
  288
  294    bool isRegularFileExists() const;
  295
  301    bool isDirectoryExists() const;
  302
  313    const APath& removeFile() const;
  314
  326    const APath& removeFileRecursive() const;
  327
  339    const APath& removeDirContentsRecursive() const;
  340
  345    const APath& makeDir() const;
  346
  351    const APath& makeDirs() const;
  352
  360    AString relativelyTo(const APath& dir) const;
  361
  365    APath extensionChanged(const AString& newExtension) const;
  366
  371    bool isAbsolute() const;
  376    bool isRelative() const {
  377        return !isAbsolute();
  378    }
  379
  380    time_t fileModifyTime() const;
  381    size_t fileSize() const;
  382
  393    const APath& chmod(int newMode) const;
  394
  395    enum DefaultPath {
  411        APPDATA,
  412
  431        TEMP,
  432
  442        HOME,
  443    };
  444
  450    static APath getDefaultPath(DefaultPath path);
  451
  457    static void copy(const APath& source, const APath& destination);
  458
  464    static void move(const APath& source, const APath& destination);
  465
  469    static APath workingDir();
  470
  471
  479    static AVector<APath> find(const AString& filename, const AVector<APath>& locations, APathFinder flags = APathFinder::NONE);
  480
  491    [[nodiscard]]
  492    APath operator/(const AString& filename) const {
  493        return file(filename);
  494    }
  495
  507    [[nodiscard]]
  508    bool isEffectivelyAccessible(AFileAccess flags) const noexcept;
  509};
  510
  524struct API_AUI_CORE APathOwner: public aui::noncopyable {
  525public:
  526    explicit APathOwner(APath mPath) noexcept: mPath(std::move(mPath)) {}
  527
  528    ~APathOwner() {
  529        try {
  530            mPath.removeFileRecursive();
  531        } catch(...) {}
  532    }
  533
  534    [[nodiscard]]
  535    operator const APath&() const noexcept {
  536        return mPath;
  537    }
  538
  539    [[nodiscard]]
  540    const APath& value() const noexcept {
  541        return mPath;
  542    }
  543
  544private:
  545    APath mPath;
  546};
  547
  548inline APath operator""_path(const char* str, std::size_t length) {
  549    return APath(str, length);
  550}
  551
  552template<>
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:128
AString systemSlashDirection() const
Transforms this path to string with platform's native slashes.
bool isAbsolute() const
Checks whether path absolute or not.
bool isEffectivelyAccessible(AFileAccess flags) const noexcept
Return true if the current process has specified access flags to path.
const APath & removeFileRecursive() const
Delete files recursively, including itself.
const APath & removeDirContentsRecursive() const
Delete directory contents (recursively).
static void copy(const APath &source, const APath &destination)
Copy regular file.
const APath & makeDirs() const
Create all nonexistent folders on the path.
const APath & makeDir() const
Create folder.
DefaultPath
Definition APath.h:395
@ HOME
User home directory.
Definition APath.h:442
@ TEMP
Folder for temporary data.
Definition APath.h:431
@ APPDATA
Folder for application data.
Definition APath.h:411
APath filenameWithoutExtension() const
File name without extension.
const APath & removeFile() const
Delete file. Relevant for empty folders and regular files.
static APath getDefaultPath(DefaultPath path)
Get system's default folder.
AString relativelyTo(const APath &dir) const
Returns same path but without dir
const APath & chmod(int newMode) const
Changes mode (permissions) on file.
bool exists() const
bool isRegularFileExists() const
static AVector< APath > find(const AString &filename, const AVector< APath > &locations, APathFinder flags=APathFinder::NONE)
APath absolute() const
Get the absolute (full) path to the file.
bool isDirectoryExists() const
APath operator/(const AString &filename) const
Path of the child element. Relevant only for folders.
Definition APath.h:492
const APath & touch() const
static void move(const APath &source, const APath &destination)
Move regular file.
APath parent() const
bool isRelative() const
Checks whether path absolute or not.
Definition APath.h:376
static APath workingDir()
AString extension() const
File extension.
APath withoutUppermostFolder() const
Remove the uppermost folder from this path.
static const APath & processTemporaryDir()
Generates a unique, process-agnostic temporary directory in the system's temp directory.
static APath nextRandomTemporary()
Creates a path to non-existent random file in system temp directory.
APath file(const AString &fileName) const
Path of the child element. Relevant only for folders.
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...
APath extensionChanged(const AString &newExtension) const
Returns same path but with extension changed.
APath filename() const
File name.
A std::vector with AUI extensions.
Definition AVector.h:39
@ NONE
Definition AFloat.h:23
#define AUI_ENUM_FLAG(name)
Make a bitfield-style enum class.
Definition AEnumerate.h:227
Definition serializable.h:26
Forbids copy of your class.
Definition values.h:45