Browse Source

HV: * Removed dos ^M characters (*shivers*)

git-svn-id: svn+ssh://code.jive.eu/code/svn/vbs_fs@50 5b2df0cb-e17b-44c8-90c8-480d528b6e0d
master
JIVE Software Dev 4 years ago
parent
commit
356b9a25ad
2 changed files with 232 additions and 271 deletions
  1. +119
    -155
      regular_expression.cc
  2. +113
    -116
      regular_expression.h

+ 119
- 155
regular_expression.cc View File

@@ -1,155 +1,119 @@
// Implementation of the RegularExpression class
#include <regular_expression.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using std::cerr;
using std::endl;
using std::flush;
using std::string;
DEFINE_EZEXCEPT(Regular_Expression_Exception)
// support classes
pcintregmatch_t::pcintregmatch_t() {
this->::regmatch_t::rm_so = -1;
this->::regmatch_t::rm_eo = -1;
}
pcintregmatch_t::pcintregmatch_t( const ::regmatch_t& rm ) {
this->::regmatch_t::rm_so = rm.rm_so;
this->::regmatch_t::rm_eo = rm.rm_eo;
}
pcintregmatch_t::operator bool() const {
return (rm_so!=-1 && rm_eo!=-1);
}
matchresult::matchresult()
{}
matchresult::matchresult( const matchresult::matchvec_t& mv,
const char* os ):
__matches( mv ),
__org_string( os )
{}
matchresult::matchresult( const matchresult::matchvec_t& mv,
const std::string& os ):
__matches( mv ),
__org_string( os )
{}
matchresult::operator bool() const {
return (__matches.size()!=0);
}
matchresult::matchvec_t::value_type matchresult::operator[]( unsigned int m ) const {
if( m>=__matches.size() ) {
THROW_EZEXCEPT(Regular_Expression_Exception,
"requesting group " << m << ", but only " << __matches.size() << " groups matched");
}
return __matches[m];
}
std::string matchresult::operator[]( const matchvec_t::value_type& m ) const {
if( !m ) {
THROW_EZEXCEPT(Regular_Expression_Exception,
"requesting string from invalid group");
}
return __org_string.substr(m.rm_so, (m.rm_eo-m.rm_so));
}
std::string matchresult::group( unsigned int m ) const {
return (*this)[(*this)[m]];
}
bool operator==( const char* tocheck, const Regular_Expression& against ) {
return against.matches( tocheck );
}
bool operator!=( const char* tocheck, const Regular_Expression& against ) {
return !(against.matches( tocheck ));
}
bool operator==( const string& s, const Regular_Expression& against ) {
return against.matches( s.c_str() );
}
bool operator!=( const string& s, const Regular_Expression& against ) {
return !(against.matches(s.c_str()));
}
Regular_Expression::Regular_Expression( const char* pattern_text, int flags ) :
myOriginalPattern( ((pattern_text!=0)?(strdup(pattern_text)):(strdup(""))) )
{
// Compile the pattern....
int r;
char errbuf[ 512 ];
if( (r=::regcomp(&myCompiledExpression,
myOriginalPattern,
flags))!=0 ) {
::regerror(r, &myCompiledExpression, errbuf, sizeof(errbuf));
THROW_EZEXCEPT(Regular_Expression_Exception,
"Failed to compile RegEx(" << myOriginalPattern << "): " << errbuf);
}
// After compiling the regexp, the re_nsub member informs how many
// sub expressions/match groups there were. Together with the zeroth
// group (the whole) match, we know how many there are in total
mySubexprs = new ::regmatch_t[ 1 + myCompiledExpression.re_nsub ];
}
Regular_Expression::Regular_Expression( const string& pattern_text, int flags ) :
myOriginalPattern( ((pattern_text.size()!=0)?(strdup(pattern_text.c_str())):(strdup(""))) )
{
// Compile the pattern....
int r;
char errbuf[ 512 ];
if( (r=::regcomp(&myCompiledExpression,
myOriginalPattern,
flags))!=0 ) {
::regerror(r, &myCompiledExpression, errbuf, sizeof(errbuf));
THROW_EZEXCEPT(Regular_Expression_Exception,
"Failed to compile RegEx(" << myOriginalPattern << "): " << errbuf);
}
// After compiling the regexp, the re_nsub member informs how many
// sub expressions/match groups there were. Together with the zeroth
// group (the whole) match, we know how many there are in total
mySubexprs = new ::regmatch_t[ 1 + myCompiledExpression.re_nsub ];
}
matchresult Regular_Expression::matches( const char* s ) const {
matchresult rv;
if( !s )
return rv;
if( ::regexec(&myCompiledExpression, s, (size_t)(1+myCompiledExpression.re_nsub), mySubexprs, 0)==0 ) {
// The "+1" is from the zeroth sub expression (the whole match)
rv = matchresult(matchresult::matchvec_t(mySubexprs, mySubexprs+myCompiledExpression.re_nsub+1), s);
}
return rv;
}
matchresult Regular_Expression::matches( const string& s ) const {
return this->matches( s.c_str() );
}
string Regular_Expression::pattern( void ) const {
return myOriginalPattern;
}
Regular_Expression::~Regular_Expression() {
if( myOriginalPattern )
::free( myOriginalPattern );
::regfree( &myCompiledExpression );
delete [] mySubexprs;
}
// Implementation of the RegularExpression class
#include <regular_expression.h>

