phonebookengines/VirtualPhonebook/VPbkEngUtils/src/TFixedDigitString.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  Fixed digit string handler class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "TFixedDigitString.h"
       
    21 #include "Error.h"
       
    22 
       
    23 namespace VPbkEngUtils {
       
    24 
       
    25 TFixedDigitString::TFixedDigitString() :
       
    26     // Init all digits to KNaNBCD
       
    27 	iBcdDigits(~TUint(0))
       
    28 	{
       
    29 	}
       
    30 
       
    31 TInt TFixedDigitString::Length() const
       
    32     {
       
    33     TUint mask = KNaNBCD;
       
    34     TInt length = 0;
       
    35     while ( (length < KMaxDigits) && ((iBcdDigits & mask) ^ mask))
       
    36         {
       
    37         mask <<= KBitsPerDigit;
       
    38         ++length;
       
    39         }
       
    40     return length;
       
    41     }
       
    42 
       
    43 TBool TFixedDigitString::IsEmpty() const
       
    44     {
       
    45     return (iBcdDigits == ~TUint(0));
       
    46     }
       
    47 
       
    48 TInt TFixedDigitString::operator[](TInt aIndex) const
       
    49     {
       
    50     __ASSERT_ALWAYS( aIndex >= 0 && aIndex < KMaxDigits, 
       
    51         Error::Panic(Error::EPanicInvalidIndex) );
       
    52 
       
    53     const TInt shift = aIndex * KBitsPerDigit;
       
    54     // Extract the digit from iBcdDigits
       
    55     const TUint result = (iBcdDigits & (0xf << shift)) >> shift;
       
    56     if (result != KNaNBCD)
       
    57         {
       
    58         return TInt(result);
       
    59         }
       
    60     else
       
    61         {
       
    62         // aIndex >= Length()
       
    63         Error::Panic(Error::EPanicInvalidIndex);
       
    64         return KNaN;
       
    65         }
       
    66     }
       
    67 
       
    68 #ifndef NDEBUG
       
    69 // Checking version of Compare for debug builds
       
    70 TBool TFixedDigitString::Compare(const TFixedDigitString& aOther, TInt aNumDigits) const
       
    71     {
       
    72     __ASSERT_DEBUG( aNumDigits >= 0 && aNumDigits <= KMaxDigits, 
       
    73         Error::Panic(Error::EPanicInvalidValue) );
       
    74     return DoCompare(aOther, aNumDigits);
       
    75     }
       
    76 #endif
       
    77         
       
    78 void TFixedDigitString::Reset()
       
    79     {
       
    80     // Reset all digits to KNaNBCD
       
    81     iBcdDigits = ~TUint(0);
       
    82     }
       
    83 
       
    84 
       
    85 void TFixedDigitString::EncodeDigits(TPtrC& aDigitsText)
       
    86     {
       
    87     // Reset all digits to zero
       
    88     iBcdDigits = 0;
       
    89 
       
    90     // Init loop counters
       
    91     TInt shift = 0;
       
    92     TInt textPos = 0;
       
    93     const TInt textLen = aDigitsText.Length();
       
    94 
       
    95     while (textPos < textLen && shift < KMaxBits)
       
    96         {
       
    97         const TInt numVal = TChar(aDigitsText[textPos++]).GetNumericValue();
       
    98         if (numVal >= 0 && numVal <= 9)
       
    99             {
       
   100             // Encode the digit to current position
       
   101             iBcdDigits |= TUint(numVal) << shift;
       
   102             shift += KBitsPerDigit;
       
   103             }
       
   104         }
       
   105 
       
   106     // Set unused digits to KNanBCD
       
   107     if (shift < KMaxBits)
       
   108         {
       
   109         iBcdDigits |= ~TUint(0) << shift;
       
   110         }
       
   111 
       
   112     // Adjust aDigitsText to point to the first character that was not
       
   113     // considered for encoding
       
   114     aDigitsText.Set(aDigitsText.Mid(textPos));
       
   115     }
       
   116 
       
   117 
       
   118 } // namespace VPbkEngUtils
       
   119 
       
   120 //End of file
       
   121 
       
   122