Lines 1-103
Link Here
|
1 |
/* |
|
|
2 |
* Copyright (c) [2004-2011] Novell, Inc. |
3 |
* |
4 |
* All Rights Reserved. |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify it |
7 |
* under the terms of version 2 of the GNU General Public License as published |
8 |
* by the Free Software Foundation. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
13 |
* more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License along |
16 |
* with this program; if not, contact Novell, Inc. |
17 |
* |
18 |
* To contact Novell about this file by physical or electronic mail, you may |
19 |
* find current contact information at www.novell.com. |
20 |
*/ |
21 |
|
22 |
|
23 |
// This file is obsolete. It is only present for compatibility of libsnapper, esp. the |
24 |
// Regex class was used in older versions of the zypp-plugin and we must avoid potentially |
25 |
// problems during updates. |
26 |
|
27 |
|
28 |
#include <stdexcept> |
29 |
#include "snapper/Regex.h" |
30 |
|
31 |
extern int _nl_msg_cat_cntr; |
32 |
|
33 |
|
34 |
namespace snapper |
35 |
{ |
36 |
|
37 |
Regex::Regex (const string& pattern, int cflags, unsigned int nm) |
38 |
: pattern (pattern), |
39 |
cflags (cflags), |
40 |
nm (cflags & REG_NOSUB ? 0 : nm) |
41 |
{ |
42 |
int errcode = regcomp (&rx, pattern.c_str (), cflags); |
43 |
if (errcode) { |
44 |
size_t esize = regerror(errcode, &rx, nullptr, 0); |
45 |
char error[esize]; |
46 |
regerror(errcode, &rx, error, esize); |
47 |
throw std::runtime_error(string("Regex compilation error: ") + error); |
48 |
} |
49 |
my_nl_msg_cat_cntr = _nl_msg_cat_cntr; |
50 |
rm = new regmatch_t[nm]; |
51 |
} |
52 |
|
53 |
|
54 |
Regex::~Regex () |
55 |
{ |
56 |
delete [] rm; |
57 |
regfree (&rx); |
58 |
} |
59 |
|
60 |
|
61 |
bool |
62 |
Regex::match (const string& str, int eflags) const |
63 |
{ |
64 |
if (my_nl_msg_cat_cntr != _nl_msg_cat_cntr) { |
65 |
regfree (&rx); |
66 |
regcomp (&rx, pattern.c_str (), cflags); |
67 |
my_nl_msg_cat_cntr = _nl_msg_cat_cntr; |
68 |
} |
69 |
|
70 |
last_str = str; |
71 |
|
72 |
return regexec (&rx, str.c_str (), nm, rm, eflags) == 0; |
73 |
} |
74 |
|
75 |
|
76 |
regoff_t |
77 |
Regex::so (unsigned int i) const |
78 |
{ |
79 |
return i < nm ? rm[i].rm_so : -1; |
80 |
} |
81 |
|
82 |
|
83 |
regoff_t |
84 |
Regex::eo (unsigned int i) const |
85 |
{ |
86 |
return i < nm ? rm[i].rm_eo : -1; |
87 |
} |
88 |
|
89 |
|
90 |
string |
91 |
Regex::cap (unsigned int i) const |
92 |
{ |
93 |
if (i < nm && rm[i].rm_so > -1) |
94 |
return last_str.substr (rm[i].rm_so, rm[i].rm_eo - rm[i].rm_so); |
95 |
return ""; |
96 |
} |
97 |
|
98 |
|
99 |
const string Regex::ws = "[ \t]*"; |
100 |
const string Regex::number = "[0123456789]+"; |
101 |
const string Regex::trailing_comment = "(#.*)?"; |
102 |
|
103 |
} |