Change the way to convert wchar_t* to char* in CopyToCharArrayN()
authordahyeong.kim <dahyeong.kim@samsung.com>
Fri, 26 Apr 2013 06:30:09 +0000 (15:30 +0900)
committerdahyeong.kim <dahyeong.kim@samsung.com>
Fri, 26 Apr 2013 06:30:09 +0000 (15:30 +0900)
Change-Id: If838977ef26759f59772d5b980bfdf5967a072b5
Signed-off-by: dahyeong.kim <dahyeong.kim@samsung.com>
src/base/FBase_StringConverter.cpp
src/base/inc/FBaseUtil_IcuConverter.h
src/base/utility/FBaseUtil_IcuConverter.cpp

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..4470ed8 100644 (file)
@@ -37,19 +37,17 @@ class _ICUConverter
        public:
                _ICUConverter();
                ~_ICUConverter();
-
-       private:
-               result GetResultFromIcuErrorCode(UErrorCode& err);
-       public:
                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);
                void CloseConverter(void);
 
        private:
+               result GetResultFromIcuErrorCode(UErrorCode& err);
                UConverter* __pConverter;
 }; // _ICUConverter
 
+char* ConvertWcsToMbsN(const wchar_t* pValue);
 }}} // Tizen::Base::Utility
 
 #endif // _FBASE_UTIL_ICU_CONVERTER_H_
index 1569e34..b833c17 100644 (file)
@@ -201,4 +201,67 @@ _ICUConverter::GetResultFromIcuErrorCode(UErrorCode& err)
        }
        return E_SUCCESS;
 }
-} } } // Tizen::Base::Utility
+
+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);
+       if (len == 0)
+       {
+               char* pRet = new (std::nothrow) char[1];
+               SysTryReturn(NID_BASE_UTIL, pRet != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
+               pRet[0] = '\0';
+               return pRet;
+       }
+
+       UErrorCode err = U_ZERO_ERROR;
+       UConverter* pConverter = ucnv_open("UTF-8", &err);
+
+       SysTryReturn(NID_BASE_UTIL, err == U_ZERO_ERROR, null, E_INVALID_ARG, "[%s] ucnv_open() failed. The err must be U_ZERO_ERROR.", GetErrorMessage(E_INVALID_ARG));
+
+       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, , 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);
+
+               if (U_FAILURE(err))
+               {
+                       SysLogException(NID_BASE_UTIL, E_INVALID_ARG, "[%s] ucnv_fromUChars() failed. pTmpOut is %s.", GetErrorMessage(E_INVALID_ARG), pTmpOut);
+                       delete[] pOutBuf;
+                       pOutBuf = null;
+               }
+       }
+       else
+       {
+               SysLogException(NID_BASE_UTIL, E_INVALID_ARG, "[%s] u_strFromWCS() failed. pValue is %ls.", GetErrorMessage(E_INVALID_ARG), pValue);
+       }
+
+CATCH:
+       if (pConverter)
+       {
+               ucnv_close(pConverter);
+               pConverter = null;
+       }
+       delete[] pIcuStr;
+       pIcuStr = null;
+       return pOutBuf;
+}
+} } } // Tizen::Base::Utilityy