Browse Source

Add conception of default handler for structures parsing.

tags/0.6.0
Vsevolod Stakhov 10 years ago
parent
commit
9ad8f56803
2 changed files with 61 additions and 15 deletions
  1. 41
    4
      src/cfg_rcl.c
  2. 20
    11
      src/cfg_rcl.h

+ 41
- 4
src/cfg_rcl.c View File

@@ -154,7 +154,17 @@ gboolean rspamd_rcl_logging_handler (struct config_file *cfg, rspamd_cl_object_t
return TRUE;
}

static inline void
/**
* Add new section to the configuration
* @param top top section
* @param name the name of the section
* @param handler handler function for all attributes
* @param type type of object handled by a handler
* @param required whether at least one of these sections is required
* @param strict_type turn on strict check for types for this section
* @return newly created structure
*/
static inline struct rspamd_rcl_section*
rspamd_rcl_add_section (struct rspamd_rcl_section *top,
const gchar *name, rspamd_rcl_handler_t handler,
enum rspamd_cl_type type, gboolean required, gboolean strict_type)
@@ -168,19 +178,46 @@ rspamd_rcl_add_section (struct rspamd_rcl_section *top,
new->strict_type = strict_type;

HASH_ADD_KEYPTR (hh, top, new->name, strlen (new->name), new);
return new;
}

/**
* Add a default handler for a section
* @param section section pointer
* @param name name of param
* @param handler handler of param
* @param offset offset in a structure
* @param flags flags for the parser
* @return newly created structure
*/
static inline struct rspamd_rcl_default_handler_data *
rspamd_rcl_add_default_handler (struct rspamd_rcl_section *section, const gchar *name,
rspamd_rcl_handler_t handler, gsize offset, gint flags)
{
struct rspamd_rcl_default_handler_data *new;

new = g_slice_alloc0 (sizeof (struct rspamd_rcl_default_handler_data));
new->key = name;
new->handler = handler;
new->pd.offset = offset;
new->pd.flags = flags;

HASH_ADD_KEYPTR (hh, section->default_parser, new->key, strlen (new->key), new);
return new;
}

struct rspamd_rcl_section*
rspamd_rcl_config_init (void)
{
struct rspamd_rcl_section *new;
struct rspamd_rcl_section *new, *sub;

new = g_slice_alloc0 (sizeof (struct rspamd_rcl_section));

/* TODO: add all known rspamd sections here */
rspamd_rcl_add_section (new, "logging", rspamd_rcl_logging_handler, RSPAMD_CL_OBJECT,
sub = rspamd_rcl_add_section (new, "logging", rspamd_rcl_logging_handler, RSPAMD_CL_OBJECT,
FALSE, TRUE);

rspamd_rcl_add_default_handler (sub, "log_buffer", rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct config_file, log_buf_size), 0);
return new;
}


+ 20
- 11
src/cfg_rcl.h View File

@@ -37,6 +37,18 @@ cfg_rcl_error_quark (void)

struct rspamd_rcl_section;

struct rspamd_rcl_struct_parser {
gpointer user_struct;
goffset offset;
gsize size;
enum {
RSPAMD_CL_FLAG_TIME_FLOAT = 0x1 << 0,
RSPAMD_CL_FLAG_TIME_TIMEVAL = 0x1 << 1,
RSPAMD_CL_FLAG_TIME_TIMESPEC = 0x1 << 2,
RSPAMD_CL_FLAG_TIME_INTEGER = 0x1 << 3
} flags;
};

/**
* Common handler type
* @param cfg configuration
@@ -48,6 +60,13 @@ struct rspamd_rcl_section;
typedef gboolean (*rspamd_rcl_handler_t) (struct config_file *cfg, rspamd_cl_object_t *obj,
gpointer ud, struct rspamd_rcl_section *section, GError **err);

struct rspamd_rcl_default_handler_data {
struct rspamd_rcl_struct_parser pd;
const gchar *key;
rspamd_rcl_handler_t handler;
UT_hash_handle hh;
};

struct rspamd_rcl_section {
const gchar *name; /**< name of section */
rspamd_rcl_handler_t handler; /**< handler of section attributes */
@@ -56,6 +75,7 @@ struct rspamd_rcl_section {
gboolean strict_type; /**< whether we need strict type */
UT_hash_handle hh; /** hash handle */
struct rspamd_rcl_section *subsections; /**< hash table of subsections */
struct rspamd_rcl_default_handler_data *default_parser; /**< generic parsing fields */
};

/**
@@ -88,17 +108,6 @@ gboolean rspamd_read_rcl_config (struct rspamd_rcl_section *top,
* which itself contains a struct pointer and the offset of a member in a
* specific structure
*/
struct rspamd_rcl_struct_parser {
gpointer user_struct;
goffset offset;
gsize size;
enum {
RSPAMD_CL_FLAG_TIME_FLOAT = 0x1 << 0,
RSPAMD_CL_FLAG_TIME_TIMEVAL = 0x1 << 1,
RSPAMD_CL_FLAG_TIME_TIMESPEC = 0x1 << 2,
RSPAMD_CL_FLAG_TIME_INTEGER = 0x1 << 3
} flags;
};

/**
* Parse a string field of a structure

Loading…
Cancel
Save