summaryrefslogtreecommitdiff
path: root/walk.h
blob: 102ed10c327c6e0b6c25744af0f67dbcd19197e4 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#ifndef _WALK_H
#define _WALK_H

#include "ledger.h"
#include "balance.h"
#include "valexpr.h"
#include "datetime.h"

#include <iostream>
#include <deque>

namespace ledger {

template <typename T>
struct item_handler {
  item_handler * handler;

 public:
  item_handler() : handler(NULL) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor item_handler<T>");
  }
  item_handler(item_handler * _handler) : handler(_handler) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor item_handler<T>");
  }

  virtual ~item_handler() {
    DEBUG_PRINT("ledger.memory.dtors", "dtor item_handler<T>");
  }
  virtual void flush() {
    if (handler)
      handler->flush();
  }
  virtual void operator()(T& item) {
    if (handler)
      (*handler)(item);
  }
};

template <typename T>
class compare_items {
  value_t left_result;
  value_t right_result;

  const value_expr_t * sort_order;

 public:
  compare_items(const value_expr_t * _sort_order)
    : sort_order(_sort_order) {
    assert(sort_order);
  }

  bool operator()(const T * left, const T * right) {
    assert(left);
    assert(right);

    sort_order->compute(left_result, details_t(*left));
    sort_order->compute(right_result, details_t(*right));

    return left_result < right_result;
  }
};

//////////////////////////////////////////////////////////////////////
//
// Transaction handlers
//

typedef std::deque<transaction_t *> transactions_deque;
typedef std::deque<entry_t *>       entries_deque;

inline void walk_transactions(transactions_list::iterator begin,
			      transactions_list::iterator end,
			      item_handler<transaction_t>& handler) {
  for (transactions_list::iterator i = begin; i != end; i++)
    handler(**i);
}

inline void walk_transactions(transactions_list& list,
			      item_handler<transaction_t>& handler) {
  walk_transactions(list.begin(), list.end(), handler);
}

inline void walk_transactions(transactions_deque::iterator begin,
			      transactions_deque::iterator end,
			      item_handler<transaction_t>& handler) {
  for (transactions_deque::iterator i = begin; i != end; i++)
    handler(**i);
}

inline void walk_transactions(transactions_deque& deque,
			      item_handler<transaction_t>& handler) {
  walk_transactions(deque.begin(), deque.end(), handler);
}

inline void walk_entries(entries_list::iterator       begin,
			 entries_list::iterator       end,
			 item_handler<transaction_t>& handler) {
  for (entries_list::iterator i = begin; i != end; i++)
    walk_transactions((*i)->transactions, handler);
}

inline void walk_entries(entries_list& list,
			 item_handler<transaction_t>& handler) {
  walk_entries(list.begin(), list.end(), handler);
}

//////////////////////////////////////////////////////////////////////

#define TRANSACTION_HANDLED   0x0001
#define TRANSACTION_DISPLAYED 0x0002
#define TRANSACTION_NO_TOTAL  0x0004

struct transaction_data_t
{
  value_t	 total;
  unsigned int   index;
  unsigned short dflags;

  transaction_data_t() : index(0), dflags(0) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor transaction_data_t");
  }
#ifdef DEBUG_ENABLED
  ~transaction_data_t() {
    DEBUG_PRINT("ledger.memory.dtors", "dtor transaction_data_t");
  }
#endif
};

#define XACT_DATA(xact) ((transaction_data_t *) ((xact)->data))
#define XACT_DATA_(xact) ((transaction_data_t *) ((xact).data))

#define ACCOUNT_DISPLAYED  0x1
#define ACCOUNT_TO_DISPLAY 0x2

struct account_data_t
{
  value_t	 value;
  value_t	 total;
  unsigned int   count;	// transactions counted toward total
  unsigned int   subcount;
  unsigned short dflags;

  account_data_t() : count(0), subcount(0), dflags(0) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor account_data_t");
  }
#ifdef DEBUG_ENABLED
  ~account_data_t() {
    DEBUG_PRINT("ledger.memory.dtors", "dtor account_data_t");
  }
#endif
};

#define ACCT_DATA(acct) ((account_data_t *) ((acct)->data))
#define ACCT_DATA_(acct) ((account_data_t *) ((acct).data))

//////////////////////////////////////////////////////////////////////

class ignore_transactions : public item_handler<transaction_t>
{
 public:
  virtual void operator()(transaction_t& xact) {}
};

class clear_transaction_data : public item_handler<transaction_t>
{
 public:
  virtual void operator()(transaction_t& xact) {
    if (xact.data) {
      delete (transaction_data_t *) xact.data;
      xact.data = NULL;
    }
  }
};

class set_account_value : public item_handler<transaction_t>
{
 public:
  set_account_value(item_handler<transaction_t> * handler = NULL)
    : item_handler<transaction_t>(handler) {}

  virtual void operator()(transaction_t& xact) {
    if (! ACCT_DATA(xact.account))
      xact.account->data = new account_data_t;

    add_transaction_to(xact, ACCT_DATA(xact.account)->value);
    ACCT_DATA(xact.account)->subcount++;

    if (handler)
      (*handler)(xact);
  }
};

class sort_transactions : public item_handler<transaction_t>
{
  transactions_deque   transactions;
  const value_expr_t * sort_order;

 public:
  sort_transactions(item_handler<transaction_t> * handler,
		    const value_expr_t * _sort_order)
    : item_handler<transaction_t>(handler),
      sort_order(_sort_order) {}

  virtual void flush();
  virtual void operator()(transaction_t& xact) {
    transactions.push_back(&xact);
  }
};

class filter_transactions : public item_handler<transaction_t>
{
  item_predicate<transaction_t> pred;

 public:
  filter_transactions(item_handler<transaction_t> * handler,
		      const std::string& predicate)
    : item_handler<transaction_t>(handler), pred(predicate) {}

  virtual void operator()(transaction_t& xact) {
    if (pred(xact))
      (*handler)(xact);
  }
};

class calc_transactions : public item_handler<transaction_t>
{
  transaction_t * last_xact;
  const bool      inverted;

 public:
  calc_transactions(item_handler<transaction_t> * handler,
		    const bool _inverted = false)
    : item_handler<transaction_t>(handler),
      last_xact(NULL), inverted(_inverted) {}

  virtual void operator()(transaction_t& xact);
};

class collapse_transactions : public item_handler<transaction_t>
{
  balance_pair_t     subtotal;
  unsigned int       count;
  entry_t *          last_entry;
  transaction_t *    last_xact;
  account_t *        totals_account;
  transactions_list xact_temps;

 public:
  collapse_transactions(item_handler<transaction_t> * handler)
    : item_handler<transaction_t>(handler), count(0),
      last_entry(NULL), last_xact(NULL) {
    totals_account = new account_t(NULL, "<Total>");
  }

  virtual ~collapse_transactions() {
    if (totals_account->data) {
      delete (account_data_t *) totals_account->data;
      totals_account->data = NULL;
    }
    delete totals_account;
    for (transactions_list::iterator i = xact_temps.begin();
	 i != xact_temps.end();
	 i++) {
      if ((*i)->data) {
	delete (transaction_data_t *) (*i)->data;
	(*i)->data = NULL;
      }
      delete *i;
    }
  }

  virtual void flush() {
    if (subtotal)
      report_cumulative_subtotal();
    item_handler<transaction_t>::flush();
  }

  void report_cumulative_subtotal();

  virtual void operator()(transaction_t& xact) {
    // If we've reached a new entry, report on the subtotal
    // accumulated thus far.

    if (last_entry && last_entry != xact.entry)
      report_cumulative_subtotal();

    add_transaction_to(xact, subtotal);
    count++;

    last_entry = xact.entry;
    last_xact  = &xact;
  }
};

class changed_value_transactions : public item_handler<transaction_t>
{
  // This filter requires that calc_transactions be used at some point
  // later in the chain.

  bool		    changed_values_only;
  transaction_t *   last_xact;
  entries_list      entry_temps;
  transactions_list xact_temps;

 public:
  changed_value_transactions(item_handler<transaction_t> * handler,
			     bool _changed_values_only)
    : item_handler<transaction_t>(handler),
      changed_values_only(_changed_values_only), last_xact(NULL) {}

  virtual ~changed_value_transactions() {
    for (entries_list::iterator i = entry_temps.begin();
	 i != entry_temps.end();
	 i++)
      delete *i;

    for (transactions_list::iterator i = xact_temps.begin();
	 i != xact_temps.end();
	 i++) {
      if ((*i)->data) {
	delete (transaction_data_t *) (*i)->data;
	(*i)->data = NULL;
      }
      delete *i;
    }
  }

  virtual void flush() {
    output_diff(std::time(NULL));
    last_xact = NULL;
    item_handler<transaction_t>::flush();
  }

  void output_diff(const std::time_t current);

  virtual void operator()(transaction_t& xact);
};

class subtotal_transactions : public item_handler<transaction_t>
{
  typedef std::map<account_t *, balance_pair_t>  balances_map;
  typedef std::pair<account_t *, balance_pair_t> balances_pair;

 protected:
  std::time_t	    start;
  std::time_t	    finish;
  balances_map	    balances;
  entries_list      entry_temps;
  transactions_list xact_temps;

