spell_syntax without bsdstring, better tests.

This commit is contained in:
Enno Rehling 2018-06-03 13:29:54 +02:00
parent 6234ae7e76
commit 4b5bd11f29
3 changed files with 82 additions and 110 deletions

View file

@ -79,7 +79,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#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>
@ -243,39 +242,44 @@ static size_t write_spell_modifier(const spell * sp, int flag, const char * str,
return 0; 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 spell *sp = spellref_get(&sbe->spref);
const char *params = sp->parameter; const char *params = sp->parameter;
const char *spname;
sbstring sbs;
sbs_init(&sbs, buf, size);
if (sp->sptyp & ISCOMBATSPELL) { if (sp->sptyp & ISCOMBATSPELL) {
bytes = (int)str_strlcpy(bufp, LOC(lang, keyword(K_COMBATSPELL)), size); sbs_strcpy(&sbs, LOC(lang, keyword(K_COMBATSPELL)));
} }
else { 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! */ /* Reihenfolge beachten: Erst REGION, dann STUFE! */
if (sp->sptyp & FARCASTING) { if (sp->sptyp & FARCASTING) {
bytes = snprintf(bufp, size, " [%s x y]", LOC(lang, parameters[P_REGION])); sbs_strcat(&sbs, " [");
if (wrptr(&bufp, &size, bytes) != 0) sbs_strcat(&sbs, LOC(lang, parameters[P_REGION]));
WARN_STATIC_BUFFER(); sbs_strcat(&sbs, " x y]");
} }
if (sp->sptyp & SPELLLEVEL) { if (sp->sptyp & SPELLLEVEL) {
bytes = snprintf(bufp, size, " [%s n]", LOC(lang, parameters[P_LEVEL])); sbs_strcat(&sbs, " [");
if (wrptr(&bufp, &size, bytes) != 0) sbs_strcat(&sbs, LOC(lang, parameters[P_LEVEL]));
WARN_STATIC_BUFFER(); sbs_strcat(&sbs, " n]");
} }
bytes = (int)snprintf(bufp, size, " \"%s\"", spell_name(sp, lang)); spname = spell_name(sp, lang);
if (wrptr(&bufp, &size, bytes) != 0) if (strchr(spname, ' ') != NULL) {
WARN_STATIC_BUFFER(); /* 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) { while (params && *params) {
typedef struct starget { typedef struct starget {
@ -298,52 +302,48 @@ void nr_spell_syntax(struct stream *out, spellbook_entry * sbe, const struct loc
if (cp == 'u') { if (cp == 'u') {
targetp = targets + 1; targetp = targets + 1;
locp = LOC(lang, targetp->vars); locp = LOC(lang, targetp->vars);
bytes = (int)snprintf(bufp, size, " <%s>", locp); sbs_strcat(&sbs, " <");
sbs_strcat(&sbs, locp);
sbs_strcat(&sbs, ">");
if (*params == '+') { if (*params == '+') {
++params; ++params;
if (wrptr(&bufp, &size, bytes) != 0) sbs_strcat(&sbs, " [<");
WARN_STATIC_BUFFER(); sbs_strcat(&sbs, locp);
bytes = (int)snprintf(bufp, size, " [<%s> ...]", locp); sbs_strcat(&sbs, "> ...]");
} }
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
else if (cp == 's') { else if (cp == 's') {
targetp = targets + 2; targetp = targets + 2;
locp = LOC(lang, targetp->vars); locp = LOC(lang, targetp->vars);
bytes = (int)snprintf(bufp, size, " <%s>", locp); sbs_strcat(&sbs, " <");
sbs_strcat(&sbs, locp);
sbs_strcat(&sbs, ">");
if (*params == '+') { if (*params == '+') {
++params; ++params;
if (wrptr(&bufp, &size, bytes) != 0) sbs_strcat(&sbs, " [<");
WARN_STATIC_BUFFER(); sbs_strcat(&sbs, locp);
bytes = (int)snprintf(bufp, size, " [<%s> ...]", locp); sbs_strcat(&sbs, "> ...]");
} }
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
else if (cp == 'r') { else if (cp == 'r') {
bytes = (int)str_strlcpy(bufp, " <x> <y>", size); sbs_strcat(&sbs, " <x> <y>");
if (*params == '+') { if (*params == '+') {
++params; ++params;
if (wrptr(&bufp, &size, bytes) != 0) sbs_strcat(&sbs, " [<x> <y> ...]");
WARN_STATIC_BUFFER();
bytes = (int)str_strlcpy(bufp, " [<x> <y> ...]", size);
} }
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
else if (cp == 'b') { else if (cp == 'b') {
targetp = targets + 3; targetp = targets + 3;
locp = LOC(lang, targetp->vars); locp = LOC(lang, targetp->vars);
bytes = (int)snprintf(bufp, size, " <%s>", locp); sbs_strcat(&sbs, " <");
sbs_strcat(&sbs, locp);
sbs_strcat(&sbs, ">");
if (*params == '+') { if (*params == '+') {
++params; ++params;
if (wrptr(&bufp, &size, bytes) != 0) sbs_strcat(&sbs, " [<");
WARN_STATIC_BUFFER(); sbs_strcat(&sbs, locp);
bytes = (int)snprintf(bufp, size, " [<%s> ...]", locp); sbs_strcat(&sbs, "> ...]");
} }
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
else if (cp == 'k') { else if (cp == 'k') {
int i, maxparam = 0; int i, maxparam = 0;
@ -361,41 +361,35 @@ void nr_spell_syntax(struct stream *out, spellbook_entry * sbe, const struct loc
++maxparam; ++maxparam;
} }
if (!maxparam || maxparam > 1) { if (!maxparam || maxparam > 1) {
bytes = (int)str_strlcpy(bufp, " (", size); sbs_strcat(&sbs, " (");
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
i = 0; i = 0;
for (targetp = targets; targetp->flag; ++targetp) { for (targetp = targets; targetp->flag; ++targetp) {
if (!maxparam || sp->sptyp & targetp->flag) { if (!maxparam || sp->sptyp & targetp->flag) {
if (i++ != 0) { if (i++ != 0) {
bytes = (int)str_strlcpy(bufp, " |", size); sbs_strcat(&sbs, " |");
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
if (targetp->param && targetp->vars) { if (targetp->param && targetp->vars) {
locp = LOC(lang, targetp->vars); locp = LOC(lang, targetp->vars);
bytes = sbs_strcat(&sbs, " ");
(int)snprintf(bufp, size, " %s <%s>", parameters[targetp->param], sbs_strcat(&sbs, parameters[targetp->param]);
locp); sbs_strcat(&sbs, " <");
sbs_strcat(&sbs, locp);
sbs_strcat(&sbs, ">");
if (multi) { if (multi) {
if (wrptr(&bufp, &size, bytes) != 0) sbs_strcat(&sbs, " [<");
WARN_STATIC_BUFFER(); sbs_strcat(&sbs, locp);
bytes = (int)snprintf(bufp, size, " [<%s> ...]", locp); sbs_strcat(&sbs, "> ...]");
} }
} }
else { else {
bytes = sbs_strcat(&sbs, " ");
(int)snprintf(bufp, size, " %s", parameters[targetp->param]); sbs_strcat(&sbs, parameters[targetp->param]);
} }
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
} }
if (!maxparam || maxparam > 1) { if (!maxparam || maxparam > 1) {
bytes = (int)str_strlcpy(bufp, " )", size); sbs_strcat(&sbs, " )");
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
} }
else if (cp == 'i' || cp == 'c') { 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 == '?') { if (*params == '?') {
++params; ++params;
bytes = (int)snprintf(bufp, size, " [<%s>]", locp); sbs_strcat(&sbs, " [<");
sbs_strcat(&sbs, locp);
sbs_strcat(&sbs, ">]");
} }
else { 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 { else {
log_error("unknown spell parameter %c for spell %s", cp, sp->sname); 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) void nr_spell(struct stream *out, spellbook_entry * sbe, const struct locale *lang)
{ {
int bytes, k, itemanz, costtyp; 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, buf, 0, 0, 0);
paragraph(out, LOC(lang, "nr_spell_syntax"), 0, 0, 0); paragraph(out, LOC(lang, "nr_spell_syntax"), 0, 0, 0);
bufp = buf; nr_spell_syntax(buf, sizeof(buf), sbe, lang);
size = sizeof(buf) - 1; paragraph(out, buf, 2, 0, 0);
nr_spell_syntax(out, sbe, lang);
newline(out); newline(out);
} }

View file

@ -30,7 +30,7 @@ extern "C" {
void report_travelthru(struct stream *out, struct region * r, const struct faction * f); 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 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); void nr_spell(struct stream *out, struct spellbook_entry * sbe, const struct locale *lang);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -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) { static void check_spell_syntax(CuTest *tc, char *msg, spell_fixture *spell, char *syntax) {
stream strm;
char buf[1024]; char buf[1024];
char *linestart, *newline;
size_t len;
mstream_init(&strm); nr_spell_syntax(buf, sizeof(buf), spell->sbe, spell->lang);
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';
linestart = strtok(buf, "\n"); CuAssertStrEquals_Msg(tc, msg, syntax, buf);
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);
} }
static void test_write_spell_syntax(CuTest *tc) { static void test_write_spell_syntax(CuTest *tc) {
@ -262,54 +241,54 @@ static void test_write_spell_syntax(CuTest *tc) {
test_setup(); test_setup();
setup_spell_fixture(&spell); setup_spell_fixture(&spell);
check_spell_syntax(tc, "vanilla", &spell, " ZAUBERE \"Testzauber\""); check_spell_syntax(tc, "vanilla", &spell, "ZAUBERE Testzauber");
spell.sp->sptyp |= FARCASTING; 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; 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; spell.sp->sptyp = 0;
set_parameter(spell, "kc"); set_parameter(spell, "kc");
check_spell_syntax(tc, "kc", &spell, " ZAUBERE \"Testzauber\" ( REGION | EINHEIT <enr> | SCHIFF <snr> | BURG <bnr> )"); check_spell_syntax(tc, "kc", &spell, "ZAUBERE Testzauber ( REGION | EINHEIT <enr> | SCHIFF <snr> | BURG <bnr> )");
spell.sp->sptyp |= BUILDINGSPELL; spell.sp->sptyp |= BUILDINGSPELL;
check_spell_syntax(tc, "kc typed", &spell, " ZAUBERE \"Testzauber\" BURG <bnr>"); check_spell_syntax(tc, "kc typed", &spell, "ZAUBERE Testzauber BURG <bnr>");
spell.sp->sptyp = 0; spell.sp->sptyp = 0;
set_parameter(spell, "b"); set_parameter(spell, "b");
check_spell_syntax(tc, "b", &spell, " ZAUBERE \"Testzauber\" <bnr>"); check_spell_syntax(tc, "b", &spell, "ZAUBERE Testzauber <bnr>");
set_parameter(spell, "s"); set_parameter(spell, "s");
check_spell_syntax(tc, "s", &spell, " ZAUBERE \"Testzauber\" <snr>"); check_spell_syntax(tc, "s", &spell, "ZAUBERE Testzauber <snr>");
set_parameter(spell, "s+"); set_parameter(spell, "s+");
check_spell_syntax(tc, "s+", &spell, " ZAUBERE \"Testzauber\" <snr> [<snr> ...]"); check_spell_syntax(tc, "s+", &spell, "ZAUBERE Testzauber <snr> [<snr> ...]");
set_parameter(spell, "u"); set_parameter(spell, "u");
check_spell_syntax(tc, "u", &spell, " ZAUBERE \"Testzauber\" <enr>"); check_spell_syntax(tc, "u", &spell, "ZAUBERE Testzauber <enr>");
set_parameter(spell, "r"); set_parameter(spell, "r");
check_spell_syntax(tc, "r", &spell, " ZAUBERE \"Testzauber\" <x> <y>"); check_spell_syntax(tc, "r", &spell, "ZAUBERE Testzauber <x> <y>");
set_parameter(spell, "bc"); set_parameter(spell, "bc");
free(spell.sp->syntax); free(spell.sp->syntax);
spell.sp->syntax = str_strdup("hodor"); spell.sp->syntax = str_strdup("hodor");
check_spell_syntax(tc, "bc hodor", &spell, " ZAUBERE \"Testzauber\" <bnr> <Hodor>"); check_spell_syntax(tc, "bc hodor", &spell, "ZAUBERE Testzauber <bnr> <Hodor>");
free(spell.sp->syntax); free(spell.sp->syntax);
spell.sp->syntax = 0; spell.sp->syntax = 0;
set_parameter(spell, "c?"); set_parameter(spell, "c?");
free(spell.sp->syntax); free(spell.sp->syntax);
spell.sp->syntax = str_strdup("hodor"); spell.sp->syntax = str_strdup("hodor");
check_spell_syntax(tc, "c?", &spell, " ZAUBERE \"Testzauber\" [<Hodor>]"); check_spell_syntax(tc, "c?", &spell, "ZAUBERE Testzauber [<Hodor>]");
free(spell.sp->syntax); free(spell.sp->syntax);
spell.sp->syntax = 0; spell.sp->syntax = 0;
set_parameter(spell, "kc+"); set_parameter(spell, "kc+");
check_spell_syntax(tc, "kc+", &spell, check_spell_syntax(tc, "kc+", &spell,
" ZAUBERE \"Testzauber\" ( REGION | EINHEIT <enr> [<enr> ...] | SCHIFF <snr>\n [<snr> ...] | BURG <bnr> [<bnr> ...] )"); "ZAUBERE Testzauber ( REGION | EINHEIT <enr> [<enr> ...] | SCHIFF <snr> [<snr> ...] | BURG <bnr> [<bnr> ...] )");
cleanup_spell_fixture(&spell); cleanup_spell_fixture(&spell);
test_teardown(); test_teardown();