libebml: move some operator code in the .cpp files (MSVC has issues in DLLs)

+ libmatroska ChangeLog

git-svn-id: https://matroska.svn.sourceforge.net/svnroot/matroska/trunk/libebml@347 a6f86f6d-0131-4f8e-9e7b-e335508773d5
This commit is contained in:
Steve Lhomme 2010-06-26 09:07:13 +00:00
parent 1f01dcd455
commit 5607f4ebc8
15 changed files with 2198 additions and 2171 deletions

3136
ChangeLog

File diff suppressed because it is too large Load Diff

View File

@ -85,7 +85,7 @@ class EBML_DLL_API EbmlBinary : public EbmlElement {
SetValueIsSet();
}
operator const binary &() const {return *Data;}
operator const binary &() const;
bool IsDefaultValue() const {
return false;

View File

@ -30,7 +30,7 @@
/*!
\file
\version \$Id: EbmlFloat.h 1079 2005-03-03 13:18:14Z robux4 $
\version \$Id$
\author Steve Lhomme <robux4 @ users.sf.net>
*/
#ifndef LIBEBML_FLOAT_H
@ -79,8 +79,8 @@ class EBML_DLL_API EbmlFloat : public EbmlElement {
virtual bool IsSmallerThan(const EbmlElement *Cmp) const;
operator const float() const {return float(Value);}
operator const double() const {return double(Value);}
operator const float() const;
operator const double() const;
void SetDefaultValue(double);

View File

@ -71,10 +71,10 @@ class EBML_DLL_API EbmlSInteger : public EbmlElement {
virtual bool IsSmallerThan(const EbmlElement *Cmp) const;
operator int8() {return int8(Value);}
operator int16() {return int16(Value);}
operator int32() {return int32(Value);}
operator int64() {return Value;}
operator int8();
operator int16();
operator int32();
operator int64();
void SetDefaultValue(int64 aValue) {assert(!DefaultISset()); DefaultValue = aValue; SetDefaultIsSet();}

View File

@ -61,7 +61,7 @@ class EBML_DLL_API EbmlString : public EbmlElement {
filepos_t UpdateSize(bool bWithDefault = false, bool bForceRender = false);
EbmlString & operator=(const std::string &);
operator const std::string &() const {return Value;}
operator const std::string &() const;
void SetDefaultValue(std::string &);

View File

@ -69,10 +69,10 @@ class EBML_DLL_API EbmlUInteger : public EbmlElement {
virtual bool IsSmallerThan(const EbmlElement *Cmp) const;
operator uint8() const {return uint8(Value); }
operator uint16() const {return uint16(Value);}
operator uint32() const {return uint32(Value);}
operator uint64() const {return Value;}
operator uint8() const;
operator uint16() const;
operator uint32() const;
operator uint64() const;
void SetDefaultValue(uint64);

View File

@ -71,7 +71,7 @@ public:
/// Return length of string
size_t length() const {return _Length;}
operator const wchar_t*() const {return _Data;}
operator const wchar_t*() const;
const wchar_t* c_str() const {return _Data;}
const std::string & GetUTF8() const {return UTF8string;}
@ -110,7 +110,7 @@ class EBML_DLL_API EbmlUnicodeString : public EbmlElement {
filepos_t UpdateSize(bool bWithDefault = false, bool bForceRender = false);
EbmlUnicodeString & operator=(const UTFstring &); ///< platform dependant code
operator const UTFstring &() const {return Value;}
operator const UTFstring &() const;
void SetDefaultValue(UTFstring &);

View File

@ -42,9 +42,9 @@
START_LIBEBML_NAMESPACE
#define LIBEBML_VERSION 0x010000
#define LIBEBML_VERSION 0x010001
static const std::string EbmlCodeVersion = "1.0.0";
static const std::string EbmlCodeVersion = "1.0.1";
static const std::string EbmlCodeDate = __TIMESTAMP__;
/*!

View File

@ -2,7 +2,7 @@ Include "*/*.proj"
LIB ebml
{
PROJECT_VERSION 1.0.0
PROJECT_VERSION 1.0.1
INCLUDE .
EXPINCLUDE .

View File

@ -1,101 +1,104 @@
/****************************************************************************
** 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**
** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
**
** Contact license@matroska.org if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
/*!
\file
\version \$Id: EbmlBinary.cpp 1112 2005-03-28 09:55:50Z mosu $
\author Steve Lhomme <robux4 @ users.sf.net>
\author Julien Coloos <suiryc @ users.sf.net>
*/
#include <cassert>
#include "ebml/EbmlBinary.h"
START_LIBEBML_NAMESPACE
EbmlBinary::EbmlBinary()
:EbmlElement(0, false), Data(NULL)
{}
EbmlBinary::EbmlBinary(const EbmlBinary & ElementToClone)
:EbmlElement(ElementToClone)
{
if (ElementToClone.Data == NULL)
Data = NULL;
else {
Data = (binary *)malloc(GetSize() * sizeof(binary));
assert(Data != NULL);
memcpy(Data, ElementToClone.Data, GetSize());
}
}
EbmlBinary::~EbmlBinary(void) {
if(Data)
free(Data);
}
filepos_t EbmlBinary::RenderData(IOCallback & output, bool bForceRender, bool bWithDefault)
{
output.writeFully(Data,GetSize());
return GetSize();
}
/*!
\note no Default binary value handled
*/
uint64 EbmlBinary::UpdateSize(bool bWithDefault, bool bForceRender)
{
return GetSize();
}
filepos_t EbmlBinary::ReadData(IOCallback & input, ScopeMode ReadFully)
{
if (Data != NULL)
free(Data);
if (ReadFully == SCOPE_NO_DATA)
{
Data = NULL;
return GetSize();
}
Data = (binary *)malloc(GetSize() * sizeof(binary));
assert(Data != NULL);
SetValueIsSet();
return input.read(Data, GetSize());
}
bool EbmlBinary::operator==(const EbmlBinary & ElementToCompare) const
{
return ((GetSize() == ElementToCompare.GetSize()) && !memcmp(Data, ElementToCompare.Data, GetSize()));
}
END_LIBEBML_NAMESPACE
/****************************************************************************
** 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**
** See http://www.matroska.org/license/lgpl/ 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>
\author Julien Coloos <suiryc @ users.sf.net>
*/
#include <cassert>
#include "ebml/EbmlBinary.h"
START_LIBEBML_NAMESPACE
EbmlBinary::EbmlBinary()
:EbmlElement(0, false), Data(NULL)
{}
EbmlBinary::EbmlBinary(const EbmlBinary & ElementToClone)
:EbmlElement(ElementToClone)
{
if (ElementToClone.Data == NULL)
Data = NULL;
else {
Data = (binary *)malloc(GetSize() * sizeof(binary));
assert(Data != NULL);
memcpy(Data, ElementToClone.Data, GetSize());
}
}
EbmlBinary::~EbmlBinary(void) {
if(Data)
free(Data);
}
EbmlBinary::operator const binary &() const {return *Data;}
filepos_t EbmlBinary::RenderData(IOCallback & output, bool bForceRender, bool bWithDefault)
{
output.writeFully(Data,GetSize());
return GetSize();
}
/*!
\note no Default binary value handled
*/
uint64 EbmlBinary::UpdateSize(bool bWithDefault, bool bForceRender)
{
return GetSize();
}
filepos_t EbmlBinary::ReadData(IOCallback & input, ScopeMode ReadFully)
{
if (Data != NULL)
free(Data);
if (ReadFully == SCOPE_NO_DATA)
{
Data = NULL;
return GetSize();
}
Data = (binary *)malloc(GetSize() * sizeof(binary));
assert(Data != NULL);
SetValueIsSet();
return input.read(Data, GetSize());
}
bool EbmlBinary::operator==(const EbmlBinary & ElementToCompare) const
{
return ((GetSize() == ElementToCompare.GetSize()) && !memcmp(Data, ElementToCompare.Data, GetSize()));
}
END_LIBEBML_NAMESPACE

View File

@ -30,7 +30,7 @@
/*!
\file
\version \$Id: EbmlFloat.cpp 1243 2006-03-30 19:33:22Z mosu $
\version \$Id$
\author Steve Lhomme <robux4 @ users.sf.net>
*/
@ -73,6 +73,9 @@ const double EbmlFloat::DefaultVal() const
return DefaultValue;
}
EbmlFloat::operator const float() const {return float(Value);}
EbmlFloat::operator const double() const {return double(Value);}
/*!
\todo handle exception on errors

View File

@ -28,7 +28,7 @@
/*!
\file
\version \$Id: EbmlSInteger.cpp 1079 2005-03-03 13:18:14Z robux4 $
\version \$Id$
\author Steve Lhomme <robux4 @ users.sf.net>
\author Moritz Bunkus <moritz @ bunkus.org>
*/
@ -55,6 +55,11 @@ EbmlSInteger::EbmlSInteger(const EbmlSInteger & ElementToClone)
{
}
EbmlSInteger::operator int8() {return int8(Value);}
EbmlSInteger::operator int16() {return int16(Value);}
EbmlSInteger::operator int32() {return int32(Value);}
EbmlSInteger::operator int64() {return Value;}
/*!
\todo handle exception on errors
*/

View File

@ -1,163 +1,165 @@
/****************************************************************************
** 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**
** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
**
** Contact license@matroska.org if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
/*!
\file
\version \$Id: EbmlString.cpp 1079 2005-03-03 13:18:14Z robux4 $
\author Steve Lhomme <robux4 @ users.sf.net>
*/
#include <cassert>
#include "ebml/EbmlString.h"
START_LIBEBML_NAMESPACE
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 !
*/
EbmlString::EbmlString(const EbmlString & ElementToClone)
:EbmlElement(ElementToClone)
,Value(ElementToClone.Value)
,DefaultValue(ElementToClone.DefaultValue)
{
}
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
binary *Pad = new binary[GetDefaultSize() - Result];
if (Pad == NULL)
{
return Result;
}
memset(Pad, 0x00, GetDefaultSize() - Result);
output.writeFully(Pad, GetDefaultSize() - Result);
Result = GetDefaultSize();
delete [] Pad;
}
return Result;
}
EbmlString & EbmlString::operator=(const std::string & NewString)
{
Value = NewString;
SetValueIsSet();
/* done automatically
SetSize_(Value.length());
if (GetDefaultSize() > GetSize())
SetSize_(GetDefaultSize());*/
return *this;
}
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)
{
if (GetSize() == 0) {
Value = "";
SetValueIsSet();
} else {
char *Buffer = new char[GetSize() + 1];
if (Buffer == NULL) {
// unable to store the data, skip it
input.setFilePointer(GetSize(), seek_current);
} else {
input.readFully(Buffer, GetSize());
if (Buffer[GetSize()-1] != '\0') {
Buffer[GetSize()] = '\0';
}
Value = Buffer;
delete [] Buffer;
SetValueIsSet();
}
}
}
return GetSize();
}
END_LIBEBML_NAMESPACE
/****************************************************************************
** 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**
** See http://www.matroska.org/license/lgpl/ 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 "ebml/EbmlString.h"
START_LIBEBML_NAMESPACE
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 !
*/
EbmlString::EbmlString(const EbmlString & ElementToClone)
:EbmlElement(ElementToClone)
,Value(ElementToClone.Value)
,DefaultValue(ElementToClone.DefaultValue)
{
}
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
binary *Pad = new binary[GetDefaultSize() - Result];
if (Pad == NULL)
{
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;
}
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)
{
if (GetSize() == 0) {
Value = "";
SetValueIsSet();
} else {
char *Buffer = new char[GetSize() + 1];
if (Buffer == NULL) {
// unable to store the data, skip it
input.setFilePointer(GetSize(), seek_current);
} else {
input.readFully(Buffer, GetSize());
if (Buffer[GetSize()-1] != '\0') {
Buffer[GetSize()] = '\0';
}
Value = Buffer;
delete [] Buffer;
SetValueIsSet();
}
}
}
return GetSize();
}
END_LIBEBML_NAMESPACE