#include <iostream>
#include <algorithm>

#include <cstdlib>
#include <cstring>

using std::cerr;
using std::endl;
using std::flush;
using std::string;

DEFINE_EZEXCEPT(Regular_Expression_Exception)

// support classes
pcintregmatch_t::pcintregmatch_t() {
this->::regmatch_t::rm_so = -1;
this->::regmatch_t::rm_eo = -1;
}

pcintregmatch_t::pcintregmatch_t( const ::regmatch_t& rm ) {
this->::regmatch_t::rm_so = rm.rm_so;
this->::regmatch_t::rm_eo = rm.rm_eo;
}

pcintregmatch_t::operator bool() const {
return (rm_so!=-1 && rm_eo!=-1);
}

matchresult::matchresult()
{}

matchresult::matchresult( size_t nSubExpr, std::string const& os ):
__matches( nSubExpr ), __org_string( os )
{}

matchresult::matchresult( const matchresult::matchvec_t& mv, const std::string& os ):
__matches( mv ), __org_string( os )
{}

matchresult::operator bool() const {
return (__matches.size()!=0);
}

matchresult::matchvec_t::value_type matchresult::operator[]( unsigned int m ) const {
if( m>=__matches.size() ) {
THROW_EZEXCEPT(Regular_Expression_Exception,
"requesting group " << m << ", but only " << __matches.size() << " groups matched");
}
return __matches[m];
}

std::string matchresult::operator[]( const matchvec_t::value_type& m ) const {
if( !m ) {
THROW_EZEXCEPT(Regular_Expression_Exception,
"requesting string from invalid group");
}
return __org_string.substr(m.rm_so, (m.rm_eo-m.rm_so));
}

std::string matchresult::group( unsigned int m ) const {
return (*this)[(*this)[m]];
}

bool operator==( const char* tocheck, const Regular_Expression& against ) {
return against.matches( tocheck );
}

bool operator!=( const char* tocheck, const Regular_Expression& against ) {
return !(against.matches( tocheck ));
}

bool operator==( const string& s, const Regular_Expression& against ) {
return against.matches( s.c_str() );
}

bool operator!=( const string& s, const Regular_Expression& against ) {
return !(against.matches(s.c_str()));
}


Regular_Expression::Regular_Expression( const string& pattern_text, int flags ) :
myOriginalPattern( pattern_text )
{
// Compile the pattern....
int r;
char errbuf[ 512 ];

if( (r=::regcomp(&myCompiledExpression,
myOriginalPattern.c_str(),
flags))!=0 ) {
::regerror(r, &myCompiledExpression, errbuf, sizeof(errbuf));
THROW_EZEXCEPT(Regular_Expression_Exception,
"Failed to compile RegEx(" << myOriginalPattern << "): " << errbuf);
}
}

matchresult Regular_Expression::matches( const string& s ) const {
// After compiling the regexp, the re_nsub member informs how many
// sub expressions/match groups there were. Together with the zeroth
// group (the whole) match, we know how many there are in total
const size_t nSubExpr( 1 + myCompiledExpression.re_nsub );
matchresult mr( nSubExpr, s );

if( ::regexec(&myCompiledExpression, s.c_str(), nSubExpr, &mr.__matches[0], 0)==0 )
return mr;
return matchresult();
}

string Regular_Expression::pattern( void ) const {
return myOriginalPattern;
}

Regular_Expression::~Regular_Expression() {
::regfree( &myCompiledExpression );
}


+ 113
- 116
regular_expression.h View File

