Main index | Section 3 | Options |
#include <figpar.h>
#include <string_m.h>
Due to the fact that configuration files may have basic syntax differences, the library does not attempt to impose any structure on the data but instead provides raw data to a set of callback functions. These callback functions can in-turn initiate abort through their return value, allowing custom syntax validation during parsing.
Configuration directives, types, and callback functions are provided through data structures defined in <figpar.h>:
struct figpar_config { enum figpar_cfgtype type; /* value type */ const char *directive; /* keyword */ union figpar_cfgvalue value; /* value *//* Pointer to function used when directive is found */ int (*action)(struct figpar_config *option, uint32_t line, char *directive, char *value); };
enum figpar_cfgtype { FIGPAR_TYPE_NONE = 0x0000, /* directives with no value */ FIGPAR_TYPE_BOOL = 0x0001, /* boolean */ FIGPAR_TYPE_INT = 0x0002, /* signed 32 bit integer */ FIGPAR_TYPE_UINT = 0x0004, /* unsigned 32 bit integer */ FIGPAR_TYPE_STR = 0x0008, /* string pointer */ FIGPAR_TYPE_STRARRAY = 0x0010, /* string array pointer */ FIGPAR_TYPE_DATA1 = 0x0020, /* void data type-1 (open) */ FIGPAR_TYPE_DATA2 = 0x0040, /* void data type-2 (open) */ FIGPAR_TYPE_DATA3 = 0x0080, /* void data type-3 (open) */ FIGPAR_TYPE_RESERVED1 = 0x0100, /* reserved data type-1 */ FIGPAR_TYPE_RESERVED2 = 0x0200, /* reserved data type-2 */ FIGPAR_TYPE_RESERVED3 = 0x0400, /* reserved data type-3 */ };
union figpar_cfgvalue { void *data; /* Pointer to NUL-terminated string */ char *str; /* Pointer to NUL-terminated string */ char **strarray; /* Pointer to an array of strings */ int32_t num; /* Signed 32-bit integer value */ uint32_t u_num; /* Unsigned 32-bit integer value */ uint32_t boolean:1; /* Boolean integer value (0 or 1) */ };
The processing_options argument to parse_config() is a mask of bit fields which indicate various processing options. The possible flags are:
FIGPAR_BREAK_ON_EQUALS | An equals sign (‘=’) is normally considered part of the directive. This flag enables terminating the directive at the equals sign. Also makes equals sign optional and transient. |
FIGPAR_BREAK_ON_SEMICOLON | |
A semicolon (‘’) is normally considered part of the value. This flag enables terminating the value at the semicolon. Also allows multiple statements on a single line separated by semicolon. | |
FIGPAR_CASE_SENSITIVE | Normally directives are matched case insensitively using fnmatch(3). This flag enables directive matching to be case sensitive. |
FIGPAR_REQUIRE_EQUALS | If a directive is not followed by an equals, processing is aborted. |
FIGPAR_STRICT_EQUALS | Equals must be part of the directive to be considered a delimiter between directive and value. |
The options struct array pointer can be NULL and every directive will run the unknown() function argument.
The directive for each figpar_config item in the parse_config() options argument is matched against each parsed directive using fnmatch(3) until a match is found. If a match is found, the action() function for that figpar_config directive is run with the line number, directive, and value. Otherwise if no match, the unknown() function is run (with the same arguments).
If either action or unknown return non-zero, parse_config() aborts reading the file and returns the error value to its caller.
get_config_option() traverses the options-array and returns the option that matches via strcmp(3), or if no match a pointer to a static dummy struct is returned (whose values are all zero or NULL).
The use of struct figpar_config is entirely optional as-is the use of enum figpar_cfgtype or union figpar_cfgvalue. For example, a NULL pointer can be passed to parse_config() for the first argument while providing a simple unknown function based on queue(3) that populates a singly-linked list of a struct containing the directive and value.
In addition, miscellaneous string manipulation routines are provided by <string_m.h>:
replaceall() | |
Replace all occurrences of find in source with replace. | |
strcount() | |
Count the number of occurrences of one string that appear in the source string. Return value is the total count. An example use would be to show how large a block of memory needs to be for a replaceall() series. | |
strexpand() | |
Expand escape sequences in a buffer pointed to by source. | |
strexpandnl() | |
Expand only the escaped newlines in a buffer pointed to by source. | |
strtolower() | |
Convert a string to lower case. | |
FIGPAR (3) | March 13, 2018 |
Main index | Section 3 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | Hang in there, people suffering from natural disasters and deadly diseases - we're putting ribbons on our cars as fast as we can | ” |
— Artur Bagyants |