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:
John Popplewell 2005-07-19 04:44:31 +00:00
parent 3f07d39e0e
commit d9f70cfde6
3 changed files with 87 additions and 9 deletions

View file

@ -372,7 +372,7 @@ static void win32_perror(const char * const str)
#define NOMINMAX #define NOMINMAX
#include "Shlwapi.h" #include "Shlwapi.h"
#define strcasestr StrStrI #define strcasestr StrStrI
#endif #endif /* WIN32 */
#ifdef __GNUC__ #ifdef __GNUC__
@ -7342,20 +7342,31 @@ static void setup(int argc, char * argv[])
SDL_Surface * tmp_imgcurup, * tmp_imgcurdown; SDL_Surface * tmp_imgcurup, * tmp_imgcurdown;
#ifdef __BEOS__ #if defined(__BEOS__) || defined(WIN32)
/* if run from gui, like OpenTracker in BeOS or Explorer in Windows, /* if run from gui, like OpenTracker in BeOS or Explorer in Windows,
find path from which binary was run and change dir to it find path from which binary was run and change dir to it
so all files will be local :) */ so all files will be local :) */
/* UPDATE (2004.10.06): Since SDL 1.2.7 SDL sets that path correctly, /* UPDATE (2004.10.06): Since SDL 1.2.7 SDL sets that path correctly,
so this code wouldn't be needed if SDL was init before anything else, so this code wouldn't be needed if SDL was init before anything else,
(just basic init, window shouldn't be needed). */ (just basic init, window shouldn't be needed). */
/* UPDATE (2005 July 19): Enable and make work on Windows. Makes testing
with MINGW/MSYS easier */
if (argc && argv[0]) if (argc && argv[0])
{ {
char * slash = strrchr(argv[0], '/'); char *app_path = strdup(argv[0]);
*(slash + 1) = '\0'; char *slash = strrchr(app_path, '/');
chdir(argv[0]);
*(slash + 1) = '/'; if (!slash)
{
slash = strrchr(app_path, '\\');
}
if (slash)
{
*(slash + 1) = '\0';
chdir(app_path);
}
free(app_path);
} }
#endif #endif
@ -7400,7 +7411,7 @@ static void setup(int argc, char * argv[])
#ifdef WIN32 #ifdef WIN32
savedir = strdup("userdata"); savedir = GetDefaultSaveDir("TuxPaint");
#elif __BEOS__ #elif __BEOS__
savedir = strdup("./userdata"); savedir = strdup("./userdata");
#else #else
@ -11600,8 +11611,9 @@ static char * get_fname(const char * const name)
and where the "current_id.txt" file is saved */ and where the "current_id.txt" file is saved */
#ifdef WIN32 #ifdef WIN32
/* Windows predefines "savedir" as "userdata", though it may get /* Windows predefines "savedir" as:
overridden with "--savedir" option */ "C:\Documents and Settings\%USERNAME%\Application Data\TuxPaint"
though it may get overridden with "--savedir" option */
snprintf(f, sizeof(f), "%s/%s", savedir, name); snprintf(f, sizeof(f), "%s/%s", savedir, name);

View file

@ -24,6 +24,7 @@
/* - prints using 24-bit (not 32-bit) bitmap */ /* - prints using 24-bit (not 32-bit) bitmap */
#include <windows.h> #include <windows.h>
#include <direct.h>
#include "SDL_syswm.h" #include "SDL_syswm.h"
#include "win32_print.h" #include "win32_print.h"
@ -470,3 +471,65 @@ error:
return res; 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");
}

View file

@ -19,4 +19,7 @@ extern const char *SurfacePrint( SDL_Surface *surf,
int showdialog ); int showdialog );
extern int IsPrinterAvailable( void ); extern int IsPrinterAvailable( void );
/* additional windows functions requiring <windows.h> */
extern char *GetDefaultSaveDir(const char *suffix);
#endif #endif