paul@375 | 1 | /* Native functions for locale handling. |
paul@375 | 2 | |
paul@622 | 3 | Copyright (C) 2016, 2017 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@375 | 32 | __attr __fn_native_locale_getlocale(__attr __args[]) |
paul@375 | 33 | { |
paul@375 | 34 | __attr * const category = &__args[1]; |
paul@376 | 35 | /* category.__data__ interpreted as int */ |
paul@624 | 36 | int cat = __load_via_object(category->value, __data__).intvalue; |
paul@376 | 37 | char *result, *out; |
paul@376 | 38 | size_t length; |
paul@375 | 39 | |
paul@375 | 40 | result = setlocale(cat, NULL); |
paul@375 | 41 | |
paul@375 | 42 | if (result == NULL) |
paul@375 | 43 | return __builtins___none_None; |
paul@376 | 44 | |
paul@376 | 45 | length = strlen(result); |
paul@376 | 46 | out = __ALLOCATE(length + 1, sizeof(char)); |
paul@376 | 47 | strncpy(out, result, length); |
paul@376 | 48 | |
paul@583 | 49 | return __new_str(result, length); |
paul@375 | 50 | } |
paul@375 | 51 | |
paul@375 | 52 | __attr __fn_native_locale_setlocale(__attr __args[]) |
paul@375 | 53 | { |
paul@375 | 54 | __attr * const category = &__args[1]; |
paul@375 | 55 | __attr * const value = &__args[2]; |
paul@376 | 56 | /* category.__data__ interpreted as int */ |
paul@624 | 57 | int cat = __load_via_object(category->value, __data__).intvalue; |
paul@376 | 58 | /* value.__data__ interpreted as string */ |
paul@624 | 59 | char *s = __load_via_object(value->value, __data__).strvalue; |
paul@376 | 60 | char *result, *out; |
paul@376 | 61 | size_t length; |
paul@375 | 62 | |
paul@375 | 63 | result = setlocale(cat, s); |
paul@375 | 64 | |
paul@375 | 65 | if (result == NULL) |
paul@375 | 66 | return __builtins___none_None; |
paul@376 | 67 | |
paul@376 | 68 | length = strlen(result); |
paul@376 | 69 | out = __ALLOCATE(length + 1, sizeof(char)); |
paul@376 | 70 | strncpy(out, result, length); |
paul@376 | 71 | |
paul@583 | 72 | return __new_str(result, length); |
paul@375 | 73 | } |
paul@375 | 74 | |
paul@375 | 75 | /* Module initialisation. */ |
paul@375 | 76 | |
paul@375 | 77 | void __main_native_locale() |
paul@375 | 78 | { |
paul@375 | 79 | } |