summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2005-02-15 10:28:42 +0000
committerJohn Wiegley <johnw@newartisans.com>2008-04-13 02:41:01 -0400
commitbb444b740ff2f83ae509cef0a8604ef73c495d90 (patch)
tree766289ba3e74c4e44884a676a77a5cf0d046f3d1
parent6b50c85f0133f89c581a4c3041f9653a71ebace5 (diff)
downloadfork-ledger-bb444b740ff2f83ae509cef0a8604ef73c495d90.tar.gz
fork-ledger-bb444b740ff2f83ae509cef0a8604ef73c495d90.tar.bz2
fork-ledger-bb444b740ff2f83ae509cef0a8604ef73c495d90.zip
(truncated): Added "style" argument, so that at least Python users can
choose which output style they want (truncation at beginning, middle or end of the string). (export_format): Expose following handlers to Python: FormatTransactions, FormatEntries, FormatXmlEntries, FormatAccount, FormatEquity.
-rw-r--r--format.cc108
1 files changed, 86 insertions, 22 deletions
diff --git a/format.cc b/format.cc
index 082b559e..b6cce9f1 100644
--- a/format.cc
+++ b/format.cc
@@ -10,7 +10,8 @@
namespace ledger {
-std::string truncated(const std::string& str, unsigned int width)
+std::string truncated(const std::string& str, unsigned int width,
+ const int style)
{
const int len = str.length();
if (len <= width)
@@ -19,28 +20,34 @@ std::string truncated(const std::string& str, unsigned int width)
assert(width < 254);
char buf[256];
-#if 1
- // This method truncates at the end.
- std::strncpy(buf, str.c_str(), width - 2);
- buf[width - 2] = '.';
- buf[width - 1] = '.';
-#else
-#if 0
- // This method truncates at the beginning.
- std::strncpy(buf, str.c_str() + (len - width), width);
- buf[0] = '.';
- buf[1] = '.';
-#else
- // This method truncates in the middle.
- std::strncpy(buf, str.c_str(), width / 2);
- std::strncpy(buf + width / 2,
- str.c_str() + (len - (width / 2 + width % 2)),
- width / 2 + width % 2);
- buf[width / 2 - 1] = '.';
- buf[width / 2] = '.';
-#endif
-#endif
+
+ switch (style) {
+ case 0:
+ // This method truncates at the beginning.
+ std::strncpy(buf, str.c_str() + (len - width), width);
+ buf[0] = '.';
+ buf[1] = '.';
+ break;
+
+ case 1:
+ // This method truncates in the middle.
+ std::strncpy(buf, str.c_str(), width / 2);
+ std::strncpy(buf + width / 2,
+ str.c_str() + (len - (width / 2 + width % 2)),
+ width / 2 + width % 2);
+ buf[width / 2 - 1] = '.';
+ buf[width / 2] = '.';
+ break;
+
+ case 2:
+ // This method truncates at the end (the default).
+ std::strncpy(buf, str.c_str(), width - 2);
+ buf[width - 2] = '.';
+ buf[width - 1] = '.';
+ break;
+ }
buf[width] = '\0';
+
return buf;
}
@@ -935,6 +942,63 @@ std::string py_format(format_t& format, const T& item)
void export_format()
{
+ typedef
+ pystream_handler_wrap<format_transactions, transaction_t, std::string>
+ format_transactions_wrap;
+
+ class_< format_transactions_wrap, bases<item_handler<transaction_t> > >
+ ("FormatTransactions",
+ init<PyObject *, std::string>()[with_custodian_and_ward<1, 2>()])
+ .def("flush", &format_transactions_wrap::flush)
+ .def("__call__", &format_transactions_wrap::operator())
+ ;
+
+ typedef
+ pystream_handler_wrap<format_entries, transaction_t, std::string>
+ format_entries_wrap;
+
+ class_< format_entries_wrap, bases<item_handler<transaction_t> > >
+ ("FormatEntries",
+ init<PyObject *, std::string>()[with_custodian_and_ward<1, 2>()])
+ .def("flush", &format_entries_wrap::flush)
+ .def("__call__", &format_entries_wrap::operator())
+ ;
+
+ typedef
+ pystream_handler_wrap<format_xml_entries, transaction_t, bool>
+ format_xml_entries_wrap;
+
+ class_< format_xml_entries_wrap, bases<item_handler<transaction_t> > >
+ ("FormatXmlEntries",
+ init<PyObject *, bool>()[with_custodian_and_ward<1, 2>()])
+ .def("flush", &format_xml_entries_wrap::flush)
+ .def("__call__", &format_xml_entries_wrap::operator())
+ ;
+
+ typedef
+ pystream_handler_wrap<format_account, account_t, std::string, std::string>
+ format_account_wrap;
+
+ class_< format_account_wrap, bases<item_handler<transaction_t> > >
+ ("FormatAccount",
+ init<PyObject *, std::string, std::string>()
+ [with_custodian_and_ward<1, 2>()])
+ .def("flush", &format_account_wrap::flush)
+ .def("__call__", &format_account_wrap::operator())
+ ;
+
+ typedef
+ pystream_handler_wrap<format_equity, account_t, std::string, std::string>
+ format_equity_wrap;
+
+ class_< format_equity_wrap, bases<item_handler<transaction_t> > >
+ ("FormatEquity",
+ init<PyObject *, std::string, std::string>()
+ [with_custodian_and_ward<1, 2>()])
+ .def("flush", &format_equity_wrap::flush)
+ .def("__call__", &format_equity_wrap::operator())
+ ;
+
class_< format_t > ("Format")
.def(init<std::string>())
.def("reset", &format_t::reset)