epoc32/include/tuladdressstringtokenizer.h
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
permissions -rw-r--r--
Final list of Symbian^2 public API header files
     1 /*
     2 * Copyright (c) 2006-2009 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 the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 *
    16 */
    17 
    18 
    19 
    20 #ifndef __TULADDRESSSTRINGTOKENIZER_H__
    21 #define __TULADDRESSSTRINGTOKENIZER_H__
    22 
    23 #include <e32base.h>
    24 
    25 /**
    26 Address String Tokenizer API offers methods for parsing phone numbers and e-mail, 
    27 URL and URI addresses from the given text. The API consists of the 
    28 CTulAddressStringTokenizer class.
    29 
    30 Usage:
    31 
    32 @code
    33  #include <tuladdressstringtokenizer.h>
    34 
    35  // SFoundItem instance
    36  CTulAddressStringTokenizer::SFoundItem item;
    37 
    38  // Some text
    39  TBufC<256> strSomeText(_L("Mail to me@someplace.com or call 040 1234567. 
    40  You can also tune in to audio feed at rtsp://someplace.com/somefeed.ra."));
    41 	
    42  // First the user has to create an instance of CTulAddressStringTokenizer by using the
    43  // factory method NewL(). The method takes two parameters. The first 
    44  // parameter defines the text to be searched from and the second parameter 
    45  // tells what exactly is being looked for.
    46  CTulAddressStringTokenizer singleSearch = CTulAddressStringTokenizer::NewL(strSomeText, 
    47                   CTulAddressStringTokenizer::EFindItemSearchMailAddressBin);
    48 
    49  // The passed text is parsed in construction, and found items can be fetched 
    50  // by using the ItemArray() method. It returns a constant array containing 
    51  // all the found items. The interface also offers helper functions for 
    52  // handling the item array by itself. 
    53 
    54  // Get count of found items.
    55  TInt count(singleSearch->ItemCount());
    56 
    57  // Get currently selected item (me@someplace.com) to the result1 variable.
    58  singleSearch->Item(item);
    59  TPtrC16 result1(strSomeText.Mid(item.iStartPos, item.iLength));
    60 
    61  // Deallocate memory
    62  delete singleSearch;
    63 
    64  // Create an instance of CTulAddressStringTokenizer and look for all possible 
    65  // things (cases work as binary mask).
    66  CTulAddressStringTokenizer* multiSearch = CTulAddressStringTokenizer::NewL(strSomeText,
    67                   (CTulAddressStringTokenizer::EFindItemSearchPhoneNumberBin |           
    68                   CTulAddressStringTokenizer::EFindItemSearchURLBin | 
    69                   CTulAddressStringTokenizer::EFindItemSearchMailAddressBin | 
    70                   CTulAddressStringTokenizer::EFindItemSearchScheme));
    71 
    72  // Get count of found items.
    73  TInt count2(multiSearch->ItemCount());
    74 
    75  // Get currently selected item to the result2 variable.
    76  multiSearch->Item(item);
    77 
    78  // Debug print all items and their type.
    79  for( TInt i=0; i<count2; i++)
    80      {
    81      TPtrC16 result2(strSomeText.Mid(item.iStartPos, item.iLength));
    82      RDebug::Print(_L("Found type %d item:"), item.iItemType);
    83      RDebug::Print(_L("%S"), &result2);
    84      multiSearch->NextItem(item);
    85      }
    86 
    87  // Deallocate memory
    88  delete multiSearch;
    89 @endcode
    90 
    91 @publishedAll
    92 @released
    93 */
    94 
    95 class CTulAddressStringTokenizer : public CBase
    96     {
    97 public:
    98 #define TFindItemSearchCase TTokenizerSearchCase	// For source compatibility with S60 only
    99     /**
   100     Enumeration to define the search case. 
   101     Multiple enumerations can be used as binary mask.
   102     */
   103     enum TTokenizerSearchCase
   104         {
   105         // Searches phone numbers.
   106 		EFindItemSearchPhoneNumberBin = 4, 
   107         // Searches mail addresses.
   108         EFindItemSearchMailAddressBin = 8,
   109         // Searches fixed start URLs ("http://", "https://", "rtsp://"), "www.", "wap." and IPv4 addresses.
   110         EFindItemSearchURLBin  = 16,
   111         // Searches for all URIs containing a scheme.
   112         EFindItemSearchScheme  = 32
   113         };
   114 
   115     // Struct to contain a found item.
   116     struct SFoundItem
   117         {
   118         TInt iStartPos;	// Start position of the found item.
   119         TInt iLength;	// Length of the found item (characters).
   120         TTokenizerSearchCase iItemType;		// Search case of the found item
   121 		};
   122 
   123 public:  // Constructors and destructor
   124     IMPORT_C static CTulAddressStringTokenizer* NewL( const TDesC& aText, TInt aSearchCases );
   125     IMPORT_C static CTulAddressStringTokenizer* NewL( const TDesC& aText,  TInt aSearchCases, TInt aMinNumbers );
   126     IMPORT_C ~CTulAddressStringTokenizer();
   127 public:
   128 	IMPORT_C TInt ItemCount() const;
   129     IMPORT_C TBool Item( SFoundItem& aItem ) const;
   130     IMPORT_C TBool NextItem( SFoundItem& aItem );
   131     IMPORT_C TBool PrevItem( SFoundItem& aItem );
   132     IMPORT_C const CArrayFixFlat<SFoundItem>* ItemArray() const;
   133 	IMPORT_C TInt Position() const;	
   134     IMPORT_C void ResetPosition();
   135     IMPORT_C TInt DoNewSearchL( const TDesC& aText, TInt aSearchCases);
   136     IMPORT_C TInt DoNewSearchL( const TDesC& aText, TInt aSearchCases,  TInt aMinNumbers );
   137 private:
   138     CTulAddressStringTokenizer();
   139     void AddItemL( TInt aStartPos, TInt aLength, TTokenizerSearchCase aType );
   140 
   141     TBool SearchPhoneNumberL( const TDesC& aText );
   142     TBool SearchMailAddressL( const TDesC& aText );  
   143     TBool SearchGenericUriL( const TDesC& aText );
   144     TBool SearchUrlL( const TDesC& aText, TBool aFindFixedSchemas );
   145     TBool ParseUrlL( const TDesC& aType, const TPtrC& aTokenPtr, TInt aTextOffset );
   146 
   147     static TBool IsValidEmailChar(const TChar& charac); // Login part of the e-mail address
   148     static TBool IsValidEmailHostChar(const TChar& charac); // Host part of the e-mail address
   149     static TBool IsValidPhoneNumberChar(const TChar& charac); // Phone number
   150     static TBool IsValidUrlChar( const TChar& charac); // URL
   151 
   152     void ConstructL( const TDesC& aText, TInt aSearchCases, TInt aMinNumbers );
   153 	void PerformSearchL( const TDesC& aText, TInt aSearchCases );
   154     
   155     CTulAddressStringTokenizer( const CTulAddressStringTokenizer& );	// Prohibit copy constructor
   156     CTulAddressStringTokenizer& operator= ( const CTulAddressStringTokenizer& );  // Prohibit assigment operator
   157 private:
   158     CArrayFixFlat<SFoundItem>* iFoundItems;		// Array of all found items.
   159     TInt iPosition;		// Engine's position in the iFoundItems.
   160     TInt iMinNumbers;	// Minimum count of numbers in a phone number
   161     };
   162 
   163 
   164 #endif      // __TULADDRESSSTRINGTOKENIZER_H__
   165