1// Copyright Vladimir Prus 2002-2004.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt
4// or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef BOOST_PROGRAM_OPTIONS_SOURCE
7# define BOOST_PROGRAM_OPTIONS_SOURCE
8#endif
9#include <boost/program_options/parsers.hpp>
10#include <cctype>
11
12using std::size_t;
13
14#ifdef _WIN32
15namespace boost { namespace program_options {
16
17 // Take a command line string and splits in into tokens, according
18 // to the rules windows command line processor uses.
19 //
20 // The rules are pretty funny, see
21 // http://article.gmane.org/gmane.comp.lib.boost.user/3005
22 // http://msdn.microsoft.com/library/en-us/vccelng/htm/progs_12.asp
23 BOOST_PROGRAM_OPTIONS_DECL
24 std::vector<std::string> split_winmain(const std::string& input)
25 {
26 std::vector<std::string> result;
27
28 std::string::const_iterator i = input.begin(), e = input.end();
29 for(;i != e; ++i)
30 if (!isspace((unsigned char)*i))
31 break;
32
33 if (i != e) {
34
35 std::string current;
36 bool inside_quoted = false;
37 bool empty_quote = false;
38 int backslash_count = 0;
39
40 for(; i != e; ++i) {
41 if (*i == '"') {
42 // '"' preceded by even number (n) of backslashes generates
43 // n/2 backslashes and is a quoted block delimiter
44 if (backslash_count % 2 == 0) {
45 current.append(backslash_count / 2, '\\');
46 empty_quote = inside_quoted && current.empty();
47 inside_quoted = !inside_quoted;
48 // '"' preceded by odd number (n) of backslashes generates
49 // (n-1)/2 backslashes and is literal quote.
50 } else {
51 current.append(backslash_count / 2, '\\');
52 current += '"';
53 }
54 backslash_count = 0;
55 } else if (*i == '\\') {
56 ++backslash_count;
57 } else {
58 // Not quote or backslash. All accumulated backslashes should be
59 // added
60 if (backslash_count) {
61 current.append(backslash_count, '\\');
62 backslash_count = 0;
63 }
64 if (isspace((unsigned char)*i) && !inside_quoted) {
65 // Space outside quoted section terminate the current argument
66 result.push_back(current);
67 current.resize(0);
68 empty_quote = false;
69 for(;i != e && isspace((unsigned char)*i); ++i)
70 ;
71 --i;
72 } else {
73 current += *i;
74 }
75 }
76 }
77
78 // If we have trailing backslashes, add them
79 if (backslash_count)
80 current.append(backslash_count, '\\');
81
82 // If we have non-empty 'current' or we're still in quoted
83 // section (even if 'current' is empty), add the last token.
84 if (!current.empty() || inside_quoted || empty_quote)
85 result.push_back(current);
86 }
87 return result;
88 }
89
90#ifndef BOOST_NO_STD_WSTRING
91 BOOST_PROGRAM_OPTIONS_DECL std::vector<std::wstring>
92 split_winmain(const std::wstring& cmdline)
93 {
94 std::vector<std::wstring> result;
95 std::vector<std::string> aux = split_winmain(to_internal(cmdline));
96 for (size_t i = 0, e = aux.size(); i < e; ++i)
97 result.push_back(from_utf8(aux[i]));
98 return result;
99 }
100#endif
101
102}}
103#endif
104
105

source code of boost/libs/program_options/src/winmain.cpp