[
I think the first copy of this message got eaten, so I’m resending.
Note, I took the time to fix the function which was broken in the first
mail.
]On Sat, Mar 02, 2002 at 05:24:31PM +0100, Walter Haslbeck wrote:
not really SDL specific, but:
How do implement plattform independent handling of
loading/saving files (eg. configuration, hiscore…).
On Unixoid-plattform I can use e.g. /var/games/myapp,
$HOME/.myapprc or $HOME/.myappconfdir/myfile1…, but
how should I handle this on Win32, BeOS or MacOS?
I have an answer for Win32 sortof. The code I’m about to provide is based
on working code, but keep in mind I’m rewriting it here for you to be NT
friendly. It’s not perfect, and it doesn’t cope with the fact that UNIX
and MacOS X prefer .foorc, but everything else would prefer foo.rc or
foo.ini or something. That’s your problem, autoconf is my friend. =)
char *
ExpandPath (char *str)
{
static char buf[PATH_MAX] = “”;
char *s, *p;
s = str;
if (*s == '~')
{
s++;
if (*s == '/' || *s == '\0')
{
/* Current user's home directory */
if ((p = getenv("HOME")))
strncpy(buf, p, PATH_MAX);
#ifdef __WIN32
else if ((p = getenv(“USERPROFILE”)))
strncpy(buf, p, PATH_MAX);
else if ((p = getenv(“WINDIR”)))
strncpy(buf, p, PATH_MAX);
#endif /* __WIN32 /
else
/ This should never happen */
strncpy(buf, “.”, PATH_MAX);
strlcat (buf, s, PATH_MAX);
} else {
/* Get named user's home directory */
if ((p = strchr(s, '/')) != NULL)
{
#ifdef HAVE_GETPWNAM
struct passwd *entry;
*p = ‘\0’;
if ((entry = getpwnam(s)) != NULL)
{
strncpy (buf, entry->pw_dir, PATH_MAX);
if §
{
p = ‘/’;
strlcat (buf, p, PATH_MAX);
}
} else
#endif / HAVE_GETPWNAM /
/ Can’t expand this, leave it alone /
strncpy (buf, str, PATH_MAX);
}
}
} else
/ Does not begin with ~, leave it alone */
strncpy (buf, str, PATH_MAX);
return buf;
}
Caveats:
-
Path seperator / is assumed to work. It does on Win32, UNIX, BeOS,
Linux, and MacOS X. It probably doesn’t on MacOS Classic since / is a
valid filename character IIRC.
-
MacOS Classic does not set HOME. I don’t know what if anything it does
set that we can use. Maybe use the binary’s directory? I don’t like
that idea much.
-
WinXP provides USERPROFILE, HOMEDRIVE, and HOMEPATH. I’m not sure of
these which are provided by any other versions of Win32. I’m going to
assume and hope that USERPROFILE is provided by older NT’s and maybe
Win9x too if I’m lucky. HOMEDRIVE and HOMEPATH must both be used
together otherwise and that’s just aggrovating. I assume that is how
Microsoft provides “compatibility” with the POSIX HOME environment
variable. It would figure, naturally.
-
This function’s not the cleanest thing I’ve ever written. Sorry, it
can’t be helped. You should have seen my first attempt at this
function.
[
- You need to change PATH_MAX to your constant for that purpose. If you
don’t have one, you need one.
]
–
Joseph Carter You expected a coherent reply?
<|Rain|> #define struct union /* great space saver */
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 273 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020302/81952e28/attachment.pgp