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 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/regex.cpp (limited to 'src/regex.cpp') 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 + ); +} + -- cgit v1.2.3