File: test00.cpp

package info (click to toggle)
libmatroska 0.8.0-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,072 kB
  • ctags: 3,018
  • sloc: cpp: 8,470; makefile: 353; ansic: 157; sh: 14
file content (250 lines) | stat: -rw-r--r-- 8,856 bytes parent folder | download | duplicates (3)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/****************************************************************************
** libmatroska : parse Matroska files, see http://www.matroska.org/
**
** <file/class description>
**
** Copyright (C) 2002-2004 Steve Lhomme.  All rights reserved.
**
** This file is part of libmatroska.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding an other license may use this file in accordance with 
** the Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.matroska.org/license/qpl/ for QPL licensing information.
** See http://www.matroska.org/license/gpl/ for GPL licensing information.
**
** Contact license@matroska.org if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

/*!
    \file
    \version \$Id: test00.cpp 1078 2005-03-03 13:13:04Z robux4 $
    \author Steve Lhomme     <robux4 @ users.sf.net>
		Test the writing a basic EBML file
*/

#include <stdio.h>
#include <string>

#include "StdIOCallback.h"

#include "EbmlHead.h"
#include "EbmlSubHead.h"
#include "EbmlStream.h"
#include "EbmlVoid.h"
#include "EbmlContexts.h"
#include "KaxSegment.h"
#include "KaxAttachments.h"
#include "KaxAttached.h"
#include "KaxContexts.h"

#include "EbmlVersion.h"
#include "KaxVersion.h"

using namespace LIBMATROSKA_NAMESPACE;

const char RW_FILENAME[] = "test.kax";

#undef SKIP_ATTACHED

