Lichen

templates/native/locale.c

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