Rearrange struct __locale_t pointers into an array

This allows looping through the structs and buffers.  Also
rearrange definitions to follow order of LC_xxx values.

Signed-off by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2016-07-22 19:57:56 +02:00
parent 1498c79db8
commit 53f84bb5ac
8 changed files with 92 additions and 104 deletions

View File

@ -77,16 +77,16 @@ __ctype_load_locale (struct __locale_t *locale, const char *name,
return -1; return -1;
memcpy (ctp, &ct, sizeof *ctp); memcpy (ctp, &ct, sizeof *ctp);
} }
locale->ctype = ret == 0 ? &_C_ctype_locale : ctp; locale->lc_cat[LC_CTYPE].ptr = ret == 0 ? &_C_ctype_locale : ctp;
if (locale->ctype_buf) if (locale->lc_cat[LC_CTYPE].buf)
free (locale->ctype_buf); free (locale->lc_cat[LC_CTYPE].buf);
locale->ctype_buf = bufp; locale->lc_cat[LC_CTYPE].buf = bufp;
ret = 0; ret = 0;
} }
#elif !defined (__HAVE_LOCALE_INFO_EXTENDED__) #elif !defined (__HAVE_LOCALE_INFO_EXTENDED__)
ret = 0; ret = 0;
if (!strcmp (name, "C")) if (!strcmp (name, "C"))
locale->ctype = NULL; locale->lc_cat[LC_CTYPE].ptr = NULL;
else else
{ {
if (locale == __get_global_locale ()) if (locale == __get_global_locale ())
@ -100,9 +100,10 @@ __ctype_load_locale (struct __locale_t *locale, const char *name,
mbc[0] = mb_cur_max; mbc[0] = mb_cur_max;
mbc[1] = '\0'; mbc[1] = '\0';
_ctype_locale.mb_cur_max = mbc; _ctype_locale.mb_cur_max = mbc;
if (locale->ctype_buf && locale->ctype_buf != _ctype_locale_buf) if (locale->lc_cat[LC_CTYPE].buf
free (locale->ctype_buf); && locale->lc_cat[LC_CTYPE].buf != _ctype_locale_buf)
locale->ctype_buf = bufp; free (locale->lc_cat[LC_CTYPE].buf);
locale->lc_cat[LC_CTYPE].buf = bufp;
} }
else else
ret = -1; ret = -1;

View File

@ -87,10 +87,10 @@ __messages_load_locale (struct __locale_t *locale, const char *name,
return -1; return -1;
memcpy (mep, &me, sizeof *mep); memcpy (mep, &me, sizeof *mep);
} }
locale->messages = ret == 0 ? &_C_messages_locale : mep; locale->lc_cat[LC_MESSAGES].ptr = ret == 0 ? &_C_messages_locale : mep;
if (locale->messages_buf) if (locale->lc_cat[LC_MESSAGES].buf)
free (locale->messages_buf); free (locale->lc_cat[LC_MESSAGES].buf);
locale->messages_buf = bufp; locale->lc_cat[LC_MESSAGES].buf = bufp;
ret = 0; ret = 0;
} }
#else #else

View File

@ -115,10 +115,10 @@ __monetary_load_locale (struct __locale_t *locale, const char *name ,
return -1; return -1;
memcpy (mop, &mo, sizeof *mop); memcpy (mop, &mo, sizeof *mop);
} }
locale->monetary = ret == 0 ? &_C_monetary_locale : mop; locale->lc_cat[LC_MONETARY].ptr = ret == 0 ? &_C_monetary_locale : mop;
if (locale->monetary_buf) if (locale->lc_cat[LC_MONETARY].buf)
free (locale->monetary_buf); free (locale->lc_cat[LC_MONETARY].buf);
locale->monetary_buf = bufp; locale->lc_cat[LC_MONETARY].buf = bufp;
ret = 0; ret = 0;
} }
#else #else

View File

