2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FText_Iso885916EncodingCore.cpp
19 * @brief This is the implementation file for _Iso885916EncodingCore class.
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include "FText_Iso885916EncodingCore.h"
28 using namespace Tizen::Base;
30 namespace Tizen { namespace Text
33 static const wchar_t ISO_8859_16_UNICODE_TABLE[] =
35 0x00A0, 0x0104, 0x0105, 0x0141, 0x20AC, 0x201E, 0x0160, 0x00A7,
36 0x0161, 0x00A9, 0x0218, 0x00AB, 0x0179, 0x00AD, 0x017A, 0x017B,
37 0x00B0, 0x00B1, 0x010C, 0x0142, 0x017D, 0x201D, 0x00B6, 0x00B7,
38 0x017E, 0x010D, 0x0219, 0x00BB, 0x0152, 0x0153, 0x0178, 0x017C,
39 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0106, 0x00C6, 0x00C7,
40 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
41 0x0110, 0x0143, 0x00D2, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x015A,
42 0x0170, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0118, 0x021A, 0x00DF,
43 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x0107, 0x00E6, 0x00E7,
44 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
45 0x0111, 0x0144, 0x00F2, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x015B,
46 0x0171, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0119, 0x021B, 0x00FF
50 _Iso885916EncodingCore::_Iso885916EncodingCore(void)
55 _Iso885916EncodingCore::~_Iso885916EncodingCore(void)
60 _Iso885916EncodingCore::GetEncodingCoreImplN(const Tizen::Base::String& encodingFrom, const Tizen::Base::String& encodingTo)
62 if (encodingFrom != L"ISO-8859-16")
64 SetLastResult(E_UNSUPPORTED_TYPE);
68 _Iso885916EncodingCore* pEncodingCore = new (std::nothrow) _Iso885916EncodingCore;
69 SysTryReturn(NID_TEXT, pEncodingCore, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
75 _Iso885916EncodingCore::GetAvailableEncodingsN(Tizen::Base::Collection::IList* pEncodingList)
77 SysTryReturnResult(NID_TEXT, pEncodingList, E_INVALID_ARG,
78 "[%s] Invalid argument is used. Input encoding list is null", GetErrorMessage(E_INVALID_ARG));
80 String* pEncodingStr = new (std::nothrow) String(L"ISO-8859-16");
81 SysTryReturnResult(NID_TEXT, pEncodingStr, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
83 result r = pEncodingList->Add(*pEncodingStr);
93 _Iso885916EncodingCore::GetName(void)
95 return String(L"ISO-8859-16");
99 _Iso885916EncodingCore::GetByteCount(const wchar_t* pSrc, int srcLength, int& retLength)
102 wchar_t* pUnicodeStr = (wchar_t*) pSrc;
105 int iso8859_16Size = srcLength;
106 while (srcLength > 0)
108 if ((*pUnicodeStr) > 0x00A0)
110 if (((*pUnicodeStr) > 0xFFFF))
112 return E_INVALID_ENCODING_RANGE;
115 if (!(*pUnicodeStr & 0xFF00))
117 if (ISO_8859_16_UNICODE_TABLE[*pUnicodeStr - 0xA0] != *pUnicodeStr)
119 return E_INVALID_ENCODING_RANGE;
122 else if (!(*pUnicodeStr & 0xFE80))
125 for (int i = 0; i <= 0x5F; i++)
127 if (ISO_8859_16_UNICODE_TABLE[i] == *pUnicodeStr)
133 // check for invalid values
136 return E_INVALID_ENCODING_RANGE;
139 else if (!(*pUnicodeStr & 0xFDE0))
141 if ((*pUnicodeStr == 0x0218) || (*pUnicodeStr == 0x0219) || (*pUnicodeStr == 0x021A) ||
142 (*pUnicodeStr == 0x021B))
148 return E_INVALID_ENCODING_RANGE;
151 else if (!(*pUnicodeStr & 0xDF40))
153 if ((*pUnicodeStr == 0x201D) || (*pUnicodeStr == 0x201E) || (*pUnicodeStr == 0x20AC))
159 return E_INVALID_ENCODING_RANGE;
164 return E_INVALID_ENCODING_RANGE;
170 retLength = iso8859_16Size;
174 return E_INVALID_ARG;
178 _Iso885916EncodingCore::GetCharCount(const byte* pSrc, int srcLength, int& retLength)
181 SysTryReturnResult(NID_TEXT, pSrc, E_INVALID_ARG,
182 "[%s] Invalid argument is used. Input buffer pSrc is null.", GetErrorMessage(E_INVALID_ARG));
183 retLength = srcLength;
188 _Iso885916EncodingCore::EncodeN(const wchar_t* pSrc, int srcLength, int& retLength)
190 wchar_t* pUnicodeStr = (wchar_t*) pSrc;
191 result r = GetByteCount(pUnicodeStr, srcLength, retLength);
192 SysTryReturn(NID_TEXT, retLength > 0, null, r, "[%s] Encoding Failed", GetErrorMessage(r));
194 std::unique_ptr<byte[]> pIso8859_16Byte(new (std::nothrow) byte[retLength + 1]);
195 SysTryReturn(NID_TEXT, pIso8859_16Byte, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
197 byte* pCurrPos = pIso8859_16Byte.get();
198 while (srcLength > 0)
200 if ((*pUnicodeStr) <= 0x00FF)
202 *pCurrPos = (byte) * pUnicodeStr;
204 else if (!(*pUnicodeStr & 0xFE80))
206 for (int i = 0; i <= 0x5F; i++)
208 if (ISO_8859_16_UNICODE_TABLE[i] == *pUnicodeStr)
210 *pCurrPos = (byte) (i + 0xA0);
215 else if (!(*pUnicodeStr & 0xFDE0))
217 if (*pUnicodeStr == 0x0218)
221 else if (*pUnicodeStr == 0x0219)
225 else if (*pUnicodeStr == 0x021A)
229 else if (*pUnicodeStr == 0x021B)
238 else if (!(*pUnicodeStr & 0xDF40))
240 if (*pUnicodeStr == 0x201D)
244 else if (*pUnicodeStr == 0x201E)
248 else if (*pUnicodeStr == 0x20AC)
267 SetLastResult(E_SUCCESS);
268 return pIso8859_16Byte.release();
272 _Iso885916EncodingCore::DecodeN(const byte* pSrc, int srcLength, int& retLength)
274 byte* pIso8859_16Byte = (byte*) pSrc;
275 result r = GetCharCount(pIso8859_16Byte, srcLength, retLength);
276 SysTryReturn(NID_TEXT, retLength > 0, null, r, "[%s] Encoding Failed", GetErrorMessage(r));
278 std::unique_ptr<wchar_t[]> pUnicodeStr( new (std::nothrow) wchar_t[retLength + 1]);
279 SysTryReturn(NID_TEXT, pUnicodeStr, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
281 wchar_t* pCurrPos = pUnicodeStr.get();
282 while (srcLength > 0)
284 if ((*pIso8859_16Byte) <= 0xA0)
286 *pCurrPos = (unsigned short) *pIso8859_16Byte;
288 // added following check to resolved c+=test issue, this check will always be true if the above condition is false
289 else if ((*pIso8859_16Byte) <= 0xFF)
291 *pCurrPos = ISO_8859_16_UNICODE_TABLE[*pIso8859_16Byte - 0xA0];
298 pUnicodeStr[retLength] = '\0';
299 SetLastResult(E_SUCCESS);
300 return pUnicodeStr.release();
304 _Iso885916EncodingCore::GetMaxByteCount(int charCount) const
310 _Iso885916EncodingCore::GetMaxCharCount(int byteCount) const