1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lib/locale.py Sun Dec 11 18:07:32 2016 +0100
1.3 @@ -0,0 +1,55 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Locale functions.
1.8 +
1.9 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from __builtins__.types import check_int, check_string
1.26 +from native import getlocale as _getlocale, setlocale as _setlocale
1.27 +
1.28 +LC_CTYPE = 0
1.29 +LC_NUMERIC = 1
1.30 +LC_TIME = 2
1.31 +LC_COLLATE = 3
1.32 +LC_MONETARY = 4
1.33 +LC_MESSAGES = 5
1.34 +LC_ALL = 6
1.35 +
1.36 +def setlocale(category, value):
1.37 +
1.38 + "Set the locale for 'category' to 'value'."
1.39 +
1.40 + check_int(category)
1.41 + check_string(value)
1.42 + return _setlocale(category, value)
1.43 +
1.44 +def getlocale(category=LC_CTYPE):
1.45 +
1.46 + "Return the locale value for 'category'."
1.47 +
1.48 + check_int(category)
1.49 + return _getlocale(category)
1.50 +
1.51 +def initlocale(category=LC_CTYPE):
1.52 +
1.53 + "Initialise the locale for 'category' from the environment."
1.54 +
1.55 + check_int(category)
1.56 + return _setlocale(category, "")
1.57 +
1.58 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/lib/native/__init__.py Sun Dec 11 01:49:54 2016 +0100
2.2 +++ b/lib/native/__init__.py Sun Dec 11 18:07:32 2016 +0100
2.3 @@ -36,6 +36,8 @@
2.4 from native.list import list_init, list_setsize, list_append, list_concat, \
2.5 list_len, list_nonempty, list_element, list_setelement
2.6
2.7 +from native.locale import getlocale, setlocale
2.8 +
2.9 from native.program import get_using
2.10
2.11 from native.str import str_add, str_eq, str_gt, str_lt, str_len, str_nonempty, \
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/lib/native/locale.py Sun Dec 11 18:07:32 2016 +0100
3.3 @@ -0,0 +1,30 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +"""
3.7 +Native library functions for locales.
3.8 +
3.9 +None of these are actually defined here. Instead, native implementations are
3.10 +substituted when each program is built. It is, however, important to declare
3.11 +non-core exceptions used by the native functions because they need to be
3.12 +identified as being needed by the program.
3.13 +
3.14 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
3.15 +
3.16 +This program is free software; you can redistribute it and/or modify it under
3.17 +the terms of the GNU General Public License as published by the Free Software
3.18 +Foundation; either version 3 of the License, or (at your option) any later
3.19 +version.
3.20 +
3.21 +This program is distributed in the hope that it will be useful, but WITHOUT
3.22 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
3.23 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
3.24 +details.
3.25 +
3.26 +You should have received a copy of the GNU General Public License along with
3.27 +this program. If not, see <http://www.gnu.org/licenses/>.
3.28 +"""
3.29 +
3.30 +def getlocale(category): pass
3.31 +def setlocale(category, value): pass
3.32 +
3.33 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/templates/native/locale.c Sun Dec 11 18:07:32 2016 +0100
4.3 @@ -0,0 +1,67 @@
4.4 +/* Native functions for locale handling.
4.5 +
4.6 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
4.7 +
4.8 +This program is free software; you can redistribute it and/or modify it under
4.9 +the terms of the GNU General Public License as published by the Free Software
4.10 +Foundation; either version 3 of the License, or (at your option) any later
4.11 +version.
4.12 +
4.13 +This program is distributed in the hope that it will be useful, but WITHOUT
4.14 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
4.15 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
4.16 +details.
4.17 +
4.18 +You should have received a copy of the GNU General Public License along with
4.19 +this program. If not, see <http://www.gnu.org/licenses/>.
4.20 +*/
4.21 +
4.22 +#include <locale.h> /* setlocale */
4.23 +#include "native/common.h"
4.24 +#include "types.h"
4.25 +#include "exceptions.h"
4.26 +#include "ops.h"
4.27 +#include "progconsts.h"
4.28 +#include "progops.h"
4.29 +#include "progtypes.h"
4.30 +#include "main.h"
4.31 +
4.32 +/* Locales. */
4.33 +
4.34 +__attr __fn_native_locale_getlocale(__attr __args[])
4.35 +{
4.36 + __attr * const category = &__args[1];
4.37 + /* category interpreted as int */
4.38 + int cat = __load_via_object(category->value, __pos___data__).intvalue;
4.39 + char *result;
4.40 +
4.41 + result = setlocale(cat, NULL);
4.42 +
4.43 + if (result == NULL)
4.44 + return __builtins___none_None;
4.45 + else
4.46 + return __new_str(result);
4.47 +}
4.48 +
4.49 +__attr __fn_native_locale_setlocale(__attr __args[])
4.50 +{
4.51 + __attr * const category = &__args[1];
4.52 + __attr * const value = &__args[2];
4.53 + /* category interpreted as int */
4.54 + int cat = __load_via_object(category->value, __pos___data__).intvalue;
4.55 + /* value interpreted as string */
4.56 + char *s = __load_via_object(value->value, __pos___data__).strvalue, *result;
4.57 +
4.58 + result = setlocale(cat, s);
4.59 +
4.60 + if (result == NULL)
4.61 + return __builtins___none_None;
4.62 + else
4.63 + return __new_str(result);
4.64 +}
4.65 +
4.66 +/* Module initialisation. */
4.67 +
4.68 +void __main_native_locale()
4.69 +{
4.70 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/templates/native/locale.h Sun Dec 11 18:07:32 2016 +0100
5.3 @@ -0,0 +1,33 @@
5.4 +/* Native functions for locale handling.
5.5 +
5.6 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
5.7 +
5.8 +This program is free software; you can redistribute it and/or modify it under
5.9 +the terms of the GNU General Public License as published by the Free Software
5.10 +Foundation; either version 3 of the License, or (at your option) any later
5.11 +version.
5.12 +
5.13 +This program is distributed in the hope that it will be useful, but WITHOUT
5.14 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
5.15 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
5.16 +details.
5.17 +
5.18 +You should have received a copy of the GNU General Public License along with
5.19 +this program. If not, see <http://www.gnu.org/licenses/>.
5.20 +*/
5.21 +
5.22 +#ifndef __NATIVE_LOCALE_H__
5.23 +#define __NATIVE_LOCALE_H__
5.24 +
5.25 +#include "types.h"
5.26 +
5.27 +/* Input/output. */
5.28 +
5.29 +__attr __fn_native_locale_getlocale(__attr __args[]);
5.30 +__attr __fn_native_locale_setlocale(__attr __args[]);
5.31 +
5.32 +/* Module initialisation. */
5.33 +
5.34 +void __main_native_locale();
5.35 +
5.36 +#endif /* __NATIVE_LOCALE_H__ */
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/tests/locale.py Sun Dec 11 18:07:32 2016 +0100
6.3 @@ -0,0 +1,7 @@
6.4 +import locale
6.5 +
6.6 +print locale.getlocale()
6.7 +print locale.initlocale()
6.8 +print locale.getlocale()
6.9 +print locale.setlocale(locale.LC_CTYPE, "en_GB.UTF-8")
6.10 +print locale.getlocale()