file_stream.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /* Copyright (C) 2010-2018 The RetroArch team
  2. *
  3. * ---------------------------------------------------------------------------------------
  4. * The following license statement only applies to this file (file_stream.c).
  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. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdarg.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include <streams/file_stream.h>
  32. #define VFS_FRONTEND
  33. #include <vfs/vfs_implementation.h>
  34. static const int64_t vfs_error_return_value = -1;
  35. static retro_vfs_get_path_t filestream_get_path_cb = NULL;
  36. static retro_vfs_open_t filestream_open_cb = NULL;
  37. static retro_vfs_close_t filestream_close_cb = NULL;
  38. static retro_vfs_size_t filestream_size_cb = NULL;
  39. static retro_vfs_truncate_t filestream_truncate_cb = NULL;
  40. static retro_vfs_tell_t filestream_tell_cb = NULL;
  41. static retro_vfs_seek_t filestream_seek_cb = NULL;
  42. static retro_vfs_read_t filestream_read_cb = NULL;
  43. static retro_vfs_write_t filestream_write_cb = NULL;
  44. static retro_vfs_flush_t filestream_flush_cb = NULL;
  45. static retro_vfs_remove_t filestream_remove_cb = NULL;
  46. static retro_vfs_rename_t filestream_rename_cb = NULL;
  47. struct RFILE
  48. {
  49. struct retro_vfs_file_handle *hfile;
  50. bool error_flag;
  51. bool eof_flag;
  52. };
  53. /* VFS Initialization */
  54. void filestream_vfs_init(const struct retro_vfs_interface_info* vfs_info)
  55. {
  56. const struct retro_vfs_interface* vfs_iface;
  57. filestream_get_path_cb = NULL;
  58. filestream_open_cb = NULL;
  59. filestream_close_cb = NULL;
  60. filestream_tell_cb = NULL;
  61. filestream_size_cb = NULL;
  62. filestream_truncate_cb = NULL;
  63. filestream_seek_cb = NULL;
  64. filestream_read_cb = NULL;
  65. filestream_write_cb = NULL;
  66. filestream_flush_cb = NULL;
  67. filestream_remove_cb = NULL;
  68. filestream_rename_cb = NULL;
  69. vfs_iface = vfs_info->iface;
  70. if (vfs_info->required_interface_version < FILESTREAM_REQUIRED_VFS_VERSION
  71. || !vfs_iface)
  72. return;
  73. filestream_get_path_cb = vfs_iface->get_path;
  74. filestream_open_cb = vfs_iface->open;
  75. filestream_close_cb = vfs_iface->close;
  76. filestream_size_cb = vfs_iface->size;
  77. filestream_truncate_cb = vfs_iface->truncate;
  78. filestream_tell_cb = vfs_iface->tell;
  79. filestream_seek_cb = vfs_iface->seek;
  80. filestream_read_cb = vfs_iface->read;
  81. filestream_write_cb = vfs_iface->write;
  82. filestream_flush_cb = vfs_iface->flush;
  83. filestream_remove_cb = vfs_iface->remove;
  84. filestream_rename_cb = vfs_iface->rename;
  85. }
  86. /* Callback wrappers */
  87. bool filestream_exists(const char *path)
  88. {
  89. RFILE *dummy = NULL;
  90. if (!path || !*path)
  91. return false;
  92. dummy = filestream_open(path,
  93. RETRO_VFS_FILE_ACCESS_READ,
  94. RETRO_VFS_FILE_ACCESS_HINT_NONE);
  95. if (!dummy)
  96. return false;
  97. filestream_close(dummy);
  98. return true;
  99. }
  100. int64_t filestream_get_size(RFILE *stream)
  101. {
  102. int64_t output;
  103. if (filestream_size_cb != NULL)
  104. output = filestream_size_cb(stream->hfile);
  105. else
  106. output = retro_vfs_file_size_impl((libretro_vfs_implementation_file*)stream->hfile);
  107. if (output == vfs_error_return_value)
  108. stream->error_flag = true;
  109. return output;
  110. }
  111. int64_t filestream_truncate(RFILE *stream, int64_t length)
  112. {
  113. int64_t output;
  114. if (filestream_truncate_cb != NULL)
  115. output = filestream_truncate_cb(stream->hfile, length);
  116. else
  117. output = retro_vfs_file_truncate_impl((libretro_vfs_implementation_file*)stream->hfile, length);
  118. if (output == vfs_error_return_value)
  119. stream->error_flag = true;
  120. return output;
  121. }
  122. /**
  123. * filestream_open:
  124. * @path : path to file
  125. * @mode : file mode to use when opening (read/write)
  126. * @hints :
  127. *
  128. * Opens a file for reading or writing, depending on the requested mode.
  129. * Returns a pointer to an RFILE if opened successfully, otherwise NULL.
  130. **/
  131. RFILE* filestream_open(const char *path, unsigned mode, unsigned hints)
  132. {
  133. struct retro_vfs_file_handle *fp = NULL;
  134. RFILE* output = NULL;
  135. if (filestream_open_cb != NULL)
  136. fp = (struct retro_vfs_file_handle*)
  137. filestream_open_cb(path, mode, hints);
  138. else
  139. fp = (struct retro_vfs_file_handle*)
  140. retro_vfs_file_open_impl(path, mode, hints);
  141. if (!fp)
  142. return NULL;
  143. output = (RFILE*)malloc(sizeof(RFILE));
  144. output->error_flag = false;
  145. output->eof_flag = false;
  146. output->hfile = fp;
  147. return output;
  148. }
  149. char* filestream_gets(RFILE *stream, char *s, size_t len)
  150. {
  151. int c = 0;
  152. char *p = s;
  153. if (!stream)
  154. return NULL;
  155. /* get max bytes or up to a newline */
  156. for (len--; len > 0; len--)
  157. {
  158. if ((c = filestream_getc(stream)) == EOF)
  159. break;
  160. *p++ = c;
  161. if (c == '\n')
  162. break;
  163. }
  164. *p = 0;
  165. if (p == s && c == EOF)
  166. return NULL;
  167. return (s);
  168. }
  169. int filestream_getc(RFILE *stream)
  170. {
  171. char c = 0;
  172. if (!stream)
  173. return EOF;
  174. if (filestream_read(stream, &c, 1) == 1)
  175. return (int)(unsigned char)c;
  176. return EOF;
  177. }
  178. int filestream_scanf(RFILE *stream, const char* format, ...)
  179. {
  180. char buf[4096];
  181. char subfmt[64];
  182. va_list args;
  183. const char * bufiter = buf;
  184. int64_t startpos = filestream_tell(stream);
  185. int ret = 0;
  186. int64_t maxlen = filestream_read(stream, buf, sizeof(buf)-1);
  187. if (maxlen <= 0)
  188. return EOF;
  189. buf[maxlen] = '\0';
  190. va_start(args, format);
  191. while (*format)
  192. {
  193. if (*format == '%')
  194. {
  195. int sublen;
  196. char* subfmtiter = subfmt;
  197. bool asterisk = false;
  198. *subfmtiter++ = *format++; /* '%' */
  199. /* %[*][width][length]specifier */
  200. if (*format == '*')
  201. {
  202. asterisk = true;
  203. *subfmtiter++ = *format++;
  204. }
  205. while (isdigit(*format)) *subfmtiter++ = *format++; /* width */
  206. /* length */
  207. if (*format == 'h' || *format == 'l')
  208. {
  209. if (format[1] == format[0]) *subfmtiter++ = *format++;
  210. *subfmtiter++ = *format++;
  211. }
  212. else if (*format == 'j' || *format == 'z' || *format == 't' || *format == 'L')
  213. {
  214. *subfmtiter++ = *format++;
  215. }
  216. /* specifier - always a single character (except ]) */
  217. if (*format == '[')
  218. {
  219. while (*format != ']') *subfmtiter++ = *format++;
  220. *subfmtiter++ = *format++;
  221. }
  222. else *subfmtiter++ = *format++;
  223. *subfmtiter++ = '%';
  224. *subfmtiter++ = 'n';
  225. *subfmtiter++ = '\0';
  226. if (sizeof(void*) != sizeof(long*)) abort(); /* all pointers must have the same size */
  227. if (asterisk)
  228. {
  229. if (sscanf(bufiter, subfmt, &sublen) != 0) break;
  230. }
  231. else
  232. {
  233. if (sscanf(bufiter, subfmt, va_arg(args, void*), &sublen) != 1) break;
  234. }
  235. ret++;
  236. bufiter += sublen;
  237. }
  238. else if (isspace(*format))
  239. {
  240. while (isspace(*bufiter)) bufiter++;
  241. format++;
  242. }
  243. else
  244. {
  245. if (*bufiter != *format)
  246. break;
  247. bufiter++;
  248. format++;
  249. }
  250. }
  251. va_end(args);
  252. filestream_seek(stream, startpos+(bufiter-buf), RETRO_VFS_SEEK_POSITION_START);
  253. return ret;
  254. }
  255. int64_t filestream_seek(RFILE *stream, int64_t offset, int seek_position)
  256. {
  257. int64_t output;
  258. if (filestream_seek_cb != NULL)
  259. output = filestream_seek_cb(stream->hfile, offset, seek_position);
  260. else
  261. output = retro_vfs_file_seek_impl((libretro_vfs_implementation_file*)stream->hfile, offset, seek_position);
  262. if (output == vfs_error_return_value)
  263. stream->error_flag = true;
  264. stream->eof_flag = false;
  265. return output;
  266. }
  267. int filestream_eof(RFILE *stream)
  268. {
  269. return stream->eof_flag;
  270. }
  271. int64_t filestream_tell(RFILE *stream)
  272. {
  273. int64_t output;
  274. if (filestream_size_cb != NULL)
  275. output = filestream_tell_cb(stream->hfile);
  276. else
  277. output = retro_vfs_file_tell_impl((libretro_vfs_implementation_file*)stream->hfile);
  278. if (output == vfs_error_return_value)
  279. stream->error_flag = true;
  280. return output;
  281. }
  282. void filestream_rewind(RFILE *stream)
  283. {
  284. if (!stream)
  285. return;
  286. filestream_seek(stream, 0L, RETRO_VFS_SEEK_POSITION_START);
  287. stream->error_flag = false;
  288. stream->eof_flag = false;
  289. }
  290. int64_t filestream_read(RFILE *stream, void *s, int64_t len)
  291. {
  292. int64_t output;
  293. if (filestream_read_cb != NULL)
  294. output = filestream_read_cb(stream->hfile, s, len);
  295. else
  296. output = retro_vfs_file_read_impl(
  297. (libretro_vfs_implementation_file*)stream->hfile, s, len);
  298. if (output == vfs_error_return_value)
  299. stream->error_flag = true;
  300. if (output < len)
  301. stream->eof_flag = true;
  302. return output;
  303. }
  304. int filestream_flush(RFILE *stream)
  305. {
  306. int output;
  307. if (filestream_flush_cb != NULL)
  308. output = filestream_flush_cb(stream->hfile);
  309. else
  310. output = retro_vfs_file_flush_impl((libretro_vfs_implementation_file*)stream->hfile);
  311. if (output == vfs_error_return_value)
  312. stream->error_flag = true;
  313. return output;
  314. }
  315. int filestream_delete(const char *path)
  316. {
  317. if (filestream_remove_cb != NULL)
  318. return filestream_remove_cb(path);
  319. return retro_vfs_file_remove_impl(path);
  320. }
  321. int filestream_rename(const char *old_path, const char *new_path)
  322. {
  323. if (filestream_rename_cb != NULL)
  324. return filestream_rename_cb(old_path, new_path);
  325. return retro_vfs_file_rename_impl(old_path, new_path);
  326. }
  327. const char* filestream_get_path(RFILE *stream)
  328. {
  329. if (filestream_get_path_cb != NULL)
  330. return filestream_get_path_cb(stream->hfile);
  331. return retro_vfs_file_get_path_impl((libretro_vfs_implementation_file*)stream->hfile);
  332. }
  333. int64_t filestream_write(RFILE *stream, const void *s, int64_t len)
  334. {
  335. int64_t output;
  336. if (filestream_write_cb != NULL)
  337. output = filestream_write_cb(stream->hfile, s, len);
  338. else
  339. output = retro_vfs_file_write_impl((libretro_vfs_implementation_file*)stream->hfile, s, len);
  340. if (output == vfs_error_return_value)
  341. stream->error_flag = true;
  342. return output;
  343. }
  344. int filestream_putc(RFILE *stream, int c)
  345. {
  346. char c_char = (char)c;
  347. if (!stream)
  348. return EOF;
  349. return filestream_write(stream, &c_char, 1)==1 ? (int)(unsigned char)c : EOF;
  350. }
  351. int filestream_vprintf(RFILE *stream, const char* format, va_list args)
  352. {
  353. static char buffer[8 * 1024];
  354. int64_t num_chars = vsprintf(buffer, format, args);
  355. if (num_chars < 0)
  356. return -1;
  357. else if (num_chars == 0)
  358. return 0;
  359. return (int)filestream_write(stream, buffer, num_chars);
  360. }
  361. int filestream_printf(RFILE *stream, const char* format, ...)
  362. {
  363. va_list vl;
  364. int result;
  365. va_start(vl, format);
  366. result = filestream_vprintf(stream, format, vl);
  367. va_end(vl);
  368. return result;
  369. }
  370. int filestream_error(RFILE *stream)
  371. {
  372. if (stream && stream->error_flag)
  373. return 1;
  374. return 0;
  375. }
  376. int filestream_close(RFILE *stream)
  377. {
  378. int output;
  379. struct retro_vfs_file_handle* fp = stream->hfile;
  380. if (filestream_close_cb != NULL)
  381. output = filestream_close_cb(fp);
  382. else
  383. output = retro_vfs_file_close_impl((libretro_vfs_implementation_file*)fp);
  384. if (output == 0)
  385. free(stream);
  386. return output;
  387. }
  388. /**
  389. * filestream_read_file:
  390. * @path : path to file.
  391. * @buf : buffer to allocate and read the contents of the
  392. * file into. Needs to be freed manually.
  393. *
  394. * Read the contents of a file into @buf.
  395. *
  396. * Returns: number of items read, -1 on error.
  397. */
  398. int64_t filestream_read_file(const char *path, void **buf, int64_t *len)
  399. {
  400. int64_t ret = 0;
  401. int64_t content_buf_size = 0;
  402. void *content_buf = NULL;
  403. RFILE *file = filestream_open(path,
  404. RETRO_VFS_FILE_ACCESS_READ,
  405. RETRO_VFS_FILE_ACCESS_HINT_NONE);
  406. if (!file)
  407. {
  408. fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
  409. goto error;
  410. }
  411. content_buf_size = filestream_get_size(file);
  412. if (content_buf_size < 0)
  413. goto error;
  414. content_buf = malloc((size_t)(content_buf_size + 1));
  415. if (!content_buf)
  416. goto error;
  417. if ((int64_t)(uint64_t)(content_buf_size + 1) != (content_buf_size + 1))
  418. goto error;
  419. ret = filestream_read(file, content_buf, (int64_t)content_buf_size);
  420. if (ret < 0)
  421. {
  422. fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno));
  423. goto error;
  424. }
  425. filestream_close(file);
  426. *buf = content_buf;
  427. /* Allow for easy reading of strings to be safe.
  428. * Will only work with sane character formatting (Unix). */
  429. ((char*)content_buf)[ret] = '\0';
  430. if (len)
  431. *len = ret;
  432. return 1;
  433. error:
  434. if (file)
  435. filestream_close(file);
  436. if (content_buf)
  437. free(content_buf);
  438. if (len)
  439. *len = -1;
  440. *buf = NULL;
  441. return 0;
  442. }
  443. /**
  444. * filestream_write_file:
  445. * @path : path to file.
  446. * @data : contents to write to the file.
  447. * @size : size of the contents.
  448. *
  449. * Writes data to a file.
  450. *
  451. * Returns: true (1) on success, false (0) otherwise.
  452. */
  453. bool filestream_write_file(const char *path, const void *data, int64_t size)
  454. {
  455. int64_t ret = 0;
  456. RFILE *file = filestream_open(path,
  457. RETRO_VFS_FILE_ACCESS_WRITE,
  458. RETRO_VFS_FILE_ACCESS_HINT_NONE);
  459. if (!file)
  460. return false;
  461. ret = filestream_write(file, data, size);
  462. filestream_close(file);
  463. if (ret != size)
  464. return false;
  465. return true;
  466. }
  467. /* Returned pointer must be freed by the caller. */
  468. char* filestream_getline(RFILE *stream)
  469. {
  470. char *newline_tmp = NULL;
  471. size_t cur_size = 8;
  472. size_t idx = 0;
  473. int in = 0;
  474. char *newline = (char*)malloc(9);
  475. if (!stream || !newline)
  476. {
  477. if (newline)
  478. free(newline);
  479. return NULL;
  480. }
  481. in = filestream_getc(stream);
  482. while (in != EOF && in != '\n')
  483. {
  484. if (idx == cur_size)
  485. {
  486. cur_size *= 2;
  487. newline_tmp = (char*)realloc(newline, cur_size + 1);
  488. if (!newline_tmp)
  489. {
  490. free(newline);
  491. return NULL;
  492. }
  493. newline = newline_tmp;
  494. }
  495. newline[idx++] = in;
  496. in = filestream_getc(stream);
  497. }
  498. newline[idx] = '\0';
  499. return newline;
  500. }
  501. libretro_vfs_implementation_file* filestream_get_vfs_handle(RFILE *stream)
  502. {
  503. return (libretro_vfs_implementation_file*)stream->hfile;
  504. }