From: g0dil Date: Fri, 13 Nov 2009 00:09:14 +0000 (+0000) Subject: Utils/Console: Introduce senf::console::str() X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=1d058092bd771d52996416763b5d9cafadc2e087;hp=411b898d78f6b51341b95448c9b73c0f8ad074aa;p=senf.git Utils/Console: Introduce senf::console::str() git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1520 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Utils/Console/ParsedCommand.test.cc b/senf/Utils/Console/ParsedCommand.test.cc index 0f23fc1..698c6da 100644 --- a/senf/Utils/Console/ParsedCommand.test.cc +++ b/senf/Utils/Console/ParsedCommand.test.cc @@ -222,7 +222,7 @@ BOOST_AUTO_UNIT_TEST(parsedCommand) " checkup Florgel, dargel and durgel\n" " default: (double) 2.1\n" " text \n" - " default: (empty)\n" + " default: \"\"\n" "\n" "Ops fortunate, ops me ut orgia vociferatio contumax per, rudo re loco emitto\n" "intolerabiliter ita iugo. Subcribo gravo. Devenio luna fonticulus Castanea\n" diff --git a/senf/Utils/Console/STLSupport.ct b/senf/Utils/Console/STLSupport.ct index 569211e..08116a0 100644 --- a/senf/Utils/Console/STLSupport.ct +++ b/senf/Utils/Console/STLSupport.ct @@ -87,7 +87,7 @@ senf::console::detail::CollectionReturnValueTraits::format(Collectio typename type::const_iterator const i_end (value.end()); if (i != i_end) for (;;) { - senf::console::format(*i, os); + os << senf::console::str(*i); if (++i == i_end) break; else @@ -152,9 +152,9 @@ senf::console::detail::MapReturnValueTraits::format(Collection const typename type::const_iterator const i_end (value.end()); if (i != i_end) for (;;) { - senf::console::format(i->first, os); - os << "="; - senf::console::format(i->second, os); + os << senf::console::str(i->first) + << "=" + << senf::console::str(i->second); if (++i == i_end) break; else @@ -198,11 +198,8 @@ template prefix_ void senf::console::ReturnValueTraits< std::pair >::format(type const & value, std::ostream & os) { - os << "("; - senf::console::format(value.first, os); - os << " "; - senf::console::format(value.second, os); - os << ")"; + os << "(" << senf::console::str(value.first) + << " " << senf::console::str(value.second) << ")"; } #endif diff --git a/senf/Utils/Console/STLSupport.test.cc b/senf/Utils/Console/STLSupport.test.cc index 878c909..ddc78dc 100644 --- a/senf/Utils/Console/STLSupport.test.cc +++ b/senf/Utils/Console/STLSupport.test.cc @@ -170,7 +170,7 @@ BOOST_AUTO_UNIT_TEST(mapSupport) senf::console::root().add("test", dir); std::map defv ( - boost::assign::map_list_of("foo",7)("bar",2).to_container(defv)); + boost::assign::map_list_of("foo bar",7)("bar",2).to_container(defv)); dir.add("test", &mapTest) .arg("data", "test data", senf::console::kw::default_value = defv); std::stringstream ss; @@ -179,7 +179,7 @@ BOOST_AUTO_UNIT_TEST(mapSupport) parser.parse("test/test; test/test (); " "test/test (vier=4 fuenf = 5 acht=8 )", boost::bind( boost::ref(executor), boost::ref(ss), _1 )) ); - BOOST_CHECK_EQUAL( ss.str(), "(barfoo 9)\n" "( 0)\n" "(achtfuenfvier 17)\n" ); + BOOST_CHECK_EQUAL( ss.str(), "(\"barfoo bar\" 9)\n" "(\"\" 0)\n" "(achtfuenfvier 17)\n" ); // ss.str(""); SENF_CHECK_NO_THROW( @@ -192,7 +192,7 @@ BOOST_AUTO_UNIT_TEST(mapSupport) "\n" "With:\n" " data test data\n" - " default: (bar=2 foo=7)\n" ); + " default: (bar=2 \"foo bar\"=7)\n" ); } ///////////////////////////////cc.e//////////////////////////////////////// diff --git a/senf/Utils/Console/Traits.cc b/senf/Utils/Console/Traits.cc index 6f75d60..f85d6f5 100644 --- a/senf/Utils/Console/Traits.cc +++ b/senf/Utils/Console/Traits.cc @@ -33,6 +33,21 @@ #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// +prefix_ std::string senf::console::ArgumentTraits::str(std::string const & value) +{ + if (! value.empty() && boost::algorithm::all(value, CommandParser::isWordChar)) + return value; + else { + std::string rv (value); + for (std::string::size_type i (0); i < rv.size(); ++i) + if (rv[i] == '"' || rv[i] == '\\') + rv.insert(i++,"\\"); + rv.insert(0,"\""); + rv.push_back('"'); + return rv; + } +} + prefix_ long senf::console::detail::parseEnum(EnumTable const & table, ParseCommandInfo::TokensRange const & tokens) { diff --git a/senf/Utils/Console/Traits.cci b/senf/Utils/Console/Traits.cci index 6625e60..4386af1 100644 --- a/senf/Utils/Console/Traits.cci +++ b/senf/Utils/Console/Traits.cci @@ -70,6 +70,20 @@ prefix_ std::string senf::console::ArgumentTraits::str(bool value) } /////////////////////////////////////////////////////////////////////////// +// senf::console::ArgumentTraits + +prefix_ void senf::console::ArgumentTraits:: +parse(ParseCommandInfo::TokensRange const & tokens, std::string & out) +{ + senf_console_parse_argument(tokens,out); +} + +prefix_ std::string senf::console::ArgumentTraits::description() +{ + return "string"; +} + +/////////////////////////////////////////////////////////////////////////// // senf::console::ReturnValueTraits prefix_ void senf::console::ReturnValueTraits::format(bool value, std::ostream & os) diff --git a/senf/Utils/Console/Traits.cti b/senf/Utils/Console/Traits.cti index 70c8869..0173633 100644 --- a/senf/Utils/Console/Traits.cti +++ b/senf/Utils/Console/Traits.cti @@ -66,6 +66,12 @@ prefix_ void senf::console::parse(ParseCommandInfo::TokensRange const & tokens, } template +prefix_ std::string senf::console::str(Type const & value) +{ + return ArgumentTraits::str(value); +} + +template prefix_ void senf::console::format(Type const & value, std::ostream & os) { ReturnValueTraits::format(value, os); diff --git a/senf/Utils/Console/Traits.hh b/senf/Utils/Console/Traits.hh index e60faed..811be19 100644 --- a/senf/Utils/Console/Traits.hh +++ b/senf/Utils/Console/Traits.hh @@ -140,6 +140,22 @@ namespace console { template void parse(ParseCommandInfo::TokensRange const & tokens, Type & out); + /** \brief Format value + + This helper will call the correct ArgumentTraits::str function to format \a value + + \see ArgumentTraits + */ + template + std::string str(Type const & value); + + /** \brief Format return value + + This helper will invoke the correct ReturnValueTraits::format function to write \a value + into the \a out stream. + + \see ReturnValueTraits + */ template void format(Type const & value, std::ostream & os); @@ -165,6 +181,17 @@ namespace console { static void format(bool value, std::ostream & os); }; + template <> + struct ArgumentTraits + { + typedef std::string type; + static bool const singleToken = true; + + static void parse(ParseCommandInfo::TokensRange const & tokens, std::string & out); + static std::string description(); + static std::string str(std::string const & value); + }; + #endif /** \brief Format boolean value as \c true / \c false */ diff --git a/senf/Utils/Console/Utility.ct b/senf/Utils/Console/Utility.ct index bd132fb..771e36d 100644 --- a/senf/Utils/Console/Utility.ct +++ b/senf/Utils/Console/Utility.ct @@ -73,9 +73,9 @@ template prefix_ void senf::console::ReturnValueTraits< senf::console::ValueRange >:: format(type const & value, std::ostream & os) { - os << value.low; + os << senf::console::str(value.low); if (value.low != value.high) - os << ':' << value.high; + os << ':' << senf::console::str(value.high); } ///////////////////////////////ct.e////////////////////////////////////////