TGUI 1.13
Loading...
Searching...
No Matches
Filesystem.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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#ifndef TGUI_FILESYSTEM_HPP
26#define TGUI_FILESYSTEM_HPP
27
28#include <TGUI/String.hpp>
29
30#include <cstdint>
31#include <ctime>
32#include <vector>
33
34#ifdef TGUI_USE_STD_FILESYSTEM
35 #include <filesystem>
36#endif
37
39
40namespace tgui
41{
48 class TGUI_API Filesystem
49 {
50 public:
54 class TGUI_API Path
55 {
56 public:
60 Path() = default;
61
67 explicit Path(const String& path);
68
69#ifdef TGUI_USE_STD_FILESYSTEM
77 template <typename PathType, std::enable_if_t<std::is_same_v<PathType, std::filesystem::path>, int> = 0>
78 explicit Path(const PathType& path) :
79 m_path(path)
80 {
81 }
82#endif
83
89 [[nodiscard]] bool isEmpty() const;
90
99 [[nodiscard]] bool isAbsolute() const;
100
109 [[nodiscard]] bool isRelative() const;
110
118 [[nodiscard]] String asString() const;
119
127 [[nodiscard]] Path getParentPath() const;
128
134 [[nodiscard]] String getFilename() const;
135
141 [[nodiscard]] Path getNormalForm() const;
142
151#ifdef TGUI_SYSTEM_WINDOWS
152 [[nodiscard]] std::wstring asNativeString() const;
153#else
154 [[nodiscard]] std::string asNativeString() const;
155#endif
156
157#ifdef TGUI_USE_STD_FILESYSTEM
161 operator const std::filesystem::path&() const
162 {
163 return m_path;
164 }
165#endif
166
174 [[nodiscard]] Path operator/(const Path& path) const;
175
183 [[nodiscard]] Path operator/(const String& path) const
184 {
185 return *this / Path(path);
186 }
187
195 Path& operator/=(const Path& path);
196
204 Path& operator/=(const String& path)
205 {
206 return *this /= Path(path);
207 }
208
216 [[nodiscard]] bool operator==(const Path& other) const;
217
225 [[nodiscard]] bool operator!=(const Path& other) const;
226
228
229 private:
230#ifdef TGUI_USE_STD_FILESYSTEM
231 std::filesystem::path m_path;
232#else
233 std::vector<String> m_parts;
234 String m_root; // Drive letter or empty
235 bool m_absolute = false;
236#endif
237 };
238
242 struct TGUI_API FileInfo
243 {
244 String filename;
245 Path path;
246 bool directory = false; // Is it a regular file or a folder?
247 std::uintmax_t fileSize = 0;
248 std::time_t modificationTime = 0;
249 };
250
258 [[nodiscard]] static bool directoryExists(const Path& path);
259
267 [[nodiscard]] static bool directoryExists(const String& path)
268 {
269 return directoryExists(Path{path});
270 }
271
279 [[nodiscard]] static bool fileExists(const Path& path);
280
288 [[nodiscard]] static bool fileExists(const String& path)
289 {
290 return fileExists(Path{path});
291 }
292
302 static bool createDirectory(const Path& path);
303
313 static bool createDirectory(const String& path)
314 {
315 return createDirectory(Path{path});
316 }
317
323 [[nodiscard]] static Path getHomeDirectory();
324
330 [[nodiscard]] static Path getCurrentWorkingDirectory();
331
341 [[nodiscard]] static Path getLocalDataDirectory();
342
350 [[nodiscard]] static std::vector<FileInfo> listFilesInDirectory(const Path& path);
351 };
352} // namespace tgui
353
354#endif // TGUI_FILESYSTEM_HPP
Object to represent paths on a filesystem.
Definition Filesystem.hpp:55
bool isEmpty() const
Check if this object is empty.
std::string asNativeString() const
Returns the path as a string, but with a string type and contents that depends on the OS.
bool isRelative() const
Checks whether the path is relative.
Path()=default
Default constructor that creates an empty path object.
String asString() const
Returns the path as a string.
Path getParentPath() const
Returns to path to the parent directory.
Path operator/(const Path &path) const
Returns a new path that consists of this object joined with another path.
bool operator!=(const Path &other) const
Checks whether the paths are not equal.
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:204
Path(const String &path)
Constructor that creates a Path object from the given path string.
String getFilename() const
Returns to filename component of the path (where the path consists of getParentPath() / getFilename()...
bool isAbsolute() const
Checks whether the path is absolute.
bool operator==(const Path &other) const
Checks whether the paths are equal.
Path operator/(const String &path) const
Returns a new path that consists of this object joined with another path.
Definition Filesystem.hpp:183
Path getNormalForm() const
Returns the lexically normal form of the path (path with '.' and '..' resolved).
Helper functionality for filesystem access.
Definition Filesystem.hpp:49
static bool directoryExists(const Path &path)
Checks if a directory exists.
static Path getLocalDataDirectory()
Returns the directory to store application data.
static bool fileExists(const String &path)
Checks if a file exists.
Definition Filesystem.hpp:288
static bool directoryExists(const String &path)
Checks if a directory exists.
Definition Filesystem.hpp:267
static std::vector< FileInfo > listFilesInDirectory(const Path &path)
Returns a list of all files and folders inside a given directory.
static bool fileExists(const Path &path)
Checks if a file exists.
static Path getCurrentWorkingDirectory()
Returns the current working directory.
static bool createDirectory(const Path &path)
Create a directory.
static Path getHomeDirectory()
Returns the home directory.
static bool createDirectory(const String &path)
Create a directory.
Definition Filesystem.hpp:313
Wrapper class to store strings.
Definition String.hpp:94
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
Information about a file or directory, used to return data from the listFilesInDirectory function.
Definition Filesystem.hpp:243