1 
2 /*
3 
4 Taken from:
5 https://github.com/gosu/gosu/blob/master/src/WinMain.cpp
6 
7 This license applies only to this file:
8 
9 Copyright (C) 2001-2017 Julian Raschke, Jan L�cker and all contributors.
10 
11 Permission is hereby granted, free of charge, to any person obtaining a
12 copy of this software and associated documentation files (the "Software"),
13 to deal in the Software without restriction, including without limitation
14 the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 and/or sell copies of the Software, and to permit persons to whom the
16 Software is furnished to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29             Julian Raschke <julian@raschke.de> & contributors
30                         http://www.libgosu.org/
31 
32 */
33 
34 #ifdef _MSC_VER
35 
36 #include <windows.h>
37 #include <exception>
38 #include <string>
39 #include <vector>
40 using namespace std;
41 
splitCmdLine()42 vector<string> splitCmdLine()
43 {
44     vector<string> result;
45 
46     const char* cmdLine = ::GetCommandLineA();
47 
48     const char* argBegin = 0;
49     bool isQuotedArg = false;
50 
51     while (*cmdLine)
52     {
53         if (*cmdLine == '"')
54         {
55             if (argBegin == 0)
56             {
57                 argBegin = cmdLine + 1;
58                 isQuotedArg = true;
59             }
60             else if (isQuotedArg)
61             {
62                 result.push_back(std::string(argBegin, cmdLine));
63                 argBegin = 0;
64             }
65         }
66         else if (!isspace((unsigned char)*cmdLine) && argBegin == 0)
67         {
68             argBegin = cmdLine;
69             isQuotedArg = false;
70         }
71         else if (isspace((unsigned char)*cmdLine) && argBegin != 0 && !isQuotedArg)
72         {
73             result.push_back(std::string(argBegin, cmdLine + 1));
74             argBegin = 0;
75         }
76         ++cmdLine;
77     }
78 
79     if (argBegin != 0)
80         result.push_back(argBegin);
81 
82     return result;
83 }
84 
85 int main(int argc, char* argv[]);
86 
WinMain(HINSTANCE,HINSTANCE,LPSTR,int)87 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
88 {
89     try
90     {
91         vector<string> arguments = splitCmdLine();
92         vector<char*> argv(arguments.size());
93         for (unsigned i = 0; i < argv.size(); ++i)
94             argv[i] = const_cast<char*>(arguments[i].c_str());
95         return main(argv.size(), &argv[0]);
96     }
97     catch (const std::exception& e)
98     {
99         ::MessageBoxA(0, e.what(), "Uncaught Exception", MB_OK | MB_ICONERROR);
100         return EXIT_FAILURE;
101     }
102 }
103 
104 #endif
105 
106