@ -77,10 +77,10 @@ __numeric_load_locale (struct __locale_t *locale, const char *name ,
return -1; return -1;
memcpy (nmp, &nm, sizeof *nmp); memcpy (nmp, &nm, sizeof *nmp);
} }
locale->numeric = ret == 0 ? &_C_numeric_locale : nmp; locale->lc_cat[LC_NUMERIC].ptr = ret == 0 ? &_C_numeric_locale : nmp;
if (locale->numeric_buf) if (locale->lc_cat[LC_NUMERIC].buf)
free (locale->numeric_buf); free (locale->lc_cat[LC_NUMERIC].buf);
locale->numeric_buf = bufp; locale->lc_cat[LC_NUMERIC].buf = bufp;
ret = 0; ret = 0;
} }
#else #else

View File

@ -243,20 +243,19 @@ struct __locale_t __global_locale =
"ASCII", "ASCII",
"ASCII", "ASCII",
#else #else
&_C_ctype_locale, {
NULL, { NULL, NULL }, /* LC_ALL */
&_C_monetary_locale,
NULL,
&_C_numeric_locale,
NULL,
&_C_time_locale,
NULL,
&_C_messages_locale,
NULL,
#ifdef __CYGWIN__ #ifdef __CYGWIN__
&_C_collate_locale, { &_C_collate_locale, NULL }, /* LC_COLLATE */
NULL, #else
{ NULL, NULL }, /* LC_COLLATE */
#endif #endif
{ &_C_ctype_locale, NULL }, /* LC_CTYPE */
{ &_C_monetary_locale, NULL }, /* LC_MONETARY */
{ &_C_numeric_locale, NULL }, /* LC_NUMERIC */
{ &_C_time_locale, NULL }, /* LC_TIME */
{ &_C_messages_locale, NULL }, /* LC_MESSAGES */
},
#endif #endif
}; };
@ -276,9 +275,6 @@ _DEFUN(_setlocale_r, (p, category, locale),
int category _AND int category _AND
_CONST char *locale) _CONST char *locale)
{ {
static char new_categories[_LC_LAST][ENCODING_LEN + 1];
static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
#ifndef _MB_CAPABLE #ifndef _MB_CAPABLE
if (locale) if (locale)
{ {
@ -288,6 +284,8 @@ _DEFUN(_setlocale_r, (p, category, locale),
} }
return "C"; return "C";
#else /* !_MB_CAPABLE */ #else /* !_MB_CAPABLE */
static char new_categories[_LC_LAST][ENCODING_LEN + 1];
static char saved_categories[_LC_LAST][ENCODING_LEN + 1];
int i, j, len, saverr; int i, j, len, saverr;
const char *env, *r; const char *env, *r;
@ -438,9 +436,6 @@ currentlocale ()
} }
return (global_locale_string); return (global_locale_string);
} }
#endif /* _MB_CAPABLE */
#ifdef _MB_CAPABLE
extern void __set_ctype (struct __locale_t *, const char *charset); extern void __set_ctype (struct __locale_t *, const char *charset);
@ -483,7 +478,7 @@ loadlocale (struct __locale_t *loc, int category, const char *new_locale)
restart: restart:
if (!locale) if (!locale)
locale = new_locale; locale = (char *) new_locale;
else if (locale != tmp_locale) else if (locale != tmp_locale)
{ {
locale = __set_locale_from_locale_alias (locale, tmp_locale); locale = __set_locale_from_locale_alias (locale, tmp_locale);

View File

@ -43,6 +43,18 @@ __BEGIN_DECLS
#define CATEGORY_LEN 11 #define CATEGORY_LEN 11
#define _LC_LAST 7 #define _LC_LAST 7
#ifdef __CYGWIN__
typedef __uint32_t LCID;
struct lc_collate_T
{
LCID lcid;
int (*mbtowc) (struct _reent *, wchar_t *, const char *, size_t, mbstate_t *);
char codeset[ENCODING_LEN + 1];
};
extern const struct lc_collate_T _C_collate_locale;
#endif
struct lc_ctype_T { struct lc_ctype_T {
const char *codeset; /* codeset for mbtowc conversion */ const char *codeset; /* codeset for mbtowc conversion */
const char *mb_cur_max; const char *mb_cur_max;
@ -153,47 +165,27 @@ struct lc_messages_T {
}; };
extern const struct lc_messages_T _C_messages_locale; extern const struct lc_messages_T _C_messages_locale;
#ifdef __CYGWIN__ struct __lc_cats
typedef __uint32_t LCID;
struct lc_collate_T
{ {
LCID lcid; const void *ptr;
int (*mbtowc) (struct _reent *, wchar_t *, const char *, size_t, mbstate_t *); char *buf;
char codeset[ENCODING_LEN + 1];
}; };
extern const struct lc_collate_T _C_collate_locale;
#endif
struct __locale_t struct __locale_t
{ {
char categories[_LC_LAST][ENCODING_LEN + 1]; char categories[_LC_LAST][ENCODING_LEN + 1];
int (*wctomb) (struct _reent *, char *, wchar_t, int (*wctomb) (struct _reent *, char *, wchar_t,
mbstate_t *); mbstate_t *);
int (*mbtowc) (struct _reent *, wchar_t *, int (*mbtowc) (struct _reent *, wchar_t *,
const char *, size_t, mbstate_t *); const char *, size_t, mbstate_t *);
int cjk_lang; int cjk_lang;
char *ctype_ptr; /* Unused in __global_locale */ char *ctype_ptr; /* Unused in __global_locale */
#ifndef __HAVE_LOCALE_INFO__ #ifndef __HAVE_LOCALE_INFO__
char mb_cur_max[2]; char mb_cur_max[2];
char ctype_codeset[ENCODING_LEN + 1]; char ctype_codeset[ENCODING_LEN + 1];
char message_codeset[ENCODING_LEN + 1]; char message_codeset[ENCODING_LEN + 1];
#else #else
const struct lc_ctype_T *ctype; struct __lc_cats lc_cat[_LC_LAST];
char *ctype_buf;
const struct lc_monetary_T *monetary;
char *monetary_buf;
const struct lc_numeric_T *numeric;
char *numeric_buf;
const struct lc_time_T *time;
char *time_buf;
const struct lc_messages_T *messages;
char *messages_buf;
#ifdef __CYGWIN__
const struct lc_collate_T *collate;
char *collate_buf;
#endif
/* Append more categories here. */
#endif #endif
}; };
@ -224,44 +216,44 @@ __get_current_locale ()
return _REENT->_locale ?: &__global_locale; return _REENT->_locale ?: &__global_locale;
} }
#ifdef __CYGWIN__
_ELIDABLE_INLINE const struct lc_collate_T *
__get_current_collate_locale (void)
{
return (const struct lc_collate_T *) __get_current_locale ()->lc_cat[LC_COLLATE].ptr;
}
#endif
_ELIDABLE_INLINE const struct lc_ctype_T * _ELIDABLE_INLINE const struct lc_ctype_T *
__get_current_ctype_locale (void) __get_current_ctype_locale (void)
{ {
return __get_current_locale ()->ctype; return (const struct lc_ctype_T *) __get_current_locale ()->lc_cat[LC_CTYPE].ptr;
} }
_ELIDABLE_INLINE const struct lc_monetary_T * _ELIDABLE_INLINE const struct lc_monetary_T *
__get_current_monetary_locale (void) __get_current_monetary_locale (void)
{ {
return __get_current_locale ()->monetary; return (const struct lc_monetary_T *) __get_current_locale ()->lc_cat[LC_MONETARY].ptr;
}
_ELIDABLE_INLINE const struct lc_time_T *
__get_current_time_locale (void)
{
return __get_current_locale ()->time;
} }
_ELIDABLE_INLINE const struct lc_numeric_T * _ELIDABLE_INLINE const struct lc_numeric_T *
__get_current_numeric_locale (void) __get_current_numeric_locale (void)
{ {
return __get_current_locale ()->numeric; return (const struct lc_numeric_T *) __get_current_locale ()->lc_cat[LC_NUMERIC].ptr;
}
_ELIDABLE_INLINE const struct lc_time_T *
__get_current_time_locale (void)
{
return (const struct lc_time_T *) __get_current_locale ()->lc_cat[LC_TIME].ptr;
} }
_ELIDABLE_INLINE const struct lc_messages_T * _ELIDABLE_INLINE const struct lc_messages_T *
__get_current_messages_locale (void) __get_current_messages_locale (void)
{ {
return __get_current_locale ()->messages; return (const struct lc_messages_T *) __get_current_locale ()->lc_cat[LC_MESSAGES].ptr;
} }
#ifdef __CYGWIN__
_ELIDABLE_INLINE const struct lc_collate_T *
__get_current_collate_locale (void)
{
return __get_current_locale ()->collate;
}
#endif
_ELIDABLE_INLINE const char * _ELIDABLE_INLINE const char *
__locale_charset (void) __locale_charset (void)
{ {

View File

@ -177,10 +177,10 @@ __time_load_locale (struct __locale_t *locale, const char *name,
return -1; return -1;
memcpy (tip, &ti, sizeof *tip); memcpy (tip, &ti, sizeof *tip);
} }
locale->time = ret == 0 ? &_C_time_locale : tip; locale->lc_cat[LC_TIME].ptr = ret == 0 ? &_C_time_locale : tip;
if (locale->time_buf) if (locale->lc_cat[LC_TIME].buf)
free (locale->time_buf); free (locale->lc_cat[LC_TIME].buf);
locale->time_buf = bufp; locale->lc_cat[LC_TIME].buf = bufp;
ret = 0; ret = 0;
} }
#else #else

