qLibc
|
Apache-style configuration file parser. More...
Go to the source code of this file.
Functions | |
qaconf_t * | qaconf (void) |
Create a new configuration object. | |
static int | addoptions (qaconf_t *qaconf, const qaconf_option_t *options) |
qaconf_t->addoptions(): Register option directives. | |
static void | setdefhandler (qaconf_t *qaconf, qaconf_cb_t *callback) |
Set default callback function. | |
static void | setuserdata (qaconf_t *qaconf, const void *userdata) |
qaconf_t->setuserdata(): Set userdata which will be provided on callback. | |
static int | parse (qaconf_t *qaconf, const char *filepath, uint8_t flags) |
qaconf_t->parse(): Run parser. | |
static const char * | errmsg (qaconf_t *qaconf) |
qaconf_t->errmsg(): Get last error message. | |
static void | reseterror (qaconf_t *qaconf) |
qaconf_t->reseterror(): Clear error message. | |
static void | free_ (qaconf_t *qaconf) |
qaconf_t->free(): Release resources. | |
Apache-style configuration file parser.
Apache-style Configuration is a configuration file syntax and format originally introduced by Apache HTTPd project. This format is powerful, flexible and human friendly. Even though this code gets distributed as a part of qLibc project, the code is written not to have any external dependencies to make this single file stands alone for better portability. It is purely written from the ground up and dedicated to the public by Seungyoung Kim.
Sample Apache-style Configuration Syntax:
Definition in file qaconf.c.
qaconf_t * qaconf | ( | void | ) |
|
static |
qaconf_t->addoptions(): Register option directives.
qaconf | qaconf_t object. |
options | array pointer of qaconf_option_t. |
It takes an array of options as provided in the sample codes. Each option consists of 5 parameters as below
Example:
OPTION NAME field:
Option name is a unique string. Even an option is section type like <option> only name part without bracket needs to be specifed.
ARGUMENT field:
This field is for providing argument checking in parser level. So in user's callback routine can go simple. This provides checking of number of arguments this option can take and those argument type.
In terms of argument types. There are 4 argument types as below. And first 5 arguments can be checked individually with different types.
When a BOOL type is specified, the argument passed to callback will be replaced to "1" or "0" for convenience use. For example, if "On" is specified as a argument and if BOOL type checking is specified, then actual argument which will be passed to callback will have "1". So we can simply determine it like "bool enabled = atoi(data->argv[1])".
If original input argument needs to be passed to callback, specify STR type.
Here is some examples of how to specify "Arguments" field.
CALLBACK field:
User defined callback function. We provide a macro, QAC_CB, for function proto type. Always use QAC_CB macro.
Callback function will be called with 2 arguments. One is callback data and the other one is userdata. Userdata is the data pointer set by setuserdata().
Here is data structure. Arguments belong to the option can be accessed via argv variables like data->argv[1]. argv[0] is for the option name.
SECTION ID field:
If an option is an section like <Option>, section id can be assigned. This section id can be used to limit some other option directives to be located only inside of that section. So this is your choice. If it doesn't require to check directory scope, we can just specify 0 here.
There are 2 pre-defined section id, QAC_SECTION_ALL and QAC_SECTION_ROOT. When we define user section, it has to be defined from 2(1 << 1)as below.
Please note that this section IDs are ORed. So the section id should be assigned in bit operation manner as 2(1<<1), 4(1<<2), 6(1<<3), 8(1<<4), ...
SECTION IDS field:
This field is to limit the scope where an option is allowed to be specified. Multiple section IDs can be ORed.
QAC_SECTION_ALL means an option can be appeared in anywhere.
QAC_SECTION_ROOT means an option can be appeared only in top level and not inside of any sections.
|
static |
|
static |
qaconf_t->setuserdata(): Set userdata which will be provided on callback.
qaconf | qaconf_t object. |
userdata | a pointer of userdata. |
|
static |
qaconf_t->parse(): Run parser.
qaconf | qaconf_t object. |
filepath | configuration file path. |
flags | parser options. (0 for default) |
Here is a list of flags. Multiple flags can be ORed.
QAC_CASEINSENSITIVE: Option name is case-insensitive.
QAC_IGNOREUNKNOWN : Ignore unknown option directives. This flag will be ignored if setdefhandler() has set.
|
static |
|
static |
qaconf_t->reseterror(): Clear error message.
qaconf | qaconf_t object. |