begin binding locales (need to init_locales in the right place, still).

fix indentation in some places.
add some assertions.
This commit is contained in:
Enno Rehling 2014-06-14 07:52:32 -07:00
parent 24dc006e43
commit 812cb98dff
13 changed files with 310 additions and 93 deletions

View File

@ -265,6 +265,10 @@
<F N="../src/bind_faction.c"/>
<F N="../src/bind_gmtool.c"/>
<F N="../src/bind_hashtable.c"/>
<F N="../src/bind_locale.c"/>
<F N="../src/bind_locale.h"/>
<F N="../src/bind_log.c"/>
<F N="../src/bind_log.h"/>
<F N="../src/bind_message.c"/>
<F N="../src/bind_monsters.c"/>
<F N="../src/bind_process.c"/>
@ -281,6 +285,9 @@
<F N="../src/gmtool.c"/>
<F N="../src/helpers.c"/>
<F N="../src/listbox.c"/>
<F N="../src/locale.pkg"/>
<F N="../src/locale.pkg.c"/>
<F N="../src/log.pkg.c"/>
<F N="../src/main.c"/>
<F N="../src/process.pkg.c"/>
<F N="../src/settings.pkg.c"/>

View File

@ -278,11 +278,12 @@
<Folder
Name="Other Files"
Filters="">
<F N="../scripts/tests/config.lua"/>
<F N="../scripts/tests/rules.lua"/>
<F N="../scripts/runtests.lua"/>
<F N="../scripts/tests/settings.lua"/>
<F N="../scripts/tests/ships.lua"/>
<F N="../tests/config.lua"/>
<F N="../tests/init.lua"/>
<F N="../tests/locale.lua"/>
<F N="../tests/regions.lua"/>
<F N="../tests/settings.lua"/>
<F N="../tests/ships.lua"/>
</Folder>
</Files>
</Project>

View File

@ -49,6 +49,7 @@ ENDMACRO(TOLUA_BINDING)
IF(NOT MSVC)
TOLUA_BINDING(log.pkg util/log.h)
TOLUA_BINDING(locale.pkg bind_locale.h)
TOLUA_BINDING(config.pkg bind_config.h)
TOLUA_BINDING(process.pkg bind_process.h)
TOLUA_BINDING(eressea.pkg bind_eressea.h)
@ -87,12 +88,14 @@ set(SERVER_SRC
console.c
helpers.c
config.pkg.c
locale.pkg.c
log.pkg.c
process.pkg.c
eressea.pkg.c
settings.pkg.c
bind_building.c
bind_config.c
bind_locale.c
bind_eressea.c
bind_faction.c
bind_hashtable.c

21
src/bind_locale.c Normal file
View File

@ -0,0 +1,21 @@
#include "bind_locale.h"
#include "util/language.h"
void locale_create(const char *lang) {
make_locale(lang);
}
void locale_set(const char *lang, const char *key, const char *str) {
struct locale *loc = find_locale(lang);
if (loc) {
locale_setstring(loc, key, str);
}
}
const char * locale_get(const char *lang, const char *key) {
struct locale *loc = find_locale(lang);
if (loc) {
return locale_getstring(loc, key);
}
return 0;
}

15
src/bind_locale.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef H_BIND_LOCALE_H
#define H_BIND_LOCALE_H
#ifdef __cplusplus
extern "C" {
#endif
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);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -131,7 +131,7 @@ static int tolua_ship_create(lua_State * L)
tolua_pushusertype(L, (void *)sh, TOLUA_CAST "ship");
return 1;
} else {
log_error("Unkown ship type '%s'\n", sname);
log_error("Unknown ship type '%s'\n", sname);
}
}
return 0;

View File

