#include #include #include "Archive.h" void Archive::add(const std::string& path, const std::vector& data) { m_files[path] = data; } const std::map>& 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 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 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& 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()); } }