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