GnuCash de0bfde+
Loading...
Searching...
No Matches
gnc-locale-utils.c
1/********************************************************************\
2 * gnc-locale-utils.c -- locale functions *
3 * Copyright (C) 2000 Dave Peticolas <dave@krondo.com> *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation; either version 2 of *
8 * the License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License*
16 * along with this program; if not, contact: *
17 * *
18 * Free Software Foundation Voice: +1-617-542-5942 *
19 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20 * Boston, MA 02110-1301, USA gnu@gnu.org *
21\********************************************************************/
22
23#include <config.h>
24
25#include "gnc-locale-utils.h"
26
27#include <glib.h>
28#include <glib/gi18n.h>
29
30#ifdef G_OS_WIN32
31#include <windows.h>
32
33static void
34gnc_lconv_set_utf8 (char **p_value, wchar_t *w_value, const char *default_value)
35{
36 g_return_if_fail (p_value != NULL);
37 if (w_value && *w_value)
38 *p_value = g_utf16_to_utf8 (w_value, -1, NULL, NULL, NULL);
39 else
40 *p_value = g_strdup (default_value);
41}
42
43static wchar_t*
44get_locale_name ()
45{
46 char* locale = g_strdup(setlocale(LC_MONETARY, NULL));
47 if (locale)
48 {
49 size_t len = strcspn(locale, ".@");
50 char* sep = memchr(locale, '_', len);
51 if (sep)
52 *sep = '-';
53
54 wchar_t *wlocale = g_utf8_to_utf16 (locale, len, NULL, NULL, NULL);
55 g_free(locale);
56 if (IsValidLocaleName(wlocale))
57 return wlocale;
58 g_free(wlocale);
59 }
60 return LOCALE_NAME_USER_DEFAULT; //aka NULL
61}
62
63#define BUFSIZE 10 //Max size used by LOCALE_SGROUPING.
64static wchar_t*
65get_lconv_wide_val (LCTYPE lctype)
66{
67 static wchar_t buffer[BUFSIZE];
68 wchar_t* locale = get_locale_name ();
69 memset (buffer, 0, BUFSIZE * sizeof(wchar_t));
70 int count = GetLocaleInfoEx (locale, lctype, buffer, BUFSIZE);
71 if (!count)
72 {
73 g_error("GetLocaleInfo failed for %ld, %ld", lctype, GetLastError());
74 return NULL;
75 }
76 if (lctype == LOCALE_SGROUPING || lctype == LOCALE_SMONGROUPING)
77 {
78 buffer[0] &= 0x0f; // The standard localeconv is a single uint8
79 buffer[1] = '\0';
80 }
81 g_free(locale);
82 return buffer;
83}
84#else
85static void
86gnc_lconv_set_utf8 (char **p_value, char *default_value)
87{
88 char *value = *p_value;
89 *p_value = NULL;
90
91 if ((value == NULL) || (value[0] == 0))
92 value = default_value;
93
94 *p_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
95
96 if (*p_value == NULL)
97 {
98 // The g_locale_to_utf8 conversion failed. FIXME: Should we rather
99 // use an empty string instead of the default_value? Not sure.
100 *p_value = default_value;
101 }
102}
103
104#endif
105static void
106gnc_lconv_set_char (char *p_value, char default_value)
107{
108 if ((p_value != NULL) && (*p_value == CHAR_MAX))
109 *p_value = default_value;
110}
111
112struct lconv *
113gnc_localeconv (void)
114{
115 static struct lconv lc;
116 static gboolean lc_set = FALSE;
117
118 if (lc_set)
119 return &lc;
120
121 lc = *localeconv();
122#ifdef G_OS_WIN32
123 gnc_lconv_set_utf8(&lc.decimal_point,
124 get_lconv_wide_val (LOCALE_SDECIMAL), ".");
125 gnc_lconv_set_utf8(&lc.thousands_sep,
126 get_lconv_wide_val (LOCALE_STHOUSAND),",");
127 gnc_lconv_set_utf8(&lc.grouping,
128 get_lconv_wide_val (LOCALE_SGROUPING), "\003");
129 gnc_lconv_set_utf8(&lc.int_curr_symbol,
130 get_lconv_wide_val (LOCALE_SINTLSYMBOL), "USD ");
131 gnc_lconv_set_utf8(&lc.currency_symbol,
132 get_lconv_wide_val (LOCALE_SCURRENCY), "$");
133 gnc_lconv_set_utf8(&lc.mon_decimal_point,
134 get_lconv_wide_val (LOCALE_SMONDECIMALSEP), ".");
135 gnc_lconv_set_utf8(&lc.mon_thousands_sep,
136 get_lconv_wide_val (LOCALE_SMONTHOUSANDSEP), ",");
137 gnc_lconv_set_utf8(&lc.mon_grouping,
138 get_lconv_wide_val (LOCALE_SMONGROUPING), "\003");
139 gnc_lconv_set_utf8(&lc.negative_sign,
140 get_lconv_wide_val (LOCALE_SNEGATIVESIGN), "-");
141 gnc_lconv_set_utf8(&lc.positive_sign,
142 get_lconv_wide_val (LOCALE_SPOSITIVESIGN), "");
143#else
144 gnc_lconv_set_utf8(&lc.decimal_point, ".");
145 gnc_lconv_set_utf8(&lc.thousands_sep, ",");
146 gnc_lconv_set_utf8(&lc.grouping, "\003");
147 gnc_lconv_set_utf8(&lc.int_curr_symbol, "USD ");
148 gnc_lconv_set_utf8(&lc.currency_symbol, "$");
149 gnc_lconv_set_utf8(&lc.mon_decimal_point, ".");
150 gnc_lconv_set_utf8(&lc.mon_thousands_sep, ",");
151 gnc_lconv_set_utf8(&lc.mon_grouping, "\003");
152 gnc_lconv_set_utf8(&lc.negative_sign, "-");
153 gnc_lconv_set_utf8(&lc.positive_sign, "");
154#endif
155 gnc_lconv_set_char(&lc.frac_digits, 2);
156 gnc_lconv_set_char(&lc.int_frac_digits, 2);
157 gnc_lconv_set_char(&lc.p_cs_precedes, 1);
158 gnc_lconv_set_char(&lc.p_sep_by_space, 0);
159 gnc_lconv_set_char(&lc.n_cs_precedes, 1);
160 gnc_lconv_set_char(&lc.n_sep_by_space, 0);
161 gnc_lconv_set_char(&lc.p_sign_posn, 1);
162 gnc_lconv_set_char(&lc.n_sign_posn, 1);
163
164 lc_set = TRUE;
165
166 return &lc;
167}
168
169const char *
170gnc_locale_default_iso_currency_code (void)
171{
172 static char *code = NULL;
173 struct lconv *lc;
174
175 if (code)
176 return code;
177
178 lc = gnc_localeconv ();
179
180 code = g_strdup (lc->int_curr_symbol);
181
182 /* The int_curr_symbol includes a space at the end! Note: you
183 * can't just change "USD " to "USD" in gnc_localeconv, because
184 * that is only used if int_curr_symbol was not defined in the
185 * current locale. If it was, it will have the space! */
186 g_strstrip (code);
187
188 return code;
189}
190
191/* Return the number of decimal places for this locale. */
192int
193gnc_locale_decimal_places (void)
194{
195 static gboolean got_it = FALSE;
196 static int places;
197 struct lconv *lc;
198
199 if (got_it)
200 return places;
201
202 lc = gnc_localeconv();
203 places = lc->frac_digits;
204
205 /* frac_digits is already initialized by gnc_localeconv, hopefully
206 * to a reasonable default. */
207 got_it = TRUE;
208
209 return places;
210}
211
212gchar *gnc_locale_name (void)
213{
214# ifdef G_OS_WIN32
215 return g_win32_getlocale();
216# else /* !G_OS_WIN32 */
217 return g_strdup (setlocale(LC_MONETARY, NULL));
218# endif /* G_OS_WIN32 */
219
220}