petidomo/libtext/easy_pattern_match.c
2000-12-13 13:19:03 +00:00

62 lines
1.4 KiB
C

/*
* $Source$
* $Revision$
* $Date$
*
* Copyright (C) 1996 by CyberSolutions GmbH.
* All rights reserved.
*/
#include <sys/types.h>
#include <regex.h>
#include "text.h"
/* Simple front end to regular expressions.
This is a simple front end to make the usage of regular expressions
easier. The routine tests whether the pattern provided in 'pattern'
matches the text in 'buffer' or not. Comparisons are done
case-insensitive, using the extended regular expression language.
RETURNS: If the pattern matches, TRUE is returned. Otherwise the
routine will return FALSE.
NOTE: The error handling is somewhat relaxed... well, don't use the
routine if you need error handling.
AUTHOR: Peter Simons <simons@rhein.de>
*/
bool
text_easy_pattern_match(const char * buffer, /* text buffer */
const char * pattern /* regular expression */
)
{
regex_t preg;
int rc;
/* Sanity checks. */
assert(buffer != NULL);
assert(pattern != NULL);
if (!buffer || !pattern)
return FALSE;
/* Compile the regular expression. */
rc = regcomp(&preg, pattern, REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE);
if (rc != 0)
return FALSE;
/* Match it. */
rc = regexec(&preg, buffer, 0, NULL, 0);
regfree(&preg);
if (rc == 0)
return TRUE;
else
return FALSE;
}