• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Acharis

Cross platform directory list

19 posts in this topic

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

Share this post


Link to post
Share on other sites
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?
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
2

Share this post


Link to post
Share on other sites
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?
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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")?
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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_
0

Share this post


Link to post
Share on other sites
[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.
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[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_
0

Share this post


Link to post
Share on other sites
[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.
0

Share this post


Link to post
Share on other sites
[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.
1

Share this post


Link to post
Share on other sites
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_
0

Share this post


Link to post
Share on other sites
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.
2

Share this post


Link to post
Share on other sites
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_
-1

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  
Followers 0