SDL 3.0
|
#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_begin_code.h>
#include <SDL3/SDL_close_code.h>
Go to the source code of this file.
Data Structures | |
struct | SDL_PathInfo |
Macros | |
#define | SDL_GLOB_CASEINSENSITIVE (1u << 0) |
Typedefs | |
typedef Uint32 | SDL_GlobFlags |
typedef SDL_EnumerationResult(* | SDL_EnumerateDirectoryCallback) (void *userdata, const char *dirname, const char *fname) |
Functions | |
const char * | SDL_GetBasePath (void) |
char * | SDL_GetPrefPath (const char *org, const char *app) |
const char * | SDL_GetUserFolder (SDL_Folder folder) |
bool | SDL_CreateDirectory (const char *path) |
bool | SDL_EnumerateDirectory (const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata) |
bool | SDL_RemovePath (const char *path) |
bool | SDL_RenamePath (const char *oldpath, const char *newpath) |
bool | SDL_CopyFile (const char *oldpath, const char *newpath) |
bool | SDL_GetPathInfo (const char *path, SDL_PathInfo *info) |
char ** | SDL_GlobDirectory (const char *path, const char *pattern, SDL_GlobFlags flags, int *count) |
char * | SDL_GetCurrentDirectory (void) |
#define SDL_GLOB_CASEINSENSITIVE (1u << 0) |
Definition at line 272 of file SDL_filesystem.h.
typedef SDL_EnumerationResult(* SDL_EnumerateDirectoryCallback) (void *userdata, const char *dirname, const char *fname) |
Callback for directory enumeration.
Enumeration of directory entries will continue until either all entries have been provided to the callback, or the callback has requested a stop through its return value.
Returning SDL_ENUM_CONTINUE will let enumeration proceed, calling the callback with further entries. SDL_ENUM_SUCCESS and SDL_ENUM_FAILURE will terminate the enumeration early, and dictate the return value of the enumeration function itself.
userdata | an app-controlled pointer that is passed to the callback. |
dirname | the directory that is being enumerated. |
fname | the next entry in the enumeration. |
Definition at line 325 of file SDL_filesystem.h.
typedef Uint32 SDL_GlobFlags |
Flags for path matching.
Definition at line 270 of file SDL_filesystem.h.
Possible results from an enumeration callback.
Definition at line 297 of file SDL_filesystem.h.
enum SDL_Folder |
The type of the OS-provided default folder for a specific purpose.
Note that the Trash folder isn't included here, because trashing files usually involves extra OS-specific functionality to remember the file's original location.
The folders supported per platform are:
Windows | macOS/iOS | tvOS | Unix (XDG) | Haiku | Emscripten | |
---|---|---|---|---|---|---|
HOME | X | X | X | X | X | |
DESKTOP | X | X | X | X | ||
DOCUMENTS | X | X | X | |||
DOWNLOADS | Vista+ | X | X | |||
MUSIC | X | X | X | |||
PICTURES | X | X | X | |||
PUBLICSHARE | X | X | ||||
SAVEDGAMES | Vista+ | |||||
SCREENSHOTS | Vista+ | |||||
TEMPLATES | X | X | X | |||
VIDEOS | X | X* | X |
Note that on macOS/iOS, the Videos folder is called "Movies".
Definition at line 182 of file SDL_filesystem.h.
enum SDL_PathType |
Types of filesystem entries.
Note that there may be other sorts of items on a filesystem: devices, symlinks, named pipes, etc. They are currently reported as SDL_PATHTYPE_OTHER.
Definition at line 237 of file SDL_filesystem.h.
|
extern |
Copy a file.
If the file at newpath
already exists, it will be overwritten with the contents of the file at oldpath
.
This function will block until the copy is complete, which might be a significant time for large files on slow disks. On some platforms, the copy can be handed off to the OS itself, but on others SDL might just open both paths, and read from one and write to the other.
Note that this is not an atomic operation! If something tries to read from newpath
while the copy is in progress, it will see an incomplete copy of the data, and if the calling thread terminates (or the power goes out) during the copy, newpath
's previous contents will be gone, replaced with an incomplete copy of the data. To avoid this risk, it is recommended that the app copy to a temporary file in the same directory as newpath
, and if the copy is successful, use SDL_RenamePath() to replace newpath
with the temporary file. This will ensure that reads of newpath
will either see a complete copy of the data, or it will see the pre-copy state of newpath
.
This function attempts to synchronize the newly-copied data to disk before returning, if the platform allows it, so that the renaming trick will not have a problem in a system crash or power failure, where the file could be renamed but the contents never made it from the system file cache to the physical disk.
If the copy fails for any reason, the state of newpath
is undefined. It might be half a copy, it might be the untouched data of what was already there, or it might be a zero-byte file, etc.
oldpath | the old path. |
newpath | the new path. |
|
extern |
Create a directory, and any missing parent directories.
This reports success if path
already exists as a directory.
If parent directories are missing, it will also create them. Note that if this fails, it will not remove any parent directories it already made.
path | the path of the directory to create. |
|
extern |
Enumerate a directory through a callback function.
This function provides every directory entry through an app-provided callback, called once for each directory entry, until all results have been provided or the callback returns either SDL_ENUM_SUCCESS or SDL_ENUM_FAILURE.
This will return false if there was a system problem in general, or if a callback returns SDL_ENUM_FAILURE. A successful return means a callback returned SDL_ENUM_SUCCESS to halt enumeration, or all directory entries were enumerated.
path | the path of the directory to enumerate. |
callback | a function that is called for each entry in the directory. |
userdata | a pointer that is passed to callback . |
|
extern |
SDL offers an API for examining and manipulating the system's filesystem. This covers most things one would need to do with directories, except for actual file I/O (which is covered by [CategoryIOStream](CategoryIOStream) and [CategoryAsyncIO](CategoryAsyncIO) instead).
There are functions to answer necessary path questions:
SDL also offers functions to manipulate the directory tree: renaming, removing, copying files. Get the directory where the application was run from.
SDL caches the result of this call internally, but the first call to this function is not necessarily fast, so plan accordingly.
macOS and iOS Specific Functionality: If the application is in a ".app" bundle, this function returns the Resource directory (e.g. MyApp.app/Contents/Resources/). This behaviour can be overridden by adding a property to the Info.plist file. Adding a string key with the name SDL_FILESYSTEM_BASE_DIR_TYPE with a supported value will change the behaviour.
Supported values for the SDL_FILESYSTEM_BASE_DIR_TYPE property (Given an application in /Applications/SDLApp/MyApp.app):
resource
: bundle resource directory (the default). For example: /Applications/SDLApp/MyApp.app/Contents/Resources
bundle
: the Bundle directory. For example: /Applications/SDLApp/MyApp.app/
parent
: the containing directory of the bundle. For example: /Applications/SDLApp/
Nintendo 3DS Specific Functionality: This function returns "romfs" directory of the application as it is uncommon to store resources outside the executable. As such it is not a writable directory.
The returned path is guaranteed to end with a path separator ('\' on Windows, '/' on most other platforms).
|
extern |
Get what the system believes is the "current working directory."
For systems without a concept of a current working directory, this will still attempt to provide something reasonable.
SDL does not provide a means to change the current working directory; for platforms without this concept, this would cause surprises with file access outside of SDL.
|
extern |
Get information about a filesystem path.
path | the path to query. |
info | a pointer filled in with information about the path, or NULL to check for the existence of a file. |
|
extern |
Get the user-and-app-specific path where files can be written.
Get the "pref dir". This is meant to be where users can write personal files (preferences and save games, etc) that are specific to your application. This directory is unique per user, per application.
This function will decide the appropriate location in the native filesystem, create the directory if necessary, and return a string of the absolute path to the directory in UTF-8 encoding.
On Windows, the string might look like:
C:\\Users\\bob\\AppData\\Roaming\\My Company\\My Program Name\\
On Linux, the string might look like:
/home/bob/.local/share/My Program Name/
On macOS, the string might look like:
/Users/bob/Library/Application Support/My Program Name/
You should assume the path returned by this function is the only safe place to write files (and that SDL_GetBasePath(), while it might be writable, or even the parent of the returned path, isn't where you should be writing things).
Both the org and app strings may become part of a directory name, so please follow these rules:
The returned path is guaranteed to end with a path separator ('\' on Windows, '/' on most other platforms).
org | the name of your organization. |
app | the name of your application. |
|
extern |
Finds the most suitable user folder for a specific purpose.
Many OSes provide certain standard folders for certain purposes, such as storing pictures, music or videos for a certain user. This function gives the path for many of those special locations.
This function is specifically for user folders, which are meant for the user to access and manage. For application-specific folders, meant to hold data for the application to manage, see SDL_GetBasePath() and SDL_GetPrefPath().
The returned path is guaranteed to end with a path separator ('\' on Windows, '/' on most other platforms).
If NULL is returned, the error may be obtained with SDL_GetError().
folder | the type of folder to find. |
|
extern |
Enumerate a directory tree, filtered by pattern, and return a list.
Files are filtered out if they don't match the string in pattern
, which may contain wildcard characters '*' (match everything) and '?' (match one character). If pattern is NULL, no filtering is done and all results are returned. Subdirectories are permitted, and are specified with a path separator of '/'. Wildcard characters '*' and '?' never match a path separator.
flags
may be set to SDL_GLOB_CASEINSENSITIVE to make the pattern matching case-insensitive.
The returned array is always NULL-terminated, for your iterating convenience, but if count
is non-NULL, on return it will contain the number of items in the array, not counting the NULL terminator.
path | the path of the directory to enumerate. |
pattern | the pattern that files in the directory must match. Can be NULL. |
flags | SDL_GLOB_* bitflags that affect this search. |
count | on return, will be set to the number of items in the returned array. Can be NULL. |
\threadsafety It is safe to call this function from any thread.
|
extern |
Remove a file or an empty directory.
Directories that are not empty will fail; this function will not recursely delete directory trees.
path | the path to remove from the filesystem. |
|
extern |
Rename a file or directory.
If the file at newpath
already exists, it will replaced.
Note that this will not copy files across filesystems/drives/volumes, as that is a much more complicated (and possibly time-consuming) operation.
Which is to say, if this function fails, SDL_CopyFile() to a temporary file in the same directory as newpath
, then SDL_RenamePath() from the temporary file to newpath
and SDL_RemovePath() on oldpath
might work for files. Renaming a non-empty directory across filesystems is dramatically more complex, however.
oldpath | the old path. |
newpath | the new path. |