config file parsing in C/C++
I'm looking for a robust, flexible way of reading a configuration file into my C++ program. By "reading a configuration file" I mean taking a file with bash-like variable syntax
Code:
foo=bar I know about popt(3) which reads in command line parameters, and this is heart-stoppingly close to what I want - I just don't want to have to code and debug the wrapper that turns bash-like variables into a command-line string. Or write a messy script that turns the variables into a command-line string. Thanks, R. |
I'm not fussy about config file formats though - I'd be perfectly happy to use something like the Apache config file, but I must be able to read in all C++ builtin data types.
R. |
Have you tried rolling your own using C++? I'd be curious to see just how messy it comes out to be. I wrote my own for a small program I'm working on, and it's pretty kludgey waiting for me to improve it.
|
Ok, well, this is probably way longer than it needs to be, but here's my program's code for parsing its config file. The file consists of lines containing one word, then a number (or three numbers) separated by spaces. Like this:
Code:
light_pos 60.0 0.0 60.0 Code:
void Viewing_geometry::read_in_viewing_params( string file_name ) |
If you use C++, take advantage of it and avoid the procedural style of code. I ran into the same problem with a command line analyzer (looked a lot like what you have as far as analysis), but later turned it into a parser class. This makes maintenance MUCH easier. Here is how I would go about processing:
1) declare separators (i.e. " " and "\t" general separation, "\n" statement separation, "#" comment, etc.) 2) parse the entire file into strings/statements with minimal whitespace 3) insert them into a list statement objects (a class you make up) 4) (maybe going too far...) create a list of patterns and corresponding function pointers to processing functions 5) register patterns/function pointers in an analyzer class that checks for patterns then dereferences the appropriate function pointer 6) submit the list of statements to the analyzer This way, everything is generic with the exception of the patterns and the actions. This can be put in a single .cpp file that is nice and neat; only containing pattern strings and global functions for individual actions (all must be the same prototype though). The rest of the implementation is up to you as far as semantics, however it will be highly reusable. It is interesting you brought that up, because I was thinking about how to implement a config file too. I think the absolute easiest way to do it is to parse and view things as objects as much as possible. I think I'll go home and try to work something out tonight and I'll post the general concept I come up with if you want. ta0kira |
Dohh! Entering "C++ read configuration file" into google returns some nice results. For my purposes, the best appear to be
http://www-personal.engin.umich.edu/...onfigFile.html (quite simple; uses templates which may be overkill but is nice if you want to read in user-defined types) http://spacetown.free.fr/lib/talos/index.php (a bunch of useful c++-like extensions; includes variable-expansion syntax) http://www.cours.polymtl.ca/roboop/d...assConfig.html (non-templated so only works on builtin types, and quite straightforward) Robert. |
Thanks ta0kira. Yes, I'd like to see what you come up with.
|
When the configuration file is more complicated you may thing about using flex/bison.
|
johnMG- sorry, it will be a few days. Had some stuff come up.
ta0kira |
I finally got started on it a few days ago. It looks like it will be too large to post here, but I can post the prototypes and descriptions when I am done.
ta0kira |
Quote:
|
Look into Boost Program Options. It'll parse command line options and simple key-value config files, and you get a nice --help message for pretty much free.
|
Quote:
http://rservr.berlios.de/user/assembly.html#UCFF http://rservr.berlios.de/app/client-...on.html#BiT_CP http://svn.berlios.de/wsvn/rservr/tr...onfig-parser.h http://svn.berlios.de/wsvn/rservr/tr...fig-parser.cpp As I said above, this isn't a stand-alone parser at the moment, but it's success as the file parser for two of my largest projects (my actual parser project not included) makes me wonder if it would be appropriate as its own project. What's nice is the actual interpreter can be painfully simple (as it is in its native project,) or it can be very complex, e.g. allowing additional syntax and sematics (as it is in a privately-funded project of mine.) Kevin Barry |
All times are GMT -5. The time now is 05:31 PM. |