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 | WinRT/UWP |macOS/iOS | tvOS | Unix (XDG) | Haiku | Emscripten |
147 * | ----------- | ------- | --------- |--------- | ---- | ---------- | ----- | ---------- |
148 * | HOME | X | X | X | | X | X | X |
149 * | DESKTOP | X | X | X | | X | X | |
150 * | DOCUMENTS | X | X | X | | X | | |
151 * | DOWNLOADS | Vista+ | X | X | | X | | |
152 * | MUSIC | X | X | X | | X | | |
153 * | PICTURES | X | X | X | | X | | |
154 * | PUBLICSHARE | | | X | | X | | |
155 * | SAVEDGAMES | Vista+ | | | | | | |
156 * | SCREENSHOTS | Vista+ | X | | | | | |
157 * | TEMPLATES | X | X | X | | X | | |
158 * | VIDEOS | X | 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 /** The folder which contains all of the current user's data, preferences,
169 and documents. It usually contains most of the other folders. If a
170 requested folder does not exist, the home folder can be considered a safe
171 fallback to store a user's documents. */
173 /** The folder of files that are displayed on the desktop. Note that the
174 existence of a desktop folder does not guarantee that the system does
175 show icons on its desktop; certain GNU/Linux distros with a graphical
176 environment may not have desktop icons. */
178 /** User document files, possibly application-specific. This is a good
179 place to save a user's projects. */
181 /** Standard folder for user files downloaded from the internet. */
183 /** Music files that can be played using a standard music player (mp3,
184 ogg...). */
186 /** Image files that can be displayed using a standard viewer (png,
187 jpg...). */
189 /** Files that are meant to be shared with other users on the same
190 computer. */
192 /** Save files for games. */
194 /** Application screenshots. */
196 /** Template files to be used when the user requests the desktop environment
197 to create a new file in a certain folder, such as "New Text File.txt".
198 Any file in the Templates folder can be used as a starting point for a
199 new file. */
201 /** Video files that can be played using a standard video player (mp4,
202 webm...). */
204 /** total number of types in this enum, not a folder type by itself. */
207
208/**
209 * Finds the most suitable user folder for a specific purpose.
210 *
211 * Many OSes provide certain standard folders for certain purposes, such as
212 * storing pictures, music or videos for a certain user. This function gives
213 * the path for many of those special locations.
214 *
215 * This function is specifically for _user_ folders, which are meant for the
216 * user to access and manage. For application-specific folders, meant to hold
217 * data for the application to manage, see SDL_GetBasePath() and
218 * SDL_GetPrefPath().
219 *
220 * The returned path is guaranteed to end with a path separator ('\\' on
221 * Windows, '/' on most other platforms).
222 *
223 * If NULL is returned, the error may be obtained with SDL_GetError().
224 *
225 * \param folder the type of folder to find.
226 * \returns either a null-terminated C string containing the full path to the
227 * folder, or NULL if an error happened.
228 *
229 * \since This function is available since SDL 3.0.0.
230 */
231extern SDL_DECLSPEC const char * SDLCALL SDL_GetUserFolder(SDL_Folder folder);
232
233
234/* Abstract filesystem interface */
235
236typedef enum SDL_PathType
237{
238 SDL_PATHTYPE_NONE, /**< path does not exist */
239 SDL_PATHTYPE_FILE, /**< a normal file */
240 SDL_PATHTYPE_DIRECTORY, /**< a directory */
241 SDL_PATHTYPE_OTHER /**< something completely different like a device node (not a symlink, those are always followed) */
243
244typedef struct SDL_PathInfo
245{
246 SDL_PathType type; /* the path type */
247 Uint64 size; /* the file size in bytes */
248 SDL_Time create_time; /* the time when the path was created */
249 SDL_Time modify_time; /* the last time the path was modified */
250 SDL_Time access_time; /* the last time the path was read */
252
253/**
254 * Flags for path matching
255 *
256 * \since This datatype is available since SDL 3.0.0.
257 *
258 * \sa SDL_GlobDirectory
259 * \sa SDL_GlobStorageDirectory
260 */
262
263#define SDL_GLOB_CASEINSENSITIVE (1u << 0)
264
265/**
266 * Create a directory.
267 *
268 * \param path the path of the directory to create.
269 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
270 * for more information.
271 *
272 * \since This function is available since SDL 3.0.0.
273 */
274extern SDL_DECLSPEC SDL_bool SDLCALL SDL_CreateDirectory(const char *path);
275
276/* Callback for directory enumeration. Return 1 to keep enumerating,
277 0 to stop enumerating (no error), -1 to stop enumerating and
278 report an error. `dirname` is the directory being enumerated,
279 `fname` is the enumerated entry. */
280typedef int (SDLCALL *SDL_EnumerateDirectoryCallback)(void *userdata, const char *dirname, const char *fname);
281
282/**
283 * Enumerate a directory through a callback function.
284 *
285 * This function provides every directory entry through an app-provided
286 * callback, called once for each directory entry, until all results have been
287 * provided or the callback returns <= 0.
288 *
289 * \param path the path of the directory to enumerate.
290 * \param callback a function that is called for each entry in the directory.
291 * \param userdata a pointer that is passed to `callback`.
292 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
293 * for more information.
294 *
295 * \since This function is available since SDL 3.0.0.
296 */
297extern SDL_DECLSPEC SDL_bool SDLCALL SDL_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
298
299/**
300 * Remove a file or an empty directory.
301 *
302 * \param path the path of the directory to enumerate.
303 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
304 * for more information.
305 *
306 * \since This function is available since SDL 3.0.0.
307 */
308extern SDL_DECLSPEC SDL_bool SDLCALL SDL_RemovePath(const char *path);
309
310/**
311 * Rename a file or directory.
312 *
313 * \param oldpath the old path.
314 * \param newpath the new path.
315 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
316 * for more information.
317 *
318 * \since This function is available since SDL 3.0.0.
319 */
320extern SDL_DECLSPEC SDL_bool SDLCALL SDL_RenamePath(const char *oldpath, const char *newpath);
321
322/**
323 * Copy a file.
324 *
325 * \param oldpath the old path.
326 * \param newpath the new path.
327 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
328 * for more information.
329 *
330 * \since This function is available since SDL 3.0.0.
331 */
332extern SDL_DECLSPEC SDL_bool SDLCALL SDL_CopyFile(const char *oldpath, const char *newpath);
333
334/**
335 * Get information about a filesystem path.
336 *
337 * \param path the path to query.
338 * \param info a pointer filled in with information about the path, or NULL to
339 * check for the existence of a file.
340 * \returns SDL_TRUE on success or SDL_FALSE if the file doesn't exist, or
341 * another failure; call SDL_GetError() for more information.
342 *
343 * \since This function is available since SDL 3.0.0.
344 */
345extern SDL_DECLSPEC SDL_bool SDLCALL SDL_GetPathInfo(const char *path, SDL_PathInfo *info);
346
347/**
348 * Enumerate a directory tree, filtered by pattern, and return a list.
349 *
350 * Files are filtered out if they don't match the string in `pattern`, which
351 * may contain wildcard characters '*' (match everything) and '?' (match one
352 * character). If pattern is NULL, no filtering is done and all results are
353 * returned. Subdirectories are permitted, and are specified with a path
354 * separator of '/'. Wildcard characters '*' and '?' never match a path
355 * separator.
356 *
357 * `flags` may be set to SDL_GLOB_CASEINSENSITIVE to make the pattern matching
358 * case-insensitive.
359 *
360 * The returned array is always NULL-terminated, for your iterating
361 * convenience, but if `count` is non-NULL, on return it will contain the
362 * number of items in the array, not counting the NULL terminator.
363 *
364 * \param path the path of the directory to enumerate.
365 * \param pattern the pattern that files in the directory must match. Can be
366 * NULL.
367 * \param flags `SDL_GLOB_*` bitflags that affect this search.
368 * \param count on return, will be set to the number of items in the returned
369 * array. Can be NULL.
370 * \returns an array of strings on success or NULL on failure; call
371 * SDL_GetError() for more information. This is a single allocation
372 * that should be freed with SDL_free() when it is no longer needed.
373 *
374 * \threadsafety It is safe to call this function from any thread.
375 *
376 * \since This function is available since SDL 3.0.0.
377 */
378extern SDL_DECLSPEC char ** SDLCALL SDL_GlobDirectory(const char *path, const char *pattern, SDL_GlobFlags flags, int *count);
379
380/* Ends C function definitions when using C++ */
381#ifdef __cplusplus
382}
383#endif
384#include <SDL3/SDL_close_code.h>
385
386#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_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_TOTAL
@ 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:290
uint32_t Uint32
Definition SDL_stdinc.h:268
bool SDL_bool
Definition SDL_stdinc.h:216
Sint64 SDL_Time
Definition SDL_stdinc.h:307
SDL_Time create_time
SDL_Time modify_time
SDL_Time access_time
SDL_PathType type