Split GetHomeDir()

Makes the WCHAR to char path code reusable.
This commit is contained in:
dhewg 2012-07-05 18:18:01 +02:00
parent 57e964f369
commit f6c0e2a174

View file

@ -229,30 +229,12 @@ const char *Sys_Cwd( void ) {
return cwd;
}
/*
==============
Returns "My Documents"/My Games/$savedir directory (or equivalent - "CSIDL_PERSONAL").
To be used with Sys_DefaultSavePath(), so savegames, screenshots etc will be
saved to the users files instead of systemwide.
Based on (with kind permission) Yamagi Quake II's Sys_GetHomeDir()
Returns the number of characters written to dst
==============
*/
static int GetHomeDir(char *dst, size_t size, const char *savedir)
{
static int WPath2A(char *dst, size_t size, const WCHAR *src) {
int len;
char profile[MAX_OSPATH];
WCHAR sprofile[MAX_OSPATH];
WCHAR uprofile[MAX_OSPATH];
BOOL default_char = FALSE;
/* Get the path to "My Documents" directory */
SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, 0, uprofile);
// test if we can convert lossless
len = WideCharToMultiByte(CP_ACP, 0, uprofile, -1, profile, sizeof(profile), NULL, &default_char);
len = WideCharToMultiByte(CP_ACP, 0, src, -1, dst, size, NULL, &default_char);
if (default_char) {
/* The following lines implement a horrible
@ -262,28 +244,60 @@ static int GetHomeDir(char *dst, size_t size, const char *savedir)
DOS filename translation" is switched off.
In that case the function will return NULL
and no homedir is used. */
len = GetShortPathNameW(uprofile, sprofile, sizeof(sprofile));
WCHAR w[MAX_OSPATH];
len = GetShortPathNameW(src, w, sizeof(w));
if (len == 0)
return 0;
/* Since the DOS path contains no UTF-16 characters, convert it to the system's default code page */
len = WideCharToMultiByte(CP_ACP, 0, sprofile, len, profile, sizeof(profile) - 1, NULL, NULL);
len = WideCharToMultiByte(CP_ACP, 0, w, len, dst, size - 1, NULL, NULL);
}
if (len == 0)
return 0;
profile[len] = 0;
len = idStr::snPrintf(dst, size, "%s/My Games/%s", profile, savedir);
if (len >= size)
return 0;
dst[len] = 0;
/* Replace backslashes by slashes */
for (int i = 0; i < len; ++i)
if (dst[i] == '\\')
dst[i] = '/';
// cut trailing slash
if (dst[len - 1] == '/') {
dst[len - 1] = 0;
len--;
}
return len;
}
/*
==============
Returns "My Documents"/My Games/dhewm3 directory (or equivalent - "CSIDL_PERSONAL").
To be used with Sys_DefaultSavePath(), so savegames, screenshots etc will be
saved to the users files instead of systemwide.
Based on (with kind permission) Yamagi Quake II's Sys_GetHomeDir()
Returns the number of characters written to dst
==============
*/
static int GetHomeDir(char *dst, size_t size)
{
int len;
WCHAR profile[MAX_OSPATH];
/* Get the path to "My Documents" directory */
SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, 0, profile);
len = WPath2A(dst, size, profile);
if (len == 0)
return 0;
idStr::Append(dst, size, "/My Games/dhewm3");
return len;
}
@ -297,7 +311,7 @@ bool Sys_GetPath(sysPath_t type, idStr &path) {
case PATH_CONFIG:
case PATH_SAVE:
if (GetHomeDir(buf, sizeof(buf), "dhewm3") < 1) {
if (GetHomeDir(buf, sizeof(buf)) < 1) {
Sys_Error("ERROR: Couldn't get dir to home path");
return false;
}