libassa  3.5.1
Regexp.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // Regexp.cpp
4 //------------------------------------------------------------------------------
5 // Copyright (C) 1997-2003 Vladislav Grinchenko <vlg@users.sourceforge.net>
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 
13 #include <assa/Regexp.h>
14 using namespace ASSA;
15 
17 Regexp (const std::string& pattern_)
18  :
19  m_pattern (NULL),
20  m_error_msg (new char [256]),
21  m_compiled_pattern (new regex_t)
22 {
23  trace_with_mask("Regexp::Regexp", REGEXP);
24 
25  m_pattern = new char [pattern_.size () + 1];
26  ::strncpy (m_pattern, pattern_.c_str (), pattern_.size ());
27  m_pattern [pattern_.size ()] = '\0';
28 
29  int ret = ::regcomp (m_compiled_pattern, m_pattern, REG_EXTENDED);
30 
31  if (ret != 0) {
32  ::regerror (ret, m_compiled_pattern, m_error_msg, 256);
33  DL((REGEXP,"regcomp(\"%s\") = %d\n", m_pattern, ret));
34  DL((REGEXP,"error: \"%s\"\n", m_error_msg));
35 
36  delete [] m_pattern;
37  m_pattern = NULL;
38  }
39 }
40 
42 ~Regexp ()
43 {
44  trace_with_mask("Regexp::~Regexp", REGEXP);
45 
46  if (m_pattern) {
47  delete [] m_pattern;
48  }
49  if (m_error_msg) {
50  delete [] m_error_msg;
51  }
52  ::regfree (m_compiled_pattern);
53  delete (m_compiled_pattern);
54 }
55 
56 int
58 match (const char* text_)
59 {
60  trace_with_mask("Regexp::match", REGEXP);
61 
62  if (text_ == NULL || m_pattern == NULL) {
63  return -1;
64  }
65 
70  int ret = ::regexec (m_compiled_pattern, text_, 0, NULL, 0);
71 
72  if (ret != 0) {
73  ::regerror (ret, m_compiled_pattern, m_error_msg, 256);
74  DL((REGEXP,"regexec(\"%s\") = %d\n", text_, ret));
75  DL((REGEXP,"pattern: \"%s\"\n", m_pattern));
76  DL((REGEXP,"error: \"%s\"\n", m_error_msg));
77  }
78 
79  return (ret == 0 ? 0 : -1);
80 }
81 
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Wrapper class for UNIX regexp (3).
char * m_error_msg
Definition: Regexp.h:72
char * m_pattern
Definition: Regexp.h:71
Regexp(const std::string &pattern_)
Constructor.
Definition: Regexp.cpp:17
regex_t * m_compiled_pattern
Definition: Regexp.h:73
~Regexp()
Destructor.
Definition: Regexp.cpp:42
int match(const char *text_)
Match an ASCII character string agains the pattern this class wraps.
Definition: Regexp.cpp:58
Definition: Acceptor.h:40
@ REGEXP
Class RegExp messages
Definition: LogMask.h:53