Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/program_options/src/winmain.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

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