Use of xml config files - simple mapping approaches?
I am writing a simple physics engine, and I want to include some form of config files.
As I look at xml, the tree structure makes a lot of sense to me, and looking at libxml it seems to be very easy to read and modify.
The problem I'm having is how I might impliment it.
My application is written in C, are there standard conventions for implementing xml config files?
For argument's sake I'll set up an example enviroment. Lets say I have a file called foo.c with a datastructure that looks like this:
typedef str开发者_运维知识库uct {
char* name;
float x;
float y;
}info;
and a config file called foo.xml that looks like
<info>
<name>something</name>
<x>0</x>
<y>0</y>
</info>
and another class bar.c that reads xml files into trees using libxml2. How would do people normally populate such a data structure?
I'M NOT ASKING FOR ALTERNATIVES TO XML
example answer (something I've been thinking about but am not sure if this is the right way to implement).
bar.c has a functions:
tree* load_config(FILE* config_file);
char[][] get_fields(tree* current_tree);
you call that function, it returns a tree and then you have a bunch of conditionals like:
tree* current_tree = load_config(fopen("foo.xml"));
char[][] fields = get_fields(current_tree);
if( contains(fields, "name") ) {
name = current_tree->name; //i'd have to navigate through the tree to find name or something
}
If you only needs config file and not necessarily XML, take a look on Boost Program Options
It is simple and easy to use. It use the format of .ini file.
Example of .ini file:
[info]
name=something
x=0
y=0
You might want to take a look at libconfig. In your case such a config file would be:
info {
name = something;
x = 0;
y = 0;
};
The Boost library is quite large in some distros (like Debian).
i use the same things in same way but i use libmxml library...its very simple
精彩评论