2004-03-13 22:06:53 +08:00
|
|
|
/*
|
|
|
|
* ZoneMinder regular expression class implementation, $Date$, $Revision$
|
2006-01-17 18:56:30 +08:00
|
|
|
* Copyright (C) 2003, 2004, 2005, 2006 Philip Coombes
|
2004-03-13 22:06:53 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "zm.h"
|
|
|
|
#include "zm_regexp.h"
|
|
|
|
|
|
|
|
#if HAVE_LIBPCRE
|
|
|
|
|
2004-03-15 02:12:53 +08:00
|
|
|
RegExpr::RegExpr( const char *pattern, int flags, int p_max_matches ) : max_matches( p_max_matches ), match_buffers( 0 ), match_lengths( 0 ), match_valid( 0 )
|
2004-03-13 22:06:53 +08:00
|
|
|
{
|
|
|
|
const char *errstr;
|
|
|
|
int erroffset = 0;
|
|
|
|
if ( !(regex = pcre_compile( pattern, flags, &errstr, &erroffset, 0 )) )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Fatal( "pcre_compile(%s): %s at %d", pattern, errstr, erroffset );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
regextra = pcre_study( regex, 0, &errstr );
|
|
|
|
if ( errstr )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Fatal( "pcre_study(%s): %s", pattern, errstr );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ok = (bool)regex )
|
|
|
|
{
|
|
|
|
match_vectors = new int[3*max_matches];
|
2004-03-15 02:12:53 +08:00
|
|
|
memset( match_vectors, 0, sizeof(*match_vectors)*3*max_matches );
|
2004-03-13 22:06:53 +08:00
|
|
|
match_buffers = new char *[max_matches];
|
|
|
|
memset( match_buffers, 0, sizeof(*match_buffers)*max_matches );
|
2004-03-15 02:12:53 +08:00
|
|
|
match_lengths = new int[max_matches];
|
|
|
|
memset( match_lengths, 0, sizeof(*match_lengths)*max_matches );
|
|
|
|
match_valid = new bool[max_matches];
|
|
|
|
memset( match_valid, 0, sizeof(*match_valid)*max_matches );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
n_matches = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegExpr::~RegExpr()
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < max_matches; i++ )
|
|
|
|
{
|
|
|
|
if ( match_buffers[i] )
|
|
|
|
{
|
|
|
|
delete[] match_buffers[i];
|
|
|
|
}
|
|
|
|
}
|
2004-03-15 02:12:53 +08:00
|
|
|
delete[] match_valid;
|
|
|
|
delete[] match_lengths;
|
2004-03-13 22:06:53 +08:00
|
|
|
delete[] match_buffers;
|
|
|
|
delete[] match_vectors;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RegExpr::Match( const char *subject_string, int subject_length, int flags )
|
|
|
|
{
|
|
|
|
match_string = subject_string;
|
|
|
|
|
|
|
|
n_matches = pcre_exec( regex, regextra, subject_string, subject_length, 0, flags, match_vectors, 2*max_matches );
|
|
|
|
|
2004-03-15 02:12:53 +08:00
|
|
|
if ( n_matches <= 0 )
|
2004-03-13 22:06:53 +08:00
|
|
|
{
|
|
|
|
if ( n_matches < PCRE_ERROR_NOMATCH )
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "Error %d executing regular expression", n_matches );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
return( n_matches = 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( int i = 0; i < max_matches; i++ )
|
|
|
|
{
|
2004-03-15 02:12:53 +08:00
|
|
|
match_valid[i] = false;
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
return( n_matches );
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *RegExpr::MatchString( int match_index ) const
|
|
|
|
{
|
|
|
|
if ( match_index > n_matches )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
2004-03-15 02:12:53 +08:00
|
|
|
if ( !match_valid[match_index] )
|
2004-03-13 22:06:53 +08:00
|
|
|
{
|
|
|
|
int match_len = match_vectors[(2*match_index)+1]-match_vectors[2*match_index];
|
2004-03-15 02:12:53 +08:00
|
|
|
if ( match_lengths[match_index] < (match_len+1) )
|
|
|
|
{
|
|
|
|
delete[] match_buffers[match_index];
|
|
|
|
match_buffers[match_index] = new char[match_len+1];
|
|
|
|
match_lengths[match_index] = match_len+1;
|
|
|
|
}
|
|
|
|
memcpy( match_buffers[match_index], match_string+match_vectors[2*match_index], match_len );
|
|
|
|
match_buffers[match_index][match_len] = '\0';
|
|
|
|
match_valid[match_index] = true;
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
return( match_buffers[match_index] );
|
|
|
|
}
|
|
|
|
|
|
|
|
int RegExpr::MatchLength( int match_index ) const
|
|
|
|
{
|
|
|
|
if ( match_index > n_matches )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
return( match_vectors[(2*match_index)+1]-match_vectors[2*match_index] );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAVE_LIBPCRE
|