In this post, I tried to make a class for reading configuration files with Boost Tokenizer. The specification of this class is as follow.
- Spawn instance with a path to a configuration file
- Ignore any statements in line after "#"
- The syntax is "VariableName=Value"
- Separate Value and store in an array if the Value included ","
- Use "param(VariableName)" or "param(VariableName, index)" to access values
- Read configuration file using "parse()"
- Ignore syntax errors
The source code download is here -> Configure.tar.gz
This archive's contents is below.
- Configure.hpp : Header file of Configuration class
- Configure.cpp : Source file of Configuration class
- def.hpp : typedef long type name
- testConfigure.cpp : Sample main function
You can build the sample execution as follow.
# g++ -o testConf testConfigure.cpp Configure.cpp -lboost_system -lboost_filesystem
The sample configuration file which I used is following.
- #aaa = aaa
- AUTHOR=INCHOKI #Author
- PATH=/aaa/aaa/bbb | aaabbbccc
- DIRPATH=aaa/bbb , 12345
- PARAMETER = xyz && aaaaaa
- VERSION = 0.1 # version
- TEST = AAA
And the result of sample is following.
- MacBook:work inchoki$ ./testConf
- /aaa/aaa/bbb | aaabbbccc
- xyz && aaaaaa
The source file is too long, so I write the abstruct of the parse process.
- Define the separator and the regular expression
- Open the configuration file
- Read the configuration file line by line
- Ignore the line which "#" is head
- Delete white spaces arround "="
- Separate the line by "#" and store the front part.
- Separate the front part by "="
- Find "," in the VALUE string.
- If "," is found, delete white spaces arround it and the separate VALUE by it
- Store the VALUE into map by key "VARNAME".