smallify order

This commit is contained in:
Enno Rehling 2017-09-30 19:22:24 +02:00
parent 86e99a560c
commit 54fdda85cd
6 changed files with 33 additions and 39 deletions

View File

@ -83,7 +83,7 @@ struct message *msg_feedback(const struct unit *u, struct order *ord,
variant var;
memset(args, 0, sizeof(args));
if (ord && ord->_noerror) {
if (ord && (ord->command & CMD_QUIET)) {
return NULL;
}

View File

@ -79,8 +79,8 @@ static void test_noerror(CuTest *tc) {
lang = test_create_locale();
u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL));
u->thisorder = parse_order("!@move", lang);
CuAssertTrue(tc, u->thisorder->_persistent);
CuAssertTrue(tc, u->thisorder->_noerror);
CuAssertIntEquals(tc, K_MOVE | CMD_QUIET | CMD_PERSIST, u->thisorder->command);
CuAssertTrue(tc, is_persistent(u->thisorder));
CuAssertPtrEquals(tc, NULL, msg_error(u, u->thisorder, 100));
CuAssertPtrEquals(tc, NULL, msg_feedback(u, u->thisorder, "error_unit_not_found", NULL));
test_cleanup();

View File

@ -94,7 +94,7 @@ char* get_command(const order *ord, char *sbuffer, size_t size) {
keyword_t kwd = ORD_KEYWORD(ord);
int bytes;
if (ord->_noerror) {
if (ord->command & CMD_QUIET) {
if (size > 0) {
*bufp++ = '!';
--size;
@ -103,7 +103,7 @@ char* get_command(const order *ord, char *sbuffer, size_t size) {
WARN_STATIC_BUFFER();
}
}
if (ord->_persistent) {
if (ord->command & CMD_PERSIST) {
if (size > 0) {
*bufp++ = '@';
--size;
@ -162,8 +162,7 @@ order *copy_order(const order * src)
if (src != NULL) {
order *ord = (order *)malloc(sizeof(order));
ord->next = NULL;
ord->_persistent = src->_persistent;
ord->_noerror = src->_noerror;
ord->command = src->command;
ord->data = src->data;
++ord->data->_refcount;
return ord;
@ -316,8 +315,9 @@ static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool p
}
locale_array[lindex]->lang = lang;
ord->_persistent = persistent;
ord->_noerror = noerror;
ord->command = (int)kwd;
if (persistent) ord->command |= CMD_PERSIST;
if (noerror) ord->command |= CMD_QUIET;
ord->next = NULL;
while (isspace(*(unsigned char *)sptr)) ++sptr;
@ -560,7 +560,7 @@ bool is_persistent(const order * ord)
case K_KOMMENTAR:
return true;
default:
return ord->_persistent || is_repeated(kwd);
return (ord->command & CMD_PERSIST) || is_repeated(kwd);
}
}

View File

@ -32,12 +32,15 @@ extern "C" {
struct order_data;
#define CMD_QUIET 0x010000
#define CMD_PERSIST 0x020000
#define CMD_DEFAULT 0x040000
typedef struct order {
struct order *next;
/* do not access this data: */
struct order_data *data;
bool _persistent;
bool _noerror;
int command;
} order;
/* constructor */

View File

@ -38,8 +38,7 @@ static void test_parse_order(CuTest *tc) {
ord = parse_order("MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord);
CuAssertTrue(tc, !ord->_noerror);
CuAssertTrue(tc, !ord->_persistent);
CuAssertIntEquals(tc, K_MOVE, ord->command);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertStrEquals(tc, "move NORTH", get_command(ord, cmd, sizeof(cmd)));
@ -51,40 +50,35 @@ static void test_parse_order(CuTest *tc) {
CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertTrue(tc, ord->_noerror);
CuAssertTrue(tc, !ord->_persistent);
CuAssertIntEquals(tc, K_MOVE | CMD_QUIET, ord->command);
free_order(ord);
ord = parse_order("@MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertTrue(tc, !ord->_noerror);
CuAssertTrue(tc, ord->_persistent);
CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST, ord->command);
free_order(ord);
ord = parse_order("!@MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command);
free_order(ord);
ord = parse_order("@!MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertTrue(tc, ord->_noerror);
CuAssertTrue(tc, ord->_persistent);
CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command);
free_order(ord);
ord = parse_order(" !@MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertTrue(tc, ord->_noerror);
CuAssertTrue(tc, ord->_persistent);
free_order(ord);
ord = parse_order(" !@MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord);
CuAssertPtrNotNull(tc, ord->data);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertTrue(tc, ord->_noerror);
CuAssertTrue(tc, ord->_persistent);
CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command);
free_order(ord);
test_cleanup();
@ -220,11 +214,11 @@ static void test_get_command(CuTest *tc) {
lang = test_create_locale();
ord = create_order(K_MAKE, lang, "iron");
CuAssertStrEquals(tc, "make iron", get_command(ord, buf, sizeof(buf)));
ord->_noerror = true;
ord->command |= CMD_QUIET;
CuAssertStrEquals(tc, "!make iron", get_command(ord, buf, sizeof(buf)));
ord->_persistent = true;
ord->command |= CMD_PERSIST;
CuAssertStrEquals(tc, "!@make iron", get_command(ord, buf, sizeof(buf)));
ord->_noerror = false;
ord->command = K_MAKE | CMD_PERSIST;
CuAssertStrEquals(tc, "@make iron", get_command(ord, buf, sizeof(buf)));
free_order(ord);
test_cleanup();

View File

@ -463,24 +463,21 @@ static void test_read_order(CuTest *tc) {
ord = read_order("MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord);
CuAssertTrue(tc, !ord->_noerror);
CuAssertTrue(tc, !ord->_persistent);
CuAssertIntEquals(tc, K_MOVE, ord->command);
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
CuAssertStrEquals(tc, "move NORTH", get_command(ord, cmd, sizeof(cmd)));
free_order(ord);
ord = read_order("MAKE TEMP foo", lang);
CuAssertPtrNotNull(tc, ord);
CuAssertTrue(tc, !ord->_noerror);
CuAssertTrue(tc, !ord->_persistent);
CuAssertIntEquals(tc, K_MAKETEMP, ord->command);
CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord));
CuAssertStrEquals(tc, "maketemp foo", get_command(ord, cmd, sizeof(cmd)));
free_order(ord);
ord = read_order("MAKETEMP foo", lang);
CuAssertPtrNotNull(tc, ord);
CuAssertTrue(tc, !ord->_noerror);
CuAssertTrue(tc, !ord->_persistent);
CuAssertIntEquals(tc, K_MAKETEMP, ord->command);
CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord));
CuAssertStrEquals(tc, "maketemp foo", get_command(ord, cmd, sizeof(cmd)));
free_order(ord);