phonebookui/Phonebook2/UIControls/src/TPbk2FindTextUtil.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Find text box util
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "TPbk2FindTextUtil.h"
       
    22 #include <e32std.h>
       
    23 #include <aknsfld.h>    // CAknSearchField
       
    24 
       
    25 #ifdef _DEBUG
       
    26 enum TPanicCode
       
    27     {
       
    28     EPanicLogic_FindTextBufferL
       
    29     };
       
    30 
       
    31 void Panic(TPanicCode aReason)
       
    32     {
       
    33     _LIT(KPanicText, "TPbk2FindTextUtil");
       
    34     User::Panic(KPanicText, aReason);
       
    35     }
       
    36 #endif // _DEBUG
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 void TPbk2FindTextUtil::EnsureFindTextBufSizeL( 
       
    41         const CAknSearchField& aFindBox,
       
    42         HBufC** aFindTextBuf )
       
    43     {
       
    44     // Initial minimum size for the find text buffer
       
    45     const TInt KInitialBufSize = 8;
       
    46 
       
    47     const TInt findBoxTextLength = aFindBox.TextLength();
       
    48     if (findBoxTextLength > 0)
       
    49         {
       
    50         TInt bufCapacity = 0;
       
    51         if ( *aFindTextBuf )
       
    52             {
       
    53             bufCapacity = (*aFindTextBuf)->Des().MaxLength();
       
    54             }
       
    55         if (bufCapacity < findBoxTextLength)
       
    56             {
       
    57             // Allocate a new buffer of at least KInitialBufSize
       
    58             // characters or twice as large as the previous one
       
    59             const TInt KDoubleSize = 2;
       
    60             const TInt newBufSize = Max(Max(KInitialBufSize,
       
    61                 KDoubleSize*bufCapacity), findBoxTextLength);
       
    62             HBufC* newBuf = HBufC::NewL(newBufSize);
       
    63             delete *aFindTextBuf;
       
    64             *aFindTextBuf = newBuf;
       
    65             }
       
    66         TPtr bufPtr = (*aFindTextBuf)->Des();
       
    67         __ASSERT_DEBUG
       
    68             (bufPtr.MaxLength()>=KInitialBufSize &&
       
    69             bufPtr.MaxLength()>=findBoxTextLength,
       
    70             Panic(EPanicLogic_FindTextBufferL));
       
    71         }
       
    72         
       
    73     // caller expect buffer to be found
       
    74     if ( !(*aFindTextBuf ) )
       
    75         {
       
    76         *aFindTextBuf = HBufC::NewL( KInitialBufSize );
       
    77         }
       
    78     
       
    79     }
       
    80 
       
    81 
       
    82 //  End of File