| 1 | /* Mednafen - Multi-system Emulator |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | #include "mednafen.h" |
| 19 | #include <stdarg.h> |
| 20 | #include <string.h> |
| 21 | #include <errno.h> |
| 22 | |
| 23 | #include "file.h" |
| 24 | #include "general.h" |
| 25 | |
| 26 | bool MDFNFILE::ApplyIPS(void *unused) |
| 27 | { |
| 28 | (void)unused; |
| 29 | |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | // This function should ALWAYS close the system file "descriptor"(gzip library, zip library, or FILE *) it's given, |
| 34 | // even if it errors out. |
| 35 | bool MDFNFILE::MakeMemWrapAndClose(void *fp) |
| 36 | { |
| 37 | bool ret = FALSE; |
| 38 | |
| 39 | location = 0; |
| 40 | |
| 41 | ::fseek((FILE *)fp, 0, SEEK_END); |
| 42 | f_size = ::ftell((FILE *)fp); |
| 43 | ::fseek((FILE *)fp, 0, SEEK_SET); |
| 44 | |
| 45 | if (!(f_data = (uint8*)malloc(f_size))) |
| 46 | goto fail; |
| 47 | ::fread(f_data, 1, f_size, (FILE *)fp); |
| 48 | |
| 49 | ret = TRUE; |
| 50 | fail: |
| 51 | fclose((FILE*)fp); |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | MDFNFILE::MDFNFILE() |
| 56 | { |
| 57 | f_data = NULL; |
| 58 | f_size = 0; |
| 59 | f_ext = NULL; |
| 60 | |
| 61 | location = 0; |
| 62 | } |
| 63 | |
| 64 | MDFNFILE::MDFNFILE(const char *path, const void *known_ext, const char *purpose) |
| 65 | { |
| 66 | (void)known_ext; |
| 67 | if (!Open(path, known_ext, purpose, false)) |
| 68 | throw(MDFN_Error(0, "TODO ERROR")); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | MDFNFILE::~MDFNFILE() |
| 73 | { |
| 74 | Close(); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | bool MDFNFILE::Open(const char *path, const void *known_ext, const char *purpose, const bool suppress_notfound_pe) |
| 79 | { |
| 80 | FILE *fp; |
| 81 | (void)known_ext; |
| 82 | |
| 83 | if (!(fp = fopen(path, "rb"))) |
| 84 | return FALSE; |
| 85 | |
| 86 | ::fseek(fp, 0, SEEK_SET); |
| 87 | |
| 88 | if (!MakeMemWrapAndClose(fp)) |
| 89 | return FALSE; |
| 90 | |
| 91 | const char *ld = (const char*)strrchr(path, '.'); |
| 92 | f_ext = strdup(ld ? ld + 1 : ""); |
| 93 | |
| 94 | return(TRUE); |
| 95 | } |
| 96 | |
| 97 | bool MDFNFILE::Close(void) |
| 98 | { |
| 99 | if (f_ext) |
| 100 | free(f_ext); |
| 101 | f_ext = 0; |
| 102 | |
| 103 | if (f_data) |
| 104 | free(f_data); |
| 105 | f_data = 0; |
| 106 | |
| 107 | return(1); |
| 108 | } |
| 109 | |
| 110 | uint64 MDFNFILE::fread(void *ptr, size_t element_size, size_t nmemb) |
| 111 | { |
| 112 | int64 avail = f_size - location; |
| 113 | if (nmemb > avail) |
| 114 | nmemb = avail; |
| 115 | |
| 116 | memcpy((uint8*)ptr, f_data + location, nmemb); |
| 117 | location += nmemb; |
| 118 | return nmemb; |
| 119 | } |
| 120 | |
| 121 | int MDFNFILE::fseek(int64 offset, int whence) |
| 122 | { |
| 123 | size_t ptr; |
| 124 | |
| 125 | switch(whence) |
| 126 | { |
| 127 | case SEEK_SET: |
| 128 | ptr = offset; |
| 129 | break; |
| 130 | case SEEK_CUR: |
| 131 | ptr = location + offset; |
| 132 | break; |
| 133 | case SEEK_END: |
| 134 | ptr = f_size + offset; |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | if (ptr <= f_size) |
| 139 | { |
| 140 | location = ptr; |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | int MDFNFILE::read16le(uint16 *val) |
| 148 | { |
| 149 | if ((location + 2) > f_size) |
| 150 | return 0; |
| 151 | |
| 152 | *val = MDFN_de16lsb(f_data + location); |
| 153 | |
| 154 | location += 2; |
| 155 | |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | int MDFNFILE::read32le(uint32 *val) |
| 160 | { |
| 161 | if ((location + 4) > f_size) |
| 162 | return 0; |
| 163 | |
| 164 | *val = MDFN_de32lsb(f_data + location); |
| 165 | |
| 166 | location += 4; |
| 167 | |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | char *MDFNFILE::fgets(char *s, int buffer_size) |
| 172 | { |
| 173 | int pos = 0; |
| 174 | |
| 175 | if (!buffer_size) |
| 176 | return(NULL); |
| 177 | |
| 178 | if (location >= buffer_size) |
| 179 | return(NULL); |
| 180 | |
| 181 | while(pos < (buffer_size - 1) && location < buffer_size) |
| 182 | { |
| 183 | int v = f_data[location]; |
| 184 | s[pos] = v; |
| 185 | location++; |
| 186 | pos++; |
| 187 | if (v == '\n') |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | if (buffer_size) |
| 192 | s[pos] = 0; |
| 193 | |
| 194 | return s; |
| 195 | } |