📄 c_locale_glibc2.c
字号:
#include <locale.h>
#include <langinfo.h>
#include <stl/c_locale.h>
#include <stdio.h>
#include <wctype.h>
#include <string.h>
#include <stdint.h>
/* Structure describing locale data in core for a category. */
/* GLIBC internal, see <glibc catalog>/locale/localeinfo.h */
#if (__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ > 2))
/* GLIBC 2.3.x */
struct locale_data
{
const char *name;
const char *filedata; /* Region mapping the file data. */
off_t filesize; /* Size of the file (and the region). */
enum /* Flavor of storage used for those. */
{
ld_malloced, /* Both are malloc'd. */
ld_mapped, /* name is malloc'd, filedata mmap'd */
ld_archive /* Both point into mmap'd archive regions. */
} alloc;
/* This provides a slot for category-specific code to cache data computed
* about this locale. That code can set a cleanup function to deallocate
* the data.
struct
{
void (*cleanup) (struct locale_data *);
union
{
void *data;
struct lc_time_data *time;
const struct gconv_fcts *ctype;
};
} private;
*/
unsigned int usage_count; /* Counter for users. */
int use_translit; /* Nonzero if the mb*towv*() and wc*tomb()
functions should use transliteration. */
unsigned int nstrings; /* Number of strings below. */
union locale_data_value
{
const uint32_t *wstr;
const char *string;
unsigned int word; /* Note endian issues vs 64-bit pointers. */
}
values[1]; /* Items, usually pointers into `filedata'. */
};
#else /* GLIBC 2.2.x */
struct locale_data
{
const char *name;
const char *filedata; /* Region mapping the file data. */
off_t filesize; /* Size of the file (and the region). */
int mmaped; /* If nonzero the data is mmaped. */
unsigned int usage_count; /* Counter for users. */
int use_translit; /* Nonzero if the mb*towv*() and wc*tomb()
functions should use transliteration. */
const char *options; /* Extra options from the locale name,
not used in the path to the locale data. */
unsigned int nstrings; /* Number of strings below. */
union locale_data_value
{
const uint32_t *wstr;
const char *string;
unsigned int word;
}
values[1]; /* Items, usually pointers into `filedata'. */
};
#endif
typedef __locale_t __c_locale;
#if (__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ > 2))
# define __nl_langinfo_l nl_langinfo_l
#endif
#if (__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ > 2))
# define __LOCALE_CREATE(nm,category) (void*)newlocale(1 << category, nm, NULL )
# define __LOCALE_DESTROY(__loc) freelocale((__c_locale)__loc)
#else
# define __LOCALE_CREATE(nm,category) (void*)__newlocale(1 << category, nm, NULL )
# define __LOCALE_DESTROY(__loc) __freelocale((__c_locale)__loc)
#endif
static const char *_empty_str = "";
static const char *_C_name = "C";
#if 0
struct _Locale_ctype
{
__c_locale __cloc;
};
struct _Locale_numeric
{
__c_locale __cloc;
};
struct _Locale_time
{
__c_locale __cloc;
};
struct _Locale_collate
{
__c_locale __cloc;
};
struct _Locale_monetary
{
__c_locale __cloc;
};
struct _Locale_messages
{
__c_locale __cloc;
};
#endif
void _Locale_init()
{}
void _Locale_final()
{}
void *_Locale_ctype_create( const char *nm, struct _Locale_name_hint* hint )
{ return __LOCALE_CREATE( nm, LC_CTYPE ); }
void* _Locale_numeric_create( const char *nm, struct _Locale_name_hint* hint )
{ return __LOCALE_CREATE( nm, LC_NUMERIC ); }
void* _Locale_time_create( const char *nm, struct _Locale_name_hint* hint )
{ return __LOCALE_CREATE( nm, LC_TIME ); }
void *_Locale_collate_create( const char *nm, struct _Locale_name_hint* hint )
{ return __LOCALE_CREATE( nm, LC_COLLATE ); }
void *_Locale_monetary_create( const char *nm, struct _Locale_name_hint* hint )
{ return __LOCALE_CREATE( nm, LC_MONETARY ); }
void *_Locale_messages_create( const char *nm, struct _Locale_name_hint* hint )
{ return __LOCALE_CREATE( nm, LC_MESSAGES ); }
/*
try to see locale category LC should be used from environment;
according POSIX, the order is
1. LC_ALL
2. category (LC_CTYPE, LC_NUMERIC, ... )
3. LANG
If set nothing, return "C" (this really implemetation-specific).
*/
const char *_Locale_aux_default( const char *LC, char *nm )
{
char *name = getenv( "LC_ALL" );
if ( name != NULL && *name != 0 ) {
return strncpy( nm, name, _Locale_MAX_SIMPLE_NAME );
}
name = getenv( LC );
if ( name != NULL && *name != 0 ) {
return strncpy( nm, name, _Locale_MAX_SIMPLE_NAME );
}
name = getenv( "LANG" );
if ( name != NULL && *name != 0 ) {
return strncpy( nm, name, _Locale_MAX_SIMPLE_NAME );
}
return strcpy( nm, "C" );
}
const char *_Locale_ctype_default( char *nm )
{
return _Locale_aux_default( "LC_CTYPE", nm );
}
const char *_Locale_numeric_default( char *nm )
{
return _Locale_aux_default( "LC_NUMERIC", nm );
}
const char *_Locale_time_default( char *nm )
{
return _Locale_aux_default( "LC_TIME", nm );
}
const char *_Locale_collate_default( char *nm )
{
return _Locale_aux_default( "LC_COLLATE", nm );
}
const char *_Locale_monetary_default( char *nm )
{
return _Locale_aux_default( "LC_MONETARY", nm );
}
const char *_Locale_messages_default( char *nm )
{
return _Locale_aux_default( "LC_MESSAGES", nm );
}
char const*_Locale_ctype_name( const void *__loc, char *buf )
{
return __loc != 0 ? strncpy( buf, ((__c_locale)__loc)->__locales[LC_CTYPE]->name, _Locale_MAX_SIMPLE_NAME ) : 0;
}
char const*_Locale_numeric_name( const void *__loc, char *buf )
{
return __loc != 0 ? strncpy( buf, ((__c_locale)__loc)->__locales[LC_NUMERIC]->name, _Locale_MAX_SIMPLE_NAME ) : 0;
}
char const*_Locale_time_name( const void *__loc, char *buf )
{
return __loc != 0 ? strncpy( buf, ((__c_locale)__loc)->__locales[LC_TIME]->name, _Locale_MAX_SIMPLE_NAME ) : 0;
}
char const*_Locale_collate_name( const void *__loc, char *buf )
{
return __loc != 0 ? strncpy( buf, ((__c_locale)__loc)->__locales[LC_COLLATE]->name, _Locale_MAX_SIMPLE_NAME ) : 0;
}
char const*_Locale_monetary_name( const void *__loc, char *buf )
{
return __loc != 0 ? strncpy( buf, ((__c_locale)__loc)->__locales[LC_MONETARY]->name, _Locale_MAX_SIMPLE_NAME ) : 0;
}
char const*_Locale_messages_name( const void *__loc, char *buf )
{
return __loc != 0 ? strncpy( buf, ((__c_locale)__loc)->__locales[LC_MESSAGES]->name, _Locale_MAX_SIMPLE_NAME ) : 0;
}
void _Locale_ctype_destroy( void *__loc )
{ __LOCALE_DESTROY(__loc); }
void _Locale_numeric_destroy( void *__loc )
{ __LOCALE_DESTROY(__loc); }
void _Locale_time_destroy( void *__loc )
{ __LOCALE_DESTROY(__loc); }
void _Locale_collate_destroy( void *__loc )
{ __LOCALE_DESTROY(__loc); }
void _Locale_monetary_destroy( void *__loc )
{ __LOCALE_DESTROY(__loc); }
void _Locale_messages_destroy( void* __loc )
{ __LOCALE_DESTROY(__loc); }
/*
* locale loc expected either locale name indeed (platform-specific)
* or string like "LC_CTYPE=LocaleNameForCType;LC_NUMERIC=LocaleNameForNum;"
*
*/
char const*__Extract_locale_name( const char *loc, const char *category, char *buf )
{
char *expr;
size_t len_name;
buf[0] = 0;
if( loc[0]=='L' && loc[1]=='C' && loc[2]=='_') {
expr = strstr( (char*)loc, category );
if ( expr == NULL )
return NULL; /* Category not found. */
++expr;
len_name = strcspn( expr, ";" );
len_name = len_name >= _Locale_MAX_SIMPLE_NAME ? _Locale_MAX_SIMPLE_NAME - 1: len_name;
strncpy( buf, expr, len_name );
buf[len_name] = 0;
return buf;
}
return strncpy( buf, loc, _Locale_MAX_SIMPLE_NAME );
}
char const*_Locale_extract_ctype_name( const char *loc, char *buf, struct _Locale_name_hint* hint )
{ return __Extract_locale_name( loc, "LC_CTYPE=", buf ); }
char const*_Locale_extract_numeric_name( const char *loc, char *buf, struct _Locale_name_hint* hint )
{ return __Extract_locale_name( loc, "LC_NUMERIC=", buf ); }
char const*_Locale_extract_time_name( const char *loc, char *buf, struct _Locale_name_hint* hint )
{ return __Extract_locale_name( loc, "LC_TIME=", buf ); }
char const*_Locale_extract_collate_name( const char *loc, char *buf, struct _Locale_name_hint* hint )
{ return __Extract_locale_name( loc, "LC_COLLATE=", buf ); }
char const*_Locale_extract_monetary_name( const char *loc, char *buf, struct _Locale_name_hint* hint )
{ return __Extract_locale_name( loc, "LC_MONETARY=", buf ); }
char const*_Locale_extract_messages_name( const char *loc, char *buf, struct _Locale_name_hint* hint )
{ return __Extract_locale_name( loc, "LC_MESSAGES=", buf ); }
char const*_Locale_compose_name(char*__DUMMY_PAR1, const char*__DUMMY_PAR2, const char*__DUMMY_PAR3,
const char*__DUMMY_PAR4,
const char*__DUMMY_PAR5, const char*__DUMMY_PAR6, const char*__DUMMY_PAR7, const char*__DUMMY_PAR8) {
/* TODO: what's this? Is this a marker that this is not yet completely
implemented? Copy the implementation from c_locale_win32 perhaps? It seems
to complement the extract functions above. */
printf( "%s:%d\n", __FILE__, __LINE__ );
return 0;
}
struct _Locale_name_hint* _Locale_get_ctype_hint(struct _Locale_ctype* ctype)
{ return 0; }
struct _Locale_name_hint* _Locale_get_numeric_hint(struct _Locale_numeric* numeric)
{ return 0; }
struct _Locale_name_hint* _Locale_get_time_hint(struct _Locale_time* time)
{ return 0; }
struct _Locale_name_hint* _Locale_get_collate_hint(struct _Locale_collate* collate)
{ return 0; }
struct _Locale_name_hint* _Locale_get_monetary_hint(struct _Locale_monetary* monetary)
{ return 0; }
struct _Locale_name_hint* _Locale_get_messages_hint(struct _Locale_messages* messages)
{ return 0; }
/* ctype */
const _Locale_mask_t *_Locale_ctype_table( struct _Locale_ctype *__loc )
{
/* return table with masks (upper, lower, alpha, etc.) */
/* return ((__c_locale)__loc)->__locales[LC_CTYPE]->values[_NL_ITEM_INDEX (_NL_CTYPE_CLASS)].string + 128; */
return ((__c_locale)__loc)->__ctype_b;
}
int _Locale_toupper( struct _Locale_ctype *__loc, int c )
{ return ((__c_locale)__loc)->__ctype_toupper[c]; }
int _Locale_tolower( struct _Locale_ctype *__loc, int c )
{ return ((__c_locale)__loc)->__ctype_tolower[c]; }
#if !defined (_STLP_NO_WCHAR_T)
_Locale_mask_t _Locale_wchar_ctype(struct _Locale_ctype*__DUMMY_PAR1, wint_t __DUMMY_PAR2, _Locale_mask_t __DUMMY_PAR3) {
printf( "%s:%d\n", __FILE__, __LINE__ );
return 0;
}
wint_t _Locale_wchar_tolower( struct _Locale_ctype *__loc, wint_t c ) {
printf( "%s:%d\n", __FILE__, __LINE__ );
return __towlower_l( c, ((__c_locale)__loc) );
}
wint_t _Locale_wchar_toupper( struct _Locale_ctype *__loc, wint_t c ) {
printf( "%s:%d\n", __FILE__, __LINE__ );
return __towupper_l( c, ((__c_locale)__loc) );
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -