TGUI  0.10-beta
Filesystem.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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#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
64
70 explicit Path(const String& path);
71
72#ifdef TGUI_USE_STD_FILESYSTEM
80 template <typename PathType, std::enable_if_t<std::is_same_v<PathType, std::filesystem::path>, int> = 0>
81 explicit Path(const PathType& path) :
82 m_path(path)
83 {
84 }
85#endif
86
92 bool isEmpty() const;
93
94
103 bool isAbsolute() const;
104
105
114 bool isRelative() const;
115
116
125
126
135
136
143
144
151
152
161#ifdef TGUI_SYSTEM_WINDOWS
162 std::wstring asNativeString() const;
163#else
164 std::string asNativeString() const;
165#endif
166
167#ifdef TGUI_USE_STD_FILESYSTEM
171 operator const std::filesystem::path&() const
172 {
173 return m_path;
174 }
175#endif
176
184 Path operator/(const Path& path) const;
185
186
194 Path operator/(const String& path) const
195 {
196 return *this / Path(path);
197 }
198
199
207 Path& operator/=(const Path& path);
208
209
217 Path& operator/=(const String& path)
218 {
219 return *this /= Path(path);
220 }
221
222
230 bool operator==(const Path& other) const;
231
232
240 bool operator!=(const Path& other) const;
241
242
244 private:
245
246#ifdef TGUI_USE_STD_FILESYSTEM
247 std::filesystem::path m_path;
248#else
249 std::vector<String> m_parts;
250 String m_root; // Drive letter or empty
251 bool m_absolute = false;
252#endif
253 };
254
255
259 struct TGUI_API FileInfo
260 {
261 String filename;
262 Path path;
263 bool directory = false; // Is it a regular file or a folder?
264 std::uintmax_t fileSize = 0;
265 std::time_t modificationTime = 0;
266 };
267
268
276 static bool directoryExists(const Path& path);
277
278
286 static bool directoryExists(const String& path)
287 {
288 return directoryExists(Path{path});
289 }
290
291
299 static bool fileExists(const Path& path);
300
301
309 static bool fileExists(const String& path)
310 {
311 return fileExists(Path{path});
312 }
313
314
324 static bool createDirectory(const Path& path);
325
326
336 static bool createDirectory(const String& path)
337 {
338 return createDirectory(Path{path});
339 }
340
341
348
349
356
357
368
369
377 static std::vector<FileInfo> listFilesInDirectory(const Path& path);
378 };
379
381}
382
384
385#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:217
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:194
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:309
static bool directoryExists(const String &path)
Checks if a directory exists.
Definition: Filesystem.hpp:286
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:336
Wrapper class to store strings.
Definition: String.hpp:79
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:260