Added GetDefaultSaveDir(suffix) which fetches the default "Application
Data" path for the current user from the registry, appends the suffix, creates the subdirectory if it doesn't exist, then returns a string from the heap. This path is the new default "savedir" location on Windows. Tested on 98/2K/XP. Changed code that sets the pwd to the directory containing the executable. Originally for BEOS only, now working and enabled for Windows also.
This commit is contained in:
parent
3f07d39e0e
commit
d9f70cfde6
3 changed files with 87 additions and 9 deletions
|
|
@ -24,6 +24,7 @@
|
|||
/* - prints using 24-bit (not 32-bit) bitmap */
|
||||
|
||||
#include <windows.h>
|
||||
#include <direct.h>
|
||||
#include "SDL_syswm.h"
|
||||
#include "win32_print.h"
|
||||
|
||||
|
|
@ -470,3 +471,65 @@ error:
|
|||
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
Read access to Windows Registry
|
||||
*/
|
||||
static HRESULT ReadRegistry(const char *key, const char *option, char *value, int size)
|
||||
{
|
||||
LONG res;
|
||||
HKEY hKey = NULL;
|
||||
|
||||
res = RegOpenKeyEx(HKEY_CURRENT_USER, key, 0, KEY_READ, &hKey);
|
||||
if (res != ERROR_SUCCESS)
|
||||
goto err_exit;
|
||||
res = RegQueryValueEx(hKey, option, NULL, NULL, (LPBYTE)value, (LPDWORD)&size);
|
||||
if (res != ERROR_SUCCESS)
|
||||
goto err_exit;
|
||||
res = ERROR_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
if (hKey) RegCloseKey(hKey);
|
||||
return HRESULT_FROM_WIN32(res);
|
||||
}
|
||||
|
||||
/*
|
||||
Removes a single '\' or '/' from end of path
|
||||
*/
|
||||
static char *remove_slash(char *path)
|
||||
{
|
||||
int len = strlen(path);
|
||||
|
||||
if (!len)
|
||||
return path;
|
||||
|
||||
if (path[len-1] == '/' || path[len-1] == '\\')
|
||||
path[len-1] = 0;
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns heap string containing default application data path.
|
||||
Creates suffix subdirectory (only one level).
|
||||
E.g. C:\Documents and Settings\jfp\Application Data\suffix
|
||||
*/
|
||||
char *GetDefaultSaveDir(const char *suffix)
|
||||
{
|
||||
char prefix[MAX_PATH];
|
||||
char path[2*MAX_PATH];
|
||||
const char *key = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
|
||||
const char *option = "AppData";
|
||||
HRESULT hr = S_OK;
|
||||
|
||||
if (SUCCEEDED(hr = ReadRegistry(key, option, prefix, sizeof(prefix))))
|
||||
{
|
||||
remove_slash(prefix);
|
||||
snprintf(path, sizeof(path), "%s/%s", prefix, suffix);
|
||||
_mkdir(path);
|
||||
return strdup(path);
|
||||
}
|
||||
return strdup("userdata");
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue