From bb444b740ff2f83ae509cef0a8604ef73c495d90 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 15 Feb 2005 10:28:42 +0000 Subject: (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. --- format.cc | 108 +++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file 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_wrap; + + class_< format_transactions_wrap, bases > > + ("FormatTransactions", + init()[with_custodian_and_ward<1, 2>()]) + .def("flush", &format_transactions_wrap::flush) + .def("__call__", &format_transactions_wrap::operator()) + ; + + typedef + pystream_handler_wrap + format_entries_wrap; + + class_< format_entries_wrap, bases > > + ("FormatEntries", + init()[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_wrap; + + class_< format_xml_entries_wrap, bases > > + ("FormatXmlEntries", + init()[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_wrap; + + class_< format_account_wrap, bases > > + ("FormatAccount", + init() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &format_account_wrap::flush) + .def("__call__", &format_account_wrap::operator()) + ; + + typedef + pystream_handler_wrap + format_equity_wrap; + + class_< format_equity_wrap, bases > > + ("FormatEquity", + init() + [with_custodian_and_ward<1, 2>()]) + .def("flush", &format_equity_wrap::flush) + .def("__call__", &format_equity_wrap::operator()) + ; + class_< format_t > ("Format") .def(init()) .def("reset", &format_t::reset) -- cgit v1.2.3