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
|
#ifndef _FORMAT_H
#define _FORMAT_H
#include "ledger.h"
#include "balance.h"
#include "expr.h"
namespace ledger {
std::string truncated(const std::string& str, unsigned int width);
std::string maximal_account_name(const item_t * item, const item_t * parent);
struct element_t
{
enum kind_t {
STRING,
VALUE_EXPR,
DATE_STRING,
PAYEE,
ACCOUNT_NAME,
ACCOUNT_FULLNAME,
VALUE,
TOTAL,
SPACER
};
bool align_left;
unsigned int min_width;
unsigned int max_width;
kind_t type;
std::string chars;
node_t * val_expr;
struct element_t * next;
element_t() : align_left(false), min_width(0), max_width(0),
type(STRING), val_expr(NULL), next(NULL) {}
~element_t() {
if (val_expr) delete val_expr;
if (next) delete next; // recursive, but not too deep
}
};
struct format_t
{
element_t * elements;
static node_t * value_expr;
static node_t * total_expr;
format_t(const std::string& _format) {
elements = parse_elements(_format);
}
~format_t() {
if (elements) delete elements;
}
static element_t * parse_elements(const std::string& fmt);
void format_elements(std::ostream& out, const item_t * item,
const item_t * displayed_parent = NULL) const;
static balance_t compute_value(const item_t * item) {
return value_expr ? value_expr->compute(item) : balance_t();
}
static balance_t compute_total(const item_t * item) {
return total_expr ? total_expr->compute(item) : balance_t();
}
};
} // namespace ledger
#endif // _REPORT_H
|