From 34ce9a05730d67b95a31e01f44e63293277a5889 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 28 Jun 2014 10:37:40 -0700 Subject: [PATCH] JSON configuration can read strings, with test. --- src/kernel/jsonconf.c | 32 ++++++++++++++++++++++++++++++++ src/kernel/jsonconf.test.c | 18 ++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 415684e1b..52f34a754 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -329,6 +329,35 @@ void json_ships(cJSON *json) { } } +void json_locale(cJSON *json, struct locale *lang) { + cJSON *child; + if (json->type!=cJSON_Object) { + log_error_n("strings is not a json object: %d", json->type); + return; + } + for (child=json->child;child;child=child->next) { + if (child->type==cJSON_String) { + locale_setstring(lang, child->string, child->valuestring); + } + } +} + +void json_strings(cJSON *json) { + cJSON *child; + if (json->type!=cJSON_Object) { + log_error_n("strings is not a json object: %d", json->type); + return; + } + for (child=json->child;child;child=child->next) { + if ((child->type==cJSON_Object)) { + struct locale *lang = get_or_create_locale(child->string); + json_locale(child, lang); + } else { + log_error_n("strings for locale `%s` are not a json object: %d", child->string, child->type); + } + } +} + static void json_direction(cJSON *json, struct locale *lang) { cJSON *child; if (json->type!=cJSON_Object) { @@ -476,6 +505,9 @@ void json_config(cJSON *json) { else if (strcmp(child->string, "ships")==0) { json_ships(child); } + else if (strcmp(child->string, "strings")==0) { + json_strings(child); + } else if (strcmp(child->string, "directions")==0) { json_directions(child); } diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 52a4746f0..7dba7e145 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -262,6 +262,23 @@ static void test_keywords(CuTest * tc) test_cleanup(); } +static void test_strings(CuTest * tc) +{ + const char * data = "{\"strings\": { \"de\" : { \"move\" : \"NACH\", \"study\" : \"LERNEN\" }}}"; + const struct locale * lang; + + cJSON *json = cJSON_Parse(data); + CuAssertPtrNotNull(tc, json); + + test_cleanup(); + lang = get_or_create_locale("de"); + CuAssertPtrNotNull(tc, lang); + CuAssertPtrEquals(tc, NULL, (void *)locale_string(lang, "move")); + json_config(json); + CuAssertStrEquals(tc, "NACH", locale_string(lang, "move")); + CuAssertStrEquals(tc, "LERNEN", locale_string(lang, "study")); +} + CuSuite *get_jsonconf_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -273,6 +290,7 @@ CuSuite *get_jsonconf_suite(void) SUITE_ADD_TEST(suite, test_buildings); SUITE_ADD_TEST(suite, test_terrains); SUITE_ADD_TEST(suite, test_races); + SUITE_ADD_TEST(suite, test_strings); SUITE_ADD_TEST(suite, test_flags); return suite; }