letzter Schliff an den order-Strukturen und möglichen defines.

This commit is contained in:
Enno Rehling 2005-05-11 20:24:51 +00:00
parent d72f078d55
commit 8a9727e5f2
4 changed files with 85 additions and 31 deletions

View File

@ -3056,11 +3056,11 @@ produce(void)
sabotage_cmd(u, u->thisorder); sabotage_cmd(u, u->thisorder);
break; break;
case K_ZUECHTE: case K_BREED:
breed_cmd(u, u->thisorder); breed_cmd(u, u->thisorder);
break; break;
case K_PFLANZE: case K_PLANT:
plant_cmd(u, u->thisorder); plant_cmd(u, u->thisorder);
break; break;

View File

@ -423,7 +423,7 @@ enum {
K_CAST, K_CAST,
K_RESHOW, K_RESHOW,
K_DESTROY, K_DESTROY,
K_ZUECHTE, K_BREED,
K_DEFAULT, K_DEFAULT,
K_REPORT, K_REPORT,
K_URSPRUNG, K_URSPRUNG,
@ -445,7 +445,7 @@ enum {
#endif #endif
K_PREFIX, K_PREFIX,
K_SYNONYM, K_SYNONYM,
K_PFLANZE, K_PLANT,
K_WEREWOLF, K_WEREWOLF,
K_XE, K_XE,
K_ALLIANCE, K_ALLIANCE,

View File

@ -22,7 +22,17 @@
#include <string.h> #include <string.h>
#define SHORT_STRINGS #define SHORT_STRINGS
#define SHARE_ORDERS
#ifdef SHARE_ORDERS
# define ORD_KEYWORD(ord) (ord)->data->_keyword
# define ORD_LOCALE(ord) locale_array[(ord)->data->_lindex]->lang
# define ORD_STRING(ord) (ord)->data->_str
#else
# define ORD_KEYWORD(ord) (ord)->data._keyword
# define ORD_LOCALE(ord) locale_array[ord->data._lindex]->lang
# define ORD_STRING(ord) (ord)->data._str
#endif
typedef struct locale_data { typedef struct locale_data {
#ifdef SHARE_ORDERS #ifdef SHARE_ORDERS
@ -35,13 +45,16 @@ typedef struct locale_data {
static struct locale_data * locale_array[16]; static struct locale_data * locale_array[16];
static int nlocales = 0; static int nlocales = 0;
#ifdef SHARE_ORDERS
typedef struct order_data { typedef struct order_data {
char * _str; char * _str;
int _refcount : 16; int _refcount : 16;
int _lindex : 8; int _lindex : 8;
keyword_t _keyword; keyword_t _keyword;
} order_data; } order_data;
#endif
#ifdef SHARE_ORDERS
static void static void
release_data(order_data * data) release_data(order_data * data)
{ {
@ -52,13 +65,18 @@ release_data(order_data * data)
} }
} }
} }
#endif
void void
replace_order(order * dst, const order * src) replace_order(order * dst, const order * src)
{ {
#ifdef SHARE_ORDERS
release_data(dst->data); release_data(dst->data);
dst->data = src->data;
++src->data->_refcount; ++src->data->_refcount;
#else
if (dst->data._str) free(dst->data._str);
#endif
dst->data = src->data;
dst->_persistent = src->_persistent; dst->_persistent = src->_persistent;
} }
@ -68,7 +86,11 @@ get_keyword(const order * ord)
if (ord==NULL) { if (ord==NULL) {
return NOKEYWORD; return NOKEYWORD;
} }
#ifdef SHARE_ORDERS
return ord->data->_keyword; return ord->data->_keyword;
#else
return ord->data._keyword;
#endif
} }
char * char *
@ -76,22 +98,24 @@ getcommand(const order * ord)
{ {
char sbuffer[DISPLAYSIZE*2]; char sbuffer[DISPLAYSIZE*2];
char * str = sbuffer; char * str = sbuffer;
const char * text = ORD_STRING(ord);
assert(ord->data->_lindex<nlocales); #ifdef SHORT_STRINGS
keyword_t kwd = ORD_KEYWORD(ord);
#endif
if (ord->_persistent) *str++ = '@'; if (ord->_persistent) *str++ = '@';
#ifdef SHORT_STRINGS #ifdef SHORT_STRINGS
if (ord->data->_keyword!=NOKEYWORD) { if (kwd!=NOKEYWORD) {
const struct locale * lang = locale_array[ord->data->_lindex]->lang; const struct locale * lang = ORD_LOCALE(ord);
strcpy(str, LOC(lang, keywords[kwd]));
strcpy(str, LOC(lang, keywords[ord->data->_keyword]));
str += strlen(str); str += strlen(str);
if (ord->data->_str) { if (text) {
*str++ = ' '; *str++ = ' ';
*str = 0; *str = 0;
} }
} }
#endif #endif
if (ord->data->_str) strcpy(str, ord->data->_str); if (text) strcpy(str, text);
return strdup(sbuffer); return strdup(sbuffer);
} }
@ -100,7 +124,12 @@ free_order(order * ord)
{ {
if (ord!=NULL) { if (ord!=NULL) {
assert(ord->next==0); assert(ord->next==0);
#ifdef SHARE_ORDERS
release_data(ord->data); release_data(ord->data);
#else
if (ord->data._str) free(ord->data._str);
#endif
free(ord); free(ord);
} }
} }
@ -113,7 +142,9 @@ copy_order(order * src)
ord->next = NULL; ord->next = NULL;
ord->_persistent = src->_persistent; ord->_persistent = src->_persistent;
ord->data = src->data; ord->data = src->data;
#ifdef SHARE_ORDERS
++ord->data->_refcount; ++ord->data->_refcount;
#endif
return ord; return ord;
} }
return NULL; return NULL;
@ -138,11 +169,11 @@ free_orders(order ** olist)
} }
} }
#ifdef SHARE_ORDERS
static order_data * static order_data *
create_data(keyword_t kwd, const char * s, const char * sptr, int lindex) create_data(keyword_t kwd, const char * s, const char * sptr, int lindex)
{ {
order_data * data; order_data * data;
#ifdef SHARE_ORDERS
const struct locale * lang = locale_array[lindex]->lang; const struct locale * lang = locale_array[lindex]->lang;
if (kwd==K_STUDY) { if (kwd==K_STUDY) {
skill_t sk = findskill(parse_token(&sptr), lang); skill_t sk = findskill(parse_token(&sptr), lang);
@ -173,7 +204,6 @@ create_data(keyword_t kwd, const char * s, const char * sptr, int lindex)
++data->_refcount; ++data->_refcount;
return data; return data;
} }
#endif
data = (order_data*)malloc(sizeof(order_data)); data = (order_data*)malloc(sizeof(order_data));
data->_keyword = kwd; data->_keyword = kwd;
data->_lindex = lindex; data->_lindex = lindex;
@ -181,6 +211,7 @@ create_data(keyword_t kwd, const char * s, const char * sptr, int lindex)
data->_refcount = 1; data->_refcount = 1;
return data; return data;
} }
#endif
order * order *
parse_order(const char * s, const struct locale * lang) parse_order(const char * s, const struct locale * lang)
@ -229,13 +260,15 @@ parse_order(const char * s, const struct locale * lang)
ord->next = NULL; ord->next = NULL;
#ifdef SHORT_STRINGS #ifdef SHORT_STRINGS
if (kwd!=NOKEYWORD) { if (kwd!=NOKEYWORD) s = (*sptr)?sptr:NULL;
s = (*sptr)?sptr:NULL;
}
#else
ord->data->_str = strdup(s);
#endif #endif
#ifdef SHARE_ORDERS
ord->data = create_data(kwd, s, sptr, lindex); ord->data = create_data(kwd, s, sptr, lindex);
#else
ord->data._keyword = kwd;
ord->data._lindex = lindex;
ord->data._str = s?strdup(s):NULL;
#endif
return ord; return ord;
} }
@ -244,10 +277,11 @@ parse_order(const char * s, const struct locale * lang)
boolean boolean
is_repeated(const order * ord) is_repeated(const order * ord)
{ {
const struct locale * lang = locale_array[ord->data->_lindex]->lang; keyword_t kwd = ORD_KEYWORD(ord);
const struct locale * lang = ORD_LOCALE(ord);
param_t param; param_t param;
switch (ord->data->_keyword) { switch (kwd) {
case K_CAST: case K_CAST:
case K_BUY: case K_BUY:
case K_SELL: case K_SELL:
@ -263,12 +297,12 @@ is_repeated(const order * ord)
case K_SABOTAGE: case K_SABOTAGE:
case K_STUDY: case K_STUDY:
case K_TEACH: case K_TEACH:
case K_ZUECHTE: case K_BREED:
case K_PIRACY: case K_PIRACY:
return true; return true;
#if GROWING_TREES #if GROWING_TREES
case K_PFLANZE: case K_PLANT:
return true; return true;
#endif #endif
@ -304,10 +338,11 @@ is_repeated(const order * ord)
boolean boolean
is_exclusive(const order * ord) is_exclusive(const order * ord)
{ {
const struct locale * lang = locale_array[ord->data->_lindex]->lang; keyword_t kwd = ORD_KEYWORD(ord);
const struct locale * lang = ORD_LOCALE(ord);
param_t param; param_t param;
switch (ord->data->_keyword) { switch (kwd) {
case K_MOVE: case K_MOVE:
case K_WEREWOLF: case K_WEREWOLF:
/* these should not become persistent */ /* these should not become persistent */
@ -323,12 +358,12 @@ is_exclusive(const order * ord)
case K_SABOTAGE: case K_SABOTAGE:
case K_STUDY: case K_STUDY:
case K_TEACH: case K_TEACH:
case K_ZUECHTE: case K_BREED:
case K_PIRACY: case K_PIRACY:
return true; return true;
#if GROWING_TREES #if GROWING_TREES
case K_PFLANZE: case K_PLANT:
return true; return true;
#endif #endif
@ -364,8 +399,9 @@ is_exclusive(const order * ord)
boolean boolean
is_persistent(const order * ord) is_persistent(const order * ord)
{ {
keyword_t kwd = ORD_KEYWORD(ord);
boolean persist = ord->_persistent!=0; boolean persist = ord->_persistent!=0;
switch (ord->data->_keyword) { switch (kwd) {
case K_MOVE: case K_MOVE:
case K_WEREWOLF: case K_WEREWOLF:
case NOKEYWORD: case NOKEYWORD:
@ -383,7 +419,8 @@ is_persistent(const order * ord)
char * char *
write_order(const order * ord, const struct locale * lang, char * buffer, size_t size) write_order(const order * ord, const struct locale * lang, char * buffer, size_t size)
{ {
if (ord==0 || ord->data->_keyword==NOKEYWORD) { keyword_t kwd = ORD_KEYWORD(ord);
if (ord==0 || kwd==NOKEYWORD) {
buffer[0]=0; buffer[0]=0;
} else { } else {
char * s = getcommand(ord); char * s = getcommand(ord);

View File

@ -25,6 +25,9 @@ extern "C" {
* implemented yet) saving approx. 50% of all string-related memory. * implemented yet) saving approx. 50% of all string-related memory.
*/ */
#define SHARE_ORDERS
#ifdef SHARE_ORDERS
struct order_data; struct order_data;
typedef struct order { typedef struct order {
@ -33,6 +36,20 @@ typedef struct order {
struct order_data * data; struct order_data * data;
int _persistent : 1; int _persistent : 1;
} order; } order;
#else
typedef struct order_data {
char * _str;
int _lindex : 8;
keyword_t _keyword;
} order_data;
typedef struct order {
struct order * next;
/* do not access this data: */
struct order_data data;
int _persistent : 1;
} order;
#endif
/* constructor */ /* constructor */
extern struct order * parse_order(const char * s, const struct locale * lang); extern struct order * parse_order(const char * s, const struct locale * lang);