2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_regexp.c
|
2007-12-30 17:41:49 +01: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
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_REGEXP_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_case.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
#include "gb_array.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gbx_c_array.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
|
|
|
|
#include "gbx_regexp.h"
|
|
|
|
|
|
|
|
|
|
|
|
static REGEXP_SCAN_FUNC _scan_cb = NULL;
|
|
|
|
static CARRAY *_scan_array;
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool REGEXP_match(const char *pattern, int len_pattern, const char *string, int len_string)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
unsigned char cp;
|
|
|
|
unsigned char cs;
|
|
|
|
|
|
|
|
void _next_pattern(void)
|
|
|
|
{
|
|
|
|
cp = *pattern;
|
|
|
|
pattern++; len_pattern--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _next_string(void)
|
|
|
|
{
|
|
|
|
cs = *string;
|
|
|
|
string++; len_string--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*if (len_pattern == 0 || len_string == 0)
|
|
|
|
return FALSE;*/
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (len_pattern == 0)
|
|
|
|
return (len_string == 0);
|
|
|
|
|
|
|
|
_next_pattern();
|
|
|
|
|
|
|
|
if (cp == '*')
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (len_pattern == 0)
|
|
|
|
{
|
|
|
|
if (_scan_cb)
|
|
|
|
(*_scan_cb)(string, len_string);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = string;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (REGEXP_match(pattern, len_pattern, string, len_string))
|
|
|
|
{
|
|
|
|
if (_scan_cb)
|
|
|
|
(*_scan_cb)(p, string - p);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (len_string == 0)
|
|
|
|
return FALSE;
|
|
|
|
_next_string();
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len_string == 0)
|
|
|
|
return FALSE; /*end || (len_pattern == 0);*/
|
|
|
|
|
|
|
|
_next_string();
|
|
|
|
|
|
|
|
if (cp == '?')
|
|
|
|
continue;
|
|
|
|
|
2009-09-27 11:28:52 +02:00
|
|
|
/*if (cp == ' ')
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (cs > ' ')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
while (len_string && cs <= ' ')
|
|
|
|
_next_string();
|
|
|
|
|
|
|
|
if (cs > ' ')
|
|
|
|
{
|
|
|
|
string--;
|
|
|
|
len_string++;
|
|
|
|
}
|
|
|
|
continue;
|
2009-09-27 11:28:52 +02:00
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (cp == '[' && len_pattern > 0)
|
|
|
|
{
|
2009-07-08 21:57:50 +02:00
|
|
|
bool not = FALSE;
|
|
|
|
bool in = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
unsigned char cb = 0;
|
|
|
|
|
|
|
|
_next_pattern();
|
|
|
|
if (cp == '^')
|
|
|
|
{
|
|
|
|
not = TRUE;
|
|
|
|
_next_pattern();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cp == cs)
|
|
|
|
{
|
|
|
|
in = TRUE;
|
|
|
|
_next_pattern();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (cp == '-' && len_pattern > 1 && cb && cb != '-')
|
|
|
|
{
|
|
|
|
_next_pattern();
|
|
|
|
if (cb <= cs && cs <= cp)
|
|
|
|
{
|
|
|
|
in = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cb = 0;
|
|
|
|
}
|
|
|
|
else if (cp == cs)
|
|
|
|
{
|
|
|
|
in = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cb = cp;
|
|
|
|
|
|
|
|
_next_pattern();
|
|
|
|
if (cp == ']')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (cp == ']')
|
|
|
|
break;
|
|
|
|
if (len_pattern == 0)
|
2009-09-27 11:28:52 +02:00
|
|
|
THROW(E_REGEXP, "Missing ']'");
|
2007-12-30 17:41:49 +01:00
|
|
|
_next_pattern();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in ^ not)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-09-27 11:28:52 +02:00
|
|
|
|
|
|
|
if (cp == ' ')
|
|
|
|
{
|
|
|
|
if (cs > ' ')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
while (len_string && cs <= ' ')
|
|
|
|
_next_string();
|
|
|
|
|
|
|
|
if (cs > ' ')
|
|
|
|
{
|
|
|
|
string--;
|
|
|
|
len_string++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cp == '{' && len_pattern > 0)
|
|
|
|
{
|
|
|
|
const char *save_string;
|
|
|
|
int save_len_string;
|
|
|
|
|
|
|
|
string--; len_string++;
|
|
|
|
save_string = string;
|
|
|
|
save_len_string = len_string;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (len_pattern == 0)
|
|
|
|
THROW(E_REGEXP, "Missing '}'");
|
|
|
|
|
|
|
|
_next_pattern();
|
|
|
|
|
|
|
|
if (cp == ',' || cp == '}')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (len_string == 0)
|
|
|
|
return FALSE;
|
|
|
|
_next_string();
|
|
|
|
|
|
|
|
if (tolower(cp) != tolower(cs))
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
_next_pattern();
|
|
|
|
if (cp == '}')
|
|
|
|
return FALSE;
|
|
|
|
if (cp == ',' || len_pattern == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
string = save_string;
|
|
|
|
len_string = save_len_string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (cp != '}')
|
|
|
|
{
|
|
|
|
if (len_pattern == 0)
|
|
|
|
THROW(E_REGEXP, "Missing '}'");
|
|
|
|
_next_pattern();
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (cp == '\\')
|
|
|
|
{
|
|
|
|
if (len_pattern == 0)
|
|
|
|
THROW(E_REGEXP, "Trailing backslash");
|
|
|
|
_next_pattern();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tolower(cp) != tolower(cs))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static void add_string(const char *str, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
char **p = (char **)GB_ArrayAdd((GB_ARRAY)_scan_array);
|
|
|
|
if (len)
|
2010-06-05 01:48:53 +02:00
|
|
|
*p = STRING_new(str, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool REGEXP_scan(CARRAY *array, const char *pattern, int len_pattern, const char *string, int len_string)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
bool match;
|
|
|
|
|
|
|
|
_scan_cb = add_string;
|
|
|
|
_scan_array = array;
|
|
|
|
match = REGEXP_match(pattern, len_pattern, string, len_string);
|
|
|
|
CARRAY_reverse(array, NULL);
|
|
|
|
_scan_cb = NULL;
|
|
|
|
_scan_array = NULL;
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|