From: g0dil Date: Fri, 13 Nov 2009 15:29:18 +0000 (+0000) Subject: Utils/Console: Add control char escaping to string formatter X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=52a6fb56dce12132287e181b787d2474d93b0183;p=senf.git Utils/Console: Add control char escaping to string formatter git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1526 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Utils/Console/Traits.cc b/senf/Utils/Console/Traits.cc index f85d6f5..64daded 100644 --- a/senf/Utils/Console/Traits.cc +++ b/senf/Utils/Console/Traits.cc @@ -27,6 +27,7 @@ #include "Traits.ih" // Custom includes +#include #include //#include "Traits.mpp" @@ -42,6 +43,13 @@ prefix_ std::string senf::console::ArgumentTraits::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(rv[i]))).str().c_str()); + rv[i] = '\\'; + i += 3; + } + rv.insert(0,"\""); rv.push_back('"'); return rv; diff --git a/senf/Utils/Console/Traits.cti b/senf/Utils/Console/Traits.cti index c96ae78..f6dd892 100644 --- a/senf/Utils/Console/Traits.cti +++ b/senf/Utils/Console/Traits.cti @@ -106,7 +106,7 @@ parse(ParseCommandInfo::TokensRange const & tokens, CharT & out) template prefix_ std::string senf::console::detail::CharArgumentTraits::description() { - return prettyBaseName(typeid(CharT)); + return std::numeric_limits::is_signed ? "byte" : "unsigned byte"; } ///////////////////////////////cti.e/////////////////////////////////////// diff --git a/senf/Utils/Console/Traits.test.cc b/senf/Utils/Console/Traits.test.cc index 85b9f35..ae8cd33 100644 --- a/senf/Utils/Console/Traits.test.cc +++ b/senf/Utils/Console/Traits.test.cc @@ -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::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;