diff --git a/source/build/include/cache1d.h b/source/build/include/cache1d.h index 80057205d..45f6f7fb3 100644 --- a/source/build/include/cache1d.h +++ b/source/build/include/cache1d.h @@ -10,6 +10,7 @@ #define cache1d_h_ #include "compat.h" +#include "files.h" #include "vfs.h" @@ -151,5 +152,55 @@ extern int32_t lz4CompressionLevel; int32_t kdfread_LZ4(void *buffer, int dasizeof, int count, buildvfs_kfd fil); void dfwrite_LZ4(const void *buffer, int dasizeof, int count, buildvfs_FILE fil); +class KFileReaderInterface : public FileReaderInterface +{ + buildvfs_kfd khandle = buildvfs_kfd_invalid; +public: + + KFileReaderInterface(int handle) + { + khandle = handle; + Length = 0; + if (khandle != buildvfs_kfd_invalid) + { + klseek(khandle, 0, SEEK_END); + Length = ktell(khandle); + klseek(khandle, 0, SEEK_SET); + } + } + ~KFileReaderInterface() + { + if (khandle != buildvfs_kfd_invalid) + { + kclose(khandle); + } + } + virtual long Tell() const + { + return ktell(khandle); + } + virtual long Seek(long offset, int origin) + { + return klseek(khandle, offset, origin); + } + virtual long Read(void* buffer, long len) + { + return kread(khandle, buffer, (int32_t)len); + } + virtual char* Gets(char* strbuf, int len) + { + // Not supported by the underlying system, so we do not need it anyway. + return nullptr; + } + +}; + +inline FileReader kopenFileReader(const char* name, int where) +{ + int handle = where == 0 ? kopen4loadfrommod(name, 0) : kopen4load(name, where); + KFileReaderInterface *fri = handle == buildvfs_kfd_invalid? nullptr : new KFileReaderInterface(handle); + return FileReader(fri); +} + #endif // cache1d_h_ diff --git a/source/build/src/scriptfile.cpp b/source/build/src/scriptfile.cpp index 99c33d542..aa9b0245d 100644 --- a/source/build/src/scriptfile.cpp +++ b/source/build/src/scriptfile.cpp @@ -301,16 +301,15 @@ void scriptfile_preparse(scriptfile *sf, char *tx, int32_t flen) scriptfile *scriptfile_fromfile(const char *fn) { - buildvfs_kfd fp = kopen4load(fn, 0); - if (fp == buildvfs_kfd_invalid) return nullptr; + auto fr = kopenFileReader(fn, 0); + if (!fr.isOpen()) return nullptr; - uint32_t flen = kfilelength(fp); + uint32_t flen = fr.GetLength(); char * tx = (char *)Xmalloc(flen + 2); scriptfile *sf = (scriptfile *)Xmalloc(sizeof(scriptfile)); - kread(fp, tx, flen); - kclose(fp); + fr.Read(tx, flen); tx[flen] = tx[flen+1] = 0; diff --git a/source/common/utility/files.h b/source/common/utility/files.h index ccb4a1ba9..2dcd7da5a 100644 --- a/source/common/utility/files.h +++ b/source/common/utility/files.h @@ -123,12 +123,13 @@ class FileReader FileReader(const FileReader &r) = delete; FileReader &operator=(const FileReader &r) = delete; +public: + explicit FileReader(FileReaderInterface *r) { mReader = r; } -public: enum ESeek { SeekSet = SEEK_SET,