summaryrefslogtreecommitdiff
path: root/src/session.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-13 05:24:28 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-13 05:24:28 -0400
commit70344b82e70f454462f31daec05d79554804e8f8 (patch)
treed9f3252972cd3b83453ea0d5dfee932f99c4a793 /src/session.cc
parent037dd0f716e82568b005a78ee4d79fce0b886af3 (diff)
downloadfork-ledger-70344b82e70f454462f31daec05d79554804e8f8.tar.gz
fork-ledger-70344b82e70f454462f31daec05d79554804e8f8.tar.bz2
fork-ledger-70344b82e70f454462f31daec05d79554804e8f8.zip
Added a "reload" command, for use at the REPL
Created a new function, session_t::reread_journal_files, which throws away all previous state data and reads in the same files again. This is needed to allow Emacs to communicate with Ledger via the REPL, so that it tell Ledger when it has made changes to the user's data file.
Diffstat (limited to 'src/session.cc')
-rw-r--r--src/session.cc96
1 files changed, 63 insertions, 33 deletions
diff --git a/src/session.cc b/src/session.cc
index 99b0d013..52c84acb 100644
--- a/src/session.cc
+++ b/src/session.cc
@@ -60,7 +60,7 @@ session_t::session_t()
current_year(CURRENT_DATE().year()),
commodity_pool(new commodity_pool_t),
- master(new account_t(NULL, "")),
+ master(new account_t),
journal(new journal_t(master.get()))
{
TRACE_CTOR(session_t, "");
@@ -106,42 +106,39 @@ std::size_t session_t::read_data(const string& master_account)
std::size_t entry_count = 0;
- if (entry_count == 0) {
- account_t * acct = journal->master;
- if (! master_account.empty())
- acct = journal->find_account(master_account);
+ account_t * acct = journal->master;
+ if (! master_account.empty())
+ acct = journal->find_account(master_account);
- if (HANDLED(price_db_) && exists(path(HANDLER(price_db_).str()))) {
- if (read_journal(HANDLER(price_db_).str()))
- throw_(parse_error, "Entries not allowed in price history file");
- }
-
-
- foreach (const path& pathname, HANDLER(file_).data_files) {
- if (pathname == "-") {
- // To avoid problems with stdin and pipes, etc., we read the entire
- // file in beforehand into a memory buffer, and then parcel it out
- // from there.
- std::ostringstream buffer;
+ if (HANDLED(price_db_) && exists(path(HANDLER(price_db_).str()))) {
+ if (read_journal(HANDLER(price_db_).str()))
+ throw_(parse_error, "Entries not allowed in price history file");
+ }
- while (std::cin.good() && ! std::cin.eof()) {
- char line[8192];
- std::cin.read(line, 8192);
- std::streamsize count = std::cin.gcount();
- buffer.write(line, count);
- }
- buffer.flush();
+ foreach (const path& pathname, HANDLER(file_).data_files) {
+ if (pathname == "-") {
+ // To avoid problems with stdin and pipes, etc., we read the entire
+ // file in beforehand into a memory buffer, and then parcel it out
+ // from there.
+ std::ostringstream buffer;
+
+ while (std::cin.good() && ! std::cin.eof()) {
+ char line[8192];
+ std::cin.read(line, 8192);
+ std::streamsize count = std::cin.gcount();
+ buffer.write(line, count);
+ }
+ buffer.flush();
- std::istringstream buf_in(buffer.str());
+ std::istringstream buf_in(buffer.str());
- entry_count += read_journal(buf_in, "/dev/stdin", acct);
- }
- else if (exists(pathname)) {
- entry_count += read_journal(pathname, acct);
- }
- else {
- throw_(parse_error, "Could not open journal file '" << pathname << "'");
- }
+ entry_count += read_journal(buf_in, "/dev/stdin", acct);
+ }
+ else if (exists(pathname)) {
+ entry_count += read_journal(pathname, acct);
+ }
+ else {
+ throw_(parse_error, "Could not open journal file '" << pathname << "'");
}
}
@@ -150,6 +147,39 @@ std::size_t session_t::read_data(const string& master_account)
return entry_count;
}
+void session_t::read_journal_files()
+{
+ INFO_START(journal, "Read journal file");
+
+ string master_account;
+ if (HANDLED(account_))
+ master_account = HANDLER(account_).str();
+
+ std::size_t count = read_data(master_account);
+ if (count == 0)
+ throw_(parse_error, "Failed to locate any journal entries; "
+ "did you specify a valid file with -f?");
+
+ INFO_FINISH(journal);
+
+ INFO("Found " << count << " entries");
+}
+
+void session_t::reread_journal_files()
+{
+ journal.reset();
+ master.reset();
+ commodity_pool.reset();
+ amount_t::shutdown();
+
+ commodity_pool.reset(new commodity_pool_t);
+ amount_t::initialize(commodity_pool);
+ master.reset(new account_t);
+ journal.reset(new journal_t(master.get()));
+
+ read_journal_files();
+}
+
void session_t::clean_xacts()
{
journal_xacts_iterator walker(*journal.get());