From 0bbee5d1ac1d8aaa25c39804e3620fb461d93aac Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 2 Mar 2020 21:05:49 +0100 Subject: [PATCH 1/3] kill read_orders, rewrite tests to use parseorders. --- src/orderfile.c | 77 ------------------------------------------- src/orderfile.h | 1 - src/orderfile.test.c | 78 +++++++++++--------------------------------- 3 files changed, 19 insertions(+), 137 deletions(-) diff --git a/src/orderfile.c b/src/orderfile.c index 12cb05834..ec3852e4b 100644 --- a/src/orderfile.c +++ b/src/orderfile.c @@ -147,83 +147,6 @@ static faction *factionorders(void) return f; } -int read_orders(input *in) -{ - const char *b; - int nfactions = 0; - struct faction *f = NULL; - const struct locale *lang = default_locale; - - /* TODO: recognize UTF8 BOM */ - b = in->getbuf(in->data); - - /* Auffinden der ersten Partei, und danach abarbeiten bis zur letzten - * Partei */ - - while (b) { - char token[128]; - param_t p; - const char *s; - init_tokens_str(b); - s = gettoken(token, sizeof(token)); - p = findparam_block(s, lang, true); - switch (p) { - case P_GAMENAME: - case P_FACTION: - f = factionorders(); - if (f) { - lang = f->locale; - ++nfactions; - } - - b = in->getbuf(in->data); - break; - - /* in factionorders wird nur eine zeile gelesen: - * diejenige mit dem passwort. Die befehle der units - * werden geloescht, und die Partei wird als aktiv - * vermerkt. */ - - case P_UNIT: - if (f) { - unitorders(in, f); - do { - b = in->getbuf(in->data); - if (!b) { - break; - } - init_tokens_str(b); - s = gettoken(token, sizeof(token)); - p = (s && s[0] != '@') ? findparam(s, lang) : NOPARAM; - } while ((p != P_UNIT || !f) && p != P_FACTION && p != P_NEXT - && p != P_GAMENAME); - } - break; - - /* Falls in unitorders() abgebrochen wird, steht dort entweder eine neue - * Partei, eine neue Einheit oder das File-Ende. Das switch() wird erneut - * durchlaufen, und die entsprechende Funktion aufgerufen. Man darf buf - * auf alle Faelle nicht ueberschreiben! Bei allen anderen Eintraegen hier - * muss buf erneut gefaellt werden, da die betreffende Information in nur - * einer Zeile steht, und nun die naechste gelesen werden muss. - */ - - case P_NEXT: - f = NULL; - lang = default_locale; - b = in->getbuf(in->data); - break; - - default: - b = in->getbuf(in->data); - break; - } - } - - log_info("done reading orders for %d factions", nfactions); - return 0; -} - typedef struct parser_state { unit *u; faction *f; diff --git a/src/orderfile.h b/src/orderfile.h index f7934556e..8803ae717 100644 --- a/src/orderfile.h +++ b/src/orderfile.h @@ -12,7 +12,6 @@ extern "C" { void *data; } input; - int read_orders(struct input *in); int parseorders(FILE *F); #ifdef __cplusplus diff --git a/src/orderfile.test.c b/src/orderfile.test.c index 204e20d5d..8088f3f65 100644 --- a/src/orderfile.test.c +++ b/src/orderfile.test.c @@ -21,63 +21,30 @@ #include #include -static const char *getbuf_null(void *data) -{ - return NULL; -} - -static void test_read_orders(CuTest *tc) { - input in; - test_setup(); - in.getbuf = getbuf_null; - in.data = NULL; - CuAssertIntEquals(tc, 0, read_orders(&in)); - test_teardown(); -} - -static const char *getbuf_strings(void *data) -{ - strlist **listp = (strlist **)data; - if (listp && *listp) { - strlist *list = *listp; - *listp = list->next; - return list->s; - } - return NULL; -} - static void test_unit_orders(CuTest *tc) { - input in; unit *u; faction *f; - strlist *orders = NULL, *backup; - char buf[64]; + FILE *F = tmpfile(); test_setup(); u = test_create_unit(f = test_create_faction(NULL), test_create_plain(0, 0)); f->locale = test_create_locale(); u->orders = create_order(K_ENTERTAIN, f->locale, NULL); faction_setpassword(f, password_hash("password", PASSWORD_DEFAULT)); - snprintf(buf, sizeof(buf), "%s %s %s", + fprintf(F, "%s %s %s\n", LOC(f->locale, parameters[P_FACTION]), itoa36(f->no), "password"); - addstrlist(&orders, buf); - snprintf(buf, sizeof(buf), "%s %s", + fprintf(F, "%s %s\n", LOC(f->locale, parameters[P_UNIT]), itoa36(u->no)); - addstrlist(&orders, buf); - snprintf(buf, sizeof(buf), "%s %s", keyword_name(K_MOVE, f->locale), + fprintf(F, "%s %s\n", keyword_name(K_MOVE, f->locale), LOC(f->locale, shortdirections[D_WEST])); - backup = orders; - addstrlist(&orders, buf); - in.data = &orders; - in.getbuf = getbuf_strings; - CuAssertIntEquals(tc, 0, read_orders(&in)); + rewind(F); + CuAssertIntEquals(tc, 0, parseorders(F)); CuAssertPtrNotNull(tc, u->old_orders); CuAssertPtrNotNull(tc, u->orders); - CuAssertPtrEquals(tc, NULL, orders); CuAssertIntEquals(tc, K_MOVE, getkeyword(u->orders)); CuAssertIntEquals(tc, K_ENTERTAIN, getkeyword(u->old_orders)); CuAssertIntEquals(tc, UFL_ORDERS, u->flags & UFL_ORDERS); - freestrlist(backup); + fclose(F); test_teardown(); } @@ -93,10 +60,8 @@ static const char *getbuf_list(void *data) } static void test_faction_password_okay(CuTest *tc) { - input in; faction *f; - order_list olist; - const char *orders[] = { "ERESSEA 1 password", NULL }; + FILE *F; test_setup(); f = test_create_faction(NULL); @@ -104,21 +69,18 @@ static void test_faction_password_okay(CuTest *tc) { CuAssertIntEquals(tc, 1, f->no); faction_setpassword(f, "password"); f->lastorders = turn - 1; - olist.orders = orders; - olist.next = 0; - in.getbuf = getbuf_list; - in.data = &olist; - CuAssertIntEquals(tc, 0, read_orders(&in)); - CuAssertIntEquals(tc, 2, olist.next); + F = tmpfile(); + fprintf(F, "ERESSEA 1 password\n"); + rewind(F); + CuAssertIntEquals(tc, 0, parseorders(F)); CuAssertIntEquals(tc, turn, f->lastorders); + fclose(F); test_teardown(); } static void test_faction_password_bad(CuTest *tc) { - input in; faction *f; - order_list olist; - const char *orders[] = { "ERESSEA 1 password", NULL }; + FILE *F; test_setup(); mt_create_va(mt_new("wrongpasswd", NULL), "password:string", MT_NEW_END); @@ -128,20 +90,18 @@ static void test_faction_password_bad(CuTest *tc) { CuAssertIntEquals(tc, 1, f->no); faction_setpassword(f, "patzword"); f->lastorders = turn - 1; - olist.orders = orders; - olist.next = 0; - in.getbuf = getbuf_list; - in.data = &olist; - CuAssertIntEquals(tc, 0, read_orders(&in)); - CuAssertIntEquals(tc, 2, olist.next); + F = tmpfile(); + fprintf(F, "ERESSEA 1 password\n"); + rewind(F); + CuAssertIntEquals(tc, 0, parseorders(F)); CuAssertIntEquals(tc, turn - 1, f->lastorders); + fclose(F); test_teardown(); } CuSuite *get_orderfile_suite(void) { CuSuite *suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, test_read_orders); SUITE_ADD_TEST(suite, test_unit_orders); SUITE_ADD_TEST(suite, test_faction_password_okay); SUITE_ADD_TEST(suite, test_faction_password_bad); From fe39eb6cf7e6f9fe8f15f989a4d0cc50e41ead4d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 2 Mar 2020 21:19:02 +0100 Subject: [PATCH 2/3] remove unused local functions. --- src/orderfile.c | 99 ------------------------------------------------- 1 file changed, 99 deletions(-) diff --git a/src/orderfile.c b/src/orderfile.c index ec3852e4b..eaa6fbb53 100644 --- a/src/orderfile.c +++ b/src/orderfile.c @@ -48,105 +48,6 @@ static void begin_orders(unit *u) { u->orders = NULL; } -static void unitorders(input *in, faction *f) -{ - int i; - unit *u; - - assert(f); - - i = getid(); - u = findunit(i); - - if (u && u->faction == f) { - order **ordp; - - begin_orders(u); - ordp = &u->orders; - - for (;;) { - const char *s; - /* Erst wenn wir sicher sind, dass kein Befehl - * eingegeben wurde, checken wir, ob nun eine neue - * Einheit oder ein neuer Spieler drankommt */ - - s = in->getbuf(in->data); - if (s == NULL) - break; - - if (s[0]) { - if (s[0] != '@') { - char token[64]; - 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 eingehaengt */ - *ordp = parse_order(s, u->faction->locale); - if (*ordp) { - ordp = &(*ordp)->next; - } - else { - ADDMSG(&f->msgs, msg_message("parse_error", "unit command", u, s)); - } - } - } - - } -} - -static faction *factionorders(void) -{ - int fid = getid(); - faction *f = findfaction(fid); - - if (f != NULL && (f->flags & FFL_NPC) == 0) { - char token[PASSWORD_MAXSIZE]; - const char *pass = gettoken(token, sizeof(token)); - - if (!checkpasswd(f, (const char *)pass)) { - log_debug("Invalid password for faction %s", itoa36(fid)); - ADDMSG(&f->msgs, msg_message("wrongpasswd", "password", pass)); - return 0; - } - /* Die Partei hat sich zumindest gemeldet, so dass sie noch - * nicht als untaetig gilt */ - f->lastorders = turn; - - } - else { - log_debug("orders for invalid faction %s", itoa36(fid)); - } - return f; -} - typedef struct parser_state { unit *u; faction *f; From b8205893d218c1be99a50e0aad0e6c063dfc78f1 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 2 Mar 2020 21:26:21 +0100 Subject: [PATCH 3/3] more unused code removed --- src/orderfile.test.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/orderfile.test.c b/src/orderfile.test.c index 8088f3f65..39653391b 100644 --- a/src/orderfile.test.c +++ b/src/orderfile.test.c @@ -48,17 +48,6 @@ static void test_unit_orders(CuTest *tc) { test_teardown(); } -typedef struct order_list { - const char **orders; - int next; -} order_list; - -static const char *getbuf_list(void *data) -{ - order_list * olist = (order_list *)data; - return olist->orders[olist->next++]; -} - static void test_faction_password_okay(CuTest *tc) { faction *f; FILE *F;