TGUI 1.11
Loading...
Searching...
No Matches
Filesystem.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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 <vector>
32#include <ctime>
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:
51
55 class TGUI_API Path
56 {
57 public:
58
62 Path() = default;
63
69 explicit Path(const String& path);
70
71#ifdef TGUI_USE_STD_FILESYSTEM
79 template <typename PathType, std::enable_if_t<std::is_same_v<PathType, std::filesystem::path>, int> = 0>
80 explicit Path(const PathType& path) :
81 m_path(path)
82 {
83 }
84#endif
85
91 TGUI_NODISCARD bool isEmpty() const;
92
101 TGUI_NODISCARD bool isAbsolute() const;
102
111 TGUI_NODISCARD bool isRelative() const;
112
120 TGUI_NODISCARD String asString() const;
121
129 TGUI_NODISCARD Path getParentPath() const;
130
136 TGUI_NODISCARD String getFilename() const;
137
143 TGUI_NODISCARD Path getNormalForm() const;
144
153#ifdef TGUI_SYSTEM_WINDOWS
154 TGUI_NODISCARD std::wstring asNativeString() const;
155#else
156 TGUI_NODISCARD std::string asNativeString() const;
157#endif
158
159#ifdef TGUI_USE_STD_FILESYSTEM
163 operator const std::filesystem::path&() const
164 {
165 return m_path;
166 }
167#endif
168
176 TGUI_NODISCARD Path operator/(const Path& path) const;
177
185 TGUI_NODISCARD Path operator/(const String& path) const
186 {
187 return *this / Path(path);
188 }
189
197 Path& operator/=(const Path& path);
198
206 Path& operator/=(const String& path)
207 {
208 return *this /= Path(path);
209 }
210
218 TGUI_NODISCARD bool operator==(const Path& other) const;
219
227 TGUI_NODISCARD bool operator!=(const Path& other) const;
228
230 private:
231
232#ifdef TGUI_USE_STD_FILESYSTEM
233 std::filesystem::path m_path;
234#else
235 std::vector<String> m_parts;
236 String m_root; // Drive letter or empty
237 bool m_absolute = false;
238#endif
239 };
240
244 struct TGUI_API FileInfo
245 {
246 String filename;
247 Path path;
248 bool directory = false; // Is it a regular file or a folder?
249 std::uintmax_t fileSize = 0;
250 std::time_t modificationTime = 0;
251 };
252
260 TGUI_NODISCARD static bool directoryExists(const Path& path);
261
269 TGUI_NODISCARD static bool directoryExists(const String& path)
270 {
271 return directoryExists(Path{path});
272 }
273
281 TGUI_NODISCARD static bool fileExists(const Path& path);
282
290 TGUI_NODISCARD static bool fileExists(const String& path)
291 {
292 return fileExists(Path{path});
293 }
294
304 static bool createDirectory(const Path& path);
305
315 static bool createDirectory(const String& path)
316 {
317 return createDirectory(Path{path});
318 }
319
325 TGUI_NODISCARD static Path getHomeDirectory();
326
332 TGUI_NODISCARD static Path getCurrentWorkingDirectory();
333
343 TGUI_NODISCARD static Path getLocalDataDirectory();
344
352 TGUI_NODISCARD static std::vector<FileInfo> listFilesInDirectory(const Path& path);
353 };
354
356}
357
359
360#endif // TGUI_FILESYSTEM_HPP
Object to represent paths on a filesystem.
Definition Filesystem.hpp:56
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:206
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:185
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:290
static bool directoryExists(const String &path)
Checks if a directory exists.
Definition Filesystem.hpp:269
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:315
Wrapper class to store strings.
Definition String.hpp:94
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
Information about a file or directory, used to return data from the listFilesInDirectory function.
Definition Filesystem.hpp:245