summaryrefslogtreecommitdiff
path: root/src/lib/evil
diff options
context:
space:
mode:
authorVincent Torri <vincent dot torri at gmail dot com>2015-11-27 18:43:54 +0100
committerTom Hacohen <tom@stosb.com>2015-11-30 16:28:02 +0000
commitcbb6e9b20f3d85b41b0c3c525fd3ae68864118b1 (patch)
treef7a2e3d3bcb1cda04613c280c8c5dd3fe0518c8a /src/lib/evil
parent21b1a3f221fb6bd477cfefbb9d0099717c13523d (diff)
Evil: add support for LC_MESSAGES for setlocale()
LC_MESSAGES is an extension to C ANSI and does not exist on Windows. So add LC_MESSAGES API and overload setlocale() to support it @feature
Diffstat (limited to 'src/lib/evil')
-rw-r--r--src/lib/evil/Evil.h3
-rw-r--r--src/lib/evil/evil_locale.c55
-rw-r--r--src/lib/evil/evil_locale.h65
3 files changed, 123 insertions, 0 deletions
diff --git a/src/lib/evil/Evil.h b/src/lib/evil/Evil.h
index 780b8b0e1e..6a69f7bd83 100644
--- a/src/lib/evil/Evil.h
+++ b/src/lib/evil/Evil.h
@@ -73,6 +73,8 @@
73 * @li @ref Evil_Mman 73 * @li @ref Evil_Mman
74 * @li @ref Evil_Unistd_Group 74 * @li @ref Evil_Unistd_Group
75 * @li @ref Evil_Dlfcn 75 * @li @ref Evil_Dlfcn
76 * @li @ref Evil_Langinfo_Group
77 * @li @ref Evil_Locale_Group
76 * @li @ref Evil_Pwd_Group 78 * @li @ref Evil_Pwd_Group
77 * @li @ref Evil_Stdio_Group 79 * @li @ref Evil_Stdio_Group
78 * @li @ref Evil_Main_Group 80 * @li @ref Evil_Main_Group
@@ -115,6 +117,7 @@ typedef unsigned long gid_t;
115#include "evil_fcntl.h" 117#include "evil_fcntl.h"
116#include "evil_inet.h" 118#include "evil_inet.h"
117#include "evil_langinfo.h" 119#include "evil_langinfo.h"
120#include "evil_locale.h"
118#include "evil_main.h" 121#include "evil_main.h"
119#include "evil_stdlib.h" 122#include "evil_stdlib.h"
120#include "evil_stdio.h" 123#include "evil_stdio.h"
diff --git a/src/lib/evil/evil_locale.c b/src/lib/evil/evil_locale.c
new file mode 100644
index 0000000000..d243ad8b34
--- /dev/null
+++ b/src/lib/evil/evil_locale.c
@@ -0,0 +1,55 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include <string.h>
6#include <locale.h>
7#include <errno.h>
8
9#ifndef WIN32_LEAN_AND_MEAN
10# define WIN32_LEAN_AND_MEAN
11#endif
12#include <windows.h>
13#undef WIN32_LEAN_AND_MEAN
14
15#include "evil_macro.h"
16#include "evil_locale.h"
17#include "evil_private.h"
18
19/*
20 * LOCALE_SISO639LANGNAME and LOCALE_SISO3166CTRYNAME need at least a buffer
21 * of 9 char each (including NULL char). So we need 2*8 + the trailing NULL
22 * char + '_', so 18 char.
23 */
24static char _evil_locale_buf[18];
25
26char *evil_setlocale(int category, const char *locale)
27{
28 char buf[9];
29 int l1;
30 int l2;
31
32 if (category != LC_MESSAGES)
33 return setlocale(category, locale);
34
35 if (locale != NULL)
36 {
37 errno = EINVAL;
38 return NULL;
39 }
40
41 l1 = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME,
42 buf, sizeof(buf));
43 if (!l1) return NULL;
44
45 memcpy(_evil_locale_buf, buf, l1 - 1);
46 _evil_locale_buf[l1 - 1] = '_';
47
48 l2 = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO3166CTRYNAME,
49 buf, sizeof(buf));
50 if (!l2) return NULL;
51
52 memcpy(_evil_locale_buf + l1, buf, l2);
53
54 return _evil_locale_buf;
55}
diff --git a/src/lib/evil/evil_locale.h b/src/lib/evil/evil_locale.h
new file mode 100644
index 0000000000..61968003de
--- /dev/null
+++ b/src/lib/evil/evil_locale.h
@@ -0,0 +1,65 @@
1#ifndef __EVIL_LOCALE_H__
2#define __EVIL_LOCALE_H__
3
4
5/**
6 * @file evil_locale.h
7 * @brief The file that provides functions ported from Unix in locale.h.
8 * @defgroup Evil_Locale_Group locale.h functions.
9 * @ingroup Evil
10 *
11 * This header provides functions ported from Unix in locale.h.
12 *
13 * @{
14 */
15
16
17/**
18 * @def LC_MESSAGES
19 *
20 * New locale value, based on the one in libintl.h
21 */
22#ifdef LC_MESSAGES
23# undef LC_MESSAGES
24#endif
25#define LC_MESSAGES 1729
26
27/**
28 * @brief Return the string associated to the given locale and category.
29 *
30 * @param category The category affected by locale.
31 * @param locale The locale specifier.
32 * @return The string associated to the specified locale and category.
33 *
34 * This function returns the string associated to @p locale and
35 * @p category. If @p category is LC_ALL, LC_COLLATE, LC_CTYPE,
36 * LC_MONETARY, LC_NUMERIC or LC_TIME, it just returns the standard
37 * setlocale() function. If @p category is #LC_MESSAGES, then if @p locale
38 * is not @c NULL, errno is set to EINVAL and @c NULL is returned, otherwise
39 * the string <language>_<country> is returned. This string is a static buffer
40 * and must not be freed. It will also be rewritten each time @category is
41 * #LC_MESSAGES and @p locale is @c NULL.
42 *
43 * Conformity: Non applicable.
44 *
45 * Supported OS: Windows XP.
46 */
47EAPI char *evil_setlocale(int category, const char *locale);
48
49/**
50 * @def setlocale(cat, loc)
51 *
52 * Wrapper around evil_setlocale().
53 */
54#ifdef setlocale /* libintl.h defines setlocale() but always returns "C" */
55# undef setlocale
56#endif
57#define setlocale(cat, loc) evil_setlocale(cat, loc)
58
59
60/**
61 * @}
62 */
63
64
65#endif /*__EVIL_LOCALE_H__ */