From 38b126edbda5d0797be09fc53e275c8156389725 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 14 Feb 2005 07:44:40 +0000 Subject: (truncated): Simplified this method, and added schemes for truncating at the beginning and middle of a string (neither of which seems better than truncating at the front). (output_xml_string): Change xml_string to output_xml_string, for simplicity's sake. Also, < and > are now output as < and >. (format_last_entry): Use output_xml_string for the account name as well as the code, payee and note. --- format.cc | 91 +++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 28 deletions(-) diff --git a/format.cc b/format.cc index 036e83e1..082b559e 100644 --- a/format.cc +++ b/format.cc @@ -12,12 +12,35 @@ namespace ledger { std::string truncated(const std::string& str, unsigned int width) { + const int len = str.length(); + if (len <= width) + return str; + + assert(width < 254); + char buf[256]; - std::memset(buf, '\0', 255); - assert(width < 256); - std::strncpy(buf, str.c_str(), str.length()); - if (buf[width]) - std::strcpy(&buf[width - 2], ".."); +#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 + buf[width] = '\0'; return buf; } @@ -608,21 +631,24 @@ void xml_write_value(std::ostream& out, const value_t& value, out << "\n"; } -std::string xml_string(const std::string& str) +void output_xml_string(std::ostream& out, const std::string& str) { - std::string::size_type pos = 0; - std::string::size_type index = str.find('&', pos); - if (index == std::string::npos) - return str; - - std::string temp; - while (index != std::string::npos) { - temp += std::string(str, pos, index) + "&"; - pos = index + 1; - index = str.find('&', pos); + for (const char * s = str.c_str(); *s; s++) { + switch (*s) { + case '<': + out << "<"; + break; + case '>': + out << "&rt;"; + break; + case '&': + out << "&"; + break; + default: + out << *s; + break; + } } - - return temp; } void format_xml_entries::format_last_entry() @@ -639,13 +665,17 @@ void format_xml_entries::format_last_entry() else if (last_entry->state == entry_t::PENDING) output_stream << " \n"; - if (! last_entry->code.empty()) - output_stream << " " << xml_string(last_entry->code) - << "\n"; + if (! last_entry->code.empty()) { + output_stream << " "; + output_xml_string(output_stream, last_entry->code); + output_stream << "\n"; + } - if (! last_entry->payee.empty()) - output_stream << " " << xml_string(last_entry->payee) - << "\n"; + if (! last_entry->payee.empty()) { + output_stream << " "; + output_xml_string(output_stream, last_entry->payee); + output_stream << "\n"; + } bool first = true; for (transactions_list::const_iterator i = last_entry->transactions.begin(); @@ -671,7 +701,10 @@ void format_xml_entries::format_last_entry() name = "[TOTAL]"; else if (name == "") name = "[UNKNOWN]"; - output_stream << " " << name << "\n"; + + output_stream << " "; + output_xml_string(output_stream, name); + output_stream << "\n"; } output_stream << " \n"; @@ -688,9 +721,11 @@ void format_xml_entries::format_last_entry() output_stream << " \n"; } - if (! (*i)->note.empty()) - output_stream << " " << xml_string((*i)->note) - << "\n"; + if (! (*i)->note.empty()) { + output_stream << " "; + output_xml_string(output_stream, (*i)->note); + output_stream << "\n"; + } if (show_totals) { output_stream << " \n"; -- cgit v1.2.3