File: Regexp.cpp

package info (click to toggle)
libassa 3.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,084 kB
  • ctags: 2,323
  • sloc: cpp: 15,641; sh: 8,704; makefile: 372; perl: 51
file content (81 lines) | stat: -rw-r--r-- 2,184 bytes parent folder | download | duplicates (9)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// -*- 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;
    }

    /**
       regexec(3) returns zero for a successful match or
       REG_NOMATCH for failure.
    */
    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);
}