-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryReader.cpp
113 lines (96 loc) · 2.22 KB
/
BinaryReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "stdafx.h"
#include "BinaryReader.h"
using namespace std;
BinaryReader::BinaryReader(const std::wstring& binaryFile)
: m_Exists(false)
, m_Reader{ ifstream(binaryFile.c_str(), ios::in | ios::binary) }
{
if (m_Reader.is_open()) m_Exists = true;
else
{
wstringstream ss;
ss << L"BinaryReader::Open -> Failed to open the file!\nBinaryFile: " << binaryFile;
Logger::GetInstance()->LogWarning(ss.str());
}
}
BinaryReader::BinaryReader(char* s, UINT32 size)
: m_Exists(false)
, m_Reader{ ifstream(s, ios::in | ios::binary) }
{
if (m_Reader.is_open()) m_Exists = true;
else
{
wstringstream ss;
ss << L"BinaryReader::Open -> Failed to open the file!\nBinaryFile: " << s;
Logger::GetInstance()->LogWarning(ss.str());
}
}
wstring BinaryReader::ReadLongString()
{
if (!m_Exists)
{
Logger::GetInstance()->LogError(L"BinaryReader doesn't exist!\nUnable to read binary data...");
return L"";
}
auto stringLength = Read<UINT>();
wstringstream ss;
for (UINT i{ 0 }; i < stringLength; ++i)
{
ss << Read<wchar_t>();
}
return ss.str();
}
wstring BinaryReader::ReadNullString()
{
if (!m_Exists)
{
Logger::GetInstance()->LogError(L"BinaryReader doesn't exist!\nUnable to read binary data...");
return L"";
}
string buff;
getline(m_Reader, buff, '\0');
return wstring(buff.begin(),buff.end());
}
wstring BinaryReader::ReadString()
{
if (!m_Exists)
{
Logger::GetInstance()->LogError(L"BinaryReader doesn't exist!\nUnable to read binary data...");
return L"";
}
int stringLength = (int)Read<char>();
wstringstream ss;
for (int i{ 0 }; i < stringLength; ++i)
{
ss << Read<char>();
}
return ss.str();
}
int BinaryReader::GetBufferPosition()
{
if(m_Exists)
{
return static_cast<int>(m_Reader.tellg());
}
Logger::GetInstance()->LogWarning(L"BinaryReader::GetBufferPosition> m_pReader doesn't exist");
return -1;
}
bool BinaryReader::SetBufferPosition(int pos)
{
if(m_Exists)
{
m_Reader.seekg(pos);
return true;
}
Logger::GetInstance()->LogWarning(L"BinaryReader::SetBufferPosition> m_pReader doesn't exist");
return false;
}
bool BinaryReader::MoveBufferPosition(int move)
{
auto currPos = GetBufferPosition();
if(currPos>0)
{
return SetBufferPosition(currPos + move);
}
return false;
}