Update Went for an alternate method of reading in See below
Original post
I'm still new to C++, I've been coding in Unreal for a bit, but I'm writing a File Parser and am doing it as a standalone solution. I want to read an unknown length of bytes from an unknown start point in the unique_ptr array. Also would be appreciative of comments on the cleanliness or efficiency of the code, want to go forward with good habits.
I've been able to read in the files using basic_ifstream as so
auto ReadFileData(std::string path) {
auto fileSize = std::filesystem::file_size(path);
auto buf = std::make_unique<std::byte[]>(fileSize);
std::basic_ifstream<std::byte> ifs(path, std::ios::binary);
ifs.read(buf.get(), fileSize);
return buf;
}
That returns the unique_ptr byte array to fileData
. I've written a method to read in a certain number of bytes to convert to a get values from the binary, but it takes them one byte at a time and I feel this is going to be slow.
uint32_t ParseUint32(auto& fileData, uint32_t dataLocation) {
return uint32_t((unsigned char)(fileData[dataLocation+3]) << 24 |
(unsigned char)(fileData[dataLocation + 2]) << 16 |
(unsigned char)(fileData[dataLocation + 1]) << 8 |
(unsigned char)(fileData[dataLocation]));
}
But I've got to a point that I need to read in a compressed range of bytes to decompress the data within. I'm not seeming to get anywhere with memcpy, and have read that unique_ptr arrays can't be copied? The last line is where I'm stuck.
for(uint16_t i = 0; i<tileOffsetCount; i++)
{
tileOffsets[i] = ParseUint32(fileData,tileOffset + (i*4));
uint32_t tileByteSize = ParseUint32(fileData,tileByteCountsOffset +(i*4)) ;
char* tileData = new char[tileByteSize];
std::memcpy(tileData, fileData+tileOffsets[i],tileByteSize); //This line is where I'm stuck
}