arsa  2.7
SDL_windows_main.c
Go to the documentation of this file.
1 /*
2  SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98
3 
4  The WinMain function -- calls your program's main() function
5 */
6 #include "SDL/SDL_config.h"
7 
8 #ifdef __WIN32__
9 
10 /* Include this so we define UNICODE properly */
11 #include "SDL_windows.h"
12 
13 /* Include the SDL main definition header */
14 #include "SDL/SDL.h"
15 #include "SDL/SDL_main.h"
16 
17 #ifdef main
18 # undef main
19 #endif /* main */
20 
21 static void
22 UnEscapeQuotes(char *arg)
23 {
24  char *last = NULL;
25 
26  while (*arg) {
27  if (*arg == '"' && (last != NULL && *last == '\\')) {
28  char *c_curr = arg;
29  char *c_last = last;
30 
31  while (*c_curr) {
32  *c_last = *c_curr;
33  c_last = c_curr;
34  c_curr++;
35  }
36  *c_last = '\0';
37  }
38  last = arg;
39  arg++;
40  }
41 }
42 
43 /* Parse a command line buffer into arguments */
44 static int
45 ParseCommandLine(char *cmdline, char **argv)
46 {
47  char *bufp;
48  char *lastp = NULL;
49  int argc, last_argc;
50 
51  argc = last_argc = 0;
52  for (bufp = cmdline; *bufp;) {
53  /* Skip leading whitespace */
54  while (SDL_isspace(*bufp)) {
55  ++bufp;
56  }
57  /* Skip over argument */
58  if (*bufp == '"') {
59  ++bufp;
60  if (*bufp) {
61  if (argv) {
62  argv[argc] = bufp;
63  }
64  ++argc;
65  }
66  /* Skip over word */
67  lastp = bufp;
68  while (*bufp && (*bufp != '"' || *lastp == '\\')) {
69  lastp = bufp;
70  ++bufp;
71  }
72  } else {
73  if (*bufp) {
74  if (argv) {
75  argv[argc] = bufp;
76  }
77  ++argc;
78  }
79  /* Skip over word */
80  while (*bufp && !SDL_isspace(*bufp)) {
81  ++bufp;
82  }
83  }
84  if (*bufp) {
85  if (argv) {
86  *bufp = '\0';
87  }
88  ++bufp;
89  }
90 
91  /* Strip out \ from \" sequences */
92  if (argv && last_argc != argc) {
93  UnEscapeQuotes(argv[last_argc]);
94  }
95  last_argc = argc;
96  }
97  if (argv) {
98  argv[argc] = NULL;
99  }
100  return (argc);
101 }
102 
103 /* Pop up an out of memory message, returns to Windows */
104 static BOOL
105 OutOfMemory(void)
106 {
107  SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
108  return FALSE;
109 }
110 
111 #if defined(_MSC_VER)
112 /* The VC++ compiler needs main/wmain defined */
113 # define console_ansi_main main
114 # if UNICODE
115 # define console_wmain wmain
116 # endif
117 #endif
118 
119 /* WinMain, main, and wmain eventually call into here. */
120 static int
121 main_utf8(int argc, char *argv[])
122 {
124 
125  /* Run the application main() code */
126  return SDL_main(argc, argv);
127 }
128 
129 /* Gets the arguments with GetCommandLine, converts them to argc and argv
130  and calls main_utf8 */
131 static int
132 main_getcmdline()
133 {
134  char **argv;
135  int argc;
136  char *cmdline;
137  int retval = 0;
138 
139  /* Grab the command line */
140  TCHAR *text = GetCommandLine();
141 #if UNICODE
142  cmdline = WIN_StringToUTF8(text);
143 #else
144  /* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */
145  cmdline = SDL_strdup(text);
146 #endif
147  if (cmdline == NULL) {
148  return OutOfMemory();
149  }
150 
151  /* Parse it into argv and argc */
152  argc = ParseCommandLine(cmdline, NULL);
153  argv = SDL_stack_alloc(char *, argc + 1);
154  if (argv == NULL) {
155  return OutOfMemory();
156  }
157  ParseCommandLine(cmdline, argv);
158 
159  retval = main_utf8(argc, argv);
160 
161  SDL_stack_free(argv);
162  SDL_free(cmdline);
163 
164  return retval;
165 }
166 
167 /* This is where execution begins [console apps, ansi] */
168 int
169 console_ansi_main(int argc, char *argv[])
170 {
171  return main_getcmdline();
172 }
173 
174 
175 #if UNICODE
176 /* This is where execution begins [console apps, unicode] */
177 int
178 console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
179 {
180  int retval = 0;
181  char **argv = SDL_stack_alloc(char*, argc + 1);
182  int i;
183 
184  for (i = 0; i < argc; ++i) {
185  argv[i] = WIN_StringToUTF8(wargv[i]);
186  }
187  argv[argc] = NULL;
188 
189  retval = main_utf8(argc, argv);
190 
191  /* !!! FIXME: we are leaking all the elements of argv we allocated. */
192  SDL_stack_free(argv);
193 
194  return retval;
195 }
196 #endif
197 
198 /* This is where execution begins [windowed apps] */
199 int WINAPI
200 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
201 {
202  return main_getcmdline();
203 }
204 
205 #endif /* __WIN32__ */
206 
207 /* vi: set ts=4 sw=4 expandtab: */
#define FALSE
Definition: cdx.h:170
DECLSPEC char *SDLCALL SDL_strdup(const char *str)
int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
#define SDL_stack_alloc(type, count)
Definition: SDL_stdinc.h:357
#define WIN_StringToUTF8(S)
Definition: SDL_windows.h:45
SDLMAIN_DECLSPEC int SDL_main(int argc, char *argv[])
DECLSPEC int SDLCALL SDL_isspace(int x)
DECLSPEC void SDLCALL SDL_SetMainReady(void)
DECLSPEC int SDLCALL SDL_ShowSimpleMessageBox(Uint32 flags, const char *title, const char *message, SDL_Window *window)
Create a simple modal message box.
#define NULL
Definition: begin_code.h:167
DECLSPEC void SDLCALL SDL_free(void *mem)
#define SDL_stack_free(data)
Definition: SDL_stdinc.h:358
#define BOOL
Definition: cdx.h:160