locale: add a stub that supports only the "C" locale (TEST)

This is enough to support the standard and likely the C++ library and
external programs to port, but also the most we can do without a proper
locale data storage and more target-specific developments that aren't a
priority right now.
This commit is contained in:
Lephenixnoir 2021-05-16 18:08:05 +02:00
parent 676601b894
commit fdf32aeb97
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 97 additions and 0 deletions

View File

@ -59,6 +59,8 @@ endif()
set(SOURCES
src/libc/assert/assert.c
src/libc/locale/setlocale.c
src/libc/locale/localeconv.c
src/libc/stdio/vsnprintf.c
src/libc/stdio/sprintf.c
src/libc/stdio/dprintf.c

43
include/locale.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef __LOCALE_H__
# define __LOCALE_H__
#include <stddef.h>
struct lconv {
char *decimal_point;
char *thousands_sep;
char *grouping;
char *mon_decimal_point;
char *mon_thousands_sep;
char *mon_grouping;
char *positive_sign;
char *negative_sign;
char *currency_symbol;
char frac_digits;
char p_cs_precedes;
char n_cs_precedes;
char p_sep_by_space;
char n_sep_by_space;
char p_sign_posn;
char n_sign_posn;
char *int_curr_symbol;
char int_frac_digits;
char int_p_cs_precedes;
char int_n_cs_precedes;
char int_p_sep_by_space;
char int_n_sep_by_space;
char int_p_sign_posn;
char int_n_sign_posn;
};
#define LC_COLLATE 0
#define LC_CTYPE 1
#define LC_MONETARY 2
#define LC_NUMERIC 3
#define LC_TIME 4
#define LC_ALL 5
extern char *setlocale(int __category, char const *__locale);
extern struct lconv *localeconv(void);
#endif /*__LOCALE_H__*/

View File

@ -0,0 +1,34 @@
#include <locale.h>
#include <limits.h>
static struct lconv lconv_c = {
.decimal_point = ".",
.thousands_sep = "",
.grouping = "",
.mon_decimal_point = "",
.mon_thousands_sep = "",
.mon_grouping = "",
.positive_sign = "",
.negative_sign = "",
.currency_symbol = "",
.frac_digits = CHAR_MAX,
.p_cs_precedes = CHAR_MAX,
.n_cs_precedes = CHAR_MAX,
.p_sep_by_space = CHAR_MAX,
.n_sep_by_space = CHAR_MAX,
.p_sign_posn = CHAR_MAX,
.n_sign_posn = CHAR_MAX,
.int_curr_symbol = "",
.int_frac_digits = CHAR_MAX,
.int_p_cs_precedes = CHAR_MAX,
.int_n_cs_precedes = CHAR_MAX,
.int_p_sep_by_space = CHAR_MAX,
.int_n_sep_by_space = CHAR_MAX,
.int_p_sign_posn = CHAR_MAX,
.int_n_sign_posn = CHAR_MAX,
};
struct lconv *localeconv(void)
{
return &lconv_c;
}

View File

@ -0,0 +1,18 @@
#include <locale.h>
/*
** This stub locale implementation only supports the "C" locale. For several
** targets there is no filesystem/LOCPATH/database of locale values, so this is
** the only setting that can always be supported. It is also sufficient to
** support when porting Linux programs because Linux does not guarantee that
** other locales will be available.
*/
char *setlocale(__attribute__((unused)) int category, char const *locale)
{
if(locale) {
if(locale[0] == 0) locale = "C";
if(locale[0] != 'C' || locale[1] != 0) return NULL;
}
return "C";
}