SDL_filesystem.h 15 KB

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