0

I have a C++ application that takes configuration options from an XML file, and uses an XSD file to validate the information provided.

At the moment, I define the location of the XSD file in the makefile like this:

DEFINES := XSD_FILE_LOCATION=\"$(HOST_DIR)/share/$(strip $(COMPONENT_DIR))/xml\"

So from the source code I use 'XSD_FILE_LOCATION' to find the path to the XSD file.

I would like to package the XSD file together with the application's binary, so that the binary can be passed around and it already contains the XSD information, rather than demanding building it on every PC it's going to be used.

EDIT: I am thinking about embedding the XSD file as a string in the source code, but the solution does not convince me, I like the idea of having it as a separate file.

Oscar_Mariani
  • 728
  • 3
  • 9
  • 22
  • 1
    See e.g. http://stackoverflow.com/questions/2627004/embedding-binary-blobs-using-gcc-mingw?lq=1 if you're using gcc, or http://stackoverflow.com/questions/7288279/how-to-embed-a-file-into-an-executable?rq=1 for a more general case. – nos Mar 26 '15 at 08:36
  • Looking into that, thanks! – Oscar_Mariani Mar 26 '15 at 08:40

1 Answers1

2

If you want it packaged inside the executable, you could consider simply creating a variable to hold it, something like (not really valid XML, the intent is just to show the method):

const char *xml =
    "<xml>"
    "  <tag>This is my XML.</tag>"
    "  <tag>There are many like it, but this one is mine.</tag>"
    "</xml>"
;

You could even have a build step which took the real XML file, and morphed it into a C++ source file for compilation, so that any funnies like character escapes could be done automatically. This allows you to manage the XML file in its raw form during development (as a separate file) while merging it with the executable at build time.

Then, once your program is running, you can do as you see fit. Use the in-memory copy contained in xml, write it to disk as part of start-up, or whatever else your heart desires.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953