1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (file_stream.c).
5 * ---------------------------------------------------------------------------------------
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:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
35 #include <compat/msvc.h>
38 #include <string/stdstring.h>
39 #include <streams/file_stream.h>
41 #include <vfs/vfs_implementation.h>
43 #define VFS_ERROR_RETURN_VALUE -1
47 struct retro_vfs_file_handle *hfile;
52 static retro_vfs_get_path_t filestream_get_path_cb = NULL;
53 static retro_vfs_open_t filestream_open_cb = NULL;
54 static retro_vfs_close_t filestream_close_cb = NULL;
55 static retro_vfs_size_t filestream_size_cb = NULL;
56 static retro_vfs_truncate_t filestream_truncate_cb = NULL;
57 static retro_vfs_tell_t filestream_tell_cb = NULL;
58 static retro_vfs_seek_t filestream_seek_cb = NULL;
59 static retro_vfs_read_t filestream_read_cb = NULL;
60 static retro_vfs_write_t filestream_write_cb = NULL;
61 static retro_vfs_flush_t filestream_flush_cb = NULL;
62 static retro_vfs_remove_t filestream_remove_cb = NULL;
63 static retro_vfs_rename_t filestream_rename_cb = NULL;
65 /* VFS Initialization */
67 void filestream_vfs_init(const struct retro_vfs_interface_info* vfs_info)
69 const struct retro_vfs_interface *
70 vfs_iface = vfs_info->iface;
72 filestream_get_path_cb = NULL;
73 filestream_open_cb = NULL;
74 filestream_close_cb = NULL;
75 filestream_tell_cb = NULL;
76 filestream_size_cb = NULL;
77 filestream_truncate_cb = NULL;
78 filestream_seek_cb = NULL;
79 filestream_read_cb = NULL;
80 filestream_write_cb = NULL;
81 filestream_flush_cb = NULL;
82 filestream_remove_cb = NULL;
83 filestream_rename_cb = NULL;
86 (vfs_info->required_interface_version <
87 FILESTREAM_REQUIRED_VFS_VERSION)
91 filestream_get_path_cb = vfs_iface->get_path;
92 filestream_open_cb = vfs_iface->open;
93 filestream_close_cb = vfs_iface->close;
94 filestream_size_cb = vfs_iface->size;
95 filestream_truncate_cb = vfs_iface->truncate;
96 filestream_tell_cb = vfs_iface->tell;
97 filestream_seek_cb = vfs_iface->seek;
98 filestream_read_cb = vfs_iface->read;
99 filestream_write_cb = vfs_iface->write;
100 filestream_flush_cb = vfs_iface->flush;
101 filestream_remove_cb = vfs_iface->remove;
102 filestream_rename_cb = vfs_iface->rename;
105 /* Callback wrappers */
106 bool filestream_exists(const char *path)
113 dummy = filestream_open(
115 RETRO_VFS_FILE_ACCESS_READ,
116 RETRO_VFS_FILE_ACCESS_HINT_NONE);
121 if (filestream_close(dummy) != 0)
129 int64_t filestream_get_size(RFILE *stream)
133 if (filestream_size_cb)
134 output = filestream_size_cb(stream->hfile);
136 output = retro_vfs_file_size_impl(
137 (libretro_vfs_implementation_file*)stream->hfile);
139 if (output == VFS_ERROR_RETURN_VALUE)
140 stream->error_flag = true;
145 int64_t filestream_truncate(RFILE *stream, int64_t length)
149 if (filestream_truncate_cb)
150 output = filestream_truncate_cb(stream->hfile, length);
152 output = retro_vfs_file_truncate_impl(
153 (libretro_vfs_implementation_file*)stream->hfile, length);
155 if (output == VFS_ERROR_RETURN_VALUE)
156 stream->error_flag = true;
163 * @path : path to file
164 * @mode : file mode to use when opening (read/write)
167 * Opens a file for reading or writing, depending on the requested mode.
168 * Returns a pointer to an RFILE if opened successfully, otherwise NULL.
170 RFILE* filestream_open(const char *path, unsigned mode, unsigned hints)
172 struct retro_vfs_file_handle *fp = NULL;
173 RFILE* output = NULL;
175 if (filestream_open_cb)
176 fp = (struct retro_vfs_file_handle*)
177 filestream_open_cb(path, mode, hints);
179 fp = (struct retro_vfs_file_handle*)
180 retro_vfs_file_open_impl(path, mode, hints);
185 output = (RFILE*)malloc(sizeof(RFILE));
186 output->error_flag = false;
187 output->eof_flag = false;
192 char* filestream_gets(RFILE *stream, char *s, size_t len)
199 /* get max bytes or up to a newline */
201 for (len--; len > 0; len--)
203 if ((c = filestream_getc(stream)) == EOF)
211 if (p == s && c == EOF)
216 int filestream_getc(RFILE *stream)
219 if (stream && filestream_read(stream, &c, 1) == 1)
220 return (int)(unsigned char)c;
224 int filestream_scanf(RFILE *stream, const char* format, ...)
229 const char * bufiter = buf;
231 int64_t startpos = filestream_tell(stream);
232 int64_t maxlen = filestream_read(stream, buf, sizeof(buf)-1);
239 va_start(args, format);
246 char* subfmtiter = subfmt;
247 bool asterisk = false;
249 *subfmtiter++ = *format++; /* '%' */
251 /* %[*][width][length]specifier */
256 *subfmtiter++ = *format++;
259 while (ISDIGIT((unsigned char)*format))
260 *subfmtiter++ = *format++; /* width */
263 if (*format == 'h' || *format == 'l')
265 if (format[1] == format[0])
266 *subfmtiter++ = *format++;
267 *subfmtiter++ = *format++;
275 *subfmtiter++ = *format++;
278 /* specifier - always a single character (except ]) */
281 while (*format != ']')
282 *subfmtiter++ = *format++;
283 *subfmtiter++ = *format++;
286 *subfmtiter++ = *format++;
290 *subfmtiter++ = '\0';
292 if (sizeof(void*) != sizeof(long*))
293 abort(); /* all pointers must have the same size */
297 int v = sscanf(bufiter, subfmt, &sublen);
305 int v = sscanf(bufiter, subfmt, va_arg(args, void*), &sublen);
315 else if (isspace((unsigned char)*format))
317 while (isspace((unsigned char)*bufiter))
323 if (*bufiter != *format)
331 filestream_seek(stream, startpos+(bufiter-buf),
332 RETRO_VFS_SEEK_POSITION_START);
337 int64_t filestream_seek(RFILE *stream, int64_t offset, int seek_position)
341 if (filestream_seek_cb)
342 output = filestream_seek_cb(stream->hfile, offset, seek_position);
344 output = retro_vfs_file_seek_impl(
345 (libretro_vfs_implementation_file*)stream->hfile,
346 offset, seek_position);
348 if (output == VFS_ERROR_RETURN_VALUE)
349 stream->error_flag = true;
351 stream->eof_flag = false;
356 int filestream_eof(RFILE *stream)
358 return stream->eof_flag;
361 int64_t filestream_tell(RFILE *stream)
365 if (filestream_size_cb)
366 output = filestream_tell_cb(stream->hfile);
368 output = retro_vfs_file_tell_impl(
369 (libretro_vfs_implementation_file*)stream->hfile);
371 if (output == VFS_ERROR_RETURN_VALUE)
372 stream->error_flag = true;
377 void filestream_rewind(RFILE *stream)
381 filestream_seek(stream, 0L, RETRO_VFS_SEEK_POSITION_START);
382 stream->error_flag = false;
383 stream->eof_flag = false;
386 int64_t filestream_read(RFILE *stream, void *s, int64_t len)
390 if (filestream_read_cb)
391 output = filestream_read_cb(stream->hfile, s, len);
393 output = retro_vfs_file_read_impl(
394 (libretro_vfs_implementation_file*)stream->hfile, s, len);
396 if (output == VFS_ERROR_RETURN_VALUE)
397 stream->error_flag = true;
399 stream->eof_flag = true;
404 int filestream_flush(RFILE *stream)
408 if (filestream_flush_cb)
409 output = filestream_flush_cb(stream->hfile);
411 output = retro_vfs_file_flush_impl(
412 (libretro_vfs_implementation_file*)stream->hfile);
414 if (output == VFS_ERROR_RETURN_VALUE)
415 stream->error_flag = true;
420 int filestream_delete(const char *path)
422 if (filestream_remove_cb)
423 return filestream_remove_cb(path);
425 return retro_vfs_file_remove_impl(path);
428 int filestream_rename(const char *old_path, const char *new_path)
430 if (filestream_rename_cb)
431 return filestream_rename_cb(old_path, new_path);
433 return retro_vfs_file_rename_impl(old_path, new_path);
436 const char* filestream_get_path(RFILE *stream)
438 if (filestream_get_path_cb)
439 return filestream_get_path_cb(stream->hfile);
441 return retro_vfs_file_get_path_impl(
442 (libretro_vfs_implementation_file*)stream->hfile);
445 int64_t filestream_write(RFILE *stream, const void *s, int64_t len)
449 if (filestream_write_cb)
450 output = filestream_write_cb(stream->hfile, s, len);
452 output = retro_vfs_file_write_impl(
453 (libretro_vfs_implementation_file*)stream->hfile, s, len);
455 if (output == VFS_ERROR_RETURN_VALUE)
456 stream->error_flag = true;
461 int filestream_putc(RFILE *stream, int c)
463 char c_char = (char)c;
466 return filestream_write(stream, &c_char, 1) == 1
467 ? (int)(unsigned char)c
471 int filestream_vprintf(RFILE *stream, const char* format, va_list args)
473 static char buffer[8 * 1024];
474 int64_t num_chars = vsnprintf(buffer, sizeof(buffer),
479 else if (num_chars == 0)
482 return (int)filestream_write(stream, buffer, num_chars);
485 int filestream_printf(RFILE *stream, const char* format, ...)
489 va_start(vl, format);
490 result = filestream_vprintf(stream, format, vl);
495 int filestream_error(RFILE *stream)
497 if (stream && stream->error_flag)
502 int filestream_close(RFILE *stream)
505 struct retro_vfs_file_handle* fp = stream->hfile;
507 if (filestream_close_cb)
508 output = filestream_close_cb(fp);
510 output = retro_vfs_file_close_impl(
511 (libretro_vfs_implementation_file*)fp);
520 * filestream_read_file:
521 * @path : path to file.
522 * @buf : buffer to allocate and read the contents of the
523 * file into. Needs to be freed manually.
525 * Read the contents of a file into @buf.
527 * Returns: number of items read, -1 on error.
529 int64_t filestream_read_file(const char *path, void **buf, int64_t *len)
532 int64_t content_buf_size = 0;
533 void *content_buf = NULL;
534 RFILE *file = filestream_open(path,
535 RETRO_VFS_FILE_ACCESS_READ,
536 RETRO_VFS_FILE_ACCESS_HINT_NONE);
544 content_buf_size = filestream_get_size(file);
546 if (content_buf_size < 0)
549 content_buf = malloc((size_t)(content_buf_size + 1));
553 if ((int64_t)(uint64_t)(content_buf_size + 1) != (content_buf_size + 1))
556 ret = filestream_read(file, content_buf, (int64_t)content_buf_size);
560 if (filestream_close(file) != 0)
566 /* Allow for easy reading of strings to be safe.
567 * Will only work with sane character formatting (Unix). */
568 ((char*)content_buf)[ret] = '\0';
577 if (filestream_close(file) != 0)
588 * filestream_write_file:
589 * @path : path to file.
590 * @data : contents to write to the file.
591 * @size : size of the contents.
593 * Writes data to a file.
595 * Returns: true (1) on success, false (0) otherwise.
597 bool filestream_write_file(const char *path, const void *data, int64_t size)
600 RFILE *file = filestream_open(path,
601 RETRO_VFS_FILE_ACCESS_WRITE,
602 RETRO_VFS_FILE_ACCESS_HINT_NONE);
606 ret = filestream_write(file, data, size);
607 if (filestream_close(file) != 0)
617 /* Returned pointer must be freed by the caller. */
618 char* filestream_getline(RFILE *stream)
620 char *newline_tmp = NULL;
624 char *newline = (char*)malloc(9);
626 if (!stream || !newline)
633 in = filestream_getc(stream);
635 while (in != EOF && in != '\n')
640 newline_tmp = (char*)realloc(newline, cur_size + 1);
648 newline = newline_tmp;
652 in = filestream_getc(stream);
659 libretro_vfs_implementation_file* filestream_get_vfs_handle(RFILE *stream)
661 return (libretro_vfs_implementation_file*)stream->hfile;