* [2SF] Used more up-to-date asmjit, despite the ugly looking code.
| ... | ... |
@@ -24,8 +24,7 @@ THE SOFTWARE. |
| 24 | 24 |
|
| 25 | 25 |
// don't use emufile for files bigger than 2GB! you have been warned! some day this will be fixed. |
| 26 | 26 |
|
| 27 |
-#ifndef EMUFILE_H |
|
| 28 |
-#define EMUFILE_H |
|
| 27 |
+#pragma once |
|
| 29 | 28 |
|
| 30 | 29 |
#include <vector> |
| 31 | 30 |
#include <algorithm> |
| ... | ... |
@@ -208,5 +207,3 @@ public: |
| 208 | 207 |
return len; |
| 209 | 208 |
} |
| 210 | 209 |
}; |
| 211 |
- |
|
| 212 |
-#endif |
| ... | ... |
@@ -90,7 +90,7 @@ protected: |
| 90 | 90 |
this->vec->resize(amt); |
| 91 | 91 |
} |
| 92 | 92 |
public: |
| 93 |
- EMUFILE_MEMORY(std::vector<uint8_t> *underlying) : vec(underlying), ownvec(false), pos(0), len(static_cast<int32_t>(underlying->size())) { }
|
|
| 93 |
+ EMUFILE_MEMORY(std::vector<uint8_t> *underlying) : vec(underlying), ownvec(false), pos(0), len(underlying->size()) { }
|
|
| 94 | 94 |
EMUFILE_MEMORY(uint32_t preallocate) : vec(new std::vector<uint8_t>()), ownvec(true), pos(0), len(0) |
| 95 | 95 |
{
|
| 96 | 96 |
this->vec->resize(preallocate); |
| ... | ... |
@@ -196,7 +196,7 @@ public: |
| 196 | 196 |
|
| 197 | 197 |
virtual size_t ftell() |
| 198 | 198 |
{
|
| 199 |
- return static_cast<size_t>(::ftell(this->fp)); |
|
| 199 |
+ return ::ftell(this->fp); |
|
| 200 | 200 |
} |
| 201 | 201 |
|
| 202 | 202 |
virtual size_t size() |
| ... | ... |
@@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | 22 |
THE SOFTWARE. |
| 23 | 23 |
*/ |
| 24 | 24 |
|
| 25 |
-//don't use emufile for files bigger than 2GB! you have been warned! some day this will be fixed. |
|
| 25 |
+// don't use emufile for files bigger than 2GB! you have been warned! some day this will be fixed. |
|
| 26 | 26 |
|
| 27 | 27 |
#ifndef EMUFILE_H |
| 28 | 28 |
#define EMUFILE_H |
| ... | ... |
@@ -35,17 +35,13 @@ THE SOFTWARE. |
| 35 | 35 |
#include <cstring> |
| 36 | 36 |
#include <cstdarg> |
| 37 | 37 |
#include <cmath> |
| 38 |
- |
|
| 39 | 38 |
#include "types.h" |
| 40 | 39 |
|
| 41 | 40 |
#if defined(_MSC_VER) || defined(__MINGW32__) |
| 42 |
-/*#define WIN32_LEAN_AND_MEAN |
|
| 43 |
-#define NOMINMAX |
|
| 44 |
-#include <io.h>*/ |
|
| 45 |
-#include "windowsh_wrapper.h" |
|
| 46 |
-#include <io.h> |
|
| 41 |
+# include "windowsh_wrapper.h" |
|
| 42 |
+# include <io.h> |
|
| 47 | 43 |
#else |
| 48 |
-#include <unistd.h> |
|
| 44 |
+# include <unistd.h> |
|
| 49 | 45 |
#endif |
| 50 | 46 |
|
| 51 | 47 |
class EMUFILE |
| ... | ... |
@@ -54,16 +50,8 @@ protected: |
| 54 | 50 |
bool failbit; |
| 55 | 51 |
|
| 56 | 52 |
public: |
| 57 |
- EMUFILE() : failbit(false) |
|
| 58 |
- {
|
|
| 59 |
- } |
|
| 60 |
- |
|
| 61 |
- //returns a new EMUFILE which is guranteed to be in memory. the EMUFILE you call this on may be deleted. use the returned EMUFILE in its place |
|
| 62 |
- //virtual EMUFILE* memwrap() = 0; |
|
| 63 |
- |
|
| 64 |
- virtual ~EMUFILE() {}
|
|
| 65 |
- |
|
| 66 |
- //static bool readAllBytes(std::vector<uint8_t> *buf, const std::string &fname); |
|
| 53 |
+ EMUFILE() : failbit(false) { }
|
|
| 54 |
+ virtual ~EMUFILE() { }
|
|
| 67 | 55 |
|
| 68 | 56 |
bool fail(bool unset = false) |
| 69 | 57 |
{
|
| ... | ... |
@@ -74,65 +62,21 @@ public: |
| 74 | 62 |
} |
| 75 | 63 |
void unfail() { failbit = false; }
|
| 76 | 64 |
|
| 77 |
- //bool eof() { return size() == ftell(); }
|
|
| 78 |
- |
|
| 79 | 65 |
size_t fread(void *ptr, size_t bytes) |
| 80 | 66 |
{
|
| 81 | 67 |
return _fread(ptr, bytes); |
| 82 | 68 |
} |
| 83 | 69 |
|
| 84 |
- //void unget() { fseek(-1, SEEK_CUR); }
|
|
| 85 |
- |
|
| 86 |
- //virtuals |
|
| 87 |
-public: |
|
| 88 |
- //virtual FILE *get_fp() = 0; |
|
| 89 |
- |
|
| 90 |
- //virtual int fprintf(const char *format, ...) = 0; |
|
| 91 |
- |
|
| 92 |
- //virtual int fgetc() = 0; |
|
| 93 |
- //virtual int fputc(int c) = 0; |
|
| 94 |
- |
|
| 70 |
+ // virtuals |
|
| 95 | 71 |
virtual size_t _fread(void *ptr, size_t bytes) = 0; |
| 96 | 72 |
|
| 97 |
- //removing these return values for now so we can find any code that might be using them and make sure |
|
| 98 |
- //they handle the return values correctly |
|
| 99 |
- |
|
| 100 |
- //virtual void fwrite(const void *ptr, size_t bytes) = 0; |
|
| 101 |
- |
|
| 102 |
- /*void write64le(uint64_t *val); |
|
| 103 |
- void write64le(uint64_t val); |
|
| 104 |
- size_t read64le(uint64_t *val); |
|
| 105 |
- uint64_t read64le(); |
|
| 106 |
- void write32le(uint32_t *val); |
|
| 107 |
- void write32le(int32_t *val) { write32le((uint32_t *)val); }
|
|
| 108 |
- void write32le(uint32_t val); |
|
| 109 |
- size_t read32le(uint32_t *val); |
|
| 110 |
- size_t read32le(int32_t *val); |
|
| 111 |
- uint32_t read32le(); |
|
| 112 |
- void write16le(uint16_t *val); |
|
| 113 |
- void write16le(int16_t *val) { write16le((uint16_t *)val); }
|
|
| 114 |
- void write16le(uint16_t val); |
|
| 115 |
- size_t read16le(int16_t *Bufo); |
|
| 116 |
- size_t read16le(uint16_t *val); |
|
| 117 |
- uint16_t read16le(); |
|
| 118 |
- void write8le(uint8_t *val); |
|
| 119 |
- void write8le(uint8_t val); |
|
| 120 |
- size_t read8le(uint8_t *val); |
|
| 121 |
- uint8_t read8le(); |
|
| 122 |
- void writedouble(double* val); |
|
| 123 |
- void writedouble(double val); |
|
| 124 |
- double readdouble(); |
|
| 125 |
- size_t readdouble(double* val);*/ |
|
| 126 |
- |
|
| 127 | 73 |
virtual int fseek(int offset, int origin) = 0; |
| 128 | 74 |
|
| 129 | 75 |
virtual size_t ftell() = 0; |
| 130 | 76 |
virtual size_t size() = 0; |
| 131 |
- |
|
| 132 |
- //virtual void truncate(int32_t length) = 0; |
|
| 133 | 77 |
}; |
| 134 | 78 |
|
| 135 |
-//todo - handle read-only specially? |
|
| 79 |
+// todo - handle read-only specially? |
|
| 136 | 80 |
class EMUFILE_MEMORY : public EMUFILE |
| 137 | 81 |
{
|
| 138 | 82 |
protected: |
| ... | ... |
@@ -142,144 +86,72 @@ protected: |
| 142 | 86 |
|
| 143 | 87 |
void reserve(uint32_t amt) |
| 144 | 88 |
{
|
| 145 |
- if (vec->size() < amt) |
|
| 146 |
- vec->resize(amt); |
|
| 89 |
+ if (this->vec->size() < amt) |
|
| 90 |
+ this->vec->resize(amt); |
|
| 147 | 91 |
} |
| 148 |
- |
|
| 149 | 92 |
public: |
| 150 | 93 |
EMUFILE_MEMORY(std::vector<uint8_t> *underlying) : vec(underlying), ownvec(false), pos(0), len(static_cast<int32_t>(underlying->size())) { }
|
| 151 | 94 |
EMUFILE_MEMORY(uint32_t preallocate) : vec(new std::vector<uint8_t>()), ownvec(true), pos(0), len(0) |
| 152 | 95 |
{
|
| 153 |
- vec->resize(preallocate); |
|
| 154 |
- len = preallocate; |
|
| 96 |
+ this->vec->resize(preallocate); |
|
| 97 |
+ this->len = preallocate; |
|
| 98 |
+ } |
|
| 99 |
+ EMUFILE_MEMORY() : vec(new std::vector<uint8_t>()), ownvec(true), pos(0), len(0) |
|
| 100 |
+ {
|
|
| 101 |
+ this->vec->reserve(1024); |
|
| 155 | 102 |
} |
| 156 |
- EMUFILE_MEMORY() : vec(new std::vector<uint8_t>()), ownvec(true), pos(0), len(0) { vec->reserve(1024); }
|
|
| 157 | 103 |
EMUFILE_MEMORY(void *Buf, int32_t Size) : vec(new std::vector<uint8_t>()), ownvec(true), pos(0), len(Size) |
| 158 | 104 |
{
|
| 159 |
- vec->resize(Size); |
|
| 105 |
+ this->vec->resize(Size); |
|
| 160 | 106 |
if (Size) |
| 161 |
- memcpy(&(*vec)[0], Buf, Size); |
|
| 107 |
+ memcpy(&(*this->vec)[0], Buf, Size); |
|
| 162 | 108 |
} |
| 163 | 109 |
|
| 164 | 110 |
~EMUFILE_MEMORY() |
| 165 | 111 |
{
|
| 166 |
- if (ownvec) |
|
| 167 |
- delete vec; |
|
| 112 |
+ if (this->ownvec) |
|
| 113 |
+ delete this->vec; |
|
| 168 | 114 |
} |
| 169 | 115 |
|
| 170 |
- //virtual EMUFILE *memwrap(); |
|
| 171 |
- |
|
| 172 |
- /*virtual void truncate(int32_t length) |
|
| 116 |
+ uint8_t *buf() |
|
| 173 | 117 |
{
|
| 174 |
- vec->resize(length); |
|
| 175 |
- len = length; |
|
| 176 |
- if (pos>length) |
|
| 177 |
- pos = length; |
|
| 178 |
- }*/ |
|
| 179 |
- |
|
| 180 |
- uint8_t *buf() {
|
|
| 181 |
- if(size()==0) reserve(1); |
|
| 182 |
- return &(*vec)[0]; |
|
| 118 |
+ if (!this->size()) |
|
| 119 |
+ this->reserve(1); |
|
| 120 |
+ return &(*this->vec)[0]; |
|
| 183 | 121 |
} |
| 184 | 122 |
|
| 185 |
- //std::vector<uint8_t> *get_vec() { return vec; };
|
|
| 186 |
- |
|
| 187 |
- //virtual FILE *get_fp() { return NULL; }
|
|
| 188 |
- |
|
| 189 |
- /*virtual int fprintf(const char *format, ...) |
|
| 190 |
- {
|
|
| 191 |
- va_list argptr; |
|
| 192 |
- va_start(argptr, format); |
|
| 193 |
- |
|
| 194 |
- //we dont generate straight into the buffer because it will null terminate (one more byte than we want) |
|
| 195 |
- int amt = vsnprintf(0, 0, format,argptr); |
|
| 196 |
- char *tempbuf = new char[amt + 1]; |
|
| 197 |
- |
|
| 198 |
- va_end(argptr); |
|
| 199 |
- va_start(argptr, format); |
|
| 200 |
- vsprintf(tempbuf, format, argptr); |
|
| 201 |
- |
|
| 202 |
- fwrite(tempbuf, amt); |
|
| 203 |
- delete [] tempbuf; |
|
| 204 |
- |
|
| 205 |
- va_end(argptr); |
|
| 206 |
- return amt; |
|
| 207 |
- };*/ |
|
| 208 |
- |
|
| 209 |
- /*virtual int fgetc() |
|
| 210 |
- {
|
|
| 211 |
- uint8_t temp; |
|
| 212 |
- |
|
| 213 |
- //need an optimized codepath |
|
| 214 |
- //if(_fread(&temp,1) != 1) |
|
| 215 |
- // return EOF; |
|
| 216 |
- //else return temp; |
|
| 217 |
- uint32_t remain = len-pos; |
|
| 218 |
- if (remain < 1) |
|
| 219 |
- {
|
|
| 220 |
- failbit = true; |
|
| 221 |
- return -1; |
|
| 222 |
- } |
|
| 223 |
- temp = buf()[pos]; |
|
| 224 |
- ++pos; |
|
| 225 |
- return temp; |
|
| 226 |
- }*/ |
|
| 227 |
- |
|
| 228 |
- /*virtual int fputc(int c) |
|
| 229 |
- {
|
|
| 230 |
- uint8_t temp = static_cast<uint8_t>(c); |
|
| 231 |
- //TODO |
|
| 232 |
- //if(fwrite(&temp,1)!=1) return EOF; |
|
| 233 |
- fwrite(&temp, 1); |
|
| 234 |
- |
|
| 235 |
- return 0; |
|
| 236 |
- }*/ |
|
| 237 |
- |
|
| 238 | 123 |
virtual size_t _fread(void *ptr, size_t bytes); |
| 239 | 124 |
|
| 240 |
- //removing these return values for now so we can find any code that might be using them and make sure |
|
| 241 |
- //they handle the return values correctly |
|
| 242 |
- |
|
| 243 |
- /*virtual void fwrite(const void *ptr, size_t bytes) |
|
| 244 |
- {
|
|
| 245 |
- reserve(pos + bytes); |
|
| 246 |
- memcpy(buf() + pos, ptr, bytes); |
|
| 247 |
- pos += bytes; |
|
| 248 |
- len = std::max(pos, len); |
|
| 249 |
- }*/ |
|
| 250 |
- |
|
| 251 | 125 |
virtual int fseek(int offset, int origin) |
| 252 | 126 |
{
|
| 253 |
- //work differently for read-only...? |
|
| 127 |
+ // work differently for read-only...? |
|
| 254 | 128 |
switch (origin) |
| 255 | 129 |
{
|
| 256 | 130 |
case SEEK_SET: |
| 257 |
- pos = offset; |
|
| 131 |
+ this->pos = offset; |
|
| 258 | 132 |
break; |
| 259 | 133 |
case SEEK_CUR: |
| 260 |
- pos += offset; |
|
| 134 |
+ this->pos += offset; |
|
| 261 | 135 |
break; |
| 262 | 136 |
case SEEK_END: |
| 263 |
- pos = size() + offset; |
|
| 137 |
+ this->pos = this->size() + offset; |
|
| 264 | 138 |
break; |
| 265 | 139 |
default: |
| 266 | 140 |
assert(false); |
| 267 | 141 |
} |
| 268 |
- reserve(pos); |
|
| 142 |
+ this->reserve(pos); |
|
| 269 | 143 |
return 0; |
| 270 | 144 |
} |
| 271 | 145 |
|
| 272 | 146 |
virtual size_t ftell() |
| 273 | 147 |
{
|
| 274 |
- return pos; |
|
| 148 |
+ return this->pos; |
|
| 275 | 149 |
} |
| 276 | 150 |
|
| 277 |
- /*void trim() |
|
| 151 |
+ virtual size_t size() |
|
| 278 | 152 |
{
|
| 279 |
- vec->resize(len); |
|
| 280 |
- }*/ |
|
| 281 |
- |
|
| 282 |
- virtual size_t size() { return len; }
|
|
| 153 |
+ return this->len; |
|
| 154 |
+ } |
|
| 283 | 155 |
}; |
| 284 | 156 |
|
| 285 | 157 |
class EMUFILE_FILE : public EMUFILE |
| ... | ... |
@@ -288,98 +160,53 @@ protected: |
| 288 | 160 |
FILE *fp; |
| 289 | 161 |
std::string fname; |
| 290 | 162 |
char mode[16]; |
| 291 |
- |
|
| 292 | 163 |
private: |
| 293 | 164 |
void open(const char *fn, const char *Mode) |
| 294 | 165 |
{
|
| 295 |
- fp = fopen(fn, Mode); |
|
| 296 |
- if (!fp) |
|
| 297 |
- failbit = true; |
|
| 166 |
+ this->fp = fopen(fn, Mode); |
|
| 167 |
+ if (!this->fp) |
|
| 168 |
+ this->failbit = true; |
|
| 298 | 169 |
this->fname = fn; |
| 299 | 170 |
strcpy(this->mode, Mode); |
| 300 | 171 |
} |
| 301 |
- |
|
| 302 | 172 |
public: |
| 303 |
- EMUFILE_FILE(const std::string &fn, const char *Mode) { open(fn.c_str(), Mode); }
|
|
| 304 |
- EMUFILE_FILE(const char *fn, const char *Mode) { open(fn, Mode); }
|
|
| 305 |
- |
|
| 306 |
- virtual ~EMUFILE_FILE() |
|
| 173 |
+ EMUFILE_FILE(const std::string &fn, const char *Mode) |
|
| 307 | 174 |
{
|
| 308 |
- if (fp) |
|
| 309 |
- fclose(fp); |
|
| 175 |
+ this->open(fn.c_str(), Mode); |
|
| 310 | 176 |
} |
| 311 | 177 |
|
| 312 |
- /*virtual FILE *get_fp() |
|
| 313 |
- {
|
|
| 314 |
- return fp; |
|
| 315 |
- }*/ |
|
| 316 |
- |
|
| 317 |
- //virtual EMUFILE *memwrap(); |
|
| 318 |
- |
|
| 319 |
- //bool is_open() { return fp != NULL; }
|
|
| 320 |
- |
|
| 321 |
- //virtual void truncate(int32_t length); |
|
| 322 |
- |
|
| 323 |
- /*virtual int fprintf(const char *format, ...) |
|
| 324 |
- {
|
|
| 325 |
- va_list argptr; |
|
| 326 |
- va_start(argptr, format); |
|
| 327 |
- int ret = ::vfprintf(fp, format, argptr); |
|
| 328 |
- va_end(argptr); |
|
| 329 |
- return ret; |
|
| 330 |
- };*/ |
|
| 331 |
- |
|
| 332 |
- /*virtual int fgetc() |
|
| 333 |
- {
|
|
| 334 |
- return ::fgetc(fp); |
|
| 335 |
- }*/ |
|
| 336 |
- |
|
| 337 |
- /*virtual int fputc(int c) |
|
| 178 |
+ virtual ~EMUFILE_FILE() |
|
| 338 | 179 |
{
|
| 339 |
- return ::fputc(c, fp); |
|
| 340 |
- }*/ |
|
| 180 |
+ if (this->fp) |
|
| 181 |
+ fclose(this->fp); |
|
| 182 |
+ } |
|
| 341 | 183 |
|
| 342 | 184 |
virtual size_t _fread(void *ptr, size_t bytes) |
| 343 | 185 |
{
|
| 344 |
- size_t ret = ::fread(ptr, 1, bytes, fp); |
|
| 186 |
+ size_t ret = ::fread(ptr, 1, bytes, this->fp); |
|
| 345 | 187 |
if (ret < bytes) |
| 346 |
- failbit = true; |
|
| 188 |
+ this->failbit = true; |
|
| 347 | 189 |
return ret; |
| 348 | 190 |
} |
| 349 | 191 |
|
| 350 |
- //removing these return values for now so we can find any code that might be using them and make sure |
|
| 351 |
- //they handle the return values correctly |
|
| 352 |
- |
|
| 353 |
- /*virtual void fwrite(const void *ptr, size_t bytes) |
|
| 354 |
- {
|
|
| 355 |
- size_t ret = ::fwrite(ptr, 1, bytes, fp); |
|
| 356 |
- if (ret < bytes) |
|
| 357 |
- failbit = true; |
|
| 358 |
- }*/ |
|
| 359 |
- |
|
| 360 | 192 |
virtual int fseek(int offset, int origin) |
| 361 | 193 |
{
|
| 362 |
- return ::fseek(fp, offset, origin); |
|
| 194 |
+ return ::fseek(this->fp, offset, origin); |
|
| 363 | 195 |
} |
| 364 | 196 |
|
| 365 | 197 |
virtual size_t ftell() |
| 366 | 198 |
{
|
| 367 |
- return static_cast<uint32_t>(::ftell(fp)); |
|
| 199 |
+ return static_cast<size_t>(::ftell(this->fp)); |
|
| 368 | 200 |
} |
| 369 | 201 |
|
| 370 | 202 |
virtual size_t size() |
| 371 | 203 |
{
|
| 372 |
- int oldpos = ftell(); |
|
| 373 |
- fseek(0, SEEK_END); |
|
| 374 |
- int len = ftell(); |
|
| 375 |
- fseek(oldpos, SEEK_SET); |
|
| 204 |
+ int oldpos = this->ftell(); |
|
| 205 |
+ this->fseek(0, SEEK_END); |
|
| 206 |
+ int len = this->ftell(); |
|
| 207 |
+ this->fseek(oldpos, SEEK_SET); |
|
| 376 | 208 |
return len; |
| 377 | 209 |
} |
| 378 |
- |
|
| 379 |
- /*virtual void fflush() |
|
| 380 |
- {
|
|
| 381 |
- ::fflush(fp); |
|
| 382 |
- }*/ |
|
| 383 | 210 |
}; |
| 384 | 211 |
|
| 385 | 212 |
#endif |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,385 @@ |
| 1 |
+/* |
|
| 2 |
+The MIT License |
|
| 3 |
+ |
|
| 4 |
+Copyright (C) 2009-2012 DeSmuME team |
|
| 5 |
+ |
|
| 6 |
+Permission is hereby granted, free of charge, to any person obtaining a copy |
|
| 7 |
+of this software and associated documentation files (the "Software"), to deal |
|
| 8 |
+in the Software without restriction, including without limitation the rights |
|
| 9 |
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
| 10 |
+copies of the Software, and to permit persons to whom the Software is |
|
| 11 |
+furnished to do so, subject to the following conditions: |
|
| 12 |
+ |
|
| 13 |
+The above copyright notice and this permission notice shall be included in |
|
| 14 |
+all copies or substantial portions of the Software. |
|
| 15 |
+ |
|
| 16 |
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
| 17 |
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
| 18 |
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
| 19 |
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
| 20 |
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
| 21 |
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
| 22 |
+THE SOFTWARE. |
|
| 23 |
+*/ |
|
| 24 |
+ |
|
| 25 |
+//don't use emufile for files bigger than 2GB! you have been warned! some day this will be fixed. |
|
| 26 |
+ |
|
| 27 |
+#ifndef EMUFILE_H |
|
| 28 |
+#define EMUFILE_H |
|
| 29 |
+ |
|
| 30 |
+#include <vector> |
|
| 31 |
+#include <algorithm> |
|
| 32 |
+#include <string> |
|
| 33 |
+#include <cassert> |
|
| 34 |
+#include <cstdio> |
|
| 35 |
+#include <cstring> |
|
| 36 |
+#include <cstdarg> |
|
| 37 |
+#include <cmath> |
|
| 38 |
+ |
|
| 39 |
+#include "types.h" |
|
| 40 |
+ |
|
| 41 |
+#if defined(_MSC_VER) || defined(__MINGW32__) |
|
| 42 |
+/*#define WIN32_LEAN_AND_MEAN |
|
| 43 |
+#define NOMINMAX |
|
| 44 |
+#include <io.h>*/ |
|
| 45 |
+#include "windowsh_wrapper.h" |
|
| 46 |
+#include <io.h> |
|
| 47 |
+#else |
|
| 48 |
+#include <unistd.h> |
|
| 49 |
+#endif |
|
| 50 |
+ |
|
| 51 |
+class EMUFILE |
|
| 52 |
+{
|
|
| 53 |
+protected: |
|
| 54 |
+ bool failbit; |
|
| 55 |
+ |
|
| 56 |
+public: |
|
| 57 |
+ EMUFILE() : failbit(false) |
|
| 58 |
+ {
|
|
| 59 |
+ } |
|
| 60 |
+ |
|
| 61 |
+ //returns a new EMUFILE which is guranteed to be in memory. the EMUFILE you call this on may be deleted. use the returned EMUFILE in its place |
|
| 62 |
+ //virtual EMUFILE* memwrap() = 0; |
|
| 63 |
+ |
|
| 64 |
+ virtual ~EMUFILE() {}
|
|
| 65 |
+ |
|
| 66 |
+ //static bool readAllBytes(std::vector<uint8_t> *buf, const std::string &fname); |
|
| 67 |
+ |
|
| 68 |
+ bool fail(bool unset = false) |
|
| 69 |
+ {
|
|
| 70 |
+ bool ret = failbit; |
|
| 71 |
+ if (unset) |
|
| 72 |
+ unfail(); |
|
| 73 |
+ return ret; |
|
| 74 |
+ } |
|
| 75 |
+ void unfail() { failbit = false; }
|
|
| 76 |
+ |
|
| 77 |
+ //bool eof() { return size() == ftell(); }
|
|
| 78 |
+ |
|
| 79 |
+ size_t fread(void *ptr, size_t bytes) |
|
| 80 |
+ {
|
|
| 81 |
+ return _fread(ptr, bytes); |
|
| 82 |
+ } |
|
| 83 |
+ |
|
| 84 |
+ //void unget() { fseek(-1, SEEK_CUR); }
|
|
| 85 |
+ |
|
| 86 |
+ //virtuals |
|
| 87 |
+public: |
|
| 88 |
+ //virtual FILE *get_fp() = 0; |
|
| 89 |
+ |
|
| 90 |
+ //virtual int fprintf(const char *format, ...) = 0; |
|
| 91 |
+ |
|
| 92 |
+ //virtual int fgetc() = 0; |
|
| 93 |
+ //virtual int fputc(int c) = 0; |
|
| 94 |
+ |
|
| 95 |
+ virtual size_t _fread(void *ptr, size_t bytes) = 0; |
|
| 96 |
+ |
|
| 97 |
+ //removing these return values for now so we can find any code that might be using them and make sure |
|
| 98 |
+ //they handle the return values correctly |
|
| 99 |
+ |
|
| 100 |
+ //virtual void fwrite(const void *ptr, size_t bytes) = 0; |
|
| 101 |
+ |
|
| 102 |
+ /*void write64le(uint64_t *val); |
|
| 103 |
+ void write64le(uint64_t val); |
|
| 104 |
+ size_t read64le(uint64_t *val); |
|
| 105 |
+ uint64_t read64le(); |
|
| 106 |
+ void write32le(uint32_t *val); |
|
| 107 |
+ void write32le(int32_t *val) { write32le((uint32_t *)val); }
|
|
| 108 |
+ void write32le(uint32_t val); |
|
| 109 |
+ size_t read32le(uint32_t *val); |
|
| 110 |
+ size_t read32le(int32_t *val); |
|
| 111 |
+ uint32_t read32le(); |
|
| 112 |
+ void write16le(uint16_t *val); |
|
| 113 |
+ void write16le(int16_t *val) { write16le((uint16_t *)val); }
|
|
| 114 |
+ void write16le(uint16_t val); |
|
| 115 |
+ size_t read16le(int16_t *Bufo); |
|
| 116 |
+ size_t read16le(uint16_t *val); |
|
| 117 |
+ uint16_t read16le(); |
|
| 118 |
+ void write8le(uint8_t *val); |
|
| 119 |
+ void write8le(uint8_t val); |
|
| 120 |
+ size_t read8le(uint8_t *val); |
|
| 121 |
+ uint8_t read8le(); |
|
| 122 |
+ void writedouble(double* val); |
|
| 123 |
+ void writedouble(double val); |
|
| 124 |
+ double readdouble(); |
|
| 125 |
+ size_t readdouble(double* val);*/ |
|
| 126 |
+ |
|
| 127 |
+ virtual int fseek(int offset, int origin) = 0; |
|
| 128 |
+ |
|
| 129 |
+ virtual size_t ftell() = 0; |
|
| 130 |
+ virtual size_t size() = 0; |
|
| 131 |
+ |
|
| 132 |
+ //virtual void truncate(int32_t length) = 0; |
|
| 133 |
+}; |
|
| 134 |
+ |
|
| 135 |
+//todo - handle read-only specially? |
|
| 136 |
+class EMUFILE_MEMORY : public EMUFILE |
|
| 137 |
+{
|
|
| 138 |
+protected: |
|
| 139 |
+ std::vector<uint8_t> *vec; |
|
| 140 |
+ bool ownvec; |
|
| 141 |
+ int32_t pos, len; |
|
| 142 |
+ |
|
| 143 |
+ void reserve(uint32_t amt) |
|
| 144 |
+ {
|
|
| 145 |
+ if (vec->size() < amt) |
|
| 146 |
+ vec->resize(amt); |
|
| 147 |
+ } |
|
| 148 |
+ |
|
| 149 |
+public: |
|
| 150 |
+ EMUFILE_MEMORY(std::vector<uint8_t> *underlying) : vec(underlying), ownvec(false), pos(0), len(static_cast<int32_t>(underlying->size())) { }
|
|
| 151 |
+ EMUFILE_MEMORY(uint32_t preallocate) : vec(new std::vector<uint8_t>()), ownvec(true), pos(0), len(0) |
|
| 152 |
+ {
|
|
| 153 |
+ vec->resize(preallocate); |
|
| 154 |
+ len = preallocate; |
|
| 155 |
+ } |
|
| 156 |
+ EMUFILE_MEMORY() : vec(new std::vector<uint8_t>()), ownvec(true), pos(0), len(0) { vec->reserve(1024); }
|
|
| 157 |
+ EMUFILE_MEMORY(void *Buf, int32_t Size) : vec(new std::vector<uint8_t>()), ownvec(true), pos(0), len(Size) |
|
| 158 |
+ {
|
|
| 159 |
+ vec->resize(Size); |
|
| 160 |
+ if (Size) |
|
| 161 |
+ memcpy(&(*vec)[0], Buf, Size); |
|
| 162 |
+ } |
|
| 163 |
+ |
|
| 164 |
+ ~EMUFILE_MEMORY() |
|
| 165 |
+ {
|
|
| 166 |
+ if (ownvec) |
|
| 167 |
+ delete vec; |
|
| 168 |
+ } |
|
| 169 |
+ |
|
| 170 |
+ //virtual EMUFILE *memwrap(); |
|
| 171 |
+ |
|
| 172 |
+ /*virtual void truncate(int32_t length) |
|
| 173 |
+ {
|
|
| 174 |
+ vec->resize(length); |
|
| 175 |
+ len = length; |
|
| 176 |
+ if (pos>length) |
|
| 177 |
+ pos = length; |
|
| 178 |
+ }*/ |
|
| 179 |
+ |
|
| 180 |
+ uint8_t *buf() {
|
|
| 181 |
+ if(size()==0) reserve(1); |
|
| 182 |
+ return &(*vec)[0]; |
|
| 183 |
+ } |
|
| 184 |
+ |
|
| 185 |
+ //std::vector<uint8_t> *get_vec() { return vec; };
|
|
| 186 |
+ |
|
| 187 |
+ //virtual FILE *get_fp() { return NULL; }
|
|
| 188 |
+ |
|
| 189 |
+ /*virtual int fprintf(const char *format, ...) |
|
| 190 |
+ {
|
|
| 191 |
+ va_list argptr; |
|
| 192 |
+ va_start(argptr, format); |
|
| 193 |
+ |
|
| 194 |
+ //we dont generate straight into the buffer because it will null terminate (one more byte than we want) |
|
| 195 |
+ int amt = vsnprintf(0, 0, format,argptr); |
|
| 196 |
+ char *tempbuf = new char[amt + 1]; |
|
| 197 |
+ |
|
| 198 |
+ va_end(argptr); |
|
| 199 |
+ va_start(argptr, format); |
|
| 200 |
+ vsprintf(tempbuf, format, argptr); |
|
| 201 |
+ |
|
| 202 |
+ fwrite(tempbuf, amt); |
|
| 203 |
+ delete [] tempbuf; |
|
| 204 |
+ |
|
| 205 |
+ va_end(argptr); |
|
| 206 |
+ return amt; |
|
| 207 |
+ };*/ |
|
| 208 |
+ |
|
| 209 |
+ /*virtual int fgetc() |
|
| 210 |
+ {
|
|
| 211 |
+ uint8_t temp; |
|
| 212 |
+ |
|
| 213 |
+ //need an optimized codepath |
|
| 214 |
+ //if(_fread(&temp,1) != 1) |
|
| 215 |
+ // return EOF; |
|
| 216 |
+ //else return temp; |
|
| 217 |
+ uint32_t remain = len-pos; |
|
| 218 |
+ if (remain < 1) |
|
| 219 |
+ {
|
|
| 220 |
+ failbit = true; |
|
| 221 |
+ return -1; |
|
| 222 |
+ } |
|
| 223 |
+ temp = buf()[pos]; |
|
| 224 |
+ ++pos; |
|
| 225 |
+ return temp; |
|
| 226 |
+ }*/ |
|
| 227 |
+ |
|
| 228 |
+ /*virtual int fputc(int c) |
|
| 229 |
+ {
|
|
| 230 |
+ uint8_t temp = static_cast<uint8_t>(c); |
|
| 231 |
+ //TODO |
|
| 232 |
+ //if(fwrite(&temp,1)!=1) return EOF; |
|
| 233 |
+ fwrite(&temp, 1); |
|
| 234 |
+ |
|
| 235 |
+ return 0; |
|
| 236 |
+ }*/ |
|
| 237 |
+ |
|
| 238 |
+ virtual size_t _fread(void *ptr, size_t bytes); |
|
| 239 |
+ |
|
| 240 |
+ //removing these return values for now so we can find any code that might be using them and make sure |
|
| 241 |
+ //they handle the return values correctly |
|
| 242 |
+ |
|
| 243 |
+ /*virtual void fwrite(const void *ptr, size_t bytes) |
|
| 244 |
+ {
|
|
| 245 |
+ reserve(pos + bytes); |
|
| 246 |
+ memcpy(buf() + pos, ptr, bytes); |
|
| 247 |
+ pos += bytes; |
|
| 248 |
+ len = std::max(pos, len); |
|
| 249 |
+ }*/ |
|
| 250 |
+ |
|
| 251 |
+ virtual int fseek(int offset, int origin) |
|
| 252 |
+ {
|
|
| 253 |
+ //work differently for read-only...? |
|
| 254 |
+ switch (origin) |
|
| 255 |
+ {
|
|
| 256 |
+ case SEEK_SET: |
|
| 257 |
+ pos = offset; |
|
| 258 |
+ break; |
|
| 259 |
+ case SEEK_CUR: |
|
| 260 |
+ pos += offset; |
|
| 261 |
+ break; |
|
| 262 |
+ case SEEK_END: |
|
| 263 |
+ pos = size() + offset; |
|
| 264 |
+ break; |
|
| 265 |
+ default: |
|
| 266 |
+ assert(false); |
|
| 267 |
+ } |
|
| 268 |
+ reserve(pos); |
|
| 269 |
+ return 0; |
|
| 270 |
+ } |
|
| 271 |
+ |
|
| 272 |
+ virtual size_t ftell() |
|
| 273 |
+ {
|
|
| 274 |
+ return pos; |
|
| 275 |
+ } |
|
| 276 |
+ |
|
| 277 |
+ /*void trim() |
|
| 278 |
+ {
|
|
| 279 |
+ vec->resize(len); |
|
| 280 |
+ }*/ |
|
| 281 |
+ |
|
| 282 |
+ virtual size_t size() { return len; }
|
|
| 283 |
+}; |
|
| 284 |
+ |
|
| 285 |
+class EMUFILE_FILE : public EMUFILE |
|
| 286 |
+{
|
|
| 287 |
+protected: |
|
| 288 |
+ FILE *fp; |
|
| 289 |
+ std::string fname; |
|
| 290 |
+ char mode[16]; |
|
| 291 |
+ |
|
| 292 |
+private: |
|
| 293 |
+ void open(const char *fn, const char *Mode) |
|
| 294 |
+ {
|
|
| 295 |
+ fp = fopen(fn, Mode); |
|
| 296 |
+ if (!fp) |
|
| 297 |
+ failbit = true; |
|
| 298 |
+ this->fname = fn; |
|
| 299 |
+ strcpy(this->mode, Mode); |
|
| 300 |
+ } |
|
| 301 |
+ |
|
| 302 |
+public: |
|
| 303 |
+ EMUFILE_FILE(const std::string &fn, const char *Mode) { open(fn.c_str(), Mode); }
|
|
| 304 |
+ EMUFILE_FILE(const char *fn, const char *Mode) { open(fn, Mode); }
|
|
| 305 |
+ |
|
| 306 |
+ virtual ~EMUFILE_FILE() |
|
| 307 |
+ {
|
|
| 308 |
+ if (fp) |
|
| 309 |
+ fclose(fp); |
|
| 310 |
+ } |
|
| 311 |
+ |
|
| 312 |
+ /*virtual FILE *get_fp() |
|
| 313 |
+ {
|
|
| 314 |
+ return fp; |
|
| 315 |
+ }*/ |
|
| 316 |
+ |
|
| 317 |
+ //virtual EMUFILE *memwrap(); |
|
| 318 |
+ |
|
| 319 |
+ //bool is_open() { return fp != NULL; }
|
|
| 320 |
+ |
|
| 321 |
+ //virtual void truncate(int32_t length); |
|
| 322 |
+ |
|
| 323 |
+ /*virtual int fprintf(const char *format, ...) |
|
| 324 |
+ {
|
|
| 325 |
+ va_list argptr; |
|
| 326 |
+ va_start(argptr, format); |
|
| 327 |
+ int ret = ::vfprintf(fp, format, argptr); |
|
| 328 |
+ va_end(argptr); |
|
| 329 |
+ return ret; |
|
| 330 |
+ };*/ |
|
| 331 |
+ |
|
| 332 |
+ /*virtual int fgetc() |
|
| 333 |
+ {
|
|
| 334 |
+ return ::fgetc(fp); |
|
| 335 |
+ }*/ |
|
| 336 |
+ |
|
| 337 |
+ /*virtual int fputc(int c) |
|
| 338 |
+ {
|
|
| 339 |
+ return ::fputc(c, fp); |
|
| 340 |
+ }*/ |
|
| 341 |
+ |
|
| 342 |
+ virtual size_t _fread(void *ptr, size_t bytes) |
|
| 343 |
+ {
|
|
| 344 |
+ size_t ret = ::fread(ptr, 1, bytes, fp); |
|
| 345 |
+ if (ret < bytes) |
|
| 346 |
+ failbit = true; |
|
| 347 |
+ return ret; |
|
| 348 |
+ } |
|
| 349 |
+ |
|
| 350 |
+ //removing these return values for now so we can find any code that might be using them and make sure |
|
| 351 |
+ //they handle the return values correctly |
|
| 352 |
+ |
|
| 353 |
+ /*virtual void fwrite(const void *ptr, size_t bytes) |
|
| 354 |
+ {
|
|
| 355 |
+ size_t ret = ::fwrite(ptr, 1, bytes, fp); |
|
| 356 |
+ if (ret < bytes) |
|
| 357 |
+ failbit = true; |
|
| 358 |
+ }*/ |
|
| 359 |
+ |
|
| 360 |
+ virtual int fseek(int offset, int origin) |
|
| 361 |
+ {
|
|
| 362 |
+ return ::fseek(fp, offset, origin); |
|
| 363 |
+ } |
|
| 364 |
+ |
|
| 365 |
+ virtual size_t ftell() |
|
| 366 |
+ {
|
|
| 367 |
+ return static_cast<uint32_t>(::ftell(fp)); |
|
| 368 |
+ } |
|
| 369 |
+ |
|
| 370 |
+ virtual size_t size() |
|
| 371 |
+ {
|
|
| 372 |
+ int oldpos = ftell(); |
|
| 373 |
+ fseek(0, SEEK_END); |
|
| 374 |
+ int len = ftell(); |
|
| 375 |
+ fseek(oldpos, SEEK_SET); |
|
| 376 |
+ return len; |
|
| 377 |
+ } |
|
| 378 |
+ |
|
| 379 |
+ /*virtual void fflush() |
|
| 380 |
+ {
|
|
| 381 |
+ ::fflush(fp); |
|
| 382 |
+ }*/ |
|
| 383 |
+}; |
|
| 384 |
+ |
|
| 385 |
+#endif |