From 9ca945cb2c7ef0ba42e81cf933c7687faf960429 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 17 May 2018 16:53:56 +0200 Subject: [PATCH] add a string escaping function. start some work on nrmessage.c. --- res/translations/strings.de.po | 5 +++- src/util/nrmessage.c | 7 ++++-- src/util/strings.c | 35 ++++++++++++++++++++++++++++ src/util/strings.h | 4 +++- src/util/strings.test.c | 14 ++++++++++++ src/xmlreader.c | 42 +++++++++++++++++----------------- 6 files changed, 82 insertions(+), 25 deletions(-) diff --git a/res/translations/strings.de.po b/res/translations/strings.de.po index faf8a7982..d739a0b6d 100644 --- a/res/translations/strings.de.po +++ b/res/translations/strings.de.po @@ -3,7 +3,10 @@ msgstr "Rostiges Kettenhemd" msgctxt "spellinfo" msgid "destroy_magic" -msgstr "Dieser Zauber ermöglicht dem Magier, Verzauberungen einer Einheit, eines Schiffes, Gebäudes oder auch der Region aufzulösen." +msgstr "" +"Dieser Zauber ermöglicht dem Magier, Verzauberungen " +"einer Einheit, eines Schiffes, Gebäudes oder auch " +"der Region aufzulösen." msgctxt "spell" msgid "shadowknights" diff --git a/src/util/nrmessage.c b/src/util/nrmessage.c index 0f538639f..f74d6ab51 100644 --- a/src/util/nrmessage.c +++ b/src/util/nrmessage.c @@ -32,7 +32,10 @@ static nrmessage_type *nrtypes[NRT_MAXHASH]; const char *nrt_string(const struct nrmessage_type *type) { - return type->string; + if (type->string) { + return type->string; + } + return locale_get(type->lang, type->mtype->name); } nrmessage_type *nrt_find(const struct locale * lang, @@ -148,7 +151,7 @@ size_t size, const void *userdata) if (nrt) { const char *m = - translate(nrt->string, userdata, nrt->vars, msg->parameters); + translate(nrt_string(nrt), userdata, nrt->vars, msg->parameters); if (m) { return str_strlcpy((char *)buffer, m, size); } diff --git a/src/util/strings.c b/src/util/strings.c index 4959fba6c..38cb020f9 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -270,3 +270,38 @@ void sbs_strcpy(struct sbstring *sbs, const char *str) } sbs->end = sbs->begin + len; } + +char *str_unescape(char *str) { + char *read = str, *write = str; + while (*read) { + char * pos = strchr(read, '\\'); + if (pos) { + size_t len = pos - read; + memmove(write, read, len); + write += len; + read += (len + 1); + switch (read[0]) { + case 'r': + *write++ = '\r'; + break; + case 'n': + *write++ = '\n'; + break; + case 't': + *write++ = '\t'; + break; + default: + *write++ = read[0]; + } + *write = 0; + ++read; + } + else { + size_t len = strlen(read); + memmove(write, read, len); + write[len] = 0; + break; + } + } + return str; +} diff --git a/src/util/strings.h b/src/util/strings.h index b50f27a30..fdf4a6c7c 100644 --- a/src/util/strings.h +++ b/src/util/strings.h @@ -26,13 +26,15 @@ extern "C" { #endif void str_replace(char *buffer, size_t size, const char *tmpl, const char *var, const char *value); - const char *str_escape(const char *str, char *buffer, size_t len); unsigned int str_hash(const char *s); size_t str_slprintf(char * dst, size_t size, const char * format, ...); size_t str_strlcpy(char *dst, const char *src, size_t len); size_t str_strlcat(char *dst, const char *src, size_t len); char *str_strdup(const char *s); + const char *str_escape(const char *str, char *buffer, size_t len); + char *str_unescape(char *str); + unsigned int jenkins_hash(unsigned int a); unsigned int wang_hash(unsigned int a); diff --git a/src/util/strings.test.c b/src/util/strings.test.c index 89c880a70..a04d0fe53 100644 --- a/src/util/strings.test.c +++ b/src/util/strings.test.c @@ -9,6 +9,19 @@ #include #include "strings.h" +static void test_str_unescape(CuTest * tc) +{ + char scratch[64]; + + strcpy(scratch, "1234 5678"); + str_unescape(scratch); + CuAssertStrEquals(tc, "1234 5678", scratch); + + strcpy(scratch, "\\\"\\\\\\n\\t\\r\\a"); + str_unescape(scratch); + CuAssertStrEquals(tc, "\"\\\n\t\ra", scratch); +} + static void test_str_escape(CuTest * tc) { char scratch[64]; @@ -122,6 +135,7 @@ CuSuite *get_strings_suite(void) CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_str_hash); SUITE_ADD_TEST(suite, test_str_escape); + SUITE_ADD_TEST(suite, test_str_unescape); SUITE_ADD_TEST(suite, test_str_replace); SUITE_ADD_TEST(suite, test_str_slprintf); SUITE_ADD_TEST(suite, test_str_strlcat); diff --git a/src/xmlreader.c b/src/xmlreader.c index c3f3ac5fd..afb421ce2 100644 --- a/src/xmlreader.c +++ b/src/xmlreader.c @@ -10,12 +10,23 @@ This program may not be used, modified or distributed without prior permission by the authors of Eressea. */ +#ifdef _MSC_VER #include -#include +#endif #include "xmlreader.h" +#include "alchemy.h" +#include "guard.h" +#include "keyword.h" +#include "move.h" +#include "prefix.h" + +#include "attributes/attributes.h" +#include "modules/score.h" + #include "kernel/building.h" +#include "kernel/calendar.h" #include "kernel/item.h" #include "kernel/messages.h" #include "kernel/race.h" @@ -27,26 +38,15 @@ without prior permission by the authors of Eressea. #include "kernel/spell.h" #include "kernel/spellbook.h" -#include "alchemy.h" -#include "kernel/calendar.h" -#include "guard.h" -#include "keyword.h" -#include "move.h" -#include "prefix.h" - -#include -#include - -/* util includes */ -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "util/attrib.h" +#include "util/crmessage.h" +#include "util/functions.h" +#include "util/language.h" +#include "util/log.h" +#include "util/message.h" +#include "util/nrmessage.h" +#include "util/strings.h" +#include "util/xml.h" /* libxml includes */ #include