omadmadapters/connmoadapter/src/Utils.cpp
changeset 30 d3981f4fe6a4
equal deleted inserted replaced
27:516a867811c3 30:d3981f4fe6a4
       
     1 /*
       
     2  * ==============================================================================
       
     3  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     4  * All rights reserved.
       
     5  * This component and the accompanying materials are made available
       
     6  * under the terms of "Eclipse Public License v1.0"
       
     7  * which accompanies this distribution, and is available
       
     8  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9  *
       
    10  * Initial Contributors:
       
    11  * Nokia Corporation - initial contribution.
       
    12  *
       
    13  * Contributors:
       
    14  * 
       
    15  * Description:
       
    16  * 
       
    17  * ==============================================================================
       
    18  */
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <utf.h>
       
    22 #include <cmpluginpacketdatadef.h>
       
    23 
       
    24 #include "Utils.h"
       
    25 #include "OstTraceDefinitions.h"
       
    26 #ifdef OST_TRACE_COMPILER_IN_USE
       
    27 #include "UtilsTraces.h"
       
    28 #endif
       
    29 
       
    30 // ======== LOCAL CONSTANTS =========
       
    31 
       
    32 _LIT8( KFalse,               "False" );
       
    33 _LIT8( KTrue,                "True" );
       
    34 _LIT8( KWhiteSpace,          " " );
       
    35 
       
    36 // ======== LOCAL FUNCTIONS =========
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========   
       
    39 //------------------------------------------------------------------------------
       
    40 // CUtils::CUtils()
       
    41 //------------------------------------------------------------------------------
       
    42 CUtils::CUtils() 
       
    43     {
       
    44     OstTraceFunctionEntry0( CUTILS_CUTILS_ENTRY );
       
    45     OstTraceFunctionExit0( CUTILS_CUTILS_EXIT );
       
    46     }
       
    47 
       
    48 //------------------------------------------------------------------------------
       
    49 // CUtils::~CUtils()
       
    50 //------------------------------------------------------------------------------
       
    51 CUtils::~CUtils()
       
    52     {
       
    53     }
       
    54 
       
    55 //------------------------------------------------------------------------------
       
    56 // TPtrC8 CUtils::GetFirstURISeg(const TDesC8& aURI)
       
    57 //------------------------------------------------------------------------------
       
    58 TPtrC8 CUtils::GetFirstURISeg( const TDesC8& aURI )
       
    59     {
       
    60     OstTraceExt1( TRACE_NORMAL, CUTILS_GETFIRSTURISEG_ENTRY, 
       
    61                   "ENTRY: CUtils::GetFirstURISeg;aURI=%s", aURI );
       
    62     
       
    63     TInt i = 0;
       
    64     TInt j = 0;
       
    65     
       
    66     for( i  =0; i <= aURI.Length()-1; i++ )
       
    67         {
       
    68         if( aURI[i] == '/' )
       
    69             {
       
    70             if ( i == 0 )
       
    71                 {
       
    72                 // Skip first slash
       
    73                 j = 1;
       
    74                 continue;
       
    75                 }
       
    76             break;
       
    77             }
       
    78         }
       
    79     OstTraceExt1( TRACE_NORMAL, CUTILS_GETFIRSTURISEG_EXIT, 
       
    80                   "EXIT: CUtils::GetFirstURISeg;retval=%s", aURI.Mid( j,i-j ) );
       
    81     
       
    82     return aURI.Mid( j,i-j );
       
    83     }
       
    84 
       
    85 //------------------------------------------------------------------------------
       
    86 // TPtrC8 CUtils::GetSecondURISeg(const TDesC8& aURI)
       
    87 //------------------------------------------------------------------------------
       
    88 TPtrC8 CUtils::GetSecondURISeg( const TDesC8& aURI )
       
    89     {
       
    90     OstTraceExt1( TRACE_NORMAL, CUTILS_GETSECONDURISEG_ENTRY, 
       
    91                   "ENTRY: CUtils::GetSecondURISeg;aURI=%s", aURI );
       
    92     
       
    93     TInt j = 0;
       
    94     TInt i = 0;
       
    95     
       
    96     for( i = 0; i <= aURI.Length() - 1; i++ )
       
    97         {
       
    98         if( aURI[i] == '/' )
       
    99             {
       
   100             for( j = i + 1; j <= aURI.Length() - 1; j++ )
       
   101                 {
       
   102                 if( aURI[j] == '/' ) // Second slash => AP/xxxx
       
   103                     {
       
   104                     break;
       
   105                     }
       
   106                 }
       
   107             break;
       
   108             }
       
   109         }
       
   110     OstTraceExt1( TRACE_NORMAL, CUTILS_GETSECONDTURISEG_EXIT, 
       
   111                   "EXIT: CUtils::GetSecondURISeg;retval=%s", aURI.Mid( i+1, j-1-i ) );
       
   112     
       
   113     return aURI.Mid( i+1, j-1-i );
       
   114     }
       
   115 
       
   116 //------------------------------------------------------------------------------
       
   117 // TPtrC8 CUtils::GetLastURISeg(const TDesC8& aURI)
       
   118 // Returns only the last uri segment
       
   119 //------------------------------------------------------------------------------
       
   120 TPtrC8 CUtils::GetLastURISeg( const TDesC8& aURI )
       
   121     {
       
   122     OstTraceExt1( TRACE_NORMAL, CUTILS_GETLASTURISEG_ENTRY, 
       
   123                   "ENTRY: CUtils::GetLastURISeg;aURI=%s", aURI );
       
   124     
       
   125     TInt i;
       
   126     
       
   127     for( i = aURI.Length() - 1 ; i >= 0; i-- )
       
   128         {
       
   129         if( aURI[i] == '/' )
       
   130             {
       
   131             break;
       
   132             }
       
   133         }
       
   134     if( i == 0 )
       
   135         {
       
   136         OstTraceExt1( TRACE_NORMAL, CUTILS_GETLASTURISEG_EXIT, 
       
   137                       "EXIT: CUtils::GetLastURISeg;retval=%s", aURI );
       
   138         return aURI;
       
   139         }
       
   140     else
       
   141         {
       
   142         OstTraceExt1( TRACE_NORMAL, CUTILS_GETLASTURISEG_EXIT2, 
       
   143                       "EXIT: CUtils::GetLastURISeg;retval=%s", aURI.Mid( i+1 ) );
       
   144         return aURI.Mid( i+1 );
       
   145         }
       
   146     }
       
   147 
       
   148 //------------------------------------------------------------------------------
       
   149 // TPtrC8 CUtils::GetEndOfURIFromSeg(const TDesC8& aURI,const TDesC8 aName)
       
   150 // Returns the end of aURI starting from aName
       
   151 //------------------------------------------------------------------------------
       
   152 TPtrC8 CUtils::GetEndOfURIFromSeg( const TDesC8& aURI, const TDesC8& aName )
       
   153     {
       
   154     OstTraceExt2( TRACE_NORMAL, CUTILS_GETENDOFURIFROMSEG_ENTRY, 
       
   155                   "ENTRY: CUtils::GetEndOfURIFromSeg;aURI=%s;aName=%s", aURI, aName );
       
   156     
       
   157     TInt begin = aURI.Find( aName );
       
   158     
       
   159     if ( begin == KErrNotFound )
       
   160         {
       
   161         OstTrace1( TRACE_NORMAL, CUTILS_GETENDOFURIFROMSEG_EXIT, 
       
   162                    "EXIT: CUtils::GetEndOfURIFromSeg;%d", KErrNotFound );
       
   163         return aURI;
       
   164         }
       
   165     OstTraceExt1( TRACE_NORMAL, CUTILS_GETENDOFURIFROMSEG_EXIT2, 
       
   166                   "EXIT: CUtils::GetEndOfURIFromSeg;%s", aURI.Mid(begin) );    
       
   167     return aURI.Mid(begin);
       
   168     }
       
   169 
       
   170 //------------------------------------------------------------------------------
       
   171 // CUtils::ConvertTo8LC()
       
   172 // Converts string value to 8-bit
       
   173 //------------------------------------------------------------------------------
       
   174 TDesC8& CUtils::ConvertTo8LC( const TDesC& aSource )
       
   175     {
       
   176     HBufC8* buf = HBufC8::NewLC( aSource.Length() * 2 );
       
   177     TPtr8 bufPtr = buf->Des();
       
   178     CnvUtfConverter::ConvertFromUnicodeToUtf8( bufPtr, aSource );
       
   179 
       
   180     return *buf;
       
   181     }
       
   182 
       
   183 //------------------------------------------------------------------------------
       
   184 // CUtils::ConvertTo16LC()
       
   185 // Converts string value to 16-bit      
       
   186 //------------------------------------------------------------------------------
       
   187 TDesC16& CUtils::ConvertTo16LC( const TDesC8& aSource )
       
   188     {
       
   189     HBufC16* buf16 = HBufC16::NewLC( aSource.Length() );
       
   190     TPtr bufPtr16 = buf16->Des();
       
   191 
       
   192     CnvUtfConverter::ConvertToUnicodeFromUtf8( bufPtr16, aSource );
       
   193 
       
   194     return *buf16;
       
   195     }
       
   196 
       
   197 // -------------------------------------------------------------------------------------
       
   198 // CUtils::FillNodeInfoL()
       
   199 // Fills the node info in ddf structure
       
   200 // -------------------------------------------------------------------------------------
       
   201 void CUtils::FillNodeInfoL( MSmlDmDDFObject& aNode,
       
   202                             TSmlDmAccessTypes aAccTypes,
       
   203                             MSmlDmDDFObject::TOccurence aOccurrence, 
       
   204                             MSmlDmDDFObject::TScope aScope, 
       
   205                             MSmlDmDDFObject::TDFFormat aFormat,
       
   206                             const TDesC8& aDescription )
       
   207                                           
       
   208     {
       
   209     aNode.SetAccessTypesL( aAccTypes );
       
   210     aNode.SetOccurenceL( aOccurrence );
       
   211     aNode.SetScopeL( aScope );
       
   212     aNode.SetDFFormatL( aFormat );
       
   213     if( aFormat != MSmlDmDDFObject::ENode )
       
   214         {
       
   215         // aNode.AddDFTypeMimeTypeL(KNSmlMimeType);
       
   216         }
       
   217     if ( aDescription != KNullDesC8 )
       
   218         {
       
   219         aNode.SetDescriptionL( aDescription );
       
   220         }
       
   221     }
       
   222 
       
   223 // -------------------------------------------------------------------------------------
       
   224 // CUtils::StripWhiteSpaces()
       
   225 // Removes all white spaces from a string
       
   226 // -------------------------------------------------------------------------------------
       
   227 void CUtils::RemoveIllegalCharsFromURI( TDes8& aString )
       
   228     {
       
   229     TInt delPos;
       
   230     TInt delLen = 1;
       
   231     
       
   232     aString.TrimAll();
       
   233     delPos = aString.Find( KWhiteSpace );
       
   234     while ( delPos != KErrNotFound )
       
   235         {
       
   236         aString.Delete( delPos,delLen );
       
   237         delPos = aString.Find( KWhiteSpace );
       
   238         }
       
   239     }
       
   240 
       
   241 // -------------------------------------------------------------------------------------
       
   242 // TInt CUtils::DesCToBool
       
   243 // -------------------------------------------------------------------------------------
       
   244 TInt32 CUtils::DesToBool( const TDesC8& aSource, TBool& aBool )
       
   245     {
       
   246     TInt retval( KErrNone );
       
   247     
       
   248     if ( aSource.Match( KTrue ) != KErrNotFound )
       
   249         {
       
   250         aBool = ETrue;
       
   251         }
       
   252     else if ( aSource.Match( KFalse ) != KErrNotFound )
       
   253         {
       
   254         aBool = EFalse;
       
   255         }
       
   256     else
       
   257         {
       
   258         retval = KErrGeneral;
       
   259         }
       
   260         
       
   261     return retval;
       
   262     }
       
   263 
       
   264 // -------------------------------------------------------------------------------------
       
   265 // CUtils::IntToCharL()
       
   266 // -------------------------------------------------------------------------------------
       
   267 TDesC8& CUtils::IntToCharL( const TInt& aObject )
       
   268     {
       
   269     HBufC8* buf = HBufC8::NewLC( 8 );
       
   270     TPtr8 ptrBuf = buf->Des();
       
   271 
       
   272     ptrBuf.Num( aObject );
       
   273 
       
   274     return *buf;
       
   275     }
       
   276 //------------------------------------------------------------------------------
       
   277 // TPtrC8 CUtils::AppendFirstURISeg( TDes8& aURI, TDes8& aTarget )
       
   278 //------------------------------------------------------------------------------
       
   279 TInt CUtils::AppendFirstURISeg( TDes8& aURI, TDes8& aTarget )
       
   280     {
       
   281     
       
   282     TInt i = aURI.FindC(_L8("/"));
       
   283     
       
   284     if ( i != KErrNotFound )
       
   285         {
       
   286         aTarget.Append( aURI.Left( i ) );
       
   287         aURI.Copy( aURI.Right( aURI.Length() - i - 1 ) );
       
   288         }
       
   289     else
       
   290         {
       
   291         aTarget.Copy( aURI );
       
   292         aURI.Copy( aURI.Right( 0 ) );
       
   293         }
       
   294     
       
   295     return i;
       
   296     }
       
   297 
       
   298 //=============================================
       
   299 //      CUtils::IntLUID()
       
   300 //      Returns IntValue for aLUID
       
   301 //=============================================
       
   302 TInt CUtils::IntLUID(const TDesC8& aLUID)
       
   303     {
       
   304     TInt ret(0);
       
   305 
       
   306     TLex8 lex(aLUID);
       
   307     
       
   308     lex.Val( ret );
       
   309     
       
   310     return ret;
       
   311     }
       
   312