embd
embd is a tool for embedding files in to C++ source code. It generates a source file containing the embedded data and a header file that gives you all the facilities you need to access the embedded files.
I don't recommend using embd to embed large amounts of data, but it is useful for little demos and such like where you'd like a single file for ease of use and distribution.
The source files that embd generates are not licensed in anyway. You are free to apply your own copyright and favorite license or release the files in to the public domain.
Recent activity
Code
Clone the repository using mercurial:
> hg clone http://bitbucket.org/edd/embd
Or get a zip file of the code.
Quick start
To create a C++ source file and a corresponding header that contains files:
embd contract.txt logo.jpg
This will generated embd.cpp and embd.hpp. By compiling and linking embd.cpp in to your code, you can access the files as follows:
embd::file f("contract.txt"); // may throw an embd::file_not_found exception
std::istream &in = f.istream();
std::string token;
while ((in >> token))
std::cout << "token: " << token << '\n';
There are many configurable options to change file names, namespaces, paths to the embedded files and so on.
Further reading
Comments
[03/03/2011 at 23:11:55]
Hi Alf,
I have no such C++ tool I'm afraid, but you could always use Python:
# source could come from a file:
# source = open(sys.argv[1], 'rb').read()
# but we'll use some dummy data
source = r"""\makeatletter
\newsavebox\zbox
\newcounter{zoom}
\newcommand{\zoombox}[2][0]{%
\leavevmode%
\sbox\zbox{#2}%
... 100 lines """
lines = source.splitlines()
literal = '\n'.join(' "' + repr(line)[1:-1] + '\\n"' for line in lines)
content = 'std::string s(\n' + literal + '\n);\n'
print content
This might not be bullet-proof, but with a little extra work could get you what you want.
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.

alfC
[01/03/2011 at 21:03:16]
Hi, looks very useful. I was looking for something like that, but I was wondering if you have a similar tool that embeding file and strings but only on header files, so there is no need for linking. Sure it will work only for text files and have limitations on the characters you can store. But it can save the burden of defining all linebreaks and add the escape sequences. My goal is to store some long hardcoded LaTeX command (100s of lines) like this:
\makeatletter
\newsavebox\zbox
\newcounter{zoom}
\newcommand{\zoombox}[2][0]{%
\leavevmode%
\sbox\zbox{#2}%
... 100 lines
internally I need to convert it to something like
std::string("
\\makeatletter\n
\\newsavebox\\zbox\n
\\newcounter{zoom}\n
\\newcommand{\\zoombox}[2][0]{%\n
\\leavevmode%\n
\\sbox\\zbox{#2}%\n
... 100 lines
")
-- Thanks