server/src/util/attrib.h

122 lines
4.6 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/*
2015-01-30 22:10:29 +01:00
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
2014-10-18 14:16:26 +02:00
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
2010-08-08 10:06:34 +02:00
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#ifndef ATTRIB_H
#define ATTRIB_H
2014-10-18 14:16:26 +02:00
#include <stdbool.h>
2010-08-08 10:06:34 +02:00
#ifdef __cplusplus
extern "C" {
#endif
2014-10-18 14:16:26 +02:00
struct gamedata;
struct storage;
typedef void(*afun) (void);
2010-08-08 10:06:34 +02:00
2014-10-18 14:16:26 +02:00
typedef struct attrib {
const struct attrib_type *type;
union {
afun f;
void *v;
2014-10-18 14:16:26 +02:00
int i;
float flt;
char c;
short s;
short sa[2];
char ca[4];
2014-10-18 14:16:26 +02:00
} data;
/* internal data, do not modify: */
struct attrib *next; /* next attribute in the list */
struct attrib *nexttype; /* skip to attribute of a different type */
} attrib;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
#define ATF_UNIQUE (1<<0) /* only one per attribute-list */
#define ATF_PRESERVE (1<<1) /* preserve order in list. append to back */
2010-08-08 10:06:34 +02:00
#define ATF_USER_DEFINED (1<<2) /* use this to make udf */
2014-10-18 14:16:26 +02:00
typedef struct attrib_type {
const char *name;
void(*initialize) (struct attrib *);
void(*finalize) (struct attrib *);
int(*age) (struct attrib *, void *owner);
2014-10-18 14:16:26 +02:00
/* age returns 0 if the attribute needs to be removed, !=0 otherwise */
void(*write) (const struct attrib *, const void *owner, struct storage *);
int(*read) (struct attrib *, void *owner, struct gamedata *); /* return AT_READ_OK on success, AT_READ_FAIL if attrib needs removal */
void(*upgrade) (struct attrib **alist, struct attrib *a);
2014-10-18 14:16:26 +02:00
unsigned int flags;
/* ---- internal data, do not modify: ---- */
struct attrib_type *nexthash;
unsigned int hashkey;
} attrib_type;
2010-08-08 10:06:34 +02:00
void at_register(attrib_type * at);
void at_deprecate(const char * name, int(*reader)(attrib *, void *, struct gamedata *));
struct attrib_type *at_find(const char *name);
2010-08-08 10:06:34 +02:00
void write_attribs(struct storage *store, struct attrib *alist, const void *owner);
int read_attribs(struct gamedata *store, struct attrib **alist, void *owner);
attrib *a_select(attrib * a, const void *data, bool(*compare) (const attrib *, const void *));
attrib *a_find(attrib * a, const attrib_type * at);
attrib *a_add(attrib ** pa, attrib * at);
int a_remove(attrib ** pa, attrib * at);
void a_removeall(attrib ** a, const attrib_type * at);
attrib *a_new(const attrib_type * at);
int a_age(attrib ** attribs, void *owner);
2010-08-08 10:06:34 +02:00
int a_read_orig(struct gamedata *data, attrib ** attribs, void *owner);
void a_write_orig(struct storage *store, const attrib * attribs, const void *owner);
int a_read(struct gamedata *data, attrib ** attribs, void *owner);
void a_write(struct storage *store, const attrib * attribs, const void *owner);
2010-08-08 10:06:34 +02:00
int a_readint(struct attrib *a, void *owner, struct gamedata *);
void a_writeint(const struct attrib *a, const void *owner,
struct storage *store);
int a_readshorts(struct attrib *a, void *owner, struct gamedata *);
void a_writeshorts(const struct attrib *a, const void *owner,
struct storage *store);
int a_readchars(struct attrib *a, void *owner, struct gamedata *);
void a_writechars(const struct attrib *a, const void *owner,
struct storage *store);
int a_readstring(struct attrib *a, void *owner, struct gamedata *);
void a_writestring(const struct attrib *a, const void *owner,
struct storage *);
void a_finalizestring(struct attrib *a);
void attrib_done(void);
2010-08-08 10:06:34 +02:00
#define DEFAULT_AGE NULL
#define DEFAULT_INIT NULL
#define DEFAULT_FINALIZE NULL
#define NO_WRITE NULL
#define NO_READ NULL
#define AT_READ_OK 0
#define AT_READ_FAIL -1
#define AT_READ_DEPR 1 /* a deprecated attribute was read, should run a_upgrade */
2010-08-08 10:06:34 +02:00
#define AT_AGE_REMOVE 0 /* remove the attribute after calling age() */
#define AT_AGE_KEEP 1 /* keep the attribute for another turn */
2010-08-08 10:06:34 +02:00
#ifdef __cplusplus
}
#endif
#endif