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: EbmlString.cpp 1079 2005-03-03 13:18:14Z robux4 $
34	\author Steve Lhomme     <robux4 @ users.sf.net>
35*/
36#include <cassert>
37
38#include "ebml/EbmlString.h"
39
40START_LIBEBML_NAMESPACE
41
42EbmlString::EbmlString()
43 :EbmlElement(0, false)
44{
45	DefaultSize = 0;
46/* done automatically
47	Size = Value.length();
48	if (DefaultSize > Size)
49		Size = DefaultSize;*/
50}
51
52EbmlString::EbmlString(const std::string & aDefaultValue)
53 :EbmlElement(0, true), Value(aDefaultValue), DefaultValue(aDefaultValue)
54{
55	DefaultSize = 0;
56	DefaultIsSet = true;
57/* done automatically
58	Size = Value.length();
59	if (DefaultSize > Size)
60		Size = DefaultSize;*/
61}
62
63/*!
64	\todo Cloning should be on the same exact type !
65*/
66EbmlString::EbmlString(const EbmlString & ElementToClone)
67 :EbmlElement(ElementToClone)
68 ,Value(ElementToClone.Value)
69 ,DefaultValue(ElementToClone.DefaultValue)
70{
71}
72
73/*!
74	\todo handle exception on errors
75*/
76uint32 EbmlString::RenderData(IOCallback & output, bool bForceRender, bool bKeepIntact)
77{
78	uint32 Result;
79	output.writeFully(Value.c_str(), Value.length());
80	Result = Value.length();
81
82	if (Result < DefaultSize) {
83		// pad the rest with 0
84		binary *Pad = new binary[DefaultSize - Result];
85		if (Pad == NULL)
86		{
87			return Result;
88		}
89		memset(Pad, 0x00, DefaultSize - Result);
90		output.writeFully(Pad, DefaultSize - Result);
91		Result = DefaultSize;
92		delete [] Pad;
93	}
94
95	return Result;
96}
97
98EbmlString & EbmlString::operator=(const std::string NewString)
99{
100	Value = NewString;
101	bValueIsSet = true;
102/* done automatically
103	Size = Value.length();
104	if (DefaultSize > Size)
105		Size = DefaultSize;*/
106	return *this;
107}
108
109uint64 EbmlString::UpdateSize(bool bKeepIntact, bool bForceRender)
110{
111	if (!bKeepIntact && IsDefaultValue())
112		return 0;
113
114	if (Value.length() < DefaultSize) {
115		Size = DefaultSize;
116	} else {
117		Size = Value.length();
118	}
119	return Size;
120}
121
122uint64 EbmlString::ReadData(IOCallback & input, ScopeMode ReadFully)
123{
124	if (ReadFully != SCOPE_NO_DATA)
125	{
126		if (Size == 0) {
127			Value = "";
128			bValueIsSet = true;
129		} else {
130			char *Buffer = new char[Size + 1];
131			if (Buffer == NULL) {
132				// unable to store the data, skip it
133				input.setFilePointer(Size, seek_current);
134			} else {
135				input.readFully(Buffer, Size);
136				if (Buffer[Size-1] != '\0') {
137					Buffer[Size] = '\0';
138				}
139				Value = Buffer;
140				delete [] Buffer;
141				bValueIsSet = true;
142			}
143		}
144	}
145
146	return Size;
147}
148
149END_LIBEBML_NAMESPACE
150