From 394c7bd8dfbe12e09b7fdee8d5c4072f4336d545 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sun, 8 Nov 2009 14:59:11 -0500 Subject: Removed a bunch of empty comments --- src/format.h | 9 --------- 1 file changed, 9 deletions(-) (limited to 'src/format.h') diff --git a/src/format.h b/src/format.h index 8b2a7965..8043594b 100644 --- a/src/format.h +++ b/src/format.h @@ -38,10 +38,6 @@ * @author John Wiegley * * @ingroup expr - * - * @brief Brief - * - * Long. */ #ifndef _FORMAT_H #define _FORMAT_H @@ -54,11 +50,6 @@ class unistring; DECLARE_EXCEPTION(format_error, std::runtime_error); -/** - * @brief Brief - * - * Long. - */ class format_t : public noncopyable { struct element_t : public supports_flags<> -- cgit v1.2.3 From fb8be53edb9d9fdd91fd906e9a4ce6c3e8e3adb3 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 9 Nov 2009 01:24:44 -0500 Subject: Redesigned the format_t class --- src/exprbase.h | 15 ++++++----- src/format.cc | 80 +++++++++++++++++++++++++++++++++------------------------- src/format.h | 40 +++++++++++++---------------- src/output.cc | 43 ++++++++++++++++--------------- src/precmd.cc | 2 +- src/scope.h | 3 ++- 6 files changed, 97 insertions(+), 86 deletions(-) (limited to 'src/format.h') diff --git a/src/exprbase.h b/src/exprbase.h index 605c30f7..d2bf5a6d 100644 --- a/src/exprbase.h +++ b/src/exprbase.h @@ -118,7 +118,8 @@ public: return str; } void set_text(const string& txt) { - str = txt; + str = txt; + compiled = false; } void parse(const string& str, const parse_flags_t& flags = PARSE_DEFAULT) { @@ -128,9 +129,7 @@ public: virtual void parse(std::istream&, const parse_flags_t& = PARSE_DEFAULT, const optional& original_string = none) { - str = original_string ? *original_string : ""; - context = NULL; - compiled = false; + set_text(original_string ? *original_string : ""); } void mark_uncompiled() { @@ -185,7 +184,9 @@ public: context = scope; } - virtual string context_to_str() const = 0; + virtual string context_to_str() const { + return empty_string; + } string print_to_str() const { std::ostringstream out; @@ -203,8 +204,8 @@ public: return out.str(); } - virtual void print(std::ostream& out) const = 0; - virtual void dump(std::ostream& out) const = 0; + virtual void print(std::ostream&) const {} + virtual void dump(std::ostream&) const {} result_type preview(std::ostream& out, scope_t& scope) const { out << _("--- Input expression ---") << std::endl; diff --git a/src/format.cc b/src/format.cc index f3f52f20..d949c350 100644 --- a/src/format.cc +++ b/src/format.cc @@ -61,8 +61,12 @@ void format_t::element_t::dump(std::ostream& out) const out << std::dec << int(max_width); switch (type) { - case STRING: out << " str: '" << chars << "'" << std::endl; break; - case EXPR: out << " expr: " << expr << std::endl; break; + case STRING: + out << " str: '" << boost::get(data) << "'" << std::endl; + break; + case EXPR: + out << " expr: " << boost::get(data) << std::endl; + break; } } @@ -117,7 +121,7 @@ format_t::element_t * format_t::parse_elements(const string& fmt, if (q != buf) { current->type = element_t::STRING; - current->chars = string(buf, q); + current->data = string(buf, q); q = buf; current->next.reset(new element_t); @@ -128,14 +132,14 @@ format_t::element_t * format_t::parse_elements(const string& fmt, p++; current->type = element_t::STRING; switch (*p) { - case 'b': current->chars = "\b"; break; - case 'f': current->chars = "\f"; break; - case 'n': current->chars = "\n"; break; - case 'r': current->chars = "\r"; break; - case 't': current->chars = "\t"; break; - case 'v': current->chars = "\v"; break; - case '\\': current->chars = "\\"; break; - default: current->chars = string(1, *p); break; + case 'b': current->data = string("\b"); break; + case 'f': current->data = string("\f"); break; + case 'n': current->data = string("\n"); break; + case 'r': current->data = string("\r"); break; + case 't': current->data = string("\t"); break; + case 'v': current->data = string("\v"); break; + case '\\': current->data = string("\\"); break; + default: current->data = string(1, *p); break; } continue; } @@ -171,8 +175,8 @@ format_t::element_t * format_t::parse_elements(const string& fmt, switch (*p) { case '%': - current->type = element_t::STRING; - current->chars = "%"; + current->type = element_t::STRING; + current->data = string("%"); break; case '$': { @@ -207,7 +211,7 @@ format_t::element_t * format_t::parse_elements(const string& fmt, if (format_amount) p++; current->type = element_t::EXPR; - current->expr = parse_single_expression(p, ! format_amount); + current->data = parse_single_expression(p, ! format_amount); // Wrap the subexpression in calls to justify and scrub if (format_amount) { @@ -216,7 +220,7 @@ format_t::element_t * format_t::parse_elements(const string& fmt, else p++; - expr_t::ptr_op_t op = current->expr.get_op(); + expr_t::ptr_op_t op = boost::get(current->data).get_op(); expr_t::ptr_op_t amount_op; expr_t::ptr_op_t colorize_op; @@ -238,8 +242,10 @@ format_t::element_t * format_t::parse_elements(const string& fmt, expr_t::ptr_op_t arg2_node(new expr_t::op_t(expr_t::op_t::VALUE)); expr_t::ptr_op_t arg3_node(new expr_t::op_t(expr_t::op_t::VALUE)); - arg1_node->set_value(current->min_width > 0 ? long(current->min_width) : -1); - arg2_node->set_value(current->max_width > 0 ? long(current->max_width) : -1); + arg1_node->set_value(current->min_width > 0 ? + long(current->min_width) : -1); + arg2_node->set_value(current->max_width > 0 ? + long(current->max_width) : -1); arg3_node->set_value(! current->has_flags(ELEMENT_ALIGN_LEFT)); current->min_width = 0; @@ -264,7 +270,7 @@ format_t::element_t * format_t::parse_elements(const string& fmt, call2_node->set_left(justify_node); call2_node->set_right(args3_node); - string prev_expr = current->expr.text(); + string prev_expr = boost::get(current->data).text(); if (colorize_op) { expr_t::ptr_op_t ansify_if_node(new expr_t::op_t(expr_t::op_t::IDENT)); @@ -278,21 +284,18 @@ format_t::element_t * format_t::parse_elements(const string& fmt, call3_node->set_left(ansify_if_node); call3_node->set_right(args4_node); - current->expr = expr_t(call3_node); + current->data = expr_t(call3_node); } else { - current->expr = expr_t(call2_node); + current->data = expr_t(call2_node); } - current->expr.set_text(prev_expr); + boost::get(current->data).set_text(prev_expr); } break; } default: - current->type = element_t::EXPR; - current->chars = string(FMT_PREFIX) + *p; - current->expr.parse(current->chars); - break; + throw_(format_error, _("Unrecognized formatting character: %1") << *p); } } @@ -304,15 +307,17 @@ format_t::element_t * format_t::parse_elements(const string& fmt, current->next.reset(new element_t); current = current->next.get(); } - current->type = element_t::STRING; - current->chars = string(buf, q); + current->type = element_t::STRING; + current->data = string(buf, q); } return result.release(); } -void format_t::format(std::ostream& out_str, scope_t& scope) +string format_t::real_calc(scope_t& scope) { + std::ostringstream out_str; + for (element_t * elem = elements.get(); elem; elem = elem->next.get()) { std::ostringstream out; string name; @@ -326,20 +331,22 @@ void format_t::format(std::ostream& out_str, scope_t& scope) case element_t::STRING: if (elem->min_width > 0) out.width(elem->min_width); - out << elem->chars; + out << boost::get(elem->data); break; - case element_t::EXPR: + case element_t::EXPR: { + expr_t& expr(boost::get(elem->data)); try { - elem->expr.compile(scope); + + expr.compile(scope); value_t value; - if (elem->expr.is_function()) { + if (expr.is_function()) { call_scope_t args(scope); args.push_back(long(elem->max_width)); - value = elem->expr.get_function()(args); + value = expr.get_function()(args); } else { - value = elem->expr.calc(scope); + value = expr.calc(scope); } DEBUG("format.expr", "value = (" << value << ")"); @@ -348,10 +355,11 @@ void format_t::format(std::ostream& out_str, scope_t& scope) } catch (const calc_error&) { add_error_context(_("While calculating format expression:")); - add_error_context(elem->expr.context_to_str()); + add_error_context(expr.context_to_str()); throw; } break; + } default: assert(false); @@ -375,6 +383,8 @@ void format_t::format(std::ostream& out_str, scope_t& scope) out_str << out.str(); } } + + return out_str.str(); } string format_t::truncate(const unistring& ustr, diff --git a/src/format.h b/src/format.h index 8043594b..516b6d7e 100644 --- a/src/format.h +++ b/src/format.h @@ -50,20 +50,20 @@ class unistring; DECLARE_EXCEPTION(format_error, std::runtime_error); -class format_t : public noncopyable +class format_t : public expr_base_t { + typedef expr_base_t base_type; + struct element_t : public supports_flags<> { #define ELEMENT_ALIGN_LEFT 0x01 enum kind_t { STRING, EXPR }; - kind_t type; - std::size_t min_width; - std::size_t max_width; - string chars; - expr_t expr; - + kind_t type; + std::size_t min_width; + std::size_t max_width; + variant data; scoped_ptr next; element_t() throw() @@ -83,8 +83,7 @@ class format_t : public noncopyable type = elem.type; min_width = elem.min_width; max_width = elem.max_width; - chars = elem.chars; - expr = elem.expr; + data = elem.data; } return *this; } @@ -124,25 +123,28 @@ private: const optional& tmpl); public: - format_t() { + format_t() : base_type() { TRACE_CTOR(format_t, ""); } - format_t(const string& _format) { + format_t(const string& _str, scope_t * context = NULL) + : base_type(context) { TRACE_CTOR(format_t, "const string&"); - parse(_format); + if (! _str.empty()) + parse_format(_str); } ~format_t() { TRACE_DTOR(format_t); } - void parse(const string& _format, const optional& tmpl = none) { + void parse_format(const string& _format, + const optional& tmpl = none) { elements.reset(parse_elements(_format, tmpl)); - format_string = _format; + set_text(_format); } - void format(std::ostream& out, scope_t& scope); + virtual result_type real_calc(scope_t& scope); - void dump(std::ostream& out) const { + virtual void dump(std::ostream& out) const { for (const element_t * elem = elements.get(); elem; elem = elem->next.get()) @@ -154,12 +156,6 @@ public: const std::size_t account_abbrev_length = 0); }; -#define FMT_PREFIX "fmt_" -#define FMT_PREFIX_LEN 4 - -#define WANT_FMT() \ - (std::strncmp(p, FMT_PREFIX, FMT_PREFIX_LEN) == 0) - } // namespace ledger #endif // _FORMAT_H diff --git a/src/output.cc b/src/output.cc index eee84553..2a6f0c20 100644 --- a/src/output.cc +++ b/src/output.cc @@ -51,17 +51,19 @@ format_posts::format_posts(report_t& _report, const char * f = format.c_str(); if (const char * p = std::strstr(f, "%/")) { - first_line_format.parse(string(f, 0, p - f)); + first_line_format.parse_format(string(f, 0, p - f)); const char * n = p + 2; if (const char * p = std::strstr(n, "%/")) { - next_lines_format.parse(string(n, 0, p - n), first_line_format); - between_format.parse(string(p + 2), first_line_format); + next_lines_format.parse_format(string(n, 0, p - n), + first_line_format); + between_format.parse_format(string(p + 2), + first_line_format); } else { - next_lines_format.parse(n, first_line_format); + next_lines_format.parse_format(string(n), first_line_format); } } else { - first_line_format.parse(format); - next_lines_format.parse(format); + first_line_format.parse_format(format); + next_lines_format.parse_format(format); } } @@ -80,7 +82,7 @@ void format_posts::operator()(post_t& post) if (last_xact != post.xact) { if (last_xact) { bind_scope_t xact_scope(report, *last_xact); - between_format.format(out, xact_scope); + out << between_format(xact_scope); } print_item(out, *post.xact); out << '\n'; @@ -96,16 +98,16 @@ void format_posts::operator()(post_t& post) if (last_xact != post.xact) { if (last_xact) { bind_scope_t xact_scope(report, *last_xact); - between_format.format(out, xact_scope); + out << between_format(xact_scope); } - first_line_format.format(out, bound_scope); + out << first_line_format(bound_scope); last_xact = post.xact; } else if (last_post && last_post->date() != post.date()) { - first_line_format.format(out, bound_scope); + out << first_line_format(bound_scope); } else { - next_lines_format.format(out, bound_scope); + out << next_lines_format(bound_scope); } post.xdata().add_flags(POST_EXT_DISPLAYED); @@ -122,17 +124,17 @@ format_accounts::format_accounts(report_t& _report, const char * f = format.c_str(); if (const char * p = std::strstr(f, "%/")) { - account_line_format.parse(string(f, 0, p - f)); + account_line_format.parse_format(string(f, 0, p - f)); const char * n = p + 2; if (const char * p = std::strstr(n, "%/")) { - total_line_format.parse(string(n, 0, p - n), account_line_format); - separator_format.parse(string(p + 2), account_line_format); + total_line_format.parse_format(string(n, 0, p - n), account_line_format); + separator_format.parse_format(string(p + 2), account_line_format); } else { - total_line_format.parse(n, account_line_format); + total_line_format.parse_format(n, account_line_format); } } else { - account_line_format.parse(format); - total_line_format.parse(format, account_line_format); + account_line_format.parse_format(format); + total_line_format.parse_format(format, account_line_format); } } @@ -148,7 +150,8 @@ std::size_t format_accounts::post_account(account_t& account, const bool flat) account.xdata().add_flags(ACCOUNT_EXT_DISPLAYED); bind_scope_t bound_scope(report, account); - account_line_format.format(report.output_stream, bound_scope); + static_cast(report.output_stream) + << account_line_format(bound_scope); return 1; } @@ -213,8 +216,8 @@ void format_accounts::flush() if (displayed > 1 && ! report.HANDLED(no_total) && ! report.HANDLED(percent)) { bind_scope_t bound_scope(report, *report.session.journal->master); - separator_format.format(out, bound_scope); - total_line_format.format(out, bound_scope); + out << separator_format(bound_scope); + out << total_line_format(bound_scope); } out.flush(); diff --git a/src/precmd.cc b/src/precmd.cc index 277e7b89..92483dc8 100644 --- a/src/precmd.cc +++ b/src/precmd.cc @@ -148,7 +148,7 @@ value_t format_command(call_scope_t& args) out << std::endl << _("--- Formatted string ---") << std::endl; bind_scope_t bound_scope(args, *post); out << '"'; - fmt.format(out, bound_scope); + out << fmt(bound_scope); out << "\"\n"; return NULL_VALUE; diff --git a/src/scope.h b/src/scope.h index cd7d93c7..44ca3229 100644 --- a/src/scope.h +++ b/src/scope.h @@ -54,7 +54,8 @@ struct symbol_t OPTION, PRECOMMAND, COMMAND, - DIRECTIVE + DIRECTIVE, + FORMAT }; kind_t kind; -- cgit v1.2.3 From 7411c74d6d5bea42cb9fa5b6b0ed90480c954a03 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 9 Nov 2009 01:36:26 -0500 Subject: Redesigned the draft_t class --- src/derive.cc | 559 ------------------------------------------------------ src/derive.h | 62 ------ src/draft.cc | 525 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/draft.h | 113 +++++++++++ src/format.h | 3 +- src/report.cc | 2 +- src/stats.cc | 2 +- tools/Makefile.am | 24 +-- 8 files changed, 653 insertions(+), 637 deletions(-) delete mode 100644 src/derive.cc delete mode 100644 src/derive.h create mode 100644 src/draft.cc create mode 100644 src/draft.h (limited to 'src/format.h') diff --git a/src/derive.cc b/src/derive.cc deleted file mode 100644 index 6f489003..00000000 --- a/src/derive.cc +++ /dev/null @@ -1,559 +0,0 @@ -/* - * Copyright (c) 2003-2009, John Wiegley. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of New Artisans LLC nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include - -#include "derive.h" -#include "xact.h" -#include "post.h" -#include "account.h" -#include "journal.h" -#include "session.h" -#include "report.h" -#include "output.h" - -namespace ledger { - -namespace { - struct xact_template_t - { - optional date; - optional code; - optional note; - mask_t payee_mask; - - struct post_template_t { - bool from; - optional account_mask; - optional amount; - optional cost_operator; - optional cost; - - post_template_t() : from(false) {} - }; - - std::list posts; - - xact_template_t() {} - - void dump(std::ostream& out) const - { - if (date) - out << _("Date: ") << *date << std::endl; - else - out << _("Date: ") << std::endl; - - if (code) - out << _("Code: ") << *code << std::endl; - if (note) - out << _("Note: ") << *note << std::endl; - - if (payee_mask.empty()) - out << _("Payee mask: INVALID (template expression will cause an error)") - << std::endl; - else - out << _("Payee mask: ") << payee_mask << std::endl; - - if (posts.empty()) { - out << std::endl - << _("") - << std::endl; - } else { - bool has_only_from = true; - bool has_only_to = true; - - foreach (const post_template_t& post, posts) { - if (post.from) - has_only_to = false; - else - has_only_from = false; - } - - foreach (const post_template_t& post, posts) { - straccstream accum; - out << std::endl - << ACCUM(accum << _("[Posting \"%1\"]") - << (post.from ? _("from") : _("to"))) - << std::endl; - - if (post.account_mask) - out << _(" Account mask: ") << *post.account_mask << std::endl; - else if (post.from) - out << _(" Account mask: ") << std::endl; - else - out << _(" Account mask: ") << std::endl; - - if (post.amount) - out << _(" Amount: ") << *post.amount << std::endl; - - if (post.cost) - out << _(" Cost: ") << *post.cost_operator - << " " << *post.cost << std::endl; - } - } - } - }; - - xact_template_t - args_to_xact_template(value_t::sequence_t::const_iterator begin, - value_t::sequence_t::const_iterator end) - { - regex date_mask(_("([0-9]+(?:[-/.][0-9]+)?(?:[-/.][0-9]+))?")); - smatch what; - - xact_template_t tmpl; - bool check_for_date = true; - - optional weekday; - xact_template_t::post_template_t * post = NULL; - - for (; begin != end; begin++) { - if (check_for_date && - regex_match((*begin).to_string(), what, date_mask)) { - tmpl.date = parse_date(what[0]); - check_for_date = false; - } - else if (check_for_date && - bool(weekday = string_to_day_of_week(what[0]))) { - short dow = static_cast(*weekday); - date_t date = CURRENT_DATE() - date_duration(1); - while (date.day_of_week() != dow) - date -= date_duration(1); - tmpl.date = date; - check_for_date = false; - } - else { - string arg = (*begin).to_string(); - - if (arg == "at") { - if (begin == end) - throw std::runtime_error(_("Invalid xact command arguments")); - tmpl.payee_mask = (*++begin).to_string(); - } - else if (arg == "to" || arg == "from") { - if (! post || post->account_mask) { - tmpl.posts.push_back(xact_template_t::post_template_t()); - post = &tmpl.posts.back(); - } - if (begin == end) - throw std::runtime_error(_("Invalid xact command arguments")); - post->account_mask = mask_t((*++begin).to_string()); - post->from = arg == "from"; - } - else if (arg == "on") { - if (begin == end) - throw std::runtime_error(_("Invalid xact command arguments")); - tmpl.date = parse_date((*++begin).to_string()); - check_for_date = false; - } - else if (arg == "code") { - if (begin == end) - throw std::runtime_error(_("Invalid xact command arguments")); - tmpl.code = (*++begin).to_string(); - } - else if (arg == "note") { - if (begin == end) - throw std::runtime_error(_("Invalid xact command arguments")); - tmpl.note = (*++begin).to_string(); - } - else if (arg == "rest") { - ; // just ignore this argument - } - else if (arg == "@" || arg == "@@") { - amount_t cost; - post->cost_operator = arg; - if (begin == end) - throw std::runtime_error(_("Invalid xact command arguments")); - arg = (*++begin).to_string(); - if (! cost.parse(arg, PARSE_SOFT_FAIL | PARSE_NO_MIGRATE)) - throw std::runtime_error(_("Invalid xact command arguments")); - post->cost = cost; - } - else { - // Without a preposition, it is either: - // - // A payee, if we have not seen one - // An account or an amount, if we have - // An account if an amount has just been seen - // An amount if an account has just been seen - - if (tmpl.payee_mask.empty()) { - tmpl.payee_mask = arg; - } - else { - amount_t amt; - optional account; - - if (! amt.parse(arg, PARSE_SOFT_FAIL | PARSE_NO_MIGRATE)) - account = mask_t(arg); - - if (! post || - (account && post->account_mask) || - (! account && post->amount)) { - tmpl.posts.push_back(xact_template_t::post_template_t()); - post = &tmpl.posts.back(); - } - - if (account) { - post->from = false; - post->account_mask = account; - } else { - post->amount = amt; - } - } - } - } - } - - if (! tmpl.posts.empty()) { - bool has_only_from = true; - bool has_only_to = true; - - // A single account at the end of the line is the "from" account - if (tmpl.posts.size() > 1 && - tmpl.posts.back().account_mask && ! tmpl.posts.back().amount) - tmpl.posts.back().from = true; - - foreach (xact_template_t::post_template_t& post, tmpl.posts) { - if (post.from) - has_only_to = false; - else - has_only_from = false; - } - - if (has_only_from) { - tmpl.posts.push_front(xact_template_t::post_template_t()); - } - else if (has_only_to) { - tmpl.posts.push_back(xact_template_t::post_template_t()); - tmpl.posts.back().from = true; - } - } - - return tmpl; - } - - xact_t * derive_xact_from_template(xact_template_t& tmpl, - report_t& report) - { - if (tmpl.payee_mask.empty()) - throw std::runtime_error(_("xact' command requires at least a payee")); - - xact_t * matching = NULL; - journal_t& journal(*report.session.journal.get()); - std::auto_ptr added(new xact_t); - - for (xacts_list::reverse_iterator j = journal.xacts.rbegin(); - j != journal.xacts.rend(); - j++) { - if (tmpl.payee_mask.match((*j)->payee)) { - matching = *j; - DEBUG("derive.xact", - "Found payee match: transaction on line " << (*j)->pos->beg_line); - break; - } - } - - if (! tmpl.date) { - added->_date = CURRENT_DATE(); - DEBUG("derive.xact", "Setting date to current date"); - } else { - added->_date = tmpl.date; - DEBUG("derive.xact", "Setting date to template date: " << *tmpl.date); - } - - added->set_state(item_t::UNCLEARED); - - if (matching) { - added->payee = matching->payee; - added->code = matching->code; - added->note = matching->note; - -#if defined(DEBUG_ON) - DEBUG("derive.xact", "Setting payee from match: " << added->payee); - if (added->code) - DEBUG("derive.xact", "Setting code from match: " << *added->code); - if (added->note) - DEBUG("derive.xact", "Setting note from match: " << *added->note); -#endif - } else { - added->payee = tmpl.payee_mask.str(); - DEBUG("derive.xact", "Setting payee from template: " << added->payee); - } - - if (tmpl.code) { - added->code = tmpl.code; - DEBUG("derive.xact", "Now setting code from template: " << *added->code); - } - if (tmpl.note) { - added->note = tmpl.note; - DEBUG("derive.xact", "Now setting note from template: " << *added->note); - } - - if (tmpl.posts.empty()) { - if (matching) { - DEBUG("derive.xact", "Template had no postings, copying from match"); - - foreach (post_t * post, matching->posts) { - added->add_post(new post_t(*post)); - added->posts.back()->set_state(item_t::UNCLEARED); - } - } else { - throw_(std::runtime_error, - _("No accounts, and no past transaction matching '%1'") - << tmpl.payee_mask); - } - } else { - DEBUG("derive.xact", "Template had postings"); - - bool any_post_has_amount = false; - foreach (xact_template_t::post_template_t& post, tmpl.posts) { - if (post.amount) { - DEBUG("derive.xact", " and at least one has an amount specified"); - any_post_has_amount = true; - break; - } - } - - foreach (xact_template_t::post_template_t& post, tmpl.posts) { - std::auto_ptr new_post; - - commodity_t * found_commodity = NULL; - - if (matching) { - if (post.account_mask) { - DEBUG("derive.xact", - "Looking for matching posting based on account mask"); - - foreach (post_t * x, matching->posts) { - if (post.account_mask->match(x->account->fullname())) { - new_post.reset(new post_t(*x)); - DEBUG("derive.xact", - "Founding posting from line " << x->pos->beg_line); - break; - } - } - } else { - if (post.from) { - for (posts_list::reverse_iterator j = matching->posts.rbegin(); - j != matching->posts.rend(); - j++) { - if ((*j)->must_balance()) { - new_post.reset(new post_t(**j)); - DEBUG("derive.xact", - "Copied last real posting from matching"); - break; - } - } - } else { - for (posts_list::iterator j = matching->posts.begin(); - j != matching->posts.end(); - j++) { - if ((*j)->must_balance()) { - new_post.reset(new post_t(**j)); - DEBUG("derive.xact", - "Copied first real posting from matching"); - break; - } - } - } - } - } - - if (! new_post.get()) { - new_post.reset(new post_t); - DEBUG("derive.xact", "New posting was NULL, creating a blank one"); - } - - if (! new_post->account) { - DEBUG("derive.xact", "New posting still needs an account"); - - if (post.account_mask) { - DEBUG("derive.xact", "The template has an account mask"); - - account_t * acct = NULL; - if (! acct) { - acct = journal.find_account_re(post.account_mask->str()); -#if defined(DEBUG_ON) - if (acct) - DEBUG("derive.xact", "Found account as a regular expression"); -#endif - } - if (! acct) { - acct = journal.find_account(post.account_mask->str()); -#if defined(DEBUG_ON) - if (acct) - DEBUG("derive.xact", "Found (or created) account by name"); -#endif - } - - // Find out the default commodity to use by looking at the last - // commodity used in that account - for (xacts_list::reverse_iterator j = journal.xacts.rbegin(); - j != journal.xacts.rend(); - j++) { - foreach (post_t * x, (*j)->posts) { - if (x->account == acct && ! x->amount.is_null()) { - new_post.reset(new post_t(*x)); - DEBUG("derive.xact", - "Found account in journal postings, setting new posting"); - break; - } - } - } - - new_post->account = acct; - DEBUG("derive.xact", - "Set new posting's account to: " << acct->fullname()); - } else { - if (post.from) { - new_post->account = journal.find_account(_("Liabilities:Unknown")); - DEBUG("derive.xact", - "Set new posting's account to: Liabilities:Unknown"); - } else { - new_post->account = journal.find_account(_("Expenses:Unknown")); - DEBUG("derive.xact", - "Set new posting's account to: Expenses:Unknown"); - } - } - } - - if (new_post.get() && ! new_post->amount.is_null()) { - found_commodity = &new_post->amount.commodity(); - - if (any_post_has_amount) { - new_post->amount = amount_t(); - DEBUG("derive.xact", "New posting has an amount, but we cleared it"); - } else { - any_post_has_amount = true; - DEBUG("derive.xact", "New posting has an amount, and we're using it"); - } - } - - if (post.amount) { - new_post->amount = *post.amount; - DEBUG("derive.xact", "Copied over posting amount"); - - if (post.from) { - new_post->amount.in_place_negate(); - DEBUG("derive.xact", "Negated new posting amount"); - } - } - - if (post.cost) { - if (post.cost->sign() < 0) - throw parse_error(_("A posting's cost may not be negative")); - - post.cost->in_place_unround(); - - if (*post.cost_operator == "@") { - // For the sole case where the cost might be uncommoditized, - // guarantee that the commodity of the cost after multiplication - // is the same as it was before. - commodity_t& cost_commodity(post.cost->commodity()); - *post.cost *= new_post->amount; - post.cost->set_commodity(cost_commodity); - } - - new_post->cost = *post.cost; - DEBUG("derive.xact", "Copied over posting cost"); - } - - if (found_commodity && - ! new_post->amount.is_null() && - ! new_post->amount.has_commodity()) { - new_post->amount.set_commodity(*found_commodity); - DEBUG("derive.xact", "Set posting amount commodity to: " - << new_post->amount.commodity()); - - new_post->amount = new_post->amount.rounded(); - DEBUG("derive.xact", - "Rounded posting amount to: " << new_post->amount); - } - - added->add_post(new_post.release()); - added->posts.back()->set_state(item_t::UNCLEARED); - - DEBUG("derive.xact", "Added new posting to derived entry"); - } - } - - if (! journal.xact_finalize_hooks.run_hooks(*added.get(), false) || - ! added->finalize() || - ! journal.xact_finalize_hooks.run_hooks(*added.get(), true)) { - throw_(std::runtime_error, - _("Failed to finalize derived transaction (check commodities)")); - } - - return added.release(); - } -} - -value_t template_command(call_scope_t& args) -{ - report_t& report(find_scope(args)); - std::ostream& out(report.output_stream); - - value_t::sequence_t::const_iterator begin = args.value().begin(); - value_t::sequence_t::const_iterator end = args.value().end(); - - out << _("--- Input arguments ---") << std::endl; - args.value().dump(out); - out << std::endl << std::endl; - - xact_template_t tmpl = args_to_xact_template(begin, end); - - out << _("--- Transaction template ---") << std::endl; - tmpl.dump(out); - - return true; -} - -value_t xact_command(call_scope_t& args) -{ - value_t::sequence_t::const_iterator begin = args.value().begin(); - value_t::sequence_t::const_iterator end = args.value().end(); - - report_t& report(find_scope(args)); - xact_template_t tmpl = args_to_xact_template(begin, end); - std::auto_ptr new_xact(derive_xact_from_template(tmpl, report)); - - // Only consider actual postings for the "xact" command - report.HANDLER(limit_).on(string("#xact"), "actual"); - - report.xact_report(post_handler_ptr - (new format_posts(report, - report.HANDLER(print_format_).str())), - *new_xact.get()); - return true; -} - -} // namespace ledger diff --git a/src/derive.h b/src/derive.h deleted file mode 100644 index 0fb071a3..00000000 --- a/src/derive.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2003-2009, John Wiegley. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * - Neither the name of New Artisans LLC nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @addtogroup derive - */ - -/** - * @file derive.h - * @author John Wiegley - * - * @ingroup report - */ -#ifndef _DERIVE_H -#define _DERIVE_H - -#include "value.h" - -namespace ledger { - -class call_scope_t; - -value_t xact_command(call_scope_t& args); -value_t template_command(call_scope_t& args); - -class xact_t; -class report_t; -xact_t * derive_new_xact(report_t& report, - value_t::sequence_t::const_iterator i, - value_t::sequence_t::const_iterator end); - -} // namespace ledger - -#endif // _DERIVE_H diff --git a/src/draft.cc b/src/draft.cc new file mode 100644 index 00000000..b4e23322 --- /dev/null +++ b/src/draft.cc @@ -0,0 +1,525 @@ +/* + * Copyright (c) 2003-2009, John Wiegley. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of New Artisans LLC nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include "draft.h" +#include "xact.h" +#include "post.h" +#include "account.h" +#include "journal.h" +#include "session.h" +#include "report.h" +#include "output.h" + +namespace ledger { + +void draft_t::xact_template_t::dump(std::ostream& out) const +{ + if (date) + out << _("Date: ") << *date << std::endl; + else + out << _("Date: ") << std::endl; + + if (code) + out << _("Code: ") << *code << std::endl; + if (note) + out << _("Note: ") << *note << std::endl; + + if (payee_mask.empty()) + out << _("Payee mask: INVALID (template expression will cause an error)") + << std::endl; + else + out << _("Payee mask: ") << payee_mask << std::endl; + + if (posts.empty()) { + out << std::endl + << _("") + << std::endl; + } else { + bool has_only_from = true; + bool has_only_to = true; + + foreach (const post_template_t& post, posts) { + if (post.from) + has_only_to = false; + else + has_only_from = false; + } + + foreach (const post_template_t& post, posts) { + straccstream accum; + out << std::endl + << ACCUM(accum << _("[Posting \"%1\"]") + << (post.from ? _("from") : _("to"))) + << std::endl; + + if (post.account_mask) + out << _(" Account mask: ") << *post.account_mask << std::endl; + else if (post.from) + out << _(" Account mask: ") << std::endl; + else + out << _(" Account mask: ") << std::endl; + + if (post.amount) + out << _(" Amount: ") << *post.amount << std::endl; + + if (post.cost) + out << _(" Cost: ") << *post.cost_operator + << " " << *post.cost << std::endl; + } + } +} + +void draft_t::parse_args(const value_t& args) +{ + regex date_mask(_("([0-9]+(?:[-/.][0-9]+)?(?:[-/.][0-9]+))?")); + smatch what; + bool check_for_date = true; + + tmpl = xact_template_t(); + + optional weekday; + xact_template_t::post_template_t * post = NULL; + + value_t::sequence_t::const_iterator begin = args.begin(); + value_t::sequence_t::const_iterator end = args.end(); + + for (; begin != end; begin++) { + if (check_for_date && + regex_match((*begin).to_string(), what, date_mask)) { + tmpl->date = parse_date(what[0]); + check_for_date = false; + } + else if (check_for_date && + bool(weekday = string_to_day_of_week(what[0]))) { + short dow = static_cast(*weekday); + date_t date = CURRENT_DATE() - date_duration(1); + while (date.day_of_week() != dow) + date -= date_duration(1); + tmpl->date = date; + check_for_date = false; + } + else { + string arg = (*begin).to_string(); + + if (arg == "at") { + if (begin == end) + throw std::runtime_error(_("Invalid xact command arguments")); + tmpl->payee_mask = (*++begin).to_string(); + } + else if (arg == "to" || arg == "from") { + if (! post || post->account_mask) { + tmpl->posts.push_back(xact_template_t::post_template_t()); + post = &tmpl->posts.back(); + } + if (begin == end) + throw std::runtime_error(_("Invalid xact command arguments")); + post->account_mask = mask_t((*++begin).to_string()); + post->from = arg == "from"; + } + else if (arg == "on") { + if (begin == end) + throw std::runtime_error(_("Invalid xact command arguments")); + tmpl->date = parse_date((*++begin).to_string()); + check_for_date = false; + } + else if (arg == "code") { + if (begin == end) + throw std::runtime_error(_("Invalid xact command arguments")); + tmpl->code = (*++begin).to_string(); + } + else if (arg == "note") { + if (begin == end) + throw std::runtime_error(_("Invalid xact command arguments")); + tmpl->note = (*++begin).to_string(); + } + else if (arg == "rest") { + ; // just ignore this argument + } + else if (arg == "@" || arg == "@@") { + amount_t cost; + post->cost_operator = arg; + if (begin == end) + throw std::runtime_error(_("Invalid xact command arguments")); + arg = (*++begin).to_string(); + if (! cost.parse(arg, PARSE_SOFT_FAIL | PARSE_NO_MIGRATE)) + throw std::runtime_error(_("Invalid xact command arguments")); + post->cost = cost; + } + else { + // Without a preposition, it is either: + // + // A payee, if we have not seen one + // An account or an amount, if we have + // An account if an amount has just been seen + // An amount if an account has just been seen + + if (tmpl->payee_mask.empty()) { + tmpl->payee_mask = arg; + } + else { + amount_t amt; + optional account; + + if (! amt.parse(arg, PARSE_SOFT_FAIL | PARSE_NO_MIGRATE)) + account = mask_t(arg); + + if (! post || + (account && post->account_mask) || + (! account && post->amount)) { + tmpl->posts.push_back(xact_template_t::post_template_t()); + post = &tmpl->posts.back(); + } + + if (account) { + post->from = false; + post->account_mask = account; + } else { + post->amount = amt; + } + } + } + } + } + + if (! tmpl->posts.empty()) { + bool has_only_from = true; + bool has_only_to = true; + + // A single account at the end of the line is the "from" account + if (tmpl->posts.size() > 1 && + tmpl->posts.back().account_mask && ! tmpl->posts.back().amount) + tmpl->posts.back().from = true; + + foreach (xact_template_t::post_template_t& post, tmpl->posts) { + if (post.from) + has_only_to = false; + else + has_only_from = false; + } + + if (has_only_from) { + tmpl->posts.push_front(xact_template_t::post_template_t()); + } + else if (has_only_to) { + tmpl->posts.push_back(xact_template_t::post_template_t()); + tmpl->posts.back().from = true; + } + } +} + +xact_t * draft_t::insert(journal_t& journal) +{ + if (tmpl->payee_mask.empty()) + throw std::runtime_error(_("xact' command requires at least a payee")); + + xact_t * matching = NULL; + + std::auto_ptr added(new xact_t); + + for (xacts_list::reverse_iterator j = journal.xacts.rbegin(); + j != journal.xacts.rend(); + j++) { + if (tmpl->payee_mask.match((*j)->payee)) { + matching = *j; + DEBUG("derive.xact", + "Found payee match: transaction on line " << (*j)->pos->beg_line); + break; + } + } + + if (! tmpl->date) { + added->_date = CURRENT_DATE(); + DEBUG("derive.xact", "Setting date to current date"); + } else { + added->_date = tmpl->date; + DEBUG("derive.xact", "Setting date to template date: " << *tmpl->date); + } + + added->set_state(item_t::UNCLEARED); + + if (matching) { + added->payee = matching->payee; + added->code = matching->code; + added->note = matching->note; + +#if defined(DEBUG_ON) + DEBUG("derive.xact", "Setting payee from match: " << added->payee); + if (added->code) + DEBUG("derive.xact", "Setting code from match: " << *added->code); + if (added->note) + DEBUG("derive.xact", "Setting note from match: " << *added->note); +#endif + } else { + added->payee = tmpl->payee_mask.str(); + DEBUG("derive.xact", "Setting payee from template: " << added->payee); + } + + if (tmpl->code) { + added->code = tmpl->code; + DEBUG("derive.xact", "Now setting code from template: " << *added->code); + } + if (tmpl->note) { + added->note = tmpl->note; + DEBUG("derive.xact", "Now setting note from template: " << *added->note); + } + + if (tmpl->posts.empty()) { + if (matching) { + DEBUG("derive.xact", "Template had no postings, copying from match"); + + foreach (post_t * post, matching->posts) { + added->add_post(new post_t(*post)); + added->posts.back()->set_state(item_t::UNCLEARED); + } + } else { + throw_(std::runtime_error, + _("No accounts, and no past transaction matching '%1'") + << tmpl->payee_mask); + } + } else { + DEBUG("derive.xact", "Template had postings"); + + bool any_post_has_amount = false; + foreach (xact_template_t::post_template_t& post, tmpl->posts) { + if (post.amount) { + DEBUG("derive.xact", " and at least one has an amount specified"); + any_post_has_amount = true; + break; + } + } + + foreach (xact_template_t::post_template_t& post, tmpl->posts) { + std::auto_ptr new_post; + + commodity_t * found_commodity = NULL; + + if (matching) { + if (post.account_mask) { + DEBUG("derive.xact", + "Looking for matching posting based on account mask"); + + foreach (post_t * x, matching->posts) { + if (post.account_mask->match(x->account->fullname())) { + new_post.reset(new post_t(*x)); + DEBUG("derive.xact", + "Founding posting from line " << x->pos->beg_line); + break; + } + } + } else { + if (post.from) { + for (posts_list::reverse_iterator j = matching->posts.rbegin(); + j != matching->posts.rend(); + j++) { + if ((*j)->must_balance()) { + new_post.reset(new post_t(**j)); + DEBUG("derive.xact", + "Copied last real posting from matching"); + break; + } + } + } else { + for (posts_list::iterator j = matching->posts.begin(); + j != matching->posts.end(); + j++) { + if ((*j)->must_balance()) { + new_post.reset(new post_t(**j)); + DEBUG("derive.xact", + "Copied first real posting from matching"); + break; + } + } + } + } + } + + if (! new_post.get()) { + new_post.reset(new post_t); + DEBUG("derive.xact", "New posting was NULL, creating a blank one"); + } + + if (! new_post->account) { + DEBUG("derive.xact", "New posting still needs an account"); + + if (post.account_mask) { + DEBUG("derive.xact", "The template has an account mask"); + + account_t * acct = NULL; + if (! acct) { + acct = journal.find_account_re(post.account_mask->str()); +#if defined(DEBUG_ON) + if (acct) + DEBUG("derive.xact", "Found account as a regular expression"); +#endif + } + if (! acct) { + acct = journal.find_account(post.account_mask->str()); +#if defined(DEBUG_ON) + if (acct) + DEBUG("derive.xact", "Found (or created) account by name"); +#endif + } + + // Find out the default commodity to use by looking at the last + // commodity used in that account + for (xacts_list::reverse_iterator j = journal.xacts.rbegin(); + j != journal.xacts.rend(); + j++) { + foreach (post_t * x, (*j)->posts) { + if (x->account == acct && ! x->amount.is_null()) { + new_post.reset(new post_t(*x)); + DEBUG("derive.xact", + "Found account in journal postings, setting new posting"); + break; + } + } + } + + new_post->account = acct; + DEBUG("derive.xact", + "Set new posting's account to: " << acct->fullname()); + } else { + if (post.from) { + new_post->account = journal.find_account(_("Liabilities:Unknown")); + DEBUG("derive.xact", + "Set new posting's account to: Liabilities:Unknown"); + } else { + new_post->account = journal.find_account(_("Expenses:Unknown")); + DEBUG("derive.xact", + "Set new posting's account to: Expenses:Unknown"); + } + } + } + + if (new_post.get() && ! new_post->amount.is_null()) { + found_commodity = &new_post->amount.commodity(); + + if (any_post_has_amount) { + new_post->amount = amount_t(); + DEBUG("derive.xact", "New posting has an amount, but we cleared it"); + } else { + any_post_has_amount = true; + DEBUG("derive.xact", "New posting has an amount, and we're using it"); + } + } + + if (post.amount) { + new_post->amount = *post.amount; + DEBUG("derive.xact", "Copied over posting amount"); + + if (post.from) { + new_post->amount.in_place_negate(); + DEBUG("derive.xact", "Negated new posting amount"); + } + } + + if (post.cost) { + if (post.cost->sign() < 0) + throw parse_error(_("A posting's cost may not be negative")); + + post.cost->in_place_unround(); + + if (*post.cost_operator == "@") { + // For the sole case where the cost might be uncommoditized, + // guarantee that the commodity of the cost after multiplication + // is the same as it was before. + commodity_t& cost_commodity(post.cost->commodity()); + *post.cost *= new_post->amount; + post.cost->set_commodity(cost_commodity); + } + + new_post->cost = *post.cost; + DEBUG("derive.xact", "Copied over posting cost"); + } + + if (found_commodity && + ! new_post->amount.is_null() && + ! new_post->amount.has_commodity()) { + new_post->amount.set_commodity(*found_commodity); + DEBUG("derive.xact", "Set posting amount commodity to: " + << new_post->amount.commodity()); + + new_post->amount = new_post->amount.rounded(); + DEBUG("derive.xact", + "Rounded posting amount to: " << new_post->amount); + } + + added->add_post(new_post.release()); + added->posts.back()->set_state(item_t::UNCLEARED); + + DEBUG("derive.xact", "Added new posting to derived entry"); + } + } + + if (! journal.add_xact(added.get())) + throw_(std::runtime_error, + _("Failed to finalize derived transaction (check commodities)")); + + return added.release(); +} + +value_t template_command(call_scope_t& args) +{ + report_t& report(find_scope(args)); + std::ostream& out(report.output_stream); + + out << _("--- Input arguments ---") << std::endl; + args.value().dump(out); + out << std::endl << std::endl; + + draft_t draft(args.value()); + + out << _("--- Transaction template ---") << std::endl; + draft.dump(out); + + return true; +} + +value_t xact_command(call_scope_t& args) +{ + report_t& report(find_scope(args)); + draft_t draft(args.value()); + + xact_t * new_xact = draft.insert(*report.session.journal.get()); + + // Only consider actual postings for the "xact" command + report.HANDLER(limit_).on(string("#xact"), "actual"); + + report.xact_report(post_handler_ptr + (new format_posts(report, + report.HANDLER(print_format_).str())), + *new_xact); + return true; +} + +} // namespace ledger diff --git a/src/draft.h b/src/draft.h new file mode 100644 index 00000000..faefa67b --- /dev/null +++ b/src/draft.h @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2003-2009, John Wiegley. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of New Artisans LLC nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @addtogroup derive + */ + +/** + * @file derive.h + * @author John Wiegley + * + * @ingroup report + */ +#ifndef _DERIVE_H +#define _DERIVE_H + +#include "exprbase.h" +#include "value.h" + +namespace ledger { + +class journal_t; +class xact_t; + +class draft_t : public expr_base_t +{ + typedef expr_base_t base_type; + + struct xact_template_t + { + optional date; + optional code; + optional note; + mask_t payee_mask; + + struct post_template_t { + bool from; + optional account_mask; + optional amount; + optional cost_operator; + optional cost; + + post_template_t() : from(false) {} + }; + + std::list posts; + + xact_template_t() {} + + void dump(std::ostream& out) const; + }; + + optional tmpl; + +public: + draft_t(const value_t& args) : base_type() { + TRACE_CTOR(draft_t, "value_t"); + if (! args.empty()) + parse_args(args); + } + ~draft_t() { + TRACE_DTOR(draft_t); + } + + void parse_args(const value_t& args); + + virtual result_type real_calc(scope_t&) { + assert(0); + return true; + } + + xact_t * insert(journal_t& journal); + + virtual void dump(std::ostream& out) const { + if (tmpl) + tmpl->dump(out); + } +}; + +value_t xact_command(call_scope_t& args); +value_t template_command(call_scope_t& args); + +} // namespace ledger + +#endif // _DERIVE_H diff --git a/src/format.h b/src/format.h index 516b6d7e..b3ae464f 100644 --- a/src/format.h +++ b/src/format.h @@ -105,8 +105,7 @@ class format_t : public expr_base_t void dump(std::ostream& out) const; }; - string format_string; - scoped_ptr elements; + scoped_ptr elements; public: static enum elision_style_t { diff --git a/src/report.cc b/src/report.cc index c0b5d745..3da71616 100644 --- a/src/report.cc +++ b/src/report.cc @@ -42,7 +42,7 @@ #include "precmd.h" #include "stats.h" #include "generate.h" -#include "derive.h" +#include "draft.h" #include "emacs.h" namespace ledger { diff --git a/src/stats.cc b/src/stats.cc index e2db9d8b..b89a5949 100644 --- a/src/stats.cc +++ b/src/stats.cc @@ -31,7 +31,7 @@ #include -#include "derive.h" +#include "draft.h" #include "xact.h" #include "post.h" #include "account.h" diff --git a/tools/Makefile.am b/tools/Makefile.am index 96776abf..a41c47ce 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -1,4 +1,4 @@ -VERSION = 3.0 +VERSION = 3.0.0 ACLOCAL_AMFLAGS = -I m4 dist_man_MANS = doc/ledger.1 SUBDIRS = po intl @@ -25,9 +25,9 @@ libledger_util_la_SOURCES = \ lib/sha1.cpp libledger_util_la_CPPFLAGS = $(lib_cppflags) -libledger_util_la_LDFLAGS = -release $(VERSION).0 +libledger_util_la_LDFLAGS = -release $(VERSION) -libledger_math_la_SOURCES = \ +libledger_math_la_SOURCES = \ src/value.cc \ src/balance.cc \ src/quotes.cc \ @@ -37,9 +37,9 @@ libledger_math_la_SOURCES = \ src/amount.cc libledger_math_la_CPPFLAGS = $(lib_cppflags) -libledger_math_la_LDFLAGS = -release $(VERSION).0 +libledger_math_la_LDFLAGS = -release $(VERSION) -libledger_expr_la_SOURCES = \ +libledger_expr_la_SOURCES = \ src/option.cc \ src/format.cc \ src/query.cc \ @@ -51,9 +51,9 @@ libledger_expr_la_SOURCES = \ src/token.cc libledger_expr_la_CPPFLAGS = $(lib_cppflags) -libledger_expr_la_LDFLAGS = -release $(VERSION).0 +libledger_expr_la_LDFLAGS = -release $(VERSION) -libledger_data_la_SOURCES = \ +libledger_data_la_SOURCES = \ src/compare.cc \ src/iterators.cc \ src/timelog.cc \ @@ -65,13 +65,13 @@ libledger_data_la_SOURCES = \ src/post.cc \ src/item.cc -libledger_data_la_CPPFLAGS = $(lib_cppflags) -libledger_data_la_LDFLAGS = -release $(VERSION).0 +libledger_data_la_CPPFLAGS = $(lib_cppflags) +libledger_data_la_LDFLAGS = -release $(VERSION) libledger_report_la_SOURCES = \ src/stats.cc \ src/generate.cc \ - src/derive.cc \ + src/draft.cc \ src/emacs.cc \ src/output.cc \ src/precmd.cc \ @@ -82,7 +82,7 @@ libledger_report_la_SOURCES = \ src/session.cc libledger_report_la_CPPFLAGS = $(lib_cppflags) -libledger_report_la_LDFLAGS = -release $(VERSION).0 +libledger_report_la_LDFLAGS = -release $(VERSION) pkginclude_HEADERS = \ src/utils.h \ @@ -132,7 +132,7 @@ pkginclude_HEADERS = \ src/temps.h \ src/chain.h \ src/precmd.h \ - src/derive.h \ + src/draft.h \ src/generate.h \ src/stats.h \ src/output.h \ -- cgit v1.2.3