 public:
  subtotal_transactions(item_handler<transaction_t> * handler)
    : item_handler<transaction_t>(handler) {}

  virtual ~subtotal_transactions() {
    for (entries_list::iterator i = entry_temps.begin();
	 i != entry_temps.end();
	 i++)
      delete *i;

    for (transactions_list::iterator i = xact_temps.begin();
	 i != xact_temps.end();
	 i++) {
      if ((*i)->data) {
	delete (transaction_data_t *) (*i)->data;
	(*i)->data = NULL;
      }
      delete *i;
    }
  }

  void flush(const char * spec_fmt);

  virtual void flush() {
    flush(NULL);
  }
  virtual void operator()(transaction_t& xact);
};

class interval_transactions : public subtotal_transactions
{
  std::time_t     begin;
  interval_t      interval;
  transaction_t * last_xact;

 public:
  interval_transactions(item_handler<transaction_t> * handler,
			const interval_t& _interval,
			const std::time_t _begin = 0)
    : subtotal_transactions(handler), begin(_begin),
      interval(_interval), last_xact(NULL) {}

  virtual ~interval_transactions() {
    start  = begin;
    finish = interval.increment(begin);
  }

  virtual void operator()(transaction_t& xact);
};

class dow_transactions : public subtotal_transactions
{
  transactions_list days_of_the_week[7];

 public:
  dow_transactions(item_handler<transaction_t> * handler)
    : subtotal_transactions(handler) {}

  virtual void flush();
  virtual void operator()(transaction_t& xact) {
    struct std::tm * desc = std::localtime(&xact.entry->date);
    days_of_the_week[desc->tm_wday].push_back(&xact);
  }
};

class related_transactions : public item_handler<transaction_t>
{
  bool also_matching;

 public:
  related_transactions(item_handler<transaction_t> * handler,
		       const bool _also_matching = false)
    : item_handler<transaction_t>(handler),
      also_matching(_also_matching) {}

  virtual void operator()(transaction_t& xact) {
    for (transactions_list::iterator i = xact.entry->transactions.begin();
	 i != xact.entry->transactions.end();
	 i++)
      if ((! (*i)->data ||
	   ! (XACT_DATA(*i)->dflags & TRANSACTION_HANDLED)) &&
	  (*i == &xact ? also_matching :
	   ! ((*i)->flags & TRANSACTION_AUTO))) {
	if (! (*i)->data)
	  (*i)->data = new transaction_data_t;
	XACT_DATA(*i)->dflags |= TRANSACTION_HANDLED;
	(*handler)(**i);
      }
  }
};


//////////////////////////////////////////////////////////////////////
//
// Account walking functions
//

class clear_account_data : public item_handler<account_t>
{
 public:
  virtual void operator()(account_t * account) {
    if (account->data) {
      delete (account_data_t *) account->data;
      account->data = NULL;
    }
  }
};

inline void sum_accounts(account_t& account) {
  if (! account.data)
    account.data = new account_data_t;

  for (accounts_map::iterator i = account.accounts.begin();
       i != account.accounts.end();
       i++) {
    sum_accounts(*(*i).second);
    ACCT_DATA_(account)->total += ACCT_DATA((*i).second)->total;
    ACCT_DATA_(account)->count += (ACCT_DATA((*i).second)->count +
				  ACCT_DATA((*i).second)->subcount);
  }
  ACCT_DATA_(account)->total += ACCT_DATA_(account)->value;
  ACCT_DATA_(account)->count += ACCT_DATA_(account)->subcount;
}

typedef std::deque<account_t *> accounts_deque;

inline void sort_accounts(account_t&	       account,
			  const value_expr_t * sort_order,
			  accounts_deque&      accounts) {
  for (accounts_map::iterator i = account.accounts.begin();
       i != account.accounts.end();
       i++)
    accounts.push_back((*i).second);

  std::stable_sort(accounts.begin(), accounts.end(),
		   compare_items<account_t>(sort_order));
}

inline void walk_accounts(account_t&		   account,
			  item_handler<account_t>& handler,
			  const value_expr_t *     sort_order = NULL) {
  handler(account);

  if (sort_order) {
    accounts_deque accounts;
    sort_accounts(account, sort_order, accounts);
    for (accounts_deque::const_iterator i = accounts.begin();
	 i != accounts.end();
	 i++)
      walk_accounts(**i, handler, sort_order);
  } else {
    for (accounts_map::const_iterator i = account.accounts.begin();
	 i != account.accounts.end();
	 i++)
      walk_accounts(*(*i).second, handler);
  }
}

} // namespace ledger

#endif // _WALK_H