aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/regex.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
committerMike Buland <eichlan@xagasoft.com>2012-03-25 20:00:08 +0000
commit469bbcf0701e1eb8a6670c23145b0da87357e178 (patch)
treeb5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/experimental/regex.cpp
parentee1b79396076edc4e30aefb285fada03bb45e80d (diff)
downloadlibbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz
libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/experimental/regex.cpp')
-rw-r--r--src/experimental/regex.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/experimental/regex.cpp b/src/experimental/regex.cpp
new file mode 100644
index 0000000..af0d364
--- /dev/null
+++ b/src/experimental/regex.cpp
@@ -0,0 +1,95 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/regex.h"
9
10#include <regex.h> // Please, please include the system regex.h file
11
12#define re ((regex_t *)pRegEx)
13#define aSubStr ((regmatch_t *)paSubStr)
14
15Bu::RegEx::RegEx() :
16 pRegEx( NULL ),
17 bCompiled( false ),
18 paSubStr( NULL )
19{
20}
21
22Bu::RegEx::RegEx( const Bu::String &sSrc ) :
23 pRegEx( NULL ),
24 bCompiled( false ),
25 paSubStr( NULL )
26{
27 compile( sSrc );
28}
29
30Bu::RegEx::~RegEx()
31{
32 if( bCompiled )
33 {
34 regfree( re );
35 delete re;
36 delete[] aSubStr;
37 }
38}
39
40void Bu::RegEx::compile( const Bu::String &sSrc )
41{
42 if( bCompiled )
43 {
44 regfree( re );
45 delete re;
46 delete[] aSubStr;
47 bCompiled = false;
48 }
49 pRegEx = (void *)(new regex_t);
50
51 int nErr = regcomp( re, sSrc.getStr(), REG_EXTENDED|REG_NEWLINE );
52 if( nErr )
53 {
54 size_t length = regerror( nErr, re, NULL, 0 );
55 char *buffer = new char[length];
56 (void) regerror( nErr, re, buffer, length );
57 Bu::String s( buffer );
58 delete[] buffer;
59 throw "???"; // BuildException( s.getStr() );
60 }
61 bCompiled = true;
62 this->sSrc = sSrc;
63
64 nSubStr = re->re_nsub+1;
65 paSubStr = (void *)(new regmatch_t[nSubStr]);
66}
67
68int Bu::RegEx::getNumSubStrings()
69{
70 return nSubStr;
71}
72
73bool Bu::RegEx::execute( const Bu::String &sSrc )
74{
75 sTest = sSrc;
76 if( regexec( re, sSrc.getStr(), nSubStr, aSubStr, 0 ) )
77 return false;
78 return true;
79}
80
81void Bu::RegEx::getSubStringRange( int nIndex, int &iStart, int &iEnd )
82{
83 iStart = aSubStr[nIndex].rm_so;
84 iEnd = aSubStr[nIndex].rm_eo;
85}
86
87Bu::String Bu::RegEx::getSubString( int nIndex )
88{
89// regmatch_t *Subs = aSubStr;
90 return Bu::String(
91 sTest.getStr()+aSubStr[nIndex].rm_so,
92 aSubStr[nIndex].rm_eo - aSubStr[nIndex].rm_so
93 );
94}
95