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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#include "constraint.h"
#include "expr.h"
#include <pcre.h>
namespace ledger {
constraints_t::~constraints_t()
{
if (predicate) delete predicate;
if (sort_order) delete sort_order;
}
mask_t::mask_t(const std::string& pat) : exclude(false)
{
const char * p = pat.c_str();
if (*p == '-') {
exclude = true;
p++;
while (std::isspace(*p))
p++;
}
else if (*p == '+') {
p++;
while (std::isspace(*p))
p++;
}
pattern = p;
const char *error;
int erroffset;
regexp = pcre_compile(pattern.c_str(), PCRE_CASELESS,
&error, &erroffset, NULL);
if (! regexp)
std::cerr << "Warning: Failed to compile regexp: " << pattern
<< std::endl;
}
mask_t::mask_t(const mask_t& m) : exclude(m.exclude), pattern(m.pattern)
{
const char *error;
int erroffset;
regexp = pcre_compile(pattern.c_str(), PCRE_CASELESS,
&error, &erroffset, NULL);
assert(regexp);
}
bool mask_t::match(const std::string& str) const
{
static int ovec[30];
int result = pcre_exec((pcre *)regexp, NULL,
str.c_str(), str.length(), 0, 0, ovec, 30);
return result >= 0 && ! exclude;
}
mask_t::~mask_t() {
pcre_free((pcre *)regexp);
}
bool matches(const masks_list& regexps, const std::string& str,
bool * by_exclusion)
{
if (regexps.empty())
return false;
bool match = false;
bool definite = false;
for (masks_list::const_iterator r = regexps.begin();
r != regexps.end();
r++) {
static int ovec[30];
int result = pcre_exec((pcre *)(*r).regexp, NULL,
str.c_str(), str.length(), 0, 0, ovec, 30);
if (result >= 0) {
match = ! (*r).exclude;
definite = true;
}
else if ((*r).exclude) {
if (! match)
match = ! definite;
}
else {
definite = true;
}
}
if (by_exclusion)
*by_exclusion = match && ! definite && by_exclusion;
return match;
}
bool constraints_t::matches_date_range(const std::time_t date) const
{
if (have_beginning && difftime(date, begin_date) < 0)
return false;
if (have_ending && difftime(date, end_date) >= 0)
return false;
if (have_date_mask) {
struct std::tm * then = std::gmtime(&date);
if (date_mask.tm_mon != -1 &&
date_mask.tm_mon != then->tm_mon)
return false;
if (date_mask.tm_mday != -1 &&
date_mask.tm_mday != then->tm_mday)
return false;
#if 0
// jww (2003-10-10): This causes only certain days of the week to
// print, even when it was not included in the mask.
if (date_mask.tm_wday != -1 &&
date_mask.tm_wday != then->tm_wday)
return false;
#endif
if (date_mask.tm_year != -1 &&
date_mask.tm_year != then->tm_year)
return false;
}
return true;
}
bool constraints_t::operator ()(const transaction_t * xact) const
{
if ((cleared_only && xact->entry->state != entry_t::CLEARED) ||
(uncleared_only && xact->entry->state == entry_t::CLEARED) ||
! matches_date_range(xact->entry->date))
return false;
if (! payee_masks.empty() &&
(! (matches(payee_masks, xact->entry->payee)
//|| matches(payee_masks, xact->entry->code))
)))
return false;
if (real_only && xact->flags & TRANSACTION_VIRTUAL)
return false;
if (! account_masks.empty() &&
! (matches(account_masks, std::string(*(xact->account)))
//|| matches(account_masks, (*i)->note)
))
return false;
return true;
}
bool constraints_t::operator ()(const entry_t * entry) const
{
if ((cleared_only && entry->state != entry_t::CLEARED) ||
(uncleared_only && entry->state == entry_t::CLEARED) ||
! matches_date_range(entry->date))
return false;
if (! payee_masks.empty() &&
(! (matches(payee_masks, entry->payee)
//|| matches(payee_masks, entry->code)
)))
return false;
if (! account_masks.empty()) {
bool match = false;
for (transactions_list::const_iterator i = entry->transactions.begin();
i != entry->transactions.end();
i++) {
if (real_only && (*i)->flags & TRANSACTION_VIRTUAL)
continue;
if (matches(account_masks, std::string(*((*i)->account)))
//|| matches(account_masks, (*i)->note)
) {
match = true;
break;
}
}
if (! match)
return false;
}
return true;
}
bool constraints_t::operator ()(const item_t * item) const
{
if (predicate && ! predicate->compute(item, begin(), end()))
return false;
if (! matches_date_range(item->date))
return false;
if (! payee_masks.empty() && ! matches(payee_masks, item->payee))
return false;
#if 0
// jww (2004-07-26): It shouldn't be necessary to check against the
// account here, since this is always done during initial compiling
// of the item_t tree.
if (! account_masks.empty()) {
bool match = false;
for (amounts_map::const_iterator i = item->value.quantity.amounts.begin();
i != item->value.quantity.amounts.end();
i++) {
if (matches(account_masks, std::string(*((*i)->account)))
//|| matches(account_masks, (*i)->note)
) {
match = true;
break;
}
}
if (! match)
return false;
}
#endif
return true;
}
} // namespace ledger
|