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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#include <stdlib.h>
#include <stdio.h>
#include <regex.h>
#include <iostream>
#include <iterator>
#include "tools.h"
Error::Error()
{
stop=false;
}
void Error::warning(string s)
{
s.insert(0, "Warning: ");
messages.push_back(s);
}
void Error::error(string s)
{
s.insert(0, "Error: ");
messages.push_back(s);
stop=true;
}
void Error::print()
{
copy(messages.begin(), messages.end(), ostream_iterator<string>(cerr));
}
/*----------------------------------------------------------------------*/
Regexp::Regexp()
{
}
bool Regexp::matches(string pattern, string src, list<string> & matches)
{
regex_t pattern_buffer;
regmatch_t regs[10];
/* try to compile pattern */
int err=0;
if (src.empty() ||
(err=regcomp(&pattern_buffer, pattern.c_str(), REG_EXTENDED)) )
{
char msg[100];
if (!(src.empty()))
{
regerror(err, &pattern_buffer, msg, 100);
cerr << "Error in regular expression: "
<< pattern << '\n' << msg << '\n';
}
/* if error occured, no matching was found */
return false;
}
/* match the pattern against the given string */
if (regexec(&pattern_buffer, src.c_str(), 10, regs, 0)!=0)
/* no match */
return false;
/* copy the part left to the point where the matching starts */
int i;
for (i=1; i<9; i++)
{
if (regs[i].rm_so>=0)
matches.push_back(src.substr(regs[i].rm_so, regs[i].rm_eo - regs[i].rm_so));
}
/* return the end of the last match */
return true;
}
bool Regexp::substitute_local(string pattern, string src, string replacement,
string & dest, int & dest_end)
{
//cout << pattern << ":" << src << ":" << replacement << endl;
regex_t pattern_buffer;
regmatch_t regs[10];
/* try to compile pattern */
int err=0;
if (src.empty() || (err=regcomp(&pattern_buffer, pattern.c_str(),
REG_EXTENDED)) )
{
char msg[100];
if (!src.empty())
{
regerror(err, &pattern_buffer, msg, 100);
cerr << "Error in regular expression: "
<< pattern << '\n' << msg << '\n';
}
/* if error occured, do not make substitution */
dest=src;
return false;
}
/* match the pattern against the given string */
if (regexec(&pattern_buffer, src.c_str(), 10, regs, 0)!=0)
{
/* no match */
dest=src;
return false;
}
/* copy the part left to the point where the matching starts */
dest=src.substr(0,regs[0].rm_so);
unsigned int i=0;
while (i<replacement.length()) {
if (replacement[i]=='\\') {
i++;
if (replacement[i]>='0' && replacement[i]<='9') {
int num=replacement[i]-'0';
dest+=src.substr(regs[num].rm_so, regs[num].rm_eo - regs[num].rm_so);
} else
dest+=replacement[i];
}
else
dest+=replacement[i];
i++;
}
dest_end=dest.length();
dest+=src.substr(regs[0].rm_eo, src.length() - regs[0].rm_eo);
//cout << ":" << dest << endl;
return true;
}
void Regexp::substitute(string pattern, string src,
string replacement, string & dest, bool global)
{
string src_left, src_right, dest_right, result;
int differ, index=0, de=0;
//cout << "!" << src << "!" << global << endl;
if (global)
{
result=src;
do {
src_left=result.substr(0,index);
src_right=result.substr(index);
differ=substitute_local(pattern, src_right,
replacement, dest_right, de);
result=src_left+dest_right;
index+=de;
} while (differ);
dest=result;
}
else
substitute_local(pattern, src, replacement, dest, de);
}
/* transliterate a string like sed's y */
void Regexp::transliterate(string from, string to, string src, string & dest)
{
int i, pos;
dest=string();
for (i=0; i<(int)src.length(); i++)
{
pos=from.find(src[i]);
if (pos>=0 && pos<(int)to.length()) dest+=to[pos];
else dest+=src[i];
}
}
void Regexp::split(char c, string s, list<string> & r)
{
size_t b=0, e;
e=s.find(c,b);
if (e<s.npos)
{
do
{
r.push_back(s.substr(b,e-b)); // between separators
b=e+1;
e=s.find(c,b);
}
while (e<s.npos);
if (b<s.length())
r.push_back(s.substr(b,e-b)); // after the last separator
}
else
r.push_back(s); // no separator present
}
void Regexp::join(char c, list<string> & r, string & s)
{
list<string>::iterator it;
for (it=r.begin();
it!=r.end();
it++)
{
if (it!=r.begin())
s+=c;
s=(*it);
}
}
|