OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
esmwriter.hpp
Go to the documentation of this file.
1 #ifndef OPENMW_ESM_WRITER_H
2 #define OPENMW_ESM_WRITER_H
3 
4 #include <iosfwd>
5 #include <list>
6 
7 #include "esmcommon.hpp"
8 #include "loadtes3.hpp"
9 
10 namespace ToUTF8
11 {
12  class Utf8Encoder;
13 }
14 
15 namespace ESM {
16 
17 class ESMWriter
18 {
19  struct RecordData
20  {
21  std::string name;
22  std::streampos position;
23  uint32_t size;
24  };
25 
26  public:
27 
28  ESMWriter();
29 
30  unsigned int getVersion() const;
31 
32  // Set various header data (ESM::Header::Data). All of the below functions must be called before writing,
33  // otherwise this data will be left uninitialized.
34  void setVersion(unsigned int ver = 0x3fa66666);
35  void setType(int type);
36  void setEncoder(ToUTF8::Utf8Encoder *encoding);
37  void setAuthor(const std::string& author);
38  void setDescription(const std::string& desc);
39 
40  // Set the record count for writing it in the file header
41  void setRecordCount (int count);
42  // Counts how many records we have actually written.
43  // It is a good idea to compare this with the value you wrote into the header (setRecordCount)
44  // It should be the record count you set + 1 (1 additional record for the TES3 header)
45  int getRecordCount() { return mRecordCount; }
46  void setFormat (int format);
47 
48  void clearMaster();
49 
50  void addMaster(const std::string& name, uint64_t size);
51 
52  void save(std::ostream& file);
54 
55  void close();
57 
58  void writeHNString(const std::string& name, const std::string& data);
59  void writeHNString(const std::string& name, const std::string& data, size_t size);
60  void writeHNCString(const std::string& name, const std::string& data)
61  {
62  startSubRecord(name);
63  writeHCString(data);
64  endRecord(name);
65  }
66  void writeHNOString(const std::string& name, const std::string& data)
67  {
68  if (!data.empty())
69  writeHNString(name, data);
70  }
71  void writeHNOCString(const std::string& name, const std::string& data)
72  {
73  if (!data.empty())
74  writeHNCString(name, data);
75  }
76 
77  template<typename T>
78  void writeHNT(const std::string& name, const T& data)
79  {
80  startSubRecord(name);
81  writeT(data);
82  endRecord(name);
83  }
84 
85 private:
86  // Prevent using writeHNT with strings. This already happened by accident and results in
87  // state being discarded without any error on writing or reading it. :(
88  // writeHNString and friends must be used instead.
89  void writeHNT(const std::string &name, std::string data)
90  {
91  }
92  void writeT(const std::string& data)
93  {
94  }
95 public:
96 
97  template<typename T>
98  void writeHNT(const std::string& name, const T& data, int size)
99  {
100  startSubRecord(name);
101  writeT(data, size);
102  endRecord(name);
103  }
104 
105  template<typename T>
106  void writeT(const T& data)
107  {
108  write((char*)&data, sizeof(T));
109  }
110 
111  template<typename T>
112  void writeT(const T& data, size_t size)
113  {
114  write((char*)&data, size);
115  }
116 
117  void startRecord(const std::string& name, uint32_t flags = 0);
118  void startRecord(uint32_t name, uint32_t flags = 0);
120  void startSubRecord(const std::string& name);
121  void endRecord(const std::string& name);
122  void endRecord(uint32_t name);
123  void writeFixedSizeString(const std::string& data, int size);
124  void writeHString(const std::string& data);
125  void writeHCString(const std::string& data);
126  void writeName(const std::string& data);
127  void write(const char* data, size_t size);
128 
129  private:
130  std::list<RecordData> mRecords;
131  std::ostream* mStream;
132  std::streampos mHeaderPos;
135  bool mCounting;
136 
138  };
139 }
140 
141 #endif
void startSubRecord(const std::string &name)
Definition: esmwriter.cpp:116
void writeT(const T &data)
Definition: esmwriter.hpp:106
std::streampos position
Definition: esmwriter.hpp:22
void writeHNT(const std::string &name, const T &data)
Definition: esmwriter.hpp:78
void writeHNOString(const std::string &name, const std::string &data)
Definition: esmwriter.hpp:66
void startRecord(const std::string &name, uint32_t flags=0)
Definition: esmwriter.cpp:89
std::ostream * mStream
Definition: esmwriter.hpp:131
void clearMaster()
Definition: esmwriter.cpp:56
void writeName(const std::string &data)
Definition: esmwriter.cpp:209
void setAuthor(const std::string &author)
Definition: esmwriter.cpp:36
void writeHNString(const std::string &name, const std::string &data)
Definition: esmwriter.cpp:158
void writeHCString(const std::string &data)
Definition: esmwriter.cpp:202
uint32_t size
Definition: esmwriter.hpp:23
std::streampos mHeaderPos
Definition: esmwriter.hpp:132
void write(const char *data, size_t size)
Definition: esmwriter.cpp:215
void setDescription(const std::string &desc)
Definition: esmwriter.cpp:41
void writeHString(const std::string &data)
Definition: esmwriter.cpp:189
void setVersion(unsigned int ver=0x3fa66666)
Definition: esmwriter.cpp:26
ToUTF8::Utf8Encoder * mEncoder
Definition: esmwriter.hpp:133
void writeHNT(const std::string &name, const T &data, int size)
Definition: esmwriter.hpp:98
Definition: esmwriter.hpp:17
void setFormat(int format)
Definition: esmwriter.cpp:51
int mRecordCount
Definition: esmwriter.hpp:134
bool mCounting
Definition: esmwriter.hpp:135
Header mHeader
Definition: esmwriter.hpp:137
void setRecordCount(int count)
Definition: esmwriter.cpp:46
unsigned int getVersion() const
Definition: esmwriter.cpp:21
void addMaster(const std::string &name, uint64_t size)
Definition: esmwriter.cpp:61
File header record.
Definition: loadtes3.hpp:17
void save(std::ostream &file)
Start saving a file by writing the TES3 header.
Definition: esmwriter.cpp:69
void writeFixedSizeString(const std::string &data, int size)
Definition: esmwriter.cpp:180
void writeHNT(const std::string &name, std::string data)
Definition: esmwriter.hpp:89
void setType(int type)
Definition: esmwriter.cpp:31
void setEncoder(ToUTF8::Utf8Encoder *encoding)
Definition: esmwriter.cpp:226
ESMWriter()
Definition: esmwriter.cpp:11
void close()
Definition: esmwriter.cpp:83
void writeHNOCString(const std::string &name, const std::string &data)
Definition: esmwriter.hpp:71
void endRecord(const std::string &name)
Definition: esmwriter.cpp:132
std::list< RecordData > mRecords
Definition: esmwriter.hpp:130
Definition: esmwriter.hpp:19
void writeT(const std::string &data)
Definition: esmwriter.hpp:92
Definition: to_utf8.hpp:25
int getRecordCount()
Definition: esmwriter.hpp:45
std::string name
Definition: esmwriter.hpp:21
void writeHNCString(const std::string &name, const std::string &data)
Definition: esmwriter.hpp:60
void writeT(const T &data, size_t size)
Definition: esmwriter.hpp:112
const char * name
Definition: crashcatcher.cpp:59