From 17c92cf5b2b0dfdfdbbdb5c41354634ca98ae1b4 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 7 Jan 2010 00:24:08 +0000 Subject: Added a new class, RegEx, it does extended regular expressions for now, more to come. --- src/regex.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/regex.h | 37 ++++++++++++++++++++++ src/tests/regex.cpp | 31 +++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 src/regex.cpp create mode 100644 src/regex.h create mode 100644 src/tests/regex.cpp (limited to 'src') 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 @@ +#include "bu/regex.h" + +#include // Please, please include the system regex.h file + +#define re ((regex_t *)pRegEx) +#define aSubStr ((regmatch_t *)paSubStr) + +Bu::RegEx::RegEx() : + pRegEx( NULL ), + bCompiled( false ), + paSubStr( NULL ) +{ +} + +Bu::RegEx::RegEx( const Bu::FString &sSrc ) : + pRegEx( NULL ), + bCompiled( false ), + paSubStr( NULL ) +{ + compile( sSrc ); +} + +Bu::RegEx::~RegEx() +{ + if( bCompiled ) + { + regfree( re ); + delete re; + delete[] aSubStr; + } +} + +void Bu::RegEx::compile( const Bu::FString &sSrc ) +{ + if( bCompiled ) + { + regfree( re ); + delete re; + delete[] aSubStr; + bCompiled = false; + } + pRegEx = (void *)(new regex_t); + + int nErr = regcomp( re, sSrc.getStr(), REG_EXTENDED|REG_NEWLINE ); + if( nErr ) + { + size_t length = regerror( nErr, re, NULL, 0 ); + char *buffer = new char[length]; + (void) regerror( nErr, re, buffer, length ); + Bu::FString s( buffer ); + delete[] buffer; + throw "???"; // BuildException( s.getStr() ); + } + bCompiled = true; + this->sSrc = sSrc; + + nSubStr = re->re_nsub+1; + paSubStr = (void *)(new regmatch_t[nSubStr]); +} + +int Bu::RegEx::getNumSubStrings() +{ + return nSubStr; +} + +bool Bu::RegEx::execute( const Bu::FString &sSrc ) +{ + sTest = sSrc; + if( regexec( re, sSrc.getStr(), nSubStr, aSubStr, 0 ) ) + return false; + return true; +} + +void Bu::RegEx::getSubStringRange( int nIndex, int &iStart, int &iEnd ) +{ + iStart = aSubStr[nIndex].rm_so; + iEnd = aSubStr[nIndex].rm_eo; +} + +Bu::FString Bu::RegEx::getSubString( int nIndex ) +{ + regmatch_t *Subs = aSubStr; + return Bu::FString( + sTest.getStr()+aSubStr[nIndex].rm_so, + aSubStr[nIndex].rm_eo - aSubStr[nIndex].rm_so + ); +} + 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 @@ +#ifndef BU_REG_EX_H +#define BU_REG_EX_H + +#include "bu/fstring.h" + +#include + +namespace Bu +{ + class RegEx + { + public: + RegEx(); + RegEx( const Bu::FString &sSrc ); + virtual ~RegEx(); + + void compile( const Bu::FString &sSrc ); + int getNumSubStrings(); + bool execute( const Bu::FString &sSrc ); + void getSubStringRange( int nIndex, int &iStart, int &iEnd ); + Bu::FString getSubString( int nIndex ); + const Bu::FString &getSource() + { + return sSrc; + } + + private: + Bu::FString sSrc; + Bu::FString sTest; + void *pRegEx; + bool bCompiled; + int nSubStr; + void *paSubStr; + }; +}; + +#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 @@ +#include +#include + +int main( int argc, char *argv[] ) +{ + if( argc < 3 ) + { + printf("No... %s \n\n", argv[0] ); + return 0; + } + + Bu::RegEx re( argv[1] ); + + printf("Regex: %s\n", argv[1] ); + printf("Match: %s\n", argv[2] ); + + if( re.execute( argv[2] ) ) + { + for( int j = 0; j < re.getNumSubStrings(); j++ ) + { + printf("SubStr %d: %s\n", j, re.getSubString( j ).getStr() ); + } + } + else + { + printf("Regex did not match.\n"); + } + + return 0; +} + -- cgit v1.2.3