From b551edbdb3d6c1bca41f4b3d531b500cc9822d3c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 9 Dec 2014 05:55:16 +0100 Subject: [PATCH] refactoring: moving unitname to unit.c. also removing static-string some cruft from config.c. --- src/kernel/command.c | 1 + src/kernel/config.c | 44 +------------------------------------------- src/kernel/config.h | 8 ++------ src/kernel/unit.c | 18 ++++++++++++++++++ src/kernel/unit.h | 3 +++ src/vortex.c | 2 +- 6 files changed, 26 insertions(+), 50 deletions(-) diff --git a/src/kernel/command.c b/src/kernel/command.c index 284064621..c6d262dd0 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -15,6 +15,7 @@ #include "command.h" #include +#include #include #include diff --git a/src/kernel/config.c b/src/kernel/config.c index b2889af8a..a6c631573 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1097,28 +1097,7 @@ typedef char name[OBJECTIDSIZE + 1]; static name idbuf[8]; static int nextbuf = 0; -char *estring_i(char *ibuf) -{ - char *p = ibuf; - - while (*p) { - if (isxspace(*(unsigned *)p) == ' ') { - *p = '~'; - } - ++p; - } - return ibuf; -} - -char *estring(const char *s) -{ - char *ibuf = idbuf[(++nextbuf) % 8]; - - strlcpy(ibuf, s, sizeof(name)); - return estring_i(ibuf); -} - -char *cstring_i(char *ibuf) +char *untilde(char *ibuf) { char *p = ibuf; @@ -1131,14 +1110,6 @@ char *cstring_i(char *ibuf) return ibuf; } -char *cstring(const char *s) -{ - char *ibuf = idbuf[(++nextbuf) % 8]; - - strlcpy(ibuf, s, sizeof(name)); - return cstring_i(ibuf); -} - building *largestbuilding(const region * r, cmp_building_cb cmp_gt, bool imaginary) { @@ -1157,19 +1128,6 @@ building *largestbuilding(const region * r, cmp_building_cb cmp_gt, return best; } -char *write_unitname(const unit * u, char *buffer, size_t size) -{ - slprintf(buffer, size, "%s (%s)", (const char *)u->name, itoa36(u->no)); - buffer[size - 1] = 0; - return buffer; -} - -const char *unitname(const unit * u) -{ - char *ubuf = idbuf[(++nextbuf) % 8]; - return write_unitname(u, ubuf, sizeof(name)); -} - /* -- Erschaffung neuer Einheiten ------------------------------ */ extern faction *dfindhash(int i); diff --git a/src/kernel/config.h b/src/kernel/config.h index 08afc5c22..cf0429993 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -34,6 +34,7 @@ extern "C" { #define INSECT_POTION /* Spezialtrank für Insekten */ #define ORCIFICATION /* giving snotlings to the peasants gets counted */ +// TODO: remove macro, move all alliance code into a module. #define ALLIED(f1, f2) (f1==f2 || (f1->alliance && f1->alliance==f2->alliance)) /* for some good prime numbers, check http://www.math.niu.edu/~rusin/known-math/98/pi_x */ @@ -179,12 +180,7 @@ extern "C" { struct faction *getfaction(void); - char *estring(const char *s); - char *estring_i(char *s); - char *cstring(const char *s); - char *cstring_i(char *s); - const char *unitname(const struct unit *u); - char *write_unitname(const struct unit *u, char *buffer, size_t size); + char *untilde(char *s); typedef int(*cmp_building_cb) (const struct building * b, const struct building * a); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 042002a48..6decd4a8b 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1843,3 +1843,21 @@ void remove_empty_units(void) remove_empty_units_in_region(r); } } + +typedef char name[OBJECTIDSIZE + 1]; +static name idbuf[8]; +static int nextbuf = 0; + +char *write_unitname(const unit * u, char *buffer, size_t size) +{ + slprintf(buffer, size, "%s (%s)", (const char *)u->name, itoa36(u->no)); + buffer[size - 1] = 0; + return buffer; +} + +const char *unitname(const unit * u) +{ + char *ubuf = idbuf[(++nextbuf) % 8]; + return write_unitname(u, ubuf, sizeof(name)); +} + diff --git a/src/kernel/unit.h b/src/kernel/unit.h index f091477dc..856e1ffe2 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -247,6 +247,9 @@ extern "C" { struct unit *findunitr(const struct region *r, int n); + const char *unitname(const struct unit *u); + char *write_unitname(const struct unit *u, char *buffer, size_t size); + #ifdef __cplusplus } #endif diff --git a/src/vortex.c b/src/vortex.c index a0791907d..f9993a561 100644 --- a/src/vortex.c +++ b/src/vortex.c @@ -86,7 +86,7 @@ static int a_readdirection(attrib * a, void *owner, struct storage *store) READ_TOK(store, NULL, 0); READ_TOK(store, lbuf, sizeof(lbuf)); - cstring_i(lbuf); + untilde(lbuf); for (; dl; dl = dl->next) { if (strcmp(lbuf, dl->oldname) == 0) { d->keyword = _strdup(dl->name);