114 lines
3.0 KiB
C
114 lines
3.0 KiB
C
/**
|
|
* @file di/encoding/me.h
|
|
* Generic matching engine
|
|
*/
|
|
#ifndef DI_ENCODING_ME_H__
|
|
#define DI_ENCODING_ME_H__
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <stdbool.h>
|
|
|
|
#include <di/buffer.h>
|
|
|
|
#ifdef __cplusplus
|
|
#define DI_ME_ITEM_STR(_s) reinterpret_cast < const uint8_t * > (_s), sizeof(_s) - 1
|
|
#else
|
|
#define DI_ME_ITEM_STR(_s) .s = (const uint8_t *)_s, .size = sizeof(_s) - 1
|
|
#endif
|
|
|
|
/**
|
|
* Matching engine item state
|
|
*/
|
|
enum di_me_item_states {
|
|
DI_ME_STATE_NO_MATCH = 0, /**< Item not matched */
|
|
DI_ME_STATE_UNMATCHABLE = 1, /**< Item is unmatchable */
|
|
DI_ME_STATE_CAN_MATCH = 2, /**< Item can match */
|
|
DI_ME_STATE_MATCHED = 3 /**< Item is matched */
|
|
};
|
|
|
|
/**
|
|
* Matching engine item flags
|
|
*/
|
|
enum di_me_item_flags {
|
|
DI_ME_ITEM_FLAGS_DEFAULTS = 0, /**< Default flags are used from di_me_ctx.cfg.flags */
|
|
DI_ME_ITEM_FLAG_CB_ON_MATCH = (1 << 0), /**< Execute callback on match */
|
|
DI_ME_ITEM_FLAG_CB_ON_RESET = (1 << 1) /**< Execute callback on reset */
|
|
};
|
|
|
|
/**
|
|
* Matching engine context
|
|
*/
|
|
struct di_me_ctx {
|
|
struct di_buffer reader;
|
|
struct di_buffer linebuffer;
|
|
struct di_buffer cc; /** Carbon copy buffer */
|
|
|
|
/** Configuration */
|
|
struct cfg {
|
|
uint8_t flags; /**< Default item flags */
|
|
uint8_t reset_char; /**< Statemachine reset character */
|
|
void *private_data; /**< Private data field for callback context */
|
|
} cfg;
|
|
|
|
struct match {
|
|
const struct di_me_item *item; /**< Current matched item */
|
|
size_t pos; /**< Current position of match */
|
|
char *str; /**< Matched item string in linbuffer */
|
|
} match;
|
|
|
|
struct items {
|
|
const struct di_me_item *list; /**< Items to match */
|
|
enum di_me_item_states *state; /**< Items state */
|
|
size_t n; /**< Items count */
|
|
} items;
|
|
};
|
|
|
|
/**
|
|
* Matching item
|
|
*/
|
|
struct di_me_item {
|
|
const uint8_t *s; /**< Item string */
|
|
const size_t size; /**< String size */
|
|
uint8_t flags; /**< Item flags, when set to DI_ME_ITEM_FLAGS_DEFAULTS.
|
|
* di_me_ctx.cfg.flags is used
|
|
*/
|
|
void (*cb)(struct di_me_ctx *ctx); /**< Item match callback */
|
|
};
|
|
|
|
/**
|
|
* Reset matching engine
|
|
* - Clear ctx->cfg.linebuffer.data
|
|
* - Set all ctx->item->flags to default when set to DI_ME_ITEM_FLAGS_DEFAULTS
|
|
* - Set all ctx->items state to NOMATCH
|
|
* - Set ctx->pos = 0
|
|
* - Set ctx->match_prev
|
|
* - Set ctx->match = NULL
|
|
*/
|
|
void di_me_reset(struct di_me_ctx *ctx);
|
|
|
|
/**
|
|
* Feed the matching engine data with size
|
|
*/
|
|
void di_me_feed(struct di_me_ctx *ctx, void *data, size_t n);
|
|
|
|
/** Count amount of ocurrences of character in string */
|
|
size_t di_me_char_count(const char *str, char c);
|
|
|
|
/** Replace first ocurrence of s in string
|
|
* @param str String (null terminated)
|
|
* @param s List of characters to search for (null terminated)
|
|
* @param c Character of replacement
|
|
*/
|
|
void di_me_chrrep(char *str, const char *s, char c);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* DI_ENCODING_ME_H__ */
|