summaryrefslogtreecommitdiffstats
path: root/src/libs/zbxcommon/regexp.c
blob: 3c82d45298d08f4456288d30109373eb9ccb7034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* 
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** 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., 675 Mass Ave, Cambridge, MA 02139, USA.
**/

#include "common.h"

#if defined(_WINDOWS)
#	include "gnuregex.h"
#endif /* _WINDOWS */

static	char	*zbx_regexp(const char *string, const char *pattern, int *len, int flags)
{ 
	char	*c = NULL;

	regex_t	re;
	regmatch_t match;

	if(len) *len = 0;

	if( string && string[0] )
	{
		if ( 0 == regcomp(&re, pattern, flags) )
		{
			if( 0 == regexec(&re, string, (size_t) 1, &match, 0) )
			{ /* Matched */
				c=(char *)string+match.rm_so;
				if(len) *len = match.rm_eo - match.rm_so;
			
			}

			regfree(&re);
		}
	}
	return	c;
}

char	*zbx_regexp_match(const char *string, const char *pattern, int *len)
{
	return zbx_regexp(string, pattern, len, REG_EXTENDED | REG_NEWLINE);
}
 
char	*zbx_iregexp_match(const char *string, const char *pattern, int *len)
{
	return zbx_regexp(string, pattern, len, REG_EXTENDED | REG_ICASE | REG_NEWLINE);
}