TGUI  0.9.1
Filesystem.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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 
40 namespace 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 
102  String asString() const;
103 
104 
113 
114 
121 
122 
129 
130 
139 #ifdef TGUI_SYSTEM_WINDOWS
140  std::wstring asNativeString() const;
141 #else
142  std::string asNativeString() const;
143 #endif
144 
145 #ifdef TGUI_USE_STD_FILESYSTEM
149  operator const std::filesystem::path&() const
150  {
151  return m_path;
152  }
153 #endif
154 
162  Path operator/(const Path& path) const;
163 
164 
172  Path operator/(const String& path) const
173  {
174  return *this / Path(path);
175  }
176 
177 
185  Path& operator/=(const Path& path);
186 
187 
195  Path& operator/=(const String& path)
196  {
197  return *this /= Path(path);
198  }
199 
200 
208  bool operator==(const Path& other) const;
209 
210 
218  bool operator!=(const Path& other) const;
219 
220 
222  private:
223 
224 #ifdef TGUI_USE_STD_FILESYSTEM
225  std::filesystem::path m_path;
226 #else
227  std::vector<String> m_parts;
228  String m_root; // Drive letter or empty
229  bool m_absolute = false;
230 #endif
231  };
232 
233 
237  struct TGUI_API FileInfo
238  {
239  String filename;
240  Path path;
241  bool directory = false; // Is it a regular file or a folder?
242  std::uintmax_t fileSize = 0;
243  std::time_t modificationTime = 0;
244  };
245 
246 
254  static bool directoryExists(const Path& path);
255 
256 
264  static bool directoryExists(const String& path)
265  {
266  return directoryExists(Path{path});
267  }
268 
269 
277  static bool fileExists(const Path& path);
278 
279 
287  static bool fileExists(const String& path)
288  {
289  return fileExists(Path{path});
290  }
291 
292 
302  static bool createDirectory(const Path& path);
303 
304 
314  static bool createDirectory(const String& path)
315  {
316  return createDirectory(Path{path});
317  }
318 
319 
326 
327 
334 
335 
346 
347 
355  static std::vector<FileInfo> listFilesInDirectory(const Path& path);
356  };
357 
359 }
360 
362 
363 #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.
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)
Joins this object with another path.
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(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()...
Path & operator/=(const String &path)
Joins this object with another path.
Definition: Filesystem.hpp:195
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:172
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 std::vector< FileInfo > listFilesInDirectory(const Path &path)
Returns a list of all files and folders inside a given directory.
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:287
static bool directoryExists(const String &path)
Checks if a directory exists.
Definition: Filesystem.hpp:264
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:314
Wrapper class to store strings.
Definition: String.hpp:74
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:238