RConfig
A readable config file (rcfile) parser and variable binder. This is totally sexy, if I can say that. The goal of this guy was to make it easy to initialise variables already declared in the normal course of programming, by getting values from a config file (or the environment) in as few lines as possible. After many iterations, this is now down to an additional 0 lines of code. Yes, zero. Time for a use-case.
Use Cases
Imagine, in the course of programming some application, you defined a variable as follows:
int myint = 5;
Now, you want to allow users to override myint in a configuration file, or by running your application as:
$ myint=5 ./myapp
Enter RConfig. Just change the definition to
int RCFILE_CONFIGVAR(myint, 5);
And you're done! Well. almost. You also need to actually parse the file at some point. So let's do a more complete example.
Hello World
Here is a complete "Hello World" main.cpp:
#include <iostream> #include "rconfig.h" std::string RCFILE_CONFIGVAR(world, "world!"); int main() { RConfig::rcfile_parse("config.txt"); std::cout << "Hello " << world << std::endl; return 0; }
Now what happens when we run it:
$ ./hello_world (cfg) std::string world = world! (hello_world.cpp) Hello world! $ world="Environment" ./hello_world (cfg) std::string world = world! (hello_world.cpp) Overriding default world to Environment Hello Environment $ echo "world = configfile" > config.txt $ ./hello_world (cfg) std::string world = world! (hello_world.cpp) Overriding default world to configfile Hello configfile
0 lines??
So why is this "0 lines". Well, for each extra variable you add, you only need to tweak the line that declares the variable. Hence, 0 lines are added. Also, the fact that this is a variable parsed from the config file becomes part of the declaration -- this is great intrinsic documentation!
More features
The examples above show an integer and std::strings. All primitive types are supported, and std::string, and std::vectors of any of these (each time a value for a vector is found, it just gets pushed back, rather than replaced.
More examples
Have a look at the "configapp" in mylib/trunk/configapp. The hello world use case can also be found at mylib/trunk/rconfig/hello_world.cpp
Documentation
Not generated yet, but it should eventually appear at (Doxygen index)
Requirements
- cmake http://www.cmake.org/
- Regular expressions -- either Boost or my own RxString
SVN Checkout & Building & Running
See also ConfigApp? (part of mylib?)
svn co http://code.apted.net/svn/pub/mylib/trunk/rconfig (cd rconfig && svn export http://code.apted.net/svn/pub/cmake/SuggestCXXFlag.cmake) mkdir rconfig-build && cd rconfig-build cmake ../rconfig && make ./hello_world
