Loading...
Searching...
No Matches
Regexp.cpp
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Regexp.cpp
//------------------------------------------------------------------------------
// Copyright (C) 1997-2003 Vladislav Grinchenko <vlg@users.sourceforge.net>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#include <assa/Regexp.h>
using namespace ASSA;
Regexp::
Regexp (const std::string& pattern_)
:
m_pattern (NULL),
m_error_msg (new char [256]),
m_compiled_pattern (new regex_t)
{
trace_with_mask("Regexp::Regexp", REGEXP);
m_pattern = new char [pattern_.size () + 1];
::strncpy (m_pattern, pattern_.c_str (), pattern_.size ());
m_pattern [pattern_.size ()] = '\0';
int ret = ::regcomp (m_compiled_pattern, m_pattern, REG_EXTENDED);
if (ret != 0) {
::regerror (ret, m_compiled_pattern, m_error_msg, 256);
DL((REGEXP,"regcomp(\"%s\") = %d\n", m_pattern, ret));
DL((REGEXP,"error: \"%s\"\n", m_error_msg));
delete [] m_pattern;
m_pattern = NULL;
}
}
Regexp::
~Regexp ()
{
trace_with_mask("Regexp::~Regexp", REGEXP);
if (m_pattern) {
delete [] m_pattern;
}
if (m_error_msg) {
delete [] m_error_msg;
}
::regfree (m_compiled_pattern);
delete (m_compiled_pattern);
}
int
Regexp::
match (const char* text_)
{
trace_with_mask("Regexp::match", REGEXP);
if (text_ == NULL || m_pattern == NULL) {
return -1;
}
int ret = ::regexec (m_compiled_pattern, text_, 0, NULL, 0);
if (ret != 0) {
::regerror (ret, m_compiled_pattern, m_error_msg, 256);
DL((REGEXP,"regexec(\"%s\") = %d\n", text_, ret));
DL((REGEXP,"pattern: \"%s\"\n", m_pattern));
DL((REGEXP,"error: \"%s\"\n", m_error_msg));
}
return (ret == 0 ? 0 : -1);
}
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
Regexp.h
Wrapper class for UNIX regexp (3).
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::Regexp::m_error_msg
char * m_error_msg
Definition Regexp.h:72
ASSA::Regexp::m_pattern
char * m_pattern
Definition Regexp.h:71
ASSA::Regexp::Regexp
Regexp(const std::string &pattern_)
Constructor.
Definition Regexp.cpp:17
ASSA::Regexp::m_compiled_pattern
regex_t * m_compiled_pattern
Definition Regexp.h:73
ASSA::Regexp::~Regexp
~Regexp()
Destructor.
Definition Regexp.cpp:42
ASSA::Regexp::match
int match(const char *text_)
Match an ASCII character string agains the pattern this class wraps.
Definition Regexp.cpp:58
ASSA
Definition Acceptor.h:40
ASSA::REGEXP
@ REGEXP
Class RegExp messages
Definition LogMask.h:53