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 : FBaseUtil_ICUConverter.cpp
19 * @brief : Implementation for _ICUConverter Class
26 #include <unique_ptr.h>
27 #include <unicode/ustring.h>
28 #include <FBaseResult.h>
29 #include "FBase_StringConverter.h"
30 #include "FBase_NativeError.h"
31 #include <FBaseSysLog.h>
32 #include "FBaseUtil_IcuConverter.h"
34 namespace Tizen { namespace Base { namespace Utility
36 _ICUConverter::_ICUConverter(void)
41 _ICUConverter::~_ICUConverter(void)
45 ucnv_close(__pConverter);
51 _ICUConverter::OpenConverter(const Tizen::Base::String& encodingScheme)
53 std::unique_ptr< char[] > pConverterName(_StringConverter::CopyToCharArrayN(encodingScheme));
54 SysTryReturn(NID_BASE_UTIL, pConverterName != null, false, E_INVALID_ARG, "[%s] Invalid coding scheme.", GetErrorMessage(E_INVALID_ARG));
55 UErrorCode err = U_ZERO_ERROR;
57 if (!encodingScheme.CompareTo("UCS-2"))
59 __pConverter = ucnv_open("UTF-16LE", &err);
63 __pConverter = ucnv_open(pConverterName.get(), &err);
66 result r = GetResultFromIcuErrorCode(err);
67 SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, r, "[%s] The err must be U_ZERO_ERROR.", GetErrorMessage(r));
69 SetLastResult(E_SUCCESS);
74 _ICUConverter::CloseConverter(void)
78 ucnv_close(__pConverter);
84 _ICUConverter::ConvertFromUcharN(const wchar_t* pSrc, int srcLength, int& retLength)
86 UErrorCode errorCode = U_ZERO_ERROR;
89 ucnv_setFromUCallBack(__pConverter, UCNV_FROM_U_CALLBACK_STOP, null, null, null, &errorCode);
92 int icuStrLen = srcLength * 2;
93 UChar* pIcuStr = new (std::nothrow) UChar[icuStrLen];
94 SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
95 UChar* pResultStr = u_strFromWCS(pIcuStr, icuStrLen, &outLen, pSrc, srcLength, &errorCode);
96 if (U_SUCCESS(errorCode))
99 int outBytesLeftOut = srcLength * 4;
100 pOutBuf = new (std::nothrow) char[outBytesLeftOut + 1];
101 SysTryCatch(NID_BASE_UTIL, pOutBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
104 memset(pOutBuf, 0, (outBytesLeftOut + 1));
105 retLength = ucnv_fromUChars(__pConverter, pTmpOut, outBytesLeftOut, pResultStr, outLen, &errorCode);
106 r = GetResultFromIcuErrorCode(errorCode);
122 _ICUConverter::ConvertToUcharN(const char* src, int srcLength)
124 UErrorCode errorCode = U_ZERO_ERROR;
125 ucnv_setToUCallBack(__pConverter, UCNV_TO_U_CALLBACK_STOP, null, null, null, &errorCode);
126 wchar_t* pDst = null;
127 wchar_t* pResultStr = null;
128 const char* pTmpIn = src;
129 int icuStrLen = (srcLength + 1);
130 UChar* pIcuStr = new (std::nothrow) UChar[icuStrLen];
131 SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
132 memset(pIcuStr, 0, sizeof(UChar) * (icuStrLen));
134 signed int retLength = ucnv_toUChars(__pConverter, pIcuStr, icuStrLen, pTmpIn, srcLength, &errorCode);
135 result r = GetResultFromIcuErrorCode(errorCode);
136 if (U_SUCCESS(errorCode))
138 pDst = new (std::nothrow) wchar_t[retLength + 1];
139 SysTryCatch(NID_BASE_UTIL, pDst != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
140 memset(pDst, 0, sizeof(wchar_t) * (retLength + 1));
141 pResultStr = u_strToWCS(pDst, retLength, &retLength, pIcuStr, retLength, &errorCode);
142 r = GetResultFromIcuErrorCode(errorCode);
158 _ICUConverter::GetResultFromIcuErrorCode(UErrorCode& err)
167 case U_MEMORY_ALLOCATION_ERROR:
168 return E_OUT_OF_MEMORY;
170 case U_INDEX_OUTOFBOUNDS_ERROR:
171 return E_INVALID_ARG;
173 case U_TRUNCATED_CHAR_FOUND:
174 // fall through to U_INVALID_CHAR_FOUND
175 case U_INVALID_CHAR_FOUND:
176 // fall through to U_ILLEGAL_CHAR_FOUND
177 case U_ILLEGAL_CHAR_FOUND:
178 return E_INVALID_ENCODING_RANGE;
180 case U_INVALID_TABLE_FORMAT:
183 case U_BUFFER_OVERFLOW_ERROR:
186 case U_STRING_NOT_TERMINATED_WARNING:
187 return E_INVALID_ARG;
189 case U_ILLEGAL_ARGUMENT_ERROR:
190 return E_INVALID_ARG;
192 case U_FILE_ACCESS_ERROR:
193 case U_AMBIGUOUS_ALIAS_WARNING:
194 return E_UNSUPPORTED_TYPE;
204 struct UConverterDeleter
206 void operator ()(UConverter* pConverter)
210 ucnv_close(pConverter);
216 WcharToUtf8N(const wchar_t* pValue)
218 SysTryReturn(NID_BASE_UTIL, pValue != null, null, E_INVALID_ARG, "[%s] Invalid argument is used. The pValue is null.", GetErrorMessage(E_INVALID_ARG));
220 int len = wcslen(pValue);
223 char* pRet = new (std::nothrow) char[1];
224 SysTryReturn(NID_BASE_UTIL, pRet != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
229 UErrorCode err = U_ZERO_ERROR;
230 std::unique_ptr< UConverter, UConverterDeleter > pConverter(ucnv_open("UTF-8", &err));
231 SysTryReturn(NID_BASE_UTIL, pConverter, null, E_OUT_OF_MEMORY, "[%s] ucnv_open() failed. pConverter is null.", GetErrorMessage(E_OUT_OF_MEMORY));
232 SysTryReturn(NID_BASE_UTIL, U_SUCCESS(err), null, E_INVALID_ARG, "[%s] ucnv_open() failed. The err must be U_ZERO_ERROR.", GetErrorMessage(E_INVALID_ARG));
234 ucnv_setFromUCallBack(pConverter.get(), UCNV_FROM_U_CALLBACK_STOP, null, null, null, &err);
236 int icuStrCapacity = len * 2 + 1;
237 std::unique_ptr< UChar[] > pIcuStr(new (std::nothrow) UChar[icuStrCapacity]);
238 SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
241 UChar* pResultStr = u_strFromWCS(pIcuStr.get(), icuStrCapacity, &outLen, pValue, len, &err);
242 SysTryReturn(NID_BASE_UTIL, U_SUCCESS(err), null, E_INVALID_ARG, "[%s] u_strFromWCS() failed. pValue is %ls.", GetErrorMessage(E_INVALID_ARG), pValue);
244 int destCapacity = len * 4 + 1;
245 std::unique_ptr< char[] > pDst(new (std::nothrow) char[destCapacity]);
246 SysTryReturn(NID_BASE_UTIL, pDst != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
248 ucnv_fromUChars(pConverter.get(), pDst.get(), destCapacity, pResultStr, outLen, &err);
249 SysTryReturn(NID_BASE_UTIL, U_SUCCESS(err), null, E_INVALID_ARG, "[%s] ucnv_fromUChars() failed. pValue is %ls.", GetErrorMessage(E_INVALID_ARG), pValue);
251 return pDst.release();
255 Utf8ToWcharN(const char* pValue)
257 SysTryReturn(NID_BASE_UTIL, pValue != null, null, E_INVALID_ARG, "[%s] Invalid argument is used. The pValue is null.", GetErrorMessage(E_INVALID_ARG));
259 UErrorCode err = U_ZERO_ERROR;
260 std::unique_ptr< UConverter, UConverterDeleter > pConverter(ucnv_open("UTF-8", &err));
261 SysTryReturn(NID_BASE_UTIL, pConverter, null, E_OUT_OF_MEMORY, "[%s] ucnv_open() failed. pConverter is null.", GetErrorMessage(E_OUT_OF_MEMORY));
262 SysTryReturn(NID_BASE_UTIL, U_SUCCESS(err), null, E_INVALID_ARG, "[%s] ucnv_open() failed. The err must be U_ZERO_ERROR.", GetErrorMessage(E_INVALID_ARG));
264 ucnv_setFromUCallBack(pConverter.get(), UCNV_FROM_U_CALLBACK_STOP, null, null, null, &err);
266 int len = strlen(pValue);
267 int icuStrCapacity = len + 1;
268 std::unique_ptr< UChar[] > pIcuStr(new (std::nothrow) UChar[icuStrCapacity]);
269 SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
271 signed int retLength = ucnv_toUChars(pConverter.get(), pIcuStr.get(), icuStrCapacity, pValue, len, &err);
272 SysTryReturn(NID_BASE_UTIL, U_SUCCESS(err), null, E_INVALID_ARG, "[%s] ucnv_toUChars() failed. pValue is %s.", GetErrorMessage(E_INVALID_ARG), pValue);
274 int destCapacity = retLength + String::DEFAULT_CAPACITY + 1;
275 std::unique_ptr< wchar_t[] > pDst(new (std::nothrow) wchar_t[destCapacity]);
276 SysTryReturn(NID_BASE_UTIL, pDst != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
278 u_strToWCS(pDst.get(), destCapacity, &retLength, pIcuStr.get(), retLength, &err);
279 SysTryReturn(NID_BASE_UTIL, U_SUCCESS(err), null, E_INVALID_ARG, "[%s] u_strToWCS() failed. pValue is %s.", GetErrorMessage(E_INVALID_ARG), pValue);
281 return pDst.release();
283 }}} // Tizen::Base::Utility