diff --git a/src/kernel/config.c b/src/kernel/config.c index 6d7f778a4..65623235c 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -794,20 +794,6 @@ parse(keyword_t kword, int(*dofun) (unit *, struct order *), bool thisorder) } } -unsigned int getuint(void) -{ - char token[16]; - const char *s = gettoken(token, sizeof(token)); - return s ? atoip(s) : 0; -} - -int getint(void) -{ - char token[16]; - const char * s = gettoken(token, sizeof(token)); - return s ? atoi(s) : 0; -} - const struct race *findrace(const char *s, const struct locale *lang) { void **tokens = get_translations(lang, UT_RACES); diff --git a/src/kernel/config.h b/src/kernel/config.h index 037c2b968..67355aae7 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -109,8 +109,6 @@ extern "C" { void make_undead_unit(struct unit *); unsigned int atoip(const char *s); - unsigned int getuint(void); - int getint(void); param_t findparam(const char *s, const struct locale *lang); param_t findparam_ex(const char *s, const struct locale * lang); diff --git a/src/study.c b/src/study.c index 173bc9d08..04f9b9a30 100644 --- a/src/study.c +++ b/src/study.c @@ -58,7 +58,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. static skill_t getskill(const struct locale *lang) { - const char * s = getstrtoken(); + char token[128]; + const char * s = gettoken(token, sizeof(token)); return s ? get_skill(s, lang) : NOSKILL; } diff --git a/src/util/parser.c b/src/util/parser.c index da2f3f521..3e3dbb374 100644 --- a/src/util/parser.c +++ b/src/util/parser.c @@ -217,6 +217,19 @@ const char *gettoken(char *lbuf, size_t bufsize) return parse_token((const char **)&states->current_token, lbuf, bufsize); } +int getint(void) +{ + char token[16]; + const char * s = gettoken(token, sizeof(token)); + return s ? atoi(s) : 0; +} + +unsigned int getuint(void) +{ + int n = getint(); + return (n < 0) ? 0 : n; +} + int getid(void) { char token[16]; diff --git a/src/util/parser.h b/src/util/parser.h index ab62591a9..f1e01393c 100644 --- a/src/util/parser.h +++ b/src/util/parser.h @@ -23,6 +23,8 @@ extern "C" { bool parser_end(void); const char *getstrtoken(void); const char *gettoken(char *lbuf, size_t bufsize); + unsigned int getuint(void); + int getint(void); int getid(void); #define getshipid() getid() #define getfactionid() getid() diff --git a/src/util/parser.test.c b/src/util/parser.test.c index 910c11fc3..961761188 100644 --- a/src/util/parser.test.c +++ b/src/util/parser.test.c @@ -15,9 +15,38 @@ static void test_gettoken(CuTest *tc) { CuAssertStrEquals(tc, "", token); } +static void test_skip_token(CuTest *tc) { + char token[128]; + init_tokens_str("HELP ONE TWO THREE"); + skip_token(); + CuAssertStrEquals(tc, "ONE", gettoken(token, sizeof(token))); +} + +static void test_getintegers(CuTest *tc) { + init_tokens_str("ii 666 666 -42 -42"); + CuAssertIntEquals(tc, 666, getid()); + CuAssertIntEquals(tc, 666, getint()); + CuAssertIntEquals(tc, 666, getuint()); + CuAssertIntEquals(tc, -42, getint()); + CuAssertIntEquals(tc, 0, getuint()); + CuAssertIntEquals(tc, 0, getint()); +} + +static void test_getstrtoken(CuTest *tc) { + init_tokens_str("HELP ONE TWO THREE"); + CuAssertStrEquals(tc, "HELP", getstrtoken()); + CuAssertStrEquals(tc, "ONE", getstrtoken()); + CuAssertStrEquals(tc, "TWO", getstrtoken()); + CuAssertStrEquals(tc, "THREE", getstrtoken()); + CuAssertPtrEquals(tc, NULL, (void *)getstrtoken()); +} + CuSuite *get_parser_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_skip_token); SUITE_ADD_TEST(suite, test_gettoken); + SUITE_ADD_TEST(suite, test_getintegers); + SUITE_ADD_TEST(suite, test_getstrtoken); return suite; }