1/*
2 *  Copyright (C) 2009 Patrick Gansterer (paroga@paroga.com)
3 *
4 *  This library is free software; you can redistribute it and/or
5 *  modify it under the terms of the GNU Library General Public
6 *  License as published by the Free Software Foundation; either
7 *  version 2 of the License, or (at your option) any later version.
8 *
9 *  This library is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 *  Library General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Library General Public License
15 *  along with this library; see the file COPYING.LIB.  If not, write to
16 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 *  Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include "config.h"
22#include "Vector.h"
23#include <winbase.h>
24#include <winnls.h>
25#include <wtf/UnusedParam.h>
26
27int main(int argc, char** argv);
28
29static inline char* convertToUtf8(LPCWSTR widecharString, int length)
30{
31    int requiredSize = WideCharToMultiByte(CP_UTF8, 0, widecharString, length, 0, 0, 0, 0);
32    char* multibyteString = new char[requiredSize + 1];
33
34    WideCharToMultiByte(CP_UTF8, 0, widecharString, length, multibyteString, requiredSize, 0, 0);
35    multibyteString[requiredSize] = '\0';
36
37    return multibyteString;
38}
39
40int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
41{
42    UNUSED_PARAM(hInstance);
43    UNUSED_PARAM(hPrevInstance);
44    UNUSED_PARAM(nCmdShow);
45
46    Vector<char*> arguments;
47    TCHAR buffer[MAX_PATH];
48
49    int length = GetModuleFileNameW(0, buffer, MAX_PATH);
50    arguments.append(convertToUtf8(buffer, length));
51
52    WCHAR* commandLine = lpCmdLine;
53    while (commandLine[0] != '\0') {
54        int commandLineLength = 1;
55        WCHAR endChar = ' ';
56
57        while (commandLine[0] == ' ')
58            ++commandLine;
59
60        if (commandLine[0] == '\"') {
61            ++commandLine;
62            endChar = '\"';
63        }
64
65        while (commandLine[commandLineLength] != endChar && commandLine[commandLineLength] != '\0')
66            ++commandLineLength;
67
68        arguments.append(convertToUtf8(commandLine, commandLineLength));
69
70        commandLine += commandLineLength;
71        if (endChar != ' ' && commandLine[0] != '\0')
72            ++commandLine;
73    }
74
75    int res = main(arguments.size(), arguments.data());
76
77    for (size_t i = 0; i < arguments.size(); i++)
78        delete arguments[i];
79
80    return res;
81}
82