2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @File : FBaseUtil_ICUConverter.cpp
20 * @brief : Implementation for _ICUConverter Class
28 #include <unique_ptr.h>
29 #include <unicode/ustring.h>
30 #include <FBaseResult.h>
31 #include "FBase_StringConverter.h"
32 #include "FBase_NativeError.h"
33 #include <FBaseSysLog.h>
34 #include "FBaseUtil_IcuConverter.h"
37 namespace Tizen { namespace Base { namespace Utility
39 _ICUConverter::_ICUConverter(void)
44 _ICUConverter::~_ICUConverter(void)
48 ucnv_close(__pConverter);
54 _ICUConverter::OpenConverter(const Tizen::Base::String& encodingScheme)
56 std::unique_ptr< char[] > pConverterName(_StringConverter::CopyToCharArrayN(encodingScheme));
57 SysTryReturn(NID_BASE_UTIL, pConverterName != null, false, E_INVALID_ARG, "[%s] Invalid coding scheme.", GetErrorMessage(E_INVALID_ARG));
58 UErrorCode err = U_ZERO_ERROR;
60 if (!encodingScheme.CompareTo("UCS-2"))
62 __pConverter = ucnv_open("UTF-16LE", &err);
66 __pConverter = ucnv_open(pConverterName.get(), &err);
69 result r = GetResultFromIcuErrorCode(err);
70 SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, r, "[%s] The err must be U_ZERO_ERROR.", GetErrorMessage(r));
72 SetLastResult(E_SUCCESS);
77 _ICUConverter::CloseConverter(void)
81 ucnv_close(__pConverter);
87 _ICUConverter::ConvertFromUcharN(const wchar_t* pSrc, int srcLength, int& retLength)
89 UErrorCode errorCode = U_ZERO_ERROR;
92 ucnv_setFromUCallBack(__pConverter, UCNV_FROM_U_CALLBACK_STOP, null, null, null, &errorCode);
95 int icuStrLen = srcLength * 2;
96 UChar* pIcuStr = new (std::nothrow) UChar[icuStrLen];
97 SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
98 UChar* pResultStr = u_strFromWCS(pIcuStr, icuStrLen, &outLen, pSrc, srcLength, &errorCode);
99 if (U_SUCCESS(errorCode))
101 char* pTmpOut = null;
102 int outBytesLeftOut = srcLength * 4;
103 pOutBuf = new (std::nothrow) char[outBytesLeftOut + 1];
104 SysTryCatch(NID_BASE_UTIL, pOutBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
107 memset(pOutBuf, 0, (outBytesLeftOut + 1));
108 retLength = ucnv_fromUChars(__pConverter, pTmpOut, outBytesLeftOut, pResultStr, outLen, &errorCode);
109 r = GetResultFromIcuErrorCode(errorCode);
125 _ICUConverter::ConvertToUcharN(const char* src, int srcLength)
127 UErrorCode errorCode = U_ZERO_ERROR;
128 ucnv_setToUCallBack(__pConverter, UCNV_TO_U_CALLBACK_STOP, null, null, null, &errorCode);
129 wchar_t* pDst = null;
130 wchar_t* pResultStr = null;
131 const char* pTmpIn = src;
132 int icuStrLen = (srcLength + 1);
133 UChar* pIcuStr = new (std::nothrow) UChar[icuStrLen];
134 SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
135 memset(pIcuStr, 0, sizeof(UChar) * (icuStrLen));
137 signed int retLength = ucnv_toUChars(__pConverter, pIcuStr, icuStrLen, pTmpIn, srcLength, &errorCode);
138 result r = GetResultFromIcuErrorCode(errorCode);
139 if (U_SUCCESS(errorCode))
141 pDst = new (std::nothrow) wchar_t[retLength + 1];
142 SysTryCatch(NID_BASE_UTIL, pDst != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
143 memset(pDst, 0, sizeof(wchar_t) * (retLength + 1));
144 pResultStr = u_strToWCS(pDst, retLength, &retLength, pIcuStr, retLength, &errorCode);
145 r = GetResultFromIcuErrorCode(errorCode);
161 _ICUConverter::GetResultFromIcuErrorCode(UErrorCode& err)
169 case U_MEMORY_ALLOCATION_ERROR:
170 return E_OUT_OF_MEMORY;
172 case U_INDEX_OUTOFBOUNDS_ERROR:
173 return E_INVALID_ARG;
175 case U_TRUNCATED_CHAR_FOUND:
176 // fall through to U_INVALID_CHAR_FOUND
177 case U_INVALID_CHAR_FOUND:
178 // fall through to U_ILLEGAL_CHAR_FOUND
179 case U_ILLEGAL_CHAR_FOUND:
180 return E_INVALID_ENCODING_RANGE;
182 case U_INVALID_TABLE_FORMAT:
185 case U_BUFFER_OVERFLOW_ERROR:
188 case U_STRING_NOT_TERMINATED_WARNING:
189 return E_INVALID_ARG;
191 case U_ILLEGAL_ARGUMENT_ERROR:
192 return E_INVALID_ARG;
194 case U_FILE_ACCESS_ERROR:
195 case U_AMBIGUOUS_ALIAS_WARNING:
196 return E_UNSUPPORTED_TYPE;
206 ConvertWcsToMbsN(const wchar_t* pValue)
208 SysTryReturn(NID_BASE_UTIL, pValue != null, null, E_INVALID_ARG, "[%s] Invalid argument is used. The pValue is null.", GetErrorMessage(E_INVALID_ARG));
210 int len = wcslen(pValue);
212 UErrorCode err = U_ZERO_ERROR;
213 UConverter* pConverter = ucnv_open("UTF-8", &err);
215 result r = _ICUConverter::GetResultFromIcuErrorCode(err);
216 SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, null, r, "[%s] The err must be U_ZERO_ERROR.", GetErrorMessage(r));
218 ucnv_setFromUCallBack(pConverter, UCNV_FROM_U_CALLBACK_STOP, null, null, null, &err);
220 int icuStrLen = len * 2;
221 UChar* pIcuStr = new (std::nothrow) UChar[icuStrLen];
222 SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
225 UChar* pResultStr = u_strFromWCS(pIcuStr, icuStrLen, &outLen, pValue, len, &err);
227 char* pOutBuf = null;
230 char* pTmpOut = null;
231 int outBytesLeftOut = len * 4;
232 pOutBuf = new (std::nothrow) char[outBytesLeftOut + 1];
233 SysTryCatch(NID_BASE_UTIL, pOutBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
236 memset(pOutBuf, 0, outBytesLeftOut + 1);
237 int retLength = ucnv_fromUChars(pConverter, pTmpOut, outBytesLeftOut, pResultStr, outLen, &err);
239 r = _ICUConverter::GetResultFromIcuErrorCode(err);
250 ucnv_close(pConverter);
260 ConvertMbsToWcsN(const char* pValue)
262 int len = strlen(pValue);
263 SysTryReturn(NID_BASE_UTIL, len != 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. The pValue is an empty string.", GetErrorMessage(E_INVALID_ARG));
265 UErrorCode err = U_ZERO_ERROR;
266 UConverter* pConverter = ucnv_open("UTF-8", &err);
268 result r = _ICUConverter::GetResultFromIcuErrorCode(err);
269 SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, null, r, "[%s] The err must be U_ZERO_ERROR.", GetErrorMessage(r));
271 ucnv_setFromUCallBack(pConverter, UCNV_FROM_U_CALLBACK_STOP, null, null, null, &err);
273 int icuStrLen = len + 1;
274 UChar* pIcuStr = new (std::nothrow) UChar[icuStrLen];
275 SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
276 memset(pIcuStr, 0, sizeof(UChar) * (icuStrLen));
278 const char* pTmpIn = pValue;
279 signed int retLength = ucnv_toUChars(pConverter, pIcuStr, icuStrLen, pTmpIn, len, &err);
281 wchar_t* pDst = null;
282 wchar_t* pResultStr = null;
285 pDst = new (std::nothrow) wchar_t[retLength + 1];
286 SysTryCatch(NID_BASE_UTIL, pDst != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
287 memset(pDst, 0, sizeof(wchar_t) * (retLength + 1));
288 pResultStr = u_strToWCS(pDst, retLength, &retLength, pIcuStr, retLength, &err);
290 r = _ICUConverter::GetResultFromIcuErrorCode(err);
301 ucnv_close(pConverter);
310 } } } // Tizen::Base::Utility