I've got a C++ program that uses SQLite. I want to store the SQL queries in a separate file -- a plain-text file, not a source code file -- but embed that file in the executable file like a resource.
(This has to run on Linux, so I can't store it as an actual resource as far as I know, though that would be perfect if it were for Windows.)
Is there any simple way to do it, or will it effectively require me to write my own resource system for Linux? (Easily possible, but it would take a lot longer.)
It's slightly ugly, but you can always use something like:
Where query_foo.txt would contain the quoted query text.
I have seen this to be done by converting the resource file to a C source file with only one char array defined containing the content of resource file in a hexadecimal format (to avoid problems with malicious characters). This automatically generated source file is then simply compiled and linked to the project.
It should be pretty easy to implement the convertor to dump C file for each resource file also as to write some facade functions for accessing the resources.
Use macros. Technically that file would be source code file but it wouldn't look like this. Example:
Later on you could do all sorts of other processing on that file by the same file, say you'd want to have array and a hash map of them, you could redefine Q to do another job and be done with it.
You can use objcopy to bind the contents of the file to a symbol your program can use. See, for instance, here for more information.
You can always write a small program or script to convert your text file into a header file and run it as part of your build process.
Here's a sample that we used for cross-platform embeddeding of files. It's pretty simplistic, but will probably work for you.
You may also need to change how it's handling linefeeds in the escapeLine function.