diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 9ec7461..5406d28 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -58,35 +58,41 @@ void File::close(){ } ErrorOr File::readByte(){ - this->fileStream.seekg(this->cursorPosition); + uint8_t* nextPointer = new uint8_t; + uint8_t nextByte; + bool failure = false; + try { + this->fileStream.seekg(this->cursorPosition); - char* byte = new char; - try{ - this->fileStream.read(byte, 1); - }catch(std::exception& e){ - return ErrorOr(true, ErrorCodes::FILE_READ_FAILED); + //FIXME: check that a byte is available for read + this->fileStream.read(reinterpret_cast(nextPointer), 1); + nextByte = *nextPointer; + + this->cursorPosition++; + + } catch (std::exception& e) { + failure = true; } - - this->cursorPosition += 1; - return ErrorOr((uint8_t) *byte); + delete nextPointer; + return failure? ErrorOr(true, ErrorCodes::FILE_READ_FAILED) : ErrorOr(nextByte); } ErrorOr> File::read(uint64_t bytes){ - this->fileStream.seekg(this->cursorPosition); - - char* buffer = new char[bytes]; + uint8_t* buffer = new uint8_t[bytes]; std::vector data; - try{ - this->fileStream.read(buffer, bytes); - for(uint64_t i=0; i>(true, ErrorCodes::FILE_READ_FAILED); + bool failure = false; + try { + this->fileStream.seekg(this->cursorPosition); + + //FIXME: check that enough data is available to read + this->fileStream.read(reinterpret_cast(buffer), bytes); + data = std::vector(buffer, buffer+bytes); + this->cursorPosition += bytes; + } catch (std::exception& e) { + failure = true; } - this->cursorPosition += bytes; - return ErrorOr>(data); + delete[] buffer; + return failure? ErrorOr>(true, ErrorCodes::FILE_READ_FAILED) : ErrorOr>(data); } ErrorOr File::readString(uint64_t bytes){