1//--------This file shamelessly stolen from Jeremy Friesner's excellent MUSCLE--------- 2/* This file is Copyright 2001 Level Control Systems. See the included LICENSE.txt file for details. */ 3 4#ifndef STRINGMATCHER_H 5#define STRINGMATCHER_H 6 7#include <sys/types.h> 8#include <regex.h> 9 10class BString; 11#define PortableString BString 12 13//////////////////////////////////////////////////////////////////////////// 14// 15// NOTE: This class is based on the psStringMatcher v1.3 class 16// developed by Lars J��rgen Aas <larsa@tihlde.hist.no> for the 17// Prodigal Software File Requester. Used by permission. 18// 19//////////////////////////////////////////////////////////////////////////// 20 21 22/** A utility class for doing globbing or regular expression matching. (A thin wrapper around the C regex calls) */ 23class StringMatcher 24{ 25public: 26 /** Default Constructor. */ 27 StringMatcher(); 28 29 /** A constructor that sets the simple expression. 30 * @param matchString the wildcard pattern or regular expression to match with 31 */ 32 StringMatcher(const char * matchString); 33 34 /** Destructor */ 35 ~StringMatcher(); 36 37 /** 38 * Set a new wildcard pattern or regular expression for this StringMatcher to use in future Match() calls. 39 * @param expression The new globbing pattern or regular expression to match with. 40 * @param isSimpleFormat If you wish to use the formal regex syntax, 41 * instead of the simple syntax, set isSimpleFormat to false. 42 * @return True on success, false on error (e.g. expression wasn't parsable, or out of memory) 43 */ 44 bool SetPattern(const char * const expression, bool isSimpleFormat=true); 45 46 /** Returns true iff (string) is matched by the current expression. 47 * @param string a string to match against using our current expression. 48 * @return true iff (string) matches, false otherwise. 49 */ 50 bool Match(const char *string) const; 51 52private: 53 bool _regExpValid; 54 regex_t _regExp; 55}; 56 57// Some regular expression utility functions 58 59/** Puts a backslash in front of any char in (str) that is "special" to the regex pattern matching. 60 * @param str The string to check for special regex chars and possibly modify to escape them. 61 */ 62void EscapeRegexTokens(PortableString & str); 63 64/** Returns true iff any "special" chars are found in (str). 65 * @param str The string to check for special regex chars. 66 * @return True iff any special regex chars were found in (str). 67 */ 68bool HasRegexTokens(const char * str); 69 70/** Returns true iff (c) is a regular expression "special" char. 71 * @param c an ASCII char 72 * @return true iff (c) is a special regex char. 73 */ 74bool IsRegexToken(char c); 75 76/** Given a regular expression, makes it case insensitive by 77 * replacing every occurance of a letter with a upper-lower combo, 78 * e.g. Hello -> [Hh][Ee][Ll][Ll][Oo] 79 * @param str a string to check for letters, and possibly modify to make case-insensitive 80 * @return true iff anything was changed, false if no changes were necessary. 81 */ 82bool MakeRegexCaseInsensitive(PortableString & str); 83 84 85#endif 86