forked from HarbourMasters/ZAPDTR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into merge-upstream
- Loading branch information
Showing
15 changed files
with
1,259 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
#include "BinaryReader.h" | ||
#include <cmath> | ||
#include <stdexcept> | ||
#include "Stream.h" | ||
|
||
BinaryReader::BinaryReader(Stream* nStream) | ||
{ | ||
stream.reset(nStream); | ||
} | ||
|
||
BinaryReader::BinaryReader(std::shared_ptr<Stream> nStream) | ||
{ | ||
stream = nStream; | ||
} | ||
|
||
void BinaryReader::Close() | ||
{ | ||
stream->Close(); | ||
} | ||
|
||
void BinaryReader::SetEndianness(Endianness endianness) | ||
{ | ||
this->endianness = endianness; | ||
} | ||
|
||
Endianness BinaryReader::GetEndianness() const | ||
{ | ||
return endianness; | ||
} | ||
|
||
void BinaryReader::Seek(uint32_t offset, SeekOffsetType seekType) | ||
{ | ||
stream->Seek(offset, seekType); | ||
} | ||
|
||
uint32_t BinaryReader::GetBaseAddress() | ||
{ | ||
return stream->GetBaseAddress(); | ||
} | ||
|
||
void BinaryReader::Read(int32_t length) | ||
{ | ||
stream->Read(length); | ||
} | ||
|
||
void BinaryReader::Read(char* buffer, int32_t length) | ||
{ | ||
stream->Read(buffer, length); | ||
} | ||
|
||
char BinaryReader::ReadChar() | ||
{ | ||
return (char)stream->ReadByte(); | ||
} | ||
|
||
int8_t BinaryReader::ReadByte() | ||
{ | ||
return stream->ReadByte(); | ||
} | ||
|
||
uint8_t BinaryReader::ReadUByte() | ||
{ | ||
return (uint8_t)stream->ReadByte(); | ||
} | ||
|
||
int16_t BinaryReader::ReadInt16() | ||
{ | ||
int16_t result = 0; | ||
|
||
stream->Read((char*)&result, sizeof(int16_t)); | ||
|
||
if (endianness != Endianness::Native) | ||
result = BSWAP16(result); | ||
|
||
return result; | ||
} | ||
|
||
int32_t BinaryReader::ReadInt32() | ||
{ | ||
int32_t result = 0; | ||
|
||
stream->Read((char*)&result, sizeof(int32_t)); | ||
|
||
if (endianness != Endianness::Native) | ||
result = BSWAP32(result); | ||
|
||
return result; | ||
} | ||
|
||
uint16_t BinaryReader::ReadUInt16() | ||
{ | ||
uint16_t result = 0; | ||
|
||
stream->Read((char*)&result, sizeof(uint16_t)); | ||
|
||
if (endianness != Endianness::Native) | ||
result = BSWAP16(result); | ||
|
||
return result; | ||
} | ||
|
||
uint32_t BinaryReader::ReadUInt32() | ||
{ | ||
uint32_t result = 0; | ||
|
||
stream->Read((char*)&result, sizeof(uint32_t)); | ||
|
||
if (endianness != Endianness::Native) | ||
result = BSWAP32(result); | ||
|
||
return result; | ||
} | ||
|
||
uint64_t BinaryReader::ReadUInt64() | ||
{ | ||
uint64_t result = 0; | ||
|
||
stream->Read((char*)&result, sizeof(uint64_t)); | ||
|
||
if (endianness != Endianness::Native) | ||
result = BSWAP64(result); | ||
|
||
return result; | ||
} | ||
|
||
float BinaryReader::ReadSingle() | ||
{ | ||
float result = NAN; | ||
|
||
stream->Read((char*)&result, sizeof(float)); | ||
|
||
if (endianness != Endianness::Native) | ||
{ | ||
float tmp; | ||
char* dst = (char*)&tmp; | ||
char* src = (char*)&result; | ||
dst[3] = src[0]; dst[2] = src[1]; dst[1] = src[2]; dst[0] = src[3]; | ||
result = tmp; | ||
} | ||
|
||
if (std::isnan(result)) | ||
throw std::runtime_error("BinaryReader::ReadSingle(): Error reading stream"); | ||
|
||
return result; | ||
} | ||
|
||
double BinaryReader::ReadDouble() | ||
{ | ||
double result = NAN; | ||
|
||
stream->Read((char*)&result, sizeof(double)); | ||
|
||
if (endianness != Endianness::Native) | ||
{ | ||
double tmp; | ||
char* dst = (char*)&tmp; | ||
char* src = (char*)&result; | ||
dst[7] = src[0]; dst[6] = src[1]; dst[5] = src[2]; dst[4] = src[3]; | ||
dst[3] = src[4]; dst[2] = src[5]; dst[1] = src[6]; dst[0] = src[7]; | ||
result = tmp; | ||
} | ||
|
||
if (std::isnan(result)) | ||
throw std::runtime_error("BinaryReader::ReadDouble(): Error reading stream"); | ||
|
||
return result; | ||
} | ||
|
||
std::string BinaryReader::ReadString() | ||
{ | ||
std::string res; | ||
int numChars = ReadInt32(); | ||
|
||
for (int i = 0; i < numChars; i++) | ||
res += ReadChar(); | ||
|
||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include <array> | ||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
#include "BitConverter.h" | ||
#include "Stream.h" | ||
|
||
class BinaryReader | ||
{ | ||
public: | ||
BinaryReader(Stream* nStream); | ||
BinaryReader(std::shared_ptr<Stream> nStream); | ||
|
||
void Close(); | ||
|
||
void SetEndianness(Endianness endianness); | ||
Endianness GetEndianness() const; | ||
|
||
void Seek(uint32_t offset, SeekOffsetType seekType); | ||
uint32_t GetBaseAddress(); | ||
|
||
void Read(int32_t length); | ||
void Read(char* buffer, int32_t length); | ||
char ReadChar(); | ||
int8_t ReadByte(); | ||
int16_t ReadInt16(); | ||
int32_t ReadInt32(); | ||
uint8_t ReadUByte(); | ||
uint16_t ReadUInt16(); | ||
uint32_t ReadUInt32(); | ||
uint64_t ReadUInt64(); | ||
float ReadSingle(); | ||
double ReadDouble(); | ||
std::string ReadString(); | ||
|
||
protected: | ||
std::shared_ptr<Stream> stream; | ||
Endianness endianness = Endianness::Native; | ||
}; |
Oops, something went wrong.