Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Cross platform directory list


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
19 replies to this topic

#1 Acharis   Crossbones+   -  Reputation: 3966

Like
0Likes
Like

Posted 26 November 2012 - 10:39 AM

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>):
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 );
}
}

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

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9662

Like
4Likes
Like

Posted 26 November 2012 - 10:44 AM

One option is boost::filesystem.

#3 Acharis   Crossbones+   -  Reputation: 3966

Like
1Likes
Like

Posted 26 November 2012 - 12:03 PM

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?

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


#4 Bregma   Crossbones+   -  Reputation: 5353

Like
4Likes
Like

Posted 26 November 2012 - 12:16 PM

Aren't there any standard functions for this?

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.
Stephen M. Webb
Professional Free Software Developer

#5 SiCrane   Moderators   -  Reputation: 9662

Like
7Likes
Like

Posted 26 November 2012 - 12:25 PM

If you want to wait a few years, some standard functionality is working it's way through committee as part of TR2 ... based on boost::filesystem. Shouldn't be more than another five to ten years. Alternately you could use POSIX functions on the *nix platforms (opendir() and friends), and download a POSIX compatibility library for Windows.

#6 Acharis   Crossbones+   -  Reputation: 3966

Like
0Likes
Like

Posted 26 November 2012 - 02:35 PM

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.

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


#7 SiCrane   Moderators   -  Reputation: 9662

Like
2Likes
Like

Posted 26 November 2012 - 02:43 PM

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.

#8 Acharis   Crossbones+   -  Reputation: 3966

Like
1Likes
Like

Posted 26 November 2012 - 05:45 PM

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?

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


#9 SiCrane   Moderators   -  Reputation: 9662

Like
1Likes
Like

Posted 26 November 2012 - 05:55 PM

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.

#10 Acharis   Crossbones+   -  Reputation: 3966

Like
1Likes
Like

Posted 26 November 2012 - 06:26 PM

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

Europe1300.eu - Historical Realistic Medieval Sim (RELEASED!)

PocketSpaceEmpire - turn based 4X with no micromanagement FB


#11 ChaosEngine   Crossbones+   -  Reputation: 2499

Like
1Likes
Like

Posted 26 November 2012 - 06:38 PM

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.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#12 Karsten_   Members   -  Reputation: 1655

Like
0Likes
Like

Posted 26 November 2012 - 06:44 PM

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_, 26 November 2012 - 06:52 PM.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.


#13 ChaosEngine   Crossbones+   -  Reputation: 2499

Like
0Likes
Like

Posted 27 November 2012 - 04:16 PM

Please don't drag in boost::filesystem just to provide this single functionality


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. Posted Image

Oh wait....

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).


Meh, have fun reinventing the wheel, I'll be over here getting work done.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#14 SiCrane   Moderators   -  Reputation: 9662

Like
1Likes
Like

Posted 28 November 2012 - 11:54 AM

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.

#15 Karsten_   Members   -  Reputation: 1655

Like
0Likes
Like

Posted 28 November 2012 - 04:05 PM

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.


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_, 28 November 2012 - 04:19 PM.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.


#16 Oberon_Command   Crossbones+   -  Reputation: 1960

Like
0Likes
Like

Posted 28 November 2012 - 04:18 PM


Please don't drag in boost::filesystem just to provide this single functionality


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. Posted Image


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.

#17 ChaosEngine   Crossbones+   -  Reputation: 2499

Like
1Likes
Like

Posted 28 November 2012 - 05:26 PM

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.



oh FFS....

Oh wait....


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).

However, none of the C standard library, C++ standard library or POSIX have a function for copying a file.

Try reading the thread next time.

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


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.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#18 Karsten_   Members   -  Reputation: 1655

Like
0Likes
Like

Posted 28 November 2012 - 05:41 PM

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_, 28 November 2012 - 05:43 PM.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.


#19 ApochPiQ   Moderators   -  Reputation: 16378

Like
2Likes
Like

Posted 28 November 2012 - 05:45 PM

Boost::filesystem can be linked statically, so this whole "extra dependency" issue is a total red herring.



Also, let's all calm down with the melodrama a bit, please.

#20 Karsten_   Members   -  Reputation: 1655

Like
-1Likes
Like

Posted 28 November 2012 - 06:06 PM

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_, 28 November 2012 - 06:08 PM.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS