Utils/Console: Add control char escaping to string formatter
g0dil [Fri, 13 Nov 2009 15:29:18 +0000 (15:29 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1526 270642c3-0616-0410-b53a-bc976706d245

senf/Utils/Console/Traits.cc
senf/Utils/Console/Traits.cti
senf/Utils/Console/Traits.test.cc

index f85d6f5..64daded 100644 (file)
@@ -27,6 +27,7 @@
 #include "Traits.ih"
 
 // Custom includes
+#include <boost/format.hpp>
 #include <senf/Utils/senfassert.hh>
 
 //#include "Traits.mpp"
@@ -42,6 +43,13 @@ prefix_ std::string senf::console::ArgumentTraits<std::string>::str(std::string
         for (std::string::size_type i (0); i < rv.size(); ++i)
             if (rv[i] == '"' || rv[i] == '\\')
                 rv.insert(i++,"\\");
+            else if (rv[i] < ' ' || rv[i] > 126) {
+                rv.insert(i+1, (boost::format("x%02x") 
+                                % unsigned(static_cast<unsigned char>(rv[i]))).str().c_str());
+                rv[i] = '\\';
+                i += 3;
+            }
+        
         rv.insert(0,"\"");
         rv.push_back('"');
         return rv;
index c96ae78..f6dd892 100644 (file)
@@ -106,7 +106,7 @@ parse(ParseCommandInfo::TokensRange const & tokens, CharT & out)
 template <class CharT>
 prefix_ std::string senf::console::detail::CharArgumentTraits<CharT>::description()
 {
-    return prettyBaseName(typeid(CharT));
+    return std::numeric_limits<CharT>::is_signed ? "byte" : "unsigned byte";
 }
 
 ///////////////////////////////cti.e///////////////////////////////////////
index 85b9f35..ae8cd33 100644 (file)
@@ -131,6 +131,13 @@ BOOST_AUTO_UNIT_TEST(boolTraits)
     BOOST_CHECK_EQUAL( ss.str(), "disabled\n" "enabled\n" );
 }
 
+BOOST_AUTO_UNIT_TEST(stringTraits)
+{
+    BOOST_CHECK_EQUAL( 
+        senf::console::ArgumentTraits<std::string>::str("Test\nOf\nA \"String\"\x01\x7f\xa0\xff"),
+        "\"Test\\x0aOf\\x0aA \\\"String\\\"\\x01\\x7f\\xa0\\xff\"" );
+}
+
 BOOST_AUTO_UNIT_TEST(enumSupport)
 {
     senf::console::Executor executor;