1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00

methods & templates

This commit is contained in:
Gabor Adam Toth 2011-11-30 13:51:50 +01:00
parent 1aa08235b7
commit 5dd68d87fc
11 changed files with 197 additions and 92 deletions

View file

@ -94,14 +94,6 @@ typedef enum {
PSYC_LIST_BINARY = 2, PSYC_LIST_BINARY = 2,
} PsycListType; } 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. * String struct.
* *

65
include/psyc/method.h Normal file
View file

@ -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

View file

@ -16,10 +16,11 @@
* @{ * @{
*/ */
#include <psyc.h>
#include <psyc/syntax.h>
#include <math.h> #include <math.h>
#include "syntax.h"
#include "method.h"
/** Modifier flags. */ /** Modifier flags. */
typedef enum { typedef enum {
/// Modifier needs to be checked if it needs length. /// Modifier needs to be checked if it needs length.
@ -67,21 +68,6 @@ typedef enum {
PSYC_STATE_RESYNC = '?', PSYC_STATE_RESYNC = '?',
} PsycStateOp; } 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. */ /** Structure for a modifier. */
typedef struct { typedef struct {
char oper; char oper;

View file

@ -114,7 +114,8 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <psyc.h>
#include "../psyc.h"
typedef enum { typedef enum {
/// Default Flag. Parse everything. /// Default Flag. Parse everything.

View file

@ -1,7 +1,7 @@
#ifndef PSYC_RENDER_H #ifndef PSYC_RENDER_H
#define PSYC_RENDER_H #define PSYC_RENDER_H
#include <psyc/packet.h> #include "packet.h"
/** /**
* @file psyc/render.h * @file psyc/render.h

View file

@ -158,6 +158,16 @@ psyc_text_bytes_written (PsycTextState *state)
PsycTextRC PsycTextRC
psyc_text (PsycTextState *state, PsycTextCB getValue, void *extra); 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 /** @} */ // end of text group
#endif #endif

View file

@ -13,8 +13,12 @@ extern const PsycString psyc_routing_vars[];
// Variable types in alphabetical order. // Variable types in alphabetical order.
extern const PsycDictInt psyc_var_types[]; 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_routing_vars_num;
extern const size_t psyc_var_types_num; extern const size_t psyc_var_types_num;
extern const size_t psyc_methods_num;
/** /**
* Is this a routing variable name? * Is this a routing variable name?
@ -39,6 +43,6 @@ psyc_var_is_list (const char *name, size_t len)
} }
PsycMethod 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 #endif

12
src/templates.h Normal file
View file

@ -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]"),

View file

@ -1,8 +1,12 @@
#include "lib.h" #include "lib.h"
#include <psyc/text.h> #include <psyc/text.h>
const PsycTemplates psyc_templates = { .s = {
#include "templates.h"
}};
PsycTextRC 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 *start = state->tmpl.data, *end; // start & end of variable name
const char *prev = state->tmpl.data + state->cursor; 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 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); end - start - state->open.length, &value, extra);
if (ret < 0) if (ret < 0)

View file

@ -9,7 +9,7 @@ const PsycString psyc_routing_vars[] = {
PSYC_C2STRI("_amount_fragments"), PSYC_C2STRI("_amount_fragments"),
PSYC_C2STRI("_context"), PSYC_C2STRI("_context"),
//PSYC_C2STRI("_count"), // older PSYC //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("_fragment"),
//PSYC_C2STRI("_length"), // older PSYC //PSYC_C2STRI("_length"), // older PSYC
PSYC_C2STRI("_source"), PSYC_C2STRI("_source"),
@ -46,22 +46,30 @@ const PsycDictInt psyc_var_types[] = {
{ PSYC_C2STRI("_uniform"), PSYC_TYPE_UNIFORM }, { PSYC_C2STRI("_uniform"), PSYC_TYPE_UNIFORM },
}; };
const PsycDictInt psyc_method_families[] = { /// Method names in alphabetical order.
{PSYC_C2STRI("_data"), PSYC_METHOD_DATA}, const PsycDictInt psyc_methods[] = {
{PSYC_C2STRI("_echo"), PSYC_METHOD_ECHO}, { PSYC_C2STRI("_data"), PSYC_MC_DATA },
{PSYC_C2STRI("_failure"), PSYC_METHOD_FAILURE}, { PSYC_C2STRI("_echo_context_enter"), PSYC_MC_ECHO_CONTEXT_ENTER },
{PSYC_C2STRI("_info"), PSYC_METHOD_INFO}, { PSYC_C2STRI("_echo_context_leave"), PSYC_MC_ECHO_CONTEXT_LEAVE },
{PSYC_C2STRI("_message"), PSYC_METHOD_MESSAGE}, { PSYC_C2STRI("_echo"), PSYC_MC_ECHO },
{PSYC_C2STRI("_message_echo"), PSYC_METHOD_MESSAGE_ECHO}, { PSYC_C2STRI("_failure"), PSYC_MC_FAILURE },
{PSYC_C2STRI("_notice"), PSYC_METHOD_NOTICE}, { PSYC_C2STRI("_info"), PSYC_MC_INFO },
{PSYC_C2STRI("_request"), PSYC_METHOD_REQUEST}, { PSYC_C2STRI("_message_echo"), PSYC_MC_MESSAGE_ECHO },
{PSYC_C2STRI("_status"), PSYC_METHOD_STATUS}, { PSYC_C2STRI("_message"), PSYC_MC_MESSAGE },
{PSYC_C2STRI("_warning"), PSYC_METHOD_WARNING}, { 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_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_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. * 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 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)]; int8_t tmp[PSYC_NUM_ELEM(psyc_methods)];
int mc = psyc_dict_lookup_int(psyc_method_families, psyc_method_families_num, int mc = psyc_dict_lookup_int(psyc_methods, psyc_methods_num,
method, methodlen, PSYC_YES, tmp); method, methodlen, PSYC_YES, tmp);
if (!flag)
return mc;
switch (mc) { switch (mc) {
case PSYC_METHOD_DATA: case PSYC_MC_DATA:
*family = PSYC_MC_DATA;
*flag = 0;
break; break;
case PSYC_METHOD_ECHO: case PSYC_MC_ECHO:
*flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE; 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; break;
case PSYC_METHOD_ERROR: case PSYC_MC_ERROR:
*flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE *family = PSYC_MC_ERROR;
| PSYC_MESSAGE_LOGGABLE; *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE
| PSYC_METHOD_LOGGABLE;
break; break;
case PSYC_METHOD_FAILURE: case PSYC_MC_FAILURE:
*flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE *family = PSYC_MC_FAILURE;
| PSYC_MESSAGE_LOGGABLE; *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE
| PSYC_METHOD_LOGGABLE;
break; break;
case PSYC_METHOD_INFO: case PSYC_MC_INFO:
*flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE *family = PSYC_MC_INFO;
| PSYC_MESSAGE_LOGGABLE; *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE
| PSYC_METHOD_LOGGABLE;
break; break;
case PSYC_METHOD_MESSAGE: case PSYC_MC_MESSAGE:
*flag = PSYC_MESSAGE_VISIBLE | PSYC_MESSAGE_LOGGABLE | PSYC_MESSAGE_MANUAL; *family = PSYC_MC_MESSAGE;
*flag = PSYC_METHOD_VISIBLE | PSYC_METHOD_LOGGABLE | PSYC_METHOD_MANUAL;
break; break;
case PSYC_METHOD_MESSAGE_ECHO: case PSYC_MC_MESSAGE_ECHO:
*flag = PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE | PSYC_MESSAGE_LOGGABLE *family = PSYC_MC_MESSAGE_ECHO;
| PSYC_MESSAGE_MANUAL; *flag = PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE | PSYC_METHOD_LOGGABLE
| PSYC_METHOD_MANUAL;
break; break;
case PSYC_METHOD_NOTICE: case PSYC_MC_NOTICE:
*flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_VISIBLE | PSYC_MESSAGE_LOGGABLE; 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; break;
case PSYC_METHOD_REQUEST: case PSYC_MC_REQUEST:
*flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_VISIBLE | PSYC_MESSAGE_LOGGABLE; 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; break;
case PSYC_METHOD_STATUS: case PSYC_MC_STATUS:
*flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE case PSYC_MC_STATUS_CONTEXTS_ENTERED:
| PSYC_MESSAGE_LOGGABLE; *family = PSYC_MC_STATUS;
*flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE
| PSYC_METHOD_LOGGABLE;
break; break;
case PSYC_METHOD_WARNING: case PSYC_MC_WARNING:
*flag = PSYC_MESSAGE_TEMPLATE | PSYC_MESSAGE_REPLY | PSYC_MESSAGE_VISIBLE *family = PSYC_MC_WARNING;
| PSYC_MESSAGE_LOGGABLE; *flag = PSYC_METHOD_TEMPLATE | PSYC_METHOD_REPLY | PSYC_METHOD_VISIBLE
| PSYC_METHOD_LOGGABLE;
break; break;
default:
*family = mc;
*flag = 0;
} }
return mc; return mc;

View file

@ -99,7 +99,11 @@ main (int argc, char **argv)
return 10 + i; 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; return 0;
} }