summaryrefslogtreecommitdiff
path: root/format.h
diff options
context:
space:
mode:
Diffstat (limited to 'format.h')
-rw-r--r--format.h259
1 files changed, 79 insertions, 180 deletions
diff --git a/format.h b/format.h
index 778ec53f..d1a87afa 100644
--- a/format.h
+++ b/format.h
@@ -1,209 +1,108 @@
#ifndef _FORMAT_H
#define _FORMAT_H
-#include "journal.h"
-#include "valexpr.h"
-#include "walk.h"
+#include "xpath.h"
+#include "error.h"
+#include "debug.h"
-namespace ledger {
-
-std::string truncated(const std::string& str, unsigned int width,
- const int style = 2);
+#include <list>
-std::string partial_account_name(const account_t& account,
- const unsigned int start_depth);
-
-#define ELEMENT_ALIGN_LEFT 0x01
-#define ELEMENT_HIGHLIGHT 0x02
+namespace ledger {
-struct element_t
+class format_t
{
- enum kind_t {
- STRING,
- VALUE_EXPR,
- SOURCE,
- ENTRY_BEG_POS,
- ENTRY_BEG_LINE,
- ENTRY_END_POS,
- ENTRY_END_LINE,
- XACT_BEG_POS,
- XACT_BEG_LINE,
- XACT_END_POS,
- XACT_END_LINE,
- DATE_STRING,
- COMPLETE_DATE_STRING,
- CLEARED,
- ENTRY_CLEARED,
- CODE,
- PAYEE,
- OPT_ACCOUNT,
- ACCOUNT_NAME,
- ACCOUNT_FULLNAME,
- AMOUNT,
- OPT_AMOUNT,
- TOTAL,
- NOTE,
- OPT_NOTE,
- SPACER,
- DEPTH_SPACER
- };
-
- kind_t type;
- unsigned char flags;
- std::string chars;
- unsigned char min_width;
- unsigned char max_width;
- value_expr val_expr;
-
- struct element_t * next;
+ public:
+ struct element_t
+ {
+ bool align_left;
+ short min_width;
+ short max_width;
+
+ enum kind_t { UNKNOWN, TEXT, COLUMN, XPATH, GROUP } kind;
+ union {
+ std::string * chars;
+ xml::xpath_t * xpath;
+ format_t * format;
+ };
+
+ element_t()
+ : align_left(false), min_width(-1), max_width(-1),
+ kind(UNKNOWN), chars(NULL) {
+ TRACE_CTOR("element_t()");
+ }
- element_t() : type(STRING), flags(false),
- min_width(0), max_width(0), next(NULL) {
- DEBUG_PRINT("ledger.memory.ctors", "ctor element_t");
- }
+ ~element_t() {
+ TRACE_DTOR("element_t");
+
+ switch (kind) {
+ case TEXT:
+ delete chars;
+ break;
+ case XPATH:
+ delete xpath;
+ break;
+ case GROUP:
+ delete format;
+ break;
+ default:
+ assert(! chars);
+ break;
+ }
+ }
- ~element_t() {
- DEBUG_PRINT("ledger.memory.dtors", "dtor element_t");
- if (next) delete next; // recursive, but not too deep
- }
-};
+ private:
+ element_t(const element_t& other);
+ };
-struct format_t
-{
- std::string format_string;
- element_t * elements;
-
- enum elision_style_t {
- TRUNCATE_TRAILING,
- TRUNCATE_MIDDLE,
- TRUNCATE_LEADING,
- ABBREVIATE
+ struct element_formatter_t {
+ virtual ~element_formatter_t() {}
+ virtual int operator()(std::ostream& out, element_t * element,
+ xml::node_t * context, int column) const;
};
- static elision_style_t elision_style;
- static int abbrev_length;
+ std::string format_string;
+ std::list<element_t *> elements;
- static bool ansi_codes;
- static bool ansi_invert;
+ private:
+ format_t(const format_t&);
- format_t() : elements(NULL) {
- DEBUG_PRINT("ledger.memory.ctors", "ctor format_t");
- }
- format_t(const std::string& _format) : elements(NULL) {
- DEBUG_PRINT("ledger.memory.ctors", "ctor format_t");
- reset(_format);
+ public:
+ format_t() {
+ TRACE_CTOR("format_t()");
}
- ~format_t() {
- DEBUG_PRINT("ledger.memory.dtors", "dtor format_t");
- if (elements) delete elements;
+ format_t(const std::string& fmt) {
+ TRACE_CTOR("format_t(const std::string&)");
+ parse(fmt);
}
- void reset(const std::string& _format) {
- if (elements)
- delete elements;
- elements = parse_elements(_format);
- format_string = _format;
+ void clear_elements() {
+ for (std::list<element_t *>::iterator i = elements.begin();
+ i != elements.end();
+ i++)
+ delete *i;
+ elements.clear();
}
- static element_t * parse_elements(const std::string& fmt);
-
- static std::string truncate(const std::string& str, unsigned int width,
- const bool is_account = false);
-
- void format(std::ostream& out, const details_t& details) const;
-};
-
-class format_transactions : public item_handler<transaction_t>
-{
- protected:
- std::ostream& output_stream;
- format_t first_line_format;
- format_t next_lines_format;
- entry_t * last_entry;
- transaction_t * last_xact;
-
- public:
- format_transactions(std::ostream& _output_stream,
- const std::string& format);
-
- virtual void flush() {
- output_stream.flush();
+ virtual ~format_t() {
+ TRACE_DTOR("format_t");
+ clear_elements();
}
- virtual void operator()(transaction_t& xact);
-};
-
-class format_entries : public format_transactions
-{
- public:
- format_entries(std::ostream& output_stream, const std::string& format)
- : format_transactions(output_stream, format) {}
- virtual void format_last_entry();
+ void parse(const std::string& fmt);
- virtual void flush() {
- if (last_entry) {
- format_last_entry();
- last_entry = NULL;
- }
- format_transactions::flush();
+ void compile(const std::string& fmt, xml::node_t * context = NULL) {
+ parse(fmt);
+ compile(context);
}
- virtual void operator()(transaction_t& xact);
-};
-
-void print_entry(std::ostream& out, const entry_base_t& entry,
- const std::string& prefix = "");
-
-bool disp_subaccounts_p(const account_t& account,
- const item_predicate<account_t>& disp_pred,
- const account_t *& to_show);
-
-inline bool disp_subaccounts_p(const account_t& account) {
- const account_t * temp;
- return disp_subaccounts_p(account, item_predicate<account_t>(NULL), temp);
-}
-
-bool display_account(const account_t& account,
- const item_predicate<account_t>& disp_pred);
-
-class format_account : public item_handler<account_t>
-{
- std::ostream& output_stream;
+ void compile(xml::node_t * context = NULL);
- item_predicate<account_t> disp_pred;
+ int format(std::ostream& out, xml::node_t * context = NULL,
+ int column = 0, const element_formatter_t& formatter =
+ element_formatter_t()) const;
- public:
- format_t format;
-
- format_account(std::ostream& _output_stream,
- const std::string& _format,
- const std::string& display_predicate = NULL)
- : output_stream(_output_stream), disp_pred(display_predicate),
- format(_format) {}
-
- virtual void flush() {
- output_stream.flush();
+ operator bool() const {
+ return ! format_string.empty();
}
-
- virtual void operator()(account_t& account);
-};
-
-class format_equity : public item_handler<account_t>
-{
- std::ostream& output_stream;
- format_t first_line_format;
- format_t next_lines_format;
-
- item_predicate<account_t> disp_pred;
-
- mutable value_t total;
-
- public:
- format_equity(std::ostream& _output_stream,
- const std::string& _format,
- const std::string& display_predicate);
-
- virtual void flush();
- virtual void operator()(account_t& account);
};
class format_error : public error {