excise bsdstring from the project

This commit is contained in:
Enno Rehling 2018-12-01 20:25:06 +01:00
parent c5acb2f351
commit 06ccc7df42
10 changed files with 38 additions and 148 deletions

View file

@ -120,7 +120,7 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si
sbs_init(&sbs, sbuffer, size); sbs_init(&sbs, sbuffer, size);
if (ord->command & CMD_QUIET) { if (ord->command & CMD_QUIET) {
sbs_strcpy(&sbs, "!"); sbs_strcat(&sbs, "!");
} }
if (ord->command & CMD_PERSIST) { if (ord->command & CMD_PERSIST) {
sbs_strcat(&sbs, "@"); sbs_strcat(&sbs, "@");

View file

@ -546,7 +546,7 @@ const char *racename(const struct locale *loc, const unit * u, const race * rc)
char ch[2]; char ch[2];
sbs_init(&sbs, lbuf, sizeof(lbuf)); sbs_init(&sbs, lbuf, sizeof(lbuf));
sbs_strcpy(&sbs, LOC(loc, mkname("prefix", prefix))); sbs_strcat(&sbs, LOC(loc, mkname("prefix", prefix)));
str = LOC(loc, rc_name_s(rc, u->number != 1)); str = LOC(loc, rc_name_s(rc, u->number != 1));
assert(~str[0] & 0x80 || !"unicode/not implemented"); assert(~str[0] & 0x80 || !"unicode/not implemented");

View file

@ -2286,7 +2286,7 @@ int follow_ship(unit * u, order * ord)
} }
sbs_init(&sbcmd, command, sizeof(command)); sbs_init(&sbcmd, command, sizeof(command));
sbs_strcpy(&sbcmd, LOC(u->faction->locale, keyword(K_MOVE))); sbs_strcat(&sbcmd, LOC(u->faction->locale, keyword(K_MOVE)));
sbs_strcat(&sbcmd, " "); sbs_strcat(&sbcmd, " ");
sbs_strcat(&sbcmd, LOC(u->faction->locale, directions[dir])); sbs_strcat(&sbcmd, LOC(u->faction->locale, directions[dir]));

View file

