From 75f4e0aee4b853a1fb2348aa62d52c7cf303e079 Mon Sep 17 00:00:00 2001 From: "tg(x)" <*@tg-x.net> Date: Wed, 30 Nov 2011 13:51:50 +0100 Subject: [PATCH] methods & templates --- include/psyc.h | 8 --- include/psyc/method.h | 65 ++++++++++++++++++ include/psyc/packet.h | 20 +----- include/psyc/parse.h | 3 +- include/psyc/render.h | 2 +- include/psyc/text.h | 10 +++ include/psyc/variable.h | 6 +- src/templates.h | 12 ++++ src/text.c | 8 ++- src/variable.c | 149 ++++++++++++++++++++++++---------------- test/test_text.c | 6 +- 11 files changed, 197 insertions(+), 92 deletions(-) create mode 100644 include/psyc/method.h create mode 100644 src/templates.h diff --git a/include/psyc.h b/include/psyc.h index 1a3506a..5c3076d 100644 --- a/include/psyc.h +++ b/include/psyc.h @@ -94,14 +94,6 @@ typedef enum { PSYC_LIST_BINARY = 2, } PsycListType; -typedef enum PsycMessageFlag { - PSYC_MESSAGE_TEMPLATE = 1 << 0, - PSYC_MESSAGE_REPLY = 1 << 1, - PSYC_MESSAGE_VISIBLE = 1 << 2, - PSYC_MESSAGE_LOGGABLE = 1 << 3, - PSYC_MESSAGE_MANUAL = 1 << 4, -} PsycMessageFlag; - /** * String struct. * diff --git a/include/psyc/method.h b/include/psyc/method.h new file mode 100644 index 0000000..e58896f --- /dev/null +++ b/include/psyc/method.h @@ -0,0 +1,65 @@ +#ifndef PSYC_METHOD_H +#define PSYC_METHOD_H + +typedef enum PsycMethodFlag { + PSYC_METHOD_TEMPLATE = 1 << 0, + PSYC_METHOD_REPLY = 1 << 1, + PSYC_METHOD_VISIBLE = 1 << 2, + PSYC_METHOD_LOGGABLE = 1 << 3, + PSYC_METHOD_MANUAL = 1 << 4, +} PsycMethodFlag; + +typedef enum PsycMethod { + PSYC_MC_UNKNOWN, + + PSYC_MC_DATA, + PSYC_MC_ECHO, + PSYC_MC_ECHO_CONTEXT_ENTER, + PSYC_MC_ECHO_CONTEXT_LEAVE, + PSYC_MC_ERROR, + PSYC_MC_FAILURE, + PSYC_MC_INFO, + PSYC_MC_MESSAGE, + PSYC_MC_MESSAGE_ACTION, + PSYC_MC_MESSAGE_ECHO, + PSYC_MC_MESSAGE_ECHO_ACTION, + PSYC_MC_NOTICE, + PSYC_MC_NOTICE_CONTEXT_ENTER, + PSYC_MC_NOTICE_CONTEXT_LEAVE, + PSYC_MC_REQUEST, + PSYC_MC_REQUEST_CONTEXT_ENTER, + PSYC_MC_REQUEST_CONTEXT_LEAVE, + PSYC_MC_STATUS, + PSYC_MC_STATUS_CONTEXTS_ENTERED, + PSYC_MC_WARNING, + + PSYC_METHODS_NUM, +} PsycMethod; + +typedef union PsycTemplates { + PsycString a[PSYC_METHODS_NUM]; + struct { + PsycString _; + PsycString _data; + PsycString _echo; + PsycString _echo_context_enter; + PsycString _echo_context_leave; + PsycString _error; + PsycString _failure; + PsycString _info; + PsycString _message; + PsycString _message_action; + PsycString _message_echo_action; + PsycString _notice; + PsycString _notice_context_enter; + PsycString _notice_context_leave; + PsycString _request; + PsycString _request_context_enter; + PsycString _request_context_leave; + PsycString _status; + PsycString _status_contexts_entered; + PsycString _warning; + } s; +} PsycTemplates; + +#endif diff --git a/include/psyc/packet.h b/include/psyc/packet.h index 1b81cff..3e5a979 100644 --- a/include/psyc/packet.h +++ b/include/psyc/packet.h @@ -16,10 +16,11 @@ * @{ */ -#include -#include #include +#include "syntax.h" +#include "method.h" + /** Modifier flags. */ typedef enum { /// Modifier needs to be checked if it needs length. @@ -67,21 +68,6 @@ typedef enum { PSYC_STATE_RESYNC = '?', } PsycStateOp; -typedef enum PsycMethod { - PSYC_METHOD_UNKNOWN, - PSYC_METHOD_DATA, - PSYC_METHOD_ECHO, - PSYC_METHOD_ERROR, - PSYC_METHOD_FAILURE, - PSYC_METHOD_INFO, - PSYC_METHOD_MESSAGE, - PSYC_METHOD_MESSAGE_ECHO, - PSYC_METHOD_NOTICE, - PSYC_METHOD_REQUEST, - PSYC_METHOD_STATUS, - PSYC_METHOD_WARNING, -} PsycMethod; - /** Structure for a modifier. */ typedef struct { char oper; diff --git a/include/psyc/parse.h b/include/psyc/parse.h index 93f64d3..afa6757 100644 --- a/include/psyc/parse.h +++ b/include/psyc/parse.h @@ -114,7 +114,8 @@ #include #include -#include + +#include "../psyc.h" typedef enum { /// Default Flag. Parse everything. diff --git a/include/psyc/render.h b/include/psyc/render.h index 4595ed0..4a826db 100644 --- a/include/psyc/render.h +++ b/include/psyc/render.h @@ -1,7 +1,7 @@ #ifndef PSYC_RENDER_H #define PSYC_RENDER_H -#include +#include "packet.h" /** * @file psyc/render.h diff --git a/include/psyc/text.h b/include/psyc/text.h index 02adbbe..03d7aff 100644 --- a/include/psyc/text.h +++ b/include/psyc/text.h @@ -158,6 +158,16 @@ psyc_text_bytes_written (PsycTextState *state) PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void *extra); +extern const PsycTemplates psyc_templates; + +static inline const char * +psyc_template (PsycMethod mc, size_t *len) { + PsycString t = psyc_templates.a[mc]; + if (len) + *len = t.length; + return t.data; +} + /** @} */ // end of text group #endif diff --git a/include/psyc/variable.h b/include/psyc/variable.h index fb9ce67..264a79c 100644 --- a/include/psyc/variable.h +++ b/include/psyc/variable.h @@ -13,8 +13,12 @@ extern const PsycString psyc_routing_vars[]; // Variable types in alphabetical order. extern const PsycDictInt psyc_var_types[]; +/// Method names in alphabetical order. +extern const PsycDictInt psyc_methods[]; + extern const size_t psyc_routing_vars_num; extern const size_t psyc_var_types_num; +extern const size_t psyc_methods_num; /** * Is this a routing variable name? @@ -39,6 +43,6 @@ psyc_var_is_list (const char *name, size_t len) } PsycMethod -psyc_method_family (char *method, size_t methodlen, unsigned int *flag); +psyc_method (char *method, size_t methodlen, PsycMethod *family, unsigned int *flag); #endif diff --git a/src/templates.h b/src/templates.h new file mode 100644 index 0000000..46b5acf --- /dev/null +++ b/src/templates.h @@ -0,0 +1,12 @@ +#define _ PSYC_C2STRI + +._echo_context_enter = + _("You enter [_nick_place]" ), +._echo_context_leave = + _("You leave [_nick_place]"), +._notice_context_enter = + _("[_nick] enters [_nick_place]"), +._notice_context_leave = + _("[_nick] leaves [_nick_place]"), +._request_context_enter = + _("[_nick] asks for your permission to enter [_nick_place]"), diff --git a/src/text.c b/src/text.c index 31d1e10..6c91446 100644 --- a/src/text.c +++ b/src/text.c @@ -1,8 +1,12 @@ #include "lib.h" #include +const PsycTemplates psyc_templates = { .s = { +#include "templates.h" +}}; + PsycTextRC -psyc_text (PsycTextState *state, PsycTextCB getValue, void *extra) +psyc_text (PsycTextState *state, PsycTextCB get_value, void *extra) { const char *start = state->tmpl.data, *end; // start & end of variable name const char *prev = state->tmpl.data + state->cursor; @@ -34,7 +38,7 @@ psyc_text (PsycTextState *state, PsycTextCB getValue, void *extra) continue; // [] is invalid, name can't be empty } - ret = getValue(start + state->open.length, + ret = get_value(start + state->open.length, end - start - state->open.length, &value, extra); if (ret < 0) diff --git a/src/variable.c b/src/variable.c index e202757..2c8eeda 100644 --- a/src/variable.c +++ b/src/variable.c @@ -9,7 +9,7 @@ const PsycString psyc_routing_vars[] = { PSYC_C2STRI("_amount_fragments"), PSYC_C2STRI("_context"), //PSYC_C2STRI("_count"), // older PSYC - PSYC_C2STRI("_counter"), // the name for this is supposed to be _count, not _counter + PSYC_C2STRI("_counter"), PSYC_C2STRI("_fragment"), //PSYC_C2STRI("_length"), // older PSYC PSYC_C2STRI("_source"), @@ -30,38 +30,46 @@ const PsycString psyc_routing_vars[] = { // Variable types in alphabetical order. const PsycDictInt psyc_var_types[] = { - {PSYC_C2STRI("_amount"), PSYC_TYPE_AMOUNT}, - {PSYC_C2STRI("_color"), PSYC_TYPE_COLOR}, - {PSYC_C2STRI("_date"), PSYC_TYPE_DATE}, - {PSYC_C2STRI("_def"), PSYC_TYPE_DEF}, - {PSYC_C2STRI("_degree"), PSYC_TYPE_DEGREE}, - {PSYC_C2STRI("_entity"), PSYC_TYPE_ENTITY}, - {PSYC_C2STRI("_flag"), PSYC_TYPE_FLAG}, - {PSYC_C2STRI("_language"), PSYC_TYPE_LANGUAGE}, - {PSYC_C2STRI("_list"), PSYC_TYPE_LIST}, - {PSYC_C2STRI("_nick"), PSYC_TYPE_NICK}, - {PSYC_C2STRI("_page"), PSYC_TYPE_PAGE}, - {PSYC_C2STRI("_table"), PSYC_TYPE_TABLE}, - {PSYC_C2STRI("_time"), PSYC_TYPE_TIME}, - {PSYC_C2STRI("_uniform"), PSYC_TYPE_UNIFORM}, + { PSYC_C2STRI("_amount"), PSYC_TYPE_AMOUNT }, + { PSYC_C2STRI("_color"), PSYC_TYPE_COLOR }, + { PSYC_C2STRI("_date"), PSYC_TYPE_DATE }, + { PSYC_C2STRI("_def"), PSYC_TYPE_DEF }, + { PSYC_C2STRI("_degree"), PSYC_TYPE_DEGREE }, + { PSYC_C2STRI("_entity"), PSYC_TYPE_ENTITY }, + { PSYC_C2STRI("_flag"), PSYC_TYPE_FLAG }, + { PSYC_C2STRI("_language"), PSYC_TYPE_LANGUAGE }, + { PSYC_C2STRI("_list"), PSYC_TYPE_LIST }, + { PSYC_C2STRI("_nick"), PSYC_TYPE_NICK }, + { PSYC_C2STRI("_page"), PSYC_TYPE_PAGE }, + { PSYC_C2STRI("_table"), PSYC_TYPE_TABLE }, + { PSYC_C2STRI("_time"), PSYC_TYPE_TIME }, + { PSYC_C2STRI("_uniform"), PSYC_TYPE_UNIFORM }, }; -const PsycDictInt psyc_method_families[] = { - {PSYC_C2STRI("_data"), PSYC_METHOD_DATA}, - {PSYC_C2STRI("_echo"), PSYC_METHOD_ECHO}, - {PSYC_C2STRI("_failure"), PSYC_METHOD_FAILURE}, - {PSYC_C2STRI("_info"), PSYC_METHOD_INFO}, - {PSYC_C2STRI("_message"), PSYC_METHOD_MESSAGE}, - {PSYC_C2STRI("_message_echo"), PSYC_METHOD_MESSAGE_ECHO}, - {PSYC_C2STRI("_notice"), PSYC_METHOD_NOTICE}, - {PSYC_C2STRI("_request"), PSYC_METHOD_REQUEST}, - {PSYC_C2STRI("_status"), PSYC_METHOD_STATUS}, - {PSYC_C2STRI("_warning"), PSYC_METHOD_WARNING}, +/// Method names in alphabetical order. +const PsycDictInt psyc_methods[] = { + { PSYC_C2STRI("_data"), PSYC_MC_DATA }, + { PSYC_C2STRI("_echo_context_enter"), PSYC_MC_ECHO_CONTEXT_ENTER }, + { PSYC_C2STRI("_echo_context_leave"), PSYC_MC_ECHO_CONTEXT_LEAVE }, + { PSYC_C2STRI("_echo"), PSYC_MC_ECHO }, + { PSYC_C2STRI("_failure"), PSYC_MC_FAILURE }, + { PSYC_C2STRI("_info"), PSYC_MC_INFO }, + { PSYC_C2STRI("_message_echo"), PSYC_MC_MESSAGE_ECHO }, + { PSYC_C2STRI("_message"), PSYC_MC_MESSAGE }, + { PSYC_C2STRI("_notice_context_enter"), PSYC_MC_NOTICE_CONTEXT_ENTER }, + { PSYC_C2STRI("_notice_context_leave"), PSYC_MC_NOTICE_CONTEXT_LEAVE }, + { PSYC_C2STRI("_notice"), PSYC_MC_NOTICE }, + { PSYC_C2STRI("_request_context_enter"), PSYC_MC_REQUEST_CONTEXT_ENTER }, + { PSYC_C2STRI("_request_context_leave"), PSYC_MC_REQUEST_CONTEXT_LEAVE }, + { PSYC_C2STRI("_request"), PSYC_MC_REQUEST }, + { PSYC_C2STRI("_status_contexts_entered"), PSYC_MC_STATUS_CONTEXTS_ENTERED }, + { PSYC_C2STRI("_status"), PSYC_MC_STATUS }, + { PSYC_C2STRI("_warning"), PSYC_MC_WARNING }, }; const size_t psyc_routing_vars_num = PSYC_NUM_ELEM(psyc_routing_vars); const size_t psyc_var_types_num = PSYC_NUM_ELEM(psyc_var_types); -const size_t psyc_method_families_num = PSYC_NUM_ELEM(psyc_method_families); +const size_t psyc_methods_num = PSYC_NUM_ELEM(psyc_methods); /** * Get the type of variable name. @@ -115,57 +123,76 @@ psyc_var_type (const char *name, size_t len) } /** - * Get the family and flags for a method. + * Get the method, its family and its flags. */ PsycMethod -psyc_method_family (char *method, size_t methodlen, unsigned int *flag) +psyc_method (char *method, size_t methodlen, PsycMethod *family, unsigned int *flag) { - int8_t tmp[PSYC_NUM_ELEM(psyc_method_families)]; - int mc = psyc_dict_lookup_int(psyc_method_families, psyc_method_families_num, + int8_t tmp[PSYC_NUM_ELEM(psyc_methods)]; + int mc = psyc_dict_lookup_int(psyc_methods, psyc_methods_num, method, methodlen, PSYC_YES, tmp); - if (!flag) - return mc; - switch (mc) { - case PSYC_METHOD_DATA: + case PSYC_MC_DATA: + *family = PSYC_MC_DATA; + *flag = 0; break; - case PSYC_METHOD_ECHO: - *flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE; + case PSYC_MC_ECHO: + case PSYC_MC_ECHO_CONTEXT_ENTER: + case PSYC_MC_ECHO_CONTEXT_LEAVE: + *family = PSYC_MC_ECHO; + *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE; break; - case PSYC_METHOD_ERROR: - *flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE - | PSYC_MESSAGE_LOGGABLE; + case PSYC_MC_ERROR: + *family = PSYC_MC_ERROR; + *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE + | PSYC_METHOD_LOGGABLE; break; - case PSYC_METHOD_FAILURE: - *flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE - | PSYC_MESSAGE_LOGGABLE; + case PSYC_MC_FAILURE: + *family = PSYC_MC_FAILURE; + *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE + | PSYC_METHOD_LOGGABLE; break; - case PSYC_METHOD_INFO: - *flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE - | PSYC_MESSAGE_LOGGABLE; + case PSYC_MC_INFO: + *family = PSYC_MC_INFO; + *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE + | PSYC_METHOD_LOGGABLE; break; - case PSYC_METHOD_MESSAGE: - *flag = PSYC_MESSAGE_VISIBLE | PSYC_MESSAGE_LOGGABLE | PSYC_MESSAGE_MANUAL; + case PSYC_MC_MESSAGE: + *family = PSYC_MC_MESSAGE; + *flag = PSYC_METHOD_VISIBLE | PSYC_METHOD_LOGGABLE | PSYC_METHOD_MANUAL; break; - case PSYC_METHOD_MESSAGE_ECHO: - *flag = PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE | PSYC_MESSAGE_LOGGABLE - | PSYC_MESSAGE_MANUAL; + case PSYC_MC_MESSAGE_ECHO: + *family = PSYC_MC_MESSAGE_ECHO; + *flag = PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE | PSYC_METHOD_LOGGABLE + | PSYC_METHOD_MANUAL; break; - case PSYC_METHOD_NOTICE: - *flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_VISIBLE | PSYC_MESSAGE_LOGGABLE; + case PSYC_MC_NOTICE: + case PSYC_MC_NOTICE_CONTEXT_ENTER: + case PSYC_MC_NOTICE_CONTEXT_LEAVE: + *family = PSYC_MC_NOTICE; + *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_VISIBLE | PSYC_METHOD_LOGGABLE; break; - case PSYC_METHOD_REQUEST: - *flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_VISIBLE | PSYC_MESSAGE_LOGGABLE; + case PSYC_MC_REQUEST: + case PSYC_MC_REQUEST_CONTEXT_ENTER: + case PSYC_MC_REQUEST_CONTEXT_LEAVE: + *family = PSYC_MC_REQUEST; + *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_VISIBLE | PSYC_METHOD_LOGGABLE; break; - case PSYC_METHOD_STATUS: - *flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE - | PSYC_MESSAGE_LOGGABLE; + case PSYC_MC_STATUS: + case PSYC_MC_STATUS_CONTEXTS_ENTERED: + *family = PSYC_MC_STATUS; + *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE + | PSYC_METHOD_LOGGABLE; break; - case PSYC_METHOD_WARNING: - *flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE - | PSYC_MESSAGE_LOGGABLE; + case PSYC_MC_WARNING: + *family = PSYC_MC_WARNING; + *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE + | PSYC_METHOD_LOGGABLE; break; + default: + *family = mc; + *flag = 0; } return mc; diff --git a/test/test_text.c b/test/test_text.c index 40716bb..3d69ea4 100644 --- a/test/test_text.c +++ b/test/test_text.c @@ -99,7 +99,11 @@ main (int argc, char **argv) return 10 + i; } - puts("psyc_text passed all tests."); + size_t tlen = 0; + char *t = psyc_template(PSYC_MC_NOTICE_CONTEXT_ENTER, &tlen); + printf("_notice_context_enter = %s, %ld\n", t, tlen); + + printf("psyc_text passed all tests.\n"); return 0; }