2015-01-30 20:37:14 +01:00
|
|
|
/*
|
2010-08-08 10:06:34 +02:00
|
|
|
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
| | Enno Rehling <enno@eressea.de>
|
|
|
|
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
|
|
|
|
| (c) 1998 - 2003 | Henning Peters <faroul@beyond.kn-bremen.de>
|
|
|
|
| | Ingo Wilken <Ingo.Wilken@informatik.uni-oldenburg.de>
|
|
|
|
+-------------------+ Stefan Reich <reich@halbling.de>
|
|
|
|
|
|
|
|
This program may not be used, modified or distributed
|
|
|
|
without prior permission by the authors of Eressea.
|
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
*/
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
#include "crmessage.h"
|
|
|
|
|
|
|
|
#include "message.h"
|
2015-05-18 08:59:38 +02:00
|
|
|
#include "strings.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/** type to string conversion **/
|
|
|
|
typedef struct tsf_list {
|
2015-01-30 20:37:14 +01:00
|
|
|
struct tsf_list *next;
|
|
|
|
const char *name;
|
|
|
|
tostring_f fun;
|
2010-08-08 10:06:34 +02:00
|
|
|
} tsf_list;
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static tsf_list *tostringfs;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2016-09-07 21:15:24 +02:00
|
|
|
void crmessage_done(void) {
|
|
|
|
tsf_list **tsp = &tostringfs;
|
|
|
|
while (*tsp) {
|
|
|
|
tsf_list *ts = *tsp;
|
|
|
|
*tsp = ts->next;
|
|
|
|
free(ts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static tostring_f tsf_find(const char *name)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (name != NULL) {
|
|
|
|
tsf_list *tsf;
|
|
|
|
for (tsf = tostringfs; tsf; tsf = tsf->next) {
|
|
|
|
if (!strcmp(tsf->name, name))
|
|
|
|
return tsf->fun;
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void tsf_register(const char *name, tostring_f fun)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
tsf_list *tsf;
|
|
|
|
for (tsf = tostringfs; tsf; tsf = tsf->next) {
|
|
|
|
if (!strcmp(tsf->name, name))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tsf == NULL) {
|
|
|
|
tsf = malloc(sizeof(tsf_list));
|
|
|
|
tsf->fun = fun;
|
|
|
|
tsf->name = name;
|
|
|
|
tsf->next = tostringfs;
|
|
|
|
tostringfs = tsf;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** crmesssage **/
|
|
|
|
typedef struct crmessage_type {
|
2015-01-30 20:37:14 +01:00
|
|
|
const struct message_type *mtype;
|
|
|
|
tostring_f *renderers;
|
|
|
|
struct crmessage_type *next;
|
2010-08-08 10:06:34 +02:00
|
|
|
} crmessage_type;
|
|
|
|
|
|
|
|
#define CRMAXHASH 63
|
2011-03-07 08:02:35 +01:00
|
|
|
static crmessage_type *crtypes[CRMAXHASH];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static crmessage_type *crt_find(const struct message_type *mtype)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
unsigned int hash = hashstring(mtype->name) % CRMAXHASH;
|
|
|
|
crmessage_type *found = NULL;
|
|
|
|
crmessage_type *type = crtypes[hash];
|
|
|
|
while (type) {
|
|
|
|
if (type->mtype == mtype)
|
|
|
|
found = type;
|
|
|
|
type = type->next;
|
|
|
|
}
|
|
|
|
return found;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void crt_register(const struct message_type *mtype)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
unsigned int hash = hashstring(mtype->name) % CRMAXHASH;
|
|
|
|
crmessage_type *crt = crtypes[hash];
|
|
|
|
while (crt && crt->mtype != mtype) {
|
|
|
|
crt = crt->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
if (!crt) {
|
|
|
|
crt = malloc(sizeof(crmessage_type));
|
|
|
|
crt->mtype = mtype;
|
|
|
|
crt->next = crtypes[hash];
|
|
|
|
crtypes[hash] = crt;
|
|
|
|
if (mtype->nparameters > 0) {
|
2015-11-05 11:14:53 +01:00
|
|
|
int i;
|
2015-01-30 20:37:14 +01:00
|
|
|
crt->renderers = malloc(sizeof(tostring_f) * mtype->nparameters);
|
2015-11-05 11:14:53 +01:00
|
|
|
/* can be scrapped for memory vs. speed */
|
|
|
|
for (i = 0; i != mtype->nparameters; ++i) {
|
|
|
|
crt->renderers[i] = tsf_find(mtype->types[i]->name);
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
crt->renderers = NULL;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int cr_render(const message * msg, char *buffer, const void *userdata)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int i;
|
|
|
|
char *c = buffer;
|
|
|
|
struct crmessage_type *crt = crt_find(msg->type);
|
|
|
|
|
|
|
|
if (crt == NULL)
|
|
|
|
return -1;
|
|
|
|
for (i = 0; i != msg->type->nparameters; ++i) {
|
|
|
|
if (crt->renderers[i] == NULL) {
|
|
|
|
log_error("No renderer for argument %s:%s of \"%s\"\n", msg->type->pnames[i], msg->type->types[i]->name, msg->type->name);
|
|
|
|
continue; /* strcpy(c, (const char*)msg->locale_string(u->faction->locale, parameters[i])); */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (crt->renderers[i](msg->parameters[i], c, userdata) != 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c += strlen(c);
|
|
|
|
sprintf(c, ";%s\n", msg->type->pnames[i]);
|
|
|
|
c += strlen(c);
|
2011-03-07 08:02:35 +01:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int cr_string(variant var, char *buffer, const void *userdata)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
sprintf(buffer, "\"%s\"", (const char *)var.v);
|
|
|
|
unused_arg(userdata);
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int cr_int(variant var, char *buffer, const void *userdata)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
sprintf(buffer, "%d", var.i);
|
|
|
|
unused_arg(userdata);
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
int cr_ignore(variant var, char *buffer, const void *userdata)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
unused_arg(var);
|
|
|
|
unused_arg(buffer);
|
|
|
|
unused_arg(userdata);
|
|
|
|
return -1;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|