5

Before I start writing by own settings library I though I would try to find an existing one. I've googled and found plenty for C# or MFC but nothing that uses plain C++/STL. I'd like something that allows access via keys such as:

mySettings["Root"]["Subsection"]["Value"].Value

Or something along those lines. Is there anything that can provide that sort of interface to an XML file or am I going to have to roll my own?

Thanks, J

2
  • Why XML? Do you really want people changing your setting from outside the program? You'll have to parse all that crap and then validate it. Does it need to be human readable from outside the program? Does it need to be machine/architecture independant. Create a structure containing all your config. Create a file the same size, mmap the file and overlay the structure. You now have a binary copy of the structure on disk. Add file locking and you can have multiple processes share the config. Sit back and profit.
    – Angelom
    Apr 10, 2011 at 10:42
  • 2
    While XML is not perfect, it has the advantage of "just works" with the added bonus that in case you add a setting later, neither older nor newer revisions of your software will crash and burn, as opposed to a binary format. The mere fact that XML is human readable is not a disadvantage. You will of course always have some fool who will break his setup editing settings in a text editor because hey, if it's XML then you absolutely must edit your config files manually -- but that's their own problem, not yours. The same people would possibly try to edit a binary file in a hex editor too.
    – Damon
    Apr 10, 2011 at 11:22

3 Answers 3

7

There is TinyXML and pugixml

PugiXML does have XPath support

3
  • +1 for TinyXML. TinyXML has been working reliably as the configuration backend for Code::Blocks for several years over many revisions. Some early stage failures due to naive assumptions on input data apart, it has never given any trouble.
    – Damon
    Apr 10, 2011 at 11:27
  • + 1 pugixml looks interesting
    – Ralf
    Apr 10, 2011 at 14:19
  • 1
    pugixml is perfect. I've replaced the existing registry based settings system with it in just a few hours and it's working great!
    – JWood
    Apr 10, 2011 at 15:10
5

Boost property tree is one such library though you have to consider whether you want to bring in boost just for this purpose. It works well for simple configuration properties as illustrated in the example, though I recall doing something more complex took me a bit longer to get right:

The following code is taken from the documentation:

// Create an empty property tree object
using boost::property_tree::ptree;
ptree pt;

// Load the XML file into the property tree. If reading fails
// (cannot open file, parse error), an exception is thrown.
read_xml(filename, pt);

// Get the filename and store it in the m_file variable.
// Note that we construct the path to the value by separating
// the individual keys with dots. If dots appear in the keys,
// a path type with a different separator can be used.
// If the debug.filename key is not found, an exception is thrown.
m_file = pt.get<std::string>("debug.filename");
3
  • Thanks for the suggestion, I was a bit reluctant to bring in boost just for this task but in the end pugixml did the trick.
    – JWood
    Apr 10, 2011 at 15:11
  • @JWood: why? it's a header only library. pretty nice
    – Inverse
    Apr 11, 2011 at 17:09
  • @Inverse - I've looked at boost before and was put off mainly by the number of other parts of boost each part relied on. I may be incorrect here but it seemed like getting one component in usually required a large number of other components to also be added. Admittedly it was only a brief glance but I had visions of Qt and the ton of additional DLLs that need to be added.
    – JWood
    Apr 13, 2011 at 13:39
0

All interesting answers. But I must stress that until C++ provides its own XML API, using a true XML configuration file, and implementing your own customized XML Configuration class is the best way to go. Today I am faced with the same task, and this is the solution that I am going to use. You can see in black and white the configuration settings, because you are maintaining your own XML schema. Then your XML Configuration class is customized to parse that schema without overloading your solution with a plethora of superfluous features and complexities. Simple, concise, and practical /= bloatware.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.