diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2010-01-07 00:24:08 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2010-01-07 00:24:08 +0000 |
| commit | 17c92cf5b2b0dfdfdbbdb5c41354634ca98ae1b4 (patch) | |
| tree | 7109629e83314cf73be9233b6a3a3c70031eff94 | |
| parent | 2dd476ff6dc904174446f269eeb97ccf9ed8c379 (diff) | |
| download | libbu++-17c92cf5b2b0dfdfdbbdb5c41354634ca98ae1b4.tar.gz libbu++-17c92cf5b2b0dfdfdbbdb5c41354634ca98ae1b4.tar.bz2 libbu++-17c92cf5b2b0dfdfdbbdb5c41354634ca98ae1b4.tar.xz libbu++-17c92cf5b2b0dfdfdbbdb5c41354634ca98ae1b4.zip | |
Added a new class, RegEx, it does extended regular expressions for now, more to
come.
Diffstat (limited to '')
| -rw-r--r-- | src/regex.cpp | 88 | ||||
| -rw-r--r-- | src/regex.h | 37 | ||||
| -rw-r--r-- | src/tests/regex.cpp | 31 |
3 files changed, 156 insertions, 0 deletions
diff --git a/src/regex.cpp b/src/regex.cpp new file mode 100644 index 0000000..a9e09bb --- /dev/null +++ b/src/regex.cpp | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | #include "bu/regex.h" | ||
| 2 | |||
| 3 | #include <regex.h> // Please, please include the system regex.h file | ||
| 4 | |||
| 5 | #define re ((regex_t *)pRegEx) | ||
| 6 | #define aSubStr ((regmatch_t *)paSubStr) | ||
| 7 | |||
| 8 | Bu::RegEx::RegEx() : | ||
| 9 | pRegEx( NULL ), | ||
| 10 | bCompiled( false ), | ||
| 11 | paSubStr( NULL ) | ||
| 12 | { | ||
| 13 | } | ||
| 14 | |||
| 15 | Bu::RegEx::RegEx( const Bu::FString &sSrc ) : | ||
| 16 | pRegEx( NULL ), | ||
| 17 | bCompiled( false ), | ||
| 18 | paSubStr( NULL ) | ||
| 19 | { | ||
| 20 | compile( sSrc ); | ||
| 21 | } | ||
| 22 | |||
| 23 | Bu::RegEx::~RegEx() | ||
| 24 | { | ||
| 25 | if( bCompiled ) | ||
| 26 | { | ||
| 27 | regfree( re ); | ||
| 28 | delete re; | ||
| 29 | delete[] aSubStr; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | void Bu::RegEx::compile( const Bu::FString &sSrc ) | ||
| 34 | { | ||
| 35 | if( bCompiled ) | ||
| 36 | { | ||
| 37 | regfree( re ); | ||
| 38 | delete re; | ||
| 39 | delete[] aSubStr; | ||
| 40 | bCompiled = false; | ||
| 41 | } | ||
| 42 | pRegEx = (void *)(new regex_t); | ||
| 43 | |||
| 44 | int nErr = regcomp( re, sSrc.getStr(), REG_EXTENDED|REG_NEWLINE ); | ||
| 45 | if( nErr ) | ||
| 46 | { | ||
| 47 | size_t length = regerror( nErr, re, NULL, 0 ); | ||
| 48 | char *buffer = new char[length]; | ||
| 49 | (void) regerror( nErr, re, buffer, length ); | ||
| 50 | Bu::FString s( buffer ); | ||
| 51 | delete[] buffer; | ||
| 52 | throw "???"; // BuildException( s.getStr() ); | ||
| 53 | } | ||
| 54 | bCompiled = true; | ||
| 55 | this->sSrc = sSrc; | ||
| 56 | |||
| 57 | nSubStr = re->re_nsub+1; | ||
| 58 | paSubStr = (void *)(new regmatch_t[nSubStr]); | ||
| 59 | } | ||
| 60 | |||
| 61 | int Bu::RegEx::getNumSubStrings() | ||
| 62 | { | ||
| 63 | return nSubStr; | ||
| 64 | } | ||
| 65 | |||
| 66 | bool Bu::RegEx::execute( const Bu::FString &sSrc ) | ||
| 67 | { | ||
| 68 | sTest = sSrc; | ||
| 69 | if( regexec( re, sSrc.getStr(), nSubStr, aSubStr, 0 ) ) | ||
| 70 | return false; | ||
| 71 | return true; | ||
| 72 | } | ||
| 73 | |||
| 74 | void Bu::RegEx::getSubStringRange( int nIndex, int &iStart, int &iEnd ) | ||
| 75 | { | ||
| 76 | iStart = aSubStr[nIndex].rm_so; | ||
| 77 | iEnd = aSubStr[nIndex].rm_eo; | ||
| 78 | } | ||
| 79 | |||
| 80 | Bu::FString Bu::RegEx::getSubString( int nIndex ) | ||
| 81 | { | ||
| 82 | regmatch_t *Subs = aSubStr; | ||
| 83 | return Bu::FString( | ||
| 84 | sTest.getStr()+aSubStr[nIndex].rm_so, | ||
| 85 | aSubStr[nIndex].rm_eo - aSubStr[nIndex].rm_so | ||
| 86 | ); | ||
| 87 | } | ||
| 88 | |||
diff --git a/src/regex.h b/src/regex.h new file mode 100644 index 0000000..11ff9f0 --- /dev/null +++ b/src/regex.h | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | #ifndef BU_REG_EX_H | ||
| 2 | #define BU_REG_EX_H | ||
| 3 | |||
| 4 | #include "bu/fstring.h" | ||
| 5 | |||
| 6 | #include <stdint.h> | ||
| 7 | |||
| 8 | namespace Bu | ||
| 9 | { | ||
| 10 | class RegEx | ||
| 11 | { | ||
| 12 | public: | ||
| 13 | RegEx(); | ||
| 14 | RegEx( const Bu::FString &sSrc ); | ||
| 15 | virtual ~RegEx(); | ||
| 16 | |||
| 17 | void compile( const Bu::FString &sSrc ); | ||
| 18 | int getNumSubStrings(); | ||
| 19 | bool execute( const Bu::FString &sSrc ); | ||
| 20 | void getSubStringRange( int nIndex, int &iStart, int &iEnd ); | ||
| 21 | Bu::FString getSubString( int nIndex ); | ||
| 22 | const Bu::FString &getSource() | ||
| 23 | { | ||
| 24 | return sSrc; | ||
| 25 | } | ||
| 26 | |||
| 27 | private: | ||
| 28 | Bu::FString sSrc; | ||
| 29 | Bu::FString sTest; | ||
| 30 | void *pRegEx; | ||
| 31 | bool bCompiled; | ||
| 32 | int nSubStr; | ||
| 33 | void *paSubStr; | ||
| 34 | }; | ||
| 35 | }; | ||
| 36 | |||
| 37 | #endif | ||
diff --git a/src/tests/regex.cpp b/src/tests/regex.cpp new file mode 100644 index 0000000..eb3aff6 --- /dev/null +++ b/src/tests/regex.cpp | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | #include <bu/regex.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | |||
| 4 | int main( int argc, char *argv[] ) | ||
| 5 | { | ||
| 6 | if( argc < 3 ) | ||
| 7 | { | ||
| 8 | printf("No... %s <regex> <string>\n\n", argv[0] ); | ||
| 9 | return 0; | ||
| 10 | } | ||
| 11 | |||
| 12 | Bu::RegEx re( argv[1] ); | ||
| 13 | |||
| 14 | printf("Regex: %s\n", argv[1] ); | ||
| 15 | printf("Match: %s\n", argv[2] ); | ||
| 16 | |||
| 17 | if( re.execute( argv[2] ) ) | ||
| 18 | { | ||
| 19 | for( int j = 0; j < re.getNumSubStrings(); j++ ) | ||
| 20 | { | ||
| 21 | printf("SubStr %d: %s\n", j, re.getSubString( j ).getStr() ); | ||
| 22 | } | ||
| 23 | } | ||
| 24 | else | ||
| 25 | { | ||
| 26 | printf("Regex did not match.\n"); | ||
| 27 | } | ||
| 28 | |||
| 29 | return 0; | ||
| 30 | } | ||
| 31 | |||
