~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to COLLADAStreamWriter/include/COLLADASWStreamWriter.h

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2008-2009 NetAllied Systems GmbH
 
3
 
 
4
        This file is part of COLLADAStreamWriter.
 
5
 
 
6
    Licensed under the MIT Open Source License,
 
7
    for details please see LICENSE file or the website
 
8
    http://www.opensource.org/licenses/mit-license.php
 
9
*/
 
10
 
 
11
#ifndef __COLLADASTREAMWRITER_H__
 
12
#define __COLLADASTREAMWRITER_H__
 
13
 
 
14
#include <COLLADABUPlatform.h>
 
15
 
 
16
#include "COLLADASWPrerequisites.h"
 
17
#include "COLLADASWColor.h"
 
18
 
 
19
#include <fstream>
 
20
#include <stack>
 
21
#include <list>
 
22
#include <vector>
 
23
#include <limits>
 
24
#include <algorithm>
 
25
#include <string.h>
 
26
 
 
27
#define WHITESPACESTRINGLENGTH 1000
 
28
 
 
29
namespace COLLADABU
 
30
{
 
31
    class NativeString;
 
32
}
 
33
 
 
34
namespace Common
 
35
{
 
36
        class FWriteBufferFlusher;
 
37
        class CharacterBuffer;
 
38
}
 
39
 
 
40
namespace COLLADASW
 
