summaryrefslogtreecommitdiff
path: root/format.h
blob: 7e5914c19c71ff073ebf6f2ebb98076be0c6ee28 (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
#ifndef _FORMAT_H
#define _FORMAT_H

#include "ledger.h"
#include "valexpr.h"
#include "walk.h"

namespace ledger {

std::string truncated(const std::string& str, unsigned int width);

std::string partial_account_name(const account_t *  account,
				 const unsigned int start_depth);

struct element_t
{
  enum kind_t {
    STRING,
    VALUE_EXPR,
    DATE_STRING,
    CLEARED,
    CODE,
    PAYEE,
    ACCOUNT_NAME,
    ACCOUNT_FULLNAME,
    OPT_AMOUNT,
    VALUE,
    TOTAL,
    SPACER
  };

  bool		 align_left;
  unsigned int	 min_width;
  unsigned int	 max_width;

  kind_t	 type;
  std::string	 chars;
  value_expr_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 std::string	date_format;
  static value_expr_t * value_expr;
  static value_expr_t * total_expr;

  format_t(const std::string& _format) : elements(NULL) {
    reset(_format);
  }
  ~format_t() {
    if (elements) delete elements;
  }

  void reset(const std::string& _format) {
    if (elements)
      delete elements;
    elements = parse_elements(_format);
  }

  static element_t * parse_elements(const std::string& fmt);

  void format_elements(std::ostream& out, const details_t& details) const;

  static void compute_value(value_t& result, const details_t& details) {
    if (value_expr)
      value_expr->compute(result, details);
  }

  static void compute_total(value_t& result, const details_t& details) {
    if (total_expr)
      total_expr->compute(result, details);
  }
};

class format_transactions : public item_handler<transaction_t>
{
  std::ostream&   output_stream;
  const format_t& first_line_format;
  const format_t& next_lines_format;
  entry_t *       last_entry;

 public:
  format_transactions(std::ostream&   _output_stream,
		      const format_t& _first_line_format,
		      const format_t& _next_lines_format)
    : output_stream(_output_stream),
      first_line_format(_first_line_format),
      next_lines_format(_next_lines_format), last_entry(NULL) {}

  virtual void flush() {
    output_stream.flush();
  }

  virtual void operator()(transaction_t * xact) {
    if (! xact->data ||
	! (XACT_DATA(xact)->dflags & TRANSACTION_DISPLAYED)) {
      if (last_entry != xact->entry) {
	first_line_format.format_elements(output_stream, details_t(xact));
	last_entry = xact->entry;
      } else {
	next_lines_format.format_elements(output_stream, details_t(xact));
      }
      if (! xact->data)
	xact->data = new transaction_data_t;
      XACT_DATA(xact)->dflags |= TRANSACTION_DISPLAYED;
    }
  }
};

class format_account : public item_handler<account_t>
{
  std::ostream&   output_stream;
  const format_t& format;

  item_predicate<account_t> disp_pred;

 public:
  format_account(std::ostream&      _output_stream,
		 const format_t&    _format,
		 const std::string& display_predicate = NULL)
    : output_stream(_output_stream), format(_format),
      disp_pred(display_predicate) {}

  static bool disp_subaccounts_p(const account_t * account,
				 const item_predicate<account_t>& disp_pred,
				 const account_t *& to_show);
  static bool disp_subaccounts_p(const account_t * account) {
    const account_t * temp;
    return disp_subaccounts_p(account, item_predicate<account_t>(NULL), temp);
  }

  static bool display_account(const account_t * account,
			      const item_predicate<account_t>& disp_pred);

  virtual void flush() {
    output_stream.flush();
  }

  virtual void operator()(account_t * account) {
    if (display_account(account, disp_pred)) {
      if (! account->parent) {
	if (! account->data)
	  account->data = new account_data_t;
	ACCT_DATA(account)->dflags |= ACCOUNT_TO_DISPLAY;
      } else {
	format.format_elements(output_stream, details_t(account));
	if (! account->data)
	  account->data = new account_data_t;
	ACCT_DATA(account)->dflags |= ACCOUNT_DISPLAYED;
      }
    }
  }
};

class format_equity : public item_handler<account_t>
{
  std::ostream&   output_stream;
  const format_t& first_line_format;
  const format_t& next_lines_format;

  item_predicate<account_t> disp_pred;

  mutable value_t total;

 public:
  format_equity(std::ostream&      _output_stream,
		const format_t&    _first_line_format,
		const format_t&    _next_lines_format,
		const std::string& display_predicate = NULL)
    : output_stream(_output_stream),
      first_line_format(_first_line_format),
      next_lines_format(_next_lines_format),
      disp_pred(display_predicate) {
    entry_t header_entry;
    header_entry.payee = "Opening Balances";
    header_entry.date  = std::time(NULL);
    first_line_format.format_elements(output_stream, details_t(&header_entry));
  }

  virtual void flush() {
    account_t summary(NULL, "Equity:Opening Balances");
    std::auto_ptr<account_data_t> acct_data(new account_data_t);
    summary.data = acct_data.get();
    ((account_data_t *) summary.data)->value = total;
    ((account_data_t *) summary.data)->value.negate();
    next_lines_format.format_elements(output_stream, details_t(&summary));
    output_stream.flush();
  }

  virtual void operator()(account_t * account) {
    if (format_account::display_account(account, disp_pred)) {
      next_lines_format.format_elements(output_stream, details_t(account));
      if (! account->data)
	account->data = new account_data_t;
      else
	total += ACCT_DATA(account)->value;
      ACCT_DATA(account)->dflags |= ACCOUNT_DISPLAYED;
    }
  }
};

} // namespace ledger

#endif // _REPORT_H