diff --git a/src/common/attributes/Jamfile b/src/common/attributes/Jamfile index 44edac56c..ce3307cf3 100644 --- a/src/common/attributes/Jamfile +++ b/src/common/attributes/Jamfile @@ -31,6 +31,7 @@ SOURCES = targetregion.c ugroup.c viewrange.c + variable.c ; Library attributes : $(SOURCES) ; diff --git a/src/common/attributes/attributes.c b/src/common/attributes/attributes.c index 1ff19821f..c53bab14e 100644 --- a/src/common/attributes/attributes.c +++ b/src/common/attributes/attributes.c @@ -39,6 +39,7 @@ #endif #include "moved.h" #include "aggressive.h" +#include "variable.h" /* util includes */ #include @@ -74,4 +75,5 @@ init_attributes(void) init_ugroup(); #endif init_aggressive(); + init_variable(); } diff --git a/src/common/attributes/variable.c b/src/common/attributes/variable.c new file mode 100644 index 000000000..f4ed021b0 --- /dev/null +++ b/src/common/attributes/variable.c @@ -0,0 +1,132 @@ +/* vi: set ts=2: + * + * + * Eressea PB(E)M host Copyright (C) 1998-2004 + * Christian Schlittchen (corwin@amber.kn-bremen.de) + * Katja Zedel (katze@felidae.kn-bremen.de) + * Enno Rehling (enno@eressea-pbem.de) + * + * This program may not be used, modified or distributed without + * prior permission by the authors of Eressea. + */ + +#include +#include +#include +#include "variable.h" + +/* libc includes */ +#include +#include +#include +#include + +typedef struct { + char *key; + char *value; +} variable; + +static void +initialize_variable(struct attrib * a) +{ + a->data.v = malloc(sizeof(variable)); +} + +static void +finalize_variable(struct attrib * a) +{ + free(a->data.v); +} + +static void +write_variable(const struct attrib * a, FILE *F) +{ + fwritestr(F, ((variable *)(a->data.v))->key); + fputc(' ', F); + fwritestr(F, ((variable *)(a->data.v))->value); + fputc(' ', F); +} + +static int +read_variable(struct attrib *a, FILE *F) +{ + char localBuffer[BUFSIZE]; + + freadstr(F, localBuffer, sizeof(localBuffer)); + ((variable *)(a->data.v))->key = strdup(localBuffer); + + freadstr(F, localBuffer, sizeof(localBuffer)); + ((variable *)(a->data.v))->value = strdup(localBuffer); + + return AT_READ_OK; +} + +attrib_type at_variable = { + "variable", initialize_variable, finalize_variable, NULL, + write_variable, read_variable +}; + +const char * +get_variable(attrib *a, const char *key) +{ + attrib *ap; + + for(ap = a; ap; ap=ap->nexttype) { + if(strcmp(key, ((variable *)ap->data.v)->key)) { + break; + } + } + + if(ap) { + return ((variable *)ap->data.v)->value; + } + + return NULL; +} + +void +set_variable(attrib **app, const char *key, const char *value) +{ + attrib *ap; + + assert(value); + + for(ap = *app; ap; ap=ap->nexttype) { + if(strcmp(key, ((variable *)ap->data.v)->key)) { + break; + } + } + + if(ap) { + free(((variable *)ap->data.v)->value); + ((variable *)ap->data.v)->value = strdup(value); + } else { + ap = a_add(app, a_new(&at_variable)); + ((variable *)ap->data.v)->key = strdup(key); + ((variable *)ap->data.v)->value = strdup(value); + } +} + +void +delete_variable(attrib **app, const char *key) +{ + attrib *ap; + + for(ap = *app; ap; ap=ap->nexttype) { + if(strcmp(key, ((variable *)ap->data.v)->key)) { + break; + } + } + + if(ap) { + free(((variable *)ap->data.v)->key); + free(((variable *)ap->data.v)->value); + a_remove(app, ap); + } +} + +void +init_variable(void) +{ + at_register(&at_variable); +} diff --git a/src/common/attributes/variable.h b/src/common/attributes/variable.h new file mode 100644 index 000000000..53ea7f9ec --- /dev/null +++ b/src/common/attributes/variable.h @@ -0,0 +1,30 @@ +/* vi: set ts=2: + * + * + * Eressea PB(E)M host Copyright (C) 1998-2004 + * Christian Schlittchen (corwin@amber.kn-bremen.de) + * Katja Zedel (katze@felidae.kn-bremen.de) + * Enno Rehling (enno@eressea-pbem.de) + * + * This program may not be used, modified or distributed without + * prior permission by the authors of Eressea. + */ + +#ifndef H_ATTRIBUTE_VARIABLE +#define H_ATTRIBUTE_VARIABLE + +#ifdef __cplusplus +extern "C" { +#endif + +const char *get_variable(struct attrib *a, const char *key); +void set_variable(struct attrib **app, const char *key, const char *value); +void delete_variable(struct attrib **app, const char *key); +void init_variable(void); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/src/eressea/lua/alliance.cpp b/src/eressea/lua/alliance.cpp index c831d76a7..773385b68 100644 --- a/src/eressea/lua/alliance.cpp +++ b/src/eressea/lua/alliance.cpp @@ -12,6 +12,7 @@ #include #include #include +#include using namespace luabind;