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
|
#include "session.h"
#if defined(USE_BOOST_PYTHON)
#include "py_eval.h"
#endif
namespace ledger {
unsigned int session_t::read_journal(std::istream& in,
journal_t * journal,
account_t * master,
const string * original_file)
{
if (! master)
master = journal->master;
for (std::list<parser_t *>::iterator i = parsers.begin();
i != parsers.end();
i++)
if ((*i)->test(in))
return (*i)->parse(in, journal, master, original_file);
return 0;
}
unsigned int session_t::read_journal(const string& path,
journal_t * journal,
account_t * master,
const string * original_file)
{
journal->sources.push_back(path);
if (access(path.c_str(), R_OK) == -1)
throw_(exception, "Cannot read file '" << path << "'");
if (! original_file)
original_file = &path;
std::ifstream stream(path.c_str());
return read_journal(stream, journal, master, original_file);
}
void session_t::read_init()
{
if (init_file.empty())
return;
if (access(init_file.c_str(), R_OK) == -1)
throw_(exception, "Cannot read init file '" << init_file << "'");
std::ifstream init(init_file.c_str());
// jww (2006-09-15): Read initialization options here!
}
journal_t * session_t::read_data(const string& master_account)
{
TRACE_START(parser, 1, "Parsing journal file");
journal_t * journal = new_journal();
journal->document = new xml::document_t;
journal->document->set_top(xml::wrap_node(journal->document, journal));
unsigned int entry_count = 0;
DEBUG_("ledger.cache",
"3. use_cache = " << use_cache);
if (use_cache && ! cache_file.empty() &&
! data_file.empty()) {
DEBUG_("ledger.cache",
"using_cache " << cache_file);
cache_dirty = true;
if (access(cache_file.c_str(), R_OK) != -1) {
std::ifstream stream(cache_file.c_str());
string price_db_orig = journal->price_db;
journal->price_db = price_db;
entry_count += read_journal(stream, journal, NULL,
&data_file);
if (entry_count > 0)
cache_dirty = false;
else
journal->price_db = price_db_orig;
}
}
if (entry_count == 0 && ! data_file.empty()) {
account_t * acct = NULL;
if (! master_account.empty())
acct = journal->find_account(master_account);
journal->price_db = price_db;
if (! journal->price_db.empty() &&
access(journal->price_db.c_str(), R_OK) != -1) {
if (read_journal(journal->price_db, journal)) {
throw_(exception, "Entries not allowed in price history file");
} else {
DEBUG_("ledger.cache",
"read price database " << journal->price_db);
journal->sources.pop_back();
}
}
DEBUG_("ledger.cache",
"rejected cache, parsing " << data_file);
if (data_file == "-") {
use_cache = false;
journal->sources.push_back("<stdin>");
entry_count += read_journal(std::cin, journal, acct);
}
else if (access(data_file.c_str(), R_OK) != -1) {
entry_count += read_journal(data_file, journal, acct);
if (! journal->price_db.empty())
journal->sources.push_back(journal->price_db);
}
}
VERIFY(journal->valid());
if (entry_count == 0)
throw_(exception, "Failed to locate any journal entries; "
"did you specify a valid file with -f?");
TRACE_STOP(parser, 1);
return journal;
}
bool session_t::resolve(const string& name, value_t& result,
xml::xpath_t::scope_t * locals)
{
const char * p = name.c_str();
switch (*p) {
case 'd':
if (name == "date_format") {
// jww (2007-04-18): What to do here?
#if 0
result.set_string(moment_t::output_format);
#endif
return true;
}
break;
case 'n':
switch (*++p) {
case 'o':
if (name == "now") {
result = now;
return true;
}
break;
}
break;
case 'r':
if (name == "register_format") {
result = register_format;
return true;
}
break;
}
return xml::xpath_t::scope_t::resolve(name, result, locals);
}
xml::xpath_t::op_t * session_t::lookup(const string& name)
{
const char * p = name.c_str();
switch (*p) {
case 'o':
if (std::strncmp(p, "option_", 7) == 0) {
p = p + 7;
switch (*p) {
case 'f':
if (! *(p + 1) || std::strcmp(p, "file") == 0)
return MAKE_FUNCTOR(session_t, option_file);
break;
case 'v':
if (std::strcmp(p, "verbose") == 0)
return MAKE_FUNCTOR(session_t, option_verbose);
break;
}
}
break;
}
return xml::xpath_t::scope_t::lookup(name);
}
// jww (2007-04-26): All of Ledger should be accessed through a
// session_t object
void initialize()
{
amount_t::initialize();
}
void shutdown()
{
#if defined(USE_BOOST_PYTHON)
shutdown_for_python();
#endif
amount_t::shutdown();
}
} // namespace ledger
|