fix get_command for K_STUDY (still bug 2427).

This commit is contained in:
Enno Rehling 2018-03-16 21:49:24 +01:00
parent db5d9e069e
commit 121bf343ea
2 changed files with 72 additions and 39 deletions

View file

@ -67,8 +67,6 @@ keyword_t getkeyword(const order * ord)
* keywords are expanded to their full length.
*/
char* get_command(const order *ord, const struct locale *lang, char *sbuffer, size_t size) {
order_data *od = NULL;
const char * text;
keyword_t kwd = ORD_KEYWORD(ord);
sbstring sbs;
@ -80,28 +78,35 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si
sbs_strcat(&sbs, "@");
}
if (ord->id < 0) {
skill_t sk = (skill_t)(100+ord->id);
assert(kwd == K_STUDY && sk != SK_MAGIC && sk < MAXSKILLS);
text = skillname(sk, lang);
} else {
od = odata_load(ord->id);
text = OD_STRING(od);
}
if (kwd != NOKEYWORD) {
const char *str = (const char *)LOC(lang, keyword(kwd));
assert(str);
sbs_strcat(&sbs, str);
if (text) {
if (ord->id < 0) {
skill_t sk = (skill_t)(100+ord->id);
assert(kwd == K_STUDY && sk != SK_MAGIC && sk < MAXSKILLS);
str = skillname(sk, lang);
if (str) {
if (strchr(str, ' ') == NULL) {
sbs_strcat(&sbs, " ");
sbs_strcat(&sbs, str);
}
else {
sbs_strcat(&sbs, " '");
sbs_strcat(&sbs, str);
sbs_strcat(&sbs, "'");
}
}
if (text) {
sbs_strcat(&sbs, text);
} else {
order_data *od = odata_load(ord->id);
str = OD_STRING(od);
if (str) {
sbs_strcat(&sbs, " ");
sbs_strcat(&sbs, str);
}
if (od) {
odata_release(od);
}
}
return sbuffer;
}
@ -118,21 +123,30 @@ int stream_order(struct stream *out, const struct order *ord, const struct local
swrite("@", 1, 1, out);
}
if (ord->id < 0) {
skill_t sk = (skill_t)(100 + ord->id);
assert(kwd == K_STUDY && sk != SK_MAGIC && sk < MAXSKILLS);
text = skillname(sk, lang);
}
else {
od = odata_load(ord->id);
text = OD_STRING(od);
}
if (kwd != NOKEYWORD) {
const char *str = (const char *)LOC(lang, keyword(kwd));
assert(str);
swrite(str, 1, strlen(str), out);
}
if (ord->id < 0) {
skill_t sk = (skill_t)(100 + ord->id);
assert(kwd == K_STUDY && sk != SK_MAGIC && sk < MAXSKILLS);
text = skillname(sk, lang);
if (strchr(text, ' ') != NULL) {
swrite(" '", 1, 2, out);
swrite(text, 1, strlen(text), out);
swrite("'", 1, 1, out);
}
else {
swrite(" ", 1, 1, out);
swrite(text, 1, strlen(text), out);
}
}
else {
od = odata_load(ord->id);
text = OD_STRING(od);
if (text) {
char obuf[1024];
swrite(" ", 1, 1, out);
@ -141,7 +155,6 @@ int stream_order(struct stream *out, const struct order *ord, const struct local
}
swrite(text, 1, strlen(text), out);
}
if (od) {
odata_release(od);
}
@ -514,11 +527,27 @@ keyword_t init_order(const struct order *ord, const struct locale *lang)
parser_od = NULL;
}
if (ord->id < 0) {
const char *str;
skill_t sk = (skill_t)(100 + ord->id);
assert(sk < MAXSKILLS);
assert(lang);
assert(kwd == K_STUDY);
init_tokens_str(skillname(sk, lang));
str = skillname(sk, lang);
if (strchr(str, ' ') == NULL) {
init_tokens_str(str);
}
else {
char token[32], *dup;
size_t len = strlen(str);
assert(len + 3 < sizeof(token));
token[0] = '\'';
memcpy(token + 1, str, len);
token[len + 1] = '\'';
token[len + 2] = '\0';
dup = str_strdup(token);
init_tokens_ex(dup, dup, free);
}
}
else {
const char *str;

View file

@ -335,7 +335,7 @@ static void test_study_orders(CuTest *tc) {
test_teardown();
}
static void test_stream_order(CuTest *tc) {
static void test_study_order(CuTest *tc) {
char token[32];
stream out;
unit *u;
@ -352,6 +352,8 @@ static void test_stream_order(CuTest *tc) {
CuAssertIntEquals(tc, K_STUDY, init_order(u->thisorder, lang));
CuAssertStrEquals(tc, skillname(SK_ALCHEMY, lang), gettoken(token, sizeof(token)));
CuAssertStrEquals(tc, "LERNE Alchemie", get_command(u->thisorder, lang, token, sizeof(token)));
mstream_init(&out);
stream_order(&out, u->thisorder, lang, true);
swrite("\n", 1, 1, &out);
@ -363,7 +365,7 @@ static void test_stream_order(CuTest *tc) {
test_teardown();
}
static void test_stream_order_quoted(CuTest *tc) {
static void test_study_order_quoted(CuTest *tc) {
char token[32];
stream out;
unit *u;
@ -380,6 +382,8 @@ static void test_stream_order_quoted(CuTest *tc) {
CuAssertIntEquals(tc, K_STUDY, init_order(u->thisorder, lang));
CuAssertStrEquals(tc, skillname(SK_WEAPONLESS, lang), gettoken(token, sizeof(token)));
CuAssertStrEquals(tc, "LERNE 'Waffenloser Kampf'", get_command(u->thisorder, lang, token, sizeof(token)));
mstream_init(&out);
stream_order(&out, u->thisorder, lang, true);
swrite("\n", 1, 1, &out);
@ -396,8 +400,8 @@ CuSuite *get_order_suite(void)
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_create_order);
SUITE_ADD_TEST(suite, test_study_orders);
SUITE_ADD_TEST(suite, test_stream_order);
SUITE_ADD_TEST(suite, test_stream_order_quoted);
SUITE_ADD_TEST(suite, test_study_order);
SUITE_ADD_TEST(suite, test_study_order_quoted);
SUITE_ADD_TEST(suite, test_parse_order);
SUITE_ADD_TEST(suite, test_parse_make);
SUITE_ADD_TEST(suite, test_parse_make_temp);