| 1 | // TODO/WIP |
| 2 | |
| 3 | /* Mednafen - Multi-system Emulator |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | #include "mednafen.h" |
| 22 | #include "Stream.h" |
| 23 | #include "FileStream.h" |
| 24 | |
| 25 | #include <stdarg.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | FileStream::FileStream(const char *path, const int mode): OpenedMode(mode) |
| 29 | { |
| 30 | fp = filestream_open(path, (mode == MODE_WRITE) ? RFILE_MODE_WRITE : RFILE_MODE_READ, -1); |
| 31 | |
| 32 | if (!fp) |
| 33 | { |
| 34 | ErrnoHolder ene(errno); |
| 35 | |
| 36 | throw(MDFN_Error(ene.Errno(), "Error opening file %s", ene.StrError())); |
| 37 | } |
| 38 | |
| 39 | original_path = strdup(path); |
| 40 | } |
| 41 | |
| 42 | FileStream::~FileStream() |
| 43 | { |
| 44 | if (original_path) |
| 45 | free(original_path); |
| 46 | original_path = NULL; |
| 47 | } |
| 48 | |
| 49 | uint64_t FileStream::attributes(void) |
| 50 | { |
| 51 | uint64_t ret = ATTRIBUTE_SEEKABLE; |
| 52 | |
| 53 | switch(OpenedMode) |
| 54 | { |
| 55 | case MODE_READ: |
| 56 | ret |= ATTRIBUTE_READABLE; |
| 57 | break; |
| 58 | case MODE_WRITE_SAFE: |
| 59 | case MODE_WRITE: |
| 60 | ret |= ATTRIBUTE_WRITEABLE; |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | uint64_t FileStream::read(void *data, uint64_t count, bool error_on_eos) |
| 68 | { |
| 69 | if (!fp) |
| 70 | return 0; |
| 71 | return filestream_read(fp, data, count); |
| 72 | } |
| 73 | |
| 74 | void FileStream::write(const void *data, uint64_t count) |
| 75 | { |
| 76 | if (!fp) |
| 77 | return; |
| 78 | filestream_write(fp, data, count); |
| 79 | } |
| 80 | |
| 81 | void FileStream::seek(int64_t offset, int whence) |
| 82 | { |
| 83 | if (!fp) |
| 84 | return; |
| 85 | filestream_seek(fp, offset, whence); |
| 86 | } |
| 87 | |
| 88 | int64_t FileStream::tell(void) |
| 89 | { |
| 90 | if (!fp) |
| 91 | return -1; |
| 92 | return filestream_tell(fp); |
| 93 | } |
| 94 | |
| 95 | int64_t FileStream::size(void) |
| 96 | { |
| 97 | if (!original_path) |
| 98 | return -1; |
| 99 | |
| 100 | return path_get_size(original_path); |
| 101 | } |
| 102 | |
| 103 | void FileStream::close(void) |
| 104 | { |
| 105 | if (!fp) |
| 106 | return; |
| 107 | filestream_close(fp); |
| 108 | } |