Files
PkmnLib/src/ScriptResolving/AngelScript/ByteCodeHandling/FileByteCodeStream.hpp
2020-05-02 11:13:04 +02:00

33 lines
891 B
C++

#ifndef PKMNLIB_FILEBYTECODESTREAM_HPP
#define PKMNLIB_FILEBYTECODESTREAM_HPP
#include <angelscript.h>
#include <cstdio>
#include "IPkmnBinaryStream.hpp"
class FileByteCodeStream : public IPkmnBinaryStream {
private:
FILE* _file;
size_t _readPosition = 0;
public:
explicit FileByteCodeStream(FILE* file, size_t bound) : IPkmnBinaryStream(bound), _file(file) {}
int Write(const void* ptr, asUINT size) override {
if (size == 0)
return 0;
return fwrite(ptr, size, 1, _file);
}
int Read(void* ptr, asUINT size) override {
if (size == 0)
return 0;
if (_readPosition + size >= _angelScriptBound) {
size = _angelScriptBound - _readPosition;
}
auto diff = fread(ptr, size, 1, _file);
_readPosition += diff;
return diff;
}
};
#endif // PKMNLIB_FILEBYTECODESTREAM_HPP