SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_filesystem.h
1#ifndef SDL3PP_FILESYSTEM_H_
2#define SDL3PP_FILESYSTEM_H_
3
4#include <SDL3/SDL_filesystem.h>
5#include "SDL3pp_stdinc.h"
6
7namespace SDL {
8
34using PathInfoRaw = SDL_PathInfo;
35
36// Forward decl
37struct PathInfo;
38
44{
47
49 Path& operator+=(std::string_view other)
50 {
52 return *this;
53 }
54
56 Path& operator+=(char ch)
57 {
59 return *this;
60 }
61
63 Path operator+(std::string_view other) const
64 {
65 Path result(*this);
66 result += other;
67 return result;
68 }
69
71 Path operator+(char ch) const
72 {
73 Path result(*this);
74 result += ch;
75 return result;
76 }
77
79 Path& operator/=(std::string_view other)
80 {
81 if (!empty() && back() != '/' && back() != '\\') this->operator+=('/');
82 return this->operator+=(other);
83 }
84
86 Path operator/(std::string_view other) const
87 {
88 Path result(*this);
89 result /= other;
90 return result;
91 }
92};
93
138inline const char* GetBasePath() { return SDL_GetBasePath(); }
139
200{
201 return Path{SDL_GetPrefPath(org, app)};
202}
203
233using Folder = SDL_Folder;
234
241constexpr Folder FOLDER_HOME = SDL_FOLDER_HOME;
242
249constexpr Folder FOLDER_DESKTOP = SDL_FOLDER_DESKTOP;
250
255constexpr Folder FOLDER_DOCUMENTS = SDL_FOLDER_DOCUMENTS;
256
258constexpr Folder FOLDER_DOWNLOADS = SDL_FOLDER_DOWNLOADS;
259
261constexpr Folder FOLDER_MUSIC = SDL_FOLDER_MUSIC;
262
264constexpr Folder FOLDER_PICTURES = SDL_FOLDER_PICTURES;
265
267constexpr Folder FOLDER_PUBLICSHARE = SDL_FOLDER_PUBLICSHARE;
268
270 SDL_FOLDER_SAVEDGAMES;
271
273 SDL_FOLDER_SCREENSHOTS;
274
280constexpr Folder FOLDER_TEMPLATES = SDL_FOLDER_TEMPLATES;
281
285constexpr Folder FOLDER_VIDEOS = SDL_FOLDER_VIDEOS;
286
288constexpr Folder FOLDER_COUNT = SDL_FOLDER_COUNT;
289
314inline const char* GetUserFolder(Folder folder)
315{
316 return SDL_GetUserFolder(folder);
317}
318
329using PathType = SDL_PathType;
330
331constexpr PathType PATHTYPE_NONE = SDL_PATHTYPE_NONE;
332
333constexpr PathType PATHTYPE_FILE = SDL_PATHTYPE_FILE;
334
335constexpr PathType PATHTYPE_DIRECTORY = SDL_PATHTYPE_DIRECTORY;
336
341constexpr PathType PATHTYPE_OTHER = SDL_PATHTYPE_OTHER;
342
352{
358 constexpr PathInfo(const PathInfoRaw& pathInfo = {}) noexcept
359 : PathInfoRaw(pathInfo)
360 {
361 }
362
368 constexpr bool operator==(std::nullptr_t _) const noexcept
369 {
370 return !bool(*this);
371 }
372
378 constexpr explicit operator bool() const noexcept
379 {
380 return type != PATHTYPE_NONE;
381 }
382};
383
393
395 SDL_GLOB_CASEINSENSITIVE;
396
413{
414 CheckError(SDL_CreateDirectory(path));
415}
416
424using EnumerationResult = SDL_EnumerationResult;
425
427 SDL_ENUM_CONTINUE;
428
430constexpr EnumerationResult ENUM_SUCCESS = SDL_ENUM_SUCCESS;
431
433constexpr EnumerationResult ENUM_FAILURE = SDL_ENUM_FAILURE;
434
460 SDLCALL*)(void* userdata, const char* dirname, const char* fname);
461
488 std::function<EnumerationResult(const char* dirname, const char* fname)>;
489
512 void* userdata)
513{
514 CheckError(SDL_EnumerateDirectory(path, callback, userdata));
515}
516
537{
538 return EnumerateDirectory(
539 std::move(path),
540 [](void* userdata, const char* dirname, const char* fname) {
541 auto& cb = *static_cast<EnumerateDirectoryCB*>(userdata);
542 return cb(dirname, fname);
543 },
544 &callback);
545}
546
557inline std::vector<Path> EnumerateDirectory(StringParam path)
558{
559 std::vector<Path> r;
560 EnumerateDirectory(std::move(path), [&](const char*, const char* fname) {
561 r.emplace_back(fname);
562 return ENUM_CONTINUE;
563 });
564 return r;
565}
566
580inline void RemovePath(StringParam path) { CheckError(SDL_RemovePath(path)); }
581
603inline void RenamePath(StringParam oldpath, StringParam newpath)
604{
605 CheckError(SDL_RenamePath(oldpath, newpath));
606}
607
649inline void CopyFile(StringParam oldpath, StringParam newpath)
650{
651 CheckError(SDL_CopyFile(oldpath, newpath));
652}
653
666{
667 PathInfo info;
668 CheckError(SDL_GetPathInfo(path, &info));
669 return info;
670}
671
701 StringParam pattern,
702 GlobFlags flags = 0)
703{
704 int count;
705 auto data = CheckError(SDL_GlobDirectory(path, pattern, flags, &count));
706 return OwnArray<char*>{data, size_t(count)};
707}
708
730inline Path GetCurrentDirectory() { return Path{SDL_GetCurrentDirectory()}; }
731
733
734} // namespace SDL
735
736#endif /* SDL3PP_FILESYSTEM_H_ */
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
constexpr bool empty() const
True if size() == 0.
Definition: SDL3pp_ownPtr.h:74
char & back()
Return last element.
Definition: SDL3pp_ownPtr.h:149
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
Path GetPrefPath(StringParam org, StringParam app)
Get the user-and-app-specific path where files can be written.
Definition: SDL3pp_filesystem.h:199
constexpr Folder FOLDER_SCREENSHOTS
Application screenshots.
Definition: SDL3pp_filesystem.h:272
constexpr Folder FOLDER_DOCUMENTS
User document files, possibly application-specific.
Definition: SDL3pp_filesystem.h:255
std::function< EnumerationResult(const char *dirname, const char *fname)> EnumerateDirectoryCB
Callback for directory enumeration.
Definition: SDL3pp_filesystem.h:488
constexpr EnumerationResult ENUM_CONTINUE
Value that requests that enumeration continue.
Definition: SDL3pp_filesystem.h:426
SDL_PathType PathType
Types of filesystem entries.
Definition: SDL3pp_filesystem.h:329
constexpr Folder FOLDER_TEMPLATES
Template files to be used when the user requests the desktop environment to create a new file in a ce...
Definition: SDL3pp_filesystem.h:280
PathInfo GetPathInfo(StringParam path)
Get information about a filesystem path.
Definition: SDL3pp_filesystem.h:665
constexpr PathType PATHTYPE_OTHER
something completely different like a device node (not a symlink, those are always followed)
Definition: SDL3pp_filesystem.h:341
void RenamePath(StringParam oldpath, StringParam newpath)
Rename a file or directory.
Definition: SDL3pp_filesystem.h:603
EnumerationResult(SDLCALL *)(void *userdata, const char *dirname, const char *fname) EnumerateDirectoryCallback
Callback for directory enumeration.
Definition: SDL3pp_filesystem.h:460
constexpr GlobFlags GLOB_CASEINSENSITIVE
CASEINSENSITIVE.
Definition: SDL3pp_filesystem.h:394
constexpr Folder FOLDER_COUNT
Total number of types in this enum, not a folder type by itself.
Definition: SDL3pp_filesystem.h:288
SDL_EnumerationResult EnumerationResult
Possible results from an enumeration callback.
Definition: SDL3pp_filesystem.h:424
SDL_PathInfo PathInfoRaw
Alias to raw representation for PathInfo.
Definition: SDL3pp_filesystem.h:34
constexpr PathType PATHTYPE_NONE
path does not exist
Definition: SDL3pp_filesystem.h:331
constexpr EnumerationResult ENUM_FAILURE
Value that requests that enumeration stop, as a failure.
Definition: SDL3pp_filesystem.h:433
constexpr Folder FOLDER_PUBLICSHARE
Files that are meant to be shared with other users on the same computer.
Definition: SDL3pp_filesystem.h:267
constexpr EnumerationResult ENUM_SUCCESS
Value that requests that enumeration stop, successfully.
Definition: SDL3pp_filesystem.h:430
constexpr Folder FOLDER_SAVEDGAMES
Save files for games.
Definition: SDL3pp_filesystem.h:269
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory through a callback function.
Definition: SDL3pp_filesystem.h:510
Path GetCurrentDirectory()
Get what the system believes is the "current working directory.".
Definition: SDL3pp_filesystem.h:730
const char * GetUserFolder(Folder folder)
Finds the most suitable user folder for a specific purpose.
Definition: SDL3pp_filesystem.h:314
constexpr Folder FOLDER_DOWNLOADS
Standard folder for user files downloaded from the internet.
Definition: SDL3pp_filesystem.h:258
constexpr Folder FOLDER_VIDEOS
Video files that can be played using a standard video player (mp4, webm...).
Definition: SDL3pp_filesystem.h:285
SDL_Folder Folder
The type of the OS-provided default folder for a specific purpose.
Definition: SDL3pp_filesystem.h:233
void CopyFile(StringParam oldpath, StringParam newpath)
Copy a file.
Definition: SDL3pp_filesystem.h:649
Uint32 GlobFlags
Flags for path matching.
Definition: SDL3pp_filesystem.h:392
void CreateDirectory(StringParam path)
Create a directory, and any missing parent directories.
Definition: SDL3pp_filesystem.h:412
constexpr Folder FOLDER_PICTURES
Image files that can be displayed using a standard viewer (png, jpg...).
Definition: SDL3pp_filesystem.h:264
void RemovePath(StringParam path)
Remove a file or an empty directory.
Definition: SDL3pp_filesystem.h:580
constexpr Folder FOLDER_DESKTOP
The folder of files that are displayed on the desktop.
Definition: SDL3pp_filesystem.h:249
constexpr Folder FOLDER_HOME
The folder which contains all of the current user's data, preferences, and documents.
Definition: SDL3pp_filesystem.h:241
const char * GetBasePath()
Get the directory where the application was run from.
Definition: SDL3pp_filesystem.h:138
constexpr PathType PATHTYPE_FILE
a normal file
Definition: SDL3pp_filesystem.h:333
constexpr PathType PATHTYPE_DIRECTORY
a directory
Definition: SDL3pp_filesystem.h:335
OwnArray< char * > GlobDirectory(StringParam path, StringParam pattern, GlobFlags flags=0)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition: SDL3pp_filesystem.h:700
constexpr Folder FOLDER_MUSIC
Music files that can be played using a standard music player (mp3, ogg...).
Definition: SDL3pp_filesystem.h:261
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:341
Main include header for the SDL3pp library.
Information about a path on the filesystem.
Definition: SDL3pp_filesystem.h:352
constexpr PathInfo(const PathInfoRaw &pathInfo={}) noexcept
Wraps PathInfo.
Definition: SDL3pp_filesystem.h:358
constexpr bool operator==(std::nullptr_t _) const noexcept
Compare with nullptr.
Definition: SDL3pp_filesystem.h:368
Convenience representation of a path under SDL.
Definition: SDL3pp_filesystem.h:44
Path & operator+=(std::string_view other)
Append.
Definition: SDL3pp_filesystem.h:49
Path operator+(std::string_view other) const
Append.
Definition: SDL3pp_filesystem.h:63
Path & operator+=(char ch)
Append.
Definition: SDL3pp_filesystem.h:56
Path & operator/=(std::string_view other)
Append path component.
Definition: SDL3pp_filesystem.h:79
Path operator/(std::string_view other) const
Append path component.
Definition: SDL3pp_filesystem.h:86
Path operator+(char ch) const
Append.
Definition: SDL3pp_filesystem.h:71
A simple std::string-like interface for SDL allocated strings.
Definition: SDL3pp_strings.h:153
StringResult & operator+=(std::string_view other)
Append string.
Definition: SDL3pp_strings.h:186
StringResult(const StringResult &other)
Copy ctor.
Definition: SDL3pp_strings.h:158