Change the way to conver Mbs to Wcs and vice versa
authordahyeong.kim <dahyeong.kim@samsung.com>
Wed, 17 Apr 2013 13:23:58 +0000 (22:23 +0900)
committerdahyeong.kim <dahyeong.kim@samsung.com>
Wed, 17 Apr 2013 13:23:58 +0000 (22:23 +0900)
Change-Id: I2a22ad56c0683c7fe82a3a02912c29946dce5db6
Signed-off-by: dahyeong.kim <dahyeong.kim@samsung.com>
src/base/FBaseString.cpp
src/base/FBase_StringConverter.cpp
src/base/inc/FBaseUtil_IcuConverter.h
src/base/utility/FBaseUtil_IcuConverter.cpp

index 24dda81..900b491 100644 (file)
@@ -38,7 +38,7 @@
 #include <FBaseResult.h>
 #include <FBaseSysLog.h>
 #include <unique_ptr.h>
-
+#include "FBaseUtil_IcuConverter.h"
 
 namespace Tizen { namespace Base
 {
@@ -127,25 +127,23 @@ String::String(const char* pValue)
        , __pValue(null)
        , __pStringImpl(null)
 {
-       int len = (pValue != null) ? mbstowcs(null, pValue, 0) : 0;
-
-       if (pValue == null)
+       if (pValue == null || strlen(pValue) == 0)
        {
                result r = InitializeToDefault(DEFAULT_CAPACITY);
                SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
        }
        else
        {
-               result r = InitializeToDefault(len + DEFAULT_CAPACITY);
-               SysTryReturnVoidResult(NID_BASE, r == E_SUCCESS, E_OUT_OF_MEMORY, "Memory allocation failed.");
+               std::unique_ptr< wchar_t[] > pStr(Tizen::Base::Utility::ConvertMbsToWcsN(pValue));
+               SysTryReturnVoidResult(NID_BASE, pStr != null, GetLastResult(), "Propagating.");
 
-               len = mbstowcs(__pValue, pValue, len);
-               if (len == -1)
-               {
-                       SysLog(NID_BASE, "Invalid encoding range[%s].", pValue);
-               }
-               __pValue[len] = '\0';
-               __length = len;
+               int strLen = wcslen(pStr.get());
+               std::unique_ptr<int> pRefCntTemp(new (std::nothrow) int(1));
+               SysTryReturnVoidResult(NID_BASE, pRefCntTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
+               __pRefCount = pRefCntTemp.release();
+               __pValue = pStr.release();
+               __length = strLen;
+               __capacity = strLen + 1;
        }
 }
 
index 05d8133..be93bb2 100644 (file)
 * @brief               This is the implementation for _StringConverter class.
 */
 
-#include <stdlib.h>
 #include <new>
 #include <FBaseResult.h>
-#include "FBase_StringConverter.h"
 #include <FBaseSysLog.h>
-
-
+#include "FBase_StringConverter.h"
+#include "FBaseUtil_IcuConverter.h"
 
 namespace Tizen { namespace Base
 {
@@ -40,20 +38,7 @@ _StringConverter::CopyToCharArrayN(const String& str)
 char*
 _StringConverter::CopyToCharArrayN(const wchar_t* pValue)
 {
-       char* pRet = null;
-
-       int len = wcstombs(0, pValue, 0);
-       SysTryReturn(NID_BASE, len != -1, null, E_INVALID_ARG, "[%s] Invalid argument is used. Invalid string.",
-               GetErrorMessage(E_INVALID_ARG));
-
-       pRet = new (std::nothrow) char[len + 1];
-       SysTryReturn(NID_BASE, pRet != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
-               GetErrorMessage(E_OUT_OF_MEMORY));
-
-       len = wcstombs(pRet, pValue, len);
-       pRet[len] = 0;
-
-       return pRet;
+       return Tizen::Base::Utility::ConvertWcsToMbsN(pValue);
 }
 
 }} //Tizen::Base
index 192622b..c0a1e25 100644 (file)
@@ -37,10 +37,7 @@ class _ICUConverter
        public:
                _ICUConverter();
                ~_ICUConverter();
-
-       private:
-               result GetResultFromIcuErrorCode(UErrorCode& err);
-       public:
+               static result GetResultFromIcuErrorCode(UErrorCode& err);
                bool OpenConverter(const Tizen::Base::String& encodingScheme);
                wchar_t* ConvertToUcharN(const char* src, int srcLength);
                char* ConvertFromUcharN(const wchar_t* pSrc, int srcLength, int& retLength);
@@ -50,6 +47,9 @@ class _ICUConverter
                UConverter* __pConverter;
 }; // _ICUConverter
 
+char* ConvertWcsToMbsN(const wchar_t* pValue);
+wchar_t* ConvertMbsToWcsN(const char* pValue);
+
 }}} // Tizen::Base::Utility
 
 #endif // _FBASE_UTIL_ICU_CONVERTER_H_
