parent
66bd3e9782
commit
2b5f1b9fd4
12 changed files with 226 additions and 310 deletions
@ -1,102 +0,0 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2008-2012 by Andrzej Rybczak * |
||||
* electricityispower@gmail.com * |
||||
* * |
||||
* This program is free software; you can redistribute it and/or modify * |
||||
* it under the terms of the GNU General Public License as published by * |
||||
* the Free Software Foundation; either version 2 of the License, or * |
||||
* (at your option) any later version. * |
||||
* * |
||||
* This program is distributed in the hope that it will be useful, * |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||||
* GNU General Public License for more details. * |
||||
* * |
||||
* You should have received a copy of the GNU General Public License * |
||||
* along with this program; if not, write to the * |
||||
* Free Software Foundation, Inc., * |
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#include <cassert> |
||||
#include "regexes.h" |
||||
|
||||
Regex::Regex() : m_cflags(0), m_compiled(false) { } |
||||
|
||||
Regex::Regex(const std::string ®ex_, int cflags) |
||||
: m_regex(regex_), m_cflags(cflags), m_compiled(false) |
||||
{ |
||||
compile(); |
||||
} |
||||
|
||||
Regex::Regex (const Regex &rhs) |
||||
: m_regex(rhs.m_regex), m_cflags(rhs.m_cflags), m_compiled(false) |
||||
{ |
||||
if (rhs.m_compiled) |
||||
compile(); |
||||
} |
||||
|
||||
Regex::~Regex() |
||||
{ |
||||
if (m_compiled) |
||||
regfree(&m_rx); |
||||
} |
||||
|
||||
const std::string &Regex::regex() const |
||||
{ |
||||
return m_regex; |
||||
} |
||||
|
||||
const std::string &Regex::error() const |
||||
{ |
||||
return m_error; |
||||
} |
||||
|
||||
bool Regex::compiled() const |
||||
{ |
||||
return m_compiled; |
||||
} |
||||
|
||||
bool Regex::compile() |
||||
{ |
||||
if (m_compiled) |
||||
{ |
||||
m_error.clear(); |
||||
regfree(&m_rx); |
||||
} |
||||
int comp_res = regcomp(&m_rx, m_regex.c_str(), m_cflags); |
||||
bool result = true; |
||||
if (comp_res != 0) |
||||
{ |
||||
char buf[256]; |
||||
regerror(comp_res, &m_rx, buf, sizeof(buf)); |
||||
m_error = buf; |
||||
result = false; |
||||
} |
||||
m_compiled = result; |
||||
return result; |
||||
} |
||||
|
||||
bool Regex::compile(const std::string ®ex_, int cflags) |
||||
{ |
||||
m_regex = regex_; |
||||
m_cflags = cflags; |
||||
return compile(); |
||||
} |
||||
|
||||
bool Regex::match(const std::string &s) const |
||||
{ |
||||
assert(m_compiled); |
||||
return regexec(&m_rx, s.c_str(), 0, 0, 0) == 0; |
||||
} |
||||
|
||||
Regex &Regex::operator=(const Regex &rhs) |
||||
{ |
||||
if (this == &rhs) |
||||
return *this; |
||||
m_regex = rhs.m_regex; |
||||
m_cflags = rhs.m_cflags; |
||||
if (rhs.m_compiled) |
||||
compile(); |
||||
return *this; |
||||
} |
||||
@ -1,65 +0,0 @@ |
||||
/***************************************************************************
|
||||
* Copyright (C) 2008-2012 by Andrzej Rybczak * |
||||
* electricityispower@gmail.com * |
||||
* * |
||||
* This program is free software; you can redistribute it and/or modify * |
||||
* it under the terms of the GNU General Public License as published by * |
||||
* the Free Software Foundation; either version 2 of the License, or * |
||||
* (at your option) any later version. * |
||||
* * |
||||
* This program is distributed in the hope that it will be useful, * |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||||
* GNU General Public License for more details. * |
||||
* * |
||||
* You should have received a copy of the GNU General Public License * |
||||
* along with this program; if not, write to the * |
||||
* Free Software Foundation, Inc., * |
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * |
||||
***************************************************************************/ |
||||
|
||||
#ifndef NCMPCPP_REGEXES_H |
||||
#define NCMPCPP_REGEXES_H |
||||
|
||||
#include <regex.h> |
||||
#include <string> |
||||
|
||||
struct Regex |
||||
{ |
||||
Regex(); |
||||
Regex(const std::string ®ex, int cflags); |
||||
Regex(const Regex &rhs); |
||||
virtual ~Regex(); |
||||
|
||||
/// @return regular expression
|
||||
const std::string ®ex() const; |
||||
|
||||
/// @return compilation error (if there was any)
|
||||
const std::string &error() const; |
||||
|
||||
/// @return true if regular expression is compiled, false otherwise
|
||||
bool compiled() const; |
||||
|
||||
/// compiles regular expression
|
||||
/// @result true if compilation was successful, false otherwise
|
||||
bool compile(); |
||||
|
||||
/// compiles regular expression
|
||||
/// @result true if compilation was successful, false otherwise
|
||||
bool compile(const std::string ®ex, int cflags); |
||||
|
||||
/// tries to match compiled regex with given string
|
||||
/// @return true if string was matched, false otherwise
|
||||
bool match(const std::string &s) const; |
||||
|
||||
Regex &operator=(const Regex &rhs); |
||||
|
||||
private: |
||||
std::string m_regex; |
||||
std::string m_error; |
||||
regex_t m_rx; |
||||
int m_cflags; |
||||
bool m_compiled; |
||||
}; |
||||
|
||||
#endif // NCMPCPP_REGEXES_H
|
||||
Loading…
Reference in new issue