@@ -1,116 +1,113 @@
//
// Wrapper class around POSIX regex, imported from pcint
//
#ifndef REGULAR_EXPRESSION_H
#define REGULAR_EXPRESSION_H
#include <ezexcept.h>
#include <sys/types.h>
#include <regex.h>
#include <string>
#include <vector>
DECLARE_EZEXCEPT(Regular_Expression_Exception)
// wrapper around regmatch_t which
// allows to give it some extra functionality
struct pcintregmatch_t:
::regmatch_t
{
// creates invalid one
pcintregmatch_t();
// init with a given regmatch
pcintregmatch_t( const ::regmatch_t& rm );
// this'un valid?
// regex(3) sais that an invalid
// regmatch has -1 for both offsets
operator bool() const;
};
// matchresult holds a vector
// of matches. Size <> 0 indicates
// a successfull match
// Size >1 implies possible matchgroups
// Not all matchgroups are required to be valid
struct matchresult {
typedef std::vector<pcintregmatch_t> matchvec_t;
// empty matchresult
matchresult();
// fully construct, a list of matches (possibly
// empty) AND a pointer to the original string
matchresult( const matchvec_t& mv, const char* os );
matchresult( const matchvec_t& mv, const std::string& os );
// implement operator bool
// which tells the truthiness
// of the matchresult
operator bool() const;
// return the m'th regmatch.
// Most likely use:
// matchresult m = rx.match(<some string>);
// if( m[1] )
// cout << "matchgroup 1=" << m.matchgroup(1) << endl;
matchvec_t::value_type operator[]( unsigned int m ) const;
// nice.. operator overloading :)
std::string operator[]( const matchvec_t::value_type& m ) const;
// snip the indicated match out of
// the original string.
// string interface, caller doesn't
// have to do mem.management, the
std::string group( unsigned int m ) const;
// our attributes
matchvec_t __matches;
std::string __org_string;
};
// A generic regex....
class Regular_Expression
{
friend bool operator==( const char* tocheck, const Regular_Expression& against );
friend bool operator!=( const char* tocheck, const Regular_Expression& against );
friend bool operator==( const std::string& s, const Regular_Expression& against );
friend bool operator!=( const std::string& s, const Regular_Expression& against );
public:
// Create from a pattern,
// note that one match for the whole pattern will be returned in matches()[0]
// so maxmatch should be at least <number of expectect matches> + 1
Regular_Expression( const char* pattern, int flags=REG_EXTENDED );
Regular_Expression( const std::string& pattern, int flags=REG_EXTENDED );
// match the string 's' against this RX
matchresult matches( const char* s ) const;
matchresult matches( const std::string& s ) const;
// Return the pattern
std::string pattern( void ) const;
// Delete all allocated stuff
~Regular_Expression();
private:
// Our private parts
char* myOriginalPattern;
regex_t myCompiledExpression;
::regmatch_t* mySubexprs;
// Prohibit these
Regular_Expression();
Regular_Expression( const Regular_Expression& );
Regular_Expression& operator=( const Regular_Expression& );
};
#endif
//
// Wrapper class around POSIX regex, imported from pcint
//
#ifndef REGULAR_EXPRESSION_H
#define REGULAR_EXPRESSION_H

#include <ezexcept.h>

#include <sys/types.h>
#include <regex.h>
#include <string>
#include <vector>

DECLARE_EZEXCEPT(Regular_Expression_Exception)

// wrapper around regmatch_t which
// allows to give it some extra functionality
struct pcintregmatch_t:
::regmatch_t
{
// creates invalid one
pcintregmatch_t();

// init with a given regmatch
pcintregmatch_t( const ::regmatch_t& rm );

// this'un valid?
// regex(3) sais that an invalid
// regmatch has -1 for both offsets
operator bool() const;

};


// matchresult holds a vector
// of matches. Size <> 0 indicates
// a successfull match
// Size >1 implies possible matchgroups
// Not all matchgroups are required to be valid
struct matchresult {
typedef std::vector<pcintregmatch_t> matchvec_t;
// empty matchresult
matchresult();

// Pre-construct so that we can let the regex engine write into this
// result
matchresult( size_t nSubExpr, std::string const& os );
// fully construct, a list of matches (possibly
// empty) AND a pointer to the original string
matchresult( const matchvec_t& mv, const std::string& os );
// implement operator bool
// which tells the truthiness
// of the matchresult
operator bool() const;

// return the m'th regmatch.
// Most likely use:
// matchresult m = rx.match(<some string>);
// if( m[1] )
// cout << "matchgroup 1=" << m.matchgroup(1) << endl;
matchvec_t::value_type operator[]( unsigned int m ) const;

// nice.. operator overloading :)
std::string operator[]( const matchvec_t::value_type& m ) const;

// snip the indicated match out of
// the original string.

// string interface, caller doesn't
// have to do mem.management, the
std::string group( unsigned int m ) const;
// our attributes
matchvec_t __matches;
std::string __org_string;
};

// A generic regex....
class Regular_Expression
{
friend bool operator==( const char* tocheck, const Regular_Expression& against );
friend bool operator!=( const char* tocheck, const Regular_Expression& against );
friend bool operator==( const std::string& s, const Regular_Expression& against );
friend bool operator!=( const std::string& s, const Regular_Expression& against );

public:
// Create from a pattern,
// note that one match for the whole pattern will be returned in matches()[0]
// so maxmatch should be at least <number of expectect matches> + 1
Regular_Expression( const std::string& pattern, int flags=REG_EXTENDED );
// match the string 's' against this RX
matchresult matches( const std::string& s ) const;

// Return the pattern
std::string pattern( void ) const;

~Regular_Expression();

private:
// Our private parts
std::string myOriginalPattern;
regex_t myCompiledExpression;

// Prohibit these
Regular_Expression();
Regular_Expression(Regular_Expression const&);
Regular_Expression const& operator=(Regular_Expression const&);
};

#endif

Loading…
Cancel
Save