/*!
    \test Some test on the Cluster use
		\todo each Master element should have a context !
		\todo move the base EBML classes to a template of each type ? (defining the default value and the ID)
		\todo find another way than using all those static_cast (using a template ?)
*/
int main(void)
{
	printf("Test00 :\nEBML %s %s / Matroska %s %s\n", EbmlCodeVersion.c_str(), EbmlCodeDate.c_str(), KaxCodeVersion.c_str(), KaxCodeDate.c_str());
	StdIOCallback Ebml_file(RW_FILENAME, ::MODE_CREATE);
	
	///// Writing test
	EbmlHead TestHead;

	printf("Write EBML + Matroska elements\n");
	EDocType & MyDocType = GetChild<EDocType>(TestHead);
	*static_cast<EbmlString *>(&MyDocType) = "matroska";

	EDocTypeVersion & MyDocTypeVer = GetChild<EDocTypeVersion>(TestHead);
	*(static_cast<EbmlUInteger *>(&MyDocTypeVer)) = 1;

	EDocTypeReadVersion & MyDocTypeReadVer = GetChild<EDocTypeReadVersion>(TestHead);
	*(static_cast<EbmlUInteger *>(&MyDocTypeReadVer)) = 1;

	TestHead.Render(Ebml_file);

	KaxSegment FirstSegment;
	// size is unknown and will always be, we can render it right away
	FirstSegment.Render(Ebml_file);

	KaxAttachments * pAllAttachments = static_cast<KaxAttachments *>(FirstSegment.FindFirstElt(KaxAttachments::ClassInfos, true));
	if (pAllAttachments == NULL)
		return -1;
	pAllAttachments->SetSizeInfinite();
	// size is unknown and will always be, we can render it right away
	pAllAttachments->Render(Ebml_file);
	
	KaxAttached * pAttachment1 = static_cast<KaxAttached *>(pAllAttachments->FindFirstElt(KaxAttached::ClassInfos, true));
	if (pAttachment1 == NULL)
		return -1;
	KaxFileName * pFileName1 = static_cast<KaxFileName *>(pAttachment1->FindFirstElt(KaxFileName::ClassInfos, true));
	if (pFileName1 == NULL)
		return -1;
	*static_cast<EbmlUnicodeString *>(pFileName1) = "file1.txt";
	KaxFileData * pFileData1 = static_cast<KaxFileData *>(pAttachment1->FindFirstElt(KaxFileData::ClassInfos, true));
	if (pFileData1 == NULL)
		return -1;
	char Buffer1[] = "Ah ah ah !";
	static_cast<EbmlBinary *>(pFileData1)->SetBuffer((const binary*) Buffer1, countof(Buffer1));
	// should produce an error if the size is not infinite and the data has been rendered
	pAttachment1->Render(Ebml_file);
	
	KaxAttached * pAttachment2 = static_cast<KaxAttached *>(pAllAttachments->AddNewElt(KaxAttached::ClassInfos));
	if (pAttachment2 == NULL)
		return -1;
	KaxFileName * pFileName2 = static_cast<KaxFileName *>(pAttachment2->FindFirstElt(KaxFileName::ClassInfos, true));
	if (pFileName2 == NULL)
		return -1;
	*static_cast<EbmlUnicodeString *>(pFileName2) = "file2.txt";
	// Add a void element (data is discarded)
	EbmlVoid * pVoid = static_cast<EbmlVoid *>(pAttachment2->FindFirstElt(EbmlVoid::ClassInfos, true));
	if (pVoid == NULL)
		return -1;
	static_cast<EbmlBinary *>(pVoid)->SetBuffer((const binary*) Buffer1, countof(Buffer1));
	pAttachment2->Render(Ebml_file);

#ifdef SKIP_ATTACHED
	KaxSegment SecondSegment;
	// size is unknown and will always be, we can render it right away
	SecondSegment.Render(Ebml_file);
#endif // SKIP_ATTACHED

	Ebml_file.close();

	///// Reading test
	StdIOCallback Ebml_Wfile(RW_FILENAME, ::MODE_READ);
	
	// example 1 skip all the elements found
	EbmlStream aStream(Ebml_Wfile);
	EbmlElement * ElementLevel0;
	EbmlElement * ElementLevel1;
	EbmlElement * ElementLevel2;
	EbmlElement * ElementLevel3;
	
	// read the data until a possible element is found (valid ID + size combination)
	printf("Read EBML elements & skip data\n");
	// find the EBML head in the file
	ElementLevel0 = aStream.FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL, false);
	if (ElementLevel0 != NULL)
	{
		printf("ID : ");
		for (unsigned int i=0; i<EbmlId(*ElementLevel0).Length; i++)
		{
			printf("[%02X]", EbmlId(*ElementLevel0).Value[i]);
		}
		printf("\n");

		ElementLevel0->SkipData(aStream, EbmlHead_Context);
		if (ElementLevel0 != NULL)
			delete ElementLevel0;
	}

	// example to read attachements in the file
	ElementLevel0 = aStream.FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL, false);
	while (ElementLevel0 != NULL)
	{
		printf("ID : ");
		for (unsigned int i=0; i<EbmlId(*ElementLevel0).Length; i++)
		{
			printf("[%02X]", EbmlId(*ElementLevel0).Value[i]);
		}
		printf("\n");

		int bUpperElement = 0;

		ElementLevel1 = aStream.FindNextID(KaxSegment_Context, bUpperElement, 0xFFFFFFFFL, true);

		while (ElementLevel1 != NULL) {
			/// \todo switch the type of the element to check if it's one we want to handle, like attachements
			if (EbmlId(*ElementLevel1) == KaxAttachments::ClassInfos.GlobalId) {
				printf("Attachments detected\n");

				ElementLevel2 = aStream.FindNextID(KaxAttachments_Context, bUpperElement, 0xFFFFFFFFL, true);
				while (ElementLevel2 != NULL) {
					/// \todo switch the type of the element to check if it's one we want to handle, like attachements
					if (EbmlId(*ElementLevel2) == KaxAttached::ClassInfos.GlobalId) {
						printf("Attached file detected\n");
					}
#ifdef SKIP_ATTACHED
					ElementLevel2 = ElementLevel2->SkipData(aStream, KaxAttached_Context);
					if (ElementLevel2 == NULL) {
						ElementLevel2 = aStream.FindNextID(KaxAttachments_Context, bUpperElement, 0xFFFFFFFFL, true);

						if (bUpperElement) {
							printf("Upper level1 element found\n");
							delete ElementLevel1;
							ElementLevel1 = ElementLevel2;
							ElementLevel2 = NULL;
						}
					}
#else // SKIP_ATTACHED
					// Display the filename (if it exists)
					ElementLevel3 = aStream.FindNextID(KaxAttached_Context, bUpperElement, 0xFFFFFFFFL, false);
					while (ElementLevel3 != NULL) {
						/// \todo switch the type of the element to check if it's one we want to handle, like attachements
						if (EbmlId(*ElementLevel3) == KaxFileName::ClassInfos.GlobalId) {
							KaxFileName & tmp = *static_cast<KaxFileName*>(ElementLevel3);
							tmp.ReadData(aStream.I_O());
							printf("File Name = %ls\n", UTFstring(tmp).c_str());
						} else {
							ElementLevel3->SkipData(aStream, KaxAttached_Context);
						}
						delete ElementLevel3;
						ElementLevel3 = aStream.FindNextID(KaxAttached_Context, bUpperElement, 0xFFFFFFFFL, false);
						if (bUpperElement)
							break;
					}

					if (bUpperElement) {
						delete ElementLevel2;
						ElementLevel2 = ElementLevel3;
					} else {
						ElementLevel2->SkipData(aStream, KaxAttached_Context);
						delete ElementLevel2;

						ElementLevel2 = aStream.FindNextID(KaxAttachments_Context, bUpperElement, 0xFFFFFFFFL, true);
					}
#endif // SKIP_ATTACHED
				}
			}
			ElementLevel1->SkipData(aStream, KaxAttachments_Context);
			delete ElementLevel1;
	
			ElementLevel1 = aStream.FindNextID(KaxSegment_Context, bUpperElement, 0xFFFFFFFFL, true);
		}

		ElementLevel0->SkipData(aStream, KaxSegment_Context);
		if (ElementLevel0 != NULL)
			delete ElementLevel0;
	
		ElementLevel0 = aStream.FindNextID(KaxSegment_Context, bUpperElement, 0xFFFFFFFFL, true);
	}

	Ebml_Wfile.close();

	return 0;
}