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
|
// This file is part of The New Aspell
// Copyright (C) 2001 by Kevin Atkinson under the GNU LGPL license
// version 2.0 or 2.1. You should have received a copy of the LGPL
// license along with this library if you did not you can find
// it at http://www.gnu.org/.
#include "settings.h"
#include "indiv_filter.hpp"
#include "convert.hpp"
#include "config.hpp"
#include "indiv_filter.hpp"
#include "mutable_container.hpp"
namespace {
using namespace acommon;
class EmailFilter : public IndividualFilter
{
bool prev_newline;
bool in_quote;
int margin;
int n;
class QuoteChars : public MutableContainer {
public:
typedef FilterChar::Chr Value;
Vector<Value> data;
Conv conv;
bool have(Value c) {
Value * i = data.pbegin();
Value * end = data.pend();
for (; i != end && *i != c; ++i);
return i != end;
}
PosibErr<bool> add(ParmStr s) {
Value c = *(Value *)conv(s);
if (!have(c)) data.push_back(c);
return true;
}
PosibErr<bool> remove(ParmStr s) {
Value c = *(Value *)conv(s);
Vector<Value>::iterator i = data.begin();
Vector<Value>::iterator end = data.end();
for (; i != end && *i != c; ++i);
if (i != end) data.erase(i);
return true;
}
PosibErr<void> clear() {
data.clear();
return no_err;
}
};
QuoteChars is_quote_char;
public:
PosibErr<bool> setup(Config *);
void reset();
void process(FilterChar * &, FilterChar * &);
};
PosibErr<bool> EmailFilter::setup(Config * opts)
{
name_ = "email-filter";
order_num_ = 0.85;
is_quote_char.conv.setup(*opts, "utf-8", "ucs-4", NormNone);
opts->retrieve_list("f-email-quote", &is_quote_char);
margin = opts->retrieve_int("f-email-margin");
reset();
return true;
}
void EmailFilter::reset()
{
prev_newline = true;
in_quote = false;
n = 0;
}
void EmailFilter::process(FilterChar * & str, FilterChar * & end)
{
FilterChar * line_begin = str;
FilterChar * cur = str;
while (cur < end) {
if (prev_newline && is_quote_char.have(*cur))
in_quote = true;
if (*cur == '\n') {
if (in_quote) {
for (FilterChar * i = line_begin; i != cur; ++i)
*i = ' ';
}
line_begin = cur;
in_quote = false;
prev_newline = true;
n = 0;
} else if (n < margin) {
++n;
} else {
prev_newline = false;
}
++cur;
}
if (in_quote)
for (FilterChar * i = line_begin; i != cur; ++i)
*i = ' ';
}
}
C_EXPORT
IndividualFilter * new_aspell_email_filter() {
return new EmailFilter;
}
|