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

View file

@ -249,6 +249,19 @@ void sbs_init(struct sbstring *sbs, char *buffer, size_t size)
buffer[0] = '\0'; 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) void sbs_strcat(struct sbstring *sbs, const char *str)
{ {
size_t len; size_t len;

View file

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

View file

@ -109,6 +109,12 @@ static void test_sbstring(CuTest * tc)
sbs_strcat(&sbs, "12345678901234567890"); sbs_strcat(&sbs, "12345678901234567890");
CuAssertStrEquals(tc, "123456789012345", sbs.begin); CuAssertStrEquals(tc, "123456789012345", sbs.begin);
CuAssertPtrEquals(tc, buffer, 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) CuSuite *get_strings_suite(void)