TGUI  1.3-dev
Loading...
Searching...
No Matches
Filesystem.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25
26#ifndef TGUI_FILESYSTEM_HPP
27#define TGUI_FILESYSTEM_HPP
28
29#include <TGUI/String.hpp>
30
31#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #include <cstdint>
33 #include <vector>
34 #include <ctime>
35
36 #ifdef TGUI_USE_STD_FILESYSTEM
37 #include <filesystem>
38 #endif
39#endif
40
42
43TGUI_MODULE_EXPORT namespace tgui
44{
51 class TGUI_API Filesystem
52 {
53 public:
54
58 class TGUI_API Path
59 {
60 public:
61
65 Path() = default;
66
67
73 explicit Path(const String& path);
74
75#ifdef TGUI_USE_STD_FILESYSTEM
83 template <typename PathType, std::enable_if_t<std::is_same_v<PathType, std::filesystem::path>, int> = 0>
84 explicit Path(const PathType& path) :
85 m_path(path)
86 {
87 }
88#endif
89
95 TGUI_NODISCARD bool isEmpty() const;
96
97
106 TGUI_NODISCARD bool isAbsolute() const;
107
108
117 TGUI_NODISCARD bool isRelative() const;
118
119
127 TGUI_NODISCARD String asString() const;
128
129
137 TGUI_NODISCARD Path getParentPath() const;
138
139
145 TGUI_NODISCARD String getFilename() const;
146
147
153 TGUI_NODISCARD Path getNormalForm() const;
154
155
164#ifdef TGUI_SYSTEM_WINDOWS
165 TGUI_NODISCARD std::wstring asNativeString() const;
166#else
167 TGUI_NODISCARD std::string asNativeString() const;
168#endif
169
170#ifdef TGUI_USE_STD_FILESYSTEM
174 operator const std::filesystem::path&() const
175 {
176 return m_path;
177 }
178#endif
179
187 TGUI_NODISCARD Path operator/(const Path& path) const;
188
189
197 TGUI_NODISCARD Path operator/(const String& path) const
198 {
199 return *this / Path(path);
200 }
201
202
210 Path& operator/=(const Path& path);
211
212
220 Path& operator/=(const String& path)
221 {
222 return *this /= Path(path);
223 }
224
225
233 TGUI_NODISCARD bool operator==(const Path& other) const;
234
235
243 TGUI_NODISCARD bool operator!=(const Path& other) const;
244
245
247 private:
248
249#ifdef TGUI_USE_STD_FILESYSTEM
250 std::filesystem::path m_path;
251#else
252 std::vector<String> m_parts;
253 String m_root; // Drive letter or empty
254 bool m_absolute = false;
255#endif
256 };
257
258
262 struct TGUI_API FileInfo
263 {
264 String filename;
265 Path path;
266 bool directory = false; // Is it a regular file or a folder?
267 std::uintmax_t fileSize = 0;
268 std::time_t modificationTime = 0;
269 };
270
271
279 TGUI_NODISCARD static bool directoryExists(const Path& path);
280
281
289 TGUI_NODISCARD static bool directoryExists(const String& path)
290 {
291 return directoryExists(Path{path});
292 }
293
294
302 TGUI_NODISCARD static bool fileExists(const Path& path);
303
304
312 TGUI_NODISCARD static bool fileExists(const String& path)
313 {
314 return fileExists(Path{path});
315 }
316
317
327 static bool createDirectory(const Path& path);
328
329
339 static bool createDirectory(const String& path)
340 {
341 return createDirectory(Path{path});
342 }
343
344
350 TGUI_NODISCARD static Path getHomeDirectory();
351
352
358 TGUI_NODISCARD static Path getCurrentWorkingDirectory();
359
360
370 TGUI_NODISCARD static Path getLocalDataDirectory();
371
372
380 TGUI_NODISCARD static std::vector<FileInfo> listFilesInDirectory(const Path& path);
381 };
382
384}
385
387
388#endif // TGUI_FILESYSTEM_HPP
Object to represent paths on a filesystem.
Definition Filesystem.hpp:59
TGUI_NODISCARD std::string asNativeString() const
Returns the path as a string, but with a string type and contents that depends on the OS.
TGUI_NODISCARD bool operator!=(const Path &other) const
Checks whether the paths are not equal.
TGUI_NODISCARD bool isAbsolute() const
Checks whether the path is absolute.
Path()=default
Default constructor that creates an empty path object.
TGUI_NODISCARD bool operator==(const Path &other) const
Checks whether the paths are equal.
TGUI_NODISCARD bool isEmpty() const
Check if this object is empty.
Path & operator/=(const Path &path)
Joins this object with another path.
Path & operator/=(const String &path)
Joins this object with another path.
Definition Filesystem.hpp:220
Path(const String &path)
Constructor that creates a Path object from the given path string.
TGUI_NODISCARD Path operator/(const String &path) const
Returns a new path that consists of this object joined with another path.
Definition Filesystem.hpp:197
TGUI_NODISCARD Path getParentPath() const
Returns to path to the parent directory.
TGUI_NODISCARD String asString() const
Returns the path as a string.
TGUI_NODISCARD Path getNormalForm() const
Returns the lexically normal form of the path (path with '.' and '..' resolved)
TGUI_NODISCARD bool isRelative() const
Checks whether the path is relative.
TGUI_NODISCARD Path operator/(const Path &path) const
Returns a new path that consists of this object joined with another path.
TGUI_NODISCARD String getFilename() const
Returns to filename component of the path (where the path consists of getParentPath() / getFilename()...
Helper functionality for filesystem access.
Definition Filesystem.hpp:52
static TGUI_NODISCARD std::vector< FileInfo > listFilesInDirectory(const Path &path)
Returns a list of all files and folders inside a given directory.
static TGUI_NODISCARD Path getLocalDataDirectory()
Returns the directory to store application data.
static TGUI_NODISCARD bool fileExists(const String &path)
Checks if a file exists.
Definition Filesystem.hpp:312
static bool createDirectory(const Path &path)
Create a directory.
static TGUI_NODISCARD bool fileExists(const Path &path)
Checks if a file exists.
static TGUI_NODISCARD Path getHomeDirectory()
Returns the home directory.
static TGUI_NODISCARD bool directoryExists(const Path &path)
Checks if a directory exists.
static bool createDirectory(const String &path)
Create a directory.
Definition Filesystem.hpp:339
static TGUI_NODISCARD Path getCurrentWorkingDirectory()
Returns the current working directory.
static TGUI_NODISCARD bool directoryExists(const String &path)
Checks if a directory exists.
Definition Filesystem.hpp:289
Wrapper class to store strings.
Definition String.hpp:101
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Information about a file or directory, used to return data from the listFilesInDirectory function.
Definition Filesystem.hpp:263