2
Copyright (C) 2009-2010 DeSmuME team
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
#include "utils/xstring.h"
27
bool EMUFILE::readAllBytes(std::vector<u8>* dstbuf, const std::string& fname)
29
EMUFILE_FILE file(fname.c_str(),"rb");
30
if(file.fail()) return false;
31
int size = file.size();
33
file.fread(&dstbuf->at(0),size);
37
size_t EMUFILE_MEMORY::_fread(const void *ptr, size_t bytes){
39
u32 todo = std::min<u32>(remain,(u32)bytes);
49
for(size_t i=0;i<todo;i++)
54
memcpy((void*)ptr,buf()+pos,todo);
62
void EMUFILE_FILE::open(const char* fname, const char* mode)
64
fp = fopen(fname,mode);
68
std::wstring wfname = mbstowcs((std::string)fname);
69
std::wstring wfmode = mbstowcs((std::string)mode);
70
fp = _wfopen(wfname.c_str(),wfmode.c_str());
76
strcpy(this->mode,mode);
80
void EMUFILE_FILE::truncate(s32 length)
84
_chsize(_fileno(fp),length);
86
ftruncate(fileno(fp),length);
90
open(fname.c_str(),mode);
94
EMUFILE* EMUFILE_FILE::memwrap()
96
EMUFILE_MEMORY* mem = new EMUFILE_MEMORY(size());
97
if(size()==0) return mem;
98
fread(mem->buf(),size());
102
EMUFILE* EMUFILE_MEMORY::memwrap()
107
void EMUFILE::write64le(u64* val)
112
void EMUFILE::write64le(u64 val)
132
size_t EMUFILE::read64le(u64 *Bufo)
135
if(fread((char*)&buf,8) != 8)
140
*Bufo = LE_TO_LOCAL_64(buf);
145
u64 EMUFILE::read64le()
152
void EMUFILE::write32le(u32* val)
157
void EMUFILE::write32le(u32 val)
171
size_t EMUFILE::read32le(s32* Bufo) { return read32le((u32*)Bufo); }
173
size_t EMUFILE::read32le(u32* Bufo)
181
*(u32*)Bufo=((buf&0xFF)<<24)|((buf&0xFF00)<<8)|((buf&0xFF0000)>>8)|((buf&0xFF000000)>>24);
186
u32 EMUFILE::read32le()
193
void EMUFILE::write16le(u16* val)
198
void EMUFILE::write16le(u16 val)
210
size_t EMUFILE::read16le(s16* Bufo) { return read16le((u16*)Bufo); }
212
size_t EMUFILE::read16le(u16* Bufo)
220
*Bufo = LE_TO_LOCAL_16(buf);
225
u16 EMUFILE::read16le()
232
void EMUFILE::write8le(u8* val)
238
void EMUFILE::write8le(u8 val)
243
size_t EMUFILE::read8le(u8* val)
248
u8 EMUFILE::read8le()
255
void EMUFILE::writedouble(double* val)
257
write64le(double_to_u64(*val));
259
void EMUFILE::writedouble(double val)
261
write64le(double_to_u64(val));
264
double EMUFILE::readdouble()
271
size_t EMUFILE::readdouble(double* val)
274
size_t ret = read64le(&temp);
275
*val = u64_to_double(temp);
b'\\ No newline at end of file'