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
27 #include <unique_ptr.h>
28 #include <unicode/ustring.h>
29 #include <FBaseResult.h>
30 #include "FBase_StringConverter.h"
31 #include "FBase_NativeError.h"
32 #include <FBaseSysLog.h>
33 #include "FBaseUtil_IcuConverter.h"
36 namespace Tizen { namespace Base { namespace Utility
38 _ICUConverter::_ICUConverter(void)
43 _ICUConverter::~_ICUConverter(void)
47 ucnv_close(__pConverter);
53 _ICUConverter::OpenConverter(const Tizen::Base::String& encodingScheme)
55 std::unique_ptr< char[] > pConverterName(_StringConverter::CopyToCharArrayN(encodingScheme));
56 SysTryReturn(NID_BASE_UTIL, pConverterName != null, false, E_INVALID_ARG, "[%s] Invalid coding scheme.", GetErrorMessage(E_INVALID_ARG));
57 UErrorCode err = U_ZERO_ERROR;
59 if (!encodingScheme.CompareTo("UCS-2"))
61 __pConverter = ucnv_open("UTF-16LE", &err);
65 __pConverter = ucnv_open(pConverterName.get(), &err);
68 result r = GetResultFromIcuErrorCode(err);
69 SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, false, r, "[%s] The err must be U_ZERO_ERROR.", GetErrorMessage(r));
71 SetLastResult(E_SUCCESS);
76 _ICUConverter::CloseConverter(void)
80 ucnv_close(__pConverter);
86 _ICUConverter::ConvertFromUcharN(const wchar_t* pSrc, int srcLength, int& retLength)
88 UErrorCode errorCode = U_ZERO_ERROR;
91 ucnv_setFromUCallBack(__pConverter, UCNV_FROM_U_CALLBACK_STOP, null, null, null, &errorCode);
94 int icuStrLen = srcLength * 2;
95 UChar* pIcuStr = new (std::nothrow) UChar[icuStrLen];
96 SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
97 UChar* pResultStr = u_strFromWCS(pIcuStr, icuStrLen, &outLen, pSrc, srcLength, &errorCode);
98 if (U_SUCCESS(errorCode))
100 char* pTmpOut = null;
101 int outBytesLeftOut = srcLength * 4;
102 pOutBuf = new (std::nothrow) char[outBytesLeftOut + 1];
103 SysTryCatch(NID_BASE_UTIL, pOutBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
106 memset(pOutBuf, 0, (outBytesLeftOut + 1));
107 retLength = ucnv_fromUChars(__pConverter, pTmpOut, outBytesLeftOut, pResultStr, outLen, &errorCode);
108 r = GetResultFromIcuErrorCode(errorCode);
124 _ICUConverter::ConvertToUcharN(const char* src, int srcLength)
126 UErrorCode errorCode = U_ZERO_ERROR;
127 ucnv_setToUCallBack(__pConverter, UCNV_TO_U_CALLBACK_STOP, null, null, null, &errorCode);
128 wchar_t* pDst = null;
129 wchar_t* pResultStr = null;
130 const char* pTmpIn = src;
131 int icuStrLen = (srcLength + 1);
132 UChar* pIcuStr = new (std::nothrow) UChar[icuStrLen];
133 SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
134 memset(pIcuStr, 0, sizeof(UChar) * (icuStrLen));
136 signed int retLength = ucnv_toUChars(__pConverter, pIcuStr, icuStrLen, pTmpIn, srcLength, &errorCode);
137 result r = GetResultFromIcuErrorCode(errorCode);
138 if (U_SUCCESS(errorCode))
140 pDst = new (std::nothrow) wchar_t[retLength + 1];
141 SysTryCatch(NID_BASE_UTIL, pDst != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
142 memset(pDst, 0, sizeof(wchar_t) * (retLength + 1));
143 pResultStr = u_strToWCS(pDst, retLength, &retLength, pIcuStr, retLength, &errorCode);
144 r = GetResultFromIcuErrorCode(errorCode);
160 _ICUConverter::GetResultFromIcuErrorCode(UErrorCode& err)
168 case U_MEMORY_ALLOCATION_ERROR:
169 return E_OUT_OF_MEMORY;
171 case U_INDEX_OUTOFBOUNDS_ERROR:
172 return E_INVALID_ARG;
174 case U_TRUNCATED_CHAR_FOUND:
175 // fall through to U_INVALID_CHAR_FOUND
176 case U_INVALID_CHAR_FOUND:
177 // fall through to U_ILLEGAL_CHAR_FOUND
178 case U_ILLEGAL_CHAR_FOUND:
179 return E_INVALID_ENCODING_RANGE;
181 case U_INVALID_TABLE_FORMAT:
184 case U_BUFFER_OVERFLOW_ERROR:
187 case U_STRING_NOT_TERMINATED_WARNING:
188 return E_INVALID_ARG;
190 case U_ILLEGAL_ARGUMENT_ERROR:
191 return E_INVALID_ARG;
193 case U_FILE_ACCESS_ERROR:
194 case U_AMBIGUOUS_ALIAS_WARNING:
195 return E_UNSUPPORTED_TYPE;
203 } } } // Tizen::Base::Utility