excise bsdstring from the project

This commit is contained in:
Enno Rehling 2018-12-01 20:25:06 +01:00
parent 5d99dc7014
commit d162c2dee1
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);
if (ord->command & CMD_QUIET) {
sbs_strcpy(&sbs, "!");
sbs_strcat(&sbs, "!");
}
if (ord->command & CMD_PERSIST) {
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];
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));
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_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, 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 */
#include <util/base36.h>
#include "util/bsdstring.h"
#include <util/goodies.h>
#include <util/language.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);
if (sp->sptyp & ISCOMBATSPELL) {
sbs_strcpy(&sbs, LOC(lang, keyword(K_COMBATSPELL)));
sbs_strcat(&sbs, LOC(lang, keyword(K_COMBATSPELL)));
}
else {
sbs_strcpy(&sbs, LOC(lang, keyword(K_CAST)));
sbs_strcat(&sbs, LOC(lang, keyword(K_CAST)));
}
/* 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) {
char *pos, *begin = sbp->begin;
assert(begin);
while (*begin && IS_UTF8_SPACE(begin)) {
/* eat whitespace */
++begin;
@ -1619,41 +1619,32 @@ static void guards(struct stream *out, const region * r, const faction * see)
}
if (nextguard || tarned) {
char buf[8192];
char *bufp = buf;
size_t size = sizeof(buf) - 1;
int bytes;
char buf[2048];
sbstring sbs;
bytes = (int)str_strlcpy(bufp, LOC(see->locale, "nr_guarding_prefix"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
sbs_init(&sbs, buf, sizeof(buf));
sbs_strcat(&sbs, LOC(see->locale, "nr_guarding_prefix"));
for (i = 0; i != nextguard + (tarned ? 1 : 0); ++i) {
if (i != 0) {
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 {
bytes = (int)str_strlcpy(bufp, ", ", size);
sbs_strcat(&sbs, ", ");
}
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
}
if (i < nextguard) {
bytes = (int)str_strlcpy(bufp, factionname(guardians[i]), size);
sbs_strcat(&sbs, factionname(guardians[i]));
}
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)
WARN_STATIC_BUFFER();
pump_paragraph(&sbs, out, REPORTWIDTH, false);
}
bytes = (int)str_strlcpy(bufp, LOC(see->locale, "nr_guarding_postfix"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
sbs_strcat(&sbs, LOC(see->locale, "nr_guarding_postfix"));
newline(out);
*bufp = 0;
paragraph(out, buf, 0, 0, 0);
pump_paragraph(&sbs, out, REPORTWIDTH, true);
}
}
@ -1704,11 +1695,11 @@ static void
nr_ship(struct stream *out, const region *r, const ship * sh, const faction * f,
const unit * captain)
{
char buffer[8192], *bufp = buffer;
size_t size = sizeof(buffer) - 1;
int bytes;
char buffer[1024];
char ch;
sbstring sbs;
sbs_init(&sbs, buffer, sizeof(buffer));
newline(out);
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);
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);
}
else {
bytes =
snprintf(bufp, size, "%s, %s", shipname(sh), LOC(f->locale,
sh->type->_name));
sbs_printf(&sbs, "%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) {
bytes = snprintf(bufp, size, ", %s (%d/%d)",
sbs_printf(&sbs, ", %s (%d/%d)",
LOC(f->locale, "nr_undercons"), sh->size,
sh->type->construction->maxsize);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
}
if (sh->damage) {
int percent = ship_damage_percent(sh);
bytes =
snprintf(bufp, size, ", %d%% %s", percent, LOC(f->locale, "nr_damaged"));
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
sbs_printf(&sbs, ", %d%% %s", percent, LOC(f->locale, "nr_damaged"));
}
if (!fval(r->terrain, SEA_REGION)) {
if (sh->coast != NODIRECTION) {
bytes = (int)str_strlcpy(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
bytes = (int)str_strlcpy(bufp, LOC(f->locale, coasts[sh->coast]), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
sbs_strcat(&sbs, ", ");
sbs_strcat(&sbs, LOC(f->locale, coasts[sh->coast]));
}
}
ch = 0;
if (sh->display && sh->display[0]) {
bytes = (int)str_strlcpy(bufp, "; ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
bytes = (int)str_strlcpy(bufp, sh->display, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
sbs_strcat(&sbs, "; ");
sbs_strcat(&sbs, sh->display);
ch = sh->display[strlen(sh->display) - 1];
}
if (ch != '!' && ch != '?' && ch != '.') {
bytes = (int)str_strlcpy(bufp, ".", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
sbs_strcat(&sbs, ".");
}
*bufp = 0;
paragraph(out, buffer, 2, 0, 0);
nr_curses(out, 4, f, TYP_SHIP, sh);

View File

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

@ -319,16 +319,6 @@ void sbs_strcat(struct sbstring *sbs, const char *str)
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)
{
if (pos > sbs->end - sbs->begin) {

View File

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

View File

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