move atoip out of config (small helper) and test it.

remove some unnecessary variables from laws.c
This commit is contained in:
Enno Rehling 2014-12-23 09:23:37 +01:00
parent 5ebf05f625
commit b27491eccd
6 changed files with 24 additions and 26 deletions

View file

@ -587,19 +587,6 @@ int change_hitpoints(unit * u, int value)
return hp; return hp;
} }
unsigned int atoip(const char *s)
{
int n;
assert(s);
n = atoi(s);
if (n < 0)
n = 0;
return n;
}
bool unit_has_cursed_item(const unit * u) bool unit_has_cursed_item(const unit * u)
{ {
item *itm = u->items; item *itm = u->items;

View file

@ -108,8 +108,6 @@ extern "C" {
/* special units */ /* special units */
void make_undead_unit(struct unit *); void make_undead_unit(struct unit *);
unsigned int atoip(const char *s);
param_t findparam(const char *s, const struct locale *lang); param_t findparam(const char *s, const struct locale *lang);
param_t findparam_ex(const char *s, const struct locale * lang); param_t findparam_ex(const char *s, const struct locale * lang);
bool isparam(const char *s, const struct locale * lang, param_t param); bool isparam(const char *s, const struct locale * lang, param_t param);

View file

@ -2017,19 +2017,15 @@ int mail_cmd(unit * u, struct order *ord)
} }
case P_FACTION: case P_FACTION:
{
bool see = false;
n = getfactionid(); n = getfactionid();
for (u2 = r->units; u2; u2 = u2->next) { for (u2 = r->units; u2; u2 = u2->next) {
if (u2->faction->no == n && seefaction(u->faction, r, u2, 0)) { if (u2->faction->no == n && seefaction(u->faction, r, u2, 0)) {
see = true;
break; break;
} }
} }
if (!see) { if (!u2) {
cmistake(u, ord, 66, MSG_MESSAGE); cmistake(u, ord, 66, MSG_MESSAGE);
break; break;
} }
@ -2041,21 +2037,17 @@ int mail_cmd(unit * u, struct order *ord)
} }
mailfaction(u, n, ord, s); mailfaction(u, n, ord, s);
return 0; return 0;
}
case P_UNIT: case P_UNIT:
{
bool see = false;
n = getid(); n = getid();
for (u2 = r->units; u2; u2 = u2->next) { for (u2 = r->units; u2; u2 = u2->next) {
if (u2->no == n && cansee(u->faction, r, u2, 0)) { if (u2->no == n && cansee(u->faction, r, u2, 0)) {
see = true;
break; break;
} }
} }
if (!see) { if (!u2) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, ADDMSG(&u->faction->msgs, msg_feedback(u, ord,
"feedback_unit_not_found", "")); "feedback_unit_not_found", ""));
return 0; return 0;
@ -2081,7 +2073,6 @@ int mail_cmd(unit * u, struct order *ord)
mailunit(r, u, n, ord, s); mailunit(r, u, n, ord, s);
} }
return 0; return 0;
}
case P_BUILDING: case P_BUILDING:
case P_GEBAEUDE: case P_GEBAEUDE:

View file

@ -240,3 +240,16 @@ int getid(void)
} }
return i; return i;
} }
unsigned int atoip(const char *s)
{
int n;
assert(s);
n = atoi(s);
if (n < 0)
n = 0;
return n;
}

View file

@ -26,6 +26,8 @@ extern "C" {
unsigned int getuint(void); unsigned int getuint(void);
int getint(void); int getint(void);
int getid(void); int getid(void);
unsigned int atoip(const char *s);
#define getshipid() getid() #define getshipid() getid()
#define getfactionid() getid() #define getfactionid() getid()

View file

@ -41,9 +41,16 @@ static void test_getstrtoken(CuTest *tc) {
CuAssertPtrEquals(tc, NULL, (void *)getstrtoken()); CuAssertPtrEquals(tc, NULL, (void *)getstrtoken());
} }
static void test_atoip(CuTest *tc) {
CuAssertIntEquals(tc, 42, atoip("42"));
CuAssertIntEquals(tc, 0, atoip("-42"));
CuAssertIntEquals(tc, 0, atoip("NOPE"));
}
CuSuite *get_parser_suite(void) CuSuite *get_parser_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_atoip);
SUITE_ADD_TEST(suite, test_skip_token); SUITE_ADD_TEST(suite, test_skip_token);
SUITE_ADD_TEST(suite, test_gettoken); SUITE_ADD_TEST(suite, test_gettoken);
SUITE_ADD_TEST(suite, test_getintegers); SUITE_ADD_TEST(suite, test_getintegers);