summaryrefslogtreecommitdiff
path: root/report.h
blob: 3776ba3e22d7cfed803fba9d96d8c26ddddfeb8e (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
#ifndef _REPORT_H
#define _REPORT_H

#include "session.h"
#include "transform.h"

#include <string>
#include <list>

namespace ledger {

typedef std::list<string> strings_list;

class report_t : public xml::xpath_t::scope_t
{
 public:
  string output_file;
  string format_string;
  string amount_expr;
  string total_expr;
  string date_output_format;

  unsigned long budget_flags;

  string account;
  string pager;

  bool show_totals;
  bool raw_mode;

  session_t *   session;
  transform_t * last_transform;

  std::list<transform_t *> transforms;

  report_t(session_t * _session)
    : xml::xpath_t::scope_t(_session),
      show_totals(false),
      raw_mode(false),
      session(_session),
      last_transform(NULL)
  {
    TRACE_CTOR(report_t, "session_t *");
    eval("t=total,TOT=0,T()=(TOT=TOT+t,TOT)");
  }

  virtual ~report_t();

  void apply_transforms(xml::document_t * document);

  //
  // Utility functions for value expressions
  //

  void ftime(value_t& result, xml::xpath_t::scope_t * locals);
  void abbrev(value_t& result, xml::xpath_t::scope_t * locals);

  //
  // Config options
  //

  void eval(const string& expr) {
    xml::xpath_t(expr).compile((xml::document_t *)NULL, this);
  }
  void option_eval(value_t&, xml::xpath_t::scope_t * locals) {
    eval(locals->args[0].to_string());
  }

  void option_amount(value_t&, xml::xpath_t::scope_t * locals) {
    eval(string("t=") + locals->args[0].to_string());
  }
  void option_total(value_t&, xml::xpath_t::scope_t * locals) {
    eval(string("T()=") + locals->args[0].to_string());
  }

  void option_format(value_t&, xml::xpath_t::scope_t * locals) {
    format_string = locals->args[0].to_string();
  }

  void option_raw(value_t&) {
    raw_mode = true;
  }

  void option_foo(value_t&) {
    std::cout << "This is foo" << std::endl;
  }
  void option_bar(value_t&, xml::xpath_t::scope_t * locals) {
    std::cout << "This is bar: " << locals->args[0] << std::endl;
  }

  //
  // Transform options
  //

#if 0
  void option_select(value_t&, xml::xpath_t::scope_t * locals) {
    transforms.push_back(new select_transform(locals->args[0].to_string()));
  }
  void option_limit(value_t&, xml::xpath_t::scope_t * locals) {
    string expr = (string("//xact[") +
			locals->args[0].to_string() + "]");
    transforms.push_back(new select_transform(expr));
  }

  void option_remove(value_t&, xml::xpath_t::scope_t * locals) {
    transforms.push_back(new remove_transform(locals->args[0].to_string()));
  }

  void option_accounts(value_t&) {
    transforms.push_back(new accounts_transform);
  }
  void option_compact(value_t&) {
    transforms.push_back(new compact_transform);
  }
  void option_clean(value_t&) {
    transforms.push_back(new clean_transform);
  }
  void option_entries(value_t&) {
    transforms.push_back(new entries_transform);
  }

  void option_split(value_t&) {
    transforms.push_back(new split_transform);
  }
  void option_merge(value_t&) {
    transforms.push_back(new merge_transform);
  }
#endif

  //
  // Scope members
  //

  virtual bool resolve(const string& name, value_t& result,
		       xml::xpath_t::scope_t * locals);
  virtual xml::xpath_t::op_t * lookup(const string& name);
};

string abbrev(const string& str, unsigned int width,
		   const bool is_account);

} // namespace ledger

#endif // _REPORT_H