williamr@2: // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). williamr@2: // All rights reserved. williamr@2: // This component and the accompanying materials are made available williamr@2: // under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members williamr@2: // which accompanies this distribution, and is available williamr@2: // at the URL "http://www.symbianfoundation.org/legal/licencesv10.html". williamr@2: // williamr@2: // Initial Contributors: williamr@2: // Nokia Corporation - initial contribution. williamr@2: // williamr@2: // Contributors: williamr@2: // williamr@2: // Description: williamr@2: // Incremental matcher class williamr@2: // williamr@2: // williamr@2: williamr@2: #if !defined (__BAMATCH_H__) williamr@2: #define __BAMATCH_H__ williamr@2: williamr@2: #if !defined(__E32STD_H__) williamr@2: #include williamr@2: #endif williamr@2: williamr@2: #if !defined(__E32BASE_H__) williamr@2: #include williamr@2: #endif williamr@2: williamr@2: #if !defined(__BAMDESCA_H__) williamr@2: #include williamr@2: #endif williamr@2: williamr@2: williamr@2: class RTextBuf williamr@2: /** A simple class that encapsulates a text string. williamr@2: williamr@2: As with the descriptor classes, the class sets a maximum length for the encapsulated williamr@2: string, which cannot be exceeded. The class might be preferred to a descriptor williamr@2: in some circumstances as its maximum length can be increased (unlike a TBuf), williamr@2: and it can be created on the stack (unlike an HBufC) . williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: { williamr@2: public: williamr@2: IMPORT_C RTextBuf(); williamr@2: IMPORT_C ~RTextBuf(); williamr@2: public: williamr@2: inline TPtr& Text(); williamr@2: inline const TPtr& Text() const; williamr@2: inline TInt MaxLength() const; williamr@2: inline void SetText(const TDesC &aDes); williamr@2: IMPORT_C void SetMaxLengthL(TInt aMaxLength); williamr@2: IMPORT_C void Close(); williamr@2: private: williamr@2: TPtr iPtr; williamr@2: TText* iText; williamr@2: }; williamr@2: williamr@2: williamr@2: williamr@2: class RIncrMatcherBase williamr@2: /** Base class for incremental matcher classes. williamr@2: williamr@2: An incremental matcher compares two text buffers from left-to-right. For example, williamr@2: the match between "garage" and "gander" is "ga". williamr@2: williamr@2: The class provides the interface for getting and setting the text to be matched williamr@2: against, and for performing match tests. Each type of match test is available williamr@2: in three versions, one using binary comparison, one using locale collation williamr@2: rules (comparison of strings to produce a dictionary-like ('lexicographic') williamr@2: ordering, e.g. ignoring punctuation), and one using locale folding rules (e.g. williamr@2: ignoring case). williamr@2: williamr@2: "bafl.lib" williamr@2: @since 5.0 williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: { williamr@2: public: williamr@2: IMPORT_C virtual ~RIncrMatcherBase(); williamr@2: protected: williamr@2: /** Default constructor. */ williamr@2: inline RIncrMatcherBase() {} williamr@2: protected: williamr@2: /** Gets the match text. williamr@2: williamr@2: @return Match text */ williamr@2: virtual TDes& MatchDes()=0; williamr@2: williamr@2: /** Gets the match text. williamr@2: williamr@2: @return Match text */ williamr@2: virtual const TDes& MatchDes() const=0; williamr@2: public: williamr@2: inline TInt MaxLength() const; williamr@2: inline TInt MatchLength() const; williamr@2: inline TPtrC MatchText() const; williamr@2: // williamr@2: inline void Clear(); williamr@2: IMPORT_C void DeleteLastChar(); williamr@2: IMPORT_C void AppendChar(TChar aLetter); williamr@2: inline void SetMatchText(const TDesC& aText); williamr@2: // williamr@2: IMPORT_C void SetBestMatch(const TDesC& aBuf); williamr@2: IMPORT_C void SetBestMatchC(const TDesC& aBuf); williamr@2: IMPORT_C void SetBestMatchF(const TDesC& aBuf); williamr@2: IMPORT_C TBool IsMatch(const TDesC& aBuf) const; williamr@2: IMPORT_C TBool IsMatchC(const TDesC& aBuf) const; williamr@2: IMPORT_C TBool IsMatchF(const TDesC& aBuf) const; williamr@2: IMPORT_C TInt FirstMatchingIndex(TInt& aResult,const MDesCArray& aDesArray,TInt aStartIndex=0) const; williamr@2: IMPORT_C TInt FirstMatchingIndexC(TInt& aResult,const MDesCArray& aDesArray,TInt aStartIndex=0) const; williamr@2: IMPORT_C TInt FirstMatchingIndexF(TInt& aResult,const MDesCArray& aDesArray,TInt aStartIndex=0) const; williamr@2: private: williamr@2: typedef TInt (*TCompareFunc)(const TText*,TInt,const TText*,TInt); williamr@2: TBool DoIsMatch(const TDesC& aQuery,TCompareFunc aCompare) const; williamr@2: TInt DoFirstMatchingIndex(TInt& aIndex,const MDesCArray& aDesArray,TCompareFunc aCompare) const; williamr@2: void DoSetBestMatch(const TDesC& aBuf,TCompareFunc aCompare); williamr@2: }; williamr@2: williamr@2: williamr@2: class RIncrMatcherPtr : public RIncrMatcherBase williamr@2: /** Incrementally matches text against a descriptor, accessed via a pointer. williamr@2: williamr@2: The class does not take a copy of the text to match against, but only stores williamr@2: the pointer. williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: { williamr@2: public: williamr@2: IMPORT_C RIncrMatcherPtr(); williamr@2: IMPORT_C RIncrMatcherPtr(TDes& aDes); williamr@2: IMPORT_C virtual ~RIncrMatcherPtr(); williamr@2: public: williamr@2: inline void SetMatcherPtr(TDes& aDes); williamr@2: protected: williamr@2: IMPORT_C TDes& MatchDes(); williamr@2: IMPORT_C const TDes& MatchDes() const; williamr@2: private: williamr@2: TDes* iDesPtr; williamr@2: }; williamr@2: williamr@2: williamr@2: class RIncrMatcherTextBuf : public RIncrMatcherBase williamr@2: /** Incrementally matches text against a text buffer with variable maximum williamr@2: length. williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: { williamr@2: public: williamr@2: IMPORT_C RIncrMatcherTextBuf(); williamr@2: IMPORT_C virtual ~RIncrMatcherTextBuf(); williamr@2: inline void SetMatcherLengthL(TInt aMaxLength); williamr@2: protected: williamr@2: IMPORT_C TDes& MatchDes(); williamr@2: IMPORT_C const TDes& MatchDes() const; williamr@2: private: williamr@2: RTextBuf iTextBuf; williamr@2: }; williamr@2: williamr@2: template williamr@2: class RIncrMatcherBuf : public RIncrMatcherBase williamr@2: /** williamr@2: Incrementally matches text against a modifiable descriptor buffer. williamr@2: Set aMaximumSize to be the maximum size of this buffer. williamr@2: @publishedAll williamr@2: @released williamr@2: */ williamr@2: { williamr@2: public: williamr@2: /** Default constructor. */ williamr@2: RIncrMatcherBuf() {} williamr@2: protected: williamr@2: williamr@2: /** Gets the match text. williamr@2: williamr@2: @return Match text (non const) */ williamr@2: inline TDes& MatchDes() {return(iMatchBuf);} williamr@2: williamr@2: /** Gets the match text. williamr@2: williamr@2: @return Match text (const)*/ williamr@2: inline const TDes& MatchDes() const {return(iMatchBuf);} williamr@2: williamr@2: /** Copy constructor. williamr@2: williamr@2: @param aMatcher RIncrMatcherBuf to copy */ williamr@2: inline RIncrMatcherBuf(const RIncrMatcherBuf& aMatcher) {iMatchBuf=aMatcher.iMatchBuf;} williamr@2: private: williamr@2: TBuf iMatchBuf; williamr@2: }; williamr@2: williamr@2: inline TPtr& RTextBuf::Text() williamr@2: /** Gets the text as a descriptor. williamr@2: williamr@2: @return Match text descriptor (non const) */ williamr@2: {return(iPtr);} williamr@2: williamr@2: inline const TPtr& RTextBuf::Text() const williamr@2: /** Gets the text as a descriptor. williamr@2: williamr@2: @return Match text descriptor (const) */ williamr@2: {return(iPtr);} williamr@2: williamr@2: inline TInt RTextBuf::MaxLength() const williamr@2: /** Gets the maximum length of the text. williamr@2: williamr@2: @return The maximum length of the text.*/ williamr@2: {return(iPtr.MaxLength());} williamr@2: williamr@2: inline void RTextBuf::SetText(const TDesC &aDes) williamr@2: /** Sets the text. You must have set the maximum length appropriately first with williamr@2: SetMaxLengthL(). */ williamr@2: {iPtr.Copy(aDes);} williamr@2: williamr@2: inline TInt RIncrMatcherBase::MaxLength() const williamr@2: /** Gets the maximum length of the match text buffer. */ williamr@2: {return(MatchDes().MaxLength());} williamr@2: inline TInt RIncrMatcherBase::MatchLength() const williamr@2: /** Gets the current length of the match text buffer. */ williamr@2: {return(MatchDes().Length());} williamr@2: inline TPtrC RIncrMatcherBase::MatchText() const williamr@2: /** Gets the match text as a TPtrC. */ williamr@2: {return(TPtrC(MatchDes()));} williamr@2: inline void RIncrMatcherBase::SetMatchText(const TDesC& aText) williamr@2: /** Sets the match text. williamr@2: williamr@2: @param aText String to which to set the match text. */ williamr@2: {MatchDes()=aText;} williamr@2: williamr@2: inline void RIncrMatcherBase::Clear() williamr@2: /** Clears the match text. */ williamr@2: {MatchDes().Zero();} williamr@2: williamr@2: inline void RIncrMatcherPtr::SetMatcherPtr(TDes& aDes) williamr@2: /** Sets the text to be matched against. williamr@2: williamr@2: @param aDes Text to be matched against */ williamr@2: {iDesPtr=&aDes;} williamr@2: williamr@2: williamr@2: inline void RIncrMatcherTextBuf::SetMatcherLengthL(TInt aMaxLength) williamr@2: /** Sets the maximum length of text that can be stored through SetMatchText() williamr@2: etc. williamr@2: williamr@2: @param aMaxLength Maximum length of text that can be stored */ williamr@2: {iTextBuf.SetMaxLengthL(aMaxLength);} williamr@2: williamr@2: williamr@2: #endif