- C++ 83%
- C 11%
- Makefile 6%
libparser #
libparser is a C++ shared library to easily parse a configuration file and read different parameters to configure an application.
The library creates an object parser that scans a configuration .cfg text file that typically contains pairs of keys and values separated by one character that serves as separator. The default separator is a colon (:).
Build #
libparser is meant to be compiled and used as a shared object libparser.so, that has to be linked to a project.
To build the library, type:
make
It is recommended to copy libparser.so to a directory pointed to by environment variable LD_LIBRARY_PATH in order to be available to applications. Also the header file parser.h can be placed in a directory pointed to by CPATH or whatever environmental variable that points to include files.
A basic example application test will also be built to check the functionality of the library. To try it, execute it using the sample configure.cfg file as a command line parameter
test configure.cfg
Linking with an application #
The main application must include parser.h as a header file and the library has to be linked. For g++, the library is linked as:
g++ -o appname -L path/to/lib -lparser
A different option to use the library is to include the source files parser.cpp and parser.h in the main project and compile them as a part of the application.
Configuration file syntax #
The basic syntax of a configuration file is the declaration of pairs of keys and values:
key:value
They can be written in any order within the configuration file. libparser will scan the whole file and store all pairs that can be later retrieved from the main application, anytime in any order. Please note that keys are case-sensitive.
A line starting with a hashtag (#) represents a comment and is ignored. Also, lines that do not contain the separator will not be stored in memory, so they are efectively treated as comments.
Basic Usage #
Basic example showing the functionality:
#include <parser.h>
...
int main(int argc, char **argv)
{
// Assume that the application is called as
// $: AppName configfile.cfg
// argv[1] contains the string configfile.cfg
parser p(argv[1])
if(p.config!=PARSER_OK)
{
fprintf(stderr,"Error!\n");
return 0;
}
// Now you are ready to ask for keys and obtain values
...
// Assume that your configuration file contains these lines:
// Number: 45
// Length: 5.7
// Position: 1.4, 0.0, 34.5
// Get simple values
const int a=p.GetValue("Number",0);
const double b=p.GetValue("length",0.0);
// Get array values
double position[3];
const int res=p.GetArray("Position",3,position);
if(res!=PARSER_OK)
fprintf(stderr,"Error: Could not read 'position' correctly!\n");
// ... Do some stuff ...
return 0;
}
Also check the main.cpp file that is used to build test.
Documentation #
For further information, see the .pdf file in Documentation