embd: a tool to embed files in applications
[24th November 2007]
Occasionally I like to make little programs where it is convenient to have a file embedded within the application itself. Perhaps a particular texture is needed in a graphics demo, or some kind of default configuration file is stored internally so that the user can ask the program copy it out to a desired location (for example, it seems doxygen does this). Sometimes embedding a handful of files is simply the most convenient option.
To this end, I decided to make a program that makes the process of embedding files in an application and accessing them in the source code easy.
I've had code to to this kicking around for a while now. You'll see that one of the jpegxx examples contains an embedded image that was made with the old version. But the code wasn't particularly good, so I decided to re-write it and release in to the wild. It's come in handy on the odd occasion. Maybe you'll find a use for it, too.
embd generates a C++ source file and a corresponding header. The source file contains the embedded data and the header provides the declarations and definitions needed to access the data easily.
Here's how you might use it:
> embd texture.png readme.txt default.cfg
This creates a source file and a corresponding header called embd.cpp and embd.hpp respectively. By compiling and linking embd.cpp in to your application, the files are embedded within.
To access the data in your software, #include the header where needed and use the file and file_name_iterator classes defined inside:
This creates a source file and a corresponding header called embd.cpp and embd.hpp respectively. By compiling and linking embd.cpp in to your application, the files are embedded within.
To access the data in your software, #include the header where needed and use the file and file_name_iterator classes defined inside:
#include "embd.hpp"
int main()
{
typedef embd::file_name_iterator nameiter;
for (nameiter b = nameiter::begin(), e = nameiter::end(); b != e; ++b)
{
std::string filename = *b; // a name of an embedded file
embd::file f(filename); // open an embedded file
std::istream &in = f.istream(); // access its data
// now read from in like you would any other istream
// ...
}
}
By default all the stuff declared and defined by the header is placed inside the embd namespace. This is configurable, as are the names of the C++ source files generated by embd. There are a bunch of other options that I won't bother going in to here. You can find out more heading over to the embd project page.
Comments
All original content copyright© Edd Dawson.
All source code appearing on this website that was written by Edd Dawson is made available under the terms of the Boost software license version 1.0 unless otherwise stated or implied by the license associated with the work from which the code is derived.
