Coverage Report

Created: 2025-06-22 07:30

/src/assimp/contrib/openddlparser/code/OpenDDLExport.cpp
Line
Count
Source (jump to first uncovered line)
1
/*-----------------------------------------------------------------------------------------------
2
The MIT License (MIT)
3
4
Copyright (c) 2014-2020 Kim Kulling
5
6
Permission is hereby granted, free of charge, to any person obtaining a copy of
7
this software and associated documentation files (the "Software"), to deal in
8
the Software without restriction, including without limitation the rights to
9
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10
the Software, and to permit persons to whom the Software is furnished to do so,
11
subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
-----------------------------------------------------------------------------------------------*/
23
#include <openddlparser/DDLNode.h>
24
#include <openddlparser/OpenDDLExport.h>
25
#include <openddlparser/OpenDDLParser.h>
26
#include <openddlparser/Value.h>
27
28
#include <sstream>
29
30
BEGIN_ODDLPARSER_NS
31
32
struct DDLNodeIterator {
33
    const DDLNode::DllNodeList &m_childs;
34
    size_t m_idx;
35
36
    DDLNodeIterator(const DDLNode::DllNodeList &childs) :
37
0
            m_childs(childs), m_idx(0) {
38
        // empty
39
0
    }
40
41
0
    ~DDLNodeIterator() {
42
        // empty
43
0
    }
44
45
0
    bool getNext(DDLNode **node) {
46
0
        if (m_childs.size() > (m_idx + 1)) {
47
0
            m_idx++;
48
0
            *node = m_childs[m_idx];
49
0
            return true;
50
0
        }
51
52
0
        return false;
53
0
    }
54
55
private:
56
    DDLNodeIterator() ddl_no_copy;
57
    DDLNodeIterator &operator=(const DDLNodeIterator &) ddl_no_copy;
58
};
59
60
0
static void writeLineEnd(std::string &statement) {
61
0
    statement += "\n";
62
0
}
63
64
OpenDDLExport::OpenDDLExport(IOStreamBase *stream) :
65
0
        m_stream(stream) {
66
0
    if (nullptr == m_stream) {
67
0
        m_stream = new IOStreamBase();
68
0
    }
69
0
}
70
71
0
OpenDDLExport::~OpenDDLExport() {
72
0
    if (nullptr != m_stream) {
73
0
        m_stream->close();
74
0
    }
75
0
    delete m_stream;
76
0
}
77
78
0
bool OpenDDLExport::exportContext(Context *ctx, const std::string &filename) {
79
0
    if (nullptr == ctx) {
80
0
        return false;
81
0
    }
82
83
0
    DDLNode *root(ctx->m_root);
84
0
    if (nullptr == root) {
85
0
        return true;
86
0
    }
87
88
0
    if (!filename.empty()) {
89
0
        if (!m_stream->open(filename)) {
90
0
            return false;
91
0
        }
92
0
    }
93
94
0
    const bool retValue(handleNode(root));
95
96
0
    return retValue;
97
0
}
98
99
0
bool OpenDDLExport::handleNode(DDLNode *node) {
100
0
    if (nullptr == node) {
101
0
        return true;
102
0
    }
103
104
0
    const DDLNode::DllNodeList &childs = node->getChildNodeList();
105
0
    if (childs.empty()) {
106
0
        return true;
107
0
    }
108
0
    DDLNode *current(nullptr);
109
0
    DDLNodeIterator it(childs);
110
0
    std::string statement;
111
0
    bool success(true);
112
0
    while (it.getNext(&current)) {
113
0
        if (nullptr != current) {
114
0
            success |= writeNode(current, statement);
115
0
            if (!handleNode(current)) {
116
0
                success = false;
117
0
            }
118
0
        }
119
0
    }
120
121
0
    return success;
122
0
}
123
124
0
bool OpenDDLExport::writeToStream(const std::string &statement) {
125
0
    if (nullptr == m_stream) {
126
0
        return false;
127
0
    }
128
129
0
    if (!statement.empty()) {
130
0
        m_stream->write(statement);
131
0
    }
132
133
0
    return true;
134
0
}
135
136
0
bool OpenDDLExport::writeNode(DDLNode *node, std::string &statement) {
137
0
    bool success(true);
138
0
    writeNodeHeader(node, statement);
139
0
    if (node->hasProperties()) {
140
0
        success = writeProperties(node, statement);
141
0
    }
142
0
    writeLineEnd(statement);
143
144
0
    statement = "}";
145
0
    DataArrayList *al(node->getDataArrayList());
146
0
    if (nullptr != al) {
147
0
        writeValueType(al->m_dataList->m_type, al->m_numItems, statement);
148
0
        writeValueArray(al, statement);
149
0
    }
150
0
    Value *v(node->getValue());
151
0
    if (nullptr != v) {
152
0
        writeValueType(v->m_type, 1, statement);
153
0
        statement = "{";
154
0
        writeLineEnd(statement);
155
0
        writeValue(v, statement);
156
0
        statement = "}";
157
0
        writeLineEnd(statement);
158
0
    }
159
0
    statement = "}";
160
0
    writeLineEnd(statement);
161
162
0
    writeToStream(statement);
163
164
0
    return success;
165
0
}
166
167
0
bool OpenDDLExport::writeNodeHeader(DDLNode *node, std::string &statement) {
168
0
    if (nullptr == node) {
169
0
        return false;
170
0
    }
171
172
0
    statement += node->getType();
173
0
    const std::string &name(node->getName());
174
0
    if (!name.empty()) {
175
0
        statement += " ";
176
0
        statement += "$";
177
0
        statement += name;
178
0
    }
179
180
0
    return true;
181
0
}
182
183
0
bool OpenDDLExport::writeProperties(DDLNode *node, std::string &statement) {
184
0
    if (nullptr == node) {
185
0
        return false;
186
0
    }
187
188
0
    Property *prop(node->getProperties());
189
    // if no properties are there, return
190
0
    if (nullptr == prop) {
191
0
        return true;
192
0
    }
193
194
0
    if (nullptr != prop) {
195
        // for instance (attrib = "position", bla=2)
196
0
        statement += "(";
197
0
        bool first(true);
198
0
        while (nullptr != prop) {
199
0
            if (!first) {
200
0
                statement += ", ";
201
0
            } else {
202
0
                first = false;
203
0
            }
204
0
            statement += std::string(prop->m_key->m_buffer);
205
0
            statement += " = ";
206
0
            writeValue(prop->m_value, statement);
207
0
            prop = prop->m_next;
208
0
        }
209
210
0
        statement += ")";
211
0
    }
212
213
0
    return true;
214
0
}
215
216
0
bool OpenDDLExport::writeValueType(Value::ValueType type, size_t numItems, std::string &statement) {
217
0
    if (Value::ValueType::ddl_types_max == type) {
218
0
        return false;
219
0
    }
220
221
0
    const std::string typeStr(getTypeToken(type));
222
0
    statement += typeStr;
223
    // if we have an array to write
224
0
    if (numItems > 1) {
225
0
        statement += "[";
226
0
        char buffer[256];
227
0
        ::memset(buffer, '\0', 256 * sizeof(char));
228
0
        snprintf(buffer, sizeof(buffer), "%d", static_cast<int>(numItems));
229
0
        statement += buffer;
230
0
        statement += "]";
231
0
    }
232
233
0
    return true;
234
0
}
235
236
0
bool OpenDDLExport::writeValue(Value *val, std::string &statement) {
237
0
    if (nullptr == val) {
238
0
        return false;
239
0
    }
240
241
0
    switch (val->m_type) {
242
0
        case Value::ValueType::ddl_bool:
243
0
            if (true == val->getBool()) {
244
0
                statement += "true";
245
0
            } else {
246
0
                statement += "false";
247
0
            }
248
0
            break;
249
0
        case Value::ValueType::ddl_int8 : {
250
0
            std::stringstream stream;
251
0
            const int i = static_cast<int>(val->getInt8());
252
0
            stream << i;
253
0
            statement += stream.str();
254
0
        } break;
255
0
        case Value::ValueType::ddl_int16: {
256
0
            std::stringstream stream;
257
0
            char buffer[256];
258
0
            ::memset(buffer, '\0', 256 * sizeof(char));
259
0
            snprintf(buffer, sizeof(buffer), "%d", val->getInt16());
260
0
            statement += buffer;
261
0
        } break;
262
0
        case Value::ValueType::ddl_int32: {
263
0
            std::stringstream stream;
264
0
            char buffer[256];
265
0
            ::memset(buffer, '\0', 256 * sizeof(char));
266
0
            const int i = static_cast<int>(val->getInt32());
267
0
            snprintf(buffer, sizeof(buffer), "%d", i);
268
0
            statement += buffer;
269
0
        } break;
270
0
        case Value::ValueType::ddl_int64: {
271
0
            std::stringstream stream;
272
0
            const int i = static_cast<int>(val->getInt64());
273
0
            stream << i;
274
0
            statement += stream.str();
275
0
        } break;
276
0
        case Value::ValueType::ddl_unsigned_int8: {
277
0
            std::stringstream stream;
278
0
            const int i = static_cast<unsigned int>(val->getUnsignedInt8());
279
0
            stream << i;
280
0
            statement += stream.str();
281
0
        } break;
282
0
        case Value::ValueType::ddl_unsigned_int16: {
283
0
            std::stringstream stream;
284
0
            const int i = static_cast<unsigned int>(val->getUnsignedInt16());
285
0
            stream << i;
286
0
            statement += stream.str();
287
0
        } break;
288
0
        case Value::ValueType::ddl_unsigned_int32: {
289
0
            std::stringstream stream;
290
0
            const int i = static_cast<unsigned int>(val->getUnsignedInt32());
291
0
            stream << i;
292
0
            statement += stream.str();
293
0
        } break;
294
0
        case Value::ValueType::ddl_unsigned_int64: {
295
0
            std::stringstream stream;
296
0
            const int i = static_cast<unsigned int>(val->getUnsignedInt64());
297
0
            stream << i;
298
0
            statement += stream.str();
299
0
        } break;
300
0
        case Value::ValueType::ddl_half:
301
0
            break;
302
0
        case Value::ValueType::ddl_float: {
303
0
            std::stringstream stream;
304
0
            stream << val->getFloat();
305
0
            statement += stream.str();
306
0
        } break;
307
0
        case Value::ValueType::ddl_double: {
308
0
            std::stringstream stream;
309
0
            stream << val->getDouble();
310
0
            statement += stream.str();
311
0
        } break;
312
0
        case Value::ValueType::ddl_string: {
313
0
            std::stringstream stream;
314
0
            stream << val->getString();
315
0
            statement += "\"";
316
0
            statement += stream.str();
317
0
            statement += "\"";
318
0
        } break;
319
0
        case Value::ValueType::ddl_ref:
320
0
            break;
321
0
        case Value::ValueType::ddl_none:
322
0
        case Value::ValueType::ddl_types_max:
323
0
        default:
324
0
            break;
325
0
    }
326
327
0
    return true;
328
0
}
329
330
0
bool OpenDDLExport::writeValueArray(DataArrayList *al, std::string &statement) {
331
0
    if (nullptr == al) {
332
0
        return false;
333
0
    }
334
335
0
    if (0 == al->m_numItems) {
336
0
        return true;
337
0
    }
338
339
0
    DataArrayList *nextDataArrayList = al;
340
0
    Value *nextValue(nextDataArrayList->m_dataList);
341
0
    while (nullptr != nextDataArrayList) {
342
0
        if (nullptr != nextDataArrayList) {
343
0
            statement += "{ ";
344
0
            nextValue = nextDataArrayList->m_dataList;
345
0
            size_t idx(0);
346
0
            while (nullptr != nextValue) {
347
0
                if (idx > 0) {
348
0
                    statement += ", ";
349
0
                }
350
0
                writeValue(nextValue, statement);
351
0
                nextValue = nextValue->m_next;
352
0
                idx++;
353
0
            }
354
0
            statement += " }";
355
0
        }
356
0
        nextDataArrayList = nextDataArrayList->m_next;
357
0
    }
358
359
0
    return true;
360
0
}
361
362
END_ODDLPARSER_NS