forked from huxingyi/dust3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds3file.cpp
160 lines (148 loc) · 4.92 KB
/
ds3file.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <QFile>
#include <QTextStream>
#include <QXmlStreamReader>
#include "ds3file.h"
QString Ds3FileReader::m_applicationName = QString("DUST3D");
QString Ds3FileReader::m_fileFormatVersion = QString("1.0");
QString Ds3FileReader::m_headFormat = QString("xml");
QString Ds3FileReader::readFirstLine()
{
QString firstLine;
QFile file(m_filename);
if (file.open(QIODevice::ReadOnly)) {
QTextStream in(&file);
if (!in.atEnd()) {
firstLine = in.readLine();
}
file.close();
}
return firstLine;
}
Ds3FileReader::Ds3FileReader(const QString &filename) :
m_headerIsGood(false),
m_binaryOffset(0)
{
m_filename = filename;
QString firstLine = readFirstLine();
QStringList tokens = firstLine.split(" ");
if (tokens.length() < 4) {
return;
}
if (tokens[0] != Ds3FileReader::m_applicationName) {
return;
}
if (tokens[1] != Ds3FileReader::m_fileFormatVersion) {
return;
}
if (tokens[2] != Ds3FileReader::m_headFormat) {
return;
}
m_binaryOffset = tokens[3].toLongLong();
QFile file(m_filename);
if (!file.open(QIODevice::ReadOnly)) {
return;
}
QString header = QString::fromUtf8(file.read(m_binaryOffset).constData()).mid(firstLine.size()).trimmed();
QXmlStreamReader xml(header);
bool ds3TagEntered = false;
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == "ds3") {
ds3TagEntered = true;
m_headerIsGood = true;
} else {
if (ds3TagEntered) {
Ds3ReaderItem readerItem;
readerItem.type = xml.name().toString().trimmed();
readerItem.name = xml.attributes().value("name").toString().trimmed();
readerItem.offset = xml.attributes().value("offset").toLongLong();
readerItem.size = xml.attributes().value("size").toLongLong();
m_items.push_back(readerItem);
m_itemsMap[readerItem.name] = readerItem;
}
}
} else if (xml.isEndElement()) {
if (xml.name() == "ds3") {
ds3TagEntered = false;
}
}
}
}
void Ds3FileReader::loadItem(const QString &name, QByteArray *byteArray)
{
byteArray->clear();
if (!m_headerIsGood)
return;
if (m_itemsMap.find(name) == m_itemsMap.end()) {
return;
}
Ds3ReaderItem readerItem = m_itemsMap[name];
QFile file(m_filename);
if (!file.open(QIODevice::ReadOnly)) {
return;
}
if (!file.seek(m_binaryOffset + readerItem.offset)) {
return;
}
*byteArray = file.read(readerItem.size);
}
const QList<Ds3ReaderItem> &Ds3FileReader::items()
{
return m_items;
}
bool Ds3FileWriter::add(const QString &name, const QString &type, const QByteArray *byteArray)
{
if (m_itemsMap.find(name) != m_itemsMap.end()) {
return false;
}
Ds3WriterItem writerItem;
writerItem.type = type;
writerItem.name = name;
writerItem.byteArray = *byteArray;
m_itemsMap[name] = writerItem;
m_items.push_back(writerItem);
return true;
}
bool Ds3FileWriter::save(const QString &filename)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
return false;
}
QByteArray headerXml;
{
QXmlStreamWriter stream(&headerXml);
stream.setAutoFormatting(true);
stream.writeStartDocument();
stream.writeStartElement("ds3");
long long offset = 0;
for (int i = 0; i < m_items.size(); i++) {
Ds3WriterItem *writerItem = &m_items[i];
stream.writeStartElement(writerItem->type);
stream.writeAttribute("name", QString("%1").arg(writerItem->name));
stream.writeAttribute("offset", QString("%1").arg(offset));
stream.writeAttribute("size", QString("%1").arg(writerItem->byteArray.size()));
offset += writerItem->byteArray.size();
stream.writeEndElement();
}
stream.writeEndElement();
stream.writeEndDocument();
}
char firstLine[1024];
int firstLineSizeExcludeSizeSelf = sprintf(firstLine, "%s %s %s ",
Ds3FileReader::m_applicationName.toUtf8().constData(),
Ds3FileReader::m_fileFormatVersion.toUtf8().constData(),
Ds3FileReader::m_headFormat.toUtf8().constData());
unsigned int headerSize = firstLineSizeExcludeSizeSelf + 12 + headerXml.size();
char headerSizeString[100] = {0};
sprintf(headerSizeString, "%010u\r\n", headerSize);
file.write(firstLine, firstLineSizeExcludeSizeSelf);
file.write(headerSizeString, strlen(headerSizeString));
file.write(headerXml);
for (int i = 0; i < m_items.size(); i++) {
Ds3WriterItem *writerItem = &m_items[i];
file.write(writerItem->byteArray);
}
return true;
}