84 lines
No EOL
1.6 KiB
C++
84 lines
No EOL
1.6 KiB
C++
|
|
#include <iostream>
|
|
#include <Spectre/System/ByteOrder.h>
|
|
#include "Archive.h"
|
|
|
|
void Archive::add(const std::string& path, const std::vector<uint8_t>& data)
|
|
{
|
|
m_files[path] = data;
|
|
}
|
|
|
|
const std::map<std::string, std::vector<uint8_t>>& Archive::files() const
|
|
{
|
|
return m_files;
|
|
}
|
|
|
|
void Archive::read(const std::string& filename)
|
|
{
|
|
struct Header hdr;
|
|
|
|
sp::File fd(filename);
|
|
|
|
// Read header
|
|
fd.read(&hdr.sig, 2);
|
|
fd.read(&hdr.version, 1);
|
|
|
|
if (hdr.sig[0] == 0xAE && hdr.sig[1] == 0xEE) {
|
|
|
|
}
|
|
|
|
|
|
for(;;) {
|
|
struct Index idx = { { '\0' }, 0 };
|
|
std::vector<uint8_t> data;
|
|
|
|
if (fd.read(&idx.path, ARCHIVE_PATH_MAX_LEN) < 1) {
|
|
break;
|
|
}
|
|
|
|
if (fd.read(&idx.size, 4) < 1) {
|
|
break;
|
|
}
|
|
|
|
data.resize(idx.size);
|
|
|
|
if (fd.read(&data[0], idx.size) < 1) {
|
|
break;
|
|
}
|
|
|
|
m_files[idx.path] = data;
|
|
}
|
|
}
|
|
|
|
void Archive::write(const std::string& filename)
|
|
{
|
|
sp::core::Buffer<uint8_t> buffer;
|
|
sp::File file(filename, sp::File::Access::WRITE,
|
|
sp::File::CREATE | sp::File::TRUNCATE);
|
|
|
|
pack(buffer);
|
|
|
|
file.write(buffer.data);
|
|
file.close();
|
|
}
|
|
|
|
#define min(a,b) ((a) < (b) ? (a) : (b))
|
|
|
|
void Archive::pack(sp::core::Buffer<uint8_t>& buffer)
|
|
{
|
|
buffer.data.reserve(sizeof(struct Header));
|
|
|
|
// Sig
|
|
buffer.data.push_back(0xAE);
|
|
buffer.data.push_back(0xEE);
|
|
// Version
|
|
buffer.data.push_back(0x01);
|
|
|
|
for (auto file : m_files) {
|
|
struct Index idx = { { '\0', }, file.second.size() };
|
|
memcpy(idx.path, file.first.c_str(), min(file.first.size(), ARCHIVE_PATH_MAX_LEN - 1));
|
|
buffer.append(&idx.path, ARCHIVE_PATH_MAX_LEN);
|
|
buffer.append(&idx.size, 4);
|
|
buffer.append(&file.second[0], file.second.size());
|
|
}
|
|
} |