Filehandling

Hi,

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?

any hints?
Walter

Use c++ streams?
Use standard C calls?On Saturday, March 2, 2002, at 10:24 AM, Walter Haslbeck wrote:

Hi,

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?

any hints?
Walter


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Use c++ streams?
Use standard C calls?

I’m pretty sure he was asking for something else…Am Samstag, 2. M?rz 2002 16:29 schrieb David Leimbach:

On Saturday, March 2, 2002, at 10:24 AM, Walter Haslbeck wrote:

Hi,

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?

any hints?
Walter

I haven’t tackled that problem yet, however the first and most important
thing is IMO to use a unified configuration format (and I’ve already got
that, it’s basically a .cfg-file like Quake-engine games have them).

Then, my idea was to have .dflt setting files in the game directory itself.
On *nixes, the filesystem structure of the game would then be duplicated in
~/.name-of-game, e.g. you’d have ~/.name-of-game/config.cfg, but the user
will also be able to put custom models etc… in the directory
(~/.name-of-game/models/modelname.mdl), so the directory structure is really
duplicated.

Now the big question remains, how do you determine the ~/.name-of-game on the
different platforms. We’ve already handled it on Unixoids.
On Windows, you could just replace the $(HOME) by the user’s My Documents
directory. I’m pretty sure that there are functions to get the name of that
directory. In the case of non-multiuser Windowses (Win9x), the replicated
configuration directory structure will simply not exist.

I have no experience with BeOS and MacOS though.

cu,
Nicolai

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?

SDL does provide some platform independant functions to read/write from
different data sources (including files). Have a look at SDL/SDL_rwops.h
in your headers include directory (probably /usr/include)

As for where to save files, the only way I can think of is to define
sort of “base directory” where to put your files on, depending on the
platform the program is compiled for:

#ifdef WIN32
#define BASEDIR “c:\myprog”
#endif

#ifdef LINUX
#define BASEDIR “/var/games/myapp/”
#endif

(WIN32 and LINUX being automatically defined by standard header files -
or maybe even the compiler itself, I don’t exactly know)

Alex.–
http://www.gnurou.org

| 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?

There’s two things you need to do:

  1. Set the correct directory delimiters (’’ ‘/’ ‘:’, etc)
  2. Work out where the files you want to load are

To do 1., take a look at the ‘aliens’ demo
(http://www.libsdl.org/projects/aliens/index.html)

It’s got some code like this at the top of aliens.c:

– cut –
#ifdef macintosh
#define DIR_SEP “:”
#define DIR_CUR “:”
#else
#define DIR_SEP “/”
#define DIR_CUR “”
#endif
#define DATAFILE(X) DIR_CUR “data” DIR_SEP X
– cut –

You’d need to work out how to check if you’re on a mac, PC, or whatever
though.

  1. is a bit more involved, and I’m not sure (you’d need to know where
    the program was installed to, maybe, but Windows users don’t have
    homedirectories really).On Sat, Mar 02, 2002 at 05:24:31PM +0100, Walter Haslbeck wrote:


Spitwads are not free speech
6AD6 865A BF6E 76BB 1FC2 | www.piku.org.uk/public-key.asc
E4C4 DEEA 7D08 D511 E149 | www.piku.org.uk wnzrf at cvxh.bet.hx (rot13’d)

Ah … I misunderstood. Its the filenames in question… I suppose a
SDL datafile project could handle that portably somehow and answer the
questions for you.

Does such a thing exist? Should it? :)On Saturday, March 2, 2002, at 09:29 AM, David Leimbach wrote:

Use c++ streams?
Use standard C calls?
On Saturday, March 2, 2002, at 10:24 AM, Walter Haslbeck wrote:

Hi,

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?

any hints?
Walter


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Maybe you should take a look at PhysFS:
http://www.icculus.org/physfs/

AlexAm Sam, 2002-03-02 um 11.24 schrieb Walter Haslbeck:

Hi,

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?

David Leimbach wrote:

Ah … I misunderstood. Its the filenames in question… I suppose a
SDL datafile project could handle that portably somehow and answer the
questions for you.

Does such a thing exist? Should it? :slight_smile:

Don’t know. Yes. :slight_smile:

Walter

Alexander Pipelka wrote:

http://www.icculus.org/physfs/

Thanks, I’ll take a look at it soon.

Walter

James wrote:

