init
This commit is contained in:
92
expkg/src/BinaryOPT/BinaryReader.cpp
Normal file
92
expkg/src/BinaryOPT/BinaryReader.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
//
|
||||
// Created by sfd on 25-8-4.
|
||||
//
|
||||
|
||||
#include "BinaryReader.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
namespace PKG
|
||||
{
|
||||
BinaryReader::BinaryReader(const std::filesystem::path& fileName)
|
||||
{
|
||||
m_FilePath = fileName.string();
|
||||
|
||||
m_File.open(m_FilePath, std::ios::in | std::ios::binary);
|
||||
if (!m_File.is_open())
|
||||
{
|
||||
std::cerr << "Failed to open file " << m_FilePath << std::endl;
|
||||
system("pause");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
BinaryReader::~BinaryReader()
|
||||
{
|
||||
if (m_File.is_open())
|
||||
m_File.close();
|
||||
}
|
||||
|
||||
|
||||
int32_t BinaryReader::ReadInt32()
|
||||
{
|
||||
int32_t result = 0;
|
||||
m_File.read(reinterpret_cast<char*>(&result), sizeof(int32_t));
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t BinaryReader::ReadUInt32()
|
||||
{
|
||||
uint32_t result = 0;
|
||||
m_File.read(reinterpret_cast<char*>(&result), sizeof(uint32_t));
|
||||
return result;
|
||||
}
|
||||
|
||||
char BinaryReader::ReadChar()
|
||||
{
|
||||
char result;
|
||||
m_File.read(&result, sizeof(char));
|
||||
pos_type a = m_File.tellg();
|
||||
return result;
|
||||
}
|
||||
|
||||
void BinaryReader::ReadData(std::string& data, const uint32_t length)
|
||||
{
|
||||
data.resize(length);
|
||||
m_File.read(data.data(), length);
|
||||
}
|
||||
|
||||
void BinaryReader::ReadData(std::vector<uint8_t>& data, uint32_t length)
|
||||
{
|
||||
data.resize(length);
|
||||
m_File.read(reinterpret_cast<char*>(data.data()), length);
|
||||
}
|
||||
|
||||
std::string BinaryReader::ReadString(const uint32_t length)
|
||||
{
|
||||
std::vector<uint8_t> result;
|
||||
result.resize(length);
|
||||
|
||||
m_File.read(reinterpret_cast<char*>(result.data()), length);
|
||||
return std::string(reinterpret_cast<const char*>(result.data()), length);
|
||||
}
|
||||
|
||||
std::string BinaryReader::ReadNString(const int32_t maxLength)
|
||||
{
|
||||
std::vector<uint8_t> result;
|
||||
result.resize(0);
|
||||
int count = maxLength <= 0 ? 16 : maxLength;
|
||||
|
||||
char chr = ReadChar();
|
||||
while (chr != '\0' && (maxLength == -1 || count <= maxLength))
|
||||
{
|
||||
result.push_back(chr);
|
||||
chr = ReadChar();
|
||||
}
|
||||
|
||||
return std::string(reinterpret_cast<const char*>(result.data()), result.size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user