add a strncat function to sbstring, use it for create_order.

eliminate bsdstring.h use.
This commit is contained in:
Enno Rehling 2017-12-31 20:07:40 +01:00
parent 0aa7a812ca
commit 62130ca44b
4 changed files with 38 additions and 23 deletions

View File

@ -19,7 +19,6 @@
#include "keyword.h"
#include <util/base36.h>
#include <util/bsdstring.h>
#include <util/language.h>
#include <util/log.h>
#include <util/parser.h>
@ -235,50 +234,46 @@ order *create_order(keyword_t kwd, const struct locale * lang,
order *ord;
char zBuffer[DISPLAYSIZE];
if (params) {
char *bufp = zBuffer;
int bytes;
size_t size = sizeof(zBuffer) - 1;
sbstring sbs;
va_list marker;
char *tok;
assert(lang);
va_start(marker, params);
sbs_init(&sbs, zBuffer, sizeof(zBuffer));
while (*params) {
if (*params == '%') {
int i;
const char *s;
++params;
switch (*params) {
int i;
const char *s;
tok = strchr(params, '%');
if (tok) {
if (tok != params) {
sbs_strncat(&sbs, params, tok - params);
}
switch (tok[1]) {
case 's':
s = va_arg(marker, const char *);
assert(s);
bytes = (int)str_strlcpy(bufp, s, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
sbs_strcat(&sbs, s);
break;
case 'd':
i = va_arg(marker, int);
bytes = (int)str_strlcpy(bufp, itoa10(i), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
sbs_strcat(&sbs, itoa10(i));
break;
case 'i':
i = va_arg(marker, int);
bytes = (int)str_strlcpy(bufp, itoa36(i), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
sbs_strcat(&sbs, itoa36(i));
break;
default:
assert(!"unknown format-character in create_order");
}
params = tok + 2;
}
else if (size > 0) {
*bufp++ = *params;
--size;
else {
sbs_strcat(&sbs, params);
break;
}
++params;
}
va_end(marker);
*bufp = 0;
}
else {
zBuffer[0] = 0;

View File

@ -249,6 +249,19 @@ void sbs_init(struct sbstring *sbs, char *buffer, size_t size)
buffer[0] = '\0';
}
void sbs_strncat(struct sbstring *sbs, const char *str, size_t size)
{
size_t len;
assert(sbs);
len = sbs->size - (sbs->end - sbs->begin) - 1;
if (len < size) {
size = len;
}
memcpy(sbs->end, str, size);
sbs->end[size] = '\0';
sbs->end += size;
}
void sbs_strcat(struct sbstring *sbs, const char *str)
{
size_t len;

View File

@ -46,6 +46,7 @@ extern "C" {
void sbs_init(struct sbstring *sbs, char *buffer, size_t size);
void sbs_strcat(struct sbstring *sbs, const char *str);
void sbs_strncat(struct sbstring *sbs, const char *str, size_t size);
void sbs_strcpy(struct sbstring *sbs, const char *str);
/* benchmark for units:

View File

@ -109,6 +109,12 @@ static void test_sbstring(CuTest * tc)
sbs_strcat(&sbs, "12345678901234567890");
CuAssertStrEquals(tc, "123456789012345", sbs.begin);
CuAssertPtrEquals(tc, buffer, sbs.begin);
sbs_strcpy(&sbs, "1234567890");
CuAssertStrEquals(tc, "1234567890", sbs.begin);
sbs_strncat(&sbs, "1234567890", 4);
CuAssertStrEquals(tc, "12345678901234", sbs.begin);
sbs_strncat(&sbs, "567890", 6);
CuAssertStrEquals(tc, "123456789012345", sbs.begin);
}
CuSuite *get_strings_suite(void)