OpenMW
components/esm/esmwriter.hpp
Go to the documentation of this file.
00001 #ifndef OPENMW_ESM_WRITER_H
00002 #define OPENMW_ESM_WRITER_H
00003 
00004 #include <iosfwd>
00005 #include <list>
00006 
00007 #include <components/to_utf8/to_utf8.hpp>
00008 
00009 #include "esmcommon.hpp"
00010 #include "loadtes3.hpp"
00011 
00012 namespace ESM {
00013 
00014 class ESMWriter
00015 {
00016         struct RecordData
00017         {
00018             std::string name;
00019             std::streampos position;
00020             size_t size;
00021         };
00022 
00023     public:
00024 
00025         ESMWriter();
00026 
00027         unsigned int getVersion() const;
00028         void setVersion(unsigned int ver = 0x3fa66666);
00029         void setEncoder(ToUTF8::Utf8Encoder *encoding);
00030         void setAuthor(const std::string& author);
00031         void setDescription(const std::string& desc);
00032         void setRecordCount (int count);
00033         void setFormat (int format);
00034 
00035         void clearMaster();
00036 
00037         void addMaster(const std::string& name, uint64_t size);
00038 
00039         void save(const std::string& file);
00041 
00042         void save(std::ostream& file);
00044 
00045         void close();
00047 
00048         void writeHNString(const std::string& name, const std::string& data);
00049         void writeHNString(const std::string& name, const std::string& data, size_t size);
00050         void writeHNCString(const std::string& name, const std::string& data)
00051         {
00052             startSubRecord(name);
00053             writeHCString(data);
00054             endRecord(name);
00055         }
00056         void writeHNOString(const std::string& name, const std::string& data)
00057         {
00058             if (!data.empty())
00059                 writeHNString(name, data);
00060         }
00061         void writeHNOCString(const std::string& name, const std::string& data)
00062         {
00063             if (!data.empty())
00064                 writeHNCString(name, data);
00065         }
00066 
00067         template<typename T>
00068         void writeHNT(const std::string& name, const T& data)
00069         {
00070             startSubRecord(name);
00071             writeT(data);
00072             endRecord(name);
00073         }
00074 
00075         template<typename T>
00076         void writeHNT(const std::string& name, const T& data, int size)
00077         {
00078             startSubRecord(name);
00079             writeT(data, size);
00080             endRecord(name);
00081         }
00082 
00083         template<typename T>
00084         void writeT(const T& data)
00085         {
00086             write((char*)&data, sizeof(T));
00087         }
00088 
00089         template<typename T>
00090         void writeT(const T& data, size_t size)
00091         {
00092             write((char*)&data, size);
00093         }
00094 
00095         void startRecord(const std::string& name, uint32_t flags = 0);
00096         void startSubRecord(const std::string& name);
00097         void endRecord(const std::string& name);
00098         void writeHString(const std::string& data);
00099         void writeHCString(const std::string& data);
00100         void writeName(const std::string& data);
00101         void write(const char* data, size_t size);
00102 
00103     private:
00104         std::list<RecordData> mRecords;
00105         std::ostream* mStream;
00106         std::streampos mHeaderPos;
00107         ToUTF8::Utf8Encoder* mEncoder;
00108         int mRecordCount;
00109         bool mCounting;
00110 
00111         Header mHeader;
00112     };
00113 }
00114 
00115 #endif