Wired up the "entry" command from 2.x, though it still needs to be ported.

This commit is contained in:
John Wiegley 2009-02-10 22:37:05 -04:00
parent f605e107fc
commit 44518bc640
8 changed files with 72 additions and 107 deletions

View file

@ -116,8 +116,6 @@ pkginclude_HEADERS = \
src/output.h \
src/emacs.h \
src/help.h \
\
src/derive.h \
src/quotes.h \
\
src/global.h \

View file

@ -29,15 +29,27 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "derive.h"
#include "session.h"
#include "iterators.h"
#include "report.h"
#include "output.h"
namespace ledger {
entry_t * derive_new_entry(report_t& report,
strings_list::iterator i,
strings_list::iterator end)
value_t entry_command(call_scope_t& args)
{
report_t& report(find_scope<report_t>(args));
scoped_ptr<entry_t> entry(derive_new_entry(report, args.begin(),
args.end()));
xact_handler_ptr handler
(new format_xacts(report, report.HANDLER(print_format_).str()));
report.entry_report(handler, *entry.get());
return true;
}
entry_t * derive_new_entry(report_t& report,
value_t::sequence_t::const_iterator i,
value_t::sequence_t::const_iterator end)
{
session_t& session(report.session);
@ -45,11 +57,11 @@ entry_t * derive_new_entry(report_t& report,
entry_t * matching = NULL;
added->_date = parse_date(*i++);
added->_date = parse_date((*i++).to_string());
if (i == end)
throw std::runtime_error("Too few arguments to 'entry'");
mask_t regexp(*i++);
mask_t regexp((*i++).to_string());
entries_list::reverse_iterator j;
@ -64,15 +76,16 @@ entry_t * derive_new_entry(report_t& report,
added->payee = matching ? matching->payee : regexp.expr.str();
string arg = (*i).to_string();
if (! matching) {
account_t * acct;
if (i == end || ((*i)[0] == '-' || std::isdigit((*i)[0]))) {
if (i == end || (arg[0] == '-' || std::isdigit(arg[0]))) {
acct = session.master->find_account("Expenses");
}
else if (i != end) {
acct = session.master->find_account_re(*i);
acct = session.master->find_account_re(arg);
if (! acct)
acct = session.master->find_account(*i);
acct = session.master->find_account(arg);
assert(acct);
i++;
}
@ -80,7 +93,7 @@ entry_t * derive_new_entry(report_t& report,
if (i == end) {
added->add_xact(new xact_t(acct));
} else {
xact_t * xact = new xact_t(acct, amount_t(*i++));
xact_t * xact = new xact_t(acct, amount_t((*i++).to_string()));
added->add_xact(xact);
if (! xact->amount.commodity()) {
@ -102,9 +115,9 @@ entry_t * derive_new_entry(report_t& report,
if (i != end) {
if (! acct)
acct = session.master->find_account_re(*i);
acct = session.master->find_account_re((*i).to_string());
if (! acct)
acct = session.master->find_account(*i);
acct = session.master->find_account((*i).to_string());
}
if (! acct) {
@ -124,11 +137,11 @@ entry_t * derive_new_entry(report_t& report,
foreach (xact_t * xact, matching->xacts)
added->add_xact(new xact_t(*xact));
}
else if ((*i)[0] == '-' || std::isdigit((*i)[0])) {
else if (arg[0] == '-' || std::isdigit(arg[0])) {
xact_t * m_xact, * xact, * first;
m_xact = matching->xacts.front();
first = xact = new xact_t(m_xact->account, amount_t(*i++));
first = xact = new xact_t(m_xact->account, amount_t((*i++).to_string()));
added->add_xact(xact);
if (! xact->amount.commodity())
@ -140,9 +153,9 @@ entry_t * derive_new_entry(report_t& report,
added->add_xact(xact);
if (i != end) {
account_t * acct = session.master->find_account_re(*i);
account_t * acct = session.master->find_account_re((*i).to_string());
if (! acct)
acct = session.master->find_account(*i);
acct = session.master->find_account((*i).to_string());
assert(acct);
added->xacts.back()->account = acct;
}
@ -151,7 +164,7 @@ entry_t * derive_new_entry(report_t& report,
account_t * draw_acct = NULL;
while (i != end) {
string& re_pat(*i++);
string re_pat((*i++).to_string());
account_t * acct = NULL;
amount_t * amt = NULL;
@ -177,13 +190,13 @@ entry_t * derive_new_entry(report_t& report,
else
xact = new xact_t(acct);
} else {
amount_t amount(*i++);
amount_t amount((*i++).to_string());
strings_list::iterator x = i;
value_t::sequence_t::const_iterator x = i;
if (i != end && ++x == end) {
draw_acct = session.master->find_account_re(*i);
draw_acct = session.master->find_account_re((*i).to_string());
if (! draw_acct)
draw_acct = session.master->find_account(*i);
draw_acct = session.master->find_account((*i).to_string());
i++;
}

View file

@ -1,59 +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 extra
*/
/**
* @file derive.h
* @author John Wiegley
*
* @ingroup extra
*
* @brief Brief
*
* Long.
*/
#ifndef _DERIVE_H
#define _DERIVE_H
#include "report.h"
namespace ledger {
entry_t * derive_new_entry(report_t& report,
strings_list::iterator begin,
strings_list::iterator end);
} // namespace ledger
#endif // _DERIVE_H

View file

@ -80,11 +80,9 @@
#include <session.h>
#include <report.h>
#include <help.h>
#include <derive.h>
#include <quotes.h>
#include <emacs.h>
#include <help.h>
#if defined(HAVE_BOOST_PYTHON)
#include <pyinterp.h>

View file

@ -34,25 +34,6 @@
namespace ledger {
namespace {
string join_args(call_scope_t& args)
{
std::ostringstream buf;
bool first = true;
for (std::size_t i = 0; i < args.size(); i++) {
if (first) {
buf << args[i];
first = false;
} else {
buf << ' ' << args[i];
}
}
return buf.str();
}
}
value_t parse_command(call_scope_t& args)
{
string arg = join_args(args);

View file

@ -521,7 +521,7 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
(reporter<account_t, acct_handler_ptr, &report_t::accounts_report>
(new format_equity(*this, HANDLER(print_format_).str()), *this));
else if (is_eq(p, "entry"))
return NULL; // jww (2009-02-07): NYI
return WRAP_FUNCTOR(entry_command);
else if (is_eq(p, "emacs"))
return NULL; // jww (2009-02-07): NYI
break;
@ -631,4 +631,21 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return NULL;
}
string join_args(call_scope_t& args)
{
std::ostringstream buf;
bool first = true;
for (std::size_t i = 0; i < args.size(); i++) {
if (first) {
buf << args[i];
first = false;
} else {
buf << ' ' << args[i];
}
}
return buf.str();
}
} // namespace ledger

View file

@ -392,6 +392,16 @@ public:
OPTION(report_t, yearly); // -Y
};
// jww (2009-02-10): These should perhaps live elsewhere
value_t entry_command(call_scope_t& args);
entry_t * derive_new_entry(report_t& report,
value_t::sequence_t::const_iterator i,
value_t::sequence_t::const_iterator end);
string join_args(call_scope_t& args);
} // namespace ledger
#endif // _REPORT_H

View file

@ -182,6 +182,13 @@ public:
args.pop_back();
}
value_t::sequence_t::const_iterator begin() const {
return args.begin();
}
value_t::sequence_t::const_iterator end() const {
return args.end();
}
std::size_t size() const {
return args.size();
}