file_path.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /* Copyright (C) 2010-2019 The RetroArch team
  2. *
  3. * ---------------------------------------------------------------------------------------
  4. * The following license statement only applies to this file (file_path.h).
  5. * ---------------------------------------------------------------------------------------
  6. *
  7. * Permission is hereby granted, free of charge,
  8. * to any person obtaining a copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  11. * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #ifndef __LIBRETRO_SDK_FILE_PATH_H
  23. #define __LIBRETRO_SDK_FILE_PATH_H
  24. #include <stdio.h>
  25. #include <stdint.h>
  26. #include <stddef.h>
  27. #include <sys/types.h>
  28. #include <libretro.h>
  29. #include <retro_common_api.h>
  30. #include <boolean.h>
  31. RETRO_BEGIN_DECLS
  32. #define PATH_REQUIRED_VFS_VERSION 3
  33. void path_vfs_init(const struct retro_vfs_interface_info* vfs_info);
  34. /* Order in this enum is equivalent to negative sort order in filelist
  35. * (i.e. DIRECTORY is on top of PLAIN_FILE) */
  36. enum
  37. {
  38. RARCH_FILETYPE_UNSET,
  39. RARCH_PLAIN_FILE,
  40. RARCH_COMPRESSED_FILE_IN_ARCHIVE,
  41. RARCH_COMPRESSED_ARCHIVE,
  42. RARCH_DIRECTORY,
  43. RARCH_FILE_UNSUPPORTED
  44. };
  45. /**
  46. * path_is_compressed_file:
  47. * @path : path
  48. *
  49. * Checks if path is a compressed file.
  50. *
  51. * Returns: true (1) if path is a compressed file, otherwise false (0).
  52. **/
  53. bool path_is_compressed_file(const char *path);
  54. /**
  55. * path_contains_compressed_file:
  56. * @path : path
  57. *
  58. * Checks if path contains a compressed file.
  59. *
  60. * Currently we only check for hash symbol (#) inside the pathname.
  61. * If path is ever expanded to a general URI, we should check for that here.
  62. *
  63. * Example: Somewhere in the path there might be a compressed file
  64. * E.g.: /path/to/file.7z#mygame.img
  65. *
  66. * Returns: true (1) if path contains compressed file, otherwise false (0).
  67. **/
  68. #define path_contains_compressed_file(path) (path_get_archive_delim((path)) != NULL)
  69. /**
  70. * path_get_archive_delim:
  71. * @path : path
  72. *
  73. * Gets delimiter of an archive file. Only the first '#'
  74. * after a compression extension is considered.
  75. *
  76. * Returns: pointer to the delimiter in the path if it contains
  77. * a compressed file, otherwise NULL.
  78. */
  79. const char *path_get_archive_delim(const char *path);
  80. /**
  81. * path_get_extension:
  82. * @path : path
  83. *
  84. * Gets extension of file. Only '.'s
  85. * after the last slash are considered.
  86. *
  87. * Returns: extension part from the path.
  88. */
  89. const char *path_get_extension(const char *path);
  90. /**
  91. * path_remove_extension:
  92. * @path : path
  93. *
  94. * Mutates path by removing its extension. Removes all
  95. * text after and including the last '.'.
  96. * Only '.'s after the last slash are considered.
  97. *
  98. * Returns:
  99. * 1) If path has an extension, returns path with the
  100. * extension removed.
  101. * 2) If there is no extension, returns NULL.
  102. * 3) If path is empty or NULL, returns NULL
  103. */
  104. char *path_remove_extension(char *path);
  105. /**
  106. * path_basename:
  107. * @path : path
  108. *
  109. * Get basename from @path.
  110. *
  111. * Returns: basename from path.
  112. **/
  113. const char *path_basename(const char *path);
  114. /**
  115. * path_basedir:
  116. * @path : path
  117. *
  118. * Extracts base directory by mutating path.
  119. * Keeps trailing '/'.
  120. **/
  121. void path_basedir(char *path);
  122. /**
  123. * path_parent_dir:
  124. * @path : path
  125. *
  126. * Extracts parent directory by mutating path.
  127. * Assumes that path is a directory. Keeps trailing '/'.
  128. * If the path was already at the root directory, returns empty string
  129. **/
  130. void path_parent_dir(char *path);
  131. /**
  132. * path_resolve_realpath:
  133. * @buf : input and output buffer for path
  134. * @size : size of buffer
  135. * @resolve_symlinks : whether to resolve symlinks or not
  136. *
  137. * Resolves use of ".", "..", multiple slashes etc in absolute paths.
  138. *
  139. * Relative paths are rebased on the current working dir.
  140. *
  141. * Returns: @buf if successful, NULL otherwise.
  142. * Note: Not implemented on consoles
  143. * Note: Symlinks are only resolved on Unix-likes
  144. * Note: The current working dir might not be what you expect,
  145. * e.g. on Android it is "/"
  146. * Use of fill_pathname_resolve_relative() should be prefered
  147. **/
  148. char *path_resolve_realpath(char *buf, size_t size, bool resolve_symlinks);
  149. /**
  150. * path_relative_to:
  151. * @out : buffer to write the relative path to
  152. * @path : path to be expressed relatively
  153. * @base : relative to this
  154. * @size : size of output buffer
  155. *
  156. * Turns @path into a path relative to @base and writes it to @out.
  157. *
  158. * @base is assumed to be a base directory, i.e. a path ending with '/' or '\'.
  159. * Both @path and @base are assumed to be absolute paths without "." or "..".
  160. *
  161. * E.g. path /a/b/e/f.cgp with base /a/b/c/d/ turns into ../../e/f.cgp
  162. **/
  163. void path_relative_to(char *out, const char *path, const char *base, size_t size);
  164. /**
  165. * path_is_absolute:
  166. * @path : path
  167. *
  168. * Checks if @path is an absolute path or a relative path.
  169. *
  170. * Returns: true if path is absolute, false if path is relative.
  171. **/
  172. bool path_is_absolute(const char *path);
  173. /**
  174. * fill_pathname:
  175. * @out_path : output path
  176. * @in_path : input path
  177. * @replace : what to replace
  178. * @size : buffer size of output path
  179. *
  180. * FIXME: Verify
  181. *
  182. * Replaces filename extension with 'replace' and outputs result to out_path.
  183. * The extension here is considered to be the string from the last '.'
  184. * to the end.
  185. *
  186. * Only '.'s after the last slash are considered as extensions.
  187. * If no '.' is present, in_path and replace will simply be concatenated.
  188. * 'size' is buffer size of 'out_path'.
  189. * E.g.: in_path = "/foo/bar/baz/boo.c", replace = ".asm" =>
  190. * out_path = "/foo/bar/baz/boo.asm"
  191. * E.g.: in_path = "/foo/bar/baz/boo.c", replace = "" =>
  192. * out_path = "/foo/bar/baz/boo"
  193. */
  194. void fill_pathname(char *out_path, const char *in_path,
  195. const char *replace, size_t size);
  196. /**
  197. * fill_dated_filename:
  198. * @out_filename : output filename
  199. * @ext : extension of output filename
  200. * @size : buffer size of output filename
  201. *
  202. * Creates a 'dated' filename prefixed by 'RetroArch', and
  203. * concatenates extension (@ext) to it.
  204. *
  205. * E.g.:
  206. * out_filename = "RetroArch-{month}{day}-{Hours}{Minutes}.{@ext}"
  207. **/
  208. void fill_dated_filename(char *out_filename,
  209. const char *ext, size_t size);
  210. /**
  211. * fill_str_dated_filename:
  212. * @out_filename : output filename
  213. * @in_str : input string
  214. * @ext : extension of output filename
  215. * @size : buffer size of output filename
  216. *
  217. * Creates a 'dated' filename prefixed by the string @in_str, and
  218. * concatenates extension (@ext) to it.
  219. *
  220. * E.g.:
  221. * out_filename = "RetroArch-{year}{month}{day}-{Hour}{Minute}{Second}.{@ext}"
  222. **/
  223. void fill_str_dated_filename(char *out_filename,
  224. const char *in_str, const char *ext, size_t size);
  225. /**
  226. * fill_pathname_noext:
  227. * @out_path : output path
  228. * @in_path : input path
  229. * @replace : what to replace
  230. * @size : buffer size of output path
  231. *
  232. * Appends a filename extension 'replace' to 'in_path', and outputs
  233. * result in 'out_path'.
  234. *
  235. * Assumes in_path has no extension. If an extension is still
  236. * present in 'in_path', it will be ignored.
  237. *
  238. */
  239. void fill_pathname_noext(char *out_path, const char *in_path,
  240. const char *replace, size_t size);
  241. /**
  242. * find_last_slash:
  243. * @str : input path
  244. *
  245. * Gets a pointer to the last slash in the input path.
  246. *
  247. * Returns: a pointer to the last slash in the input path.
  248. **/
  249. char *find_last_slash(const char *str);
  250. /**
  251. * fill_pathname_dir:
  252. * @in_dir : input directory path
  253. * @in_basename : input basename to be appended to @in_dir
  254. * @replace : replacement to be appended to @in_basename
  255. * @size : size of buffer
  256. *
  257. * Appends basename of 'in_basename', to 'in_dir', along with 'replace'.
  258. * Basename of in_basename is the string after the last '/' or '\\',
  259. * i.e the filename without directories.
  260. *
  261. * If in_basename has no '/' or '\\', the whole 'in_basename' will be used.
  262. * 'size' is buffer size of 'in_dir'.
  263. *
  264. * E.g..: in_dir = "/tmp/some_dir", in_basename = "/some_content/foo.c",
  265. * replace = ".asm" => in_dir = "/tmp/some_dir/foo.c.asm"
  266. **/
  267. void fill_pathname_dir(char *in_dir, const char *in_basename,
  268. const char *replace, size_t size);
  269. /**
  270. * fill_pathname_base:
  271. * @out : output path
  272. * @in_path : input path
  273. * @size : size of output path
  274. *
  275. * Copies basename of @in_path into @out_path.
  276. **/
  277. void fill_pathname_base(char *out_path, const char *in_path, size_t size);
  278. void fill_pathname_base_noext(char *out_dir,
  279. const char *in_path, size_t size);
  280. void fill_pathname_base_ext(char *out,
  281. const char *in_path, const char *ext,
  282. size_t size);
  283. /**
  284. * fill_pathname_basedir:
  285. * @out_dir : output directory
  286. * @in_path : input path
  287. * @size : size of output directory
  288. *
  289. * Copies base directory of @in_path into @out_path.
  290. * If in_path is a path without any slashes (relative current directory),
  291. * @out_path will get path "./".
  292. **/
  293. void fill_pathname_basedir(char *out_path, const char *in_path, size_t size);
  294. void fill_pathname_basedir_noext(char *out_dir,
  295. const char *in_path, size_t size);
  296. /**
  297. * fill_pathname_parent_dir_name:
  298. * @out_dir : output directory
  299. * @in_dir : input directory
  300. * @size : size of output directory
  301. *
  302. * Copies only the parent directory name of @in_dir into @out_dir.
  303. * The two buffers must not overlap. Removes trailing '/'.
  304. * Returns true on success, false if a slash was not found in the path.
  305. **/
  306. bool fill_pathname_parent_dir_name(char *out_dir,
  307. const char *in_dir, size_t size);
  308. /**
  309. * fill_pathname_parent_dir:
  310. * @out_dir : output directory
  311. * @in_dir : input directory
  312. * @size : size of output directory
  313. *
  314. * Copies parent directory of @in_dir into @out_dir.
  315. * Assumes @in_dir is a directory. Keeps trailing '/'.
  316. * If the path was already at the root directory, @out_dir will be an empty string.
  317. **/
  318. void fill_pathname_parent_dir(char *out_dir,
  319. const char *in_dir, size_t size);
  320. /**
  321. * fill_pathname_resolve_relative:
  322. * @out_path : output path
  323. * @in_refpath : input reference path
  324. * @in_path : input path
  325. * @size : size of @out_path
  326. *
  327. * Joins basedir of @in_refpath together with @in_path.
  328. * If @in_path is an absolute path, out_path = in_path.
  329. * E.g.: in_refpath = "/foo/bar/baz.a", in_path = "foobar.cg",
  330. * out_path = "/foo/bar/foobar.cg".
  331. **/
  332. void fill_pathname_resolve_relative(char *out_path, const char *in_refpath,
  333. const char *in_path, size_t size);
  334. /**
  335. * fill_pathname_join:
  336. * @out_path : output path
  337. * @dir : directory
  338. * @path : path
  339. * @size : size of output path
  340. *
  341. * Joins a directory (@dir) and path (@path) together.
  342. * Makes sure not to get two consecutive slashes
  343. * between directory and path.
  344. **/
  345. void fill_pathname_join(char *out_path, const char *dir,
  346. const char *path, size_t size);
  347. void fill_pathname_join_special_ext(char *out_path,
  348. const char *dir, const char *path,
  349. const char *last, const char *ext,
  350. size_t size);
  351. void fill_pathname_join_concat_noext(char *out_path,
  352. const char *dir, const char *path,
  353. const char *concat,
  354. size_t size);
  355. void fill_pathname_join_concat(char *out_path,
  356. const char *dir, const char *path,
  357. const char *concat,
  358. size_t size);
  359. void fill_pathname_join_noext(char *out_path,
  360. const char *dir, const char *path, size_t size);
  361. /**
  362. * fill_pathname_join_delim:
  363. * @out_path : output path
  364. * @dir : directory
  365. * @path : path
  366. * @delim : delimiter
  367. * @size : size of output path
  368. *
  369. * Joins a directory (@dir) and path (@path) together
  370. * using the given delimiter (@delim).
  371. **/
  372. void fill_pathname_join_delim(char *out_path, const char *dir,
  373. const char *path, const char delim, size_t size);
  374. void fill_pathname_join_delim_concat(char *out_path, const char *dir,
  375. const char *path, const char delim, const char *concat,
  376. size_t size);
  377. /**
  378. * fill_short_pathname_representation:
  379. * @out_rep : output representation
  380. * @in_path : input path
  381. * @size : size of output representation
  382. *
  383. * Generates a short representation of path. It should only
  384. * be used for displaying the result; the output representation is not
  385. * binding in any meaningful way (for a normal path, this is the same as basename)
  386. * In case of more complex URLs, this should cut everything except for
  387. * the main image file.
  388. *
  389. * E.g.: "/path/to/game.img" -> game.img
  390. * "/path/to/myarchive.7z#folder/to/game.img" -> game.img
  391. */
  392. void fill_short_pathname_representation(char* out_rep,
  393. const char *in_path, size_t size);
  394. void fill_short_pathname_representation_noext(char* out_rep,
  395. const char *in_path, size_t size);
  396. void fill_pathname_expand_special(char *out_path,
  397. const char *in_path, size_t size);
  398. void fill_pathname_abbreviate_special(char *out_path,
  399. const char *in_path, size_t size);
  400. /**
  401. * path_basedir:
  402. * @path : path
  403. *
  404. * Extracts base directory by mutating path.
  405. * Keeps trailing '/'.
  406. **/
  407. void path_basedir_wrapper(char *path);
  408. /**
  409. * path_char_is_slash:
  410. * @c : character
  411. *
  412. * Checks if character (@c) is a slash.
  413. *
  414. * Returns: true (1) if character is a slash, otherwise false (0).
  415. */
  416. #ifdef _WIN32
  417. #define path_char_is_slash(c) (((c) == '/') || ((c) == '\\'))
  418. #else
  419. #define path_char_is_slash(c) ((c) == '/')
  420. #endif
  421. /**
  422. * path_default_slash and path_default_slash_c:
  423. *
  424. * Gets the default slash separator.
  425. *
  426. * Returns: default slash separator.
  427. */
  428. #ifdef _WIN32
  429. #define path_default_slash() "\\"
  430. #define path_default_slash_c() '\\'
  431. #else
  432. #define path_default_slash() "/"
  433. #define path_default_slash_c() '/'
  434. #endif
  435. /**
  436. * fill_pathname_slash:
  437. * @path : path
  438. * @size : size of path
  439. *
  440. * Assumes path is a directory. Appends a slash
  441. * if not already there.
  442. **/
  443. void fill_pathname_slash(char *path, size_t size);
  444. #if !defined(RARCH_CONSOLE) && defined(RARCH_INTERNAL)
  445. void fill_pathname_application_path(char *buf, size_t size);
  446. void fill_pathname_application_dir(char *buf, size_t size);
  447. void fill_pathname_home_dir(char *buf, size_t size);
  448. #endif
  449. /**
  450. * path_mkdir:
  451. * @dir : directory
  452. *
  453. * Create directory on filesystem.
  454. *
  455. * Returns: true (1) if directory could be created, otherwise false (0).
  456. **/
  457. bool path_mkdir(const char *dir);
  458. /**
  459. * path_is_directory:
  460. * @path : path
  461. *
  462. * Checks if path is a directory.
  463. *
  464. * Returns: true (1) if path is a directory, otherwise false (0).
  465. */
  466. bool path_is_directory(const char *path);
  467. bool path_is_character_special(const char *path);
  468. int path_stat(const char *path);
  469. bool path_is_valid(const char *path);
  470. int32_t path_get_size(const char *path);
  471. bool is_path_accessible_using_standard_io(const char *path);
  472. RETRO_END_DECLS
  473. #endif