As a lighter alternative to XML, I introduce Extensible Configuration Script.
This is an example of the script:
orb{ screen { width=1024 height=768 } workingdir="c:\\orb\\resources" levels { level1 somelevel mylevel }}
Nice and simple, and will nest as deeply as you want.
It is the usage in code that appeals to me. There are two situations - one where your configuration file must contain a certain value in a certain format or you have an error (the screen resolutions above for example), or another where you need to dynamically examine the contents (the list of levels above).
So for the first example, you can use the following syntax:
#include #include "xcs.h"int main(int,const char **av){ std::cout << "XcsDriver 1.0 Copyright(C)2007 All Rights Reserved\n"; try { xcs::tree t("sample.xcs"); std::cout << t["orb"]["screen"]["width"].as() << "\n"; std::cout << t["orb"]["screen"]["height"].as() << "\n"; std::cout << t["orb"]["workingdir"].as() << "\n"; } catch(xcs::exception &e) { std::cout << e.what() << std::endl; }}
Obviously different exceptions are thrown depending on what the problem is - a name missing in a scope, a value not being converted etc.
Alternatively, to dynamically look at the list of levels:
#include #include "xcs.h"int main(int,const char **av){ std::cout << "XcsDriver 1.0 Copyright(C)2007 All Rights Reserved\n"; try { xcs::tree t("sample.xcs"); xcs::node &n=t["orb"]["levels"]; for(xcs::iterator i=n.begin();i!=n.end();++i) std::cout << i->as() << "\n"; } catch(xcs::exception &e) { std::cout << e.what() << std::endl; }}
Or to use a recursive function to traverse the entire contents of the tree:
#include #include "xcs.h"void traverse(xcs::node &n){ std::cout << n.full_name() << "\n"; for(xcs::iterator i=n.begin();i!=n.end();++i) traverse(*i);}int main(int,const char **av){ std::cout << "XcsDriver 1.0 Copyright(C)2007 All Rights Reserved\n"; try { xcs::tree t("sample.xcs"); traverse(t.root()); } catch(xcs::exception &e) { std::cout << e.what() << std::endl; }}
You can concatenate quoted strings in the script using the + operator, so you can split lengthy text over several lines, and the C system of forward-slash escape sequences is working for strings as well.
I plan to start using this quite extensively in Orb and future projects. When I've tested it a bit more thoroughly, I might stick up the source code somewhere.
[EDIT] Oh, and I bought The Orange Box. Portal rocks as much as I thought it would, then a little bit more.
Orb is looking pretty sweet too, have you told us what the game is going to be about yet?