lowlevellibsandfws/apputils/src/BAMATCH.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Started by Brendan, January 1996
       
    15 // Descended from HCIMATCH.CPP
       
    16 // Incremental matcher class
       
    17 // 
       
    18 //
       
    19 
       
    20 #include <bamatch.h>
       
    21 #include <baflpan.h>
       
    22 #include <bamdesca.h>
       
    23 
       
    24 
       
    25 
       
    26 EXPORT_C RTextBuf::RTextBuf()
       
    27 	: iPtr(NULL,0), iText(NULL)
       
    28 /** Default constructor. */
       
    29 	{}
       
    30 
       
    31 
       
    32 EXPORT_C RTextBuf::~RTextBuf()
       
    33 /** Destructor. */
       
    34 	{
       
    35 	Close();
       
    36 	}
       
    37 
       
    38 
       
    39 EXPORT_C void RTextBuf::SetMaxLengthL(TInt aMaxLength)
       
    40 /** Sets the maximum length of the text that the object can store.
       
    41 
       
    42 Attempts to store text beyond the maximum length cause a panic, as
       
    43 with descriptors. */
       
    44     {
       
    45    	__ASSERT_ALWAYS(aMaxLength>0,Panic(EBafPanicTextBufOutOfRange));
       
    46 	Close();
       
    47 	iText=new(ELeave) TText[aMaxLength+1];
       
    48 	iPtr.Set(iText,0,aMaxLength);
       
    49 	}
       
    50 
       
    51 EXPORT_C void RTextBuf::Close()
       
    52 	{
       
    53 	delete [] iText;
       
    54 	iText=NULL;
       
    55 	iPtr.Set(NULL,0,0);
       
    56    	}
       
    57 
       
    58 // class RIncrMatcherBase
       
    59 
       
    60 EXPORT_C RIncrMatcherBase::~RIncrMatcherBase()
       
    61 /** Virtual destructor, for reimplementation by derived classes. */
       
    62 	{}
       
    63 
       
    64 TBool RIncrMatcherBase::DoIsMatch(const TDesC& aQuery,TCompareFunc aCompare) const
       
    65 	{
       
    66 	const TDes& des=MatchDes();
       
    67 	TInt l=des.Length();
       
    68 	return l>aQuery.Length() ? EFalse : aCompare(aQuery.Ptr(),l,des.Ptr(),l)==0;
       
    69 	}
       
    70 
       
    71 TInt RIncrMatcherBase::DoFirstMatchingIndex(TInt& aIndex,const MDesCArray& aDesArray,TCompareFunc aCompare) const
       
    72 	{
       
    73 	TInt noElements=aDesArray.MdcaCount();
       
    74 	__ASSERT_ALWAYS(aIndex>=0 && aIndex<noElements,Panic(EBafPanicMatcherOutOfRange));
       
    75 	do
       
    76 		{
       
    77 		if (DoIsMatch(aDesArray.MdcaPoint(aIndex),aCompare))
       
    78 			return KErrNone;
       
    79 		} while (++aIndex<noElements);
       
    80 	return KErrNotFound;
       
    81 	}
       
    82 
       
    83 void RIncrMatcherBase::DoSetBestMatch(const TDesC& aBuf,TCompareFunc aCompare)
       
    84 	{
       
    85 	TDes& des=MatchDes();
       
    86 	des.SetLength(Min(des.Length(),aBuf.Length()));
       
    87 	while (!DoIsMatch(aBuf,aCompare))
       
    88 		DeleteLastChar();
       
    89 	}
       
    90 
       
    91 
       
    92 EXPORT_C TBool RIncrMatcherBase::IsMatch(const TDesC& aQuery) const
       
    93 /**Tests for match, using a binary comparison.
       
    94 
       
    95 @param aQuery Text to match
       
    96 @return  ETrue if match found, else EFalse */
       
    97     {
       
    98 	return DoIsMatch(aQuery,Mem::Compare);
       
    99 	}
       
   100 
       
   101 
       
   102 EXPORT_C TBool RIncrMatcherBase::IsMatchC(const TDesC& aQuery) const
       
   103 /** Tests for match, using locale collation rules.
       
   104 
       
   105 @param aQuery  Text to match
       
   106 @return ETrue if match found, else EFalse */
       
   107     {
       
   108 	return DoIsMatch(aQuery,Mem::CompareC);
       
   109 	}
       
   110 
       
   111 EXPORT_C TBool RIncrMatcherBase::IsMatchF(const TDesC& aQuery) const
       
   112 /** Tests for match, using locale folding rules.
       
   113 
       
   114 @param    aQuery  Text to match
       
   115 @return  ETrue if match found, else EFalse */
       
   116 	{
       
   117 	return DoIsMatch(aQuery,Mem::CompareF);
       
   118 	}
       
   119 
       
   120 EXPORT_C TInt RIncrMatcherBase::FirstMatchingIndex(TInt& aResultIndex,const MDesCArray& aDesArray,TInt aStartIndex) const
       
   121 /** Finds the first match in an array, using a binary comparison.
       
   122 
       
   123 @param aResult On return, index of the first match in aDesArray 
       
   124 @param aDesArray  Array of descriptors to search
       
   125 @param aStartIndex Index of aDesArray at which to begin  search
       
   126 @return  KErrNone if success or KErrNotFound if no match is found */
       
   127 	{
       
   128 	aResultIndex=aStartIndex;
       
   129 	return DoFirstMatchingIndex(aResultIndex,aDesArray,Mem::Compare);
       
   130 	}
       
   131 
       
   132 EXPORT_C TInt RIncrMatcherBase::FirstMatchingIndexC(TInt& aResultIndex,const MDesCArray& aDesArray,TInt aStartIndex) const
       
   133 /** Finds the first match in an array, using locale collation rules.
       
   134 
       
   135 @param aResult  On return, index of the first match in aDesArray 
       
   136 @param aDesArray Array of descriptors to search
       
   137 @param aStartIndex Index of aDesArray  at which to begin search
       
   138 @return  KErrNone if success or KErrNotFound if no match is found */
       
   139     {
       
   140 	aResultIndex=aStartIndex;
       
   141 	return DoFirstMatchingIndex(aResultIndex,aDesArray,Mem::CompareC);
       
   142 	}
       
   143 
       
   144 EXPORT_C TInt RIncrMatcherBase::FirstMatchingIndexF(TInt& aResultIndex,const MDesCArray& aDesArray,TInt aStartIndex) const
       
   145 /** Finds the first match in an array, using locale folding rules.
       
   146 
       
   147 @param aResult  On return, index of the first match in aDesArray
       
   148 @param     aDesArray Array of descriptors to search
       
   149 @param     aStartIndex Index of aDesArray at which to begin search
       
   150 @return  KErrNone if success or KErrNotFound if no match is found */
       
   151     {
       
   152 	aResultIndex=aStartIndex;
       
   153 	return DoFirstMatchingIndex(aResultIndex,aDesArray,Mem::CompareF);
       
   154 	}
       
   155 
       
   156 EXPORT_C void RIncrMatcherBase::SetBestMatch(const TDesC& aBuf)
       
   157 /** Sets the match text to the best match between the match text and the passed 
       
   158 buffer, using a binary comparision.
       
   159 
       
   160 For example, if the original match text is "goose" and the passed buffer is 
       
   161 "gooSE", the match text would be changed to "goo".
       
   162 
       
   163 @param aBuf Text to match */
       
   164 	{
       
   165 	DoSetBestMatch(aBuf,Mem::Compare);
       
   166 	}
       
   167 
       
   168 EXPORT_C void RIncrMatcherBase::SetBestMatchC(const TDesC& aBuf)
       
   169 /** Sets the match text to the best match between the match text and the passed 
       
   170 buffer, using locale collation rules.
       
   171 
       
   172 @param aBuf Text to match */
       
   173 	{
       
   174 	DoSetBestMatch(aBuf,Mem::CompareC);
       
   175 	}
       
   176 
       
   177 EXPORT_C void RIncrMatcherBase::SetBestMatchF(const TDesC& aBuf)
       
   178 /** Sets the match text to the best match between the match text and the passed 
       
   179 buffer, using locale folding rules.
       
   180 
       
   181 @param aBuf Text to match */
       
   182 	{
       
   183 	DoSetBestMatch(aBuf,Mem::CompareF);
       
   184 	}
       
   185 
       
   186 EXPORT_C void RIncrMatcherBase::AppendChar(TChar aLetter)
       
   187 /** Appends a character to the end of the match text.
       
   188 
       
   189 @param aLetter Character to append */
       
   190 	{
       
   191 	TDes& des=MatchDes();
       
   192 	__ASSERT_ALWAYS(des.Length()<des.MaxLength(),Panic(EBafPanicMatcherOutOfRange));
       
   193 	des.Append(aLetter);
       
   194 	}
       
   195 
       
   196 EXPORT_C void RIncrMatcherBase::DeleteLastChar()
       
   197 /** Deletes the final character in the match text. */
       
   198 	{
       
   199 	TDes& des=MatchDes();
       
   200 	TInt l=des.Length();
       
   201 	if (--l>=0)
       
   202 		{
       
   203 		des[l]=0;
       
   204 		des.SetLength(l);
       
   205 		}
       
   206 	}
       
   207 
       
   208 EXPORT_C RIncrMatcherPtr::RIncrMatcherPtr()
       
   209     : iDesPtr(NULL)
       
   210 /** Default constructor. */
       
   211     {
       
   212     }
       
   213 
       
   214 EXPORT_C RIncrMatcherPtr::RIncrMatcherPtr(TDes& aDes)
       
   215     : iDesPtr(&aDes)
       
   216 /** Constructor that initialises the object with the text to be matched against.
       
   217 
       
   218 @param aDes Text to be matched against */
       
   219     {
       
   220     }
       
   221 
       
   222 EXPORT_C RIncrMatcherPtr::~RIncrMatcherPtr()
       
   223 /** Destructor. */
       
   224     {
       
   225     }
       
   226 
       
   227 EXPORT_C TDes& RIncrMatcherPtr::MatchDes()
       
   228 /** Gets the match text.
       
   229 
       
   230 @return Match text */
       
   231     {
       
   232     return(*iDesPtr);
       
   233     }
       
   234 
       
   235 EXPORT_C const TDes& RIncrMatcherPtr::MatchDes() const
       
   236 /** Gets the match text.
       
   237 
       
   238 @return Match text */
       
   239     {
       
   240     return(*iDesPtr);
       
   241     }
       
   242 
       
   243 
       
   244 EXPORT_C RIncrMatcherTextBuf::RIncrMatcherTextBuf()
       
   245 /** Default constructor. */
       
   246     {
       
   247     }
       
   248 
       
   249 EXPORT_C RIncrMatcherTextBuf::~RIncrMatcherTextBuf()
       
   250 /** Destructor.*/
       
   251     {
       
   252     }
       
   253 
       
   254 EXPORT_C TDes& RIncrMatcherTextBuf::MatchDes()
       
   255 /** Gets the match text.
       
   256 
       
   257 @return Match text */
       
   258     {
       
   259     return(iTextBuf.Text());
       
   260     }
       
   261 
       
   262 EXPORT_C const TDes& RIncrMatcherTextBuf::MatchDes() const
       
   263 /** Gets the match text.
       
   264 
       
   265 @return Match text */
       
   266    {
       
   267     return(iTextBuf.Text());
       
   268     }