View File

@ -1074,7 +1074,7 @@ __collate_load_locale (struct __locale_t *locale, const char *name,
void *f_mbtowc, const char *charset) void *f_mbtowc, const char *charset)
{ {
const struct lc_collate_T *ccop; const struct lc_collate_T *ccop;
char *buf = NULL; char *bufp = NULL;
LCID lcid = __get_lcid_from_locale (name); LCID lcid = __get_lcid_from_locale (name);
if (lcid == (LCID) -1) if (lcid == (LCID) -1)
@ -1082,23 +1082,23 @@ __collate_load_locale (struct __locale_t *locale, const char *name,
if (!lcid) if (!lcid)
{ {
ccop = &_C_collate_locale; ccop = &_C_collate_locale;
buf = NULL; bufp = NULL;
} }
else else
{ {
buf = (char *) calloc (1, sizeof (struct lc_collate_T)); bufp = (char *) calloc (1, sizeof (struct lc_collate_T));
if (!buf) if (!bufp)
return -1; return -1;
struct lc_collate_T *cop = (struct lc_collate_T *) buf; struct lc_collate_T *cop = (struct lc_collate_T *) bufp;
cop->lcid = lcid; cop->lcid = lcid;
cop->mbtowc = (mbtowc_p) f_mbtowc; cop->mbtowc = (mbtowc_p) f_mbtowc;
stpcpy (cop->codeset, charset); stpcpy (cop->codeset, charset);
ccop = (const struct lc_collate_T *) cop; ccop = (const struct lc_collate_T *) cop;
} }
locale->collate = ccop; locale->lc_cat[LC_COLLATE].ptr = ccop;
if (locale->collate_buf) if (locale->lc_cat[LC_COLLATE].buf)
free (locale->collate_buf); free (locale->lc_cat[LC_COLLATE].buf);
locale->collate_buf = buf; locale->lc_cat[LC_COLLATE].buf = bufp;
return 0; return 0;
} }