server/src/util/nrmessage.c

119 lines
2.9 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
#include <platform.h>
#include "nrmessage.h"
/* util includes */
#include "log.h"
#include "message.h"
#include "language.h"
#include "translation.h"
#include "strings.h"
2010-08-08 10:06:34 +02:00
/* libc includes */
#include <assert.h>
#include <string.h>
#include <stdlib.h>
2018-05-18 08:34:00 +02:00
typedef struct nrmessage_type {
2018-05-18 18:50:13 +02:00
const struct message_type *mtype;
char *vars;
struct nrmessage_type *next;
2018-05-18 08:34:00 +02:00
} nrmessage_type;
2010-08-08 10:06:34 +02:00
#define NRT_MAXHASH 1021
static nrmessage_type *nrtypes[NRT_MAXHASH] = { 0 };
void free_nrmesssages(void) {
int i;
for (i = 0; i != NRT_MAXHASH; ++i) {
while (nrtypes[i]) {
nrmessage_type *nr = nrtypes[i];
nrtypes[i] = nr->next;
free(nr->vars);
free(nr);
}
}
}
2010-08-08 10:06:34 +02:00
2018-05-18 02:46:34 +02:00
const char *nrt_string(const struct message_type *mtype,
const struct locale *lang)
2010-08-08 10:06:34 +02:00
{
2018-05-18 02:46:34 +02:00
const char * str = locale_getstring(lang, mtype->name);
2018-05-17 22:47:16 +02:00
if (!str) {
2018-05-18 02:46:34 +02:00
str = locale_getstring(default_locale, mtype->name);
2018-05-17 22:47:16 +02:00
}
assert(str);
return str;
2010-08-08 10:06:34 +02:00
}
2018-05-18 02:46:34 +02:00
static nrmessage_type *nrt_find(const struct message_type * mtype)
2010-08-08 10:06:34 +02:00
{
nrmessage_type *found = NULL;
unsigned int hash = mtype->key % NRT_MAXHASH;
nrmessage_type *type = nrtypes[hash];
while (type) {
if (type->mtype == mtype) {
if (found == NULL) {
found = type;
}
}
type = type->next;
2010-08-08 10:06:34 +02:00
}
if (!found) {
log_warning("could not find nr-type %s\n", mtype->name);
}
return found;
2010-08-08 10:06:34 +02:00
}
void
nrt_register(const struct message_type *mtype)
2010-08-08 10:06:34 +02:00
{
unsigned int hash = mtype->key % NRT_MAXHASH;
nrmessage_type *nrt = nrtypes[hash];
while (nrt && nrt->mtype != mtype) {
nrt = nrt->next;
}
if (nrt) {
log_error("duplicate message-type %s\n", mtype->name);
assert(!nrt || !"trying to register same nr-type twice");
}
else {
int i;
char zNames[256];
char *c = zNames;
nrt = calloc(1, sizeof(nrmessage_type));
if (!nrt) abort();
nrt->mtype = mtype;
nrt->next = nrtypes[hash];
nrtypes[hash] = nrt;
*c = '\0';
for (i = 0; i != mtype->nparameters; ++i) {
if (i != 0)
*c++ = ' ';
2017-12-30 19:49:21 +01:00
c += str_strlcpy(c, mtype->pnames[i], sizeof(zNames)-(c-zNames));
}
nrt->vars = str_strdup(zNames);
if (!nrt->vars) abort();
2010-08-08 10:06:34 +02:00
}
}
size_t
2011-03-07 08:02:35 +01:00
nr_render(const struct message *msg, const struct locale *lang, char *buffer,
size_t size, const void *userdata)
2010-08-08 10:06:34 +02:00
{
struct nrmessage_type *nrt = nrt_find(msg->type);
if (nrt) {
const char *m =
2018-05-18 02:46:34 +02:00
translate(nrt_string(msg->type, lang), userdata, nrt->vars, msg->parameters);
if (m) {
2017-12-30 19:49:21 +01:00
return str_strlcpy((char *)buffer, m, size);
}
else {
log_error("Couldn't render message %s\n", nrt->mtype->name);
}
2010-08-08 10:06:34 +02:00
}
if (size > 0 && buffer)
buffer[0] = 0;
return 0;
2010-08-08 10:06:34 +02:00
}