]> gitweb.factorcode.org Git - factor.git/blob - vm/main-windows-ce.cpp
GAME: syntax for defining game entry point with game-loop attributes
[factor.git] / vm / main-windows-ce.cpp
1 #include "master.hpp"
2
3 /* 
4         Windows CE argument parsing ported to work on
5         int main(int argc, wchar_t **argv).
6
7         This would not be necessary if Windows CE had CommandLineToArgvW.
8
9         Based on MinGW's public domain char** version.
10
11 */
12
13 int __argc;
14 wchar_t **__argv;
15
16 static int
17 parse_tokens(wchar_t* string, wchar_t*** tokens, int length)
18 {
19         /* Extract whitespace- and quotes- delimited tokens from the given string
20            and put them into the tokens array. Returns number of tokens
21            extracted. Length specifies the current size of tokens[].
22            THIS METHOD MODIFIES string.  */
23
24         const wchar_t* whitespace = L" \t\r\n";
25         wchar_t* tokenEnd = 0;
26         const wchar_t* quoteCharacters = L"\"\'";
27         wchar_t *end = string + wcslen(string);
28
29         if (string == NULL)
30                 return length;
31
32         while (1)
33         {
34                 const wchar_t* q;
35                 /* Skip over initial whitespace.  */
36                 string += wcsspn(string, whitespace);
37                 if (*string == '\0')
38                         break;
39
40                 for (q = quoteCharacters; *q; ++q)
41                 {
42                         if (*string == *q)
43                                 break;
44                 }
45                 if (*q)
46                 {
47                         /* Token is quoted.  */
48                         wchar_t quote = *string++;
49                         tokenEnd = wcschr(string, quote);
50                         /* If there is no endquote, the token is the rest of the string.  */
51                         if (!tokenEnd)
52                                 tokenEnd = end;
53                 }
54                 else
55                 {
56                         tokenEnd = string + wcscspn(string, whitespace);
57                 }
58
59                 *tokenEnd = '\0';
60
61                 {
62                         wchar_t** new_tokens;
63                         int newlen = length + 1;
64                         new_tokens = realloc (*tokens, sizeof (wchar_t**) * newlen);
65                         if (!new_tokens)
66                         {
67                                 /* Out of memory.  */
68                                 return -1;
69                         }
70
71                         *tokens = new_tokens;
72                         (*tokens)[length] = string;
73                         length = newlen;
74                 }
75                 if (tokenEnd == end)
76                         break;
77                 string = tokenEnd + 1;
78         }
79         return length;
80 }
81
82 static void
83 parse_args(int *argc, wchar_t ***argv, wchar_t *cmdlinePtrW)
84 {
85         wchar_t cmdnameBufW[MAX_UNICODE_PATH];
86         int cmdlineLen = 0;
87         int modlen;
88
89         /* argv[0] is the path of invoked program - get this from CE.  */
90         cmdnameBufW[0] = 0;
91         modlen = GetModuleFileNameW(NULL, cmdnameBufW, sizeof (cmdnameBufW)/sizeof (cmdnameBufW[0]));
92
93         if (!cmdlinePtrW)
94                 cmdlineLen = 0;
95         else
96                 cmdlineLen = wcslen(cmdlinePtrW);
97
98         /* gets realloc()'d later */
99         *argv = malloc (sizeof (wchar_t**) * 1);
100         if (!*argv)
101                 ExitProcess(-1);
102
103         (*argv)[0] = wcsdup(cmdnameBufW);
104         if(!(*argv[0]))
105                 ExitProcess(-1);
106         /* Add one to account for argv[0] */
107         (*argc)++;
108
109         if (cmdlineLen > 0)
110         {
111                 wchar_t* argv1 = (*argv)[0] + wcslen((*argv)[0]) + 1;
112                 argv1 = wcsdup(cmdlinePtrW);
113                 if(!argv1)
114                         ExitProcess(-1);
115                 *argc = parse_tokens(argv1, argv, 1);
116                 if (*argc < 0)
117                         ExitProcess(-1);
118         }
119         (*argv)[*argc] = 0;
120         return;
121 }
122
123 int WINAPI
124 WinMain(
125         HINSTANCE hInstance,
126         HINSTANCE hPrevInstance,
127         LPWSTR lpCmdLine,
128         int nCmdShow)
129 {
130         parse_args(&__argc, &__argv, lpCmdLine);
131         factor::start_standalone_factor(__argc,(LPWSTR*)__argv);
132         // memory leak from malloc, wcsdup
133         return 0;
134 }