replace bsdstring functions in cycle_route.

This commit is contained in:
Enno Rehling 2018-05-27 20:31:36 +02:00
parent 034872c44e
commit 6abf054b42
4 changed files with 33 additions and 29 deletions

View File

@ -1047,15 +1047,17 @@ int movewhere(const unit * u, const char *token, region * r, region ** resultp)
order * cycle_route(order * ord, const struct locale *lang, int gereist)
{
int cm = 0;
char tail[1024], *bufp = tail;
char neworder[2048], *obuf = neworder;
char tail[1024];
char neworder[2048];
char token[128];
direction_t d = NODIRECTION;
bool paused = false;
bool pause;
order *norder;
size_t size = sizeof(tail) - 1;
sbstring sbtail;
sbstring sborder;
sbs_init(&sbtail, tail, sizeof(tail));
sbs_init(&sborder, neworder, sizeof(neworder));
assert(getkeyword(ord) == K_ROUTE);
tail[0] = '\0';
neworder[0] = '\0';
@ -1063,14 +1065,10 @@ order * cycle_route(order * ord, const struct locale *lang, int gereist)
for (cm = 0;; ++cm) {
const char *s;
pause = false;
s = gettoken(token, sizeof(token));
if (s && *s) {
d = get_direction(s, lang);
if (d == D_PAUSE) {
pause = true;
}
else if (d == NODIRECTION) {
if (d == NODIRECTION) {
break;
}
}
@ -1079,38 +1077,37 @@ order * cycle_route(order * ord, const struct locale *lang, int gereist)
}
if (cm < gereist) {
/* TODO: hier sollte keine PAUSE auftreten */
assert(!pause);
if (!pause) {
assert (d != D_PAUSE);
if (d != D_PAUSE) {
const char *loc = LOC(lang, shortdirections[d]);
assert(loc);
if (bufp != tail) {
bufp = STRLCPY_EX(bufp, " ", &size, "cycle_route");
if (sbs_length(&sbtail) > 0) {
sbs_strcat(&sbtail, " ");
}
bufp = STRLCPY_EX(bufp, loc, &size, "cycle_route");
sbs_strcat(&sbtail, loc);
}
}
else if (strlen(neworder) > sizeof(neworder) / 2)
break;
else if (cm == gereist && !paused && pause) {
else if (cm == gereist && !paused && (d == D_PAUSE)) {
const char *loc = LOC(lang, parameters[P_PAUSE]);
bufp = STRLCPY_EX(bufp, " ", &size, "cycle_route");
bufp = STRLCPY_EX(bufp, loc, &size, "cycle_route");
sbs_strcat(&sbtail, " ");
sbs_strcat(&sbtail, loc);
paused = true;
}
else if (pause) {
/* da PAUSE nicht in ein shortdirections[d] umgesetzt wird (ist
* hier keine normale direction), muss jede PAUSE einzeln
* herausgefiltert und explizit gesetzt werden */
if (neworder != obuf) {
obuf += str_strlcat(obuf, " ", sizeof(neworder) - (obuf - neworder));
}
obuf += str_strlcat(obuf, LOC(lang, parameters[P_PAUSE]), sizeof(neworder) - (obuf - neworder));
}
else {
if (neworder != obuf) {
obuf += str_strlcat(obuf, " ", sizeof(neworder) - (obuf - neworder));
if (sbs_length(&sbtail) > 0) {
sbs_strcat(&sborder, " ");
}
if (d == D_PAUSE) {
/* da PAUSE nicht in ein shortdirections[d] umgesetzt wird (ist
* hier keine normale direction), muss jede PAUSE einzeln
* herausgefiltert und explizit gesetzt werden */
sbs_strcat(&sborder, LOC(lang, parameters[P_PAUSE]));
}
else {
sbs_strcat(&sborder, LOC(lang, shortdirections[d]));
}
obuf += str_strlcat(obuf, LOC(lang, shortdirections[d]), sizeof(neworder) - (obuf - neworder));
}
}

View File

@ -495,6 +495,7 @@ static void test_follow_ship_msg(CuTest * tc) {
follow_ship(u, ord);
CuAssertPtrEquals(tc, r, u->region);
CuAssertPtrNotNull(tc, msg = test_find_messagetype(u->faction->msgs, "error18"));
p = msg->parameters[2].v;
CuAssertPtrNotNull(tc, p);

View File

@ -275,6 +275,11 @@ void sbs_strcpy(struct sbstring *sbs, const char *str)
sbs->end = sbs->begin + len;
}
size_t sbs_length(const struct sbstring *sbs)
{
return sbs->end - sbs->begin;
}
char *str_unescape(char *str) {
char *read = str, *write = str;
while (*read) {

View File

@ -50,6 +50,7 @@ extern "C" {
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);
size_t sbs_length(const struct sbstring *sbs);
/* benchmark for units:
* JENKINS_HASH: 5.25 misses/hit (with good cache behavior)