menufw/hierarchynavigator/hnutilities/src/hnconvutils.cpp
branchv5backport
changeset 14 1abc632eb502
parent 13 6205fd287e8a
child 20 636d517f67e6
equal deleted inserted replaced
13:6205fd287e8a 14:1abc632eb502
     1 /*
       
     2 * Copyright (c) 2007-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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <utf.h> 
       
    20 #include <e32std.h>
       
    21 
       
    22 #include "hnconvutils.h"
       
    23 #include "hnglobals.h"
       
    24 
       
    25 // ======== MEMBER FUNCTIONS ========
       
    26 
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // 
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 EXPORT_C HBufC* HnConvUtils::Str8ToStr( const TDesC8& aStr )
       
    33     {
       
    34     HBufC* ret = HBufC::New( aStr.Length() );
       
    35     if (ret)
       
    36     	{
       
    37     	TPtr dest( ret->Des() );
       
    38     	CnvUtfConverter::ConvertToUnicodeFromUtf8( dest, aStr );
       
    39     	}
       
    40     return ret;
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // 
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 EXPORT_C HBufC* HnConvUtils::Str8ToStrLC( const TDesC8& aStr )
       
    48     {
       
    49     HBufC* ret = HBufC::NewLC( aStr.Length() );
       
    50     TPtr dest( ret->Des() );
       
    51     CnvUtfConverter::ConvertToUnicodeFromUtf8( dest, aStr );
       
    52     return ret;
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // 
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 EXPORT_C HBufC8* HnConvUtils::StrToStr8LC( const TDesC16& aStr )
       
    60     {
       
    61     TInt length = aStr.Length();
       
    62     HBufC8* ret = HBufC8::NewL( length );
       
    63     TPtr8 dest( ret->Des() );
       
    64     TInt num = CnvUtfConverter::ConvertFromUnicodeToUtf8( dest, aStr );
       
    65     while( num != 0 )
       
    66     	{
       
    67     	delete ret;
       
    68     	length += num;
       
    69     	ret = HBufC8::NewL( length );
       
    70     	dest.Set( ret->Des() );
       
    71     	num = CnvUtfConverter::ConvertFromUnicodeToUtf8( dest, aStr );
       
    72     	}
       
    73     CleanupStack::PushL(ret);
       
    74     return ret;
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // 
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 EXPORT_C HBufC8* HnConvUtils::StrToStr8L( const TDesC16& aStr )
       
    82     {
       
    83     HBufC8* buf = HnConvUtils::StrToStr8LC( aStr );
       
    84     CleanupStack::Pop( buf );
       
    85     return buf;
       
    86     }
       
    87     
       
    88 // ---------------------------------------------------------------------------
       
    89 // 
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 EXPORT_C HBufC8* HnConvUtils::NumToStr8LC( const TInt& aNum )
       
    93     {
       
    94     HBufC8* ret = HBufC8::NewLC( KMaxLength );
       
    95     ret->Des().AppendNum( aNum );
       
    96     return ret;
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // 
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 EXPORT_C TInt HnConvUtils::Str8ToInt( const TDesC8& aValue, TInt& aResult )
       
   104     {
       
   105     // if a string starts from '0x' then convert aStr from hex to int.
       
   106     // else send aStr to the StrDec2Uint    
       
   107     TInt err( KErrNotFound );
       
   108     TInt position( aValue.Find( KHexStart8 ) );
       
   109     
       
   110     TLex8 lexer( aValue );
       
   111     if ( position == 0 )
       
   112         {
       
   113         // is hex, it it can only unsinged
       
   114         lexer.SkipAndMark( KHexStart8().Length() );
       
   115         TUint tmp;
       
   116         err = lexer.Val( tmp, EHex );
       
   117         aResult = tmp;
       
   118         }
       
   119     else
       
   120         {
       
   121         err = lexer.Val( aResult );
       
   122         }
       
   123     return err;
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // 
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 EXPORT_C TInt HnConvUtils::StrToInt( const TDesC& aValue, TInt& aResult )
       
   131     {
       
   132     TInt err( KErrNotFound );
       
   133     RBuf8 buf;
       
   134     err = buf.Create( aValue.Length() );
       
   135     if ( KErrNone == err )
       
   136         {
       
   137         buf.Copy( aValue );
       
   138         err = HnConvUtils::Str8ToInt( buf, aResult );
       
   139         }
       
   140     buf.Close();
       
   141     return err;
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // 
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 EXPORT_C TInt HnConvUtils::Str8ToInt( const TDesC8& aValue, TInt32& aResult )
       
   149     {
       
   150     // if a string starts from '0x' then convert aStr from hex to int.
       
   151     // else send aStr to the StrDec2Uint    
       
   152     TInt err( KErrNotFound );
       
   153     TInt position( aValue.Find( KHexStart8 ) );
       
   154     
       
   155     TLex8 lexer( aValue );
       
   156     if ( position == 0 )
       
   157         {
       
   158         // is hex, it it can only unsinged
       
   159         TUint32 tmp;
       
   160         lexer.SkipAndMark( KHexStart8().Length() );
       
   161         err = lexer.Val( tmp, EHex );
       
   162         aResult = tmp;
       
   163         }
       
   164     else
       
   165         {
       
   166         err = lexer.Val( aResult );
       
   167         }
       
   168     return err;
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // 
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 EXPORT_C TInt  HnConvUtils::StrToInt( const TDesC& aValue, TInt32& aResult )
       
   176     {
       
   177     TInt err( KErrNotFound );
       
   178     RBuf8 buf; 
       
   179     err = buf.Create( aValue.Length() );
       
   180     if ( KErrNone == err )
       
   181         {
       
   182         buf.Copy( aValue );
       
   183         err = HnConvUtils::Str8ToInt( buf, aResult );
       
   184         }
       
   185     buf.Close();
       
   186     return err;
       
   187     }
       
   188 
       
   189 // ---------------------------------------------------------------------------
       
   190 // 
       
   191 // ---------------------------------------------------------------------------
       
   192 //
       
   193 EXPORT_C TInt HnConvUtils::Str8ToUint( const TDesC8& aValue, TUint& aResult )
       
   194     {
       
   195     // if a string starts from '0x' then convert aStr from hex to int.
       
   196     // else send aStr to the StrDec2Uint    
       
   197     TInt err( KErrNotFound );
       
   198     TInt position( aValue.Find( KHexStart8 ) );
       
   199     
       
   200     TLex8 lexer( aValue );
       
   201     if ( position == 0 )
       
   202         {
       
   203         // is hex
       
   204         lexer.SkipAndMark( KHexStart8().Length() );
       
   205         err = lexer.Val( aResult, EHex );
       
   206         }
       
   207     else
       
   208         {
       
   209         err = lexer.Val( aResult );
       
   210         }
       
   211     return err;
       
   212     }