From e5acabc45245f7728f08a42414b7cbf7e0a7bf21 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 27 Aug 2011 10:14:59 -0400 Subject: [PATCH] Helper functions - Rescued the ::encode and ::decode funtions and put them in util.cpp. This is because 2.0 will still need to perform the same encode/decode operations without Att.cpp around. --- src/util.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 3 +++ 2 files changed, 57 insertions(+) diff --git a/src/util.cpp b/src/util.cpp index acb208523..8ec934b14 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -485,3 +485,57 @@ unsigned burndown_size (unsigned ntasks) // Round up to max of unsigned. return std::numeric_limits::max (); } + +//////////////////////////////////////////////////////////////////////////////// +// Encode values prior to serialization. +// \t -> &tab; +// " -> &dquot; +// [ -> &open; +// ] -> &close; +// \ -> \\ (extra chars to disambiguate multi-line comment) +const std::string encode (const std::string& value) +{ + std::string modified = value; + + str_replace (value, "\t", "&tab;"); + str_replace (value, "\"", "&dquot;"); + str_replace (value, "[", "&open;"); + str_replace (value, "]", "&close;"); + str_replace (value, "\\", "\\\\"); + + return modified; +} + +//////////////////////////////////////////////////////////////////////////////// +// Decode values after parse. +// \t <- &tab; +// " <- " or &dquot; +// ' <- &squot; +// , <- , +// [ <- &open; +// ] <- &close; +// : <- : +const std::string decode (const std::string& value) +{ + std::string modified = value; + + // Supported encodings. + str_replace (modified, "&tab;", "\t"); + str_replace (modified, "&dquot;", "\""); + str_replace (modified, """, "'"); + str_replace (modified, "&open;", "["); + str_replace (modified, "&close;", "]"); + + // Support for deprecated encodings. These cannot be removed or old files + // will not be parsable. Not just old files - completed.data can contain + // tasks formatted/encoded using these. + str_replace (modified, "&squot;", "'"); + str_replace (modified, ",", ","); + str_replace (modified, ":", ":"); + + return modified; +} + + +//////////////////////////////////////////////////////////////////////////////// + diff --git a/src/util.h b/src/util.h index a2d6cd5e6..b679c87b1 100644 --- a/src/util.h +++ b/src/util.h @@ -85,5 +85,8 @@ void combine (std::vector &, const std::vector &); unsigned burndown_size (unsigned ntasks); +const std::string encode (const std::string&); +const std::string decode (const std::string&); + #endif ////////////////////////////////////////////////////////////////////////////////