Sign in to follow this  
Acharis

Cross platform directory list

Recommended Posts

Acharis    5979
I need a crossplatform function to list a directory and then do something on the files inside (delete all files from there or copy files to another directory). I was doing it several decades ago and I'm not familiar how it's done nowadays, especially crosplatform :)


Example code (I think it was using <dos.h>):
[CODE]
void fClearDir(char* sp)
{
char sf[250];
char *n;
DIR *dirp;
struct dirent *direntp;
dirp = opendir( sp );
if( dirp != NULL ) {
for(;;) {
direntp = readdir( dirp );
if( direntp == NULL ) break;
n=direntp->d_name;
if (strcmp(n,".")!=0 && strcmp(n,"..")!=0)
{
strcpy(sf,sp);strcat(sf,n);
remove(sf);
}
}
closedir( dirp );
}
}
[/CODE]

I need a modern replacement of this (crossplatform Windows+Mac+Linux).

Share this post


Link to post
Share on other sites
SiCrane    11839
One option is [url=http://www.boost.org/doc/libs/1_52_0/libs/filesystem/doc/index.htm]boost::filesystem[/url].

Share this post


Link to post
Share on other sites
Acharis    5979
Well, I just need one small feature (iterate through directory) a full library would be an overkill in my case... Aren't there any standard functions for this? Something that could just go through all files in a directory regardless of platform?

Share this post


Link to post
Share on other sites
Bregma    9214
[quote name='Acharis' timestamp='1353953021' post='5004256']
Aren't there any standard functions for this?
[/quote]
No, the concept of file hierarchies is pretty platform-specific. Like the concept of a GUI, there is no standard way to do something that's not standard across platforms.

Unless you really think it's really necessary to have a hierarchical filesystem on a toaster. Or a phone.

Share this post


Link to post
Share on other sites
Acharis    5979
Hmmm, it's more messy than I thought. OK, I can lower the requirements to "computers" only (Windows/Mac/Linux).

Does all Linux version use the same file hierarchy (I'm quite sure these does, just asking because of paranoia)?
Does Mac use the same file hierarchy (and therefore the same code can be used) as Linux?

Or to put shorter, can we narrow it to 2 versions of the code, one for Windows and one for Linux/Mac? Again, I'm just asking for basic iterate through a directory code.

Share this post


Link to post
Share on other sites
SiCrane    11839
Are you asking if they are the same file hierarchy or the same functions to enumerate directories? If you're asking the former, then no, different linux distributions and OS X will organize their directories differently. However, both Linux and OS X both support the POSIX functions for enumerating directories and files in directories (the previously mentioned opendir() and friends). The Windows API functions for enumerating entities within directories are FindFirstFile(Ex)() and related functions. If you want to support Mac classic on top of OS X then you've got another API you'll need to use. Or again you can download a library to emulate the POSIX calls on Windows and only need to write the code once.

Share this post


Link to post
Share on other sites
Acharis    5979
I stumbled across something called "dirent.h" (apparently there is more than one version of this). How you evaluate it, is it good? If yes and you used it, can you drop a link to the implementation you used and can recommend?

Share this post


Link to post
Share on other sites
SiCrane    11839
dirent.h is the POSIX header for directory functions, which contains opendir() and the other related functions that I've already told you about twice now. For Linux and OS X there's no need to download an implementation, it's part of the OS.

Share this post


Link to post
Share on other sites
Acharis    5979
Oh, I understand, this is the "library" (consisting of exactly one .h file, which is extremely nice) to emulate POSIX on Windows and the other versions of dirent.h were the original files for *nix systems :) So, I just use this header for Windows version and for other versions I just use the default one.

Just to be sure, we are talking about this POSIX Windows API http://softagalleria.net/dirent.php and it will work with all IDEs (it says "Dirent API for Microsoft Visual Studio")?

Share this post


Link to post
Share on other sites
ChaosEngine    5185
Seriously, you're going to an awful lot of trouble to avoid using the library. There's no need to reinvent the wheel, just download boost.filesystem and be done with it. It has a much nicer API if you're writing in C++ anyway.

Besides, there's an awful lot of good, useful stuff in boost.

Share this post


Link to post
Share on other sites
kop0113    2453
Please don't drag in boost::filesystem just to provide this single functionality, that would be a dependency pain and add to the filesize of your project needlessly.

Since Windows is the only OS that needs bespoke code, I typically use the standard POSIX code on all operating systems and then a simple dirent.h API shim for windows.

Such as...

http://slam6d.sourceforge.net/html/doxygen/dirent_8h_source.html
or
http://users.cis.fiu.edu/~weiss/cop4338_sum07/dirent.h
or
http://softagalleria.net/dirent.php

I think that it is also provided when using GCC on Windows (Mingw), but it is also definitely provided by Cygwin. Edited by Karsten_

Share this post


Link to post
Share on other sites
ChaosEngine    5185
[quote name='Karsten_' timestamp='1353977091' post='5004367']
Please don't drag in boost::filesystem just to provide this single functionality
[/quote]

Right, because when you iterate over the contents of a directory that's all you're ever going to want to do with the file system. [img]http://public.gamedev.net//public/style_emoticons/default/rolleyes.gif[/img]

Oh wait....
[quote name='Acharis' timestamp='1353947969' post='5004227']
I need a crossplatform function to list a directory and then [b]do something on the files inside (delete all files from there or copy files to another directory)[/b].
[/quote]

Meh, have fun reinventing the wheel, I'll be over here getting work done.

Share this post


Link to post
Share on other sites
SiCrane    11839
Well for deleting a file, there is a function in the C standard library for that: remove(). However, none of the C standard library, C++ standard library or POSIX have a function for copying a file. boost::filesystem, the Windows API and OS X have functions to copy files, though only boost::filesystem is portable. Linux also has a sendfile() function which can be used to copy files, but requires file descriptors instead of file names, but its semantics differ from other *nix sendfile() implementations. It is specifically listed to be non-portable in the kernel documentation.

Share this post


Link to post
Share on other sites
kop0113    2453
[quote name='ChaosEngine' timestamp='1354054617' post='5004701']
Right, because when you iterate over the contents of a directory that's all you're ever going to want to do with the file system.
[/quote]

Gee, I wonder those 3 dirent.h libraries I listed above even exist then.

Perhaps to iterate over the files in a directory and read them? Perhaps using std::ifstream... or maybe we should drag in some boost stuff just to read a simple file too. ;)

