forked from Matroska-Org/libebml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEbmlString.cpp
More file actions
162 lines (138 loc) · 3.98 KB
/
EbmlString.cpp
File metadata and controls
162 lines (138 loc) · 3.98 KB
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
/****************************************************************************
** libebml : parse EBML files, see http://embl.sourceforge.net/
**
** <file/class description>
**
** Copyright (C) 2002-2010 Steve Lhomme. All rights reserved.
**
** This file is part of libebml.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** See http://www.gnu.org/licenses/lgpl-2.1.html for LGPL licensing information.
**
** Contact license@matroska.org if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
/*!
\file
\version \$Id$
\author Steve Lhomme <robux4 @ users.sf.net>
*/
#include <cassert>
#include <limits>
#include "ebml/EbmlString.h"
namespace libebml {
EbmlString::EbmlString()
:EbmlElement(0, false)
{
SetDefaultSize(0);
/* done automatically
SetSize_(Value.length());
if (GetDefaultSize() > GetSize())
SetSize_(GetDefaultSize());*/
}
EbmlString::EbmlString(const std::string & aDefaultValue)
:EbmlElement(0, true), Value(aDefaultValue), DefaultValue(aDefaultValue)
{
SetDefaultSize(0);
SetDefaultIsSet();
/* done automatically
SetSize_(Value.length());
if (GetDefaultSize() > GetSize())
SetSize_(GetDefaultSize());*/
}
/*!
\todo Cloning should be on the same exact type !
*/
void EbmlString::SetDefaultValue(std::string & aValue)
{
assert(!DefaultISset());
DefaultValue = aValue;
SetDefaultIsSet();
}
const std::string & EbmlString::DefaultVal() const
{
assert(DefaultISset());
return DefaultValue;
}
/*!
\todo handle exception on errors
*/
filepos_t EbmlString::RenderData(IOCallback & output, bool /* bForceRender */, bool /* bWithDefault */)
{
filepos_t Result;
output.writeFully(Value.c_str(), Value.length());
Result = Value.length();
if (Result < GetDefaultSize()) {
// pad the rest with 0
auto Pad = new (std::nothrow) binary[GetDefaultSize() - Result];
if (Pad == nullptr) {
return Result;
}
memset(Pad, 0x00, GetDefaultSize() - Result);
output.writeFully(Pad, GetDefaultSize() - Result);
Result = GetDefaultSize();
delete [] Pad;
}
return Result;
}
EbmlString::operator const std::string &() const {return Value;}
EbmlString & EbmlString::operator=(const std::string & NewString)
{
Value = NewString;
SetValueIsSet();
/* done automatically
SetSize_(Value.length());
if (GetDefaultSize() > GetSize())
SetSize_(GetDefaultSize());*/
return *this;
}
EbmlString &EbmlString::SetValue(std::string const &NewValue) {
return *this = NewValue;
}
std::string EbmlString::GetValue() const {
return Value;
}
uint64 EbmlString::UpdateSize(bool bWithDefault, bool /* bForceRender */)
{
if (!bWithDefault && IsDefaultValue())
return 0;
if (Value.length() < GetDefaultSize()) {
SetSize_(GetDefaultSize());
} else {
SetSize_(Value.length());
}
return GetSize();
}
filepos_t EbmlString::ReadData(IOCallback & input, ScopeMode ReadFully)
{
if (ReadFully == SCOPE_NO_DATA)
return GetSize();
if (GetSize() == 0) {
Value.clear();
} else {
Value.resize(GetSize());
std::memset(&Value[0], 0, GetSize());
input.readFully(&Value[0], GetSize());
auto PosNull = Value.find('\0');
if (PosNull != std::string::npos)
Value.resize(PosNull);
}
SetValueIsSet();
return GetSize();
}
} // namespace libebml