Added a new --raw option, for use with print

This commit is contained in:
John Wiegley 2009-02-21 19:45:13 -04:00
parent dc63429785
commit a577e8c48e
6 changed files with 67 additions and 32 deletions

View file

@ -172,6 +172,11 @@ See \fB\-\-leeway\fR.
.It Fl \-print-format Ar FMT .It Fl \-print-format Ar FMT
.It Fl \-quantity Pq Fl O .It Fl \-quantity Pq Fl O
.It Fl \-quarterly .It Fl \-quarterly
.It Fl \-raw
For use only with the
.Nm print
command, it causes Ledger to print out matching entries exactly as they
appeared in the original journal file.
.It Fl \-real Pq Fl R .It Fl \-real Pq Fl R
.It Fl \-register-format Ar FMT .It Fl \-register-format Ar FMT
.It Fl \-related Pq Fl r .It Fl \-related Pq Fl r

View file

@ -359,6 +359,32 @@ bool item_t::valid() const
return true; return true;
} }
void print_item(std::ostream& out,
const item_t& item,
const string& prefix)
{
std::size_t len = item.end_pos - item.beg_pos;
ifstream in(item.pathname);
in.seekg(item.beg_pos, std::ios::beg);
scoped_array<char> buf(new char[len + 1]);
in.read(buf.get(), len);
assert(static_cast<std::size_t>(in.gcount()) == len);
buf[len] = '\0';
bool first = true;
for (char * p = std::strtok(buf.get(), "\n");
p;
p = std::strtok(NULL, "\n")) {
if (first)
first = false;
else
out << '\n';
out << prefix << p;
}
}
string item_context(const item_t& item, const string& desc) string item_context(const item_t& item, const string& desc)
{ {
std::size_t len = item.end_pos - item.beg_pos; std::size_t len = item.end_pos - item.beg_pos;
@ -375,14 +401,6 @@ string item_context(const item_t& item, const string& desc)
return out.str(); return out.str();
} }
ifstream in(item.pathname);
in.seekg(item.beg_pos, std::ios::beg);
scoped_array<char> buf(new char[len + 1]);
in.read(buf.get(), len);
assert(static_cast<std::size_t>(in.gcount()) == len);
buf[len] = '\0';
out << desc << " from \"" << item.pathname.string() << "\""; out << desc << " from \"" << item.pathname.string() << "\"";
if (item.beg_line != item.end_line) if (item.beg_line != item.end_line)
@ -391,16 +409,8 @@ string item_context(const item_t& item, const string& desc)
else else
out << ", line " << item.beg_line << ":\n"; out << ", line " << item.beg_line << ":\n";
bool first = true; print_item(out, item, "> ");
for (char * p = std::strtok(buf.get(), "\n");
p;
p = std::strtok(NULL, "\n")) {
if (first)
first = false;
else
out << '\n';
out << "> " << p;
}
return out.str(); return out.str();
} }

View file

@ -159,7 +159,8 @@ public:
}; };
value_t get_comment(item_t& item); value_t get_comment(item_t& item);
void print_item(std::ostream& out, const item_t& item,
const string& prefix = "");
string item_context(const item_t& item, const string& desc); string item_context(const item_t& item, const string& desc);
} // namespace ledger } // namespace ledger

View file

@ -33,8 +33,11 @@
namespace ledger { namespace ledger {
format_xacts::format_xacts(report_t& _report, const string& format) format_xacts::format_xacts(report_t& _report,
: report(_report), last_entry(NULL), last_xact(NULL) const string& format,
bool _print_raw)
: report(_report), last_entry(NULL), last_xact(NULL),
print_raw(_print_raw)
{ {
TRACE_CTOR(format_xacts, "report&, const string&"); TRACE_CTOR(format_xacts, "report&, const string&");
@ -59,23 +62,37 @@ void format_xacts::operator()(xact_t& xact)
{ {
std::ostream& out(report.output_stream); std::ostream& out(report.output_stream);
if (print_raw) {
if (! xact.has_xdata() || if (! xact.has_xdata() ||
! xact.xdata().has_flags(XACT_EXT_DISPLAYED)) { ! xact.xdata().has_flags(XACT_EXT_DISPLAYED)) {
if (last_entry != xact.entry) { if (last_entry != xact.entry) {
if (last_entry) { if (last_entry) {
bind_scope_t bound_scope(report, *last_entry); bind_scope_t entry_scope(report, *last_entry);
between_format.format(out, bound_scope); between_format.format(out, entry_scope);
} }
print_item(out, *xact.entry);
out << '\n';
last_entry = xact.entry;
}
xact.xdata().add_flags(XACT_EXT_DISPLAYED);
last_xact = &xact;
}
}
else if (! xact.has_xdata() ||
! xact.xdata().has_flags(XACT_EXT_DISPLAYED)) {
bind_scope_t bound_scope(report, xact); bind_scope_t bound_scope(report, xact);
if (last_entry != xact.entry) {
if (last_entry) {
bind_scope_t entry_scope(report, *last_entry);
between_format.format(out, entry_scope);
}
first_line_format.format(out, bound_scope); first_line_format.format(out, bound_scope);
last_entry = xact.entry; last_entry = xact.entry;
} }
else if (last_xact && last_xact->date() != xact.date()) { else if (last_xact && last_xact->date() != xact.date()) {
bind_scope_t bound_scope(report, xact);
first_line_format.format(out, bound_scope); first_line_format.format(out, bound_scope);
} }
else { else {
bind_scope_t bound_scope(report, xact);
next_lines_format.format(out, bound_scope); next_lines_format.format(out, bound_scope);
} }

View file

@ -65,9 +65,11 @@ protected:
format_t between_format; format_t between_format;
entry_t * last_entry; entry_t * last_entry;
xact_t * last_xact; xact_t * last_xact;
bool print_raw;
public: public:
format_xacts(report_t& _report, const string& format); format_xacts(report_t& _report, const string& format,
bool _print_raw = false);
virtual ~format_xacts() { virtual ~format_xacts() {
TRACE_DTOR(format_xacts); TRACE_DTOR(format_xacts);
} }

View file

@ -530,8 +530,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
if (*(q + 1) == '\0' || is_eq(q, "print")) if (*(q + 1) == '\0' || is_eq(q, "print"))
return WRAP_FUNCTOR return WRAP_FUNCTOR
(reporter<> (reporter<>
(new format_xacts(*this, report_format(HANDLER(print_format_))), (new format_xacts(*this, report_format(HANDLER(print_format_)),
*this)); HANDLED(raw)), *this));
else if (is_eq(q, "prices")) else if (is_eq(q, "prices"))
return expr_t::op_t::wrap_functor return expr_t::op_t::wrap_functor
(reporter<xact_t, xact_handler_ptr, &report_t::commodities_report> (reporter<xact_t, xact_handler_ptr, &report_t::commodities_report>