From 4b5bd11f296485a5b15337815f6fb659eebfae90 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 3 Jun 2018 13:29:54 +0200 Subject: [PATCH] spell_syntax without bsdstring, better tests. --- src/report.c | 139 ++++++++++++++++++++++------------------------ src/report.h | 2 +- src/report.test.c | 51 +++++------------ 3 files changed, 82 insertions(+), 110 deletions(-) diff --git a/src/report.c b/src/report.c index 3976bb05b..55189a66c 100644 --- a/src/report.c +++ b/src/report.c @@ -79,7 +79,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* util includes */ #include #include -#include #include #include #include @@ -243,39 +242,44 @@ static size_t write_spell_modifier(const spell * sp, int flag, const char * str, return 0; } -void nr_spell_syntax(struct stream *out, spellbook_entry * sbe, const struct locale *lang) +void nr_spell_syntax(char *buf, size_t size, spellbook_entry * sbe, const struct locale *lang) { - int bytes; - char buf[4096]; - char *bufp = buf; - size_t size = sizeof(buf) - 1; const spell *sp = spellref_get(&sbe->spref); const char *params = sp->parameter; + const char *spname; + sbstring sbs; + sbs_init(&sbs, buf, size); if (sp->sptyp & ISCOMBATSPELL) { - bytes = (int)str_strlcpy(bufp, LOC(lang, keyword(K_COMBATSPELL)), size); + sbs_strcpy(&sbs, LOC(lang, keyword(K_COMBATSPELL))); } else { - bytes = (int)str_strlcpy(bufp, LOC(lang, keyword(K_CAST)), size); + sbs_strcpy(&sbs, LOC(lang, keyword(K_CAST))); } - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); /* Reihenfolge beachten: Erst REGION, dann STUFE! */ if (sp->sptyp & FARCASTING) { - bytes = snprintf(bufp, size, " [%s x y]", LOC(lang, parameters[P_REGION])); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); + sbs_strcat(&sbs, " ["); + sbs_strcat(&sbs, LOC(lang, parameters[P_REGION])); + sbs_strcat(&sbs, " x y]"); } if (sp->sptyp & SPELLLEVEL) { - bytes = snprintf(bufp, size, " [%s n]", LOC(lang, parameters[P_LEVEL])); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); + sbs_strcat(&sbs, " ["); + sbs_strcat(&sbs, LOC(lang, parameters[P_LEVEL])); + sbs_strcat(&sbs, " n]"); } - bytes = (int)snprintf(bufp, size, " \"%s\"", spell_name(sp, lang)); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); + spname = spell_name(sp, lang); + if (strchr(spname, ' ') != NULL) { + /* contains spaces, needs quotes */ + sbs_strcat(&sbs, " '"); + sbs_strcat(&sbs, spname); + sbs_strcat(&sbs, "'"); + } + else { + sbs_strcat(&sbs, " "); + sbs_strcat(&sbs, spname); + } while (params && *params) { typedef struct starget { @@ -298,52 +302,48 @@ void nr_spell_syntax(struct stream *out, spellbook_entry * sbe, const struct loc if (cp == 'u') { targetp = targets + 1; locp = LOC(lang, targetp->vars); - bytes = (int)snprintf(bufp, size, " <%s>", locp); + sbs_strcat(&sbs, " <"); + sbs_strcat(&sbs, locp); + sbs_strcat(&sbs, ">"); if (*params == '+') { ++params; - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); - bytes = (int)snprintf(bufp, size, " [<%s> ...]", locp); + sbs_strcat(&sbs, " [<"); + sbs_strcat(&sbs, locp); + sbs_strcat(&sbs, "> ...]"); } - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); } else if (cp == 's') { targetp = targets + 2; locp = LOC(lang, targetp->vars); - bytes = (int)snprintf(bufp, size, " <%s>", locp); + sbs_strcat(&sbs, " <"); + sbs_strcat(&sbs, locp); + sbs_strcat(&sbs, ">"); if (*params == '+') { ++params; - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); - bytes = (int)snprintf(bufp, size, " [<%s> ...]", locp); + sbs_strcat(&sbs, " [<"); + sbs_strcat(&sbs, locp); + sbs_strcat(&sbs, "> ...]"); } - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); } else if (cp == 'r') { - bytes = (int)str_strlcpy(bufp, " ", size); + sbs_strcat(&sbs, " "); if (*params == '+') { ++params; - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); - bytes = (int)str_strlcpy(bufp, " [ ...]", size); + sbs_strcat(&sbs, " [ ...]"); } - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); } else if (cp == 'b') { targetp = targets + 3; locp = LOC(lang, targetp->vars); - bytes = (int)snprintf(bufp, size, " <%s>", locp); + sbs_strcat(&sbs, " <"); + sbs_strcat(&sbs, locp); + sbs_strcat(&sbs, ">"); if (*params == '+') { ++params; - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); - bytes = (int)snprintf(bufp, size, " [<%s> ...]", locp); + sbs_strcat(&sbs, " [<"); + sbs_strcat(&sbs, locp); + sbs_strcat(&sbs, "> ...]"); } - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); } else if (cp == 'k') { int i, maxparam = 0; @@ -361,41 +361,35 @@ void nr_spell_syntax(struct stream *out, spellbook_entry * sbe, const struct loc ++maxparam; } if (!maxparam || maxparam > 1) { - bytes = (int)str_strlcpy(bufp, " (", size); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); + sbs_strcat(&sbs, " ("); } i = 0; for (targetp = targets; targetp->flag; ++targetp) { if (!maxparam || sp->sptyp & targetp->flag) { if (i++ != 0) { - bytes = (int)str_strlcpy(bufp, " |", size); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); + sbs_strcat(&sbs, " |"); } if (targetp->param && targetp->vars) { locp = LOC(lang, targetp->vars); - bytes = - (int)snprintf(bufp, size, " %s <%s>", parameters[targetp->param], - locp); + sbs_strcat(&sbs, " "); + sbs_strcat(&sbs, parameters[targetp->param]); + sbs_strcat(&sbs, " <"); + sbs_strcat(&sbs, locp); + sbs_strcat(&sbs, ">"); if (multi) { - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); - bytes = (int)snprintf(bufp, size, " [<%s> ...]", locp); + sbs_strcat(&sbs, " [<"); + sbs_strcat(&sbs, locp); + sbs_strcat(&sbs, "> ...]"); } } else { - bytes = - (int)snprintf(bufp, size, " %s", parameters[targetp->param]); + sbs_strcat(&sbs, " "); + sbs_strcat(&sbs, parameters[targetp->param]); } - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); } } if (!maxparam || maxparam > 1) { - bytes = (int)str_strlcpy(bufp, " )", size); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); + sbs_strcat(&sbs, " )"); } } else if (cp == 'i' || cp == 'c') { @@ -416,23 +410,24 @@ void nr_spell_syntax(struct stream *out, spellbook_entry * sbe, const struct loc } if (*params == '?') { ++params; - bytes = (int)snprintf(bufp, size, " [<%s>]", locp); + sbs_strcat(&sbs, " [<"); + sbs_strcat(&sbs, locp); + sbs_strcat(&sbs, ">]"); } else { - bytes = (int)snprintf(bufp, size, " <%s>", locp); + sbs_strcat(&sbs, " <"); + sbs_strcat(&sbs, locp); + sbs_strcat(&sbs, ">"); } - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); } else { log_error("unknown spell parameter %c for spell %s", cp, sp->sname); } } - *bufp = 0; - paragraph(out, buf, 2, 0, 0); - } +#include "util/bsdstring.h" + void nr_spell(struct stream *out, spellbook_entry * sbe, const struct locale *lang) { int bytes, k, itemanz, costtyp; @@ -542,10 +537,8 @@ void nr_spell(struct stream *out, spellbook_entry * sbe, const struct locale *la paragraph(out, buf, 0, 0, 0); paragraph(out, LOC(lang, "nr_spell_syntax"), 0, 0, 0); - bufp = buf; - size = sizeof(buf) - 1; - - nr_spell_syntax(out, sbe, lang); + nr_spell_syntax(buf, sizeof(buf), sbe, lang); + paragraph(out, buf, 2, 0, 0); newline(out); } diff --git a/src/report.h b/src/report.h index 45c620562..bb3629497 100644 --- a/src/report.h +++ b/src/report.h @@ -30,7 +30,7 @@ extern "C" { void report_travelthru(struct stream *out, struct region * r, const struct faction * f); void report_region(struct stream *out, const struct region * r, struct faction * f); - void nr_spell_syntax(struct stream *out, struct spellbook_entry * sbe, const struct locale *lang); + void nr_spell_syntax(char *buf, size_t size, struct spellbook_entry * sbe, const struct locale *lang); void nr_spell(struct stream *out, struct spellbook_entry * sbe, const struct locale *lang); #ifdef __cplusplus diff --git a/src/report.test.c b/src/report.test.c index 77bb6dca6..4fa97825c 100644 --- a/src/report.test.c +++ b/src/report.test.c @@ -228,32 +228,11 @@ static void set_parameter(spell_fixture spell, char *value) { } static void check_spell_syntax(CuTest *tc, char *msg, spell_fixture *spell, char *syntax) { - stream strm; char buf[1024]; - char *linestart, *newline; - size_t len; - mstream_init(&strm); - nr_spell_syntax(&strm, spell->sbe, spell->lang); - strm.api->rewind(strm.handle); - len = strm.api->read(strm.handle, buf, sizeof(buf)); - buf[len] = '\0'; + nr_spell_syntax(buf, sizeof(buf), spell->sbe, spell->lang); - linestart = strtok(buf, "\n"); - while (linestart && !strstr(linestart, "ZAUBERE")) - linestart = strtok(NULL, "\n"); - - CuAssertPtrNotNull(tc, linestart); - - newline = strtok(NULL, "\n"); - while (newline) { - *(newline - 1) = '\n'; - newline = strtok(NULL, "\n"); - } - - CuAssertStrEquals_Msg(tc, msg, syntax, linestart); - - mstream_done(&strm); + CuAssertStrEquals_Msg(tc, msg, syntax, buf); } static void test_write_spell_syntax(CuTest *tc) { @@ -262,54 +241,54 @@ static void test_write_spell_syntax(CuTest *tc) { test_setup(); setup_spell_fixture(&spell); - check_spell_syntax(tc, "vanilla", &spell, " ZAUBERE \"Testzauber\""); + check_spell_syntax(tc, "vanilla", &spell, "ZAUBERE Testzauber"); spell.sp->sptyp |= FARCASTING; - check_spell_syntax(tc, "far", &spell, " ZAUBERE [REGION x y] \"Testzauber\""); + check_spell_syntax(tc, "far", &spell, "ZAUBERE [REGION x y] Testzauber"); spell.sp->sptyp |= SPELLLEVEL; - check_spell_syntax(tc, "farlevel", &spell, " ZAUBERE [REGION x y] [STUFE n] \"Testzauber\""); + check_spell_syntax(tc, "farlevel", &spell, "ZAUBERE [REGION x y] [STUFE n] Testzauber"); spell.sp->sptyp = 0; set_parameter(spell, "kc"); - check_spell_syntax(tc, "kc", &spell, " ZAUBERE \"Testzauber\" ( REGION | EINHEIT | SCHIFF | BURG )"); + check_spell_syntax(tc, "kc", &spell, "ZAUBERE Testzauber ( REGION | EINHEIT | SCHIFF | BURG )"); spell.sp->sptyp |= BUILDINGSPELL; - check_spell_syntax(tc, "kc typed", &spell, " ZAUBERE \"Testzauber\" BURG "); + check_spell_syntax(tc, "kc typed", &spell, "ZAUBERE Testzauber BURG "); spell.sp->sptyp = 0; set_parameter(spell, "b"); - check_spell_syntax(tc, "b", &spell, " ZAUBERE \"Testzauber\" "); + check_spell_syntax(tc, "b", &spell, "ZAUBERE Testzauber "); set_parameter(spell, "s"); - check_spell_syntax(tc, "s", &spell, " ZAUBERE \"Testzauber\" "); + check_spell_syntax(tc, "s", &spell, "ZAUBERE Testzauber "); set_parameter(spell, "s+"); - check_spell_syntax(tc, "s+", &spell, " ZAUBERE \"Testzauber\" [ ...]"); + check_spell_syntax(tc, "s+", &spell, "ZAUBERE Testzauber [ ...]"); set_parameter(spell, "u"); - check_spell_syntax(tc, "u", &spell, " ZAUBERE \"Testzauber\" "); + check_spell_syntax(tc, "u", &spell, "ZAUBERE Testzauber "); set_parameter(spell, "r"); - check_spell_syntax(tc, "r", &spell, " ZAUBERE \"Testzauber\" "); + check_spell_syntax(tc, "r", &spell, "ZAUBERE Testzauber "); set_parameter(spell, "bc"); free(spell.sp->syntax); spell.sp->syntax = str_strdup("hodor"); - check_spell_syntax(tc, "bc hodor", &spell, " ZAUBERE \"Testzauber\" "); + check_spell_syntax(tc, "bc hodor", &spell, "ZAUBERE Testzauber "); free(spell.sp->syntax); spell.sp->syntax = 0; set_parameter(spell, "c?"); free(spell.sp->syntax); spell.sp->syntax = str_strdup("hodor"); - check_spell_syntax(tc, "c?", &spell, " ZAUBERE \"Testzauber\" []"); + check_spell_syntax(tc, "c?", &spell, "ZAUBERE Testzauber []"); free(spell.sp->syntax); spell.sp->syntax = 0; set_parameter(spell, "kc+"); check_spell_syntax(tc, "kc+", &spell, - " ZAUBERE \"Testzauber\" ( REGION | EINHEIT [ ...] | SCHIFF \n [ ...] | BURG [ ...] )"); + "ZAUBERE Testzauber ( REGION | EINHEIT [ ...] | SCHIFF [ ...] | BURG [ ...] )"); cleanup_spell_fixture(&spell); test_teardown();