[SDL] Filesystem routines

Milan Babuskov albis at eunet.yu
Tue Nov 11 07:55:02 PST 2003


Stuart Bingë wrote:
> What I would like to see is a group of functions such as the following:
> 
> SDL_FIND_FILE* SDL_FindFirst(char* path, char* filter);
> int SDL_FindNext(SDL_FIND_FILE* filerec);
> void SDL_FindClose(SDL_FIND_FILE* filerec);

I wouldn't like it. The SDL should be as it is. Simple and easy. Not all 
games need stuff like this. If it is to be added to SDL, it should go to 
some external library. In fact, you can create one yourself :)

> The purpose of which is to iterate through a list of files in a 
> specified directory. We already have standard functions in the various C 
> libraries to read and write files, however there is no real portable way 
> to search for files. The functions that I've described correspond very 
> closely to the Win32 way of iterating through files; from my experience 
> this is a very simple and clean way to do it, although there may be far 
> more efficient ways to do it. The POSIX way of opendir(), readdir() and 
> stat() is not that great, IMHO.

Well, it's not great, you're right, but it's still easy to work with. 
Besides, opendir, readdir, etc. work also on win32, so that is a 
portable way of doing things. Just parse the direntry and see if the 
extension matches... I did this in my game Njam, it's really only a few 
more lines of code comparing to what you suggest.

> Why would you want this? Well, I'm thinking to use these in a setup such 
> as how Quake does things, where data files from later archives in an 
> alphabetically sorted list of archives override data files in earlier 
> archives (e.g. data files in ZZZ.pak override those in AAA.pak). 
> Currently there is no easy/portable way to search a given directory for 
> a list of files such as `*.pak'.

The reasons are understood. However, there is a portable way with 
opendir/readdir/closedir. Not very nice&easy, but works on most 
platforms (Linux, Win32, *BSD, BeOS for sure, others probably).

> Problems with this? Of course. So far I've worked on Win32 and several 
> UNIX'es and the way they represent the filesystem is totally different, 

Not really. Besides, SDL already converts / to \ on windows.

> etc). Leave the problem of portablly representing paths to the user of 
> the functions; the only viable way to do it would be to use relative 
> paths, or perhaps load a base path from a config file.

I thought the same before. But some of the distros had to change my 
sources to insert full paths. For example, if you run executable on 
Linux wiht full path (ex. /usr/local/games/sdl/njam/njam) it doesn't 
change the CWD, so relative paths dont work. So I did something else, 
when the game runs, I get directory with argv[0], and chdir to it.

> char* SDL_GetWorkingDirectory();

Ok. Good idea.

> char* SDL_GetHomeDirectory();  // "~", "C:\Documents and Settings\User\ 
> My Documents\", etc

How is this portable to Windows98?

> int SDL_ChangeWorkingDirectory(char* newcwd);
> int SDL_CreateDirectory(char* dir);
> int SDL_DeletePath(char* dir);    // Used for files & dirs
> int SDL_RenamePath(char* old, char* new); // Used for files & dirs
> 
> These are just rough ideas however, I'm primarily interested in the file 
> search routines. I would greatly appreciate feedback on all of this, as 
> I'm sure my ideas can be improved upon.

The best is to create your own library named: SDL_FileUtils
Make it OpenSource so others can contribute versions for their 
platforms, and that others can add to it. For example, you just make 
search routines and publish it, others can join and add other usefull 
stuff. I'm sure it will be usable, I'm even willing to help you 
depending on my free time.

-- 
Milan Babuskov
http://fbexport.sourceforge.net






More information about the SDL mailing list