views:

218

answers:

4

Is there a platform independent resource system for C++ like the one that comes with Qt (but without the Qt dependency)?

I would like to access arbitrary data from within my C++ source code. That is, not only icons but also translations or shaders, etc.

Alternatively some sort of virtual file system library to access e.g. a ZIP compressed file or such would also fit my needs.

+2  A: 

I rolled my own system for a C++ web server project that basically took a bunch of files (HTML, CSS, JS, PNGs, etc.) and created C++ headers containing the data encoded as static const char*. I then #include those headers where I need access to the data. The app that encodes the 'resource' files executes as a pre-build step. The encoding app itself used boost::filesystem to create the resource headers, so works on Windows/*nix.

A typical resource file might look like this:

namespace resource
{
  // Generated from mainPage.htm
  static const char* mainPage_[] =
  {
    "<html>...</html>"
  };
}

For binary content I encode using the \x notation. I also make sure to line-wrap the data so it is readable in an editor.

I did have some issues though - the MS compiler doesn't allow a static const char* to be bigger than 64Kb which was a PITA. Luckily the only files larger than this were JavaScript files that I could easily split into smaller chunks - large images would be a problem though.

Rob
and why would you do that?
Chris Becke
Because I couldn't find a cross-platform Qt-like resource system, that's why. I gave the question a sensible answer and you voted me down? Thanks!
Rob
A: 

We're using ICU ResourceBundles for that and are pretty satisfied with it.

Using the pkgdata tool, packageing of ResourceBundles is pretty flexible: as a shared library, static library, or as files that can be memory-mapped by ICU.

Éric Malenfant
A: 

I have just patched them onto the end of the executable at link time as a binary blob. With the last 4bytes being the size of the previous block and then have the program read the data items from the tail.

Another approach if you need a more filesystem type structure (though I havem't tried it) would be to put everything in a zip file and append that to the end. Again you would need some easily findabale size of the added data.

Martin Beckett
A: 

The xxd answer to this question is what you're looking for.

Lucas