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
39{
42
44 Path& operator+=(std::string_view other)
45 {
47 return *this;
48 }
49
51 Path& operator+=(char ch)
52 {
54 return *this;
55 }
56
58 Path operator+(std::string_view other) const
59 {
60 Path result(*this);
61 result += other;
62 return result;
63 }
64
66 Path operator+(char ch) const
67 {
68 Path result(*this);
69 result += ch;
70 return result;
71 }
72
74 Path& operator/=(std::string_view other)
75 {
76 if (!empty() && back() != '/' && back() != '\\') this->operator+=('/');
77 return this->operator+=(other);
78 }
79
81 Path operator/(std::string_view other) const
82 {
83 Path result(*this);
84 result /= other;
85 return result;
86 }
87};
88
128inline const char* GetBasePath() { return SDL_GetBasePath(); }
129
184{
185 return Path{SDL_GetPrefPath(org, app)};
186}
187
217using Folder = SDL_Folder;
218
225constexpr Folder FOLDER_HOME = SDL_FOLDER_HOME;
226
233constexpr Folder FOLDER_DESKTOP = SDL_FOLDER_DESKTOP;
234
239constexpr Folder FOLDER_DOCUMENTS = SDL_FOLDER_DOCUMENTS;
240
244constexpr Folder FOLDER_DOWNLOADS = SDL_FOLDER_DOWNLOADS;
245
249constexpr Folder FOLDER_MUSIC = SDL_FOLDER_MUSIC;
250
254constexpr Folder FOLDER_PICTURES = SDL_FOLDER_PICTURES;
255
259constexpr Folder FOLDER_PUBLICSHARE = SDL_FOLDER_PUBLICSHARE;
260
262 SDL_FOLDER_SAVEDGAMES;
263
265 SDL_FOLDER_SCREENSHOTS;
266
272constexpr Folder FOLDER_TEMPLATES = SDL_FOLDER_TEMPLATES;
273
277constexpr Folder FOLDER_VIDEOS = SDL_FOLDER_VIDEOS;
278
282constexpr Folder FOLDER_COUNT = SDL_FOLDER_COUNT;
283
307inline const char* GetUserFolder(Folder folder)
308{
309 return SDL_GetUserFolder(folder);
310}
311
323using PathType = SDL_PathType;
324
325constexpr PathType PATHTYPE_NONE = SDL_PATHTYPE_NONE;
326
327constexpr PathType PATHTYPE_FILE = SDL_PATHTYPE_FILE;
328
329constexpr PathType PATHTYPE_DIRECTORY = SDL_PATHTYPE_DIRECTORY;
330
335constexpr PathType PATHTYPE_OTHER = SDL_PATHTYPE_OTHER;
336
345struct PathInfo : SDL_PathInfo
346{
352 constexpr PathInfo(const SDL_PathInfo& pathInfo = {})
353 : SDL_PathInfo(pathInfo)
354 {
355 }
356
362 constexpr bool operator==(std::nullptr_t _) const { return !bool(*this); }
363
369 constexpr explicit operator bool() const { return type != PATHTYPE_NONE; }
370};
371
380using GlobFlags = Uint32;
381
383 SDL_GLOB_CASEINSENSITIVE;
384
399{
400 CheckError(SDL_CreateDirectory(path));
401}
402
410using EnumerationResult = SDL_EnumerationResult;
411
413 SDL_ENUM_CONTINUE;
414
418constexpr EnumerationResult ENUM_SUCCESS = SDL_ENUM_SUCCESS;
419
423constexpr EnumerationResult ENUM_FAILURE = SDL_ENUM_FAILURE;
424
449using EnumerateDirectoryCallback = SDL_EnumerateDirectoryCallback;
450
475 std::function<EnumerationResult(const char* dirname, const char* fname)>;
476
499 void* userdata)
500{
501 CheckError(SDL_EnumerateDirectory(path, callback, userdata));
502}
503
524{
525 return EnumerateDirectory(
526 std::move(path),
527 [](void* userdata, const char* dirname, const char* fname) {
528 auto& cb = *static_cast<EnumerateDirectoryCB*>(userdata);
529 return cb(dirname, fname);
530 },
531 &callback);
532}
533
542inline std::vector<Path> EnumerateDirectory(StringParam path)
543{
544 std::vector<Path> r;
545 EnumerateDirectory(std::move(path), [&](const char*, const char* fname) {
546 r.emplace_back(fname);
547 return ENUM_CONTINUE;
548 });
549 return r;
550}
551
563inline void RemovePath(StringParam path) { CheckError(SDL_RemovePath(path)); }
564
585inline void RenamePath(StringParam oldpath, StringParam newpath)
586{
587 CheckError(SDL_RenamePath(oldpath, newpath));
588}
589
627inline void CopyFile(StringParam oldpath, StringParam newpath)
628{
629 CheckError(SDL_CopyFile(oldpath, newpath));
630}
631
642{
643 PathInfo info;
644 CheckError(SDL_GetPathInfo(path, &info));
645 return info;
646}
647
677 StringParam pattern,
678 GlobFlags flags = 0)
679{
680 int count;
681 auto data = CheckError(SDL_GlobDirectory(path, pattern, flags, &count));
682 return OwnArray<char*>{data, size_t(count)};
683}
684
704inline Path GetCurrentDirectory() { return Path{SDL_GetCurrentDirectory()}; }
705
707} // namespace SDL
708
709#endif /* SDL3PP_FILESYSTEM_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
constexpr bool empty() const
True if size() == 0.
Definition SDL3pp_ownPtr.h:70
char & back()
Return last element.
Definition SDL3pp_ownPtr.h:147
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:206
Path GetPrefPath(StringParam org, StringParam app)
Get the user-and-app-specific path where files can be written.
Definition SDL3pp_filesystem.h:183
constexpr Folder FOLDER_SCREENSHOTS
Application screenshots.
Definition SDL3pp_filesystem.h:264
constexpr Folder FOLDER_DOCUMENTS
User document files, possibly application-specific.
Definition SDL3pp_filesystem.h:239
std::function< EnumerationResult(const char *dirname, const char *fname)> EnumerateDirectoryCB
Callback for directory enumeration.
Definition SDL3pp_filesystem.h:475
constexpr EnumerationResult ENUM_CONTINUE
Value that requests that enumeration continue.
Definition SDL3pp_filesystem.h:412
SDL_PathType PathType
Types of filesystem entries.
Definition SDL3pp_filesystem.h:323
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:272
PathInfo GetPathInfo(StringParam path)
Get information about a filesystem path.
Definition SDL3pp_filesystem.h:641
constexpr PathType PATHTYPE_OTHER
something completely different like a device node (not a symlink, those are always followed)
Definition SDL3pp_filesystem.h:335
void RenamePath(StringParam oldpath, StringParam newpath)
Rename a file or directory.
Definition SDL3pp_filesystem.h:585
constexpr GlobFlags GLOB_CASEINSENSITIVE
CASEINSENSITIVE.
Definition SDL3pp_filesystem.h:382
constexpr Folder FOLDER_COUNT
Total number of types in this enum, not a folder type by itself.
Definition SDL3pp_filesystem.h:282
SDL_EnumerationResult EnumerationResult
Possible results from an enumeration callback.
Definition SDL3pp_filesystem.h:410
constexpr PathType PATHTYPE_NONE
path does not exist
Definition SDL3pp_filesystem.h:325
constexpr EnumerationResult ENUM_FAILURE
Value that requests that enumeration stop, as a failure.
Definition SDL3pp_filesystem.h:423
constexpr Folder FOLDER_PUBLICSHARE
Files that are meant to be shared with other users on the same computer.
Definition SDL3pp_filesystem.h:259
constexpr EnumerationResult ENUM_SUCCESS
Value that requests that enumeration stop, successfully.
Definition SDL3pp_filesystem.h:418
constexpr Folder FOLDER_SAVEDGAMES
Save files for games.
Definition SDL3pp_filesystem.h:261
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory through a callback function.
Definition SDL3pp_filesystem.h:497
Path GetCurrentDirectory()
Get what the system believes is the "current working directory.".
Definition SDL3pp_filesystem.h:704
const char * GetUserFolder(Folder folder)
Finds the most suitable user folder for a specific purpose.
Definition SDL3pp_filesystem.h:307
SDL_EnumerateDirectoryCallback EnumerateDirectoryCallback
Callback for directory enumeration.
Definition SDL3pp_filesystem.h:449
constexpr Folder FOLDER_DOWNLOADS
Standard folder for user files downloaded from the internet.
Definition SDL3pp_filesystem.h:244
constexpr Folder FOLDER_VIDEOS
Video files that can be played using a standard video player (mp4, webm...).
Definition SDL3pp_filesystem.h:277
SDL_Folder Folder
The type of the OS-provided default folder for a specific purpose.
Definition SDL3pp_filesystem.h:217
void CopyFile(StringParam oldpath, StringParam newpath)
Copy a file.
Definition SDL3pp_filesystem.h:627
Uint32 GlobFlags
Flags for path matching.
Definition SDL3pp_filesystem.h:380
void CreateDirectory(StringParam path)
Create a directory, and any missing parent directories.
Definition SDL3pp_filesystem.h:398
constexpr Folder FOLDER_PICTURES
Image files that can be displayed using a standard viewer (png, jpg...).
Definition SDL3pp_filesystem.h:254
void RemovePath(StringParam path)
Remove a file or an empty directory.
Definition SDL3pp_filesystem.h:563
constexpr Folder FOLDER_DESKTOP
The folder of files that are displayed on the desktop.
Definition SDL3pp_filesystem.h:233
constexpr Folder FOLDER_HOME
The folder which contains all of the current user's data, preferences, and documents.
Definition SDL3pp_filesystem.h:225
const char * GetBasePath()
Get the directory where the application was run from.
Definition SDL3pp_filesystem.h:128
constexpr PathType PATHTYPE_FILE
a normal file
Definition SDL3pp_filesystem.h:327
constexpr PathType PATHTYPE_DIRECTORY
a directory
Definition SDL3pp_filesystem.h:329
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:676
constexpr Folder FOLDER_MUSIC
Music files that can be played using a standard music player (mp3, ogg...).
Definition SDL3pp_filesystem.h:249
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Information about a path on the filesystem.
Definition SDL3pp_filesystem.h:346
constexpr PathInfo(const SDL_PathInfo &pathInfo={})
Wraps PathInfo.
Definition SDL3pp_filesystem.h:352
constexpr bool operator==(std::nullptr_t _) const
Compare with nullptr.
Definition SDL3pp_filesystem.h:362
Convenience representation of a path under SDL.
Definition SDL3pp_filesystem.h:39
Path & operator+=(std::string_view other)
Append.
Definition SDL3pp_filesystem.h:44
Path operator+(std::string_view other) const
Append.
Definition SDL3pp_filesystem.h:58
Path & operator+=(char ch)
Append.
Definition SDL3pp_filesystem.h:51
Path & operator/=(std::string_view other)
Append path component.
Definition SDL3pp_filesystem.h:74
Path operator/(std::string_view other) const
Append path component.
Definition SDL3pp_filesystem.h:81
Path operator+(char ch) const
Append.
Definition SDL3pp_filesystem.h:66
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:187
StringResult(const StringResult &other)
Copy ctor.
Definition SDL3pp_strings.h:159