Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / text / FText_Iso885916EncodingCore.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FText_Iso885916EncodingCore.cpp
19  * @brief               This is the implementation file for _Iso885916EncodingCore class.
20  */
21
22 // Includes
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include "FText_Iso885916EncodingCore.h"
26
27
28 using namespace Tizen::Base;
29
30 namespace Tizen { namespace Text
31 {
32
33 static const wchar_t ISO_8859_16_UNICODE_TABLE[] =
34 {
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
47 };
48
49
50 _Iso885916EncodingCore::_Iso885916EncodingCore(void)
51 {
52 }
53
54
55 _Iso885916EncodingCore::~_Iso885916EncodingCore(void)
56 {
57 }
58
59 _EncodingCore*
60 _Iso885916EncodingCore::GetEncodingCoreImplN(const Tizen::Base::String& encodingFrom, const Tizen::Base::String& encodingTo)
61 {
62         if (encodingFrom != L"ISO-8859-16")
63         {
64                 SetLastResult(E_UNSUPPORTED_TYPE);
65                 return null;
66         }
67
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));
70
71         return pEncodingCore;
72 }
73
74 result
75 _Iso885916EncodingCore::GetAvailableEncodingsN(Tizen::Base::Collection::IList* pEncodingList)
76 {
77         SysTryReturnResult(NID_TEXT, pEncodingList, E_INVALID_ARG,
78                         "[%s] Invalid argument is used. Input encoding list is null", GetErrorMessage(E_INVALID_ARG));
79
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));
82
83         result r = pEncodingList->Add(*pEncodingStr);
84         if (IsFailed(r))
85         {
86                 delete pEncodingStr;
87                 return r;
88         }
89         return E_SUCCESS;
90 }
91
92 String
93 _Iso885916EncodingCore::GetName(void)
94 {
95         return String(L"ISO-8859-16");
96 }
97
98 result
99 _Iso885916EncodingCore::GetByteCount(const wchar_t* pSrc, int srcLength, int& retLength)
100 {
101         retLength = -1;
102         wchar_t* pUnicodeStr = (wchar_t*) pSrc;
103         if (pUnicodeStr)
104         {
105                 int iso8859_16Size = srcLength;
106                 while (srcLength > 0)
107                 {
108                         if ((*pUnicodeStr) > 0x00A0)
109                         {
110                                 if (((*pUnicodeStr) > 0xFFFF))
111                                 {
112                                         return E_INVALID_ENCODING_RANGE;
113                                 }
114
115                                 if (!(*pUnicodeStr & 0xFF00))
116                                 {
117                                         if (ISO_8859_16_UNICODE_TABLE[*pUnicodeStr - 0xA0] != *pUnicodeStr)
118                                         {
119                                                 return E_INVALID_ENCODING_RANGE;
120                                         }
121                                 }
122                                 else if (!(*pUnicodeStr & 0xFE80))
123                                 {
124                                         bool invalid = true;
125                                         for (int i = 0; i <= 0x5F; i++)
126                                         {
127                                                 if (ISO_8859_16_UNICODE_TABLE[i] == *pUnicodeStr)
128                                                 {
129                                                         invalid = false;
130                                                         break;
131                                                 }
132                                         }
133                                         // check for invalid values
134                                         if (invalid)
135                                         {
136                                                 return E_INVALID_ENCODING_RANGE;
137                                         }
138                                 }
139                                 else if (!(*pUnicodeStr & 0xFDE0))
140                                 {
141                                         if ((*pUnicodeStr == 0x0218) || (*pUnicodeStr == 0x0219) || (*pUnicodeStr == 0x021A) ||
142                                                 (*pUnicodeStr == 0x021B))
143                                         {
144                                                 // empty statement
145                                         }
146                                         else
147                                         {
148                                                 return E_INVALID_ENCODING_RANGE;
149                                         }
150                                 }
151                                 else if (!(*pUnicodeStr & 0xDF40))
152                                 {
153                                         if ((*pUnicodeStr == 0x201D) || (*pUnicodeStr == 0x201E) || (*pUnicodeStr == 0x20AC))
154                                         {
155                                                 // empty statement
156                                         }
157                                         else
158                                         {
159                                                 return E_INVALID_ENCODING_RANGE;
160                                         }
161                                 }
162                                 else
163                                 {
164                                         return E_INVALID_ENCODING_RANGE;
165                                 }
166                         }
167                         pUnicodeStr++;
168                         srcLength--;
169                 }
170                 retLength = iso8859_16Size;
171                 return E_SUCCESS;
172         }
173
174         return E_INVALID_ARG;
175 }
176
177 result
178 _Iso885916EncodingCore::GetCharCount(const byte* pSrc, int srcLength, int& retLength)
179 {
180         retLength = -1;
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;
184         return E_SUCCESS;
185 }
186
187 byte*
188 _Iso885916EncodingCore::EncodeN(const wchar_t* pSrc, int srcLength, int& retLength)
189 {
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));
193
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));
196
197         byte* pCurrPos = pIso8859_16Byte.get();
198         while (srcLength > 0)
199         {
200                 if ((*pUnicodeStr) <= 0x00FF)
201                 {
202                         *pCurrPos = (byte) * pUnicodeStr;
203                 }
204                 else if (!(*pUnicodeStr & 0xFE80))
205                 {
206                         for (int i = 0; i <= 0x5F; i++)
207                         {
208                                 if (ISO_8859_16_UNICODE_TABLE[i] == *pUnicodeStr)
209                                 {
210                                         *pCurrPos = (byte) (i + 0xA0);
211                                         break;
212                                 }
213                         }
214                 }
215                 else if (!(*pUnicodeStr & 0xFDE0))
216                 {
217                         if (*pUnicodeStr == 0x0218)
218                         {
219                                 *pCurrPos = 0xAA;
220                         }
221                         else if (*pUnicodeStr == 0x0219)
222                         {
223                                 *pCurrPos = 0xBA;
224                         }
225                         else if (*pUnicodeStr == 0x021A)
226                         {
227                                 *pCurrPos = 0xDE;
228                         }
229                         else if (*pUnicodeStr == 0x021B)
230                         {
231                                 *pCurrPos = 0xFE;
232                         }
233                         else
234                         {
235                                 return null;
236                         }
237                 }
238                 else if (!(*pUnicodeStr & 0xDF40))
239                 {
240                         if (*pUnicodeStr == 0x201D)
241                         {
242                                 *pCurrPos = 0xB5;
243                         }
244                         else if (*pUnicodeStr == 0x201E)
245                         {
246                                 *pCurrPos = 0xA5;
247                         }
248                         else if (*pUnicodeStr == 0x20AC)
249                         {
250                                 *pCurrPos = 0xA4;
251                         }
252                         else
253                         {
254                                 return null;
255                         }
256                 }
257                 else
258                 {
259                         return null;
260                 }
261
262                 pCurrPos++;
263                 pUnicodeStr++;
264                 srcLength--;
265         }
266
267         SetLastResult(E_SUCCESS);
268         return pIso8859_16Byte.release();
269 }
270
271 wchar_t*
272 _Iso885916EncodingCore::DecodeN(const byte* pSrc, int srcLength, int& retLength)
273 {
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));
277
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));
280
281         wchar_t* pCurrPos = pUnicodeStr.get();
282         while (srcLength > 0)
283         {
284                 if ((*pIso8859_16Byte) <= 0xA0)
285                 {
286                         *pCurrPos = (unsigned short) *pIso8859_16Byte;
287                 }
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)
290                 {
291                         *pCurrPos = ISO_8859_16_UNICODE_TABLE[*pIso8859_16Byte - 0xA0];
292                 }
293                 pCurrPos++;
294                 pIso8859_16Byte++;
295                 srcLength--;
296         }
297
298         pUnicodeStr[retLength] = '\0';
299         SetLastResult(E_SUCCESS);
300         return pUnicodeStr.release();
301 }
302
303 int
304 _Iso885916EncodingCore::GetMaxByteCount(int charCount) const
305 {
306         return charCount;
307 }
308
309 int
310 _Iso885916EncodingCore::GetMaxCharCount(int byteCount) const
311 {
312         return byteCount;
313 }
314
315 } } // Tizen::Text