View File

@ -70,6 +70,11 @@ uint64 EbmlUInteger::DefaultVal() const
return DefaultValue;
}
EbmlUInteger::operator uint8() const {return uint8(Value); }
EbmlUInteger::operator uint16() const {return uint16(Value);}
EbmlUInteger::operator uint32() const {return uint32(Value);}
EbmlUInteger::operator uint64() const {return Value;}
/*!
\todo handle exception on errors

View File

@ -1,321 +1,326 @@
/****************************************************************************
** 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**
** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
**
** Contact license@matroska.org if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
/*!
\file
\version \$Id: EbmlUnicodeString.cpp 1079 2005-03-03 13:18:14Z robux4 $
\author Steve Lhomme <robux4 @ users.sf.net>
\author Jory Stone <jcsston @ toughguy.net>
*/
#include <cassert>
#if __GNUC__ == 2 && ! defined ( __OpenBSD__ )
#include <wchar.h>
#endif
#include "ebml/EbmlUnicodeString.h"
START_LIBEBML_NAMESPACE
// ===================== UTFstring class ===================
UTFstring::UTFstring()
:_Length(0)
,_Data(NULL)
{}
UTFstring::UTFstring(const wchar_t * _aBuf)
:_Length(0)
,_Data(NULL)
{
*this = _aBuf;
}
UTFstring::~UTFstring()
{
delete [] _Data;
}
UTFstring::UTFstring(const UTFstring & _aBuf)
:_Length(0)
,_Data(NULL)
{
*this = _aBuf.c_str();
}
UTFstring & UTFstring::operator=(const UTFstring & _aBuf)
{
*this = _aBuf.c_str();
return *this;
}
UTFstring & UTFstring::operator=(const wchar_t * _aBuf)
{
delete [] _Data;
if (_aBuf == NULL) {
_Data = new wchar_t[1];
_Data[0] = 0;
UpdateFromUCS2();
return *this;
}
size_t aLen;
for (aLen=0; _aBuf[aLen] != 0; aLen++);
_Length = aLen;
_Data = new wchar_t[_Length+1];
for (aLen=0; _aBuf[aLen] != 0; aLen++) {
_Data[aLen] = _aBuf[aLen];
}
_Data[aLen] = 0;
UpdateFromUCS2();
return *this;
}
UTFstring & UTFstring::operator=(wchar_t _aChar)
{
delete [] _Data;
_Data = new wchar_t[2];
_Length = 1;
_Data[0] = _aChar;
_Data[1] = 0;
UpdateFromUCS2();
return *this;
}
bool UTFstring::operator==(const UTFstring& _aStr) const
{
if ((_Data == NULL) && (_aStr._Data == NULL))
return true;
if ((_Data == NULL) || (_aStr._Data == NULL))
return false;
return wcscmp_internal(_Data, _aStr._Data);
}
void UTFstring::SetUTF8(const std::string & _aStr)
{
UTF8string = _aStr;
UpdateFromUTF8();
}
/*!
\see RFC 2279
*/
void UTFstring::UpdateFromUTF8()
{
delete [] _Data;
// find the size of the final UCS-2 string
size_t i;
for (_Length=0, i=0; i<UTF8string.length(); _Length++) {
if ((UTF8string[i] & 0x80) == 0) {
i++;
} else if ((UTF8string[i] & 0x20) == 0) {
i += 2;
} else if ((UTF8string[i] & 0x10) == 0) {
i += 3;
}
}
_Data = new wchar_t[_Length+1];
size_t j;
for (j=0, i=0; i<UTF8string.length(); j++) {
if ((UTF8string[i] & 0x80) == 0) {
_Data[j] = UTF8string[i];
i++;
} else if ((UTF8string[i] & 0x20) == 0) {
_Data[j] = ((UTF8string[i] & 0x1F) << 6) + (UTF8string[i+1] & 0x3F);
i += 2;
} else if ((UTF8string[i] & 0x10) == 0) {
_Data[j] = ((UTF8string[i] & 0x0F) << 12) + ((UTF8string[i+1] & 0x3F) << 6) + (UTF8string[i+2] & 0x3F);
i += 3;
}
}
_Data[j] = 0;
}
void UTFstring::UpdateFromUCS2()
{
// find the size of the final UTF-8 string
size_t i,Size=0;
for (i=0; i<_Length; i++)
{
if (_Data[i] < 0x80) {
Size++;
} else if (_Data[i] < 0x800) {
Size += 2;
} else if (_Data[i] < 0x10000) {
Size += 3;
}
}
std::string::value_type *tmpStr = new std::string::value_type[Size+1];
for (i=0, Size=0; i<_Length; i++)
{
if (_Data[i] < 0x80) {
tmpStr[Size++] = _Data[i];
} else if (_Data[i] < 0x800) {
tmpStr[Size++] = 0xC0 | (_Data[i] >> 6);
tmpStr[Size++] = 0x80 | (_Data[i] & 0x3F);
} else if (_Data[i] < 0x10000) {
tmpStr[Size++] = 0xE0 | (_Data[i] >> 12);
tmpStr[Size++] = 0x80 | ((_Data[i] >> 6) & 0x3F);
tmpStr[Size++] = 0x80 | (_Data[i] & 0x3F);
}
}
tmpStr[Size] = 0;
UTF8string = tmpStr; // implicit conversion
delete [] tmpStr;
}
bool UTFstring::wcscmp_internal(const wchar_t *str1, const wchar_t *str2)
{
size_t Index=0;
while (str1[Index] == str2[Index] && str1[Index] != 0) {
Index++;
}
return (str1[Index] == str2[Index]);
}
// ===================== EbmlUnicodeString class ===================
EbmlUnicodeString::EbmlUnicodeString()
:EbmlElement(0, false)
{
SetDefaultSize(0);
}
EbmlUnicodeString::EbmlUnicodeString(const UTFstring & aDefaultValue)
:EbmlElement(0, true), Value(aDefaultValue), DefaultValue(aDefaultValue)
{
SetDefaultSize(0);
SetDefaultIsSet();
}
EbmlUnicodeString::EbmlUnicodeString(const EbmlUnicodeString & ElementToClone)
:EbmlElement(ElementToClone)
,Value(ElementToClone.Value)
,DefaultValue(ElementToClone.DefaultValue)
{
}
void EbmlUnicodeString::SetDefaultValue(UTFstring & aValue)
{
assert(!DefaultISset());
DefaultValue = aValue;
SetDefaultIsSet();
}
const UTFstring & EbmlUnicodeString::DefaultVal() const
{
assert(DefaultISset());
return DefaultValue;
}
/*!
\note limited to UCS-2
\todo handle exception on errors
*/
filepos_t EbmlUnicodeString::RenderData(IOCallback & output, bool bForceRender, bool bWithDefault)
{
uint32 Result = Value.GetUTF8().length();
if (Result != 0) {
output.writeFully(Value.GetUTF8().c_str(), Result);
}
if (Result < GetDefaultSize()) {
// pad the rest with 0
binary *Pad = new binary[GetDefaultSize() - Result];
if (Pad != NULL) {
memset(Pad, 0x00, GetDefaultSize() - Result);
output.writeFully(Pad, GetDefaultSize() - Result);
Result = GetDefaultSize();
delete [] Pad;
}
}
return Result;
}
EbmlUnicodeString & EbmlUnicodeString::operator=(const UTFstring & NewString)
{
Value = NewString;
SetValueIsSet();
return *this;
}
/*!
\note limited to UCS-2
*/
uint64 EbmlUnicodeString::UpdateSize(bool bWithDefault, bool bForceRender)
{
if (!bWithDefault && IsDefaultValue())
return 0;
SetSize_(Value.GetUTF8().length());
if (GetSize() < GetDefaultSize())
SetSize_(GetDefaultSize());
return GetSize();
}
/*!
\note limited to UCS-2
*/
filepos_t EbmlUnicodeString::ReadData(IOCallback & input, ScopeMode ReadFully)
{
if (ReadFully != SCOPE_NO_DATA)
{
if (GetSize() == 0) {
Value = UTFstring::value_type(0);
SetValueIsSet();
} else {
char *Buffer = new char[GetSize()+1];
if (Buffer == NULL) {
// impossible to read, skip it
input.setFilePointer(GetSize(), seek_current);
} else {
input.readFully(Buffer, GetSize());
if (Buffer[GetSize()-1] != 0) {
Buffer[GetSize()] = 0;
}
Value.SetUTF8(Buffer); // implicit conversion to std::string
delete [] Buffer;
SetValueIsSet();
}
}
}
return GetSize();
}
END_LIBEBML_NAMESPACE
/****************************************************************************
** 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**
** See http://www.matroska.org/license/lgpl/ 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>
\author Jory Stone <jcsston @ toughguy.net>
*/
#include <cassert>
#if __GNUC__ == 2 && ! defined ( __OpenBSD__ )
#include <wchar.h>
#endif
#include "ebml/EbmlUnicodeString.h"
START_LIBEBML_NAMESPACE
// ===================== UTFstring class ===================
UTFstring::UTFstring()
:_Length(0)
,_Data(NULL)
{}
UTFstring::UTFstring(const wchar_t * _aBuf)
:_Length(0)
,_Data(NULL)
{
*this = _aBuf;
}
UTFstring::~UTFstring()
{
delete [] _Data;
}
UTFstring::UTFstring(const UTFstring & _aBuf)
:_Length(0)
,_Data(NULL)
{
*this = _aBuf.c_str();
}
UTFstring & UTFstring::operator=(const UTFstring & _aBuf)
{
*this = _aBuf.c_str();
return *this;
}
UTFstring::operator const wchar_t*() const {return _Data;}
UTFstring & UTFstring::operator=(const wchar_t * _aBuf)
{
delete [] _Data;
if (_aBuf == NULL) {
_Data = new wchar_t[1];
_Data[0] = 0;
UpdateFromUCS2();
return *this;
}
size_t aLen;
for (aLen=0; _aBuf[aLen] != 0; aLen++);
_Length = aLen;
_Data = new wchar_t[_Length+1];
for (aLen=0; _aBuf[aLen] != 0; aLen++) {
_Data[aLen] = _aBuf[aLen];
}
_Data[aLen] = 0;
UpdateFromUCS2();
return *this;
}
UTFstring & UTFstring::operator=(wchar_t _aChar)
{
delete [] _Data;
_Data = new wchar_t[2];
_Length = 1;
_Data[0] = _aChar;
_Data[1] = 0;
UpdateFromUCS2();
return *this;
}
bool UTFstring::operator==(const UTFstring& _aStr) const
{
if ((_Data == NULL) && (_aStr._Data == NULL))
return true;
if ((_Data == NULL) || (_aStr._Data == NULL))
return false;
return wcscmp_internal(_Data, _aStr._Data);
}
void UTFstring::SetUTF8(const std::string & _aStr)
{
UTF8string = _aStr;
UpdateFromUTF8();
}
/*!
\see RFC 2279
*/
void UTFstring::UpdateFromUTF8()
{
delete [] _Data;
// find the size of the final UCS-2 string
size_t i;
for (_Length=0, i=0; i<UTF8string.length(); _Length++) {
if ((UTF8string[i] & 0x80) == 0) {
i++;
} else if ((UTF8string[i] & 0x20) == 0) {
i += 2;
} else if ((UTF8string[i] & 0x10) == 0) {
i += 3;
}
}
_Data = new wchar_t[_Length+1];
size_t j;
for (j=0, i=0; i<UTF8string.length(); j++) {
if ((UTF8string[i] & 0x80) == 0) {
_Data[j] = UTF8string[i];
i++;
} else if ((UTF8string[i] & 0x20) == 0) {
_Data[j] = ((UTF8string[i] & 0x1F) << 6) + (UTF8string[i+1] & 0x3F);
i += 2;
} else if ((UTF8string[i] & 0x10) == 0) {
_Data[j] = ((UTF8string[i] & 0x0F) << 12) + ((UTF8string[i+1] & 0x3F) << 6) + (UTF8string[i+2] & 0x3F);
i += 3;
}
}
_Data[j] = 0;
}
void UTFstring::UpdateFromUCS2()
{
// find the size of the final UTF-8 string
size_t i,Size=0;
for (i=0; i<_Length; i++)
{
if (_Data[i] < 0x80) {
Size++;
} else if (_Data[i] < 0x800) {
Size += 2;
} else if (_Data[i] < 0x10000) {
Size += 3;
}
}
std::string::value_type *tmpStr = new std::string::value_type[Size+1];
for (i=0, Size=0; i<_Length; i++)
{
if (_Data[i] < 0x80) {
tmpStr[Size++] = _Data[i];
} else if (_Data[i] < 0x800) {
tmpStr[Size++] = 0xC0 | (_Data[i] >> 6);
tmpStr[Size++] = 0x80 | (_Data[i] & 0x3F);
} else if (_Data[i] < 0x10000) {
tmpStr[Size++] = 0xE0 | (_Data[i] >> 12);
tmpStr[Size++] = 0x80 | ((_Data[i] >> 6) & 0x3F);
tmpStr[Size++] = 0x80 | (_Data[i] & 0x3F);
}
}
tmpStr[Size] = 0;
UTF8string = tmpStr; // implicit conversion
delete [] tmpStr;
}
bool UTFstring::wcscmp_internal(const wchar_t *str1, const wchar_t *str2)
{
size_t Index=0;
while (str1[Index] == str2[Index] && str1[Index] != 0) {
Index++;
}
return (str1[Index] == str2[Index]);
}
// ===================== EbmlUnicodeString class ===================
EbmlUnicodeString::EbmlUnicodeString()
:EbmlElement(0, false)
{
SetDefaultSize(0);
}
EbmlUnicodeString::EbmlUnicodeString(const UTFstring & aDefaultValue)
:EbmlElement(0, true), Value(aDefaultValue), DefaultValue(aDefaultValue)
{
SetDefaultSize(0);
SetDefaultIsSet();
}
EbmlUnicodeString::EbmlUnicodeString(const EbmlUnicodeString & ElementToClone)
:EbmlElement(ElementToClone)
,Value(ElementToClone.Value)
,DefaultValue(ElementToClone.DefaultValue)
{
}
void EbmlUnicodeString::SetDefaultValue(UTFstring & aValue)
{
assert(!DefaultISset());
DefaultValue = aValue;
SetDefaultIsSet();
}
const UTFstring & EbmlUnicodeString::DefaultVal() const
{
assert(DefaultISset());
return DefaultValue;
}
/*!
\note limited to UCS-2
\todo handle exception on errors
*/
filepos_t EbmlUnicodeString::RenderData(IOCallback & output, bool bForceRender, bool bWithDefault)
{
uint32 Result = Value.GetUTF8().length();
if (Result != 0) {
output.writeFully(Value.GetUTF8().c_str(), Result);
}
if (Result < GetDefaultSize()) {
// pad the rest with 0
binary *Pad = new binary[GetDefaultSize() - Result];
if (Pad != NULL) {
memset(Pad, 0x00, GetDefaultSize() - Result);
output.writeFully(Pad, GetDefaultSize() - Result);
Result = GetDefaultSize();
delete [] Pad;
}
}
return Result;
}
EbmlUnicodeString::operator const UTFstring &() const {return Value;}
EbmlUnicodeString & EbmlUnicodeString::operator=(const UTFstring & NewString)
{
Value = NewString;
SetValueIsSet();
return *this;
}
/*!
\note limited to UCS-2
*/
uint64 EbmlUnicodeString::UpdateSize(bool bWithDefault, bool bForceRender)
{
if (!bWithDefault && IsDefaultValue())
return 0;
SetSize_(Value.GetUTF8().length());
if (GetSize() < GetDefaultSize())
SetSize_(GetDefaultSize());
return GetSize();
}
/*!
\note limited to UCS-2
*/
filepos_t EbmlUnicodeString::ReadData(IOCallback & input, ScopeMode ReadFully)
{
if (ReadFully != SCOPE_NO_DATA)
{
if (GetSize() == 0) {
Value = UTFstring::value_type(0);
SetValueIsSet();
} else {
char *Buffer = new char[GetSize()+1];
if (Buffer == NULL) {
// impossible to read, skip it
input.setFilePointer(GetSize(), seek_current);
} else {
input.readFully(Buffer, GetSize());
if (Buffer[GetSize()-1] != 0) {
Buffer[GetSize()] = 0;
}
Value.SetUTF8(Buffer); // implicit conversion to std::string
delete [] Buffer;
SetValueIsSet();
}
}
}
return GetSize();
}
END_LIBEBML_NAMESPACE