File: test0.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 (303 lines) | stat: -rw-r--r-- 8,597 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/****************************************************************************
** 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: test0.cpp 1078 2005-03-03 13:13:04Z robux4 $
    \author Steve Lhomme     <robux4 @ users.sf.net>
		Test the EBML write possibilities
		\see http://www.cl.cam.ac.uk/~mgk25/unicode.html
		\see http://www-106.ibm.com/developerworks/unicode/library/u-encode.html
*/

#include <stdio.h>

#include "StdIOCallback.hpp"

#include "EbmlUInteger.hpp"
#include "EbmlSInteger.hpp"
#include "EbmlBinary.hpp"
#include "EbmlString.hpp"
#include "EbmlUnicodeString.hpp"
#include "EbmlMaster.hpp"
#include "EbmlFloat.hpp"
#include "EbmlStream.hpp"

using namespace LIBMATROSKA_NAMESPACE;

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

enum SemanticType {
	EBML_U_INTEGER,
	EBML_S_INTEGER,
	EBML_BINARY,
	EBML_STRING,
	EBML_STRING_UNICODE,
	EBML_FLOAT,
	EBML_MASTER,
};

typedef binary EBMLID[4];
struct Semantic {
	EBMLID Id;
	int IdLength;
	SemanticType Type;
};

struct Semantic SemanticList[] = 
{
	{{0x42, 0x45},             2, EBML_U_INTEGER},
	{{0x1A, 0x45, 0x33, 0x80}, 4, EBML_S_INTEGER},
	{{0x87},                   1, EBML_BINARY},
	{{0x22, 0x33, 0x44},       3, EBML_STRING},
	{{0x44, 0x33},             2, EBML_STRING_UNICODE},
	{{0x50, 0x50},             2, EBML_FLOAT},
	{{0x36, 0x47, 0x58},       3, EBML_MASTER},
};

