paul@375 | 1 | /* Native functions for locale handling. |
paul@375 | 2 | |
paul@971 | 3 | Copyright (C) 2016, 2017, 2021, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@375 | 4 | |
paul@375 | 5 | This program is free software; you can redistribute it and/or modify it under |
paul@375 | 6 | the terms of the GNU General Public License as published by the Free Software |
paul@375 | 7 | Foundation; either version 3 of the License, or (at your option) any later |
paul@375 | 8 | version. |
paul@375 | 9 | |
paul@375 | 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@375 | 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@375 | 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@375 | 13 | details. |
paul@375 | 14 | |
paul@375 | 15 | You should have received a copy of the GNU General Public License along with |
paul@375 | 16 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@375 | 17 | */ |
paul@375 | 18 | |
paul@375 | 19 | #include <locale.h> /* setlocale */ |
paul@376 | 20 | #include <string.h> /* strlen */ |
paul@375 | 21 | #include "native/common.h" |
paul@375 | 22 | #include "types.h" |
paul@375 | 23 | #include "exceptions.h" |
paul@375 | 24 | #include "ops.h" |
paul@375 | 25 | #include "progconsts.h" |
paul@375 | 26 | #include "progops.h" |
paul@375 | 27 | #include "progtypes.h" |
paul@375 | 28 | #include "main.h" |
paul@375 | 29 | |
paul@375 | 30 | /* Locales. */ |
paul@375 | 31 | |
paul@971 | 32 | __attr __fn_native_locale_getlocale(__attr __result, __attr __self, __attr category) |
paul@375 | 33 | { |
paul@758 | 34 | /* category interpreted as int */ |
paul@763 | 35 | int cat = __TOINT(category); |
paul@376 | 36 | char *result, *out; |
paul@376 | 37 | size_t length; |
paul@375 | 38 | |
paul@375 | 39 | result = setlocale(cat, NULL); |
paul@375 | 40 | |
paul@375 | 41 | if (result == NULL) |
paul@375 | 42 | return __builtins___none_None; |
paul@376 | 43 | |
paul@376 | 44 | length = strlen(result); |
paul@376 | 45 | out = __ALLOCATE(length + 1, sizeof(char)); |
paul@913 | 46 | strcpy(out, result); |
paul@376 | 47 | |
paul@583 | 48 | return __new_str(result, length); |
paul@375 | 49 | } |
paul@375 | 50 | |
paul@971 | 51 | __attr __fn_native_locale_setlocale(__attr __result, __attr __self, __attr category, __attr value) |
paul@375 | 52 | { |
paul@758 | 53 | /* category interpreted as int */ |
paul@763 | 54 | int cat = __TOINT(category); |
paul@664 | 55 | /* value interpreted as string */ |
paul@763 | 56 | char *s = __load_via_object(__VALUE(value), __data__).strvalue; |
paul@376 | 57 | char *result, *out; |
paul@376 | 58 | size_t length; |
paul@375 | 59 | |
paul@375 | 60 | result = setlocale(cat, s); |
paul@375 | 61 | |
paul@375 | 62 | if (result == NULL) |
paul@375 | 63 | return __builtins___none_None; |
paul@376 | 64 | |
paul@376 | 65 | length = strlen(result); |
paul@376 | 66 | out = __ALLOCATE(length + 1, sizeof(char)); |
paul@913 | 67 | strcpy(out, result); |
paul@376 | 68 | |
paul@583 | 69 | return __new_str(result, length); |
paul@375 | 70 | } |
paul@375 | 71 | |
paul@375 | 72 | /* Module initialisation. */ |
paul@375 | 73 | |
paul@375 | 74 | void __main_native_locale() |
paul@375 | 75 | { |
paul@375 | 76 | } |