@ -79,7 +79,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */ /* util includes */
#include <util/base36.h> #include <util/base36.h>
#include "util/bsdstring.h"
#include <util/goodies.h> #include <util/goodies.h>
#include <util/language.h> #include <util/language.h>
#include <util/lists.h> #include <util/lists.h>
@ -257,10 +256,10 @@ void nr_spell_syntax(char *buf, size_t size, spellbook_entry * sbe, const struct
sbs_init(&sbs, buf, size); sbs_init(&sbs, buf, size);
if (sp->sptyp & ISCOMBATSPELL) { if (sp->sptyp & ISCOMBATSPELL) {
sbs_strcpy(&sbs, LOC(lang, keyword(K_COMBATSPELL))); sbs_strcat(&sbs, LOC(lang, keyword(K_COMBATSPELL)));
} }
else { else {
sbs_strcpy(&sbs, LOC(lang, keyword(K_CAST))); sbs_strcat(&sbs, LOC(lang, keyword(K_CAST)));
} }
/* Reihenfolge beachten: Erst REGION, dann STUFE! */ /* Reihenfolge beachten: Erst REGION, dann STUFE! */
@ -1431,6 +1430,7 @@ void pump_paragraph(sbstring *sbp, stream *out, size_t maxlen, bool isfinal)
{ {
while (sbs_length(sbp) > maxlen) { while (sbs_length(sbp) > maxlen) {
char *pos, *begin = sbp->begin; char *pos, *begin = sbp->begin;
assert(begin);
while (*begin && IS_UTF8_SPACE(begin)) { while (*begin && IS_UTF8_SPACE(begin)) {
/* eat whitespace */ /* eat whitespace */
++begin; ++begin;
@ -1619,41 +1619,32 @@ static void guards(struct stream *out, const region * r, const faction * see)
} }
if (nextguard || tarned) { if (nextguard || tarned) {
char buf[8192]; char buf[2048];
char *bufp = buf; sbstring sbs;
size_t size = sizeof(buf) - 1;
int bytes;
bytes = (int)str_strlcpy(bufp, LOC(see->locale, "nr_guarding_prefix"), size); sbs_init(&sbs, buf, sizeof(buf));
if (wrptr(&bufp, &size, bytes) != 0) sbs_strcat(&sbs, LOC(see->locale, "nr_guarding_prefix"));
WARN_STATIC_BUFFER();
for (i = 0; i != nextguard + (tarned ? 1 : 0); ++i) { for (i = 0; i != nextguard + (tarned ? 1 : 0); ++i) {
if (i != 0) { if (i != 0) {
if (i == nextguard - (tarned ? 0 : 1)) { if (i == nextguard - (tarned ? 0 : 1)) {
bytes = (int)str_strlcpy(bufp, LOC(see->locale, "list_and"), size); sbs_strcat(&sbs, LOC(see->locale, "list_and"));
} }
else { else {
bytes = (int)str_strlcpy(bufp, ", ", size); sbs_strcat(&sbs, ", ");
} }
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
if (i < nextguard) { if (i < nextguard) {
bytes = (int)str_strlcpy(bufp, factionname(guardians[i]), size); sbs_strcat(&sbs, factionname(guardians[i]));
} }
else { else {
bytes = (int)str_strlcpy(bufp, LOC(see->locale, "nr_guarding_unknown"), size); sbs_strcat(&sbs, LOC(see->locale, "nr_guarding_unknown"));
} }
if (wrptr(&bufp, &size, bytes) != 0) pump_paragraph(&sbs, out, REPORTWIDTH, false);
WARN_STATIC_BUFFER();
} }
bytes = (int)str_strlcpy(bufp, LOC(see->locale, "nr_guarding_postfix"), size); sbs_strcat(&sbs, LOC(see->locale, "nr_guarding_postfix"));
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
newline(out); newline(out);
*bufp = 0; pump_paragraph(&sbs, out, REPORTWIDTH, true);
paragraph(out, buf, 0, 0, 0);
} }
} }
@ -1704,11 +1695,11 @@ static void
nr_ship(struct stream *out, const region *r, const ship * sh, const faction * f, nr_ship(struct stream *out, const region *r, const ship * sh, const faction * f,
const unit * captain) const unit * captain)
{ {
char buffer[8192], *bufp = buffer; char buffer[1024];
size_t size = sizeof(buffer) - 1;
int bytes;
char ch; char ch;
sbstring sbs;
sbs_init(&sbs, buffer, sizeof(buffer));
newline(out); newline(out);
if (captain && captain->faction == f) { if (captain && captain->faction == f) {
@ -1716,57 +1707,37 @@ nr_ship(struct stream *out, const region *r, const ship * sh, const faction * f,
getshipweight(sh, &n, &p); getshipweight(sh, &n, &p);
n = (n + 99) / 100; /* 1 Silber = 1 GE */ n = (n + 99) / 100; /* 1 Silber = 1 GE */
bytes = snprintf(bufp, size, "%s, %s, (%d/%d)", shipname(sh), sbs_printf(&sbs, "%s, %s, (%d/%d)", shipname(sh),
LOC(f->locale, sh->type->_name), n, shipcapacity(sh) / 100); LOC(f->locale, sh->type->_name), n, shipcapacity(sh) / 100);
} }
else { else {
bytes = sbs_printf(&sbs, "%s, %s", shipname(sh), LOC(f->locale, sh->type->_name));
snprintf(bufp, size, "%s, %s", shipname(sh), LOC(f->locale,
sh->type->_name));
} }
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (sh->size != sh->type->construction->maxsize) { if (sh->size != sh->type->construction->maxsize) {
bytes = snprintf(bufp, size, ", %s (%d/%d)", sbs_printf(&sbs, ", %s (%d/%d)",
LOC(f->locale, "nr_undercons"), sh->size, LOC(f->locale, "nr_undercons"), sh->size,
sh->type->construction->maxsize); sh->type->construction->maxsize);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
if (sh->damage) { if (sh->damage) {
int percent = ship_damage_percent(sh); int percent = ship_damage_percent(sh);
bytes = sbs_printf(&sbs, ", %d%% %s", percent, LOC(f->locale, "nr_damaged"));
snprintf(bufp, size, ", %d%% %s", percent, LOC(f->locale, "nr_damaged"));
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
if (!fval(r->terrain, SEA_REGION)) { if (!fval(r->terrain, SEA_REGION)) {
if (sh->coast != NODIRECTION) { if (sh->coast != NODIRECTION) {
bytes = (int)str_strlcpy(bufp, ", ", size); sbs_strcat(&sbs, ", ");
if (wrptr(&bufp, &size, bytes) != 0) sbs_strcat(&sbs, LOC(f->locale, coasts[sh->coast]));
WARN_STATIC_BUFFER();
bytes = (int)str_strlcpy(bufp, LOC(f->locale, coasts[sh->coast]), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
} }
ch = 0; ch = 0;
if (sh->display && sh->display[0]) { if (sh->display && sh->display[0]) {
bytes = (int)str_strlcpy(bufp, "; ", size); sbs_strcat(&sbs, "; ");
if (wrptr(&bufp, &size, bytes) != 0) sbs_strcat(&sbs, sh->display);
WARN_STATIC_BUFFER();
bytes = (int)str_strlcpy(bufp, sh->display, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
ch = sh->display[strlen(sh->display) - 1]; ch = sh->display[strlen(sh->display) - 1];
} }
if (ch != '!' && ch != '?' && ch != '.') { if (ch != '!' && ch != '?' && ch != '.') {
bytes = (int)str_strlcpy(bufp, ".", size); sbs_strcat(&sbs, ".");
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
*bufp = 0;
paragraph(out, buffer, 2, 0, 0); paragraph(out, buffer, 2, 0, 0);
nr_curses(out, 4, f, TYP_SHIP, sh); nr_curses(out, 4, f, TYP_SHIP, sh);

View file

@ -4,7 +4,6 @@ add_subdirectory (crypto)
SET(_TEST_FILES SET(_TEST_FILES
base36.test.c base36.test.c
# bsdstring.test.c
# crmessage.test.c # crmessage.test.c
# dice.test.c # dice.test.c
# filereader.test.c # filereader.test.c
@ -34,7 +33,6 @@ variant.test.c
SET(_FILES SET(_FILES
base36.c base36.c
bsdstring.c
crmessage.c crmessage.c
dice.c dice.c
filereader.c filereader.c

View file

@ -1,52 +0,0 @@
#ifdef _MSC_VER
#include <platform.h>
#endif
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include "bsdstring.h"
#include "log.h"
#include "strings.h"
int wrptr(char **ptr, size_t * size, int result)
{
size_t bytes = (size_t)result;
if (result < 0) {
/* buffer was too small */
if (*size > 0) {
**ptr = 0;
*size = 0;
}
errno = 0;
return ERANGE;
}
if (bytes == 0) {
return 0;
}
if (bytes <= *size) {
*ptr += bytes;
*size -= bytes;
return 0;
}
*ptr += *size;
*size = 0;
return ERANGE;
}
char * strlcpy_w(char *dst, const char *src, size_t *siz, const char *err, const char *file, int line)
{
size_t bytes = str_strlcpy(dst, src, *siz);
char * buf = dst;
assert(bytes <= INT_MAX);
if (wrptr(&buf, siz, (int)bytes) != 0) {
if (err) {
log_warning("%s: static buffer too small in %s:%d\n", err, file, line);
} else {
log_warning("static buffer too small in %s:%d\n", file, line);
}
}
return buf;
}

View file

@ -1,16 +0,0 @@
#ifndef UTIL_BSDSTRING_H
#define UTIL_BSDSTRING_H
#include <stddef.h>
int wrptr(char **ptr, size_t * size, int bytes);
char * strlcpy_w(char *dst, const char *src, size_t *siz, const char *err, const char *file, int line);
#define BUFFER_STRCAT(bufp, size, bytes) if (wrptr(&bufp, &size, bytes) != 0) log_warning("static buffer too small in %s:%d\n", __FILE__, __LINE__);
#define WARN_STATIC_BUFFER() log_warning("static buffer too small in %s:%d\n", __FILE__, __LINE__)
#define INFO_STATIC_BUFFER() log_info("static buffer too small in %s:%d\n", __FILE__, __LINE__)
#define STRLCPY(dst, src, siz) strlcpy_w((dst), (src), &(siz), 0, __FILE__, __LINE__)
#define STRLCPY_EX(dst, src, siz, err) strlcpy_w((dst), (src), (siz), (err), __FILE__, __LINE__)
#endif

View file

@ -323,16 +323,6 @@ void sbs_strcat(struct sbstring *sbs, const char *str)
assert(sbs->begin + sbs->size >= sbs->end); assert(sbs->begin + sbs->size >= sbs->end);
} }
void sbs_strcpy(struct sbstring *sbs, const char *str)
{
size_t len = str_strlcpy(sbs->begin, str, sbs->size);
if (len >= sbs->size) {
len = sbs->size - 1;
}
sbs->end = sbs->begin + len;
assert(sbs->begin + sbs->size >= sbs->end);
}
void sbs_substr(sbstring *sbs, ptrdiff_t pos, size_t len) void sbs_substr(sbstring *sbs, ptrdiff_t pos, size_t len)
{ {
if (pos > sbs->end - sbs->begin) { if (pos > sbs->end - sbs->begin) {

View file

@ -54,7 +54,6 @@ extern "C" {
void sbs_adopt(struct sbstring *sbs, char *buffer, size_t size); void sbs_adopt(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_strncat(struct sbstring *sbs, const char *str, size_t size);
void sbs_strcpy(struct sbstring *sbs, const char *str);
void sbs_substr(struct sbstring *sbp, ptrdiff_t pos, size_t len); void sbs_substr(struct sbstring *sbp, ptrdiff_t pos, size_t len);
size_t sbs_length(const struct sbstring *sbs); size_t sbs_length(const struct sbstring *sbs);

View file

@ -139,19 +139,19 @@ static void test_sbstring(CuTest * tc)
sbstring sbs; sbstring sbs;
sbs_init(&sbs, buffer, sizeof(buffer)); sbs_init(&sbs, buffer, sizeof(buffer));
CuAssertStrEquals(tc, "", sbs.begin); CuAssertStrEquals(tc, "", sbs.begin);
sbs_strcpy(&sbs, "Hodor"); sbs_strcat(&sbs, "Hodor");
CuAssertStrEquals(tc, "Hodor", sbs.begin); CuAssertStrEquals(tc, "Hodor", sbs.begin);
sbs_strcat(&sbs, "Hodor"); sbs_strcat(&sbs, "Hodor");
CuAssertStrEquals(tc, "HodorHodor", sbs.begin); CuAssertStrEquals(tc, "HodorHodor", sbs.begin);
sbs_strcpy(&sbs, "Hodor"); sbs_strcat(&sbs, "Hodor");
CuAssertStrEquals(tc, "Hodor", sbs.begin); CuAssertStrEquals(tc, "Hodor", sbs.begin);
sbs_strcpy(&sbs, "12345678901234567890"); sbs_strcat(&sbs, "12345678901234567890");
CuAssertStrEquals(tc, "123456789012345", sbs.begin); CuAssertStrEquals(tc, "123456789012345", sbs.begin);
CuAssertPtrEquals(tc, sbs.begin + sbs.size - 1, sbs.end); CuAssertPtrEquals(tc, sbs.begin + sbs.size - 1, sbs.end);
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"); sbs_strcat(&sbs, "1234567890");
CuAssertStrEquals(tc, "1234567890", sbs.begin); CuAssertStrEquals(tc, "1234567890", sbs.begin);
sbs_strncat(&sbs, "1234567890", 4); sbs_strncat(&sbs, "1234567890", 4);
CuAssertStrEquals(tc, "12345678901234", sbs.begin); CuAssertStrEquals(tc, "12345678901234", sbs.begin);
@ -194,31 +194,31 @@ static void test_sbs_substr(CuTest * tc)
buffer[9] = 'A'; buffer[9] = 'A';
sbs_init(&sbs, buffer, sizeof(buffer)); sbs_init(&sbs, buffer, sizeof(buffer));
sbs_strcpy(&sbs, "12345678"); sbs_strcat(&sbs, "12345678");
CuAssertStrEquals(tc, "12345678", buffer); CuAssertStrEquals(tc, "12345678", buffer);
sbs_substr(&sbs, 0, 4); sbs_substr(&sbs, 0, 4);
CuAssertStrEquals(tc, "1234", buffer); CuAssertStrEquals(tc, "1234", buffer);
CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs)); CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs));
sbs_strcpy(&sbs, "12345678"); sbs_strcat(&sbs, "12345678");
CuAssertStrEquals(tc, "12345678", buffer); CuAssertStrEquals(tc, "12345678", buffer);
sbs_substr(&sbs, 4, 4); sbs_substr(&sbs, 4, 4);
CuAssertStrEquals(tc, "5678", buffer); CuAssertStrEquals(tc, "5678", buffer);
CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs)); CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs));
sbs_strcpy(&sbs, "12345678"); sbs_strcat(&sbs, "12345678");
CuAssertStrEquals(tc, "12345678", buffer); CuAssertStrEquals(tc, "12345678", buffer);
sbs_substr(&sbs, 2, 4); sbs_substr(&sbs, 2, 4);
CuAssertStrEquals(tc, "3456", buffer); CuAssertStrEquals(tc, "3456", buffer);
CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs)); CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs));
sbs_strcpy(&sbs, "12345678"); sbs_strcat(&sbs, "12345678");
CuAssertStrEquals(tc, "12345678", buffer); CuAssertStrEquals(tc, "12345678", buffer);
sbs_substr(&sbs, 4, 4); sbs_substr(&sbs, 4, 4);
CuAssertStrEquals(tc, "5678", buffer); CuAssertStrEquals(tc, "5678", buffer);
CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs)); CuAssertIntEquals(tc, 4, (int)sbs_length(&sbs));
sbs_strcpy(&sbs, "12345678"); sbs_strcat(&sbs, "12345678");
CuAssertStrEquals(tc, "12345678", buffer); CuAssertStrEquals(tc, "12345678", buffer);
sbs_substr(&sbs, 4, 8); sbs_substr(&sbs, 4, 8);
CuAssertStrEquals(tc, "5678", buffer); CuAssertStrEquals(tc, "5678", buffer);