diff -r bb1748e0dd9b -r c6e8afe0ba85 secureswitools/swisistools/source/common/util.cpp --- a/secureswitools/swisistools/source/common/util.cpp Tue Jun 29 16:50:12 2010 +0300 +++ b/secureswitools/swisistools/source/common/util.cpp Thu Jul 08 20:28:00 2010 +0300 @@ -1,5 +1,5 @@ /* -* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). +* Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). * All rights reserved. * This component and the accompanying materials are made available * under the terms of the License "Eclipse Public License v1.0" @@ -24,7 +24,6 @@ #include "util.h" #include "symbiantypes.h" -#include #include #include #include @@ -33,6 +32,13 @@ #include #include +#ifdef _WIN32 +#include +#endif // _WIN32 + + +#include "utf8_wrapper.h" + static const TUint32 CrcTab32[256] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, @@ -100,18 +106,96 @@ 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; + +namespace Util +{ +/** + * Converts wide char (unicode) string to multibyte string + * This interface is provided so that we can have different implementation + * in windows and linux machine. + * @param aSource string to be converted + * @param aSourceLen Source len. If this is -1 then it will calculate the length of the source. + * @param aTarget target location. + * @param aTargetLen Space in the target location. + * @param aCodePage Code page number (currently supported in windows only) + * @return Number of bytes that make up the converted part of multibyte sequence. + * If aTarget is NULL then the function will return the size needed to store + * the complete conversion of the source string. + */ +int ConvertWideCharToMultiByte(const wchar_t* aSource, int aSourceLen, char* aTarget, int aTargetLen, TUint32 aCodePage = 0); +/** + * Converts multibyte string to wide char (unicode) + * This interface is provided so that we can have different implementation + * in windows and linux machine. + * @param aSource string to be converted + * @param aSourceLen Source len. If this is -1 then it will calculate the length of the source. + * @param aTarget target location. + * @param aTargetLen Space in the target location. + * @param aCodePage Code page number (currently supported in windows only) + * @return Number of bytes that make up the converted part of widechar sequence. + * If aTarget is NULL then the function will return the size needed to store + * the complete conversion of the source string. + */ +int ConvertMultiByteToWideChar(const char* aSource, int aSourceLen, wchar_t* aTarget, int aTargetLen, TUint32 aCodePage = 0); +}; // namespace Util +#ifdef __linux__ +int Util::ConvertWideCharToMultiByte(const wchar_t* aSource, int /*aSourceLen*/, char* aTarget, int aTargetLen, TUint32 /*aCodePage*/) + { + int retValue = wcstombs(aTarget, aSource, aTargetLen); + if (-1 == retValue) + { + return 0; + } + return retValue; + } + +int Util::ConvertMultiByteToWideChar(const char* aSource, int /*aSourceLen*/, wchar_t* aTarget, int aTargetLen, TUint32 /*aCodePage*/) + { + int retValue = mbstowcs(aTarget, aSource, aTargetLen); + if (-1 == retValue) + { + return 0; + } + return retValue; + } + +#else + +int Util::ConvertWideCharToMultiByte(const wchar_t* aSource, int aSourceLen, char* aTarget, int aTargetLen, TUint32 aCodePage) + { + if(0 == aCodePage) + { + aCodePage = CP_OEMCP; + } + return WideCharToMultiByte( aCodePage, 0, aSource, aSourceLen, aTarget, aTargetLen, NULL, NULL); + } + +int Util::ConvertMultiByteToWideChar(const char* aSource, int aSourceLen, wchar_t* aTarget, int aTargetLen, TUint32 aCodePage) + { + if(0 == aCodePage) + { + aCodePage = CP_OEMCP; + } + return MultiByteToWideChar( aCodePage, 0, aSource, aSourceLen, aTarget, aTargetLen); + } + +#endif // __linux__ + + +#ifndef __linux__ +/* DllExport std::string Util::wstring2string (const std::wstring& aWide) { - int max = WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),0,0,0,0); + int max = ConvertWideCharToMultiByte(aWide.c_str(),aWide.length(),0,0); std::string reply; if (max > 0 ) { char* buffer = new char [max]; try { - WideCharToMultiByte(CP_OEMCP,0,aWide.c_str(),aWide.length(),buffer,max,0,0); + ConvertWideCharToMultiByte(aWide.c_str(),aWide.length(),buffer,max); reply = std::string (buffer, max); } catch (...) @@ -124,14 +208,37 @@ std::wstring Util::string2wstring (const std::string& aNarrow) { - int max = MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),0,0); + int max = ConvertMultiByteToWideChar(aNarrow.c_str(),aNarrow.length(),0,0); std::wstring reply; if (max > 0 ) { wchar_t* buffer = new wchar_t [max]; try { - MultiByteToWideChar(CP_OEMCP,0,aNarrow.c_str(),aNarrow.length(),buffer,max); + ConvertMultiByteToWideChar(aNarrow.c_str(),aNarrow.length(),buffer,max); + reply = std::wstring (buffer, max); + } + catch (...) + { + } + delete [] buffer; + } + return reply; + } +*/ +#endif + +std::wstring Util::string2wstring (const char* aNarrow) + { + std::string narrowStr(aNarrow); + int max = ConvertMultiByteToWideChar(aNarrow, strlen(aNarrow),0,0); + std::wstring reply; + if (max > 0 ) + { + wchar_t* buffer = new wchar_t [max]; + try + { + ConvertMultiByteToWideChar(aNarrow, strlen(aNarrow),buffer,max); reply = std::wstring (buffer, max); } catch (...) @@ -142,26 +249,6 @@ return reply; } -std::wstring Util::string2wstring (const char* aNarrow) - { - std::string narrowStr(aNarrow); - int max = MultiByteToWideChar(CP_OEMCP,0,narrowStr.c_str(),narrowStr.length(),0,0); - std::wstring reply; - if (max > 0 ) - { - wchar_t* buffer = new wchar_t [max]; - try - { - MultiByteToWideChar(CP_OEMCP,0,narrowStr.c_str(),narrowStr.length(),buffer,max); - reply = std::wstring (buffer, max); - } - catch (...) - { - } - delete [] buffer; - } - return reply; - } DllExport int Util::WideCharToInteger(const wchar_t* aWideChar) { @@ -173,9 +260,10 @@ TInt64 Util::WideCharToInt64(const wchar_t* aWideChar) { - __int64 i64 = 0; - swscanf(aWideChar, L"%I64d", &i64); - return i64; + TInt64 value=0; + std::wstringstream str(aWideChar); + str >> value; + return value; } DllExport const std::wstring Util::IntegerToWideString(int aInt) @@ -230,4 +318,7 @@ crc = (crc >> 8) ^ CrcTab32[(crc ^ *p++) & 0xff]; return crc; } - \ No newline at end of file + + + +