Windows: moved user settings dir to the "Personal" user special folder (aka "My documents", but portable and locale aware)

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@1428 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: 61c37c42c4504c807e1b07cb0a20b6d5651b578e
Former-commit-id: f2649712fc41b5c77112f0e5aad2cf0c45881c01
This commit is contained in:
pouillot 2009-09-19 13:37:04 +00:00
parent 81bcaf215f
commit be0b6e3a05

View file

@ -19,6 +19,7 @@
#ifdef WIN32 #ifdef WIN32
#include <windows.h> #include <windows.h>
#include <shlobj.h>
#include <stdlib.h> #include <stdlib.h>
#endif #endif
#include <GL/glut.h> #include <GL/glut.h>
@ -31,71 +32,90 @@
static void static void
init_args(int argc, char **argv) init_args(int argc, char **argv)
{ {
int i; int i;
i = 1; i = 1;
while (i < argc) { while (i < argc) {
if ((strncmp(argv[i], "-s", 2) == 0) || (strncmp(argv[i], "/s", 2) == 0)) { if (!strncmp(argv[i], "-s", 2) || !strncmp(argv[i], "/s", 2)) {
i++; i++;
SetSingleTextureMode (); SetSingleTextureMode ();
} else } else
i++; // Ignore bad args i++; // Ignore bad args
} }
static const int BUFSIZE = 1024; static const int BUFSIZE = MAX_PATH;
char buf[BUFSIZE]; char buf[BUFSIZE];
strncpy(buf, argv[0], BUFSIZE); strncpy(buf, argv[0], BUFSIZE);
buf[BUFSIZE-1] = '\0'; // Guarantee zero termination for next operation. buf[BUFSIZE-1] = '\0'; // Guarantee zero termination for next operation.
char *end = strrchr(buf, '\\'); char *end = strrchr(buf, '\\');
// Did we find the last '\' and do we get a complete path? // Did we find the last '\' and do we get a complete path?
if (end != NULL && buf[1] == ':') { if (end && buf[1] == ':') {
end++; end++;
*(end) = '\0'; *(end) = '\0';
// replace '\' with '/' // replace '\' with '/'
for (i = 0; i < BUFSIZE && buf[i] != '\0'; i++) {
if (buf[i] == '\\')
buf[i] = '/';
}
SetDataDir(buf);
SetLibDir("");
} else {
if (_fullpath(buf, argv[0], BUFSIZE) != NULL &&
(strcmp(argv[0], "wtorcs") == 0 ||
strcmp(argv[0], "wtorcs.exe") == 0)
)
{
end = strrchr(buf, '\\');
end++;
*(end) = '\0';
// replace '\' with '/'
for (i = 0; i < BUFSIZE && buf[i] != '\0'; i++) {
if (buf[i] == '\\')
buf[i] = '/';
}
SetDataDir(buf);
SetLibDir("");
} else {
printf("Run wtorcs.exe either from the GUI or from the directory which contains wtorcs.exe\n");
exit(1);
}
}
// Set LocalDir to the user settings dir for Torcs-NG
snprintf(buf, BUFSIZE, "%s/.torcs-ng/", getenv("USERPROFILE"));
for (i = 0; i < BUFSIZE && buf[i] != '\0'; i++) { for (i = 0; i < BUFSIZE && buf[i] != '\0'; i++) {
if (buf[i] == '\\') if (buf[i] == '\\')
buf[i] = '/'; buf[i] = '/';
} }
SetDataDir(buf);
SetLibDir("");
} else {
if (_fullpath(buf, argv[0], BUFSIZE) &&
(strcmp(argv[0], "wtorcs") == 0 ||
strcmp(argv[0], "wtorcs.exe") == 0)
)
{
end = strrchr(buf, '\\');
end++;
*(end) = '\0';
// replace '\' with '/'
for (i = 0; i < BUFSIZE && buf[i] != '\0'; i++) {
if (buf[i] == '\\')
buf[i] = '/';
}
SetDataDir(buf);
SetLibDir("");
} else {
printf("Run wtorcs.exe either from the GUI or from the directory which contains wtorcs.exe\n");
exit(1);
}
}
// Set LocalDir to the user settings dir for Torcs-NG
// (in My documents, to give access to the user for advanced settings).
//snprintf(buf, BUFSIZE, "%s", getenv("USERPROFILE"));
//#if (WINVER < 0x0600)
// if (SHGetSpecialFolderPath(0, buf, CSIDL_PERSONAL, 0))
//#else
// How can we do that under Vista / 7 as CSIDL_PERSONAL is a virtual folder
// and SHGetSpecialFolderPath is told to fail in this case ?
//#endif
LPITEMIDLIST pidl;
if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl))
&& SHGetPathFromIDList(pidl, buf))
{
for (i = 0; i < BUFSIZE && buf[i]; i++)
if (buf[i] == '\\')
buf[i] = '/';
if (buf[0] && buf[strlen(buf)-1] != '/')
strcat(buf, "/");
strcat(buf, ".torcs-ng/");
SetLocalDir(buf); SetLocalDir(buf);
}
else
{
printf("Could not get user's My documents folder path\n");
exit(1);
}
} }
/* /*
* Function * Function
* main * main
* *
* Description * Description
* Win32 entry point of TORCS * Win32 entry point of TORCS
* *
* Parameters * Parameters
* *
@ -111,14 +131,14 @@ main(int argc, char *argv[])
{ {
init_args(argc, argv); init_args(argc, argv);
WindowsSpecInit(); /* init specific windows functions */ WindowsSpecInit(); /* init specific windows functions */
GfScrInit(argc, argv); /* init screen */ GfScrInit(argc, argv); /* init screen */
TorcsEntry(); /* launch TORCS */ TorcsEntry(); /* launch TORCS */
glutMainLoop(); /* event loop of glut */ glutMainLoop(); /* event loop of glut */
return 0; /* just for the compiler, never reached */ return 0; /* just for the compiler, never reached */
} }