aboutsummaryrefslogtreecommitdiff
path: root/src/regex.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/regex.cpp')
-rw-r--r--src/regex.cpp88
1 files changed, 88 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
8Bu::RegEx::RegEx() :
9 pRegEx( NULL ),
10 bCompiled( false ),
11 paSubStr( NULL )
12{
13}
14
15Bu::RegEx::RegEx( const Bu::FString &sSrc ) :
16 pRegEx( NULL ),
17 bCompiled( false ),
18 paSubStr( NULL )
19{
20 compile( sSrc );
21}
22
23Bu::RegEx::~RegEx()
24{
25 if( bCompiled )
26 {
27 regfree( re );
28 delete re;
29 delete[] aSubStr;
30 }
31}
32
33void 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
61int Bu::RegEx::getNumSubStrings()
62{
63 return nSubStr;
64}
65
66bool 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
74void Bu::RegEx::getSubStringRange( int nIndex, int &iStart, int &iEnd )
75{
76 iStart = aSubStr[nIndex].rm_so;
77 iEnd = aSubStr[nIndex].rm_eo;
78}
79
80Bu::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