comparison src/main/win32/SDL_win32_main.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children ed4d4f1ea201
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
53 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t')) 53 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
54 #endif /* _WIN32_WCE < 300 */ 54 #endif /* _WIN32_WCE < 300 */
55 55
56 /* Parse a command line buffer into arguments */ 56 /* Parse a command line buffer into arguments */
57 static int 57 static int
58 ParseCommandLine (char *cmdline, char **argv) 58 ParseCommandLine(char *cmdline, char **argv)
59 { 59 {
60 char *bufp; 60 char *bufp;
61 int argc; 61 int argc;
62 62
63 argc = 0; 63 argc = 0;
64 for (bufp = cmdline; *bufp;) { 64 for (bufp = cmdline; *bufp;) {
65 /* Skip leading whitespace */ 65 /* Skip leading whitespace */
66 while (isspace (*bufp)) { 66 while (isspace(*bufp)) {
67 ++bufp; 67 ++bufp;
68 } 68 }
69 /* Skip over argument */ 69 /* Skip over argument */
70 if (*bufp == '"') { 70 if (*bufp == '"') {
71 ++bufp; 71 ++bufp;
85 argv[argc] = bufp; 85 argv[argc] = bufp;
86 } 86 }
87 ++argc; 87 ++argc;
88 } 88 }
89 /* Skip over word */ 89 /* Skip over word */
90 while (*bufp && !isspace (*bufp)) { 90 while (*bufp && !isspace(*bufp)) {
91 ++bufp; 91 ++bufp;
92 } 92 }
93 } 93 }
94 if (*bufp) { 94 if (*bufp) {
95 if (argv) { 95 if (argv) {
104 return (argc); 104 return (argc);
105 } 105 }
106 106
107 /* Show an error message */ 107 /* Show an error message */
108 static void 108 static void
109 ShowError (const char *title, const char *message) 109 ShowError(const char *title, const char *message)
110 { 110 {
111 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */ 111 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
112 #ifdef USE_MESSAGEBOX 112 #ifdef USE_MESSAGEBOX
113 MessageBox (NULL, message, title, MB_ICONEXCLAMATION | MB_OK); 113 MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
114 #else 114 #else
115 fprintf (stderr, "%s: %s\n", title, message); 115 fprintf(stderr, "%s: %s\n", title, message);
116 #endif 116 #endif
117 } 117 }
118 118
119 /* Pop up an out of memory message, returns to Windows */ 119 /* Pop up an out of memory message, returns to Windows */
120 static BOOL 120 static BOOL
121 OutOfMemory (void) 121 OutOfMemory(void)
122 { 122 {
123 ShowError ("Fatal Error", "Out of memory - aborting"); 123 ShowError("Fatal Error", "Out of memory - aborting");
124 return FALSE; 124 return FALSE;
125 } 125 }
126 126
127 /* SDL_Quit() shouldn't be used with atexit() directly because 127 /* SDL_Quit() shouldn't be used with atexit() directly because
128 calling conventions may differ... */ 128 calling conventions may differ... */
129 static void 129 static void
130 cleanup (void) 130 cleanup(void)
131 { 131 {
132 SDL_Quit (); 132 SDL_Quit();
133 } 133 }
134 134
135 /* Remove the output files if there was no output written */ 135 /* Remove the output files if there was no output written */
136 static void 136 static void
137 cleanup_output (void) 137 cleanup_output(void)
138 { 138 {
139 #ifndef NO_STDIO_REDIRECT 139 #ifndef NO_STDIO_REDIRECT
140 FILE *file; 140 FILE *file;
141 int empty; 141 int empty;
142 #endif 142 #endif
143 143
144 /* Flush the output in case anything is queued */ 144 /* Flush the output in case anything is queued */
145 fclose (stdout); 145 fclose(stdout);
146 fclose (stderr); 146 fclose(stderr);
147 147
148 #ifndef NO_STDIO_REDIRECT 148 #ifndef NO_STDIO_REDIRECT
149 /* See if the files have any output in them */ 149 /* See if the files have any output in them */
150 if (stdoutPath[0]) { 150 if (stdoutPath[0]) {
151 file = fopen (stdoutPath, TEXT ("rb")); 151 file = fopen(stdoutPath, TEXT("rb"));
152 if (file) { 152 if (file) {
153 empty = (fgetc (file) == EOF) ? 1 : 0; 153 empty = (fgetc(file) == EOF) ? 1 : 0;
154 fclose (file); 154 fclose(file);
155 if (empty) { 155 if (empty) {
156 remove (stdoutPath); 156 remove(stdoutPath);
157 } 157 }
158 } 158 }
159 } 159 }
160 if (stderrPath[0]) { 160 if (stderrPath[0]) {
161 file = fopen (stderrPath, TEXT ("rb")); 161 file = fopen(stderrPath, TEXT("rb"));
162 if (file) { 162 if (file) {
163 empty = (fgetc (file) == EOF) ? 1 : 0; 163 empty = (fgetc(file) == EOF) ? 1 : 0;
164 fclose (file); 164 fclose(file);
165 if (empty) { 165 if (empty) {
166 remove (stderrPath); 166 remove(stderrPath);
167 } 167 }
168 } 168 }
169 } 169 }
170 #endif 170 #endif
171 } 171 }
175 #define console_main main 175 #define console_main main
176 #endif 176 #endif
177 177
178 /* This is where execution begins [console apps] */ 178 /* This is where execution begins [console apps] */
179 int 179 int
180 console_main (int argc, char *argv[]) 180 console_main(int argc, char *argv[])
181 { 181 {
182 size_t n; 182 size_t n;
183 char *bufp, *appname; 183 char *bufp, *appname;
184 int status; 184 int status;
185 185
186 /* Get the class name from argv[0] */ 186 /* Get the class name from argv[0] */
187 appname = argv[0]; 187 appname = argv[0];
188 if ((bufp = SDL_strrchr (argv[0], '\\')) != NULL) { 188 if ((bufp = SDL_strrchr(argv[0], '\\')) != NULL) {
189 appname = bufp + 1; 189 appname = bufp + 1;
190 } else if ((bufp = SDL_strrchr (argv[0], '/')) != NULL) { 190 } else if ((bufp = SDL_strrchr(argv[0], '/')) != NULL) {
191 appname = bufp + 1; 191 appname = bufp + 1;
192 } 192 }
193 193
194 if ((bufp = SDL_strrchr (appname, '.')) == NULL) 194 if ((bufp = SDL_strrchr(appname, '.')) == NULL)
195 n = SDL_strlen (appname); 195 n = SDL_strlen(appname);
196 else 196 else
197 n = (bufp - appname); 197 n = (bufp - appname);
198 198
199 bufp = SDL_stack_alloc (char, n + 1); 199 bufp = SDL_stack_alloc(char, n + 1);
200 if (bufp == NULL) { 200 if (bufp == NULL) {
201 return OutOfMemory (); 201 return OutOfMemory();
202 } 202 }
203 SDL_strlcpy (bufp, appname, n + 1); 203 SDL_strlcpy(bufp, appname, n + 1);
204 appname = bufp; 204 appname = bufp;
205 205
206 /* Load SDL dynamic link library */ 206 /* Load SDL dynamic link library */
207 if (SDL_Init (SDL_INIT_NOPARACHUTE) < 0) { 207 if (SDL_Init(SDL_INIT_NOPARACHUTE) < 0) {
208 ShowError ("WinMain() error", SDL_GetError ()); 208 ShowError("WinMain() error", SDL_GetError());
209 return (FALSE); 209 return (FALSE);
210 } 210 }
211 atexit (cleanup_output); 211 atexit(cleanup_output);
212 atexit (cleanup); 212 atexit(cleanup);
213 213
214 /* Sam: 214 /* Sam:
215 We still need to pass in the application handle so that 215 We still need to pass in the application handle so that
216 DirectInput will initialize properly when SDL_RegisterApp() 216 DirectInput will initialize properly when SDL_RegisterApp()
217 is called later in the video initialization. 217 is called later in the video initialization.
218 */ 218 */
219 SDL_SetModuleHandle (GetModuleHandle (NULL)); 219 SDL_SetModuleHandle(GetModuleHandle(NULL));
220 220
221 /* Run the application main() code */ 221 /* Run the application main() code */
222 status = SDL_main (argc, argv); 222 status = SDL_main(argc, argv);
223 223
224 /* Exit cleanly, calling atexit() functions */ 224 /* Exit cleanly, calling atexit() functions */
225 exit (status); 225 exit(status);
226 226
227 /* Hush little compiler, don't you cry... */ 227 /* Hush little compiler, don't you cry... */
228 return 0; 228 return 0;
229 } 229 }
230 230
231 /* This is where execution begins [windowed apps] */ 231 /* This is where execution begins [windowed apps] */
232 #ifdef _WIN32_WCE 232 #ifdef _WIN32_WCE
233 int WINAPI 233 int WINAPI
234 WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw) 234 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
235 #else 235 #else
236 int WINAPI 236 int WINAPI
237 WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) 237 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
238 #endif 238 #endif
239 { 239 {
240 HINSTANCE handle; 240 HINSTANCE handle;
241 char **argv; 241 char **argv;
242 int argc; 242 int argc;
260 260
261 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't 261 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
262 keep them open. This is a hack.. hopefully it will be fixed 262 keep them open. This is a hack.. hopefully it will be fixed
263 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded. 263 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
264 */ 264 */
265 handle = LoadLibrary (TEXT ("DDRAW.DLL")); 265 handle = LoadLibrary(TEXT("DDRAW.DLL"));
266 if (handle != NULL) { 266 if (handle != NULL) {
267 FreeLibrary (handle); 267 FreeLibrary(handle);
268 } 268 }
269 #ifndef NO_STDIO_REDIRECT 269 #ifndef NO_STDIO_REDIRECT
270 pathlen = GetModuleFileName (NULL, path, SDL_arraysize (path)); 270 pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
271 while (pathlen > 0 && path[pathlen] != '\\') { 271 while (pathlen > 0 && path[pathlen] != '\\') {
272 --pathlen; 272 --pathlen;
273 } 273 }
274 path[pathlen] = '\0'; 274 path[pathlen] = '\0';
275 275
276 #ifdef _WIN32_WCE 276 #ifdef _WIN32_WCE
277 wcsncpy (stdoutPath, path, SDL_arraysize (stdoutPath)); 277 wcsncpy(stdoutPath, path, SDL_arraysize(stdoutPath));
278 wcsncat (stdoutPath, DIR_SEPERATOR STDOUT_FILE, 278 wcsncat(stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath));
279 SDL_arraysize (stdoutPath)); 279 #else
280 #else 280 SDL_strlcpy(stdoutPath, path, SDL_arraysize(stdoutPath));
281 SDL_strlcpy (stdoutPath, path, SDL_arraysize (stdoutPath)); 281 SDL_strlcat(stdoutPath, DIR_SEPERATOR STDOUT_FILE,
282 SDL_strlcat (stdoutPath, DIR_SEPERATOR STDOUT_FILE, 282 SDL_arraysize(stdoutPath));
283 SDL_arraysize (stdoutPath));
284 #endif 283 #endif
285 284
286 /* Redirect standard input and standard output */ 285 /* Redirect standard input and standard output */
287 newfp = freopen (stdoutPath, TEXT ("w"), stdout); 286 newfp = freopen(stdoutPath, TEXT("w"), stdout);
288 287
289 #ifndef _WIN32_WCE 288 #ifndef _WIN32_WCE
290 if (newfp == NULL) { /* This happens on NT */ 289 if (newfp == NULL) { /* This happens on NT */
291 #if !defined(stdout) 290 #if !defined(stdout)
292 stdout = fopen (stdoutPath, TEXT ("w")); 291 stdout = fopen(stdoutPath, TEXT("w"));
293 #else 292 #else
294 newfp = fopen (stdoutPath, TEXT ("w")); 293 newfp = fopen(stdoutPath, TEXT("w"));
295 if (newfp) { 294 if (newfp) {
296 *stdout = *newfp; 295 *stdout = *newfp;
297 } 296 }
298 #endif 297 #endif
299 } 298 }
300 #endif /* _WIN32_WCE */ 299 #endif /* _WIN32_WCE */
301 300
302 #ifdef _WIN32_WCE 301 #ifdef _WIN32_WCE
303 wcsncpy (stderrPath, path, SDL_arraysize (stdoutPath)); 302 wcsncpy(stderrPath, path, SDL_arraysize(stdoutPath));
304 wcsncat (stderrPath, DIR_SEPERATOR STDOUT_FILE, 303 wcsncat(stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath));
305 SDL_arraysize (stdoutPath)); 304 #else
306 #else 305 SDL_strlcpy(stderrPath, path, SDL_arraysize(stderrPath));
307 SDL_strlcpy (stderrPath, path, SDL_arraysize (stderrPath)); 306 SDL_strlcat(stderrPath, DIR_SEPERATOR STDERR_FILE,
308 SDL_strlcat (stderrPath, DIR_SEPERATOR STDERR_FILE, 307 SDL_arraysize(stderrPath));
309 SDL_arraysize (stderrPath)); 308 #endif
310 #endif 309
311 310 newfp = freopen(stderrPath, TEXT("w"), stderr);
312 newfp = freopen (stderrPath, TEXT ("w"), stderr);
313 #ifndef _WIN32_WCE 311 #ifndef _WIN32_WCE
314 if (newfp == NULL) { /* This happens on NT */ 312 if (newfp == NULL) { /* This happens on NT */
315 #if !defined(stderr) 313 #if !defined(stderr)
316 stderr = fopen (stderrPath, TEXT ("w")); 314 stderr = fopen(stderrPath, TEXT("w"));
317 #else 315 #else
318 newfp = fopen (stderrPath, TEXT ("w")); 316 newfp = fopen(stderrPath, TEXT("w"));
319 if (newfp) { 317 if (newfp) {
320 *stderr = *newfp; 318 *stderr = *newfp;
321 } 319 }
322 #endif 320 #endif
323 } 321 }
324 #endif /* _WIN32_WCE */ 322 #endif /* _WIN32_WCE */
325 323
326 setvbuf (stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */ 324 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */
327 setbuf (stderr, NULL); /* No buffering */ 325 setbuf(stderr, NULL); /* No buffering */
328 #endif /* !NO_STDIO_REDIRECT */ 326 #endif /* !NO_STDIO_REDIRECT */
329 327
330 #ifdef _WIN32_WCE 328 #ifdef _WIN32_WCE
331 nLen = wcslen (szCmdLine) + 128 + 1; 329 nLen = wcslen(szCmdLine) + 128 + 1;
332 bufp = SDL_stack_alloc (wchar_t, nLen * 2); 330 bufp = SDL_stack_alloc(wchar_t, nLen * 2);
333 wcscpy (bufp, TEXT ("\"")); 331 wcscpy(bufp, TEXT("\""));
334 GetModuleFileName (NULL, bufp + 1, 128 - 3); 332 GetModuleFileName(NULL, bufp + 1, 128 - 3);
335 wcscpy (bufp + wcslen (bufp), TEXT ("\" ")); 333 wcscpy(bufp + wcslen(bufp), TEXT("\" "));
336 wcsncpy (bufp + wcslen (bufp), szCmdLine, nLen - wcslen (bufp)); 334 wcsncpy(bufp + wcslen(bufp), szCmdLine, nLen - wcslen(bufp));
337 nLen = wcslen (bufp) + 1; 335 nLen = wcslen(bufp) + 1;
338 cmdline = SDL_stack_alloc (char, nLen); 336 cmdline = SDL_stack_alloc(char, nLen);
339 if (cmdline == NULL) { 337 if (cmdline == NULL) {
340 return OutOfMemory (); 338 return OutOfMemory();
341 } 339 }
342 WideCharToMultiByte (CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL); 340 WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
343 #else 341 #else
344 /* Grab the command line */ 342 /* Grab the command line */
345 bufp = GetCommandLine (); 343 bufp = GetCommandLine();
346 nLen = SDL_strlen (bufp) + 1; 344 nLen = SDL_strlen(bufp) + 1;
347 cmdline = SDL_stack_alloc (char, nLen); 345 cmdline = SDL_stack_alloc(char, nLen);
348 if (cmdline == NULL) { 346 if (cmdline == NULL) {
349 return OutOfMemory (); 347 return OutOfMemory();
350 } 348 }
351 SDL_strlcpy (cmdline, bufp, nLen); 349 SDL_strlcpy(cmdline, bufp, nLen);
352 #endif 350 #endif
353 351
354 /* Parse it into argv and argc */ 352 /* Parse it into argv and argc */
355 argc = ParseCommandLine (cmdline, NULL); 353 argc = ParseCommandLine(cmdline, NULL);
356 argv = SDL_stack_alloc (char *, argc + 1); 354 argv = SDL_stack_alloc(char *, argc + 1);
357 if (argv == NULL) { 355 if (argv == NULL) {
358 return OutOfMemory (); 356 return OutOfMemory();
359 } 357 }
360 ParseCommandLine (cmdline, argv); 358 ParseCommandLine(cmdline, argv);
361 359
362 /* Run the main program (after a little SDL initialization) */ 360 /* Run the main program (after a little SDL initialization) */
363 console_main (argc, argv); 361 console_main(argc, argv);
364 362
365 /* Hush little compiler, don't you cry... */ 363 /* Hush little compiler, don't you cry... */
366 return 0; 364 return 0;
367 } 365 }
368 366