To do 1., take a look at the ‘aliens’ demo
(http://www.libsdl.org/projects/aliens/index.html)

ah, thanks!

  1. is a bit more involved, and I’m not sure (you’d need to know where
    the program was installed to, maybe, but Windows users don’t have
    homedirectories really).

Does it make sense to use the registry instead of a configfile
on Win32?

Walter

Nicolai Haehnle <prefect_ at gmx.net> wrote:

I haven’t tackled that problem yet, however the first and most important
thing is IMO to use a unified configuration format (and I’ve already got
that, it’s basically a .cfg-file like Quake-engine games have them).

I think I will use plain ASCII files with a simple syntax.

Now the big question remains, how do you determine the ~/.name-of-game on the
different platforms. We’ve already handled it on Unixoids.
On Windows, you could just replace the $(HOME) by the user’s My Documents
directory. I’m pretty sure that there are functions to get the name of that

Is the path of that directory stored on some environment-variable on
Win or is there a Win32API call?

I have no experience with BeOS and MacOS though.

So I have to do some research :slight_smile:

Walter

David Leimbach wrote:

Use c++ streams?
Use standard C calls?

I use standard ANSI C.

Walter

Ah … I misunderstood. Its the filenames in question… I suppose a
SDL datafile project could handle that portably somehow and answer the
questions for you.

Does such a thing exist? Should it? :slight_smile:

http://icculus.org/physfs/

–ryan.

| > 2. is a bit more involved, and I’m not sure (you’d need to know where
| > the program was installed to, maybe, but Windows users don’t have
| > homedirectories really).
|
| Does it make sense to use the registry instead of a configfile
| on Win32?

I suppose. And in NT4 upwards it seems to be normal. However then you’d
need to somehow talk to the registry, which would involve including
windows.h (and random bits of the MFC I think), and then your program’s
nolonger portable unless you hide all the windows stuff behind some
#ifdef#endif pairs (which would make sense really).

Talking to the registry using MSVC++ is quite easy, it’s the same as
writing to an ini file, only in NT4 > it goes into the registry. The
stuff in the MSDN that deals with it isn’t too conflicting or confusing
(for once!).

And then there’s the mac (OS9,8,7 or X?) :-)On Sat, Mar 02, 2002 at 09:32:51PM +0100, Walter Haslbeck wrote:


Garlic gum is not funny
6AD6 865A BF6E 76BB 1FC2 | www.piku.org.uk/public-key.asc
E4C4 DEEA 7D08 D511 E149 | www.piku.org.uk wnzrf at cvxh.bet.hx (rot13’d)

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[MAX_PATH] = “”;
char *s, *p;

s = str;
if (*s == '~')
{
    s++;
if (*s == '/' || *s == '\0')
{
    /* Current user's home directory */
    if ((p = getenv("HOME")))
        strncpy(buf, p, MAX_PATH);

#ifdef __WIN32
else if ((p = getenv(“USERPROFILE”)))
strncpy(buf, p, MAX_PATH);
else if ((p = getenv(“WINDIR”)))
strncpy(buf, p, MAX_PATH);
#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, MAX_PATH);
}
}
} 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. ;)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?


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/8ed6b1d1/attachment.pgp

Caveats:
[snip]

Not to advertise my own projects too heavily, but a lot of this could be
of more use to PhysicsFS, and has less and less to do with SDL. Can we
move this to the physfs mailing list?

Joseph Carter You expected a coherent reply?

<|Rain|> #define struct union /* great space saver */

Btw, even when I’m in a hurry, I stop to read your signatures. :slight_smile:

–ryan.

Well… Joe Random Developer’s own program wouldn’t be #including
"windows.h"… the library (SDL_config or SDL_settings or whatever)
would.

I’ve wanted a portable way of doing config files ever since I cross-compiled
my first game to Windows. :slight_smile:

Unix - $HOME/.progrc
Windows - ???\proc.ini
MacOS - ???:???
BeOS - ???

-bill!On Sat, Mar 02, 2002 at 07:09:31PM +0000, James wrote:

I suppose. And in NT4 upwards it seems to be normal. However then you’d
need to somehow talk to the registry, which would involve including
windows.h (and random bits of the MFC I think), and then your program’s
nolonger portable unless you hide all the windows stuff behind some
#ifdef#endif pairs (which would make sense really).

char *
ExpandPath (char *str)
[…]

Caveats:
[…]

  • The function is broken as shipped. =) I merged the win32 and everyone
    else versions of the function when I wrote the version in my message.
    The win32 version used MAX_PATH, the rest of the code uses MAX_PATH.
    If you have a constant already, use that. If not, you’re gonna need
    one. It’s better to have none, but this is a game and performance is
    generally important, so even if the OS doesn’t have an upper limit
    (such as HURD), we still have to have one.On Sat, Mar 02, 2002 at 11:12:07AM -0800, Joseph Carter wrote:


Joseph Carter Not many fishes

A subversive is anyone who can out-argue their government.

-------------- 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/e61ef567/attachment.pgp

I’ve wanted a portable way of doing config files ever since I cross-compiled
my first game to Windows. :slight_smile:

Unix - $HOME/.progrc
Windows - ???\proc.ini
MacOS - ???:???
BeOS - ???

-bill!

This is what (and all) libfnkdat does :).
http://www.maccormack.net/~djm/libfnkdat/

DaveOn Sat, 2 Mar 2002, nbs wrote:

David MacCormack
@David_MacCormack

:wq
damn!

[
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. :wink:

[

  • 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