Refactor the "home"-path finding logic into own functions

With this change the homedir is no longer selected in the filesystem,
but in platform dependend functions. This allows us to use WINABI calls
for selection, resulting using the apropriate diretory on localized
versions of Windows XP.
This commit is contained in:
Yamagi Burmeister 2012-06-11 09:55:54 +02:00
parent e8eea857e4
commit e70067ceba
4 changed files with 66 additions and 39 deletions

View file

@ -1253,51 +1253,18 @@ FS_AddGameDirectory(const char *dir)
void
FS_AddHomeAsGameDirectory(char *dir)
{
char *home;
char gdir[MAX_OSPATH];
int len;
size_t len;
#ifdef _WIN32
char *cur;
char *old;
char *profile = getenv("USERPROFILE");
home = Sys_GetHomeDir();
if (!profile)
if (home == NULL)
{
return;
}
cur = old = profile;
/* Replace backslashes by slashes */
if (strstr(cur, "\\") != NULL)
{
while (cur != NULL)
{
if ((cur - old) > 1)
{
*cur = '/';
}
old = cur;
cur = strchr(old + 1, '\\');
}
}
len = snprintf(gdir, sizeof(gdir), "%s%s%s/", profile,
"/Documents/YamagiQ2/", dir);
#else
char *homedir = getenv("HOME");
if (!homedir)
{
return;
}
len = snprintf(gdir, sizeof(gdir), "%s/.yq2/%s/", homedir, dir);
#endif
len = snprintf(gdir, sizeof(gdir), "%s%s/", home, dir);
FS_CreatePath(gdir);
if ((len > 0) && (len < sizeof(gdir)) && (gdir[len-1] == '/'))

View file

@ -55,6 +55,12 @@
#define CPUSTRING "Unknown"
#endif
#ifdef _WIN32
#define CFGDIR "YamagiQ2"
#else
#define CFGDIR ".yq2"
#endif
/* ================================================================== */
typedef struct sizebuf_s
@ -741,6 +747,7 @@ void Sys_ConsoleOutput (char *string);
void Sys_SendKeyEvents (void);
void Sys_Error (char *error, ...);
void Sys_Quit (void);
char *Sys_GetHomeDir(void);
/* CLIENT / SERVER SYSTEMS */

View file

@ -501,3 +501,22 @@ Sys_SendKeyEvents ( void )
/* grab frame time */
sys_frame_time = Sys_Milliseconds();
}
char *
Sys_GetHomeDir(void)
{
static char gdir[MAX_OSPATH];
char *home;
home = getenv("HOME");
if (!home)
{
return NULL;
}
snprintf(gdir, sizeof(gdir), "%s/%s/", home, CFGDIR);
return gdir;
}

View file

@ -32,6 +32,7 @@
#include <direct.h>
#include <io.h>
#include <conio.h>
#include <shlobj.h>
#include "../common/header/common.h"
#include "header/conproc.h"
@ -606,6 +607,39 @@ Sys_GetCurrentDirectory(void)
return dir;
}
char *
Sys_GetHomeDir(void)
{
char *old;
char *cur;
static char gdir[MAX_OSPATH];
TCHAR profile[MAX_OSPATH];
SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, profile);
/* Replace backslashes by slashes */
cur = old = profile;
if (strstr(cur, "\\") != NULL)
{
while (cur != NULL)
{
if ((cur - old) > 1)
{
*cur = '/';
}
old = cur;
cur = strchr(old + 1, '\\');
}
}
snprintf(gdir, sizeof(gdir), "%s/%s/", profile, CFGDIR);
return gdir;
}
/* ======================================================================= */
/*