Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Platform independent resource system (like the Qt Resource system)

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.

like image 487
user231968 Avatar asked Dec 15 '09 09:12

user231968


People also ask

What is a Qt resource file?

The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable. This is useful if your application always needs a certain set of files (icons, translation files, etc.) and you don't want to run the risk of losing the files.

What is RCC in Qt?

The rcc tool is used to embed resources into a Qt application during the build process. It works by generating a C++ source file containing data specified in a Qt resource (. qrc) file.


2 Answers

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.

like image 139
Rob Avatar answered Oct 23 '22 07:10

Rob


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

like image 37
Lucas Avatar answered Oct 23 '22 07:10

Lucas