server/src/common/kernel/order.c

117 lines
2 KiB
C
Raw Normal View History

/* vi: set ts=2:
+-------------------+
| | Christian Schlittchen <corwin@amber.kn-bremen.de>
| Eressea PBEM host | Enno Rehling <enno@eressea-pbem.de>
| (c) 1998 - 2004 | Katja Zedel <katze@felidae.kn-bremen.de>
| |
+-------------------+
This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#include <config.h>
#include "eressea.h"
#include "order.h"
/* libc includes */
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
keyword_t
2004-06-21 18:45:27 +02:00
get_keyword(const order * ord)
{
2004-06-21 18:45:27 +02:00
if (ord==NULL) {
return NOKEYWORD;
}
return ord->_keyword;
}
const char *
getcommand(const order * ord)
{
2004-06-21 18:45:27 +02:00
return ord->_str;
}
void
free_order(order * ord)
{
2004-06-21 18:45:27 +02:00
if (ord!=NULL && --ord->_refcount==0) {
free(ord->_str);
free(ord);
}
}
order *
copy_order(order * ord)
{
2004-06-21 18:45:27 +02:00
if (ord!=NULL) ++ord->_refcount;
return ord;
}
void
set_order(struct order ** destp, struct order * src)
{
2004-06-21 18:45:27 +02:00
if (*destp==src) return;
free_order(*destp);
*destp = copy_order(src);
}
void
free_orders(order ** olist)
{
while (*olist) {
order * ord = *olist;
*olist = ord->next;
free_order(ord);
}
}
order *
parse_order(const char * s, const struct locale * lang)
{
2004-06-21 18:45:27 +02:00
while (isspace(*s)) ++s;
if (*s==0) return NULL;
else {
order * ord = (order*)malloc(sizeof(order));
2004-06-21 18:45:27 +02:00
ord->_str = strdup(s);
ord->_keyword = findkeyword(parse_token(&s), lang);
ord->_refcount = 1;
ord->next = NULL;
return ord;
2004-06-21 18:45:27 +02:00
}
}
boolean
is_persistent(const order * cmd)
{
#ifdef AT_PERSISTENT
2004-06-21 18:45:27 +02:00
if (cmd->_str[0] == '@') return true;
#endif /* Nur kurze Befehle! */
2004-06-21 18:45:27 +02:00
switch (cmd->_keyword) {
case K_KOMMENTAR:
case K_LIEFERE:
return true;
break;
}
return false;
}
char *
write_order(const order * cmd, const struct locale * lang, char * buffer, size_t size)
{
2004-06-21 18:45:27 +02:00
if (cmd==0) {
buffer[0]=0;
} else {
2004-06-21 18:45:27 +02:00
#ifndef NDEBUG
2004-06-26 11:28:39 +02:00
const char * s = cmd->_str;
assert(findkeyword(parse_token(&s), lang)==cmd->_keyword);
2004-06-21 18:45:27 +02:00
#endif
strncpy(buffer, cmd->_str, size);
}
return buffer;
}