index 1569e34..38c4aa7 100644 (file)
@@ -201,4 +201,111 @@ _ICUConverter::GetResultFromIcuErrorCode(UErrorCode& err)
        }
        return E_SUCCESS;
 }
+
+char*
+ConvertWcsToMbsN(const wchar_t* pValue)
+{
+       SysTryReturn(NID_BASE_UTIL, pValue != null, null, E_INVALID_ARG, "[%s] Invalid argument is used. The pValue is null.", GetErrorMessage(E_INVALID_ARG));
+
+       int len = wcslen(pValue);
+       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));
+
+       UErrorCode err = U_ZERO_ERROR;
+       UConverter* pConverter = ucnv_open("UTF-8", &err);
+
+       result r = _ICUConverter::GetResultFromIcuErrorCode(err);
+       SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, null, r, "[%s] The err must be U_ZERO_ERROR.", GetErrorMessage(r));
+
+       ucnv_setFromUCallBack(pConverter, UCNV_FROM_U_CALLBACK_STOP, null, null, null, &err);
+
+       int icuStrLen = len * 2;
+       UChar* pIcuStr = new (std::nothrow) UChar[icuStrLen];
+       SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+       
+       int outLen = 0;
+       UChar* pResultStr = u_strFromWCS(pIcuStr, icuStrLen, &outLen, pValue, len, &err);
+
+       char* pOutBuf = null;
+       if (U_SUCCESS(err))
+       {
+               char* pTmpOut = null;
+               int outBytesLeftOut = len * 4;
+               pOutBuf = new (std::nothrow) char[outBytesLeftOut + 1];
+               SysTryCatch(NID_BASE_UTIL, pOutBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+
+               pTmpOut = pOutBuf;
+               memset(pOutBuf, 0, outBytesLeftOut + 1);
+               int retLength = ucnv_fromUChars(pConverter, pTmpOut, outBytesLeftOut, pResultStr, outLen, &err);
+               
+               r = _ICUConverter::GetResultFromIcuErrorCode(err);
+               if (IsFailed(r))
+               {
+                       delete[] pOutBuf;
+                       pOutBuf = null;
+               }
+       }
+
+CATCH:
+       if (pConverter)
+       {
+               ucnv_close(pConverter);
+               pConverter = null;
+       }
+       delete[] pIcuStr;
+       pIcuStr = null;
+       SetLastResult(r);
+       return pOutBuf;
+}
+
+wchar_t*
+ConvertMbsToWcsN(const char* pValue)
+{
+       int len = strlen(pValue);
+       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));
+
+       UErrorCode err = U_ZERO_ERROR;
+       UConverter* pConverter = ucnv_open("UTF-8", &err);
+
+       result r = _ICUConverter::GetResultFromIcuErrorCode(err);
+       SysTryReturn(NID_BASE_UTIL, r == E_SUCCESS, null, r, "[%s] The err must be U_ZERO_ERROR.", GetErrorMessage(r));
+
+       ucnv_setFromUCallBack(pConverter, UCNV_FROM_U_CALLBACK_STOP, null, null, null, &err);
+
+       int icuStrLen = len + 1;
+       UChar* pIcuStr = new (std::nothrow) UChar[icuStrLen];
+       SysTryReturn(NID_BASE_UTIL, pIcuStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+       memset(pIcuStr, 0, sizeof(UChar) * (icuStrLen));
+
+       const char* pTmpIn = pValue;
+       signed int retLength = ucnv_toUChars(pConverter, pIcuStr, icuStrLen, pTmpIn, len, &err);
+
+       wchar_t* pDst = null;
+       wchar_t* pResultStr = null;
+       if (U_SUCCESS(err))
+       {
+               pDst = new (std::nothrow) wchar_t[retLength + 1];
+               SysTryCatch(NID_BASE_UTIL, pDst != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+               memset(pDst, 0, sizeof(wchar_t) * (retLength + 1));
+               pResultStr = u_strToWCS(pDst, retLength, &retLength, pIcuStr, retLength, &err);
+               
+               r = _ICUConverter::GetResultFromIcuErrorCode(err);
+               if (IsFailed(r))
+               {
+                       delete[] pDst;
+                       pDst = null;
+               }
+       }
+
+CATCH:
+       if (pConverter)
+       {
+               ucnv_close(pConverter);
+               pConverter = null;
+       }
+       SetLastResult(r);
+       delete[] pIcuStr;
+       pIcuStr = null;
+       return pResultStr;
+}
+
 } } } // Tizen::Base::Utility