lowlevellibsandfws/apputils/inc/BAMATCH.H
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2003-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 // Incremental matcher class
       
    15 // 
       
    16 //
       
    17 
       
    18 #if !defined (__BAMATCH_H__)
       
    19 #define __BAMATCH_H__
       
    20 
       
    21 #if !defined(__E32STD_H__)
       
    22 #include <e32std.h>
       
    23 #endif
       
    24 
       
    25 #if !defined(__E32BASE_H__)
       
    26 #include <e32base.h>
       
    27 #endif
       
    28 
       
    29 #if !defined(__BAMDESCA_H__)
       
    30 #include <bamdesca.h>
       
    31 #endif
       
    32 
       
    33 
       
    34 class RTextBuf
       
    35 /** A simple class that encapsulates a text string.
       
    36 
       
    37 As with the descriptor classes, the class sets a maximum length for the encapsulated 
       
    38 string, which cannot be exceeded. The class might be preferred to a descriptor 
       
    39 in some circumstances as its maximum length can be increased (unlike a TBuf), 
       
    40 and it can be created on the stack (unlike an HBufC) . 
       
    41 @publishedAll
       
    42 @released
       
    43 */
       
    44 	{
       
    45 public:
       
    46 	IMPORT_C RTextBuf();
       
    47 	IMPORT_C ~RTextBuf();
       
    48 public:
       
    49 	inline TPtr& Text();
       
    50 	inline const TPtr& Text() const;
       
    51 	inline TInt MaxLength() const;
       
    52 	inline void SetText(const TDesC &aDes);
       
    53 	IMPORT_C void SetMaxLengthL(TInt aMaxLength);
       
    54 	IMPORT_C void Close();
       
    55 private:
       
    56 	TPtr iPtr;
       
    57 	TText* iText;
       
    58 	};
       
    59 
       
    60 
       
    61 
       
    62 class RIncrMatcherBase
       
    63 /** Base class for incremental matcher classes.
       
    64 
       
    65 An incremental matcher compares two text buffers from left-to-right. For example, 
       
    66 the match between "garage" and "gander" is "ga".
       
    67 
       
    68 The class provides the interface for getting and setting the text to be matched 
       
    69 against, and for performing match tests. Each type of match test is available 
       
    70 in three versions, one using binary comparison, one using locale collation 
       
    71 rules (comparison of strings to produce a dictionary-like ('lexicographic') 
       
    72 ordering, e.g. ignoring punctuation), and one using locale folding rules (e.g. 
       
    73 ignoring case). 
       
    74 
       
    75 "bafl.lib" 
       
    76 @since 5.0
       
    77 @publishedAll
       
    78 @released
       
    79 */
       
    80 	{
       
    81 public:
       
    82     IMPORT_C virtual ~RIncrMatcherBase();
       
    83 protected:
       
    84 	/** Default constructor. */
       
    85 	inline RIncrMatcherBase() {}
       
    86 protected:
       
    87 	/** Gets the match text.
       
    88 	
       
    89 	@return Match text */
       
    90 	virtual TDes& MatchDes()=0;
       
    91 
       
    92 	/** Gets the match text.
       
    93 	
       
    94 	@return Match text */
       
    95 	virtual const TDes& MatchDes() const=0; 
       
    96 public:
       
    97 	inline TInt MaxLength() const;
       
    98 	inline TInt MatchLength() const;
       
    99 	inline TPtrC MatchText() const;
       
   100 //
       
   101 	inline void Clear();
       
   102 	IMPORT_C void DeleteLastChar();
       
   103 	IMPORT_C void AppendChar(TChar aLetter);
       
   104 	inline void SetMatchText(const TDesC& aText);
       
   105 //
       
   106 	IMPORT_C void SetBestMatch(const TDesC& aBuf);
       
   107 	IMPORT_C void SetBestMatchC(const TDesC& aBuf);
       
   108 	IMPORT_C void SetBestMatchF(const TDesC& aBuf);
       
   109 	IMPORT_C TBool IsMatch(const TDesC& aBuf) const;
       
   110 	IMPORT_C TBool IsMatchC(const TDesC& aBuf) const;
       
   111 	IMPORT_C TBool IsMatchF(const TDesC& aBuf) const;
       
   112 	IMPORT_C TInt FirstMatchingIndex(TInt& aResult,const MDesCArray& aDesArray,TInt aStartIndex=0) const;
       
   113 	IMPORT_C TInt FirstMatchingIndexC(TInt& aResult,const MDesCArray& aDesArray,TInt aStartIndex=0) const;
       
   114 	IMPORT_C TInt FirstMatchingIndexF(TInt& aResult,const MDesCArray& aDesArray,TInt aStartIndex=0) const;
       
   115 private:
       
   116 	typedef TInt (*TCompareFunc)(const TText*,TInt,const TText*,TInt);
       
   117 	TBool DoIsMatch(const TDesC& aQuery,TCompareFunc aCompare) const;
       
   118 	TInt DoFirstMatchingIndex(TInt& aIndex,const MDesCArray& aDesArray,TCompareFunc aCompare) const;
       
   119 	void DoSetBestMatch(const TDesC& aBuf,TCompareFunc aCompare);
       
   120 	};
       
   121 
       
   122 
       
   123 class RIncrMatcherPtr : public RIncrMatcherBase
       
   124 /** Incrementally matches text against a descriptor, accessed via a pointer.
       
   125 
       
   126 The class does not take a copy of the text to match against, but only stores 
       
   127 the pointer. 
       
   128 @publishedAll
       
   129 @released
       
   130 */
       
   131 	{
       
   132 public:
       
   133 	IMPORT_C RIncrMatcherPtr();
       
   134 	IMPORT_C RIncrMatcherPtr(TDes& aDes);
       
   135     IMPORT_C virtual ~RIncrMatcherPtr();
       
   136 public:
       
   137 	inline void SetMatcherPtr(TDes& aDes);
       
   138 protected:
       
   139 	IMPORT_C TDes& MatchDes();
       
   140 	IMPORT_C const TDes& MatchDes() const;
       
   141 private:
       
   142 	TDes* iDesPtr;
       
   143 	};
       
   144 
       
   145 
       
   146 class RIncrMatcherTextBuf : public RIncrMatcherBase
       
   147 /** Incrementally matches text against a text buffer with variable maximum 
       
   148 length. 
       
   149 @publishedAll
       
   150 @released
       
   151 */
       
   152 	{
       
   153 public:
       
   154     IMPORT_C RIncrMatcherTextBuf();
       
   155     IMPORT_C virtual ~RIncrMatcherTextBuf();
       
   156 	inline void SetMatcherLengthL(TInt aMaxLength);
       
   157 protected:
       
   158 	IMPORT_C TDes& MatchDes();
       
   159 	IMPORT_C const TDes& MatchDes() const;
       
   160 private:
       
   161 	RTextBuf iTextBuf;
       
   162 	};
       
   163 
       
   164 template <TInt aMaximumSize> 
       
   165 class RIncrMatcherBuf : public RIncrMatcherBase
       
   166 /**
       
   167 Incrementally matches text against a modifiable descriptor buffer.
       
   168 Set aMaximumSize to be the maximum size of this buffer. 
       
   169 @publishedAll
       
   170 @released
       
   171 */
       
   172 	{
       
   173 public:
       
   174 	/** Default constructor. */
       
   175 	RIncrMatcherBuf() {}
       
   176 protected:
       
   177 
       
   178 	/**	 Gets the match text.
       
   179 	 
       
   180 	  @return   Match text (non const) */
       
   181 	inline TDes& MatchDes() {return(iMatchBuf);}
       
   182 
       
   183 	/**	 Gets the match text.
       
   184 	 
       
   185 	  @return   Match text (const)*/
       
   186 	inline const TDes& MatchDes() const {return(iMatchBuf);}
       
   187 
       
   188 	/** Copy constructor.
       
   189 	
       
   190 	@param aMatcher RIncrMatcherBuf to copy */
       
   191 	inline RIncrMatcherBuf(const RIncrMatcherBuf& aMatcher) {iMatchBuf=aMatcher.iMatchBuf;}
       
   192 private:
       
   193 	TBuf<aMaximumSize> iMatchBuf;
       
   194     };
       
   195 
       
   196 inline TPtr& RTextBuf::Text()
       
   197 /** Gets the text as a descriptor. 
       
   198 
       
   199 @return   Match text descriptor (non const) */
       
   200 	{return(iPtr);}
       
   201 
       
   202 inline const TPtr& RTextBuf::Text() const
       
   203 /** Gets the text as a descriptor. 
       
   204 
       
   205 @return   Match text descriptor (const) */
       
   206 	{return(iPtr);}
       
   207 
       
   208 inline TInt RTextBuf::MaxLength() const
       
   209 /** Gets the maximum length of the text. 
       
   210 
       
   211 @return The maximum length of the text.*/
       
   212 	{return(iPtr.MaxLength());}
       
   213 
       
   214 inline void RTextBuf::SetText(const TDesC &aDes)
       
   215 /** Sets the text. You must have set the maximum length appropriately first with
       
   216 SetMaxLengthL(). */
       
   217 	{iPtr.Copy(aDes);}
       
   218 
       
   219 inline TInt RIncrMatcherBase::MaxLength() const
       
   220 /** Gets the maximum length of the match text buffer. */
       
   221 	{return(MatchDes().MaxLength());}
       
   222 inline TInt RIncrMatcherBase::MatchLength() const
       
   223 /** Gets the current length of the match text buffer. */
       
   224 	{return(MatchDes().Length());}
       
   225 inline TPtrC RIncrMatcherBase::MatchText() const
       
   226 /** Gets the match text as a TPtrC. */
       
   227 	{return(TPtrC(MatchDes()));}
       
   228 inline void RIncrMatcherBase::SetMatchText(const TDesC& aText)
       
   229 /** Sets the match text.
       
   230 	
       
   231 @param aText String to which to set the match text. */
       
   232 	{MatchDes()=aText;}
       
   233 
       
   234 inline void RIncrMatcherBase::Clear()
       
   235 /** Clears the match text. */
       
   236 	{MatchDes().Zero();}
       
   237 
       
   238 inline void RIncrMatcherPtr::SetMatcherPtr(TDes& aDes)
       
   239 /** Sets the text to be matched against.
       
   240 	
       
   241 @param aDes Text to be matched against */
       
   242 	{iDesPtr=&aDes;}
       
   243 
       
   244 
       
   245 inline void RIncrMatcherTextBuf::SetMatcherLengthL(TInt aMaxLength)
       
   246 /** Sets the maximum length of text that can be stored through SetMatchText() 
       
   247 etc.
       
   248 	
       
   249 @param aMaxLength Maximum length of text that can be stored */
       
   250 	{iTextBuf.SetMaxLengthL(aMaxLength);}
       
   251 
       
   252 
       
   253 #endif