If you want to prevent the users of your program from modifying the XML file, you should use resources, to compile the file inside the executable.
If you are already using Qt, then by all means have a look at qrc in Qt 4.6. Qt provides a nice system independent way of embedding resource files into the executable. If your project is not already using Qt, then you probably don't want to add it just for that.
If you want a Windows-specific resource file, you can have a look at .rc files documentation. To check what was inside compiled resource files, I used ResEdit in the past and found it really useful. You can even create resource files with it.
If you are targeting Mac OS, you probably want to have a look at bundles, which are sort of a directory where you can put your resources. If you sign your application you can prevent the user to modify any file in the bundle (I'm not sure signing it is required though, I don't have much experience on Mac OS development).
And if you are targeting Linux or similar, you can try doing this trick which seems to work quite well.
All these methods aim to only embed your XML file into your executable, so that your users cannot easily modify it (well, in theory it is still possible but Muggles won't be able to do so). Reading the file may depend on the solution you choose. I personally dislike the Windows resource system and use it only when I really have to, avoiding the Win32 API like plague. If you choose to use Qt, it is quite easy to read the file, and you can do it with boost if you want to. Instead of reading the file at "resources/config.xml"
, you point to the resource file using ":/config.xml"
. Here would be the resource file:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>config.xml</file>
</qresource>
</RCC>
Then when you read it, I'm not sure if you have to use a Qt class to access it or not, but even if you do, you can open the file using QFile
and call readAll
, then use the result with boost:
QFile myConfigFile(":/config.xml");
if (!myConfigFile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
read_xml(QString(myConfigFile.readAll()), [...]);
I haven't tested it but it should work with something like that.