Lichen

templates/native/locale.c

934:2989aab1b4f7
10 months ago Paul Boddie Renamed the utf8string class to unicode, eliminating the unicode function. This means that the simple case of merely returning an object if it is already a Unicode object no longer occurs when using the unicode callable, but such behaviour might be better supported with more general customised instantiation functionality.
     1 /* Native functions for locale handling.     2      3 Copyright (C) 2016, 2017, 2021 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 __self, __attr category)    33 {    34     /* category interpreted as int */    35     int cat = __TOINT(category);    36     char *result, *out;    37     size_t length;    38     39     result = setlocale(cat, NULL);    40     41     if (result == NULL)    42         return __builtins___none_None;    43     44     length = strlen(result);    45     out = __ALLOCATE(length + 1, sizeof(char));    46     strcpy(out, result);    47     48     return __new_str(result, length);    49 }    50     51 __attr __fn_native_locale_setlocale(__attr __self, __attr category, __attr value)    52 {    53     /* category interpreted as int */    54     int cat = __TOINT(category);    55     /* value interpreted as string */    56     char *s = __load_via_object(__VALUE(value), __data__).strvalue;    57     char *result, *out;    58     size_t length;    59     60     result = setlocale(cat, s);    61     62     if (result == NULL)    63         return __builtins___none_None;    64     65     length = strlen(result);    66     out = __ALLOCATE(length + 1, sizeof(char));    67     strcpy(out, result);    68     69     return __new_str(result, length);    70 }    71     72 /* Module initialisation. */    73     74 void __main_native_locale()    75 {    76 }