DCL 4.0
Loading...
Searching...
No Matches
CharsetDecoder Class Referenceabstract

#include <Charset.h>

Inheritance diagram for CharsetDecoder:
Object AsciiDecoder Latin1Decoder LocaleDecoder UTF16Decoder UTF32Decoder UTF8Decoder

Public Member Functions

virtual int decode (const byte_t *_in, size_t &_inCount, wchar_t *_out, size_t &_outCount)
size_t getDecodedLength (const char *_mbs, size_t _mbslen) __DCL_THROWS1(CharsetConvertException *)
String decode (const char *_mbs, size_t _mbslen=(size_t) -1) __DCL_THROWS1(CharsetConvertException *)
String decode (const ByteString &_str) __DCL_THROWS1(CharsetConvertException *)
Public Member Functions inherited from Object
virtual String toString () const
virtual void destroy ()
String className () const
bool isInstanceOf (const std::type_info &typeinfo) const
virtual const std::type_info & typeInfo () const

Protected Member Functions

 CharsetDecoder ()
virtual int toWideChar (const byte_t *_mbs, size_t _mbslen, ucs4_t *_uc)=0
Protected Member Functions inherited from Object
virtual ~Object ()
 Object ()

Detailed Description

Definition at line 106 of file Charset.h.

Constructor & Destructor Documentation

◆ CharsetDecoder()

CharsetDecoder::CharsetDecoder ( )
protected

Member Function Documentation

◆ decode() [1/3]

int CharsetDecoder::decode ( const byte_t * _in,
size_t & _inCount,
wchar_t * _out,
size_t & _outCount )
virtual

Definition at line 50 of file CharsetDecoder.cpp.

56{
57 __DCL_ASSERT(_in != NULL && _out != NULL);
58
59 wchar_t* dst = _out;
60 wchar_t* dstend = dst + _outCount;
61
62 const byte_t* src = _in;
63 const byte_t* srcend = src + _inCount;
64 size_t srclen;
65
66 int _mbslen = 0;
67 while((srclen = srcend - src) > 0 && dst < dstend) {
68 ucs4_t uc;
69 _mbslen = toWideChar(src, srclen, &uc);
70
71 if (_mbslen <= 0)
72 break;
73
74#if __SIZEOF_WCHAR_T__ == 2
75 if (uc > 0xFFFF || (0xD800 <= uc && uc < 0xE000)) {
76 _mbslen = ILLEGAL_UCS2;
77 break;
78 }
79#endif
80 *dst = (wchar_t)uc;
81
82 src += (size_t)_mbslen;
83 dst++;
84 }
85
86 _inCount = src - _in;
87 _outCount = dst - _out;
88
89 if (_mbslen >= 0)
90 return CS_NOERROR;
91
92 switch (_mbslen) {
93 case ILLEGAL_UCS2 :
94 return CS_ILLEGAL_UCS2;
95 case ILLEGAL_SEQUENCE :
97 }
98 return CS_SOURCE_FEW;
99}
__DCL_BEGIN_NAMESPACE typedef uint32_t ucs4_t
Definition Charset.h:29
@ CS_SOURCE_FEW
Definition Charset.h:65
@ CS_NOERROR
Definition Charset.h:61
@ CS_ILLEGAL_UCS2
Definition Charset.h:67
@ CS_ILLEGAL_SEQUENCE
Definition Charset.h:66
#define ILLEGAL_SEQUENCE
#define ILLEGAL_UCS2
#define NULL
Definition Config.h:340
unsigned char byte_t
Definition Config.h:274
#define __DCL_ASSERT(expr)
Definition Object.h:371
virtual int toWideChar(const byte_t *_mbs, size_t _mbslen, ucs4_t *_uc)=0

◆ decode() [2/3]

String CharsetDecoder::decode ( const ByteString & _str)

Definition at line 162 of file CharsetDecoder.cpp.

164{
165 return decode(_str, _str.length());
166}
virtual int decode(const byte_t *_in, size_t &_inCount, wchar_t *_out, size_t &_outCount)

◆ decode() [3/3]

String CharsetDecoder::decode ( const char * _mbs,
size_t _mbslen = (size_t)-1 )

Definition at line 128 of file CharsetDecoder.cpp.

130{
131 __DCL_ASSERT(_mbs != NULL);
132 if (_mbslen == (size_t) -1)
133 _mbslen = ByteString::length(_mbs);
134
135 String rstr;
136 if (_mbslen) {
137 const byte_t* _in = (const byte_t*) _mbs;
138 size_t _inCount = _mbslen;
139#if 0
140 size_t _outCount = getDecodedLength(_mbs, _mbslen);
141#else
142 size_t _outCount = _mbslen;
143#endif
144
145 CharBuffer* buf = CharBuffer::create(_outCount);
146 int rn = decode(_in, _inCount, buf->data(), _outCount);
147 __DCL_ASSERT(_outCount <= buf->__allocLength);
148 if (rn != CS_NOERROR) {
149 buf->release();
150 throw new CharsetConvertException(rn);
151 }
152 buf->data()[_outCount] = L'\0';
153 buf->__dataLength = _outCount;
154 CharBuffer::shrink(buf);
155
156 rstr.assign(buf);
157 buf->release();
158 }
159 return rstr;
160}
ByteBuffer * buf
size_t getDecodedLength(const char *_mbs, size_t _mbslen) __DCL_THROWS1(CharsetConvertException *)

◆ getDecodedLength()

size_t CharsetDecoder::getDecodedLength ( const char * _mbs,
size_t _mbslen )

Definition at line 101 of file CharsetDecoder.cpp.

103{
104 const byte_t* _in = (const byte_t*)_mbs;
105 size_t inTotal = _mbslen;
106 size_t outTotal = 0;
107 wchar_t buf[6];
108
109 for ( ; ; ) {
110 size_t _inCount = inTotal;
111 size_t _outCount = __countof(buf, wchar_t);
112 int r = decode(_in, _inCount, buf, _outCount);
113 outTotal += _outCount;
114 if (r == CS_NOERROR) {
115 inTotal -= _inCount;
116 if (inTotal == 0)
117 break;
118 // else buffer small
119 }
120 else {
121 throw(new CharsetConvertException(r));
122 }
123 _in += _inCount;
124 }
125 return outTotal;
126}
#define __countof(array, type)
Definition Config.h:365
ByteString r

◆ toWideChar()

virtual int CharsetDecoder::toWideChar ( const byte_t * _mbs,
size_t _mbslen,
ucs4_t * _uc )
protectedpure virtual

The documentation for this class was generated from the following files: