javacommons/utils/src/javacommonutils.cpp
branchRCL_3
changeset 19 04becd199f91
child 48 e0d6e9bd3ca7
child 60 6c158198356e
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  JavaCommonUtils
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <iostream>
       
    20 #include <sstream>
       
    21 #include <vector>
       
    22 
       
    23 #include <string.h>
       
    24 
       
    25 #include <openssl/bio.h>
       
    26 #include <openssl/evp.h>
       
    27 #include <openssl/buffer.h>
       
    28 
       
    29 #include "logger.h"
       
    30 #include "javacommonutils.h"
       
    31 #include "exceptionbase.h"
       
    32 #include "convertutf.h"
       
    33 
       
    34 using namespace java::util;
       
    35 
       
    36 OS_EXPORT int JavaCommonUtils::stringToInt(const std::string& str)
       
    37 {
       
    38 //    JELOG2(EUtils);
       
    39 //    value = boost::lexical_cast<int>( str );
       
    40     std::istringstream iss(str);
       
    41     int value = -1;
       
    42     if (!(iss >> value))
       
    43     {
       
    44         std::string errorStr = "Not able to convert to integer: ";
       
    45         errorStr.append(str);
       
    46         throw ExceptionBase(errorStr, __FILE__, __FUNCTION__, __LINE__);
       
    47     }
       
    48     return value;
       
    49 }
       
    50 
       
    51 OS_EXPORT long long JavaCommonUtils::stringToLongLong(const std::string& str)
       
    52 {
       
    53 //  JELOG2(EUtils);
       
    54     std::istringstream iss(str);
       
    55     long long value = -1LL;
       
    56     if (!(iss >> value))
       
    57     {
       
    58         std::string errorStr = "Not able to convert to 'long long': ";
       
    59         errorStr.append(str);
       
    60         throw ExceptionBase(errorStr, __FILE__, __FUNCTION__, __LINE__);
       
    61     }
       
    62     return value;
       
    63 }
       
    64 
       
    65 OS_EXPORT std::string JavaCommonUtils::intToString(const int& value)
       
    66 {
       
    67 //    JELOG2(EUtils);
       
    68 //    str = boost::lexical_cast<std::string>( value );
       
    69     std::ostringstream oss;
       
    70     oss<<value;
       
    71     return oss.str();
       
    72 }
       
    73 
       
    74 OS_EXPORT std::string JavaCommonUtils::longLongToString(const long long& value)
       
    75 {
       
    76 //  JELOG2(EUtils);
       
    77     std::ostringstream oss;
       
    78     oss<<value;
       
    79     return oss.str();
       
    80 }
       
    81 
       
    82 OS_EXPORT int JavaCommonUtils::wstringToInt(const std::wstring& str)
       
    83 {
       
    84 //    JELOG2(EUtils);
       
    85     //throw ExceptionBase("JavaCommonUtils::wstringToInt not supported yet", __FILE__, __FUNCTION__, __LINE__);
       
    86 //    value = boost::lexical_cast<int>( str );
       
    87 
       
    88     std::wistringstream iss(str);
       
    89     int value = -1;
       
    90     if (!(iss >> value))
       
    91     {
       
    92         std::string errorStr = "Not able to convert wstring to integer";
       
    93         throw ExceptionBase(errorStr, __FILE__, __FUNCTION__, __LINE__);
       
    94     }
       
    95     return value;
       
    96 }
       
    97 
       
    98 OS_EXPORT std::wstring JavaCommonUtils::intToWstring(const int& value)
       
    99 {
       
   100 //    JELOG2(EUtils);
       
   101     //throw ExceptionBase("JavaCommonUtils::wstringToInt not supported yet", __FILE__, __FUNCTION__, __LINE__);
       
   102 //  return boost::lexical_cast<std::wstring>( value );
       
   103     std::wostringstream oss;
       
   104     oss<<value;
       
   105     return oss.str();
       
   106 }
       
   107 
       
   108 OS_EXPORT std::wstring JavaCommonUtils::longLongToWstring(const long long& value)
       
   109 {
       
   110 //  JELOG2(EUtils);
       
   111     std::wostringstream oss;
       
   112     oss<<value;
       
   113     return oss.str();
       
   114 }
       
   115 
       
   116 OS_EXPORT long long JavaCommonUtils::wstringToLongLong(const std::wstring& str)
       
   117 {
       
   118 //  JELOG2(EUtils);
       
   119     std::wistringstream iss(str);
       
   120     long long value;
       
   121     if (!(iss >> value))
       
   122     {
       
   123         std::string errorStr = "Not able to convert wstring to 'long long'";
       
   124         throw ExceptionBase(errorStr, __FILE__, __FUNCTION__, __LINE__);
       
   125     }
       
   126     return value;
       
   127 }
       
   128 
       
   129 OS_EXPORT std::wstring JavaCommonUtils::utf8ToWstring(const char* chr)
       
   130 {
       
   131 //    JELOG2(EUtils);
       
   132     if (chr==0)
       
   133     {
       
   134         throw ExceptionBase("Null argument", __FILE__, __FUNCTION__, __LINE__);
       
   135     }
       
   136 
       
   137     size_t widesize = strlen(chr);
       
   138     if (sizeof(wchar_t) == 2)
       
   139     {
       
   140         wchar_t* widestringnative = new wchar_t[widesize+3];
       
   141         const UTF8* sourcestart = reinterpret_cast<const UTF8*>(chr);
       
   142         const UTF8* sourceend = sourcestart + widesize;
       
   143         UTF16* targetstart = reinterpret_cast<UTF16*>(widestringnative);
       
   144         UTF16* targetend = targetstart + widesize + 3;
       
   145         ConversionResult res =
       
   146             ConvertUTF8toUTF16(&sourcestart, sourceend, &targetstart, targetend, lenientConversion);
       
   147         if (res != conversionOK)
       
   148         {
       
   149             delete [] widestringnative;
       
   150             std::string errorStr = "Not able to convert utf8 to utf16 wstring";
       
   151             throw ExceptionBase(errorStr, __FILE__, __FUNCTION__, __LINE__);
       
   152         }
       
   153         *targetstart = 0;  // add terminating null character
       
   154         std::wstring resultstring(widestringnative);
       
   155         delete [] widestringnative;
       
   156         return resultstring;
       
   157     }
       
   158     else if (sizeof(wchar_t) == 4)
       
   159     {
       
   160         wchar_t* widestringnative = new wchar_t[widesize+3];
       
   161         const UTF8* sourcestart = reinterpret_cast<const UTF8*>(chr);
       
   162         const UTF8* sourceend = sourcestart + widesize;
       
   163         UTF32* targetstart = reinterpret_cast<UTF32*>(widestringnative);
       
   164         UTF32* targetend = targetstart + widesize + 3;
       
   165         ConversionResult res =
       
   166             ConvertUTF8toUTF32(&sourcestart, sourceend, &targetstart, targetend, lenientConversion);
       
   167         if (res != conversionOK)
       
   168         {
       
   169             delete [] widestringnative;
       
   170             std::string errorStr = "Not able to convert utf8 to utf32 wstring";
       
   171             throw ExceptionBase(errorStr, __FILE__, __FUNCTION__, __LINE__);
       
   172         }
       
   173         *targetstart = 0; // add terminating null character
       
   174         std::wstring resultstring(widestringnative);
       
   175         delete [] widestringnative;
       
   176         return resultstring;
       
   177     }
       
   178     else
       
   179     {
       
   180         int len = strlen(chr);
       
   181         wchar_t* dest = new wchar_t[len+1];
       
   182         int i;
       
   183         for (i = 0; i < len; i++)
       
   184         {
       
   185             dest[i] = chr[i];
       
   186         }
       
   187         dest[i] = 0;
       
   188         std::wstring result = std::wstring(dest);
       
   189         delete [] dest;
       
   190         return result;
       
   191     }
       
   192 }
       
   193 
       
   194 OS_EXPORT char* JavaCommonUtils::wstringToUtf8(const std::wstring& str)
       
   195 {
       
   196     size_t widesize = str.length();
       
   197 
       
   198     if (sizeof(wchar_t) == 2)
       
   199     {
       
   200         size_t utf8size = 3 * widesize + 3;
       
   201         // should be large enough to contain the conversion always
       
   202         char* utf8stringnative = new char[utf8size];
       
   203         const UTF16* sourcestart = reinterpret_cast<const UTF16*>(str.c_str());
       
   204         const UTF16* sourceend = sourcestart + widesize;
       
   205         UTF8* targetstart = reinterpret_cast<UTF8*>(utf8stringnative);
       
   206         UTF8* targetend = targetstart + utf8size;
       
   207         ConversionResult res =
       
   208             ConvertUTF16toUTF8(&sourcestart, sourceend, &targetstart, targetend, lenientConversion);
       
   209         if (res != conversionOK)
       
   210         {
       
   211             delete [] utf8stringnative;
       
   212             std::string errorStr = "Not able to convert utf16 wstring to utf8";
       
   213             throw ExceptionBase(errorStr, __FILE__, __FUNCTION__, __LINE__);
       
   214         }
       
   215         *targetstart = 0;
       
   216         return utf8stringnative;
       
   217     }
       
   218     else if (sizeof(wchar_t) == 4)
       
   219     {
       
   220         size_t utf8size = 4 * widesize + 3;
       
   221         // should be large enough to contain the conversion always
       
   222         char* utf8stringnative = new char[utf8size];
       
   223         const UTF32* sourcestart = reinterpret_cast<const UTF32*>(str.c_str());
       
   224         const UTF32* sourceend = sourcestart + widesize;
       
   225         UTF8* targetstart = reinterpret_cast<UTF8*>(utf8stringnative);
       
   226         UTF8* targetend = targetstart + utf8size;
       
   227         ConversionResult res =
       
   228             ConvertUTF32toUTF8(&sourcestart, sourceend, &targetstart, targetend, lenientConversion);
       
   229         if (res != conversionOK)
       
   230         {
       
   231             delete [] utf8stringnative;
       
   232             std::string errorStr = "Not able to convert utf32 wstring to utf8";
       
   233             throw ExceptionBase(errorStr, __FILE__, __FUNCTION__, __LINE__);
       
   234         }
       
   235         *targetstart = 0;
       
   236         return utf8stringnative;
       
   237     }
       
   238     else
       
   239     {
       
   240         char* chr = new char[str.size()+1];
       
   241         sprintf(chr,"%ls",str.c_str());
       
   242         return chr;
       
   243     }
       
   244 }
       
   245 
       
   246 OS_EXPORT void JavaCommonUtils::trimWstring(std::wstring& aStr,
       
   247         const wchar_t& aStrippedChar)
       
   248 {
       
   249     if (aStr.empty())
       
   250         return;
       
   251     std::string::size_type const first = aStr.find_first_not_of(aStrippedChar);
       
   252     std::string::size_type const second = aStr.find_last_not_of(aStrippedChar);
       
   253     aStr = aStr.substr(first,second-first+1);
       
   254 }
       
   255 
       
   256 OS_EXPORT std::string JavaCommonUtils::base64encode(const std::string& aData)
       
   257 {
       
   258     BIO* b64  = BIO_new(BIO_f_base64());
       
   259     if (NULL == b64)
       
   260     {
       
   261         return "";
       
   262     }
       
   263     BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
       
   264     BIO* bmem = BIO_new(BIO_s_mem());
       
   265 
       
   266     b64 = BIO_push(b64, bmem);
       
   267     BIO_write(b64, aData.c_str(), aData.length());
       
   268     BIO_flush(b64);
       
   269 
       
   270     BUF_MEM* bptr;
       
   271     BIO_get_mem_ptr(b64, &bptr);
       
   272 
       
   273     std::string result;
       
   274     result.insert(0, bptr->data, bptr->length);
       
   275     BIO_free_all(b64);
       
   276 
       
   277     return result;
       
   278 }
       
   279 
       
   280 OS_EXPORT std::string JavaCommonUtils::base64decode(const std::string& aData)
       
   281 {
       
   282     BIO* b64  = BIO_new(BIO_f_base64());
       
   283     if (NULL == b64)
       
   284     {
       
   285         return "";
       
   286     }
       
   287     BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
       
   288     BIO* bmem = BIO_new_mem_buf(
       
   289                     const_cast<char*>(aData.c_str()), aData.length());
       
   290 
       
   291     bmem = BIO_push(b64, bmem);
       
   292 
       
   293     char* buf = new char[aData.length()];
       
   294     int olen = BIO_read(bmem, buf, aData.length());
       
   295 
       
   296     std::string result;
       
   297     result.insert(0, buf, olen);
       
   298 
       
   299     BIO_free_all(bmem);
       
   300     delete[] buf;
       
   301 
       
   302     return result;
       
   303 }
       
   304 
       
   305 OS_EXPORT std::wstring JavaCommonUtils::percentDecode(const std::wstring& str)
       
   306 {
       
   307     const std::wstring percent(L"%");
       
   308     std::string::size_type idx = str.find(percent);
       
   309     std::string::size_type cur = 0;
       
   310 
       
   311     std::wstring res;
       
   312     std::string convBuf;
       
   313 
       
   314     while (idx != std::string::npos)
       
   315     {
       
   316         // Add all characters before % sequence to final result string
       
   317         if ((idx - cur) > 0)
       
   318         {
       
   319             res.append(str.substr(cur, (idx - cur)));
       
   320         }
       
   321 
       
   322         // Decode all percent encoded chars in this sequence.
       
   323         do
       
   324         {
       
   325             char ch = decodeOnePercentSeq(str.at(idx + 1), str.at(idx + 2));
       
   326             // Add one UTF-8 value in this sequence to the buffer that will
       
   327             // be converter to UTF-16
       
   328             convBuf.append(&ch, 1);
       
   329             cur = idx + 3;
       
   330             idx = str.find(percent, cur);
       
   331         }
       
   332         while (idx == cur);
       
   333 
       
   334         // Now convert all UTF-8 values in buffer to UTF-16 and add result
       
   335         // to the final result string
       
   336         res.append(utf8ToWstring(convBuf.c_str()));
       
   337         convBuf.clear();
       
   338     }
       
   339 
       
   340     // Add characters after last % sequence if any
       
   341     res.append(str.substr(cur, str.length() - cur));
       
   342 
       
   343     return res;
       
   344 }
       
   345 
       
   346 
       
   347 char JavaCommonUtils::decodeOnePercentSeq(wchar_t first, wchar_t sec)
       
   348 {
       
   349     // Handle one encoded %XY
       
   350     char decodedCh = 0;
       
   351     wchar_t ch = first;
       
   352     if ((ch >= '0') && (ch <= '9'))
       
   353     {
       
   354         decodedCh = (ch - '0') * 16;
       
   355     }
       
   356     else if ((ch >= 'A') && (ch <= 'F'))
       
   357     {
       
   358         decodedCh = ((ch - 'A') + 10) * 16;
       
   359     }
       
   360     else if ((ch >= 'a') && (ch <= 'f'))
       
   361     {
       
   362         decodedCh = ((ch - 'a') + 10) * 16;
       
   363     }
       
   364     else
       
   365     {
       
   366         std::string errorStr = "Illegal % encoding";
       
   367         throw ExceptionBase(errorStr, __FILE__, __FUNCTION__, __LINE__);
       
   368     }
       
   369 
       
   370     ch = sec;
       
   371     if ((ch >= '0') && (ch <= '9'))
       
   372     {
       
   373         decodedCh += (ch - '0');
       
   374     }
       
   375     else if ((ch >= 'A') && (ch <= 'F'))
       
   376     {
       
   377         decodedCh += (ch - 'A') + 10;
       
   378     }
       
   379     else if ((ch >= 'a') && (ch <= 'f'))
       
   380     {
       
   381         decodedCh += (ch - 'a') + 10;
       
   382     }
       
   383     else
       
   384     {
       
   385         std::string errorStr = "Illegal % encoding";
       
   386         throw ExceptionBase(errorStr, __FILE__, __FUNCTION__, __LINE__);
       
   387     }
       
   388 
       
   389     return decodedCh;
       
   390 }
       
   391