[]RSS

[ Here: About | Archives+Tags | Artwork | Resumé | Contact ] [ Elsewhere: Comic | Projects | Philosophy | Work ]

HOWTO: Directory recursion in Boost (and other tips)

[Comment]

April 25th, 2008 in Micro Blog

Boost’s Filesystem library is an incredible library: it abstracts paths, directories, and stat results. It simplifies coding shell problems in C++, it’s portable, and is maintained by a large community of contributors. The one downside of Boost is that some of its newer libraries are poorly documented.1 Until I have time to get involved in the Boost project, I’m going to post examples here.

Traversing a directory tree

This is the coolest feature I’ve found in boost::filesystem so far. It treats directory elements like iterators, and has a convenience iterator that flattens the problem of iterating through a directory tree recursively. The only examples I found for it were in their extensive test sources,2 which are a bit light on comments.

#include "boost/filesystem.hpp"
#include <iostream>

for ( boost::filesystem::recursive_directory_iterator end, dir("./"); 
       dir != end; ++dir ) {
       cout << *dir << std::endl;                                    
}

The example starts in the current working directory, and prints all of the file names (and directories) in inode order.3

Notes:

  1. I don’t alias the namespace here (but I recommend doing so in production code, see below)
  2. Creating an instance of recursive_directory_iterator sets it to the .end() element by default.
  3. The path type supports all standard string paths, including relative paths.

Aliasing Boost namespaces

Here’s how Boost’s own source recommends aliasing their namespaces:

namespace fs = boost::filesystem;
namespace sys = boost::system;

Other cool bits

I’ll write more about these later, but for now here are a few things you’ll find in the library:

  • fs::exists( boost_root / "libs" ), a static function to check if a file exists (-e)
  • fs::current_path() that returns the application’s cwd
  • fs::create_directories( "xx/yy/zz" ) is equivalent to mkdir -p xx/yy/zz
  • fs::is_directory( "xx" ) is the same as Perl/*sh’s -d
  • fs::change_extension("a.txt", ".tex") does the obvious
  • fs::extension("a/b.txt") == "txt" is used to check file extensions
  • fs::remove_all( "x/" ); deletes everything in "x/"
  • ifstream file2( arg_path / "foo" / "bar" ); shows the overloaded / operator!

Things that are great about boost::filesystem’s approach:

  • Static functions are used for ‘helper’ stuff. Take note C++ devs: this is a great balance for types, it keeps the types noise-free while still providing a great deal of utility.
  • It mirrors Perl/*sh functionality, something most developers should know well.
  • It throws errors (filesystem_error), allowing for some really clean, transactional code.
  • And, it abstracts paths that work on Win32, OSX, Unix, and Linux variants.
  1. Something I want to contribute to.
  2. See convenience tests and operations tests for more examples.
  3. Later I’ll try to post an example that uses the stat features to dump extra information.

A Sim’s prototype by Will Wright

[Comment]

February 12th, 2008 in Links

HD:XmotiveHarness:src/Motive.c, also known as “The Soul of The Sims, by Will Wright”. It’s interesting to see prototype code for such a classic game, and even more interesting that he uses K&R-style braces.

Hurray for native widgets

[Comment]

January 31st, 2008 in Links

An example of cross-platform development without resorting to cross-platform GUI widgets. Damn straight!

Prototype 1.6 examples

[Comment]

November 3rd, 2007 in Links

Here are some real world examples of Prototype 1.6. Every few months I look at the many alternatives, but Prototype always remains the most elegant of the lot.

Huge web UI example collection at Flickr

[Comment]

October 25th, 2007 in Links

Subtraction points out a large Flickr set of user interface elements, a slick repository of web interface element metaphors. I love pointing developers at good examples, and then watching their imagination explode.

CSS3

[Comment]

June 23rd, 2007 in Links

The CSS3 test site, including examples of some of the new CSS3 selectors.

A 3D window manager focused on usability

[Comment]

June 21st, 2007 in Links

Some examples of the Metisse window manager, now included in Mandriva’s fine Linux distribution. See the Metisse home for project details. It’s fun to watch the ideas fly by with the various new window managers. (via).

Selection bias

[Comment]

June 6th, 2007 in Links

I was talking to one of our bright devs the other day about selection bias and usability testing, it’s easy to be blinded by bad stats. Somewhat related, Microsoft’s statistics for Word 2003 feature useage are interesting, a good example of surprising measures.

Add-to-cart button examples

[Comment]

May 16th, 2007 in Links

A whole whack of add-to-cart buttons. I’m not sure why, but I love button design.

Forceless force feedback

[Comment]

May 13th, 2007 in Links

Force feedback GUIs without the force, a set of examples showing methods of force-like feedback that don’t require special hardware effects.

Next page [>>]