Actually, since we are dragging in large unnecessary deps, perhaps we should drag in wxWidgets and use wxFile::read... Edited by Karsten_

Share this post


Link to post
Share on other sites
Oberon_Command    6089
[quote name='ChaosEngine' timestamp='1354054617' post='5004701']
[quote name='Karsten_' timestamp='1353977091' post='5004367']
Please don't drag in boost::filesystem just to provide this single functionality
[/quote]

Right, because when you iterate over the contents of a directory that's all you're ever going to want to do with the file system. [img]http://public.gamedev.net//public/style_emoticons/default/rolleyes.gif[/img]

[/quote]

Believe it or not, sometimes some of us just want to look for a particular file so we can open it and read its contents.

Share this post


Link to post
Share on other sites
ChaosEngine    5185
[quote name='Oberon_Command' timestamp='1354141122' post='5005103']
Believe it or not, sometimes some of us just want to look for a particular file so we can open it and read its contents.
[/quote]


oh FFS....

[quote name='ChaosEngine' timestamp='1354054617' post='5004701']
Oh wait....
[quote name='Acharis' timestamp='1353947969' post='5004227']
I need a crossplatform function to list a directory and then do something on the files inside (delete all files from there or copy files to another directory).
[/quote]
[/quote]
[quote name='SiCrane' timestamp='1354125254' post='5005011']
However, none of the C standard library, C++ standard library or POSIX have a function for copying a file.
[/quote]
Try reading the thread next time.

[quote name='Karsten_' timestamp='1354140316' post='5005100']
Actually, since we are dragging in large unnecessary deps, perhaps we should drag in wxWidgets and use wxFile::read...
[/quote]

Oh teh noes, however shall my application (which, let's not forget, is running on a pc, not some embedded device or console) cope with adding an extra 135kb dll?

boost.filesystem is a small library that's purpose built for dealing with exactly the kind of job the OP wants to do. So you can drop the ridiculous strawman.

Share this post


Link to post
Share on other sites
kop0113    2453
It is this kind of mentality which causes games on Linux to drag in half the package repository.

I hate it when developers do that.

On Windows it is common practice to ship all the .dlls, but Linux package management has a very different approach.
Edited by Karsten_

Share this post


Link to post
Share on other sites
kop0113    2453
It is still a large compile time dependency.

I.e if the game ever gets included in a continuous integration or ports system it does start to matter.

I.e Quake 3 compared to a very simple chess game. Quake 3 will take much less resources.

http://www.freebsd.org/cgi/ports.cgi?query=ioquake3-1.36_11&amp;stype=name&amp;sektion=all
http://www.freebsd.org/cgi/ports.cgi?query=glchess-1.0.6_6&amp;stype=all&amp;sektion=games

While I don't really care enough to argue, I would still go for the dirent.h solution every time. Edited by Karsten_

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this