javacommons/utils/inc/javacommonutils.h
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Wed, 13 Oct 2010 14:23:59 +0300
branchRCL_3
changeset 83 26b2b12093af
parent 60 6c158198356e
permissions -rw-r--r--
Revision: v2.2.17 Kit: 201041

/*
* Copyright (c) 2007-2007 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/


#ifndef JAVACOMMONUTILS_H
#define JAVACOMMONUTILS_H

#include <string>
#include "javaosheaders.h"

#include <iostream>
#include <ostream>

namespace java
{
namespace util
{

class JavaCommonUtils
{
public:
    OS_IMPORT static int stringToInt(const std::string& str);
    OS_IMPORT static long long stringToLongLong(const std::string& str);
    OS_IMPORT static std::string intToString(const int& value);
    OS_IMPORT static std::string longLongToString(const long long& value);
    OS_IMPORT static int wstringToInt(const std::wstring& str);
    OS_IMPORT static std::wstring intToWstring(const int& value);
    OS_IMPORT static std::wstring longLongToWstring(const long long& value);
    OS_IMPORT static long long wstringToLongLong(const std::wstring& str);

    /**
     * Converts the null terminated UTF-8 string given as parameter
     * to UTF-16 wstring that is returned.
     * @param chr  the null terminated UTF-8 string to be converted
     * @return UTF-16 wstring
     * @throws ExceptionBase if conversion fails
     */
    OS_IMPORT static std::wstring utf8ToWstring(const char* chr);

    /**
     * Converts UTF-16 (UCS-2) wstring to null terminated UTF-8 string
     * User must note that this method allocates memory for UTF-8 content.
     * User must delete[] the memory once it is not needed any more.
     * @param str UTF-16 wstring to be converted
     * @return new null terminated UTF-8 string
     * @throws ExceptionBase if conversion fails
     */
    OS_IMPORT static char* wstringToUtf8(const std::wstring& str);

    /**
     * Strips characters at the beginning and end of the string.
     * @param aStr          Stripped string. Output argument.
     * @param aStrippedChar Character what is stripped away.
     * <p>
     * <h3>Example</h3>
     * <pre>
     * std::wstring str(L"\n EFFFFFFF \t");
     * trimWstring(str,L'\n');
     * trimWstring(str,L'\t');
     * //Note: For some reason 'space' character must be stripped last.
     * trimWstring(str,L' ');
     * //Result is 'EFFFFFFF'.
     *</pre>
     */
    OS_IMPORT static void trimWstring(std::wstring& aStr,const wchar_t& aStrippedChar);

    /**
     * Encode string to base64 string.
     *
     * @param aData to be encoded.
     * @return base64 encoded string.
     */
    OS_IMPORT static std::string base64encode(const std::string& aData);

    /**
     * Decode base64 string.
     *
     * @param aData base64 encoded string.
     * @return decoded string.
     */
    OS_IMPORT static std::string base64decode(const std::string& aData);

    /**
     * Decodes percent-encoded UTF-8 characters embedded to UCS-2 string.
     * After the characters has been decoded, they are converted to UCS-2.
     * When the whole string is in UCS-2 format, it is returned.
     * @param str UTF-16 (UCS-2) wstring that contains %-encoded UTF-8 characters,
     *  for example "%C3%80%C3%80NEST%C3%80.EXE"
     * @return UTF-16 wstring, for example "ÄÄNESTÄ.EXE"
     * @throws ExceptionBase if UTF-8 to UTF-16 conversion fails
     */
    OS_IMPORT static std::wstring percentDecode(const std::wstring& str);

    /**
     * This function checks whether is it called during the first device boot
     * and stores the information to static data so that isFirstBoot() can
     * return it.
     *
     * Call this function always when the process is starting.
     *
     * This function creates a flag file to the private data cage / work directory
     * of the process.
     *
     * In Symbian this method can be called only from Java Installer or
     * Java MIDP processes.
     *
     * Return 0 if check was made successfully. Otherwise it returns errno
     * defined in STDLIBS errno.h
     */
    OS_IMPORT static int initIsFirstBoot();

    /**
     * This function returns true if it called during the first boot.
     * Otherwise it returns false.
     *
     * initIsFirstBoot() must be called before this function can be called.
     */
    OS_IMPORT static bool isFirstBoot();


public:
    /**
     * Decodes one %<X><Y> sequence.
     * @param first the character <X>
     * @param sec the character <Y>
     * @return decoded char value 0-255
     * @throws ExceptionBase if <X> and <Y> are not hexadecimal characters
     */
    static char decodeOnePercentSeq(wchar_t first, wchar_t sec);

private:
    static bool mFirstBoot;

};

// A simple class for storing char array that is deleted automatically
// when going out of scope.
class ScopedCharArray
{
public:
    ScopedCharArray(int size) : mBuffer(0)
    {
        mBuffer = new char[size];
    }
    ~ScopedCharArray()
    {
        delete [] mBuffer;
    }

    char* get()
    {
        return mBuffer;
    }

private:
    ScopedCharArray();                                      //No default constructor allowed
    ScopedCharArray(const ScopedCharArray&);                //No copy constructor allowed
    ScopedCharArray& operator= (const ScopedCharArray&);    //No Assignment operator allowed

private:
    char* mBuffer;
};


} //end namespace util
} //end namespace java

#endif // JAVACOMMONUTILS_H