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 * \file SDL_filesystem.h
24 *
25 * Include file for filesystem SDL API functions
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 * This is not necessarily a fast call, so you should call this once near
45 * startup and save the string if you need it.
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 * The pointer returned is owned by the caller. Please call SDL_free() on the
72 * pointer when done with it.
73 *
74 * \returns an absolute path in UTF-8 encoding to the application data
75 * directory. NULL will be returned on error or when the platform
76 * doesn't implement this functionality, call SDL_GetError() for more
77 * information.
78 *
79 * \since This function is available since SDL 3.0.0.
80 *
81 * \sa SDL_GetPrefPath
82 */
83extern DECLSPEC char *SDLCALL SDL_GetBasePath(void);
84
85/**
86 * Get the user-and-app-specific path where files can be written.
87 *
88 * Get the "pref dir". This is meant to be where users can write personal
89 * files (preferences and save games, etc) that are specific to your
90 * application. This directory is unique per user, per application.
91 *
92 * This function will decide the appropriate location in the native
93 * filesystem, create the directory if necessary, and return a string of the
94 * absolute path to the directory in UTF-8 encoding.
95 *
96 * On Windows, the string might look like:
97 *
98 * `C:\\Users\\bob\\AppData\\Roaming\\My Company\\My Program Name\\`
99 *
100 * On Linux, the string might look like:
101 *
102 * `/home/bob/.local/share/My Program Name/`
103 *
104 * On macOS, the string might look like:
105 *
106 * `/Users/bob/Library/Application Support/My Program Name/`
107 *
108 * You should assume the path returned by this function is the only safe place
109 * to write files (and that SDL_GetBasePath(), while it might be writable, or
110 * even the parent of the returned path, isn't where you should be writing
111 * things).
112 *
113 * Both the org and app strings may become part of a directory name, so please
114 * follow these rules:
115 *
116 * - Try to use the same org string (_including case-sensitivity_) for all
117 * your applications that use this function.
118 * - Always use a unique app string for each one, and make sure it never
119 * changes for an app once you've decided on it.
120 * - Unicode characters are legal, as long as it's UTF-8 encoded, but...
121 * - ...only use letters, numbers, and spaces. Avoid punctuation like "Game
122 * Name 2: Bad Guy's Revenge!" ... "Game Name 2" is sufficient.
123 *
124 * The returned path is guaranteed to end with a path separator ('\\' on
125 * Windows, '/' on most other platforms).
126 *
127 * The pointer returned is owned by the caller. Please call SDL_free() on the
128 * pointer when done with it.
129 *
130 * \param org the name of your organization
131 * \param app the name of your application
132 * \returns a UTF-8 string of the user directory in platform-dependent
133 * notation. NULL if there's a problem (creating directory failed,
134 * etc.).
135 *
136 * \since This function is available since SDL 3.0.0.
137 *
138 * \sa SDL_GetBasePath
139 */
140extern DECLSPEC char *SDLCALL SDL_GetPrefPath(const char *org, const char *app);
141
142/**
143 * The type of the OS-provided default folder for a specific purpose.
144 *
145 * Note that the Trash folder isn't included here, because trashing files
146 * usually involves extra OS-specific functionality to remember the file's
147 * original location.
148 *
149 * The folders supported per platform are:
150 *
151 * | | Windows | WinRT/UWP |macOS/iOS | tvOS | Unix (XDG) | Haiku | Emscripten |
152 * | ----------- | ------- | --------- |--------- | ---- | ---------- | ----- | ---------- |
153 * | HOME | X | X | X | | X | X | X |
154 * | DESKTOP | X | X | X | | X | X | |
155 * | DOCUMENTS | X | X | X | | X | | |
156 * | DOWNLOADS | Vista+ | X | X | | X | | |
157 * | MUSIC | X | X | X | | X | | |
158 * | PICTURES | X | X | X | | X | | |
159 * | PUBLICSHARE | | | X | | X | | |
160 * | SAVEDGAMES | Vista+ | | | | | | |
161 * | SCREENSHOTS | Vista+ | X | | | | | |
162 * | TEMPLATES | X | X | X | | X | | |
163 * | VIDEOS | X | X | X* | | X | | |
164 *
165 * Note that on macOS/iOS, the Videos folder is called "Movies".
166 *
167 * \since This enum is available since SDL 3.0.0.
168 *
169 * \sa SDL_GetUserFolder
170 */
171typedef enum SDL_Folder
172{
173 /** The folder which contains all of the current user's data, preferences,
174 and documents. It usually contains most of the other folders. If a
175 requested folder does not exist, the home folder can be considered a safe
176 fallback to store a user's documents. */
178 /** The folder of files that are displayed on the desktop. Note that the
179 existence of a desktop folder does not guarantee that the system does
180 show icons on its desktop; certain GNU/Linux distros with a graphical
181 environment may not have desktop icons. */
183 /** User document files, possibly application-specific. This is a good
184 place to save a user's projects. */
186 /** Standard folder for user files downloaded from the internet. */
188 /** Music files that can be played using a standard music player (mp3,
189 ogg...). */
191 /** Image files that can be displayed using a standard viewer (png,
192 jpg...). */
194 /** Files that are meant to be shared with other users on the same
195 computer. */
197 /** Save files for games. */
199 /** Application screenshots. */
201 /** Template files to be used when the user requests the desktop environment
202 to create a new file in a certain folder, such as "New Text File.txt".
203 Any file in the Templates folder can be used as a starting point for a
204 new file. */
206 /** Video files that can be played using a standard video player (mp4,
207 webm...). */
210
211/**
212 * Finds the most suitable user folder for the specified purpose, and returns
213 * its path in OS-specific notation.
214 *
215 * Many OSes provide certain standard folders for certain purposes, such as
216 * storing pictures, music or videos for a certain user. This function gives
217 * the path for many of those special locations.
218 *
219 * This function is specifically for _user_ folders, which are meant for the
220 * user to access and manage. For application-specific folders, meant to hold
221 * data for the application to manage, see SDL_GetBasePath() and
222 * SDL_GetPrefPath().
223 *
224 * Note that the function is expensive, and should be called once at the
225 * beginning of the execution and kept for as long as needed.
226 *
227 * The returned path is guaranteed to end with a path separator ('\\' on
228 * Windows, '/' on most other platforms).
229 *
230 * The returned value is owned by the caller and should be freed with
231 * SDL_free().
232 *
233 * If NULL is returned, the error may be obtained with SDL_GetError().
234 *
235 * \param folder The type of folder to find
236 * \returns Either a null-terminated C string containing the full path to the
237 * folder, or NULL if an error happened.
238 *
239 * \since This function is available since SDL 3.0.0.
240 */
241extern DECLSPEC char *SDLCALL SDL_GetUserFolder(SDL_Folder folder);
242
243
244/* Abstract filesystem interface */
245
246typedef enum SDL_PathType
247{
248 SDL_PATHTYPE_NONE, /**< path does not exist */
249 SDL_PATHTYPE_FILE, /**< a normal file */
250 SDL_PATHTYPE_DIRECTORY, /**< a directory */
251 SDL_PATHTYPE_OTHER /**< something completely different like a device node (not a symlink, those are always followed) */
253
254typedef struct SDL_PathInfo
255{
256 SDL_PathType type; /* the path type */
257 Uint64 size; /* the file size in bytes */
258 SDL_Time create_time; /* the time when the path was created */
259 SDL_Time modify_time; /* the last time the path was modified */
260 SDL_Time access_time; /* the last time the path was read */
262
263/**
264 * Create a directory.
265 *
266 * \param path the path of the directory to create
267 * \returns 0 on success or a negative error code on failure; call
268 * SDL_GetError() for more information.
269 *
270 * \since This function is available since SDL 3.0.0.
271 */
272extern DECLSPEC int SDLCALL SDL_CreateDirectory(const char *path);
273
274/* Callback for directory enumeration. Return 1 to keep enumerating,
275 0 to stop enumerating (no error), -1 to stop enumerating and
276 report an error. `dirname` is the directory being enumerated,
277 `fname` is the enumerated entry. */
278typedef int (SDLCALL *SDL_EnumerateDirectoryCallback)(void *userdata, const char *dirname, const char *fname);
279
280/**
281 * Enumerate a directory through a callback function.
282 *
283 * This function provides every directory entry through an app-provided
284 * callback, called once for each directory entry, until all results have been
285 * provided or the callback returns <= 0.
286 *
287 * \param path the path of the directory to enumerate
288 * \param callback a function that is called for each entry in the directory
289 * \param userdata a pointer that is passed to `callback`
290 * \returns 0 on success or a negative error code on failure; call
291 * SDL_GetError() for more information.
292 *
293 * \since This function is available since SDL 3.0.0.
294 */
295extern DECLSPEC int SDLCALL SDL_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
296
297/**
298 * Remove a file or an empty directory.
299 *
300 * \param path the path of the directory to enumerate
301 * \returns 0 on success or a negative error code on failure; call
302 * SDL_GetError() for more information.
303 *
304 * \since This function is available since SDL 3.0.0.
305 */
306extern DECLSPEC int SDLCALL SDL_RemovePath(const char *path);
307
308/**
309 * Rename a file or directory.
310 *
311 * \param oldpath the old path
312 * \param newpath the new path
313 * \returns 0 on success or a negative error code on failure; call
314 * SDL_GetError() for more information.
315 *
316 * \since This function is available since SDL 3.0.0.
317 */
318extern DECLSPEC int SDLCALL SDL_RenamePath(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 0 on success or a negative error code if the file doesn't exist,
327 * or another failure; call SDL_GetError() for more information.
328 *
329 * \since This function is available since SDL 3.0.0.
330 */
331extern DECLSPEC int SDLCALL SDL_GetPathInfo(const char *path, SDL_PathInfo *info);
332
333
334#define SDL_GLOB_CASEINSENSITIVE (1 << 0)
335
336/**
337 * Enumerate a directory tree, filtered by pattern, and return a list.
338 *
339 * Files are filtered out if they don't match the string in `pattern`, which
340 * may contain wildcard characters '*' (match everything) and '?' (match one
341 * character). If pattern is NULL, no filtering is done and all results are
342 * returned. Subdirectories are permitted, and are specified with a path
343 * separator of '/'. Wildcard characters '*' and '?' never match a path
344 * separator.
345 *
346 * `flags` may be set to SDL_GLOB_CASEINSENSITIVE to make the pattern matching
347 * case-insensitive.
348 *
349 * The returned array is always NULL-terminated, for your iterating
350 * convenience, but if `count` is non-NULL, on return it will contain the
351 * number of items in the array, not counting the NULL terminator.
352 *
353 * You must free the returned pointer with SDL_free() when done with it.
354 *
355 * \param path the path of the directory to enumerate
356 * \param pattern the pattern that files in the directory must match. Can be
357 * NULL.
358 * \param flags `SDL_GLOB_*` bitflags that affect this search.
359 * \param count on return, will be set to the number of items in the returned
360 * array. Can be NULL.
361 * \returns an array of strings on success or NULL on failure; call
362 * SDL_GetError() for more information. The caller should pass the
363 * returned pointer to SDL_free when done with it.
364 *
365 * \threadsafety It is safe to call this function from any thread.
366 *
367 * \since This function is available since SDL 3.0.0.
368 */
369extern DECLSPEC char **SDLCALL SDL_GlobDirectory(const char *path, const char *pattern, Uint32 flags, int *count);
370
371/* Ends C function definitions when using C++ */
372#ifdef __cplusplus
373}
374#endif
375#include <SDL3/SDL_close_code.h>
376
377#endif /* SDL_filesystem_h_ */
char * SDL_GetUserFolder(SDL_Folder folder)
int SDL_RenamePath(const char *oldpath, const char *newpath)
int SDL_CreateDirectory(const char *path)
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_MUSIC
int SDL_RemovePath(const char *path)
int SDL_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata)
char ** SDL_GlobDirectory(const char *path, const char *pattern, Uint32 flags, int *count)
int SDL_GetPathInfo(const char *path, SDL_PathInfo *info)
char * SDL_GetBasePath(void)
char * SDL_GetPrefPath(const char *org, const char *app)
SDL_PathType
@ SDL_PATHTYPE_FILE
@ SDL_PATHTYPE_NONE
@ SDL_PATHTYPE_DIRECTORY
@ SDL_PATHTYPE_OTHER
uint64_t Uint64
Definition SDL_stdinc.h:242
uint32_t Uint32
Definition SDL_stdinc.h:224
Sint64 SDL_Time
Definition SDL_stdinc.h:256
SDL_Time create_time
SDL_Time modify_time
SDL_Time access_time
SDL_PathType type