00001
00002
00003
00004
00005
00006
00007
00008
00012 #include <cstdio>
00013 #include <cstdarg>
00014
00015 #include <iostream>
00016
00017 #include "zypp/base/Regex.h"
00018
00019 using namespace zypp;
00020 using namespace zypp::str;
00021
00022 regex::regex()
00023 : m_valid(false)
00024 {
00025
00026 }
00027
00028 void regex::assign(const std::string& str,int flags)
00029 {
00030 m_valid = true;
00031 int err;
00032 char errbuff[100];
00033 if (!(flags & normal)) {
00034 flags |= match_extended;
00035 flags &= ~(normal);
00036 }
00037
00038 if ((err = regcomp(&m_preg, str.c_str(), flags))) {
00039 m_valid = false;
00040 regerror(err, &m_preg, errbuff, sizeof(errbuff));
00041 ZYPP_THROW(regex_error(std::string(errbuff)));
00042 }
00043 }
00044
00045 regex::regex(const std::string& str, int flags)
00046 {
00047 assign(str, flags);
00048 }
00049
00050 regex::~regex() throw()
00051 {
00052 if (m_valid)
00053 regfree(&m_preg);
00054 }
00055
00056 bool zypp::str::regex_match(const std::string& s, smatch& matches, const regex& regex)
00057 {
00058 bool r = regex.m_valid && !regexec(®ex.m_preg, s.c_str(), 12, &matches.pmatch[0], 0);
00059 if (r)
00060 matches.match_str = s;
00061 return r;
00062 }
00063
00064 bool zypp::str::regex_match(const std::string& s, const regex& regex)
00065 {
00066 return !regexec(®ex.m_preg, s.c_str(), 0, NULL, 0);
00067 }
00068
00069 smatch::smatch()
00070 {
00071 memset(&pmatch, -1, sizeof(pmatch));
00072 }
00073
00074 std::string smatch::operator[](unsigned i) const
00075 {
00076 if (i < sizeof(pmatch)/sizeof(*pmatch) && pmatch[i].rm_so != -1)
00077 return match_str.substr(pmatch[i].rm_so, pmatch[i].rm_eo-pmatch[i].rm_so);
00078 return std::string();
00079 }
00080
00081
00082 unsigned smatch::size() const
00083 {
00084 unsigned matches = 0;
00085 while (matches < ((sizeof(pmatch)/sizeof(*pmatch))-1) && pmatch[matches+1].rm_so != -1) {
00086
00087
00088 matches++;
00089 }
00090
00091 return matches;
00092 }