1/****************************************************************************
2** libebml : parse EBML files, see http://embl.sourceforge.net/
3**
4** <file/class description>
5**
6** Copyright (C) 2002-2005 Steve Lhomme.  All rights reserved.
7**
8** This file is part of libebml.
9**
10** This library is free software; you can redistribute it and/or
11** modify it under the terms of the GNU Lesser General Public
12** License as published by the Free Software Foundation; either
13** version 2.1 of the License, or (at your option) any later version.
14**
15** This library is distributed in the hope that it will be useful,
16** but WITHOUT ANY WARRANTY; without even the implied warranty of
17** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18** Lesser General Public License for more details.
19**
20** You should have received a copy of the GNU Lesser General Public
21** License along with this library; if not, write to the Free Software
22** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23**
24** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
25**
26** Contact license@matroska.org if any conditions of this licensing are
27** not clear to you.
28**
29**********************************************************************/
30
31/*!
32	\file
33	\version \$Id: EbmlFloat.cpp 1243 2006-03-30 19:33:22Z mosu $
34	\author Steve Lhomme     <robux4 @ users.sf.net>
35*/
36
37#include <cassert>
38
39#include "ebml/EbmlFloat.h"
40
41START_LIBEBML_NAMESPACE
42
43EbmlFloat::EbmlFloat(const EbmlFloat::Precision prec)
44 :EbmlElement(0, false)
45{
46	SetPrecision(prec);
47}
48
49EbmlFloat::EbmlFloat(const double aDefaultValue, const EbmlFloat::Precision prec)
50 :EbmlElement(0, true), Value(aDefaultValue), DefaultValue(aDefaultValue)
51{
52	DefaultIsSet = true;
53	SetPrecision(prec);
54}
55
56EbmlFloat::EbmlFloat(const EbmlFloat & ElementToClone)
57 :EbmlElement(ElementToClone)
58 ,Value(ElementToClone.Value)
59 ,DefaultValue(ElementToClone.DefaultValue)
60{
61}
62
63/*!
64	\todo handle exception on errors
65	\todo handle 10 bits precision
66*/
67uint32 EbmlFloat::RenderData(IOCallback & output, bool bForceRender, bool bKeepIntact)
68{
69	assert(Size == 4 || Size == 8);
70
71	if (Size == 4) {
72		float val = Value;
73		int Tmp;
74		memcpy(&Tmp, &val, 4);
75		big_int32 TmpToWrite(Tmp);
76		output.writeFully(&TmpToWrite.endian(), Size);
77	} else if (Size == 8) {
78		double val = Value;
79		int64 Tmp;
80		memcpy(&Tmp, &val, 8);
81		big_int64 TmpToWrite(Tmp);
82		output.writeFully(&TmpToWrite.endian(), Size);
83	}
84
85	return Size;
86}
87
88uint64 EbmlFloat::UpdateSize(bool bKeepIntact, bool bForceRender)
89{
90	if (!bKeepIntact && IsDefaultValue())
91		return 0;
92	return Size;
93}
94
95/*!
96	\todo remove the hack for possible endianess pb (test on little & big endian)
97*/
98uint64 EbmlFloat::ReadData(IOCallback & input, ScopeMode ReadFully)
99{
100	if (ReadFully != SCOPE_NO_DATA)
101	{
102		binary Buffer[20];
103		assert(Size <= 20);
104		input.readFully(Buffer, Size);
105
106		if (Size == 4) {
107			big_int32 TmpRead;
108			TmpRead.Eval(Buffer);
109			int32 tmpp = int32(TmpRead);
110			float val;
111			memcpy(&val, &tmpp, 4);
112			Value = val;
113			bValueIsSet = true;
114		} else if (Size == 8) {
115			big_int64 TmpRead;
116			TmpRead.Eval(Buffer);
117			int64 tmpp = int64(TmpRead);
118			double val;
119			memcpy(&val, &tmpp, 8);
120			Value = val;
121			bValueIsSet = true;
122		}
123	}
124
125	return Size;
126}
127
128END_LIBEBML_NAMESPACE
129