@ -86,6 +86,7 @@ TOLUA_PKG(eressea);
TOLUA_PKG(process);
TOLUA_PKG(settings);
TOLUA_PKG(config);
TOLUA_PKG(locale);
TOLUA_PKG(log);
int log_lua_error(lua_State * L)
@ -780,16 +781,15 @@ static int config_get_buildings(lua_State * L)
static int config_get_locales(lua_State * L)
{
const struct locale *lang;
int i = 0, n = 0;
for (lang = locales; lang; lang = nextlocale(lang))
++n;
lua_createtable(L, n, 0);
for (lang = locales; lang; lang = nextlocale(lang)) {
tolua_pushstring(L, TOLUA_CAST locale_name(lang));
lua_rawseti(L, -2, ++i);
}
return 1;
const struct locale *lang;
int i = 0, n = 0;
for (lang = locales; lang; lang = nextlocale(lang)) ++n;
lua_createtable(L, n, 0);
for (lang = locales; lang; lang = nextlocale(lang)) {
tolua_pushstring(L, TOLUA_CAST locale_name(lang));
lua_rawseti(L, -2, ++i);
}
return 1;
}
static int config_get_resource(lua_State * L)
@ -1061,6 +1061,7 @@ int tolua_bindings_open(lua_State * L)
tolua_process_open(L);
tolua_settings_open(L);
tolua_config_open(L);
tolua_locale_open(L);
tolua_log_open(L);
/* register user types */

View File

