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.
34 #include <compat/msvc.h>
37 #include <string/stdstring.h>
38 #include <streams/file_stream.h>
40 #include <vfs/vfs_implementation.h>
42 #define VFS_ERROR_RETURN_VALUE -1
46 struct retro_vfs_file_handle *hfile;
50 static retro_vfs_get_path_t filestream_get_path_cb = NULL;
51 static retro_vfs_open_t filestream_open_cb = NULL;
52 static retro_vfs_close_t filestream_close_cb = NULL;
53 static retro_vfs_size_t filestream_size_cb = NULL;
54 static retro_vfs_truncate_t filestream_truncate_cb = NULL;
55 static retro_vfs_tell_t filestream_tell_cb = NULL;
56 static retro_vfs_seek_t filestream_seek_cb = NULL;
57 static retro_vfs_read_t filestream_read_cb = NULL;
58 static retro_vfs_write_t filestream_write_cb = NULL;
59 static retro_vfs_flush_t filestream_flush_cb = NULL;
60 static retro_vfs_remove_t filestream_remove_cb = NULL;
61 static retro_vfs_rename_t filestream_rename_cb = NULL;
63 /* VFS Initialization */
65 void filestream_vfs_init(const struct retro_vfs_interface_info* vfs_info)
67 const struct retro_vfs_interface *
68 vfs_iface = vfs_info->iface;
70 filestream_get_path_cb = NULL;
71 filestream_open_cb = NULL;
72 filestream_close_cb = NULL;
73 filestream_tell_cb = NULL;
74 filestream_size_cb = NULL;
75 filestream_truncate_cb = NULL;
76 filestream_seek_cb = NULL;
77 filestream_read_cb = NULL;
78 filestream_write_cb = NULL;
79 filestream_flush_cb = NULL;
80 filestream_remove_cb = NULL;
81 filestream_rename_cb = NULL;
84 (vfs_info->required_interface_version <
85 FILESTREAM_REQUIRED_VFS_VERSION)
89 filestream_get_path_cb = vfs_iface->get_path;
90 filestream_open_cb = vfs_iface->open;
91 filestream_close_cb = vfs_iface->close;
92 filestream_size_cb = vfs_iface->size;
93 filestream_truncate_cb = vfs_iface->truncate;
94 filestream_tell_cb = vfs_iface->tell;
95 filestream_seek_cb = vfs_iface->seek;
96 filestream_read_cb = vfs_iface->read;
97 filestream_write_cb = vfs_iface->write;
98 filestream_flush_cb = vfs_iface->flush;
99 filestream_remove_cb = vfs_iface->remove;
100 filestream_rename_cb = vfs_iface->rename;
103 /* Callback wrappers */
104 bool filestream_exists(const char *path)
110 if (!(dummy = filestream_open(
112 RETRO_VFS_FILE_ACCESS_READ,
113 RETRO_VFS_FILE_ACCESS_HINT_NONE)))
116 if (filestream_close(dummy) != 0)
123 int64_t filestream_get_size(RFILE *stream)
127 if (filestream_size_cb)
128 output = filestream_size_cb(stream->hfile);
130 output = retro_vfs_file_size_impl(
131 (libretro_vfs_implementation_file*)stream->hfile);
133 if (output == VFS_ERROR_RETURN_VALUE)
134 stream->error_flag = true;
139 int64_t filestream_truncate(RFILE *stream, int64_t length)
143 if (filestream_truncate_cb)
144 output = filestream_truncate_cb(stream->hfile, length);
146 output = retro_vfs_file_truncate_impl(
147 (libretro_vfs_implementation_file*)stream->hfile, length);
149 if (output == VFS_ERROR_RETURN_VALUE)
150 stream->error_flag = true;
157 * @path : path to file
158 * @mode : file mode to use when opening (read/write)
161 * Opens a file for reading or writing, depending on the requested mode.
162 * @return A pointer to an RFILE if opened successfully, otherwise NULL.
164 RFILE* filestream_open(const char *path, unsigned mode, unsigned hints)
166 struct retro_vfs_file_handle *fp = NULL;
167 RFILE* output = NULL;
169 if (filestream_open_cb)
170 fp = (struct retro_vfs_file_handle*)
171 filestream_open_cb(path, mode, hints);
173 fp = (struct retro_vfs_file_handle*)
174 retro_vfs_file_open_impl(path, mode, hints);
179 output = (RFILE*)malloc(sizeof(RFILE));
180 output->error_flag = false;
185 char* filestream_gets(RFILE *stream, char *s, size_t len)
192 /* get max bytes or up to a newline */
194 for (len--; len > 0; len--)
196 if ((c = filestream_getc(stream)) == EOF)
204 if (p == s && c == EOF)
209 int filestream_getc(RFILE *stream)
212 if (stream && filestream_read(stream, &c, 1) == 1)
213 return (int)(unsigned char)c;
217 int filestream_vscanf(RFILE *stream, const char* format, va_list *args)
222 const char *bufiter = buf;
224 int64_t startpos = filestream_tell(stream);
225 int64_t maxlen = filestream_read(stream, buf, sizeof(buf)-1);
232 /* Have to copy the input va_list here
233 * > Calling va_arg() on 'args' directly would
234 * cause the va_list to have an indeterminate value
235 * in the function calling filestream_vscanf(),
236 * leading to unexpected behaviour */
238 __va_copy(args_copy, *args);
240 va_copy(args_copy, *args);
248 char* subfmtiter = subfmt;
249 bool asterisk = false;
251 *subfmtiter++ = *format++; /* '%' */
253 /* %[*][width][length]specifier */
258 *subfmtiter++ = *format++;
261 while (ISDIGIT((unsigned char)*format))
262 *subfmtiter++ = *format++; /* width */
265 if (*format == 'h' || *format == 'l')
267 if (format[1] == format[0])
268 *subfmtiter++ = *format++;
269 *subfmtiter++ = *format++;
277 *subfmtiter++ = *format++;
280 /* specifier - always a single character (except ]) */
283 while (*format != ']')
284 *subfmtiter++ = *format++;
285 *subfmtiter++ = *format++;
288 *subfmtiter++ = *format++;
292 *subfmtiter++ = '\0';
294 if (sizeof(void*) != sizeof(long*))
295 abort(); /* all pointers must have the same size */
299 int v = sscanf(bufiter, subfmt, &sublen);
307 int v = sscanf(bufiter, subfmt, va_arg(args_copy, void*), &sublen);
317 else if (isspace((unsigned char)*format))
319 while (isspace((unsigned char)*bufiter))
325 if (*bufiter != *format)
333 filestream_seek(stream, startpos + (bufiter - buf),
334 RETRO_VFS_SEEK_POSITION_START);
339 int filestream_scanf(RFILE *stream, const char* format, ...)
343 va_start(vl, format);
344 result = filestream_vscanf(stream, format, &vl);
349 int64_t filestream_seek(RFILE *stream, int64_t offset, int seek_position)
353 if (filestream_seek_cb)
354 output = filestream_seek_cb(stream->hfile, offset, seek_position);
356 output = retro_vfs_file_seek_impl(
357 (libretro_vfs_implementation_file*)stream->hfile,
358 offset, seek_position);
360 if (output == VFS_ERROR_RETURN_VALUE)
361 stream->error_flag = true;
366 int filestream_eof(RFILE *stream)
368 return filestream_tell(stream) == filestream_get_size(stream) ? EOF : 0;
371 int64_t filestream_tell(RFILE *stream)
375 if (filestream_size_cb)
376 output = filestream_tell_cb(stream->hfile);
378 output = retro_vfs_file_tell_impl(
379 (libretro_vfs_implementation_file*)stream->hfile);
381 if (output == VFS_ERROR_RETURN_VALUE)
382 stream->error_flag = true;
387 void filestream_rewind(RFILE *stream)
391 filestream_seek(stream, 0L, RETRO_VFS_SEEK_POSITION_START);
392 stream->error_flag = false;
395 int64_t filestream_read(RFILE *stream, void *s, int64_t len)
399 if (filestream_read_cb)
400 output = filestream_read_cb(stream->hfile, s, len);
402 output = retro_vfs_file_read_impl(
403 (libretro_vfs_implementation_file*)stream->hfile, s, len);
405 if (output == VFS_ERROR_RETURN_VALUE)
406 stream->error_flag = true;
411 int filestream_flush(RFILE *stream)
415 if (filestream_flush_cb)
416 output = filestream_flush_cb(stream->hfile);
418 output = retro_vfs_file_flush_impl(
419 (libretro_vfs_implementation_file*)stream->hfile);
421 if (output == VFS_ERROR_RETURN_VALUE)
422 stream->error_flag = true;
427 int filestream_delete(const char *path)
429 if (filestream_remove_cb)
430 return filestream_remove_cb(path);
432 return retro_vfs_file_remove_impl(path);
435 int filestream_rename(const char *old_path, const char *new_path)
437 if (filestream_rename_cb)
438 return filestream_rename_cb(old_path, new_path);
440 return retro_vfs_file_rename_impl(old_path, new_path);
443 const char* filestream_get_path(RFILE *stream)
445 if (filestream_get_path_cb)
446 return filestream_get_path_cb(stream->hfile);
448 return retro_vfs_file_get_path_impl(
449 (libretro_vfs_implementation_file*)stream->hfile);
452 int64_t filestream_write(RFILE *stream, const void *s, int64_t len)
456 if (filestream_write_cb)
457 output = filestream_write_cb(stream->hfile, s, len);
459 output = retro_vfs_file_write_impl(
460 (libretro_vfs_implementation_file*)stream->hfile, s, len);
462 if (output == VFS_ERROR_RETURN_VALUE)
463 stream->error_flag = true;
468 int filestream_putc(RFILE *stream, int c)
470 char c_char = (char)c;
473 return filestream_write(stream, &c_char, 1) == 1
474 ? (int)(unsigned char)c
478 int filestream_vprintf(RFILE *stream, const char* format, va_list args)
480 static char buffer[8 * 1024];
481 int64_t num_chars = vsnprintf(buffer, sizeof(buffer),
486 else if (num_chars == 0)
489 return (int)filestream_write(stream, buffer, num_chars);
492 int filestream_printf(RFILE *stream, const char* format, ...)
496 va_start(vl, format);
497 result = filestream_vprintf(stream, format, vl);
502 int filestream_error(RFILE *stream)
504 return (stream && stream->error_flag);
507 int filestream_close(RFILE *stream)
510 struct retro_vfs_file_handle* fp = stream->hfile;
512 if (filestream_close_cb)
513 output = filestream_close_cb(fp);
515 output = retro_vfs_file_close_impl(
516 (libretro_vfs_implementation_file*)fp);
525 * filestream_read_file:
526 * @path : path to file.
527 * @buf : buffer to allocate and read the contents of the
528 * file into. Needs to be freed manually.
529 * @len : optional output integer containing bytes read.
531 * Read the contents of a file into @buf.
533 * @return Non-zero on success.
535 int64_t filestream_read_file(const char *path, void **buf, int64_t *len)
538 int64_t content_buf_size = 0;
539 void *content_buf = NULL;
540 RFILE *file = filestream_open(path,
541 RETRO_VFS_FILE_ACCESS_READ,
542 RETRO_VFS_FILE_ACCESS_HINT_NONE);
550 if ((content_buf_size = filestream_get_size(file)) < 0)
553 if (!(content_buf = malloc((size_t)(content_buf_size + 1))))
555 if ((int64_t)(uint64_t)(content_buf_size + 1) != (content_buf_size + 1))
558 if ((ret = filestream_read(file, content_buf, (int64_t)content_buf_size)) <
562 if (filestream_close(file) != 0)
567 /* Allow for easy reading of strings to be safe.
568 * Will only work with sane character formatting (Unix). */
569 ((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 * @return true on success, otherwise false.
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);
605 ret = filestream_write(file, data, size);
606 if (filestream_close(file) != 0)
608 return (ret == size);
612 * filestream_getline:
614 * Returned pointer must be freed by the caller.
616 char *filestream_getline(RFILE *stream)
618 char *newline_tmp = NULL;
622 char *newline = (char*)malloc(9);
624 if (!stream || !newline)
631 in = filestream_getc(stream);
633 while (in != EOF && in != '\n')
639 if (!(newline_tmp = (char*)realloc(newline, cur_size + 1)))
645 newline = newline_tmp;
649 in = filestream_getc(stream);
656 libretro_vfs_implementation_file* filestream_get_vfs_handle(RFILE *stream)
658 return (libretro_vfs_implementation_file*)stream->hfile;