SDL_filesystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. * \file SDL_filesystem.h
  20. *
  21. * Include file for filesystem SDL API functions
  22. */
  23. #ifndef SDL_filesystem_h_
  24. #define SDL_filesystem_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_begin_code.h>
  27. /* Set up for C function definitions, even when using C++ */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /**
  32. * Get the directory where the application was run from.
  33. *
  34. * This is not necessarily a fast call, so you should call this once near
  35. * startup and save the string if you need it.
  36. *
  37. * **macOS and iOS Specific Functionality**: If the application is in a ".app"
  38. * bundle, this function returns the Resource directory (e.g.
  39. * MyApp.app/Contents/Resources/). This behaviour can be overridden by adding
  40. * a property to the Info.plist file. Adding a string key with the name
  41. * SDL_FILESYSTEM_BASE_DIR_TYPE with a supported value will change the
  42. * behaviour.
  43. *
  44. * Supported values for the SDL_FILESYSTEM_BASE_DIR_TYPE property (Given an
  45. * application in /Applications/SDLApp/MyApp.app):
  46. *
  47. * - `resource`: bundle resource directory (the default). For example:
  48. * `/Applications/SDLApp/MyApp.app/Contents/Resources`
  49. * - `bundle`: the Bundle directory. For example:
  50. * `/Applications/SDLApp/MyApp.app/`
  51. * - `parent`: the containing directory of the bundle. For example:
  52. * `/Applications/SDLApp/`
  53. *
  54. * **Nintendo 3DS Specific Functionality**: This function returns "romfs"
  55. * directory of the application as it is uncommon to store resources outside
  56. * the executable. As such it is not a writable directory.
  57. *
  58. * The returned path is guaranteed to end with a path separator ('\\' on
  59. * Windows, '/' on most other platforms).
  60. *
  61. * The pointer returned is owned by the caller. Please call SDL_free() on the
  62. * pointer when done with it.
  63. *
  64. * \returns an absolute path in UTF-8 encoding to the application data
  65. * directory. NULL will be returned on error or when the platform
  66. * doesn't implement this functionality, call SDL_GetError() for more
  67. * information.
  68. *
  69. * \since This function is available since SDL 3.0.0.
  70. *
  71. * \sa SDL_GetPrefPath
  72. */
  73. extern DECLSPEC char *SDLCALL SDL_GetBasePath(void);
  74. /**
  75. * Get the user-and-app-specific path where files can be written.
  76. *
  77. * Get the "pref dir". This is meant to be where users can write personal
  78. * files (preferences and save games, etc) that are specific to your
  79. * application. This directory is unique per user, per application.
  80. *
  81. * This function will decide the appropriate location in the native
  82. * filesystem, create the directory if necessary, and return a string of the
  83. * absolute path to the directory in UTF-8 encoding.
  84. *
  85. * On Windows, the string might look like:
  86. *
  87. * `C:\\Users\\bob\\AppData\\Roaming\\My Company\\My Program Name\\`
  88. *
  89. * On Linux, the string might look like:
  90. *
  91. * `/home/bob/.local/share/My Program Name/`
  92. *
  93. * On macOS, the string might look like:
  94. *
  95. * `/Users/bob/Library/Application Support/My Program Name/`
  96. *
  97. * You should assume the path returned by this function is the only safe place
  98. * to write files (and that SDL_GetBasePath(), while it might be writable, or
  99. * even the parent of the returned path, isn't where you should be writing
  100. * things).
  101. *
  102. * Both the org and app strings may become part of a directory name, so please
  103. * follow these rules:
  104. *
  105. * - Try to use the same org string (_including case-sensitivity_) for all
  106. * your applications that use this function.
  107. * - Always use a unique app string for each one, and make sure it never
  108. * changes for an app once you've decided on it.
  109. * - Unicode characters are legal, as long as it's UTF-8 encoded, but...
  110. * - ...only use letters, numbers, and spaces. Avoid punctuation like "Game
  111. * Name 2: Bad Guy's Revenge!" ... "Game Name 2" is sufficient.
  112. *
  113. * The returned path is guaranteed to end with a path separator ('\\' on
  114. * Windows, '/' on most other platforms).
  115. *
  116. * The pointer returned is owned by the caller. Please call SDL_free() on the
  117. * pointer when done with it.
  118. *
  119. * \param org the name of your organization
  120. * \param app the name of your application
  121. * \returns a UTF-8 string of the user directory in platform-dependent
  122. * notation. NULL if there's a problem (creating directory failed,
  123. * etc.).
  124. *
  125. * \since This function is available since SDL 3.0.0.
  126. *
  127. * \sa SDL_GetBasePath
  128. */
  129. extern DECLSPEC char *SDLCALL SDL_GetPrefPath(const char *org, const char *app);
  130. /**
  131. * The type of the OS-provided default folder for a specific purpose.
  132. *
  133. * Note that the Trash folder isn't included here, because trashing files usually
  134. * involves extra OS-specific functionality to remember the file's original
  135. * location.
  136. *
  137. * The folders supported per platform are:
  138. *
  139. * | | Windows | macOS/iOS | tvOS | Unix (XDG) | Haiku | Emscripten |
  140. * | ----------- | ------- | --------- | ---- | ---------- | ----- | ---------- |
  141. * | HOME | X | X | | X | X | X |
  142. * | DESKTOP | X | X | | X | X | |
  143. * | DOCUMENTS | X | X | | X | | |
  144. * | DOWNLOADS | Vista+ | X | | X | | |
  145. * | MUSIC | X | X | | X | | |
  146. * | PICTURES | X | X | | X | | |
  147. * | PUBLICSHARE | | X | | X | | |
  148. * | SAVEDGAMES | Vista+ | | | | | |
  149. * | SCREENSHOTS | Vista+ | | | | | |
  150. * | TEMPLATES | X | X | | X | | |
  151. * | VIDEOS | X | X* | | X | | |
  152. *
  153. * * Note that on macOS/iOS, the Videos folder is called "Movies".
  154. *
  155. * \sa SDL_GetUserFolder
  156. */
  157. typedef enum
  158. {
  159. /** The folder which contains all of the current user's data, preferences,
  160. and documents. It usually contains most of the other folders. If a
  161. requested folder does not exist, the home folder can be considered a safe
  162. fallback to store a user's documents. */
  163. SDL_FOLDER_HOME,
  164. /** The folder of files that are displayed on the desktop. Note that the
  165. existence of a desktop folder does not guarantee that the system does
  166. show icons on its desktop; certain GNU/Linux distros with a graphical
  167. environment may not have desktop icons. */
  168. SDL_FOLDER_DESKTOP,
  169. /** User document files, possibly application-specific. This is a good
  170. place to save a user's projects. */
  171. SDL_FOLDER_DOCUMENTS,
  172. /** Standard folder for user files downloaded from the internet. */
  173. SDL_FOLDER_DOWNLOADS,
  174. /** Music files that can be played using a standard music player (mp3,
  175. ogg...). */
  176. SDL_FOLDER_MUSIC,
  177. /** Image files that can be displayed using a standard viewer (png,
  178. jpg...). */
  179. SDL_FOLDER_PICTURES,
  180. /** Files that are meant to be shared with other users on the same
  181. computer. */
  182. SDL_FOLDER_PUBLICSHARE,
  183. /** Save files for games. */
  184. SDL_FOLDER_SAVEDGAMES,
  185. /** Application screenshots. */
  186. SDL_FOLDER_SCREENSHOTS,
  187. /** Template files to be used when the user requests the desktop environment
  188. to create a new file in a certain folder, such as "New Text File.txt".
  189. Any file in the Templates folder can be used as a starting point for a
  190. new file. */
  191. SDL_FOLDER_TEMPLATES,
  192. /** Video files that can be played using a standard video player (mp4,
  193. webm...). */
  194. SDL_FOLDER_VIDEOS
  195. } SDL_Folder;
  196. /**
  197. * Finds the most suitable user folder for the specified purpose, and returns
  198. * its path in OS-specific notation.
  199. *
  200. * Many OSes provide certain standard folders for certain purposes, such as
  201. * storing pictures, music or videos for a certain user. This function gives
  202. * the path for many of those special locations.
  203. *
  204. * This function is specifically for _user_ folders, which are meant for the
  205. * user to access and manage. For application-specific folders, meant to hold
  206. * data for the application to manage, see SDL_GetBasePath() and
  207. * SDL_GetPrefPath().
  208. *
  209. * Note that the function is expensive, and should be called once at the
  210. * beginning of the execution and kept for as long as needed.
  211. *
  212. * The returned value is owned by the caller and should be freed with
  213. * SDL_free().
  214. *
  215. * If NULL is returned, the error may be obtained with SDL_GetError().
  216. *
  217. * \param folder The type of folder to find
  218. * \returns Either a null-terminated C string containing the full path to the
  219. * folder, or NULL if an error happened.
  220. *
  221. * \since This function is available since SDL 3.0.0.
  222. */
  223. extern DECLSPEC char *SDLCALL SDL_GetUserFolder(SDL_Folder folder);
  224. /* Abstract filesystem interface */
  225. typedef enum SDL_PathType
  226. {
  227. SDL_PATHTYPE_FILE, /**< a normal file */
  228. SDL_PATHTYPE_DIRECTORY, /**< a directory */
  229. SDL_PATHTYPE_OTHER /**< something completely different like a device node (not a symlink, those are always followed) */
  230. } SDL_PathType;
  231. /* SDL file timestamps are 64-bit integers representing seconds since the Unix epoch (Jan 1, 1970) */
  232. typedef Sint64 SDL_FileTimestamp;
  233. typedef struct SDL_PathInfo
  234. {
  235. SDL_PathType type; /* the path type */
  236. Uint64 size; /* the file size in bytes */
  237. SDL_FileTimestamp create_time; /* the time when the path was created */
  238. SDL_FileTimestamp modify_time; /* the last time the path was modified */
  239. SDL_FileTimestamp access_time; /* the last time the path was read */
  240. } SDL_PathInfo;
  241. /**
  242. * Create a directory.
  243. *
  244. * \param path the path of the directory to create
  245. * \returns 0 on success or a negative error code on failure; call
  246. * SDL_GetError() for more information.
  247. *
  248. * \since This function is available since SDL 3.0.0.
  249. */
  250. extern DECLSPEC int SDLCALL SDL_CreateDirectory(const char *path);
  251. /* Callback for directory enumeration. Return 1 to keep enumerating, 0 to stop enumerating (no error), -1 to stop enumerating and report an error. `dirname` is the directory being enumerated, `fname` is the enumerated entry. */
  252. typedef int (SDLCALL *SDL_EnumerateDirectoryCallback)(void *userdata, const char *dirname, const char *fname);
  253. /**
  254. * Enumerate a directory.
  255. *
  256. * \param path the path of the directory to enumerate
  257. * \param callback a function that is called for each entry in the directory
  258. * \param userdata a pointer that is passed to `callback`
  259. * \returns 0 on success or a negative error code on failure; call
  260. * SDL_GetError() for more information.
  261. *
  262. * \since This function is available since SDL 3.0.0.
  263. */
  264. extern DECLSPEC int SDLCALL SDL_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
  265. /**
  266. * Remove a file or an empty directory.
  267. *
  268. * \param path the path of the directory to enumerate
  269. * \returns 0 on success or a negative error code on failure; call
  270. * SDL_GetError() for more information.
  271. *
  272. * \since This function is available since SDL 3.0.0.
  273. */
  274. extern DECLSPEC int SDLCALL SDL_RemovePath(const char *path);
  275. /**
  276. * Rename a file or directory.
  277. *
  278. * \param oldpath the old path
  279. * \param newpath the new path
  280. * \returns 0 on success or a negative error code on failure; call
  281. * SDL_GetError() for more information.
  282. *
  283. * \since This function is available since SDL 3.0.0.
  284. */
  285. extern DECLSPEC int SDLCALL SDL_RenamePath(const char *oldpath, const char *newpath);
  286. /**
  287. * Get information about a filesystem path.
  288. *
  289. * \param path the path to query
  290. * \param info a pointer filled in with information about the path
  291. * \returns 0 on success or a negative error code on failure; call
  292. * SDL_GetError() for more information.
  293. *
  294. * \since This function is available since SDL 3.0.0.
  295. */
  296. extern DECLSPEC int SDLCALL SDL_GetPathInfo(const char *path, SDL_PathInfo *info);
  297. /* some helper functions ... */
  298. /* Converts an SDL file timestamp into a Windows FILETIME (100-nanosecond intervals since January 1, 1601). Fills in the two 32-bit values of the FILETIME structure.
  299. *
  300. * \param ftime the time to convert
  301. * \param low a pointer filled in with the low portion of the Windows FILETIME value
  302. * \param high a pointer filled in with the high portion of the Windows FILETIME value
  303. *
  304. * \since This function is available since SDL 3.0.0.
  305. */
  306. extern DECLSPEC void SDLCALL SDL_FileTimeToWindows(Sint64 ftime, Uint32 *low, Uint32 *high);
  307. /* Ends C function definitions when using C++ */
  308. #ifdef __cplusplus
  309. }
  310. #endif
  311. #include <SDL3/SDL_close_code.h>
  312. #endif /* SDL_filesystem_h_ */