summaryrefslogtreecommitdiff
path: root/src/journal.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-11-05 02:26:06 -0500
committerJohn Wiegley <johnw@newartisans.com>2009-11-05 02:27:42 -0500
commit34ee358f5e16d4018adf3db5dac31d2d9c16b9f5 (patch)
tree5f2db02f896b2e50edb4fa1b143960286f6c1991 /src/journal.cc
parent817f1ae1619df06f06f79f772f075041bb70f751 (diff)
downloadfork-ledger-34ee358f5e16d4018adf3db5dac31d2d9c16b9f5.tar.gz
fork-ledger-34ee358f5e16d4018adf3db5dac31d2d9c16b9f5.tar.bz2
fork-ledger-34ee358f5e16d4018adf3db5dac31d2d9c16b9f5.zip
Moved journal reading code into journal_t
Diffstat (limited to 'src/journal.cc')
-rw-r--r--src/journal.cc97
1 files changed, 84 insertions, 13 deletions
diff --git a/src/journal.cc b/src/journal.cc
index 4d6e084e..31557635 100644
--- a/src/journal.cc
+++ b/src/journal.cc
@@ -41,23 +41,23 @@
namespace ledger {
journal_t::journal_t()
- : master(new account_t), basket(NULL), was_loaded(false),
- commodity_pool(new commodity_pool_t)
{
TRACE_CTOR(journal_t, "");
+ initialize();
+}
- // Add time commodity conversions, so that timelog's may be parsed
- // in terms of seconds, but reported as minutes or hours.
- if (commodity_t * commodity = commodity_pool->create("s"))
- commodity->add_flags(COMMODITY_BUILTIN | COMMODITY_NOMARKET);
- else
- assert(false);
+journal_t::journal_t(const path& pathname)
+{
+ TRACE_CTOR(journal_t, "path");
+ initialize();
+ read(pathname);
+}
- // Add a "percentile" commodity
- if (commodity_t * commodity = commodity_pool->create("%"))
- commodity->add_flags(COMMODITY_BUILTIN | COMMODITY_NOMARKET);
- else
- assert(false);
+journal_t::journal_t(const string& str)
+{
+ TRACE_CTOR(journal_t, "string");
+ initialize();
+ read(str);
}
journal_t::~journal_t()
@@ -80,6 +80,28 @@ journal_t::~journal_t()
commodity_pool.reset();
}
+void journal_t::initialize()
+{
+ master = new account_t;
+ basket = NULL;
+ was_loaded = false;
+
+ commodity_pool.reset(new commodity_pool_t);
+
+ // Add time commodity conversions, so that timelog's may be parsed
+ // in terms of seconds, but reported as minutes or hours.
+ if (commodity_t * commodity = commodity_pool->create("s"))
+ commodity->add_flags(COMMODITY_BUILTIN | COMMODITY_NOMARKET);
+ else
+ assert(false);
+
+ // Add a "percentile" commodity
+ if (commodity_t * commodity = commodity_pool->create("%"))
+ commodity->add_flags(COMMODITY_BUILTIN | COMMODITY_NOMARKET);
+ else
+ assert(false);
+}
+
void journal_t::add_account(account_t * acct)
{
master->add_account(acct);
@@ -134,6 +156,55 @@ bool journal_t::remove_xact(xact_t * xact)
return true;
}
+std::size_t journal_t::read(std::istream& in,
+ const path& pathname,
+ account_t * master_alt,
+ scope_t * scope)
+{
+ std::size_t count = 0;
+ try {
+ if (! scope)
+ scope = scope_t::default_scope;
+
+ if (! scope)
+ throw_(std::runtime_error,
+ _("No default scope in which to read journal file '%1'")
+ << pathname);
+
+ value_t strict = expr_t("strict").calc(*scope);
+
+ count = parse(in, *scope, master_alt ? master_alt : master,
+ &pathname, strict.to_boolean());
+ }
+ catch (...) {
+ clear_xdata();
+ throw;
+ }
+
+ // xdata may have been set for some accounts and transaction due to the use
+ // of balance assertions or other calculations performed in valexpr-based
+ // posting amounts.
+ clear_xdata();
+
+ return count;
+}
+
+std::size_t journal_t::read(const path& pathname,
+ account_t * master,
+ scope_t * scope)
+{
+ path filename = resolve_path(pathname);
+
+ if (! exists(filename))
+ throw_(std::runtime_error,
+ _("Cannot read journal file '%1'") << filename);
+
+ ifstream stream(filename);
+ std::size_t count = read(stream, filename, master, scope);
+ sources.push_back(fileinfo_t(filename));
+ return count;
+}
+
void journal_t::clear_xdata()
{
foreach (xact_t * xact, xacts)