# HG changeset patch # User Paul Boddie # Date 1481476052 -3600 # Node ID 553d14eaf82c71eb7d10f0fe8da3990e3a6c0364 # Parent 0613a74e6acae0f4e28479a7b8fc5b46793da1ef Added a module providing locale initialisation, access and update functions. diff -r 0613a74e6aca -r 553d14eaf82c lib/locale.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/locale.py Sun Dec 11 18:07:32 2016 +0100 @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +""" +Locale functions. + +Copyright (C) 2016 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +from __builtins__.types import check_int, check_string +from native import getlocale as _getlocale, setlocale as _setlocale + +LC_CTYPE = 0 +LC_NUMERIC = 1 +LC_TIME = 2 +LC_COLLATE = 3 +LC_MONETARY = 4 +LC_MESSAGES = 5 +LC_ALL = 6 + +def setlocale(category, value): + + "Set the locale for 'category' to 'value'." + + check_int(category) + check_string(value) + return _setlocale(category, value) + +def getlocale(category=LC_CTYPE): + + "Return the locale value for 'category'." + + check_int(category) + return _getlocale(category) + +def initlocale(category=LC_CTYPE): + + "Initialise the locale for 'category' from the environment." + + check_int(category) + return _setlocale(category, "") + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 0613a74e6aca -r 553d14eaf82c lib/native/__init__.py --- a/lib/native/__init__.py Sun Dec 11 01:49:54 2016 +0100 +++ b/lib/native/__init__.py Sun Dec 11 18:07:32 2016 +0100 @@ -36,6 +36,8 @@ from native.list import list_init, list_setsize, list_append, list_concat, \ list_len, list_nonempty, list_element, list_setelement +from native.locale import getlocale, setlocale + from native.program import get_using from native.str import str_add, str_eq, str_gt, str_lt, str_len, str_nonempty, \ diff -r 0613a74e6aca -r 553d14eaf82c lib/native/locale.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/native/locale.py Sun Dec 11 18:07:32 2016 +0100 @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +""" +Native library functions for locales. + +None of these are actually defined here. Instead, native implementations are +substituted when each program is built. It is, however, important to declare +non-core exceptions used by the native functions because they need to be +identified as being needed by the program. + +Copyright (C) 2016 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +def getlocale(category): pass +def setlocale(category, value): pass + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 0613a74e6aca -r 553d14eaf82c templates/native/locale.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/native/locale.c Sun Dec 11 18:07:32 2016 +0100 @@ -0,0 +1,67 @@ +/* Native functions for locale handling. + +Copyright (C) 2016 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +*/ + +#include /* setlocale */ +#include "native/common.h" +#include "types.h" +#include "exceptions.h" +#include "ops.h" +#include "progconsts.h" +#include "progops.h" +#include "progtypes.h" +#include "main.h" + +/* Locales. */ + +__attr __fn_native_locale_getlocale(__attr __args[]) +{ + __attr * const category = &__args[1]; + /* category interpreted as int */ + int cat = __load_via_object(category->value, __pos___data__).intvalue; + char *result; + + result = setlocale(cat, NULL); + + if (result == NULL) + return __builtins___none_None; + else + return __new_str(result); +} + +__attr __fn_native_locale_setlocale(__attr __args[]) +{ + __attr * const category = &__args[1]; + __attr * const value = &__args[2]; + /* category interpreted as int */ + int cat = __load_via_object(category->value, __pos___data__).intvalue; + /* value interpreted as string */ + char *s = __load_via_object(value->value, __pos___data__).strvalue, *result; + + result = setlocale(cat, s); + + if (result == NULL) + return __builtins___none_None; + else + return __new_str(result); +} + +/* Module initialisation. */ + +void __main_native_locale() +{ +} diff -r 0613a74e6aca -r 553d14eaf82c templates/native/locale.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/native/locale.h Sun Dec 11 18:07:32 2016 +0100 @@ -0,0 +1,33 @@ +/* Native functions for locale handling. + +Copyright (C) 2016 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +*/ + +#ifndef __NATIVE_LOCALE_H__ +#define __NATIVE_LOCALE_H__ + +#include "types.h" + +/* Input/output. */ + +__attr __fn_native_locale_getlocale(__attr __args[]); +__attr __fn_native_locale_setlocale(__attr __args[]); + +/* Module initialisation. */ + +void __main_native_locale(); + +#endif /* __NATIVE_LOCALE_H__ */ diff -r 0613a74e6aca -r 553d14eaf82c tests/locale.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/locale.py Sun Dec 11 18:07:32 2016 +0100 @@ -0,0 +1,7 @@ +import locale + +print locale.getlocale() +print locale.initlocale() +print locale.getlocale() +print locale.setlocale(locale.LC_CTYPE, "en_GB.UTF-8") +print locale.getlocale()