customization/DMUtils/src/TARMCharConv.cpp
changeset 0 3ce708148e4d
equal deleted inserted replaced
-1:000000000000 0:3ce708148e4d
       
     1 /*
       
     2 * Copyright (c) 2005 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:  TARM character conversion methods
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "TARMCharConv.h"
       
    20 
       
    21 #include <utf.h>
       
    22 
       
    23 CTARMCharConv::CTARMCharConv()
       
    24 	{
       
    25 	
       
    26 	}
       
    27 
       
    28 // ------------------------------------------------------------------------------------------------
       
    29 // TInt CTARMCharConv::ConvertToUtf8LC
       
    30 // Encodes from Unicode UCS-2 to UTF-8
       
    31 // ------------------------------------------------------------------------------------------------
       
    32 NSMLDMURI_EXPORT_C HBufC8* CTARMCharConv::ConvertToUtf8LC(const TDesC16& aText)
       
    33     {
       
    34     TPtrC16    remainder( aText );
       
    35     TBuf8<20>  utfBuffer;
       
    36     HBufC8     *ret    = 0;
       
    37     CBufFlat   *buffer = CBufFlat::NewL( 128 );
       
    38     CleanupStack::PushL( buffer );
       
    39 
       
    40     TBool finish = EFalse;
       
    41     while( !finish )
       
    42         {
       
    43         utfBuffer.Zero();
       
    44         TInt unconverted = CnvUtfConverter::ConvertFromUnicodeToUtf8( utfBuffer, remainder );
       
    45         if( unconverted >= 0 )
       
    46             {
       
    47             remainder.Set( remainder.Right( unconverted ) );
       
    48             buffer->InsertL( buffer->Size(), utfBuffer );
       
    49             finish = (unconverted == 0);
       
    50             }
       
    51         else
       
    52             {
       
    53             User::Leave( unconverted );
       
    54             }
       
    55         }
       
    56 
       
    57     buffer->Compress();
       
    58     ret = buffer->Ptr( 0 ).Alloc();
       
    59     CleanupStack::PopAndDestroy( buffer );
       
    60     CleanupStack::PushL( ret );
       
    61     return ret;
       
    62     }
       
    63 
       
    64 
       
    65 // ------------------------------------------------------------------------------------------------
       
    66 // TInt CTARMCharConv::ConvertFromUtf8LC
       
    67 // Decodes from UTF-8 to Unicode UCS-2
       
    68 // ------------------------------------------------------------------------------------------------
       
    69 NSMLDMURI_EXPORT_C HBufC16* CTARMCharConv::ConvertFromUtf8LC(const TDesC8& aText)
       
    70     {
       
    71     TPtrC8     remainder( aText );
       
    72     TBuf16<20> unicodeBuffer;
       
    73     HBufC16    *ret    = 0;
       
    74     CBufFlat   *buffer = CBufFlat::NewL( 128 );
       
    75     CleanupStack::PushL( buffer );
       
    76 
       
    77     TBool finish = EFalse;
       
    78     while( !finish )
       
    79         {
       
    80         unicodeBuffer.Zero();
       
    81         TInt unconverted = CnvUtfConverter::ConvertToUnicodeFromUtf8( unicodeBuffer, remainder );
       
    82         if( unconverted >= 0 )
       
    83             {
       
    84             remainder.Set( remainder.Right( unconverted ) );
       
    85             TPtrC8 ptr( reinterpret_cast<const unsigned char*>(unicodeBuffer.Ptr()), unicodeBuffer.Length()*2 );
       
    86             buffer->InsertL( buffer->Size(), ptr );
       
    87             finish = (unconverted == 0);
       
    88             }
       
    89         else
       
    90             {
       
    91             User::Leave( unconverted );
       
    92             }
       
    93         }
       
    94 
       
    95     buffer->Compress();
       
    96     TPtrC16 ptr16( reinterpret_cast<const unsigned short*>(buffer->Ptr( 0 ).Ptr()), buffer->Size() / 2 );
       
    97     ret = ptr16.Alloc();
       
    98     CleanupStack::PopAndDestroy( buffer );
       
    99     CleanupStack::PushL( ret );
       
   100     return ret;
       
   101     }
       
   102 
       
   103 // End of file