fix sbs_strcat, use it for orders.

This commit is contained in:
Enno Rehling 2017-12-31 18:51:56 +01:00
parent ddd30e6210
commit cc8d34a1e7
3 changed files with 16 additions and 44 deletions

View File

@ -68,29 +68,17 @@ 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) {
char *bufp = sbuffer;
order_data *od = NULL;
const char * text;
keyword_t kwd = ORD_KEYWORD(ord);
int bytes;
sbstring sbs;
sbs_init(&sbs, sbuffer, size);
if (ord->command & CMD_QUIET) {
if (size > 0) {
*bufp++ = '!';
--size;
}
else {
WARN_STATIC_BUFFER();
}
sbs_strcpy(&sbs, "!");
}
if (ord->command & CMD_PERSIST) {
if (size > 0) {
*bufp++ = '@';
--size;
}
else {
WARN_STATIC_BUFFER();
}
sbs_strcat(&sbs, "@");
}
if (ord->id < 0) {
@ -102,39 +90,19 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si
text = OD_STRING(od);
}
if (kwd != NOKEYWORD) {
if (size > 0) {
const char *str = (const char *)LOC(lang, keyword(kwd));
assert(str);
if (text) --size;
bytes = (int)str_strlcpy(bufp, str, size);
if (wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
if (text) *bufp++ = ' ';
}
else {
WARN_STATIC_BUFFER();
const char *str = (const char *)LOC(lang, keyword(kwd));
assert(str);
sbs_strcat(&sbs, str);
if (text) {
sbs_strcat(&sbs, " ");
}
}
if (text) {
bytes = (int)str_strlcpy(bufp, (const char *)text, size);
if (wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
if (bufp - sbuffer >= 6) {
bufp -= 6;
while (bufp > sbuffer && (*bufp & 0x80) != 0) {
++size;
--bufp;
}
memcpy(bufp, "[...]", 6); /* TODO: make sure this only happens in eval_command */
bufp += 6;
}
}
sbs_strcat(&sbs, text);
}
if (od) {
odata_release(od);
}
if (size > 0) *bufp = 0;
return sbuffer;
}

View File

@ -261,5 +261,8 @@ void sbs_strcat(struct sbstring *sbs, const char *str)
void sbs_strcpy(struct sbstring *sbs, const char *str)
{
size_t len = str_strlcpy(sbs->begin, str, sbs->size);
sbs->end += len;
if (len >= sbs->size) {
len = sbs->size - 1;
}
sbs->end = sbs->begin + len;
}

View File

@ -105,9 +105,10 @@ static void test_sbstring(CuTest * tc)
CuAssertStrEquals(tc, "Hodor", sbs.begin);
sbs_strcpy(&sbs, "12345678901234567890");
CuAssertStrEquals(tc, "123456789012345", sbs.begin);
CuAssertPtrEquals(tc, sbs.begin + sbs.size - 1, sbs.end);
sbs_strcat(&sbs, "12345678901234567890");
CuAssertStrEquals(tc, "123456789012345", sbs.begin);
CuAssertStrEquals(tc, buffer, sbs.begin);
CuAssertPtrEquals(tc, buffer, sbs.begin);
}
CuSuite *get_strings_suite(void)