libassa 3.5.0
|
00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // Regexp.cpp 00004 //------------------------------------------------------------------------------ 00005 // Copyright (C) 1997-2003 Vladislav Grinchenko <vlg@users.sourceforge.net> 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Library General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 2 of the License, or (at your option) any later version. 00011 //------------------------------------------------------------------------------ 00012 00013 #include <assa/Regexp.h> 00014 using namespace ASSA; 00015 00016 Regexp:: 00017 Regexp (const std::string& pattern_) 00018 : 00019 m_pattern (NULL), 00020 m_error_msg (new char [256]), 00021 m_compiled_pattern (new regex_t) 00022 { 00023 trace_with_mask("Regexp::Regexp", REGEXP); 00024 00025 m_pattern = new char [pattern_.size () + 1]; 00026 ::strncpy (m_pattern, pattern_.c_str (), pattern_.size ()); 00027 m_pattern [pattern_.size ()] = '\0'; 00028 00029 int ret = ::regcomp (m_compiled_pattern, m_pattern, REG_EXTENDED); 00030 00031 if (ret != 0) { 00032 ::regerror (ret, m_compiled_pattern, m_error_msg, 256); 00033 DL((REGEXP,"regcomp(\"%s\") = %d\n", m_pattern, ret)); 00034 DL((REGEXP,"error: \"%s\"\n", m_error_msg)); 00035 00036 delete [] m_pattern; 00037 m_pattern = NULL; 00038 } 00039 } 00040 00041 Regexp:: 00042 ~Regexp () 00043 { 00044 trace_with_mask("Regexp::~Regexp", REGEXP); 00045 00046 if (m_pattern) { 00047 delete [] m_pattern; 00048 } 00049 if (m_error_msg) { 00050 delete [] m_error_msg; 00051 } 00052 ::regfree (m_compiled_pattern); 00053 delete (m_compiled_pattern); 00054 } 00055 00056 int 00057 Regexp:: 00058 match (const char* text_) 00059 { 00060 trace_with_mask("Regexp::match", REGEXP); 00061 00062 if (text_ == NULL || m_pattern == NULL) { 00063 return -1; 00064 } 00065 00070 int ret = ::regexec (m_compiled_pattern, text_, 0, NULL, 0); 00071 00072 if (ret != 0) { 00073 ::regerror (ret, m_compiled_pattern, m_error_msg, 256); 00074 DL((REGEXP,"regexec(\"%s\") = %d\n", text_, ret)); 00075 DL((REGEXP,"pattern: \"%s\"\n", m_pattern)); 00076 DL((REGEXP,"error: \"%s\"\n", m_error_msg)); 00077 } 00078 00079 return (ret == 0 ? 0 : -1); 00080 } 00081