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
This commit is contained in:
Vincent Torri 2015-11-27 18:43:54 +01:00 committed by Tom Hacohen
parent 21b1a3f221
commit cbb6e9b20f
4 changed files with 125 additions and 0 deletions

View File

@ -10,6 +10,7 @@ lib/evil/Evil.h \
lib/evil/evil_fcntl.h \
lib/evil/evil_inet.h \
lib/evil/evil_langinfo.h \
lib/evil/evil_locale.h \
lib/evil/evil_macro.h \
lib/evil/evil_macro_pop.h \
lib/evil/evil_main.h \
@ -35,6 +36,7 @@ lib/evil/evil_fnmatch.c \
lib/evil/evil_fnmatch_list_of_states.c \
lib/evil/evil_inet.c \
lib/evil/evil_langinfo.c \
lib/evil/evil_locale.c \
lib/evil/evil_link_xp.cpp \
lib/evil/evil_main.c \
lib/evil/evil_mman.c \

View File

@ -73,6 +73,8 @@
* @li @ref Evil_Mman
* @li @ref Evil_Unistd_Group
* @li @ref Evil_Dlfcn
* @li @ref Evil_Langinfo_Group
* @li @ref Evil_Locale_Group
* @li @ref Evil_Pwd_Group
* @li @ref Evil_Stdio_Group
* @li @ref Evil_Main_Group
@ -115,6 +117,7 @@ typedef unsigned long gid_t;
#include "evil_fcntl.h"
#include "evil_inet.h"
#include "evil_langinfo.h"
#include "evil_locale.h"
#include "evil_main.h"
#include "evil_stdlib.h"
#include "evil_stdio.h"

View File

@ -0,0 +1,55 @@
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <locale.h>
#include <errno.h>
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#include "evil_macro.h"
#include "evil_locale.h"
#include "evil_private.h"
/*
* LOCALE_SISO639LANGNAME and LOCALE_SISO3166CTRYNAME need at least a buffer
* of 9 char each (including NULL char). So we need 2*8 + the trailing NULL
* char + '_', so 18 char.
*/
static char _evil_locale_buf[18];
char *evil_setlocale(int category, const char *locale)
{
char buf[9];
int l1;
int l2;
if (category != LC_MESSAGES)
return setlocale(category, locale);
if (locale != NULL)
{
errno = EINVAL;
return NULL;
}
l1 = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME,
buf, sizeof(buf));
if (!l1) return NULL;
memcpy(_evil_locale_buf, buf, l1 - 1);
_evil_locale_buf[l1 - 1] = '_';
l2 = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO3166CTRYNAME,
buf, sizeof(buf));
if (!l2) return NULL;
memcpy(_evil_locale_buf + l1, buf, l2);
return _evil_locale_buf;
}

View File

@ -0,0 +1,65 @@
#ifndef __EVIL_LOCALE_H__
#define __EVIL_LOCALE_H__
/**
* @file evil_locale.h
* @brief The file that provides functions ported from Unix in locale.h.
* @defgroup Evil_Locale_Group locale.h functions.
* @ingroup Evil
*
* This header provides functions ported from Unix in locale.h.
*
* @{
*/
/**
* @def LC_MESSAGES
*
* New locale value, based on the one in libintl.h
*/
#ifdef LC_MESSAGES
# undef LC_MESSAGES
#endif
#define LC_MESSAGES 1729
/**
* @brief Return the string associated to the given locale and category.
*
* @param category The category affected by locale.
* @param locale The locale specifier.
* @return The string associated to the specified locale and category.
*
* This function returns the string associated to @p locale and
* @p category. If @p category is LC_ALL, LC_COLLATE, LC_CTYPE,
* LC_MONETARY, LC_NUMERIC or LC_TIME, it just returns the standard
* setlocale() function. If @p category is #LC_MESSAGES, then if @p locale
* is not @c NULL, errno is set to EINVAL and @c NULL is returned, otherwise
* the string <language>_<country> is returned. This string is a static buffer
* and must not be freed. It will also be rewritten each time @category is
* #LC_MESSAGES and @p locale is @c NULL.
*
* Conformity: Non applicable.
*
* Supported OS: Windows XP.
*/
EAPI char *evil_setlocale(int category, const char *locale);
/**
* @def setlocale(cat, loc)
*
* Wrapper around evil_setlocale().
*/
#ifdef setlocale /* libintl.h defines setlocale() but always returns "C" */
# undef setlocale
#endif
#define setlocale(cat, loc) evil_setlocale(cat, loc)
/**
* @}
*/
#endif /*__EVIL_LOCALE_H__ */