|
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 when an option is a section type like <option>, only the name part without brackets needs to be specified.
ARGUMENT field:
This field provides argument checking at the parser level, so the user's callback routine can stay simple. It checks the number of arguments this option can take and their argument types.
There are 4 argument types, as shown below. And first 5 arguments can be checked individually with different types.
When a BOOL type is specified, the argument passed to the callback will be replaced with "1" or "0" for convenience. For example, if "On" is specified as an argument and BOOL type checking is enabled, the actual argument passed to the callback will be "1". So we can simply determine it like "bool enabled = atoi(data->argv[1])".
If the original input argument needs to be passed to the callback, specify STR type.
Here are some examples of how to specify the "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 a section like <Option>, a section ID can be assigned. This section ID can be used to limit some other option directives so they are located only inside that section. This is optional. If you do not need to check directory scope, you can simply specify 0 here.
There are 2 predefined section IDs: QAC_SECTION_ALL and QAC_SECTION_ROOT. User-defined section IDs should start from 1 << 1, as shown below.
Please note that these section IDs are ORed together. The values should be assigned as bit flags, such as 2 (1 << 1), 4 (1 << 2), 8 (1 << 3), ...
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 appear anywhere.
QAC_SECTION_ROOT means an option can appear only at the top level, not inside any section.
|
static |
|
static |
qaconf_t->setuserdata(): Set userdata which will be provided on callback.
| qaconf | qaconf_t object. |
| userdata | pointer to 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. |