javacommons/utils/inc/javacommonutils.h
branchRCL_3
changeset 19 04becd199f91
child 48 e0d6e9bd3ca7
child 60 6c158198356e
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2007-2007 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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef JAVACOMMONUTILS_H
       
    20 #define JAVACOMMONUTILS_H
       
    21 
       
    22 #include <string>
       
    23 #include "javaosheaders.h"
       
    24 
       
    25 #include <iostream>
       
    26 #include <ostream>
       
    27 
       
    28 namespace java
       
    29 {
       
    30 namespace util
       
    31 {
       
    32 
       
    33 class JavaCommonUtils
       
    34 {
       
    35 public:
       
    36     OS_IMPORT static int stringToInt(const std::string& str);
       
    37     OS_IMPORT static long long stringToLongLong(const std::string& str);
       
    38     OS_IMPORT static std::string intToString(const int& value);
       
    39     OS_IMPORT static std::string longLongToString(const long long& value);
       
    40     OS_IMPORT static int wstringToInt(const std::wstring& str);
       
    41     OS_IMPORT static std::wstring intToWstring(const int& value);
       
    42     OS_IMPORT static std::wstring longLongToWstring(const long long& value);
       
    43     OS_IMPORT static long long wstringToLongLong(const std::wstring& str);
       
    44 
       
    45     /**
       
    46      * Converts the null terminated UTF-8 string given as parameter
       
    47      * to UTF-16 wstring that is returned.
       
    48      * @param chr  the null terminated UTF-8 string to be converted
       
    49      * @return UTF-16 wstring
       
    50      * @throws ExceptionBase if conversion fails
       
    51      */
       
    52     OS_IMPORT static std::wstring utf8ToWstring(const char* chr);
       
    53 
       
    54     /**
       
    55      * Converts UTF-16 (UCS-2) wstring to null terminated UTF-8 string
       
    56      * User must note that this method allocates memory for UTF-8 content.
       
    57      * User must delete[] the memory once it is not needed any more.
       
    58      * @param str UTF-16 wstring to be converted
       
    59      * @return new null terminated UTF-8 string
       
    60      * @throws ExceptionBase if conversion fails
       
    61      */
       
    62     OS_IMPORT static char* wstringToUtf8(const std::wstring& str);
       
    63 
       
    64     /**
       
    65      * Strips characters at the beginning and end of the string.
       
    66      * @param aStr          Stripped string. Output argument.
       
    67      * @param aStrippedChar Character what is stripped away.
       
    68      * <p>
       
    69      * <h3>Example</h3>
       
    70      * <pre>
       
    71      * std::wstring str(L"\n EFFFFFFF \t");
       
    72      * trimWstring(str,L'\n');
       
    73      * trimWstring(str,L'\t');
       
    74      * //Note: For some reason 'space' character must be stripped last.
       
    75      * trimWstring(str,L' ');
       
    76      * //Result is 'EFFFFFFF'.
       
    77      *</pre>
       
    78      */
       
    79     OS_IMPORT static void trimWstring(std::wstring& aStr,const wchar_t& aStrippedChar);
       
    80 
       
    81     /**
       
    82      * Encode string to base64 string.
       
    83      *
       
    84      * @param aData to be encoded.
       
    85      * @return base64 encoded string.
       
    86      */
       
    87     OS_IMPORT static std::string base64encode(const std::string& aData);
       
    88 
       
    89     /**
       
    90      * Decode base64 string.
       
    91      *
       
    92      * @param aData base64 encoded string.
       
    93      * @return decoded string.
       
    94      */
       
    95     OS_IMPORT static std::string base64decode(const std::string& aData);
       
    96 
       
    97     /**
       
    98      * Decodes percent-encoded UTF-8 characters embedded to UCS-2 string.
       
    99      * After the characters has been decoded, they are converted to UCS-2.
       
   100      * When the whole string is in UCS-2 format, it is returned.
       
   101      * @param str UTF-16 (UCS-2) wstring that contains %-encoded UTF-8 characters,
       
   102      *  for example "%C3%80%C3%80NEST%C3%80.EXE"
       
   103      * @return UTF-16 wstring, for example "ÄÄNESTÄ.EXE"
       
   104      * @throws ExceptionBase if UTF-8 to UTF-16 conversion fails
       
   105      */
       
   106     OS_IMPORT static std::wstring percentDecode(const std::wstring& str);
       
   107 
       
   108 public:
       
   109     /**
       
   110      * Decodes one %<X><Y> sequence.
       
   111      * @param first the character <X>
       
   112      * @param sec the character <Y>
       
   113      * @return decoded char value 0-255
       
   114      * @throws ExceptionBase if <X> and <Y> are not hexadecimal characters
       
   115      */
       
   116     static char decodeOnePercentSeq(wchar_t first, wchar_t sec);
       
   117 };
       
   118 
       
   119 // A simple class for storing char array that is deleted automatically
       
   120 // when going out of scope.
       
   121 class ScopedCharArray
       
   122 {
       
   123 public:
       
   124     ScopedCharArray(int size) : mBuffer(0)
       
   125     {
       
   126         mBuffer = new char[size];
       
   127     }
       
   128     ~ScopedCharArray()
       
   129     {
       
   130         delete [] mBuffer;
       
   131     }
       
   132 
       
   133     char* get()
       
   134     {
       
   135         return mBuffer;
       
   136     }
       
   137 
       
   138 private:
       
   139     ScopedCharArray();                                      //No default constructor allowed
       
   140     ScopedCharArray(const ScopedCharArray&);                //No copy constructor allowed
       
   141     ScopedCharArray& operator= (const ScopedCharArray&);    //No Assignment operator allowed
       
   142 
       
   143 private:
       
   144     char* mBuffer;
       
   145 };
       
   146 
       
   147 
       
   148 } //end namespace util
       
   149 } //end namespace java
       
   150 
       
   151 #endif // JAVACOMMONUTILS_H
       
   152