00001
00026
00027 #ifndef CLDAQ__TREGULAREXPRESSION_HH
00028 #define CLDAQ__TREGULAREXPRESSION_HH
00029
00030 #include "Tglobals.h"
00031
00032
00052 class TRegularExpression
00053 {
00054
00055 public:
00056
00057
00058
00059
00060
00061
00062
00063
00064 enum { DEFAULT = REG_EXTENDED|REG_NEWLINE, IOPTION = DEFAULT|REG_ICASE };
00065
00066 private:
00067 Tstring thePattern;
00068 Tint theOption;
00069 Tint theNumberOfSubMatches;
00070 Tregex_t* theCompiledPattern;
00071 Tregmatch_t* theMatch;
00072 Tregmatch_t* theSubMatch;
00073
00074 public:
00075 TRegularExpression( const Tstring& pattern, Tint option = DEFAULT );
00076 TRegularExpression( Tint option = DEFAULT, const Tstring& pattern = "" );
00077 TRegularExpression( const TRegularExpression& right );
00078 ~TRegularExpression();
00079
00080 public:
00081 const TRegularExpression& operator=( const TRegularExpression& right );
00082 const TRegularExpression& operator=( const Tstring& right );
00083 Tbool operator==( const Tstring& right );
00084 Tbool operator!=( const Tstring& right );
00085
00086 public:
00087 Tvoid Compile();
00088 Tint Index( const Tstring& source, Tint pos = 0 );
00089 TintList Indexes( const Tstring& source, Tint pos = 0 );
00090 Tint Size( const Tstring& source, Tint pos = 0 );
00091 TintList Sizes( const Tstring& source, Tint pos = 0 );
00092 Tstring MatchString( const Tstring& source, Tint pos = 0 );
00093 TstringList MatchStrings( const Tstring& source, Tint pos = 0 );
00094 Tbool IsMatch( const Tstring& source, Tint pos = 0 );
00095 Tint GetNumberOfMatches( const Tstring& source, Tint pos = 0 );
00096 Tstring Substitute( const Tstring& source, const Tstring& substr = "", Tint pos = 0 );
00097 Tstring SubstituteAll( const Tstring& source, const Tstring& substr = "", Tint pos = 0 );
00098 TstringList Split( const Tstring& source, Tint pos = 0 );
00099 TstringList Split( const Tstring& pattern, const Tstring& source, Tint pos = 0 );
00100 TstringList Split( const TRegularExpression& regex, const Tstring& source, Tint pos = 0 );
00101 Tstring GetSubMatch( Tint index, const Tstring& source, Tint pos = 0 );
00102 TstringList GetSubMatch( const Tstring& source, Tint pos = 0 );
00103
00104 public:
00105 const Tstring& GetPattern() const;
00106 Tint GetOption() const;
00107 Tint GetNumberOfSubMatches() const;
00108 const Tregex_t* GetCompiledPattern() const;
00109 const Tregmatch_t* GetMatch() const;
00110 const Tregmatch_t* GetSubMatch() const;
00111 Tvoid SetPattern( const Tstring& pattern );
00112 Tvoid SetOption( Tint option = DEFAULT );
00113 Tvoid IgnoreCase( Tbool stat = Ttrue );
00114
00115 private:
00116 Tvoid free();
00117 Tint execute( const Tstring& source, Tint pos );
00118
00119 };
00120
00121 inline const Tstring& TRegularExpression::GetPattern() const
00122 {
00123 return thePattern;
00124 }
00125
00126 inline Tint TRegularExpression::GetOption() const
00127 {
00128 return theOption;
00129 }
00130
00131 inline Tint TRegularExpression::GetNumberOfSubMatches() const
00132 {
00133 return theNumberOfSubMatches;
00134 }
00135
00136 inline const Tregex_t* TRegularExpression::GetCompiledPattern() const
00137 {
00138 return theCompiledPattern;
00139 }
00140
00141 inline const Tregmatch_t* TRegularExpression::GetMatch() const
00142 {
00143 return theMatch;
00144 }
00145
00146 inline const Tregmatch_t* TRegularExpression::GetSubMatch() const
00147 {
00148 return theSubMatch;
00149 }
00150
00151 inline Tvoid TRegularExpression::SetPattern( const Tstring& pattern )
00152 {
00153 thePattern = pattern;
00154 Compile();
00155 return;
00156 }
00157
00158 inline Tvoid TRegularExpression::SetOption( Tint option )
00159 {
00160 theOption = option;
00161 Compile();
00162 return;
00163 }
00164
00165 inline Tvoid TRegularExpression::IgnoreCase( Tbool stat )
00166 {
00167 Tint option = theOption;
00168 if ( stat == Ttrue ) {
00169 option |= REG_ICASE;
00170 } else {
00171 option &= ~REG_ICASE;
00172 }
00173 SetOption( option );
00174 return;
00175 }
00176
00177 #endif