mmserv/radioutility/presetutility/src/cenrepbookkeeping.cpp
changeset 14 80975da52420
equal deleted inserted replaced
12:5a06f39ad45b 14:80975da52420
       
     1 /*
       
     2 * Copyright (c) 2009 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 <s32mem.h> // RWriteStream, RReadStream
       
    20 
       
    21 #include "cenrepbookkeeping.h"
       
    22 #include "trace.h"
       
    23 
       
    24 
       
    25 const TUint8 KNbrOfBitsPerByte = 8;
       
    26 
       
    27 
       
    28 #define BITSPERLWORD ( sizeof(iBookKeepingData.iUsedIndexLWords[0]) * KNbrOfBitsPerByte)
       
    29 
       
    30 
       
    31 // ============= MEMBER FUNCTIONS ==============
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CCenrepBookKeeping::CCenrepBookKeeping()
       
    38     {
       
    39     FUNC_LOG;
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CCenrepBookKeeping::~CCenrepBookKeeping()
       
    47     {
       
    48     FUNC_LOG;
       
    49     Clear();
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 TInt CCenrepBookKeeping::PresetCount() const
       
    57     {
       
    58     FUNC_LOG;
       
    59     return iBookKeepingData.iCount;
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 TInt CCenrepBookKeeping::FirstIndex() const
       
    67     {
       
    68     FUNC_LOG;
       
    69     TInt retIndex( KErrGeneral );
       
    70     TUint32 bitMask( 0 );
       
    71     for ( TInt lwordOffset = 0; ; ++lwordOffset)
       
    72         {
       
    73         if ( 0 !=iBookKeepingData.iUsedIndexLWords[lwordOffset] )
       
    74             {
       
    75             bitMask = 1;
       
    76             for ( TUint8 bitOffset( 0 ); BITSPERLWORD > bitOffset; ++bitOffset )
       
    77                 {
       
    78                 if ( KMaxNumberOfPresetStations <= ( ( BITSPERLWORD * lwordOffset ) + bitOffset ) )
       
    79                     {
       
    80                     retIndex = KErrNotFound;
       
    81                     break;
       
    82                     }
       
    83                 if ( 0 != ( iBookKeepingData.iUsedIndexLWords[lwordOffset] & bitMask) )
       
    84                     {
       
    85                     retIndex = ( BITSPERLWORD * lwordOffset ) + bitOffset;
       
    86                     break;
       
    87                     }
       
    88                 bitMask = bitMask << 1;                
       
    89                 }
       
    90             }
       
    91         else if ( KMaxNumberOfPresetStations < ( BITSPERLWORD * (lwordOffset + 1) ) )
       
    92             {
       
    93             retIndex = KErrNotFound;
       
    94             break;            
       
    95             }
       
    96         if ( KErrGeneral != retIndex )
       
    97             {
       
    98             break;
       
    99             }
       
   100         }
       
   101     INFO_1( "retIndex: %i", retIndex );
       
   102     return retIndex;
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 TInt CCenrepBookKeeping::NextIndex( TInt aFromIndex )
       
   110     {
       
   111     FUNC_LOG;
       
   112     TInt retIndex( KErrGeneral );
       
   113     TInt startIndex( aFromIndex + 1 );
       
   114     TUint32 bitMask( 0 );
       
   115     TUint8 bitOffset( 0 );
       
   116     for ( TInt lwordOffset(startIndex/BITSPERLWORD); ; ++lwordOffset)
       
   117         {
       
   118         if ( lwordOffset == (startIndex/BITSPERLWORD) )
       
   119             {
       
   120             bitMask = 1 << (startIndex%BITSPERLWORD);
       
   121             bitOffset = (startIndex%BITSPERLWORD);
       
   122             }
       
   123         else
       
   124             {
       
   125             bitMask = 1;
       
   126             bitOffset = 0;
       
   127             }
       
   128         if ( 0 !=iBookKeepingData.iUsedIndexLWords[lwordOffset] )
       
   129             {            
       
   130             for ( ; BITSPERLWORD > bitOffset; ++bitOffset )
       
   131                 {
       
   132                 if ( KMaxNumberOfPresetStations <=  ( ( BITSPERLWORD * lwordOffset ) + bitOffset ) )
       
   133                     {
       
   134                     retIndex = KErrNotFound;
       
   135                     break;
       
   136                     }
       
   137                 if ( 0 != ( iBookKeepingData.iUsedIndexLWords[lwordOffset] & bitMask) )
       
   138                     {
       
   139                     retIndex = ( BITSPERLWORD * lwordOffset ) + bitOffset;
       
   140                     break;
       
   141                     }
       
   142                 bitMask = bitMask << 1;                
       
   143                 }
       
   144             }
       
   145         else if ( KMaxNumberOfPresetStations < ( BITSPERLWORD * (lwordOffset + 1) ) )
       
   146             {
       
   147             retIndex = KErrNotFound;
       
   148             break;            
       
   149             }
       
   150         if ( KErrGeneral != retIndex )
       
   151             {
       
   152             break;
       
   153             }
       
   154         }
       
   155     INFO_1( "retIndex: %i", retIndex );
       
   156     return retIndex;
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 // ---------------------------------------------------------------------------
       
   162 //
       
   163 void CCenrepBookKeeping::AddIndex( const TInt aIndex )
       
   164     {
       
   165     FUNC_LOG;
       
   166     if  (
       
   167         ( KMaxNumberOfPresetStations >iBookKeepingData.iCount ) &&
       
   168         ( !IsIndexUsed( aIndex ) )
       
   169     )
       
   170         {
       
   171         TInt lwordOffset(aIndex/BITSPERLWORD);
       
   172         TUint bitOffset( (aIndex%BITSPERLWORD) );
       
   173         TUint32 bitMask( 1 );
       
   174         bitMask  = bitMask << bitOffset;
       
   175        iBookKeepingData.iUsedIndexLWords[lwordOffset] |= bitMask;    
       
   176         ++iBookKeepingData.iCount;       
       
   177         }
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184 void CCenrepBookKeeping::RemoveIndex( const TInt aIndex )
       
   185     {
       
   186     FUNC_LOG;
       
   187     if  ( 
       
   188         ( 0 <iBookKeepingData.iCount ) &&
       
   189         ( IsIndexUsed( aIndex ) )
       
   190     ) 
       
   191         {
       
   192         TInt lwordOffset(aIndex/BITSPERLWORD);
       
   193         TUint bitOffset( (aIndex%BITSPERLWORD) );
       
   194         TUint32 bitMask( 1 );
       
   195         bitMask = bitMask << bitOffset;
       
   196         bitMask = ~bitMask;
       
   197        iBookKeepingData.iUsedIndexLWords[lwordOffset] &= bitMask;    
       
   198         --iBookKeepingData.iCount;       
       
   199         }
       
   200     }
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 //
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void CCenrepBookKeeping::Clear()
       
   207     {
       
   208     FUNC_LOG;
       
   209     for ( TInt lwordOffset(0); LENGTHOFUSEDINDEXLWORDSARRAY > lwordOffset; ++lwordOffset)
       
   210         {
       
   211        iBookKeepingData.iUsedIndexLWords[lwordOffset] = 0;
       
   212         }
       
   213    iBookKeepingData.iCount = 0;    
       
   214     }
       
   215 
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 // ---------------------------------------------------------------------------
       
   219 //
       
   220 TInt CCenrepBookKeeping::FindUnusedIndex() const
       
   221     {
       
   222     FUNC_LOG;
       
   223     TInt retIndex( KErrGeneral );
       
   224     TUint32 bitMask( 0 );
       
   225     for ( TInt lwordOffset = 0; ; ++lwordOffset)
       
   226         {
       
   227         if ( 0xFFFFFFFF !=iBookKeepingData.iUsedIndexLWords[lwordOffset] )
       
   228             {
       
   229             bitMask = 1;
       
   230             for ( TUint8 bitOffset = 0; BITSPERLWORD > bitOffset; ++bitOffset )
       
   231                 {
       
   232                 if ( KMaxNumberOfPresetStations <=  ( ( BITSPERLWORD * lwordOffset ) + bitOffset ) )
       
   233                     {
       
   234                     retIndex = KErrNotFound;
       
   235                     break;
       
   236                     }
       
   237                 if ( 0 == ( iBookKeepingData.iUsedIndexLWords[lwordOffset] & bitMask) )
       
   238                     {
       
   239                     retIndex = ( BITSPERLWORD * lwordOffset ) + bitOffset;
       
   240                     break;
       
   241                     }
       
   242                 bitMask = bitMask << 1;                
       
   243                 }
       
   244             }
       
   245         else if ( KMaxNumberOfPresetStations < ( BITSPERLWORD * (lwordOffset + 1) ) )
       
   246             {
       
   247             retIndex = KErrNotFound;
       
   248             break;            
       
   249             }
       
   250         if ( KErrGeneral != retIndex )
       
   251             {
       
   252             break;
       
   253             }
       
   254         }
       
   255     INFO_1( "retIndex: %i", retIndex );
       
   256     return retIndex;
       
   257     }
       
   258 
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 TBool CCenrepBookKeeping::IsIndexUsed( const TInt aIndex )
       
   264     {
       
   265     FUNC_LOG;
       
   266     TInt lwordOffset(aIndex/BITSPERLWORD);
       
   267     TUint bitOffset( (aIndex%BITSPERLWORD) );
       
   268     TUint32 bitMask( 1 );
       
   269     bitMask = bitMask << bitOffset;
       
   270     return ( iBookKeepingData.iUsedIndexLWords[lwordOffset] & bitMask);    
       
   271     }
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 // ---------------------------------------------------------------------------
       
   276 //
       
   277 TPtr8 CCenrepBookKeeping::BookKeepingData()
       
   278     {
       
   279     TPtr8 bookKeepingData( (TUint8*)&iBookKeepingData, sizeof( iBookKeepingData ), sizeof( iBookKeepingData ) );
       
   280     return bookKeepingData;
       
   281     }