41
{
 
42
 
 
43
    class StreamWriter;
 
44
 
 
45
        typedef unsigned long ElementIndexType;
 
46
 
 
47
 
 
48
    /** Class that simplifies closes open elements*/
 
49
    class TagCloser
 
50
    {
 
51
 
 
52
    public:
 
53
        TagCloser();
 
54
 
 
55
                /** We use the default copy ctor.*/
 
56
        ~TagCloser();
 
57
 
 
58
        /** Closes all elements that have been open and not closed, since this object has been instantiated*/
 
59
        void close();
 
60
 
 
61
        TagCloser & operator= ( const TagCloser & other );
 
62
 
 
63
    private:
 
64
        friend class StreamWriter;
 
65
        TagCloser ( StreamWriter * streamWriter, ElementIndexType elementIndex );
 
66
 
 
67
        StreamWriter * mStreamWriter;
 
68
                ElementIndexType mElementIndex; //!< the index of the open tag
 
69
    };
 
70
 
 
71
 
 
72
    /** A class to write a COLLADASW document directly to a file, without storing the data in an internal data model*/
 
73
    class StreamWriter
 
74
    {
 
75
        public:
 
76
                enum COLLADAVersion
 
77
                {
 
78
                        COLLADA_1_4_1,
 
79
                        COLLADA_1_5_0
 
80
                };
 
81
    private:
 
82
 
 
83
        /** Contains information about an open tag*/
 
84
        struct OpenTag
 
85
        {
 
86
            OpenTag ( const String* name, ElementIndexType elementIndex ) 
 
87
                                : mName ( name )
 
88
                                , mElementIndex( elementIndex )
 
89
                                , mHasContents( false )
 
90
                                , mHasText ( false ) 
 
91
                        {}
 
92
 
 
93
            const String* mName;            //!< The name of the tag
 
94
                        ElementIndexType mElementIndex; //!< the index of the open tag
 
95
            bool mHasContents;              //!< true, if contents, i.e. elements or text has been added to the element
 
96
            bool mHasText;                  //!< true, if text has been added to the element
 
97
        };
 
98
 
 
99
                typedef std::deque<OpenTag> OpenTagStack;
 
100
 
 
101
    private:
 
102
                Common::FWriteBufferFlusher* mBufferFlusher;
 
103
 
 
104
                Common::CharacterBuffer* mCharacterBuffer;
 
105
 
 
106
        /** If true, the double values will be exported with a maximum precision of 20 digits. */
 
107
        bool mDoublePrecision;
 
108
 
 
109
        OpenTagStack mOpenTags;  //!< A stack that holds all the open tags.
 
110
 
 
111
        size_t mLevel;
 
112
 
 
113
        size_t mIndent;
 
114
 
 
115
                static const int FWRITEBUFFERSIZE;
 
116
 
 
117
                static const int CHARACTERBUFFERSIZE;
 
118
 
 
119
        static const String mWhiteSpaceString;
 
120
 
 
121
        friend class TagCloser;
 
122
 
 
123
                /** Each element gets a continuous index. This is the index the next opened element gets.*/
 
124
                ElementIndexType mNextElementIndex;
 
125
 
 
126
                /** The version of the COLLADA file.*/
 
127
                COLLADAVersion mCOLLADAVersion;
 
128
 
 
129
    public:
 
130
        /** Creates a stream writer that writes to file @a fileName*/
 
131
        StreamWriter ( const NativeString& fileName, bool doublePrecision = false, COLLADAVersion cOLLADAVersion = COLLADA_1_4_1);
 
132
 
 
133
        /** Closes all open tags and closes the stream*/
 
134
        ~StreamWriter();
 
135
 
 
136
        /** Writes the document start, including the opening @a \<COLLADASW\> element.
 
137
        This member must be called, before any other member function can be called.*/
 
138
        void startDocument();
 
139
 
 
140
        /** Closes all open tags*/
 
141
        void endDocument();
 
142
 
 
143
        /** Adds the attribute @a name with value "#" plus @a value to the last opened element.
 
144
        This function can only be called after openElement() before any content is written.*/
 
145
        void appendURIAttribute ( const String& name, const URI& value );
 
146
 
 
147
        /** Adds the attribute @a name with value @a value to the last opened element.
 
148
        This function can only be called after openElement() before any content is written.*/
 
149
        void appendAttribute ( const String& name, const String& value );
 
150
 
 
151
        /** Adds the attribute @a name with value @a value to the last opened element.
 
152
        This function can only be called after openElement() before any content is written.*/
 
153
        void appendAttribute ( const String& name, const double value );
 
154
 
 
155
        /** Adds the attribute @a name with value @a value to the last opened element.
 
156
        This function can only be called after openElement() before any content is written.*/
 
157
        void appendAttribute ( const String& name, const unsigned long value );
 
158
 
 
159
        /** Adds the attribute @a name with value @a value to the last opened element.
 
160
        This function can only be called after openElement() before any content is written.*/
 
161
        void appendAttribute ( const String& name, const unsigned int value );
 
162
 
 
163
        /** Adds the attribute @a name with value @a value to the last opened element.
 
164
        This function can only be called after openElement() before any content is written.*/
 
165
        void appendAttribute ( const String& name, const int value );
 
166
 
 
167
        /** Adds @a text to the COLLADASW file.
 
168
        No checks are performed, if @a text contains forbidden characters. */
 
169
        void appendText ( const String& text );
 
170
 
 
171
        /** Adds @a text to the COLLADASW file in a new line.
 
172
        No checks are performed, if @a text contains forbidden characters. */
 
173
        void appendTextBlock ( const String& text );
 
174
 
 
175
        /** Adds @a number to the COLLADASW file.*/
 
176
        void appendValues ( const double number );
 
177
 
 
178
        /** Adds @a number1 and @a number2 to the COLLADASW file.*/
 
179
        void appendValues ( const double number1, const double number2 );
 
180
 
 
181
        /** Adds @a number1, @a number2 and @a number3 to the COLLADASW file.*/
 
182
        void appendValues ( const double number1, const double number2, const double number3 );
 
183
 
 
184
        /** Adds @a number1, @a number2, @a number3 and @a number4 to the COLLADASW file.*/
 
185
        void appendValues ( const double number1, const double number2, const double number3, const double number4 );
 
186
 
 
187
        /** Adds @a number to the COLLADASW file.*/
 
188
        void appendValues ( const float number );
 
189
 
 
190
        /** Adds @a number1 and @a number2 to the COLLADASW file.*/
 
191
        void appendValues ( const float number1, const float number2 );
 
192
 
 
193
        /** Adds @a number1, @a number2 and @a number3 to the COLLADASW file.*/
 
194
        void appendValues ( const float number1, const float number2, const float number3 );
 
195
 
 
196
        /** Adds @a number1, @a number2, @a number3 and @a number4 to the COLLADASW file.*/
 
197
        void appendValues ( const float number1, const float number2, const float number3, const float number4 );
 
198
 
 
199
        /** Adds all values in the array to the COLLADASW file.*/
 
200
        void appendValues ( const float values[], const size_t length );
 
201
 
 
202
        /** Adds all values in the array to the COLLADASW file.*/
 
203
        void appendValues ( const int values[], const size_t length );
 
204
 
 
205
        /** Adds all values in the array to the COLLADASW file.*/
 
206
        void appendValues ( const double values[], const size_t length );
 
207
 
 
208
        /** Adds all values in the array to the COLLADASW file.*/
 
209
        void appendValues ( const float matrix[4][4] );
 
210
 
 
211
        /** Adds all values in the array to the COLLADASW file.*/
 
212
        void appendValues ( const double matrix[4][4] );
 
213
 
 
214
        /** Adds all values in the array to the COLLADASW file.*/
 
215
        void appendValues ( const std::vector<float>& values );
 
216
 
 
217
        /** Adds all values in the array to the COLLADASW file.*/
 
218
        void appendValues ( const std::vector<double>& values );
 
219
 
 
220
        /** Adds all values in the array to the COLLADASW file.*/
 
221
        void appendValues ( const std::vector<String>& values );
 
222
 
 
223
        /** Adds all values in the array to the COLLADASW file.*/
 
224
        void appendValues ( const std::vector<unsigned long>& values );
 
225
 
 
226
        /** Adds @a number to the COLLADASW file.*/
 
227
        void appendValues ( int number );
 
228
 
 
229
        /** Adds @a number to the COLLADASW file.*/
 
230
        void appendValues ( int number, int number2 );
 
231
 
 
232
        /** Adds @a number to the COLLADASW file.*/
 
233
        void appendValues ( unsigned int number );
 
234
 
 
235
        /** Adds @a number to the COLLADASW file.*/
 
236
        void appendValues ( unsigned int number, unsigned int number2 );
 
237
 
 
238
                /** Adds @a number to the COLLADASW file.*/
 
239
                void appendValues ( long number );
 
240
 
 
241
                /** Adds @a number1 and @a number2 to the COLLADASW file.*/
 
242
                void appendValues ( long  number1, long  number2 );
 
243
 
 
244
                /** Adds @a number1, @a number2 and @a number3 to the COLLADASW file.*/
 
245
                void appendValues ( long  number1, long  number2, long  number3 );
 
246
 
 
247
                /** Adds @a number1, @a number2, @a number3 and @a number4 to the COLLADASW file.*/
 
248
                void appendValues ( long  number1, long  number2, long  number3, long  number4 );
 
249
 
 
250
                /** Adds @a number to the COLLADASW file.*/
 
251
                void appendValues ( unsigned long number );
 
252
 
 
253
                /** Adds @a number1 and @a number2 to the COLLADASW file.*/
 
254
                void appendValues ( unsigned long number1, unsigned long number2 );
 
255
 
 
256
                /** Adds @a number1, @a number2 and @a number3 to the COLLADASW file.*/
 
257
                void appendValues ( unsigned long number1, unsigned long number2, unsigned long number3 );
 
258
 
 
259
                /** Adds @a number1, @a number2, @a number3 and @a number4 to the COLLADASW file.*/
 
260
                void appendValues ( unsigned long number1, unsigned long number2, unsigned long number3, unsigned long number4 );
 
261
 
 
262
                /** Adds @a number to the COLLADASW file.*/
 
263
                void appendValues ( long long number );
 
264
 
 
265
                /** Adds @a number1 and @a number2 to the COLLADASW file.*/
 
266
                void appendValues ( long long number1, long long number2 );
 
267
 
 
268
                /** Adds @a number1, @a number2 and @a number3 to the COLLADASW file.*/
 
269
                void appendValues ( long long number1, long long number2, long long number3 );
 
270
 
 
271
                /** Adds @a number1, @a number2, @a number3 and @a number4 to the COLLADASW file.*/
 
272
                void appendValues ( long long number1, long long number2, long long number3, long long number4 );
 
273
 
 
274
                /** Adds @a number to the COLLADASW file.*/
 
275
                void appendValues ( unsigned long long number );
 
276
 
 
277
                /** Adds @a number1 and @a number2 to the COLLADASW file.*/
 
278
                void appendValues ( unsigned long long number1, unsigned long long number2 );
 
279
 
 
280
                /** Adds @a number1, @a number2 and @a number3 to the COLLADASW file.*/
 
281
                void appendValues ( unsigned long long number1, unsigned long long number2, unsigned long long number3 );
 
282
 
 
283
                /** Adds @a number1, @a number2, @a number3 and @a number4 to the COLLADASW file.*/
 
284
                void appendValues ( unsigned long long number1, unsigned long long number2, unsigned long long number3, unsigned long long number4 );
 
285
 
 
286
        /** Adds the float @a number to the COLLADASW file.*/
 
287
        void appendValues ( bool value );
 
288
 
 
289
        /** Adds the float @a number to the COLLADASW file.*/
 
290
        void appendValues ( const Color& value );
 
291
 
 
292
                /** Adds @a text to the COLLADASW file.
 
293
                No checks are performed, if @a text contains forbidden characters. */
 
294
                void appendValues ( const String& text );
 
295
 
 
296
                /** Adds @a text to the COLLADASW file.
 
297
                No checks are performed, if @a text contains forbidden characters. */
 
298
                void appendValues ( const char* text );
 
299
 
 
300
                /** Adds @a text to the COLLADASW file.
 
301
                No checks are performed, if @a text contains forbidden characters. */
 
302
                void appendValues ( const char* text, size_t length );
 
303
 
 
304
        /** Opens a new element with the name @a name.
 
305
        The string must persist at least until the corresponding closeElement() member is called.*/
 
306
        TagCloser openElement ( const String& name );
 
307
 
 
308
        /** Closes the previously opened element. */
 
309
        void closeElement();
 
310
 
 
311
        /** Adds an element with name @a elementName that contains only @a text.*/
 
312
        void appendTextElement ( const String& elementName, const String& text );
 
313
 
 
314
        /** Adds an element with name @a elementName that contains only @a text.*/
 
315
        void appendURIElement ( const String& elementName, const URI& text );
 
316
 
 
317
                /** Returns the version of the COLLADA file that ias written by the StreamWriter.*/
 
318
                COLLADAVersion getCOLLADAVersion() const { return mCOLLADAVersion; } 
 
319
 
 
320
    private:
 
321
 
 
322
                /** Closes all elements opened since the element with index @a elementIndex has been open, 
 
323
                including the element itself. */
 
324
                void closeElements( ElementIndexType elementIndex );
 
325
 
 
326
                /** Adds the string @a str to the stream.*/
 
327
                inline void appendString ( const String & str )
 
328
                {
 
329
                        appendNCNameString(str);
 
330
                }
 
331
 
 
332
                /** Adds the string @a str to the stream.*/
 
333
                inline void appendString ( const char* text )
 
334
                {
 
335
                        appendString(text, strlen(text));
 
336
                }
 
337
 
 
338
                /** Adds the string @a str to the stream.*/
 
339
                void appendString ( const char* text, size_t length );
 
340
 
 
341
        /** Adds the string @a str to the stream.
 
342
            The string have to be a valid ncname. */
 
343
        void appendNCNameString ( const String & str );
 
344
 
 
345
        /** Adds the first @n characters of string @a str to the stream.
 
346
        @a n must not be larger than the length of @a str.*/
 
347
        void appendNCNameString ( const String & str, size_t n );
 
348
 
 
349
 
 
350
        /** Adds the char @a c to the stream*/
 
351
        void appendChar ( char c );
 
352
 
 
353
        /** Adds the double @a number to the stream*/
 
354
        void appendNumber ( double number );
 
355
 
 
356
        /** Adds the float @a number to the stream*/
 
357
        void appendNumber ( float number );
 
358
 
 
359
        /** Adds the long @a number to the stream*/
 
360
        void appendNumber ( int number );
 
361
 
 
362
        /** Adds the long @a number to the stream*/
 
363
        void appendNumber ( unsigned int number );
 
364
 
 
365
                /** Adds the long @a number to the stream*/
 
366
                void appendNumber ( long number );
 
367
 
 
368
                /** Adds the long @a number to the stream*/
 
369
                void appendNumber ( unsigned long number );
 
370
 
 
371
                /** Adds the long long @a number to the stream*/
 
372
                void appendNumber ( long long number );
 
373
 
 
374
                /** Adds the long long @a number to the stream*/
 
375
                void appendNumber ( unsigned long long number );
 
376
 
 
377
        /** Adds the bool @a value to the stream*/
 
378
        void appendBoolean ( bool value );
 
379
 
 
380
        /** Adds a new line to the stream*/
 
381
        inline void appendNewLine()
 
382
        {
 
383
            appendChar ( '\n' );
 
384
        }
 
385
 
 
386
        /** Adds @a number white spaces to the stream*/
 
387
        void addWhiteSpace (  size_t number );
 
388
 
 
389
    private:
 
390
 
 
391
        /** This function prepares the last opened tag to add contents to it.
 
392
        This function must be called before any contents is added to an element. After this function
 
393
        has been calls, contents should be added, but if not, the xml file will still be valid*/
 
394
        void prepareToAddContents();
 
395
 
 
396
    };
 
397
 
 
398
 
 
399
} //namespace COLLADASW
 
400
 
 
401
 
 
402
#endif //__COLLADASTREAMWRITER_H__