/*!
    \test Some test on the Cluster use
    \todo render the Cluster to a file
*/
int main(void)
{
	StdIOCallback Ebml_file(RW_FILENAME, ::MODE_CREATE);
	
	///// Writing test

	///////////////////////////////
	//   Unsigned integer
	///////////////////////////////
	
	EbmlUInteger testUInt(4); // supposed to hold a 4*8 bits value
	
	testUInt.SetID(SemanticList[0].Id, SemanticList[0].IdLength);
	testUInt = 52;
	testUInt.SetSizeLength(3); // size should be coded on at least 3 octets
	testUInt.Render(Ebml_file);

	///////////////////////////////
	//   Signed integer
	///////////////////////////////
	
	EbmlSInteger testSInt(4); // supposed to hold a 4*8 bits value

	testSInt.SetID(SemanticList[1].Id, SemanticList[1].IdLength);
	testSInt = -20;
	testSInt.Render(Ebml_file);

	///////////////////////////////
	//   Binary data
	///////////////////////////////
	const int BINARY_SIZE=3000;
	
	binary *bin = new binary[BINARY_SIZE];
	memset(bin, 0x61, BINARY_SIZE);
	EbmlBinary testBin;

	testBin.SetID(SemanticList[2].Id, SemanticList[2].IdLength);
	testBin.SetBuffer(bin, BINARY_SIZE);
	testBin.Render(Ebml_file);

	///////////////////////////////
	//   String data
	///////////////////////////////
	std::string aString = "Hello World !";
	EbmlString testStr(200);

	testStr.SetID(SemanticList[3].Id, SemanticList[3].IdLength);
	testStr = aString;
	testStr.Render(Ebml_file);

	///////////////////////////////
	//   Master element
	///////////////////////////////
	EbmlMaster testMaster;

	testMaster.SetID(SemanticList[6].Id, SemanticList[6].IdLength);
	testMaster.PushElement(testStr);
	testMaster.PushElement(testUInt);
	testMaster.Render(Ebml_file);

	///////////////////////////////
	//   Unicode String data
	///////////////////////////////
	UTFstring bString = L"Stve Lhomm";
	EbmlUnicodeString testUStr(200);

	testUStr.SetID(SemanticList[4].Id, SemanticList[4].IdLength);
	testUStr = bString;
	testUStr.Render(Ebml_file);

	///////////////////////////////
	//   Float data
	///////////////////////////////
	EbmlFloat testFloat(EbmlFloat::FLOAT_32);

	testFloat.SetID(SemanticList[5].Id, SemanticList[5].IdLength);
	testFloat.SetPrecision(EbmlFloat::FLOAT_32);
	testFloat = 1.01234567890123456;
	testFloat.Render(Ebml_file);
	
	testFloat.SetPrecision(EbmlFloat::FLOAT_64);
	testFloat = -1.01234567890123456L;
	testFloat.Render(Ebml_file);
	
	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;
	
	// read the data until a possible element is found (valid ID + size combination)
	ElementLevel0 = aStream.FindNextID(0xFFFFFFFFL, false);
	printf("Read EBML elements & skip data\n");
	while (ElementLevel0 != NULL)
	{
		printf("ID : ");
		for (int i=0; i<ElementLevel0->GetIDLength(); i++)
		{
			printf("[%02X]", ElementLevel0->GetID()[i]);
		}
		printf("\n");

		ElementLevel0->SkipData(Ebml_Wfile);
		if (ElementLevel0 != NULL)
			delete ElementLevel0;
	
		ElementLevel0 = aStream.FindNextID(0xFFFFFFFFL, false);
	}
	
	// example 2 evaluation of all elements found
	EbmlStream bStream(Ebml_Wfile);
	EbmlElement * EvaledElementLevel0;
//	EbmlElement * EvaledElementLevel1;
	
	// reset the stream to the beggining
	Ebml_Wfile.setFilePointer(0);
	
	// list of all IDs and their semantic type
//	std::list<struct Semantic> SemanticList;
//	SemanticList.push_back();
	
	ElementLevel0 = aStream.FindNextID(0xFFFFFFFFL, false);
	printf("Read EBML elements & evaluate data\n");
	while (ElementLevel0 != NULL)
	{
		int i;
		printf("ID : ");
		for (i=0; i<ElementLevel0->GetIDLength(); i++)
		{
			printf("[%02X]", ElementLevel0->GetID()[i]);
		}

		// check if the element is known
		for (i=0; i<countof(SemanticList); i++) {
			if (ElementLevel0->GetIDLength() != SemanticList[i].IdLength)
				continue;
			if (memcmp(SemanticList[i].Id, ElementLevel0->GetID(), SemanticList[i].IdLength) == 0)
				break;
		}
		/// \todo check if it is known in the context
		// handle the data inside the element
		if (i < countof(SemanticList)) {
			switch (SemanticList[i].Type)
			{
				case EBML_U_INTEGER:
					EvaledElementLevel0 = new EbmlUInteger(*ElementLevel0);
					break;
				case EBML_S_INTEGER:
					EvaledElementLevel0 = new EbmlSInteger(*ElementLevel0);
					break;
				case EBML_BINARY:
					EvaledElementLevel0 = new EbmlBinary(*ElementLevel0);
					break;
				case EBML_STRING:
					EvaledElementLevel0 = new EbmlString(*ElementLevel0);
					break;
				case EBML_STRING_UNICODE:
					EvaledElementLevel0 = new EbmlUnicodeString(*ElementLevel0);
					break;
				case EBML_FLOAT:
					EvaledElementLevel0 = new EbmlFloat(*ElementLevel0);
					break;
				case EBML_MASTER:
					EvaledElementLevel0 = new EbmlMaster(*ElementLevel0);
					break;
			}

			EvaledElementLevel0->ReadData(Ebml_Wfile);

			switch (SemanticList[i].Type)
			{
				case EBML_U_INTEGER:
					printf(" : %d", uint32(*(EbmlUInteger*)EvaledElementLevel0));
					break;
				case EBML_S_INTEGER:
					printf(" : %d", int32(*(EbmlSInteger*)EvaledElementLevel0));
					break;
				case EBML_BINARY:
					printf(" : binary data, size = %ld", (*(EbmlBinary*)EvaledElementLevel0).GetSize());
					printf(" [%02X]", binary(*(EbmlBinary*)EvaledElementLevel0));
					break;
				case EBML_STRING:
					printf(" : %s", std::string(*(EbmlString*)EvaledElementLevel0).data());
					break;
				case EBML_STRING_UNICODE:
					printf(" : (wide chars) %ls", UTFstring(*(EbmlUnicodeString*)EvaledElementLevel0).data());
					break;
				case EBML_FLOAT:
					printf(" : %f / %.15lf", float(*(EbmlFloat*)EvaledElementLevel0), double(*(EbmlFloat*)EvaledElementLevel0));
					break;
				case EBML_MASTER:
					printf(" : unsupported format 'Master'");
					break;
			}
			delete EvaledElementLevel0;
		} else {
			ElementLevel0->SkipData(Ebml_Wfile);
		}
		if (ElementLevel0 != NULL)
			delete ElementLevel0;

		printf("\n");
	
		ElementLevel0 = aStream.FindNextID(0xFFFFFFFFL, false);
	}
	
	Ebml_Wfile.close();

	return 0;
}