Saving user data in "%APPDATA%/Application Data" on windows.

Some cleanups.


git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@503 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
meep-eep
2002-12-30 01:40:44 +00:00
parent f66a9b7f74
commit 51419e5ee7
32 changed files with 187 additions and 145 deletions
+1
View File
@@ -1,4 +1,5 @@
0.2: 0.2:
- Saving user data in "%APPDATA%/Application Data" on windows - SvdB
- Melnorme comm fix, from TD. - Melnorme comm fix, from TD.
- Planet scan font character fixes (micro.fon), from fOSSiL - Planet scan font character fixes (micro.fon), from fOSSiL
- Temporary files are deleted on exit. - SvdB - Temporary files are deleted on exit. - SvdB
+1 -3
View File
@@ -27,9 +27,7 @@
# define _NO_PROTO # define _NO_PROTO
#endif #endif
#ifdef HAVE_CONFIG_H #include <config.h>
# include <config.h>
#endif
#ifndef HAVE_GETOPT_H #ifndef HAVE_GETOPT_H
#if !defined __STDC__ || !__STDC__ #if !defined __STDC__ || !__STDC__
-2
View File
@@ -18,9 +18,7 @@
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */ 02111-1307 USA. */
#ifdef HAVE_CONFIG_H
#include <config.h> #include <config.h>
#endif
#ifndef HAVE_GETOPT_H #ifndef HAVE_GETOPT_H
#include "getopt.h" #include "getopt.h"
+2 -2
View File
@@ -42,7 +42,7 @@ RSC=rc.exe
# PROP Ignore_Export_Lib 0 # PROP Ignore_Export_Lib 0
# PROP Target_Dir "" # PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /MD /W3 /GX /O2 /I "." /I ".." /I "..\sc2code" /I "..\sc2code\libs" /I "..\sc2code\ships" /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D _VW=320 /D _VH=240 /D "HAVE_CONFIG_H" /D "HAVE_OPENGL" /D "GFXMODULE_SDL" /D "SOUNDMODULE_OPENAL" /YX /FD /c # ADD CPP /nologo /MD /W3 /GX /O2 /I "." /I ".." /I "..\sc2code" /I "..\sc2code\libs" /I "..\sc2code\ships" /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D _VW=320 /D _VH=240 /D "HAVE_OPENGL" /D "GFXMODULE_SDL" /D "SOUNDMODULE_OPENAL" /YX /FD /c
# ADD BASE RSC /l 0x409 /d "NDEBUG" # ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG" # ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe BSC32=bscmake.exe
@@ -66,7 +66,7 @@ LINK32=link.exe
# PROP Ignore_Export_Lib 0 # PROP Ignore_Export_Lib 0
# PROP Target_Dir "" # PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "." /I ".." /I "..\sc2code" /I "..\sc2code\libs" /I "..\sc2code\ships" /D "_DEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D _VW=320 /D _VH=240 /D "HAVE_CONFIG_H" /D "HAVE_OPENGL" /D "GFXMODULE_SDL" /D "SOUNDMODULE_OPENAL" /FR /YX /FD /GZ /c # ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "." /I ".." /I "..\sc2code" /I "..\sc2code\libs" /I "..\sc2code\ships" /D "_DEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D _VW=320 /D _VH=240 /D "HAVE_OPENGL" /D "GFXMODULE_SDL" /D "SOUNDMODULE_OPENAL" /FR /YX /FD /GZ /c
# ADD BASE RSC /l 0x409 /d "_DEBUG" # ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG" # ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe BSC32=bscmake.exe
+2 -1
View File
@@ -12,7 +12,8 @@
#define CONTENTDIR "../content" #define CONTENTDIR "../content"
/* Directory where game data will be stored */ /* Directory where game data will be stored */
#define USERDIR "../userdata/" //#define USERDIR "../userdata/"
#define USERDIR "%APPDATA%/uqm/"
/* Directory where supermelee teams will be stored */ /* Directory where supermelee teams will be stored */
#define MELEEDIR USERDIR "teams/" #define MELEEDIR USERDIR "teams/"
+1 -10
View File
@@ -26,21 +26,12 @@
#include "compiler.h" #include "compiler.h"
#include "options.h" #include "options.h"
#include "file.h" #include "file.h"
#include "config.h"
int optWhichMusic = MUSIC_3DO; int optWhichMusic = MUSIC_3DO;
char *configDir, *saveDir, *meleeDir; char *configDir, *saveDir, *meleeDir;
BOOLEAN optSubtitles = TRUE; BOOLEAN optSubtitles = TRUE;
BOOLEAN FileExists (const char *filename)
{
FILE *fp;
if ((fp = fopen (filename, "rb")) == NULL)
return FALSE;
fclose (fp);
return TRUE;
}
void void
prepareConfigDir (void) { prepareConfigDir (void) {
configDir = malloc (PATH_MAX - 13); configDir = malloc (PATH_MAX - 13);
-2
View File
@@ -35,8 +35,6 @@ extern int optWhichMusic;
extern BOOLEAN optSubtitles; extern BOOLEAN optSubtitles;
BOOLEAN FileExists (const char *filename);
extern char *configDir; extern char *configDir;
extern char *meleeDir; extern char *meleeDir;
extern char *saveDir; extern char *saveDir;
-4
View File
@@ -19,10 +19,6 @@
#ifndef _COMPILER_H #ifndef _COMPILER_H
#define _COMPILER_H #define _COMPILER_H
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
typedef unsigned char BYTE; typedef unsigned char BYTE;
typedef BYTE *PBYTE; typedef BYTE *PBYTE;
typedef unsigned char UBYTE; typedef unsigned char UBYTE;
+7 -2
View File
@@ -38,13 +38,18 @@
# include <unistd.h> # include <unistd.h>
#endif #endif
// for BOOLEAN
#include "compiler.h"
void initTempDir (void); void initTempDir (void);
void unInitTempDir (void); void unInitTempDir (void);
char *tempFilePath (const char *filename); char *tempFilePath (const char *filename);
int mkdirhier (const char *path); int mkdirhier (const char *path);
const char *getHomeDir (void); const char *getHomeDir (void);
int createDirectory(const char *dir, int mode); int createDirectory (const char *dir, int mode);
int expandPath(char *dest, size_t len, const char *src); int expandPath (char *dest, size_t len, const char *src);
BOOLEAN fileExists (const char *name);
int getUserDataDir (char *dir, size_t len);
#endif /* _FILE_H */ #endif /* _FILE_H */
+166 -36
View File
@@ -21,15 +21,33 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#ifdef WIN32
#include <direct.h>
#endif
#include "port.h" #include "port.h"
#include "config.h" #include "config.h"
#include "filintrn.h" #include "filintrn.h"
#include "compiler.h"
#ifdef WIN32
# include <direct.h>
// this would lead to conflicts
//# include <shlobj.h>
typedef void *HWND;
typedef void *HANDLE;
typedef char *LPTSTR;
typedef long HRESULT;
#define SUCCEEDED(Status) ((HRESULT) (Status) >= 0)
HRESULT __stdcall SHGetFolderPathA (HWND hwndOwner, int nFolder,
HANDLE hToken, DWORD dwFlags, LPTSTR pszPath);
#define SHGetFolderPath SHGetFolderPathA
#define CSIDL_APPDATA 0x001a // <user name>\Application Data
#define CSIDL_FLAG_CREATE 0x8000
// combine with CSIDL_ value to force folder creation in
// SHGetFolderPath()
#endif
int int
createDirectory(const char *dir, int mode) { createDirectory(const char *dir, int mode)
{
#ifdef WIN32 #ifdef WIN32
return mkdir (dir); return mkdir (dir);
#else #else
@@ -41,7 +59,8 @@ createDirectory(const char *dir, int mode) {
// returns 0 on success, -1 on failure. // returns 0 on success, -1 on failure.
// on failure, some parts may still have been created. // on failure, some parts may still have been created.
int int
mkdirhier (const char *path) { mkdirhier (const char *path)
{
char *buf; // buffer char *buf; // buffer
char *ptr; // end of the string in buf char *ptr; // end of the string in buf
const char *pathstart; // start of a component of path const char *pathstart; // start of a component of path
@@ -67,7 +86,8 @@ mkdirhier (const char *path) {
} }
// walk through the path as long as the components exist // walk through the path as long as the components exist
while (1) { while (1)
{
pathend = strchr (pathstart, '/'); pathend = strchr (pathstart, '/');
if (pathend == NULL) if (pathend == NULL)
pathend = path + len; pathend = path + len;
@@ -75,8 +95,10 @@ mkdirhier (const char *path) {
ptr += pathend - pathstart; ptr += pathend - pathstart;
*ptr = '\0'; *ptr = '\0';
if (stat (buf, &statbuf) == -1) { if (stat (buf, &statbuf) == -1)
if (errno != ENOENT) { {
if (errno != ENOENT)
{
fprintf (stderr, "Can't stat %s: %s\n", buf, fprintf (stderr, "Can't stat %s: %s\n", buf,
strerror (errno)); strerror (errno));
return -1; return -1;
@@ -99,8 +121,10 @@ mkdirhier (const char *path) {
} }
// create all components left // create all components left
while (1) { while (1)
if (createDirectory (buf, 0777) == -1) { {
if (createDirectory (buf, 0777) == -1)
{
fprintf (stderr, "Error: Can't create %s: %s\n", buf, fprintf (stderr, "Error: Can't create %s: %s\n", buf,
strerror (errno)); strerror (errno));
return -1; return -1;
@@ -133,15 +157,28 @@ mkdirhier (const char *path) {
// Get the user's home dir // Get the user's home dir
// returns a pointer to a static buffer // returns a pointer to a static buffer
const char * const char *
getHomeDir(void) { getHomeDir (void)
{
return getenv ("HOME"); return getenv ("HOME");
} }
// Expand ~/ in a path, and replaces \ by / on Windows.
// Environment variable parsing could be added here if we need it.
int int
expandPath(char *dest, size_t len, const char *src) { expandPath (char *dest, size_t len, const char *src)
size_t srclen; {
const char *destend;
srclen = strlen (src); #define CHECKLEN(n) \
if (dest + (n) > destend) \
{ \
errno = ENAMETOOLONG; \
return -1; \
} \
else \
(void) 0
destend = dest + len;
if (src[0] == '~') if (src[0] == '~')
{ {
const char *home; const char *home;
@@ -161,39 +198,132 @@ expandPath(char *dest, size_t len, const char *src) {
} }
homelen = strlen (home); homelen = strlen (home);
if (homelen + srclen > len) CHECKLEN (homelen);
{ memcpy (dest, home, homelen);
errno = ENAMETOOLONG;
return -1;
}
memcpy(dest, home, homelen);
dest += homelen; dest += homelen;
src++; /* skip the ~ */ src++; /* skip the ~ */
} else {
if (strlen (src) >= len)
{
errno = ENAMETOOLONG;
return -1;
}
} }
#ifdef WIN32
/* Replaces backslashes in path by slashes. */
while (*src != '\0') while (*src != '\0')
{ {
if (*src == '\\') switch (*src)
{ {
*(dest++) = '/'; #ifdef WIN32
src++; case '\\':
/* Replaces backslashes in path by slashes in Windows. */
CHECKLEN(1);
*(dest++) = '/';
src++;
break;
#endif
#ifdef WIN32
case '%':
{
/* Environment variable substitution in Windows */
const char *end; // end of env var name in src
const char *envVar;
char *envName;
size_t envNameLen, envVarLen;
src++;
end = strchr (src, '%');
if (end == NULL)
{
errno = EINVAL;
return -1;
}
envNameLen = end - src;
envName = malloc (envNameLen + 1);
memcpy (envName, src, envNameLen + 1);
envName[envNameLen] = '\0';
envVar = getenv (envName);
free (envName);
if (envVar == NULL)
{
#if 1
if (strncmp (src, "APPDATA", envNameLen) == 0)
{
// fallback for when the APPDATA env var is not set
if (getUserDataDir (dest, destend - dest) == -1)
return -1;
dest += strlen (dest);
src = end + 1;
break;
}
#endif
// Substitute an empty string
src = end + 1;
break;
}
envVarLen = strlen (envVar);
CHECKLEN (envVarLen);
strcpy (dest, envVar);
dest += envVarLen;
src = end + 1;
break;
}
#endif
#if 0
case '$':
/* Environment variable substitution for Unix */
// not implemented
break;
#endif
default:
CHECKLEN(1);
*(dest++) = *(src++);
break;
} }
else
*(dest++) = *(src++);
} }
*dest = '\0'; *dest = '\0';
#else
strcpy(dest, src);
#endif
return 0; return 0;
} }
// returns 0 on success, and fills dir with the path to the dir where there
// user data is stored.
// return -1 on error, and sets errno.
int
getUserDataDir (char *dir, size_t len)
{
#ifdef WIN32
char buf[PATH_MAX];
if (!SUCCEEDED (SHGetFolderPath(NULL,
CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, buf)))
{
errno = ENOENT;
return -1;
}
if (strlen (buf) >= len)
{
errno = ENAMETOOLONG;
return -1;
}
strcpy (dir, buf);
return 0;
#else
const char *homeDir;
homeDir = getHomeDir();
if (strlen (homeDir) >= len)
{
errno = ENAMETOOLONG;
return -1;
}
strcpy (dir, homeDir);
return 0;
#endif
}
BOOLEAN
fileExists (const char *name)
{
return access (name, F_OK) == 0;
}
-3
View File
@@ -20,8 +20,5 @@
#include "../file.h" #include "../file.h"
void CreateDirectory (const char *dir, int mode);
void RemoveTempDir (void);
#endif /* _FILEINTRN_H */ #endif /* _FILEINTRN_H */
@@ -34,10 +34,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
@@ -28,10 +28,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
@@ -28,10 +28,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mikmod_internals.h" #include "mikmod_internals.h"
MIKMODAPI extern struct MDRIVER drv_openal; MIKMODAPI extern struct MDRIVER drv_openal;
@@ -28,10 +28,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
@@ -28,10 +28,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_MEMORY_H #ifdef HAVE_MEMORY_H
#include <memory.h> #include <memory.h>
#endif #endif
@@ -28,10 +28,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mikmod_internals.h" #include "mikmod_internals.h"
void MikMod_RegisterAllLoaders_internal(void) void MikMod_RegisterAllLoaders_internal(void)
@@ -28,10 +28,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_MEMORY_H #ifdef HAVE_MEMORY_H
#include <memory.h> #include <memory.h>
#endif #endif
@@ -28,10 +28,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mikmod_internals.h" #include "mikmod_internals.h"
/* Same as malloc, but sets error variable _mm_error when fails */ /* Same as malloc, but sets error variable _mm_error when fails */
@@ -36,10 +36,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mikmod_internals.h" #include "mikmod_internals.h"
CHAR *_mm_errmsg[MMERR_MAX+1] = CHAR *_mm_errmsg[MMERR_MAX+1] =
@@ -54,10 +54,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -30,10 +30,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#ifdef SUNOS #ifdef SUNOS
@@ -28,10 +28,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h> #include <string.h>
#include "mikmod_internals.h" #include "mikmod_internals.h"
@@ -32,10 +32,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h> #include <string.h>
#include "mikmod_internals.h" #include "mikmod_internals.h"
@@ -28,10 +28,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mikmod_internals.h" #include "mikmod_internals.h"
UWORD npertab[7*OCTAVE]={ UWORD npertab[7*OCTAVE]={
@@ -29,10 +29,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mikmod_internals.h" #include "mikmod_internals.h"
static int sl_rlength; static int sl_rlength;
@@ -36,10 +36,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
@@ -37,10 +37,6 @@
#ifdef SOUNDMODULE_OPENAL #ifdef SOUNDMODULE_OPENAL
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
+1 -1
View File
@@ -143,7 +143,7 @@ _GetMusicData (FILE *fp, DWORD length)
strcpy (threedoname, filename); strcpy (threedoname, filename);
strcpy (&threedoname[strlen (threedoname)-3], "ogg"); strcpy (&threedoname[strlen (threedoname)-3], "ogg");
if (FileExists (threedoname)) if (fileExists (threedoname))
{ {
strcpy (filename, threedoname); strcpy (filename, threedoname);
} }
+1 -1
View File
@@ -178,7 +178,7 @@ _GetMusicData (FILE *fp, DWORD length)
strcpy (threedoname, filename); strcpy (threedoname, filename);
strcpy (&threedoname[strlen (threedoname)-3], "ogg"); strcpy (&threedoname[strlen (threedoname)-3], "ogg");
if (FileExists (threedoname)) if (fileExists (threedoname))
{ {
strcpy (filename, threedoname); strcpy (filename, threedoname);
} }
+1 -3
View File
@@ -41,9 +41,7 @@ extern int ScreenHeight;
#define SIS_SCREEN_WIDTH (SPACE_WIDTH - 14) #define SIS_SCREEN_WIDTH (SPACE_WIDTH - 14)
#define SIS_SCREEN_HEIGHT (SPACE_HEIGHT - 13) #define SIS_SCREEN_HEIGHT (SPACE_HEIGHT - 13)
#ifdef HAVE_CONFIG_H #include "config.h"
# include "config.h"
#endif
#include "../port.h" #include "../port.h"
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
+4 -7
View File
@@ -22,9 +22,6 @@
#ifdef WIN32 #ifdef WIN32
# include <direct.h> # include <direct.h>
#endif #endif
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_GETOPT_H #ifdef HAVE_GETOPT_H
#include <getopt.h> #include <getopt.h>
@@ -50,11 +47,11 @@ CDToContentDir (char *contentdir)
{ {
const char *testfile = "starcon.txt"; const char *testfile = "starcon.txt";
if (!FileExists (testfile)) if (!fileExists (testfile))
{ {
if ((chdir (contentdir) || !FileExists (testfile)) && if ((chdir (contentdir) || !fileExists (testfile)) &&
(chdir ("content") || !FileExists (testfile)) && (chdir ("content") || !fileExists (testfile)) &&
(chdir ("../../content") || !FileExists (testfile))) { (chdir ("../../content") || !fileExists (testfile))) {
fprintf(stderr, "Fatal error: content not available, running from wrong dir?\n"); fprintf(stderr, "Fatal error: content not available, running from wrong dir?\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }