From 55c15c1905d270944744f9b896d6b8f35be77967 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 27 Nov 2015 15:14:25 +0100 Subject: [PATCH] add a lua binding to convert user-language strings to directions --- scripts/tests/common.lua | 8 ++++++++ src/bind_locale.c | 9 +++++++++ src/bind_locale.h | 1 + src/locale.pkg | 1 + src/locale.pkg.c | 31 ++++++++++++++++++++++++++++++- 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index decd0c14b..43c77f93f 100644 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -34,6 +34,14 @@ function setup() eressea.settings.set("study.random_progress", "0") end +function test_locales() + assert_equal(2, eressea.locale.direction("de", "Ost")) + assert_equal(5, eressea.locale.direction("de", "westen")) + assert_equal(4, eressea.locale.direction("de", "sw")) + assert_equal(-1, eressea.locale.direction("de", "foo")) + assert_equal(-1, eressea.locale.direction("foo", "sw")) +end + function test_flags() local r = region.create(0, 0, "plain") local f = faction.create("flags@eressea.de", "halfling", "de") diff --git a/src/bind_locale.c b/src/bind_locale.c index a4c2ac0f0..992f140b7 100644 --- a/src/bind_locale.c +++ b/src/bind_locale.c @@ -1,5 +1,6 @@ #include "bind_locale.h" #include "util/language.h" +#include "direction.h" void locale_create(const char *lang) { get_or_create_locale(lang); @@ -19,3 +20,11 @@ const char * locale_get(const char *lang, const char *key) { } return 0; } + +int locale_direction(const char *lang, const char *str) { + struct locale *loc = get_locale(lang); + if (loc) { + return get_direction(str, loc); + } + return NODIRECTION; +} diff --git a/src/bind_locale.h b/src/bind_locale.h index 87c0eb742..7391c2b05 100644 --- a/src/bind_locale.h +++ b/src/bind_locale.h @@ -7,6 +7,7 @@ extern "C" { void locale_create(const char *lang); void locale_set(const char *lang, const char *key, const char *str); const char * locale_get(const char *lang, const char *key); + int locale_direction(const char *lang, const char *str); #ifdef __cplusplus } diff --git a/src/locale.pkg b/src/locale.pkg index 9a2c420de..b14dc9261 100644 --- a/src/locale.pkg +++ b/src/locale.pkg @@ -7,5 +7,6 @@ module eressea { void locale_create @ create(const char *lang); void locale_set @ set(const char *lang, const char *key, const char *str); const char * locale_get @ get(const char *lang, const char *key); + int locale_direction @ direction(const char *lang, const char *str); } } diff --git a/src/locale.pkg.c b/src/locale.pkg.c index 34517584f..1792b742e 100644 --- a/src/locale.pkg.c +++ b/src/locale.pkg.c @@ -20,7 +20,6 @@ LUALIB_API int luaopen_locale (lua_State* tolua_S); #undef tolua_reg_types #define tolua_reg_types tolua_reg_types_locale -#include "bind_tolua.h" #include "bind_locale.h" /* function to register type */ @@ -113,6 +112,35 @@ static int tolua_locale_eressea_locale_get00(lua_State* tolua_S) #endif } +/* function: locale_direction */ +static int tolua_locale_eressea_locale_direction00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isstring(tolua_S,1,0,&tolua_err) || + !tolua_isstring(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char* lang = ((const char*) tolua_tostring(tolua_S,1,0)); + const char* str = ((const char*) tolua_tostring(tolua_S,2,0)); + { + int tolua_ret = (int) locale_direction(lang,str); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'direction'.",&tolua_err); + return 0; +#endif +} + /* Open lib function */ LUALIB_API int luaopen_locale (lua_State* tolua_S) { @@ -127,6 +155,7 @@ LUALIB_API int luaopen_locale (lua_State* tolua_S) tolua_function(tolua_S,"create",tolua_locale_eressea_locale_create00); tolua_function(tolua_S,"set",tolua_locale_eressea_locale_set00); tolua_function(tolua_S,"get",tolua_locale_eressea_locale_get00); + tolua_function(tolua_S,"direction",tolua_locale_eressea_locale_direction00); tolua_endmodule(tolua_S); tolua_endmodule(tolua_S); tolua_endmodule(tolua_S);