2012-04-06 21:04:30 +02:00
|
|
|
#include "esm_writer.hpp"
|
2012-04-08 11:51:52 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <cstring>
|
2012-04-06 21:04:30 +02:00
|
|
|
|
|
|
|
namespace ESM
|
|
|
|
{
|
|
|
|
|
2012-04-06 22:25:33 +02:00
|
|
|
void ESMWriter::setVersion(Version ver)
|
|
|
|
{
|
|
|
|
m_header.version = ver;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESMWriter::setType(FileType type)
|
|
|
|
{
|
|
|
|
m_header.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESMWriter::setAuthor(const std::string& auth)
|
|
|
|
{
|
2012-04-08 11:51:52 +02:00
|
|
|
strncpy((char*)&m_header.author, auth.c_str(), 32);
|
2012-04-06 22:25:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ESMWriter::setDescription(const std::string& desc)
|
|
|
|
{
|
2012-04-08 11:51:52 +02:00
|
|
|
strncpy((char*)&m_header.desc, desc.c_str(), 256);
|
2012-04-06 22:25:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ESMWriter::save(const std::string& file)
|
|
|
|
{
|
2012-04-08 11:51:52 +02:00
|
|
|
std::ofstream fs(file.c_str(), std::ios_base::out | std::ios_base::trunc);
|
|
|
|
save(fs);
|
|
|
|
fs.close();
|
2012-04-06 22:25:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ESMWriter::save(std::ostream& file)
|
|
|
|
{
|
2012-04-08 11:51:52 +02:00
|
|
|
m_stream = &file;
|
|
|
|
|
|
|
|
startRecord("TES3");
|
|
|
|
writeT<int>(0);
|
|
|
|
writeT<int>(0);
|
|
|
|
|
|
|
|
endRecord();
|
|
|
|
|
2012-04-06 22:25:33 +02:00
|
|
|
// TODO: Saving
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESMWriter::close()
|
|
|
|
{
|
|
|
|
// TODO: Saving
|
|
|
|
}
|
|
|
|
|
2012-04-08 11:51:52 +02:00
|
|
|
void ESMWriter::startRecord(const std::string& name)
|
|
|
|
{
|
|
|
|
writeName(name);
|
|
|
|
RecordData rec;
|
|
|
|
rec.position = m_stream->tellp();
|
|
|
|
rec.size = 0;
|
|
|
|
m_records.push_back(rec);
|
|
|
|
writeT<int>(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESMWriter::endRecord()
|
|
|
|
{
|
|
|
|
std::streampos cur = m_stream->tellp();
|
|
|
|
RecordData rec = m_records.back();
|
|
|
|
m_records.pop_back();
|
|
|
|
|
|
|
|
m_stream->seekp(rec.position);
|
|
|
|
m_stream->write((char*)&rec.size, sizeof(int));
|
|
|
|
|
|
|
|
m_stream->seekp(cur);
|
|
|
|
}
|
|
|
|
|
2012-04-06 21:04:30 +02:00
|
|
|
void ESMWriter::writeHNString(const std::string& name, const std::string& data)
|
|
|
|
{
|
|
|
|
writeName(name);
|
|
|
|
writeHString(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESMWriter::writeHString(const std::string& data)
|
|
|
|
{
|
|
|
|
writeT<int>(data.size()-1);
|
|
|
|
write(data.c_str(), data.size()-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESMWriter::writeName(const std::string& name)
|
|
|
|
{
|
2012-04-06 22:25:33 +02:00
|
|
|
assert((name.size() == 4 && name[3] != '\0') || (name.size() == 5 && name[4] == '\0'));
|
2012-04-06 21:04:30 +02:00
|
|
|
write(name.c_str(), name.size()-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESMWriter::write(const char* data, int size)
|
|
|
|
{
|
2012-04-08 11:51:52 +02:00
|
|
|
if (!m_records.empty())
|
|
|
|
m_records.back().size += size;
|
|
|
|
|
|
|
|
m_stream->write(data, size);
|
2012-04-06 21:04:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|