aboutsummaryrefslogtreecommitdiff
path: root/arch/all/core/src/locales.c
blob: 1fa7266c2675e482cba3ae9134c9263731fd573b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* ****************************************************************************
 * locales.c -- localization register.
 * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of libcarrot.
 * libcarrot is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3.0 of the License,
 * or (at your option) any later version.
 *
 * libcarrot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the libcarrot; if not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************* */
#include <locale.h>
#include <limits.h>
#include <string.h>

/* ************************************************************************* */
/*  Built-in locales and registry                                            */
/* ************************************************************************* */
/* Default 'C' locale, defined in the C standard. */

static const struct lconv c_locale = {
	".", "", "", "", "", "", "", "", "",
	CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, "",
	CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX
};

/* Main registry. */

static const struct lconv *current_locales[5] = {
	&c_locale, /* LC_COLLATE */
	&c_locale, /* LC_CTYPE */
	&c_locale, /* LC_MONETARY */
	&c_locale, /* LC_NUMERIC */
	&c_locale  /* LC_TIME */
};
/* ************************************************************************* */
/*  Main functions                                                           */
/* ************************************************************************* */
/**
 *	setlocale:
 *	Set the current locale.
 *
 *	@arg	category	the locale category to edit.
 *	@arg	locale		the identifier of the locale to set.
 *	@return				a string if the locale was found, NULL otherwise.
 */

char *setlocale(int category, const char *locale_name)
{
	static char opaque[2];
	const struct lconv *locale;

	/* select the locale */
	if (!locale_name[0] || !memcmp(locale_name, "C", 2)) {
		memcpy(opaque, "C", 2);
		locale = &c_locale;
	} else return (NULL);

	/* set the locale */
	if (!category) {
		current_locales[0] = locale;
		current_locales[1] = locale;
		current_locales[2] = locale;
		current_locales[3] = locale;
		current_locales[4] = locale;
	} else if (category <= 5)
		current_locales[category - 1] = locale;

	/* return the opaque string */
	return (opaque);
}

/**
 *	localeconv:
 *	Get the locale numerical conversion thing.
 *
 *	@return				the conversion.
 */

struct lconv *localeconv(void)
{
	static struct lconv locale;
	const struct lconv *num = current_locales[LC_NUMERIC - 1];
	const struct lconv *mon = current_locales[LC_MONETARY - 1];

	/* nonmonetary things */
	locale.decimal_point = num->decimal_point;
	locale.thousands_sep = num->thousands_sep;
	locale.grouping = num->grouping;

	/* monetary things */
	locale.mon_decimal_point = mon->mon_decimal_point;
	locale.mon_thousands_sep = mon->mon_thousands_sep;
	locale.mon_grouping = mon->mon_grouping;
	locale.positive_sign = mon->positive_sign;
	locale.negative_sign = mon->negative_sign;
	locale.currency_symbol = mon->currency_symbol;
	locale.frac_digits = mon->frac_digits;
	locale.p_cs_precedes = mon->p_cs_precedes;
	locale.n_cs_precedes = mon->n_cs_precedes;
	locale.p_sep_by_space = mon->p_sep_by_space;
	locale.n_sep_by_space = mon->n_sep_by_space;
	locale.p_sign_posn = mon->p_sign_posn;
	locale.n_sign_posn = mon->n_sign_posn;
	locale.int_curr_symbol = mon->int_curr_symbol;
	locale.int_frac_digits = mon->int_frac_digits;
	locale.int_p_cs_precedes = mon->int_p_cs_precedes;
	locale.int_n_cs_precedes = mon->int_n_cs_precedes;
	locale.int_p_sep_by_space = mon->int_p_sep_by_space;
	locale.int_n_sep_by_space = mon->int_n_sep_by_space;
	locale.int_p_sign_posn = mon->int_p_sign_posn;
	locale.int_n_sign_posn = mon->int_n_sign_posn;

	return (&locale);
}