Implement host file I/O

Host file I/O API to read files from disk
This commit is contained in:
Nathan Fulton 2017-04-29 22:58:56 -04:00
parent e02296b6ac
commit 2ef0ab5ae8
3 changed files with 45 additions and 17 deletions

View File

@ -8829,10 +8829,16 @@ void HostGetSilkRect(int irc, Rect *prc) secHost;
const word kfOfRead = 0x0001; // same as "rb"
const word kfOfWrite = 0x0002; // same as "wb"
#define kfSeekSet 0
#define kfSeekCur 1
#define kfSeekEnd 2
FileHandle HostOpenFile(const char *pszFilename, word wf) secHost;
void HostCloseFile(FileHandle hf) secHost;
dword HostWriteFile(FileHandle hf, void *pv, dword cb) secHost;
dword HostReadFile(FileHandle hf, void *pv, dword cb) secHost;
dword HostWriteFile(void *pv, dword c, dword cb, FileHandle hf) secHost;
dword HostReadFile(void *pv, dword c, dword cb, FileHandle hf) secHost;
dword HostSeekFile(FileHandle hf, int off, int nOrigin) secHost;
dword HostTellFile(FileHandle hf) secHost;
// Save game

View File

@ -23,28 +23,28 @@ bool MemPdbReader::Open(char *pszFn)
// Attempt to open
Assert(m_pb == NULL);
FILE *pfil = fopen(pszFn, "rb");
if (pfil == NULL) {
FileHandle hf = HostOpenFile(pszFn, kfOfRead);
if (hf == NULL) {
Trace("fopen(\"%s\", \"rb\"); failed", pszFn);
return false;
}
// Read in the entire thing
fseek(pfil, 0, SEEK_END);
m_cb = (dword)ftell(pfil);
fseek(pfil, 0, SEEK_SET);
HostSeekFile(hf, 0, kfSeekEnd);
m_cb = HostTellFile(hf);
HostSeekFile(hf, 0, kfSeekSet);
m_pb = new byte[m_cb];
if (m_pb == NULL) {
fclose(pfil);
HostCloseFile(hf);
return false;
}
if (fread(m_pb, m_cb, 1, pfil) != 1) {
fclose(pfil);
if (HostReadFile(m_pb, m_cb, 1, hf) != 1) {
HostCloseFile(hf);
return false;
}
fclose(pfil);
HostCloseFile(hf);
// Alloc cache handle array

View File

@ -700,22 +700,44 @@ FileHandle HostOpenFile(const char *pszFilename, word wf)
else if (wf == (kfOfRead | kfOfWrite))
pszMode = "rb+";
return (FileHandle)fopen((char *)pszFilename, pszMode);
return (FileHandle)SDL_RWFromFile(pszFilename, pszMode);
}
void HostCloseFile(FileHandle hf)
{
fclose((FILE *)hf);
SDL_RWclose((SDL_RWops *)hf);
}
dword HostWriteFile(FileHandle hf, void *pv, dword cb)
dword HostWriteFile(void *pv, dword c, dword cb, FileHandle hf)
{
return fwrite(pv, 1, cb, (FILE *)hf);
// SDL_RWwrite() returns the number of objects written,
// which will be less than cb on error
return (dword)SDL_RWwrite((SDL_RWops *)hf, pv, c, cb);
}
dword HostReadFile(FileHandle hf, void *pv, dword cb)
dword HostReadFile(void *pv, dword c, dword cb, FileHandle hf)
{
return fread(pv, 1, cb, (FILE *)hf);
// SDLRWread() returns the number of objects read,
// or 0 at error or end of file
return (dword)SDL_RWread((SDL_RWops *)hf, pv, c, cb);
}
dword HostSeekFile(FileHandle hf, int off, int nOrigin)
{
// SDL_RWseek() returns the final offset in the data
// stream after the seek or -1 on error
return (dword)SDL_RWseek((SDL_RWops *)hf, off, nOrigin);
}
dword HostTellFile(FileHandle hf)
{
// SDL_RWtell() returns the current offset in the stream,
// or -1 if the information can not be determined
return (dword)SDL_RWtell((SDL_RWops *)hf);
}
void HostSleep(dword ct)