@ -1351,6 +1351,8 @@ keyword_t findkeyword(const char *s, const struct locale * lang)
keyword_t result = NOKEYWORD;
char buffer[64];
assert(lang);
assert(s);
while (*s == '@') ++s;
if (*s) {
@ -1361,6 +1363,7 @@ keyword_t findkeyword(const char *s, const struct locale * lang)
const void * match;
void **tokens = get_translations(lang, UT_KEYWORDS);
critbit_tree *cb = (critbit_tree *)*tokens;
assert(cb);
if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) {
cb_get_kv(match, &i, sizeof(int));
result = (keyword_t)i;
@ -1965,12 +1968,13 @@ static void init_translations(const struct locale *lang, int ut, const char * (*
for (i = 0; i != maxstrings; ++i) {
const char * s = string_cb(i);
const char * key = s ? locale_string(lang, s) : 0;
key = key ? key : s;
if (key) {
char * str = transliterate(buffer, sizeof(buffer)-sizeof(int), key);
if (str) {
critbit_tree * cb = (critbit_tree *)*tokens;
size_t len = strlen(str);
if (!cb) {
size_t len = strlen(str);
if (!cb) {
*tokens = cb = (critbit_tree *)calloc(1, sizeof(critbit_tree *));
}
len = cb_new_kv(str, len, &i, sizeof(int), buffer);
@ -2002,61 +2006,59 @@ static const char * skill_key(int sk)
static void init_locale(const struct locale *lang)
{
variant var;
int i;
const struct race *rc;
const terrain_type *terrain;
void **tokens;
variant var;
int i;
const struct race *rc;
const terrain_type *terrain;
void **tokens;
tokens = get_translations(lang, UT_MAGIC);
if (tokens) {
const char *str = get_param(global.parameters, "rules.magic.playerschools");
char *sstr, *tok;
if (str == NULL) {
str = "gwyrrd illaun draig cerddor tybied";
tokens = get_translations(lang, UT_MAGIC);
if (tokens) {
const char *str = get_param(global.parameters, "rules.magic.playerschools");
char *sstr, *tok;
if (str == NULL) {
str = "gwyrrd illaun draig cerddor tybied";
}
sstr = _strdup(str);
tok = strtok(sstr, " ");
while (tok) {
for (i = 0; i != MAXMAGIETYP; ++i) {
if (strcmp(tok, magic_school[i]) == 0) break;
}
assert(i != MAXMAGIETYP);
var.i = i;
addtoken(tokens, LOC(lang, mkname("school", tok)), var);
tok = strtok(NULL, " ");
}
free(sstr);
}
sstr = _strdup(str);
tok = strtok(sstr, " ");
while (tok) {
for (i = 0; i != MAXMAGIETYP; ++i) {
if (strcmp(tok, magic_school[i]) == 0)
break;
}
assert(i != MAXMAGIETYP);
var.i = i;
addtoken(tokens, LOC(lang, mkname("school", tok)), var);
tok = strtok(NULL, " ");
tokens = get_translations(lang, UT_DIRECTIONS);
init_directions(tokens, lang);
tokens = get_translations(lang, UT_RACES);
for (rc = races; rc; rc = rc->next) {
var.v = (void *)rc;
addtoken(tokens, LOC(lang, rc_name(rc, 1)), var);
addtoken(tokens, LOC(lang, rc_name(rc, 0)), var);
}
free(sstr);
}
tokens = get_translations(lang, UT_DIRECTIONS);
init_directions(tokens, lang);
init_translations(lang, UT_PARAMS, parameter_key, MAXPARAMS);
init_translations(lang, UT_SKILLS, skill_key, MAXSKILLS);
init_translations(lang, UT_KEYWORDS, keyword_key, MAXKEYWORDS);
tokens = get_translations(lang, UT_RACES);
for (rc = races; rc; rc = rc->next) {
var.v = (void *)rc;
addtoken(tokens, LOC(lang, rc_name(rc, 1)), var);
addtoken(tokens, LOC(lang, rc_name(rc, 0)), var);
}
tokens = get_translations(lang, UT_OPTIONS);
for (i = 0; i != MAXOPTIONS; ++i) {
var.i = i;
if (options[i]) addtoken(tokens, LOC(lang, options[i]), var);
}
init_translations(lang, UT_PARAMS, parameter_key, MAXPARAMS);
init_translations(lang, UT_SKILLS, skill_key, MAXSKILLS);
init_translations(lang, UT_KEYWORDS, keyword_key, MAXKEYWORDS);
tokens = get_translations(lang, UT_OPTIONS);
for (i = 0; i != MAXOPTIONS; ++i) {
var.i = i;
if (options[i])
addtoken(tokens, LOC(lang, options[i]), var);
}
tokens = get_translations(lang, UT_TERRAINS);
for (terrain = terrains(); terrain != NULL; terrain = terrain->next) {
var.v = (void *)terrain;
addtoken(tokens, LOC(lang, terrain->_name), var);
}
tokens = get_translations(lang, UT_TERRAINS);
for (terrain = terrains(); terrain != NULL; terrain = terrain->next) {
var.v = (void *)terrain;
addtoken(tokens, LOC(lang, terrain->_name), var);
}
}
typedef struct param {
@ -2179,12 +2181,14 @@ const char *localenames[] = {
void init_locales(void)
{
int l;
for (l = 0; localenames[l]; ++l) {
const struct locale *lang = find_locale(localenames[l]);
if (lang)
init_locale(lang);
}
int l;
for (l = 0; localenames[l]; ++l) {
const struct locale *lang = find_locale(localenames[l]);
if (!lang) {
lang = make_locale(localenames[l]);
}
init_locale(lang);
}
}
/* TODO: soll hier weg */

View File

@ -338,27 +338,29 @@ order *create_order(keyword_t kwd, const struct locale * lang,
order *parse_order(const char *s, const struct locale * lang)
{
while (*s && !isalnum(*(unsigned char *)s) && !ispunct(*(unsigned char *)s))
++s;
if (*s != 0) {
keyword_t kwd;
const char *sptr;
int persistent = 0;
assert(lang);
assert(s);
while (*s && !isalnum(*(unsigned char *)s) && !ispunct(*(unsigned char *)s)) {
++s;
}
if (*s != 0) {
keyword_t kwd;
const char *sptr;
int persistent = 0;
while (*s == '@') {
persistent = 1;
++s;
while (*s == '@') {
persistent = 1;
++s;
}
sptr = s;
kwd = findkeyword(parse_token(&sptr), lang);
if (kwd != NOKEYWORD) {
while (isxspace(*(unsigned char *)sptr)) ++sptr;
s = sptr;
}
return create_order_i(kwd, s, persistent, lang);
}
sptr = s;
kwd = findkeyword(parse_token(&sptr), lang);
if (kwd != NOKEYWORD) {
while (isxspace(*(unsigned char *)sptr))
++sptr;
s = sptr;
}
return create_order_i(kwd, s, persistent, lang);
}
return NULL;
return NULL;
}
/**

9
src/locale.pkg Normal file
View File

@ -0,0 +1,9 @@
$#include "bind_locale.h"
module eressea {
module locale {
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);
}
}

139
src/locale.pkg.c Normal file
View File

@ -0,0 +1,139 @@
/*
** Lua binding: locale
*/
#include "tolua.h"
#ifndef __cplusplus
#include <stdlib.h>
#endif
#ifdef __cplusplus
extern "C" int tolua_bnd_takeownership (lua_State* L); // from tolua_map.c
#else
int tolua_bnd_takeownership (lua_State* L); /* from tolua_map.c */
#endif
#include <string.h>
/* Exported function */
TOLUA_API int tolua_locale_open (lua_State* tolua_S);
LUALIB_API int luaopen_locale (lua_State* tolua_S);
#include "bind_locale.h"
/* function to register type */
static void tolua_reg_types (lua_State* tolua_S)
{
}
/* function: locale_create */
static int tolua_locale_eressea_locale_create00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isstring(tolua_S,1,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,2,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const char* lang = ((const char*) tolua_tostring(tolua_S,1,0));
{
locale_create(lang);
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err);
return 0;
#endif
}
/* function: locale_set */
static int tolua_locale_eressea_locale_set00(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_isstring(tolua_S,3,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,4,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const char* lang = ((const char*) tolua_tostring(tolua_S,1,0));
const char* key = ((const char*) tolua_tostring(tolua_S,2,0));
const char* str = ((const char*) tolua_tostring(tolua_S,3,0));
{
locale_set(lang,key,str);
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'set'.",&tolua_err);
return 0;
#endif
}
/* function: locale_get */
static int tolua_locale_eressea_locale_get00(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* key = ((const char*) tolua_tostring(tolua_S,2,0));
{
const char* tolua_ret = (const char*) locale_get(lang,key);
tolua_pushstring(tolua_S,(const char*)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'get'.",&tolua_err);
return 0;
#endif
}
/* Open lib function */
LUALIB_API int luaopen_locale (lua_State* tolua_S)
{
tolua_open(tolua_S);
tolua_reg_types(tolua_S);
tolua_module(tolua_S,NULL,0);
tolua_beginmodule(tolua_S,NULL);
tolua_module(tolua_S,"eressea",0);
tolua_beginmodule(tolua_S,"eressea");
tolua_module(tolua_S,"locale",0);
tolua_beginmodule(tolua_S,"locale");
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_endmodule(tolua_S);
tolua_endmodule(tolua_S);
tolua_endmodule(tolua_S);
return 1;
}
/* Open tolua function */
TOLUA_API int tolua_locale_open (lua_State* tolua_S)
{
lua_pushcfunction(tolua_S, luaopen_locale);
lua_pushstring(tolua_S, "locale");
lua_call(tolua_S, 1, 0);
return 1;
}

View File

@ -8,7 +8,6 @@ end
function test_read_race()
local f
eressea.free_game()
f = faction.create("orc@example.com", "orc", "en")
assert_equal(nil, f)
assert_not_nil(eressea.config)
@ -19,7 +18,6 @@ end
function test_read_ship()
local s
eressea.free_game()
s = ship.create(nil, "boat")
assert_equal(nil, s)
assert_not_nil(eressea.config)

17
tests/locale.lua Normal file
View File

@ -0,0 +1,17 @@
require "lunit"
module("tests.eressea.locale", package.seeall, lunit.testcase )
function setup()
eressea.free_game()
end
function test_get_set()
local loc = "en"
assert_not_nil(eressea.locale)
eressea.locale.create(loc)
assert_equal(nil, eressea.locale.get(loc, "move"))
eressea.locale.set(loc, "move", "MOVE")
assert_equal("MOVE", eressea.locale.get(loc, "move"))
end