//---------------------------------------------------------------------
//
// Copyright (C) 2004 Yingle Jia
//
// Permission to copy, use, modify, sell and distribute this software is
// granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied warranty,
// and with no claim as to its suitability for any purpose.
//
#include "stdafx.h"
namespace Acf {
namespace Text {
//---------------------------------------------------------------------
// class DefaultEncoder
//
// Default encoder implementation for encodings that do not require
// state to be maintained between successive calls.
//
class DefaultEncoder : public Encoder
{
private:
RefPtr<Encoding> _encoding;
public:
DefaultEncoder(Encoding* encoding)
{
this->_encoding = encoding;
}
override int GetByteCount(Array<wchar_t>* chars, int index, int count, bool /*flush*/)
{
return this->_encoding->GetByteCount(chars, index, count);
}
override int GetBytes(Array<wchar_t>* chars, int charIndex, int charCount,
Array<byte>* bytes, int byteIndex, bool /*flush*/)
{
return this->_encoding->GetBytes(chars, charIndex, charCount,
bytes, byteIndex);
}
};
//---------------------------------------------------------------------
// class DefaultDecoder
//
// Default decoder implementation for encodings that do not require
// state to be maintained between successive calls.
//
class DefaultDecoder : public Decoder
{
private:
RefPtr<Encoding> _encoding;
public:
DefaultDecoder(Encoding* encoding)
{
this->_encoding = encoding;
}
override int GetCharCount(Array<byte>* bytes, int index, int count)
{
return this->_encoding->GetCharCount(bytes, index, count);
}
override int GetChars(Array<byte>* bytes, int byteIndex, int byteCount,
Array<wchar_t>* chars, int charIndex)
{
return this->_encoding->GetChars(bytes, byteIndex, byteCount,
chars, charIndex);
}
};
//---------------------------------------------------------------------
// class Encoding
// Static Properties
/*static*/ EncodingPtr Encoding::get_ASCII()
{
return new ASCIIEncoding();
}
/*static*/ EncodingPtr Encoding::get_Unicode()
{
return new UnicodeEncoding();
}
/*static*/ EncodingPtr Encoding::get_BigEndianUnicode()
{
return new UnicodeEncoding(true, true);
}
/*static*/ EncodingPtr Encoding::get_UTF8()
{
return new UTF8Encoding();
}
/*static*/ EncodingPtr Encoding::get_Default()
{
int codePage = (int) ::GetACP();
switch (codePage)
{
case 1200:
return new UnicodeEncoding();
break;
case 1201:
return new UnicodeEncoding(true, true);
break;
case 65001:
return new UTF8Encoding();
break;
default:
return new Internal::CodePageEncoding(codePage);
break;
}
}
// Static Methods
/*static*/ RefPtr< Array<byte> > Encoding::Convert(Encoding* srcEncoding, Encoding* dstEncoding,
Array<byte>* bytes)
{
if (bytes == null)
throw ArgumentNullException();
return Convert(srcEncoding, dstEncoding, bytes, 0, bytes->Length);
}
/*static*/ RefPtr< Array<byte> > Encoding::Convert(Encoding* srcEncoding, Encoding* dstEncoding,
Array<byte>* bytes, int index, int count)
{
Acf::VerifyArrayRange(bytes, index, count);
RefPtr< Array<wchar_t> > chars = srcEncoding->GetChars(bytes, index, count);
return dstEncoding->GetBytes(chars);
}
// Methods
/*virtual*/ RefPtr< Array<byte> > Encoding::GetPreamble()
{
return new Array<byte>(0);
}
/*virtual*/ EncoderPtr Encoding::GetEncoder()
{
return new DefaultEncoder(this);
}
/*virtual*/ DecoderPtr Encoding::GetDecoder()
{
return new DefaultDecoder(this);
}
/*virtual*/ int Encoding::GetByteCount(Array<wchar_t>* chars)
{
if (chars == null)
throw ArgumentNullException();
return GetByteCount(chars, 0, chars->Length);
}
/*virtual*/ int Encoding::GetByteCount(String* s)
{
if (s == null)
throw ArgumentNullException();
RefPtr< Array<wchar_t> > chars = s->ToCharArray();
return GetByteCount(chars, 0, chars->Length);
}
/*virtual*/ RefPtr< Array<byte> > Encoding::GetBytes(Array<wchar_t>* chars)
{
if (chars == null)
throw ArgumentNullException();
return GetBytes(chars, 0, chars->Length);
}
/*virtual*/ RefPtr< Array<byte> > Encoding::GetBytes(Array<wchar_t>* chars,
int index, int count)
{
int byteCount = GetByteCount(chars, index, count);
RefPtr< Array<byte> > bytes = new Array<byte>(byteCount);
GetBytes(chars, index, count, bytes, 0);
return bytes;
}
/*virtual*/ RefPtr< Array<byte> > Encoding::GetBytes(String* s)
{
if (s == null)
throw ArgumentNullException();
RefPtr< Array<wchar_t> > chars = s->ToCharArray();
return GetBytes(chars, 0, chars->Length);
}
/*virtual*/ int Encoding::GetBytes(String* s, int charIndex, int charCount,
Array<byte>* bytes, int byteIndex)
{
if (s == null)
throw ArgumentNullException();
return GetBytes(s->ToCharArray(), charIndex, charCount, bytes, byteIndex);
}
/*virtual*/ int Encoding::GetCharCount(Array<byte>* bytes)
{
if (bytes == null)
throw ArgumentNullException();
return GetCharCount(bytes, 0, bytes->Length);
}
/*virtual*/ RefPtr< Array<wchar_t> > Encoding::GetChars(Array<byte>* bytes)
{
if (bytes == null)
throw ArgumentNullException();
return GetChars(bytes, 0, bytes->Length);
}
/*virtual*/ RefPtr< Array<wchar_t> > Encoding::GetChars(Array<byte>* bytes,
int index, int count)
{
int charCount = GetCharCount(bytes, index, count);
RefPtr< Array<wchar_t> > chars = new Array<wchar_t>(charCount);
GetChars(bytes, index, count, chars, 0);
return chars;
}
/*virtual*/ StringPtr Encoding::GetString(Array<byte>* bytes)
{
if (bytes == null)
throw ArgumentNullException();
return GetString(bytes, 0, bytes->Length);
}
/*virtual*/ StringPtr Encoding::GetString(Array<byte>* bytes,
int byteIndex, int byteCount)
{
return new String(GetChars(bytes, byteIndex, byteCount));
}
} // namespace Text
} // namespace Acf