SDL 3.0
SDL_filesystem.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 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
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryFilesystem
24 *
25 * SDL Filesystem API.
26 */
27
28#ifndef SDL_filesystem_h_
29#define SDL_filesystem_h_
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33
34#include <SDL3/SDL_begin_code.h>
35
36/* Set up for C function definitions, even when using C++ */
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
42 * Get the directory where the application was run from.
43 *
44 * SDL caches the result of this call internally, but the first call to this
45 * function is not necessarily fast, so plan accordingly.
46 *
47 * **macOS and iOS Specific Functionality**: If the application is in a ".app"
48 * bundle, this function returns the Resource directory (e.g.
49 * MyApp.app/Contents/Resources/). This behaviour can be overridden by adding
50 * a property to the Info.plist file. Adding a string key with the name
51 * SDL_FILESYSTEM_BASE_DIR_TYPE with a supported value will change the
52 * behaviour.
53 *
54 * Supported values for the SDL_FILESYSTEM_BASE_DIR_TYPE property (Given an
55 * application in /Applications/SDLApp/MyApp.app):
56 *
57 * - `resource`: bundle resource directory (the default). For example:
58 * `/Applications/SDLApp/MyApp.app/Contents/Resources`
59 * - `bundle`: the Bundle directory. For example:
60 * `/Applications/SDLApp/MyApp.app/`
61 * - `parent`: the containing directory of the bundle. For example:
62 * `/Applications/SDLApp/`
63 *
64 * **Nintendo 3DS Specific Functionality**: This function returns "romfs"
65 * directory of the application as it is uncommon to store resources outside
66 * the executable. As such it is not a writable directory.
67 *
68 * The returned path is guaranteed to end with a path separator ('\\' on
69 * Windows, '/' on most other platforms).
70 *
71 * \returns an absolute path in UTF-8 encoding to the application data
72 * directory. NULL will be returned on error or when the platform
73 * doesn't implement this functionality, call SDL_GetError() for more
74 * information.
75 *
76 * \since This function is available since SDL 3.0.0.
77 *
78 * \sa SDL_GetPrefPath
79 */
80extern SDL_DECLSPEC const char * SDLCALL SDL_GetBasePath(void);
81
82/**
83 * Get the user-and-app-specific path where files can be written.
84 *
85 * Get the "pref dir". This is meant to be where users can write personal
86 * files (preferences and save games, etc) that are specific to your
87 * application. This directory is unique per user, per application.
88 *
89 * This function will decide the appropriate location in the native
90 * filesystem, create the directory if necessary, and return a string of the
91 * absolute path to the directory in UTF-8 encoding.
92 *
93 * On Windows, the string might look like:
94 *
95 * `C:\\Users\\bob\\AppData\\Roaming\\My Company\\My Program Name\\`
96 *
97 * On Linux, the string might look like:
98 *
99 * `/home/bob/.local/share/My Program Name/`
100 *
101 * On macOS, the string might look like:
102 *
103 * `/Users/bob/Library/Application Support/My Program Name/`
104 *
105 * You should assume the path returned by this function is the only safe place
106 * to write files (and that SDL_GetBasePath(), while it might be writable, or
107 * even the parent of the returned path, isn't where you should be writing
108 * things).
109 *
110 * Both the org and app strings may become part of a directory name, so please
111 * follow these rules:
112 *
113 * - Try to use the same org string (_including case-sensitivity_) for all
114 * your applications that use this function.
115 * - Always use a unique app string for each one, and make sure it never
116 * changes for an app once you've decided on it.
117 * - Unicode characters are legal, as long as they are UTF-8 encoded, but...
118 * - ...only use letters, numbers, and spaces. Avoid punctuation like "Game
119 * Name 2: Bad Guy's Revenge!" ... "Game Name 2" is sufficient.
120 *
121 * The returned path is guaranteed to end with a path separator ('\\' on
122 * Windows, '/' on most other platforms).
123 *
124 * \param org the name of your organization.
125 * \param app the name of your application.
126 * \returns a UTF-8 string of the user directory in platform-dependent
127 * notation. NULL if there's a problem (creating directory failed,
128 * etc.). This should be freed with SDL_free() when it is no longer
129 * needed.
130 *
131 * \since This function is available since SDL 3.0.0.
132 *
133 * \sa SDL_GetBasePath
134 */
135extern SDL_DECLSPEC char * SDLCALL SDL_GetPrefPath(const char *org, const char *app);
136
137/**
138 * The type of the OS-provided default folder for a specific purpose.
139 *
140 * Note that the Trash folder isn't included here, because trashing files
141 * usually involves extra OS-specific functionality to remember the file's
142 * original location.
143 *
144 * The folders supported per platform are:
145 *
146 * | | Windows | macOS/iOS | tvOS | Unix (XDG) | Haiku | Emscripten |
147 * | ----------- | ------- | --------- | ---- | ---------- | ----- | ---------- |
148 * | HOME | X | X | | X | X | X |
149 * | DESKTOP | X | X | | X | X | |
150 * | DOCUMENTS | X | X | | X | | |
151 * | DOWNLOADS | Vista+ | X | | X | | |
152 * | MUSIC | X | X | | X | | |
153 * | PICTURES | X | X | | X | | |
154 * | PUBLICSHARE | | X | | X | | |
155 * | SAVEDGAMES | Vista+ | | | | | |
156 * | SCREENSHOTS | Vista+ | | | | | |
157 * | TEMPLATES | X | X | | X | | |
158 * | VIDEOS | X | X* | | X | | |
159 *
160 * Note that on macOS/iOS, the Videos folder is called "Movies".
161 *
162 * \since This enum is available since SDL 3.0.0.
163 *
164 * \sa SDL_GetUserFolder
165 */
166typedef enum SDL_Folder
167{
168 SDL_FOLDER_HOME, /**< The folder which contains all of the current user's data, preferences, and documents. It usually contains most of the other folders. If a requested folder does not exist, the home folder can be considered a safe fallback to store a user's documents. */
169
170 SDL_FOLDER_DESKTOP, /**< The folder of files that are displayed on the desktop. Note that the existence of a desktop folder does not guarantee that the system does show icons on its desktop; certain GNU/Linux distros with a graphical environment may not have desktop icons. */
171
172 SDL_FOLDER_DOCUMENTS, /**< User document files, possibly application-specific. This is a good place to save a user's projects. */
173
174 SDL_FOLDER_DOWNLOADS, /**< Standard folder for user files downloaded from the internet. */
175
176 SDL_FOLDER_MUSIC, /**< Music files that can be played using a standard music player (mp3, ogg...). */
177
178 SDL_FOLDER_PICTURES, /**< Image files that can be displayed using a standard viewer (png, jpg...). */
179
180 SDL_FOLDER_PUBLICSHARE, /**< Files that are meant to be shared with other users on the same computer. */
181
182 SDL_FOLDER_SAVEDGAMES, /**< Save files for games. */
183
184 SDL_FOLDER_SCREENSHOTS, /**< Application screenshots. */
185
186 SDL_FOLDER_TEMPLATES, /**< Template files to be used when the user requests the desktop environment to create a new file in a certain folder, such as "New Text File.txt". Any file in the Templates folder can be used as a starting point for a new file. */
187
188 SDL_FOLDER_VIDEOS, /**< Video files that can be played using a standard video player (mp4, webm...). */
189
190 SDL_FOLDER_COUNT /**< Total number of types in this enum, not a folder type by itself. */
191
193
194/**
195 * Finds the most suitable user folder for a specific purpose.
196 *
197 * Many OSes provide certain standard folders for certain purposes, such as
198 * storing pictures, music or videos for a certain user. This function gives
199 * the path for many of those special locations.
200 *
201 * This function is specifically for _user_ folders, which are meant for the
202 * user to access and manage. For application-specific folders, meant to hold
203 * data for the application to manage, see SDL_GetBasePath() and
204 * SDL_GetPrefPath().
205 *
206 * The returned path is guaranteed to end with a path separator ('\\' on
207 * Windows, '/' on most other platforms).
208 *
209 * If NULL is returned, the error may be obtained with SDL_GetError().
210 *
211 * \param folder the type of folder to find.
212 * \returns either a null-terminated C string containing the full path to the
213 * folder, or NULL if an error happened.
214 *
215 * \since This function is available since SDL 3.0.0.
216 */
217extern SDL_DECLSPEC const char * SDLCALL SDL_GetUserFolder(SDL_Folder folder);
218
219
220/* Abstract filesystem interface */
221
222typedef enum SDL_PathType
223{
224 SDL_PATHTYPE_NONE, /**< path does not exist */
225 SDL_PATHTYPE_FILE, /**< a normal file */
226 SDL_PATHTYPE_DIRECTORY, /**< a directory */
227 SDL_PATHTYPE_OTHER /**< something completely different like a device node (not a symlink, those are always followed) */
229
230typedef struct SDL_PathInfo
231{
232 SDL_PathType type; /**< the path type */
233 Uint64 size; /**< the file size in bytes */
234 SDL_Time create_time; /**< the time when the path was created */
235 SDL_Time modify_time; /**< the last time the path was modified */
236 SDL_Time access_time; /**< the last time the path was read */
238
239/**
240 * Flags for path matching
241 *
242 * \since This datatype is available since SDL 3.0.0.
243 *
244 * \sa SDL_GlobDirectory
245 * \sa SDL_GlobStorageDirectory
246 */
248
249#define SDL_GLOB_CASEINSENSITIVE (1u << 0)
250
251/**
252 * Create a directory.
253 *
254 * \param path the path of the directory to create.
255 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
256 * for more information.
257 *
258 * \since This function is available since SDL 3.0.0.
259 */
260extern SDL_DECLSPEC SDL_bool SDLCALL SDL_CreateDirectory(const char *path);
261
262/* Callback for directory enumeration. Return 1 to keep enumerating,
263 0 to stop enumerating (no error), -1 to stop enumerating and
264 report an error. `dirname` is the directory being enumerated,
265 `fname` is the enumerated entry. */
266typedef int (SDLCALL *SDL_EnumerateDirectoryCallback)(void *userdata, const char *dirname, const char *fname);
267
268/**
269 * Enumerate a directory through a callback function.
270 *
271 * This function provides every directory entry through an app-provided
272 * callback, called once for each directory entry, until all results have been
273 * provided or the callback returns <= 0.
274 *
275 * \param path the path of the directory to enumerate.
276 * \param callback a function that is called for each entry in the directory.
277 * \param userdata a pointer that is passed to `callback`.
278 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
279 * for more information.
280 *
281 * \since This function is available since SDL 3.0.0.
282 */
283extern SDL_DECLSPEC SDL_bool SDLCALL SDL_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
284
285/**
286 * Remove a file or an empty directory.
287 *
288 * \param path the path of the directory to enumerate.
289 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
290 * for more information.
291 *
292 * \since This function is available since SDL 3.0.0.
293 */
294extern SDL_DECLSPEC SDL_bool SDLCALL SDL_RemovePath(const char *path);
295
296/**
297 * Rename a file or directory.
298 *
299 * \param oldpath the old path.
300 * \param newpath the new path.
301 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
302 * for more information.
303 *
304 * \since This function is available since SDL 3.0.0.
305 */
306extern SDL_DECLSPEC SDL_bool SDLCALL SDL_RenamePath(const char *oldpath, const char *newpath);
307
308/**
309 * Copy a file.
310 *
311 * \param oldpath the old path.
312 * \param newpath the new path.
313 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
314 * for more information.
315 *
316 * \since This function is available since SDL 3.0.0.
317 */
318extern SDL_DECLSPEC SDL_bool SDLCALL SDL_CopyFile(const char *oldpath, const char *newpath);
319
320/**
321 * Get information about a filesystem path.
322 *
323 * \param path the path to query.
324 * \param info a pointer filled in with information about the path, or NULL to
325 * check for the existence of a file.
326 * \returns SDL_TRUE on success or SDL_FALSE if the file doesn't exist, or
327 * another failure; call SDL_GetError() for more information.
328 *
329 * \since This function is available since SDL 3.0.0.
330 */
331extern SDL_DECLSPEC SDL_bool SDLCALL SDL_GetPathInfo(const char *path, SDL_PathInfo *info);
332
333/**
334 * Enumerate a directory tree, filtered by pattern, and return a list.
335 *
336 * Files are filtered out if they don't match the string in `pattern`, which
337 * may contain wildcard characters '*' (match everything) and '?' (match one
338 * character). If pattern is NULL, no filtering is done and all results are
339 * returned. Subdirectories are permitted, and are specified with a path
340 * separator of '/'. Wildcard characters '*' and '?' never match a path
341 * separator.
342 *
343 * `flags` may be set to SDL_GLOB_CASEINSENSITIVE to make the pattern matching
344 * case-insensitive.
345 *
346 * The returned array is always NULL-terminated, for your iterating
347 * convenience, but if `count` is non-NULL, on return it will contain the
348 * number of items in the array, not counting the NULL terminator.
349 *
350 * \param path the path of the directory to enumerate.
351 * \param pattern the pattern that files in the directory must match. Can be
352 * NULL.
353 * \param flags `SDL_GLOB_*` bitflags that affect this search.
354 * \param count on return, will be set to the number of items in the returned
355 * array. Can be NULL.
356 * \returns an array of strings on success or NULL on failure; call
357 * SDL_GetError() for more information. This is a single allocation
358 * that should be freed with SDL_free() when it is no longer needed.
359 *
360 * \threadsafety It is safe to call this function from any thread.
361 *
362 * \since This function is available since SDL 3.0.0.
363 */
364extern SDL_DECLSPEC char ** SDLCALL SDL_GlobDirectory(const char *path, const char *pattern, SDL_GlobFlags flags, int *count);
365
366/* Ends C function definitions when using C++ */
367#ifdef __cplusplus
368}
369#endif
370#include <SDL3/SDL_close_code.h>
371
372#endif /* SDL_filesystem_h_ */
char ** SDL_GlobDirectory(const char *path, const char *pattern, SDL_GlobFlags flags, int *count)
SDL_bool SDL_CopyFile(const char *oldpath, const char *newpath)
const char * SDL_GetUserFolder(SDL_Folder folder)
int(* SDL_EnumerateDirectoryCallback)(void *userdata, const char *dirname, const char *fname)
SDL_Folder
@ SDL_FOLDER_COUNT
@ SDL_FOLDER_DOCUMENTS
@ SDL_FOLDER_TEMPLATES
@ SDL_FOLDER_DOWNLOADS
@ SDL_FOLDER_HOME
@ SDL_FOLDER_SAVEDGAMES
@ SDL_FOLDER_DESKTOP
@ SDL_FOLDER_PICTURES
@ SDL_FOLDER_PUBLICSHARE
@ SDL_FOLDER_SCREENSHOTS
@ SDL_FOLDER_VIDEOS
@ SDL_FOLDER_MUSIC
SDL_bool SDL_RenamePath(const char *oldpath, const char *newpath)
SDL_bool SDL_GetPathInfo(const char *path, SDL_PathInfo *info)
Uint32 SDL_GlobFlags
SDL_bool SDL_RemovePath(const char *path)
SDL_bool SDL_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata)
char * SDL_GetPrefPath(const char *org, const char *app)
const char * SDL_GetBasePath(void)
SDL_bool SDL_CreateDirectory(const char *path)
SDL_PathType
@ SDL_PATHTYPE_FILE
@ SDL_PATHTYPE_NONE
@ SDL_PATHTYPE_DIRECTORY
@ SDL_PATHTYPE_OTHER
uint64_t Uint64
Definition SDL_stdinc.h:375
uint32_t Uint32
Definition SDL_stdinc.h:353
bool SDL_bool
Definition SDL_stdinc.h:301
Sint64 SDL_Time
Definition SDL_stdinc.h:392
SDL_Time create_time
SDL_Time modify_time
SDL_Time access_time
SDL_PathType type