Reading file in user's Home directory on MacOS X? (SDL 1.3)

My unix is a little rusty, when I find that “cd ~” takes me to the home directory on my Mac system (when using the command-line interface).

Therefore, I’ve tried to use paths like:

~/Library/Preferences/

to write to or read files from the Preferences folder on MacOS X. However, it fails to open a file for reading or writing. (Null pointer returned from SDL_RWFromFile.)

If I use a path in the program’s own directory, such as the following, it works:

/myData/

[Of course, a filename is appended to each of the paths above.]

I’m wondering if SDL programs are incapable of reading/writing to files inside the user’s home directory, or is the problem simply the ~ character? How would I access the home directory (for things like /Library/Preferences and /Library/Application Support/) without using this character?

There are APIs in Cocoa that will:

  • Expand tilde to a proper path
  • Give you the path to the home directory (Remember that advanced
    users may remap their home directories).
  • Give you more specific directories (like Preferences) which can be
    directly asked for instead of appending to the home directory (better
    practice again in case users modify paths)

Try looking up:
stringByExpandingTildeInPath (category on NSString I think)
NSHomeDirectory()
NSSearchPathForDirectoriesInDomains()

You will obviously have to integrate this platform specific code into
your SDL so it does not affect other platforms. You will probably find
it easiest to implement the backend in a .m file (Objective-C), but
make the public API your SDL app calls a regular C function declared
in a corresponding .h file. In theory, this C function interface could
be generic enough so you can implement a different backend for every
other platform you need to support.

-EricOn 11/11/10, VernJensen wrote:

My unix is a little rusty, when I find that “cd ~” takes me to the home
directory on my Mac system (when using the command-line interface).

Therefore, I’ve tried to use paths like:

~/Library/Preferences/

to write to or read files from the Preferences folder on MacOS X. However,
it fails to open a file for reading or writing. (Null pointer returned from
SDL_RWFromFile.)

If I use a path in the program’s own directory, such as the following, it
works:

/myData/

[Of course, a filename is appended to each of the paths above.]

I’m wondering if SDL programs are incapable of reading/writing to files
inside the user’s home directory, or is the problem simply the ~ character?
How would I access the home directory (for things like /Library/Preferences
and /Library/Application Support/) without using this character?


Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/

The ~ is a shell expansion. It won’t work for direct file system access.

This might help:

– BrianOn 11 November 2010 19:42, VernJensen wrote:

My unix is a little rusty, when I find that “cd ~” takes me to the home
directory on my Mac system (when using the command-line interface).

Thanks Brian! The code there does this:

    struct passwd* pwd = getpwuid(getuid());
    if (pwd)
       homeDir = pwd->pw_dir;

which works perfectly for me on my Mac.

The thing is, the author of that post said you’d have to modify this for Windows. Any idea how it needs to be modified?

-Vern

It needs to be totally written I believe.

This link contains some useful information:
http://msdn.microsoft.com/en-us/library/bb762494(v=VS.85).aspx
Of particular interest are the functions near “See Also” section at the bottom.

– BrianOn 11 November 2010 21:22, VernJensen wrote:

The thing is, the author of that post said you’d have to modify this for
Windows. Any idea how it needs to be modified?

I use this on Windows:
string getHomeDir()
{
char path[PATH_MAX];

if(SHGetSpecialFolderPath(HWND_DESKTOP, path, CSIDL_PERSONAL, FALSE))
return path;

//Error…
}

Brian’s link will help you if you need to do more.

Jonny DOn Thu, Nov 11, 2010 at 4:49 PM, Brian Barrett <brian.ripoff at gmail.com>wrote:

It needs to be totally written I believe.

This link contains some useful information:
http://msdn.microsoft.com/en-us/library/bb762494(v=VS.85).aspx
Of particular interest are the functions near “See Also” section at the
bottom.

– Brian

On 11 November 2010 21:22, VernJensen wrote:

The thing is, the author of that post said you’d have to modify this for
Windows. Any idea how it needs to be modified?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

And that works in Windows? path is a local character array; it lives on the
stack. ‘return path’ should return a pointer to the array ‘path’, which data
is not guaranteed to survive for long.

JeffOn Friday 12 November 2010 09:28, Jonathan Dearborn wrote:

I use this on Windows:
string getHomeDir()
{
char path[PATH_MAX];

if(SHGetSpecialFolderPath(HWND_DESKTOP, path, CSIDL_PERSONAL, FALSE))
return path;

//Error…
}

If that’s C++, the local char array is converted to a new string during
the return, so there’s no problem.On November 12, 2010 2:15:09 pm Jeff Post wrote:

On Friday 12 November 2010 09:28, Jonathan Dearborn wrote:

I use this on Windows:
string getHomeDir()
{
char path[PATH_MAX];

if(SHGetSpecialFolderPath(HWND_DESKTOP, path, CSIDL_PERSONAL, FALSE))

return path;

//Error…
}

And that works in Windows? path is a local character array; it lives on
the stack. ‘return path’ should return a pointer to the array ‘path’,
which data is not guaranteed to survive for long.

Yeah, my language selection was perhaps a bit subtle, but the point was
there. I was going to put a throw in there, which would have made it more
clear. For C, I would take a char* argument and fill that in instead, just
wrapping and simplifying the SHGetSpecialFolderPath call:

void getHomeDir(char* path)
{
SHGetSpecialFolderPath(HWND_DESKTOP, path, CSIDL_PERSONAL, FALSE);
}

You have to be careful to call this with enough memory at that char*.

Jonny DOn Fri, Nov 12, 2010 at 1:39 PM, Stephen Anthony wrote:

On November 12, 2010 2:15:09 pm Jeff Post wrote:

On Friday 12 November 2010 09:28, Jonathan Dearborn wrote:

I use this on Windows:
string getHomeDir()
{
char path[PATH_MAX];

if(SHGetSpecialFolderPath(HWND_DESKTOP, path, CSIDL_PERSONAL, FALSE))

return path;

//Error…
}

And that works in Windows? path is a local character array; it lives on
the stack. ‘return path’ should return a pointer to the array ‘path’,
which data is not guaranteed to survive for long.

If that’s C++, the local char array is converted to a new string during
the return, so there’s no problem.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org