phonebookui/Phonebook2/remotecontactlookup/contactactionservice/src/FscPresentationUtils.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Implementation of presentation utilities.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "emailtrace.h"
       
    21 #include "FscPresentationUtils.h"
       
    22 
       
    23 // From system
       
    24 #include <featmgr.h>
       
    25 
       
    26 /// Namespace for local PresentationUtils definitions
       
    27 namespace __RVCT_UNS_FscPresentationUtils {
       
    28 
       
    29 // LOCAL CONSTANTS
       
    30 enum TPanicCode
       
    31     {
       
    32     EPanicPreCond_CopyAndReplaceChars = 1
       
    33     };
       
    34 
       
    35 // ================= LOCAL FUNCTIONS =======================
       
    36 #ifdef _DEBUG
       
    37 void Panic(TPanicCode aReason)
       
    38     {
       
    39     _LIT(KPanicText, "FscPresentationUtils");
       
    40     User::Panic(KPanicText, aReason);
       
    41     }
       
    42 #endif // _DEBUG
       
    43 
       
    44 // Zero Width characters
       
    45 const TUint KZWSCharacter = 0x200B;
       
    46 const TUint KZWNJCharacter = 0x200C;
       
    47 const TUint KZWJCharacter = 0x200D;
       
    48 
       
    49 // ================= LOCAL FUNCTIONS =======================
       
    50 
       
    51 /** 
       
    52  * Check if the given char is a zero width character: 
       
    53  * Zero Width Space, Zero Width Non-Joiner
       
    54  * or Zero Width Joiner character
       
    55  * @param aChar Given character
       
    56  * @return ETrue if aChar is zero width character.
       
    57  *         EFalse if aChar is not specified zero width character.
       
    58  */
       
    59 inline TBool ZWSCharacter( const TChar aChar )
       
    60     {
       
    61     const TChar zwsChar( KZWSCharacter );
       
    62     const TChar zwnjChar( KZWNJCharacter );
       
    63     const TChar zwjChar( KZWJCharacter );
       
    64     return ( aChar == zwsChar ) || ( aChar == zwnjChar ) ||
       
    65            ( aChar == zwjChar );
       
    66     }
       
    67       
       
    68 } // __RVCT_UNS_FscPresentationUtils
       
    69 
       
    70 using namespace __RVCT_UNS_FscPresentationUtils;
       
    71 
       
    72 
       
    73 // ================= MEMBER FUNCTIONS =======================
       
    74 
       
    75 // --------------------------------------------------------------------------
       
    76 // FscPresentationUtils::TrimAllAppend
       
    77 // --------------------------------------------------------------------------
       
    78 //
       
    79 TInt FscPresentationUtils::TrimAllAppend(const TDesC& aText, TDes& aDest)
       
    80     {
       
    81     FUNC_LOG;
       
    82     const TInt oldDestLength = aDest.Length();
       
    83 
       
    84     // 1. Find first non space character
       
    85     const TInt textLength = aText.Length();
       
    86     TInt firstNonSpaceChar = 0;
       
    87     while(firstNonSpaceChar < textLength
       
    88         && TChar(aText[firstNonSpaceChar]).IsSpace())
       
    89         {
       
    90         ++firstNonSpaceChar;
       
    91         }
       
    92 
       
    93     // 2. Append the source text to destination,
       
    94     // not including the leading spaces
       
    95     aDest.Append(aText.Mid(firstNonSpaceChar));
       
    96     // 3. Trim the new part of the destination to remove
       
    97     // trailing and middle spaces
       
    98     TPtr ptr = aDest.MidTPtr(oldDestLength);
       
    99     ptr.TrimAll();
       
   100     aDest.SetLength(oldDestLength);
       
   101     aDest.Append(ptr);
       
   102 
       
   103     return aDest.Length() - oldDestLength;
       
   104     }
       
   105 
       
   106 // --------------------------------------------------------------------------
       
   107 // FscPresentationUtils::TrimRightAppend
       
   108 // --------------------------------------------------------------------------
       
   109 //
       
   110  TInt FscPresentationUtils::TrimRightAppend(
       
   111         const TDesC& aText, TDes& aDest)
       
   112     {
       
   113     FUNC_LOG;
       
   114     const TInt oldDestLength = aDest.Length();
       
   115 
       
   116     // 1. Find first non space character
       
   117     const TInt textLength = aText.Length();
       
   118     TInt firstNonSpaceChar = 0;
       
   119     while(firstNonSpaceChar < textLength
       
   120           && TChar(aText[firstNonSpaceChar]).IsSpace())
       
   121         {
       
   122         ++firstNonSpaceChar;
       
   123         }
       
   124 
       
   125     // 2. Append the source text to destination,
       
   126     // not including the leading spaces
       
   127     aDest.Append(aText.Mid(firstNonSpaceChar));
       
   128     // 3. Trim the new part of the destination to remove
       
   129     // trailing and middle spaces
       
   130     TPtr ptr = aDest.MidTPtr(oldDestLength);
       
   131     ptr.TrimAll();
       
   132     aDest.SetLength(oldDestLength);
       
   133     aDest.Append(ptr);
       
   134     // 4. Insert the leading spaces to destination
       
   135     aDest.Insert(oldDestLength, aText.Left(firstNonSpaceChar));
       
   136 
       
   137     return aDest.Length() - oldDestLength;
       
   138     }
       
   139     
       
   140 // --------------------------------------------------------------------------
       
   141 // FscPresentationUtils::AppendAndReplaceChars
       
   142 // --------------------------------------------------------------------------
       
   143 //
       
   144  TInt FscPresentationUtils::AppendAndReplaceChars
       
   145         (TDes& aDest, const TDesC& aSrc,
       
   146         const TDesC& aCharsToReplace, const TDesC& aReplaceChars)
       
   147     {
       
   148     FUNC_LOG;
       
   149     // Check that aDest has enough capacity
       
   150     __ASSERT_DEBUG((aDest.MaxLength()-aDest.Length()) >= aSrc.Length(),
       
   151         Panic(EPanicPreCond_CopyAndReplaceChars));
       
   152     // Check that a replacament is provided for all characters
       
   153     __ASSERT_DEBUG(aCharsToReplace.Length() == aReplaceChars.Length(),
       
   154         Panic(EPanicPreCond_CopyAndReplaceChars));
       
   155 
       
   156     TInt count, i;
       
   157     const TInt sourceLenght = aSrc.Length();
       
   158     for (count=0, i=0; i < sourceLenght; ++i)
       
   159         {
       
   160         TText ch = aSrc[i];
       
   161         const TInt replacePos = aCharsToReplace.Locate(aSrc[i]);
       
   162         if (replacePos != KErrNotFound)
       
   163             {
       
   164             ++count;
       
   165             ch = aReplaceChars[replacePos];
       
   166             }
       
   167         aDest.Append(ch);
       
   168         }
       
   169 
       
   170     return count;
       
   171     }
       
   172     
       
   173 // --------------------------------------------------------------------------
       
   174 // FscPresentationUtils::ReplaceNonGraphicCharacters
       
   175 // --------------------------------------------------------------------------
       
   176 //
       
   177  void FscPresentationUtils::ReplaceNonGraphicCharacters
       
   178         (TDes& aText, TText aChar)
       
   179     {
       
   180     FUNC_LOG;
       
   181     const TInt len = aText.Length();
       
   182     for ( TInt i=0; i < len; ++i )
       
   183         {
       
   184         if ( !TChar(aText[i]).IsGraph() &&
       
   185              !ZWSCharacter( aText[i] ) )
       
   186             {
       
   187             // If non-graphic char is specified in ZWSCharacter,
       
   188             // it will not be replaced. Otherwise replace non-graphic
       
   189             // character with aChar.
       
   190                 aText[i] = aChar;
       
   191             }
       
   192         }
       
   193     }
       
   194 
       
   195 // --------------------------------------------------------------------------
       
   196 // FscPresentationUtils::AppendWithNewlineTranslationL
       
   197 // --------------------------------------------------------------------------
       
   198 //
       
   199  void FscPresentationUtils::AppendWithNewlineTranslationL
       
   200         ( TDes& aBuffer, const TDesC& aText )
       
   201     {
       
   202     FUNC_LOG;
       
   203     const TUint16 KCarriageReturn = 0x000d;
       
   204     const TUint16 KLineFeed = 0x000a;
       
   205     const TUint16 KParagraphSeparator = 0x2029;
       
   206     
       
   207     const TInt length( aText.Length() );
       
   208     const TInt max( aBuffer.MaxLength() );
       
   209     
       
   210     for ( register TInt i = 0;
       
   211           (i < length) && (aBuffer.Length() < max); ++i )
       
   212         {
       
   213         register TUint16 ch = aText[i];
       
   214         if ( ch == KCarriageReturn )
       
   215             {
       
   216             ch = KParagraphSeparator;
       
   217             // Skip linefeeds that follow carriage return
       
   218             if ( (i < length-1) && (aText[i+1] == KLineFeed) )
       
   219                 {
       
   220                 ++i;
       
   221                 }
       
   222             }
       
   223         else if ( ch == KLineFeed )
       
   224             {
       
   225             ch = KParagraphSeparator;
       
   226             }
       
   227         aBuffer.Append( ch );
       
   228         }
       
   229     }
       
   230 // End of File
       
   231