enhance split_unix() to allow unix style splitting of command line string

[SVN r58133]
This commit is contained in:
Sascha Ochsenknecht
2009-12-04 08:09:43 +00:00
parent 263534a213
commit 00dadb4203
3 changed files with 107 additions and 42 deletions

View File

@@ -4,62 +4,59 @@
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#define BOOST_PROGRAM_OPTIONS_SOURCE
#include <boost/program_options/parsers.hpp>
#include <boost/tokenizer.hpp>
#include <string>
#include <vector>
namespace boost { namespace program_options { namespace detail {
template<class charT>
template< class charT >
std::vector<std::basic_string<charT> >
split(const std::basic_string<charT>& cmdline, const std::basic_string<charT>& sep)
{
std::vector<std::basic_string<charT> > result;
if (!cmdline.empty())
{
std::basic_string<charT> sub(cmdline), val;
std::size_t pos;
while (sub.size() > 0)
{
if ((pos = sub.find_first_of(sep)) != sub.npos)
{
val = sub.substr(0,pos);
sub = sub.substr(pos+1);
}
else
{
val = sub;
sub.erase();
}
if (!val.empty())
{
result.push_back(val);
}
}
split_unix(
const std::basic_string<charT>& cmdline,
const std::basic_string<charT>& seperator,
const std::basic_string<charT>& quote,
const std::basic_string<charT>& escape)
{
typedef boost::tokenizer< boost::escaped_list_separator<charT>,
typename std::basic_string<charT>::const_iterator,
std::basic_string<charT> > tokenizerT;
tokenizerT tok(cmdline.begin(), cmdline.end(),
boost::escaped_list_separator< charT >(escape, seperator, quote));
std::vector< std::basic_string<charT> > result;
for (typename tokenizerT::iterator cur_token(tok.begin()), end_token(tok.end()); cur_token != end_token; ++cur_token) {
if (!cur_token->empty())
result.push_back(*cur_token);
}
return result;
return result;
}
}}}
}}} // namespace
namespace boost { namespace program_options {
// Take a command line string and splits in into tokens, according
// to the given collection of seperators chars.
BOOST_PROGRAM_OPTIONS_DECL std::vector<std::string>
split(const std::string& cmdline, const std::string& sep)
split_unix(const std::string& cmdline, const std::string& seperator,
const std::string& quote, const std::string& escape)
{
return detail::split(cmdline, sep);
return detail::split_unix< char >(cmdline, seperator, quote, escape);
}
#ifndef BOOST_NO_STD_WSTRING
BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
split(const std::wstring& cmdline, const std::wstring& sep)
split_unix(const std::wstring& cmdline, const std::wstring& seperator,
const std::wstring& quote, const std::wstring& escape)
{
return detail::split(cmdline, sep);
return detail::split_unix< wchar_t >(cmdline, seperator, quote, escape);
}
#endif
}}
}} // namespace