Revert "refactor: extract a read_order function."

This reverts commit 8b39133dbf.
This commit is contained in:
Enno Rehling 2017-10-07 18:06:54 +02:00
parent b255af52e2
commit ecce8e5d91
3 changed files with 43 additions and 89 deletions

View File

@ -123,42 +123,6 @@ char *rns(FILE * f, char *c, size_t size)
return c; return c;
} }
struct order *read_order(const char *in, const struct locale *lang) {
assert(in);
assert(lang);
if (in[0]) {
const char *s = in;
keyword_t kwd;
char token[64];
const char *stok;
stok = parse_token(&s, token, sizeof(token));
if (stok) {
param_t param = findparam(token, lang);
switch (param) {
case P_UNIT:
case P_REGION:
return NULL;
case P_FACTION:
case P_NEXT:
case P_GAMENAME:
/* these terminate the orders, so we apply extra checking */
if (strlen(stok) >= 3) {
return NULL;
}
break;
default:
break;
}
}
/* Nun wird der Befehl erzeut und eingehängt */
kwd = get_keyword(stok, lang);
if (kwd != NOKEYWORD) {
return parse_order(in, lang);
}
}
return NULL;
}
static unit *unitorders(FILE * F, int enc, struct faction *f) static unit *unitorders(FILE * F, int enc, struct faction *f)
{ {
@ -201,7 +165,6 @@ static unit *unitorders(FILE * F, int enc, struct faction *f)
for (;;) { for (;;) {
const char *s; const char *s;
order * ord;
/* Erst wenn wir sicher sind, dass kein Befehl /* Erst wenn wir sicher sind, dass kein Befehl
* eingegeben wurde, checken wir, ob nun eine neue * eingegeben wurde, checken wir, ob nun eine neue
* Einheit oder ein neuer Spieler drankommt */ * Einheit oder ein neuer Spieler drankommt */
@ -210,13 +173,49 @@ static unit *unitorders(FILE * F, int enc, struct faction *f)
if (s == NULL) if (s == NULL)
break; break;
ord = read_order(s, f->locale); if (s[0]) {
if (!ord) { if (s[0] != '@') {
ADDMSG(&f->msgs, msg_message("parse_error", "unit command", u, s)); char token[64];
break; const char *stok = s;
stok = parse_token(&stok, token, sizeof(token));
if (stok) {
bool quit = false;
param_t param = findparam(stok, u->faction->locale);
switch (param) {
case P_UNIT:
case P_REGION:
quit = true;
break;
case P_FACTION:
case P_NEXT:
case P_GAMENAME:
/* these terminate the orders, so we apply extra checking */
if (strlen(stok) >= 3) {
quit = true;
break;
}
else {
quit = false;
}
break;
default:
break;
}
if (quit) {
break;
}
}
}
/* Nun wird der Befehl erzeut und eingehängt */
*ordp = parse_order(s, u->faction->locale);
if (*ordp) {
ordp = &(*ordp)->next;
}
else {
ADDMSG(&f->msgs, msg_message("parse_error", "unit command", u, s));
}
} }
*ordp = ord;
ordp = &(*ordp)->next;
} }
} }
@ -306,7 +305,7 @@ int readorders(const char *filename)
} }
init_tokens_str(b); init_tokens_str(b);
s = gettoken(token, sizeof(token)); s = gettoken(token, sizeof(token));
p = (s && s[0] != '@' && s[0] != '!') ? findparam(s, lang) : NOPARAM; p = (s && s[0] != '@') ? findparam(s, lang) : NOPARAM;
} while ((p != P_UNIT || !f) && p != P_FACTION && p != P_NEXT } while ((p != P_UNIT || !f) && p != P_FACTION && p != P_NEXT
&& p != P_GAMENAME); && p != P_GAMENAME);
} }

View File

@ -31,8 +31,6 @@ extern "C" {
struct spellbook; struct spellbook;
struct unit; struct unit;
struct building; struct building;
struct order;
struct locale;
struct faction; struct faction;
struct region; struct region;
struct ship; struct ship;
@ -45,8 +43,6 @@ extern "C" {
/* TODO: is this *really* still in use? */ /* TODO: is this *really* still in use? */
extern int enc_gamedata; extern int enc_gamedata;
struct order *read_order(const char *s, const struct locale *lang);
int readorders(const char *filename); int readorders(const char *filename);
int readgame(const char *filename); int readgame(const char *filename);
int writegame(const char *filename); int writegame(const char *filename);

View File

@ -5,7 +5,6 @@
#include <attributes/key.h> #include <attributes/key.h>
#include "save.h" #include "save.h"
#include "order.h"
#include "version.h" #include "version.h"
#include "building.h" #include "building.h"
#include "ship.h" #include "ship.h"
@ -453,45 +452,6 @@ static void test_version_no(CuTest *tc) {
CuAssertIntEquals(tc, 0x10203, version_no("1.2.3-what.is.42")); CuAssertIntEquals(tc, 0x10203, version_no("1.2.3-what.is.42"));
} }
static void test_read_order(CuTest *tc) {
char cmd[32];
order *ord;
struct locale * lang;
test_setup();
lang = test_create_locale();
ord = read_order("MOVE NORTH", lang);
CuAssertPtrNotNull(tc, ord);
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);
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);
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);
CuAssertPtrEquals(tc, NULL, read_order("HODOR HODOR HODOR", lang));
CuAssertPtrEquals(tc, NULL, read_order("FACTION abcd", lang));
CuAssertPtrEquals(tc, NULL, read_order("UNIT abcd", lang));
CuAssertPtrEquals(tc, NULL, read_order("ERESSEA abcd", lang));
CuAssertPtrEquals(tc, NULL, read_order("REGION 2,3", lang));
CuAssertPtrEquals(tc, NULL, read_order("NEXT", lang));
test_cleanup();
}
CuSuite *get_save_suite(void) CuSuite *get_save_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -508,7 +468,6 @@ CuSuite *get_save_suite(void)
SUITE_ADD_TEST(suite, test_readwrite_dead_faction_group); SUITE_ADD_TEST(suite, test_readwrite_dead_faction_group);
SUITE_ADD_TEST(suite, test_read_password); SUITE_ADD_TEST(suite, test_read_password);
SUITE_ADD_TEST(suite, test_read_password_external); SUITE_ADD_TEST(suite, test_read_password_external);
SUITE_ADD_TEST(suite, test_read_order);
SUITE_ADD_TEST(suite, test_version_no); SUITE_ADD_TEST(suite, test_version_no);
return suite; return suite;