summaryrefslogtreecommitdiff
path: root/src/session.h
blob: c8d83065924ac218da8878c1039f8802c357c1df (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
#ifndef _SESSION_H
#define _SESSION_H

#include "journal.h"
#include "parser.h"
#include "register.h"

namespace ledger {

class session_t : public xml::xpath_t::scope_t
{
 public:
  static session_t * current;

  path		 data_file;
  optional<path> init_file;
  optional<path> cache_file;
  optional<path> price_db;

  string register_format;
  string wide_register_format;
  string print_format;
  string balance_format;
  string equity_format;
  string plot_amount_format;
  string plot_total_format;
  string write_hdr_format;
  string write_xact_format;
  string prices_format;
  string pricesdb_format;

  unsigned long pricing_leeway;

  bool download_quotes;
  bool use_cache;
  bool cache_dirty;

  moment_t now;

  elision_style_t elision_style;

  int abbrev_length;

  bool ansi_codes;
  bool ansi_invert;

  ptr_list<journal_t> journals;
  ptr_list<parser_t>  parsers;

  session_t(xml::xpath_t::scope_t * _parent = NULL) :
    xml::xpath_t::scope_t(_parent),

    register_format
    ("%((//entry)%{date} %-.20{payee}"
     "%((./xact)%32|%-22{abbrev(account, 22)} %12.67t %12.80T\n))"),
    wide_register_format
    ("%D  %-.35P %-.38A %22.108t %!22.132T\n%/"
     "%48|%-.38A %22.108t %!22.132T\n"),
    print_format
#if 1
    ("%(/%(/%{date} %-.20{payee}\n%(:    %-34{account}  %12t\n)\n))"),
#else
    ("\n%d %Y%C%P\n    %-34W  %12o%n\n%/    %-34W  %12o%n\n"),
#endif
    balance_format
    ("%(/%(//%20t  %{\"  \" * rdepth}%{rname}\n))--------------------\n%20t\n"),
    equity_format

    ("%((/)%{ftime(now, date_format)} %-.20{\"Opening Balance\"}\n%((.//account[value != 0])    %-34{fullname}  %12{value}\n)\n)"),
    plot_amount_format
    ("%D %(@S(@t))\n"),
    plot_total_format
    ("%D %(@S(@T))\n"),
    write_hdr_format
    ("%d %Y%C%P\n"),
    write_xact_format
    ("    %-34W  %12o%n\n"),
    prices_format
    ("%[%Y/%m/%d %H:%M:%S %Z]   %-10A %12t %12T\n"),
    pricesdb_format
    ("P %[%Y/%m/%d %H:%M:%S] %A %t\n"),

    pricing_leeway(24 * 3600),

    download_quotes(false),
    use_cache(false),
    cache_dirty(false),

    now(now),

    elision_style(ABBREVIATE),
    abbrev_length(2),

    ansi_codes(false),
    ansi_invert(false) {
    TRACE_CTOR(session_t, "xml::xpath_t::scope_t *");
  }

  virtual ~session_t() {
    TRACE_DTOR(session_t);
  }

  journal_t * new_journal() {
    journal_t * journal = new journal_t(this);
    journals.push_back(journal);
    return journal;
  }
  void close_journal(journal_t * journal) {
    for (ptr_list<journal_t>::iterator i = journals.begin();
	 i != journals.end();
	 i++)
      if (&*i == journal) {
	journals.erase(i);
	return;
      }
    assert(false);
    checked_delete(journal);
  }

  unsigned int read_journal(std::istream&	  in,
			    journal_t *		  journal,
			    account_t *		  master   = NULL,
			    const optional<path>& original = optional<path>());

  unsigned int read_journal(const path&		  path,
			    journal_t *		  journal,
			    account_t *		  master   = NULL,
			    const optional<path>& original = optional<path>());

  void read_init();

  journal_t * read_data(const string& master_account = "");

  void register_parser(parser_t * parser) {
    parsers.push_back(parser);
  }
  void unregister_parser(parser_t * parser) {
    for (ptr_list<parser_t>::iterator i = parsers.begin();
	 i != parsers.end();
	 i++)
      if (&*i == parser) {
	parsers.erase(i);
	return;
      }
    assert(false);
    checked_delete(parser);
  }

  //
  // Scope members
  //

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

  //
  // Debug options
  //

  void option_verify(value_t&) {}
  void option_trace(value_t&, xml::xpath_t::scope_t * locals) {}
  void option_debug(value_t&, xml::xpath_t::scope_t * locals) {}

  void option_verbose(value_t&) {
    if (_log_level < LOG_INFO)
      _log_level = LOG_INFO;
  }

  //
  // Option handlers
  //

  void option_file(value_t&, xml::xpath_t::scope_t * locals) {
    data_file = locals->args.string_value();
  }

#if 0
#if defined(USE_BOOST_PYTHON)
  void option_import(value_t&) {
    python_import(optarg);
  }
  void option_import_stdin(value_t&) {
    python_eval(std::cin, PY_EVAL_MULTI);
  }
#endif
#endif
};

/**
 * This sets the current session context, transferring all static
 * globals to point at the data structures related to this session.
 * Although Ledger itself is not thread-safe, by locking, switching
 * session context, then unlocking after the operation is done,
 * multiple threads can sequentially make use of the library.  Thus, a
 * session_t maintains all of the information relating to a single
 * usage of the Ledger library.
 */
void set_session_context(session_t * session = NULL);

} // namespace ledger

#endif // _SESSION_H