/* * call-seq: * Encoding.locale_charmap -> string * * Returns the locale charmap name. * It returns nil if no appropriate information. * * Debian GNU/Linux * LANG=C * Encoding.locale_charmap #=> "ANSI_X3.4-1968" * LANG=ja_JP.EUC-JP * Encoding.locale_charmap #=> "EUC-JP" * * SunOS 5 * LANG=C * Encoding.locale_charmap #=> "646" * LANG=ja * Encoding.locale_charmap #=> "eucJP" * * The result is highly platform dependent. * So Encoding.find(Encoding.locale_charmap) may cause an error. * If you need some encoding object even for unknown locale, * Encoding.find("locale") can be used. * */ VALUE rb_locale_charmap(VALUE klass) { #if defined NO_LOCALE_CHARMAP return rb_usascii_str_new2("ASCII-8BIT"); #elif defined _WIN32 || defined __CYGWIN__ const char *codeset = 0; char cp[sizeof(int) * 3 + 4]; # ifdef __CYGWIN__ const char *nl_langinfo_codeset(void); codeset = nl_langinfo_codeset(); # endif if (!codeset) { UINT codepage = GetConsoleCP(); if (!codepage) codepage = GetACP(); snprintf(cp, sizeof(cp), "CP%d", codepage); codeset = cp; } return rb_usascii_str_new2(codeset); #elif defined HAVE_LANGINFO_H char *codeset; codeset = nl_langinfo(CODESET); return rb_usascii_str_new2(codeset); #else return Qnil; #endif }
static VALUE locale_charmap(VALUE (*conv)(const char *)) { #if defined NO_LOCALE_CHARMAP # error NO_LOCALE_CHARMAP defined #elif defined _WIN32 || defined __CYGWIN__ const char *codeset = 0; char cp[SIZEOF_CP_NAME]; # ifdef __CYGWIN__ const char *nl_langinfo_codeset(void); codeset = nl_langinfo_codeset(); # endif if (!codeset) { UINT codepage = GetConsoleCP(); if (!codepage) codepage = GetACP(); CP_FORMAT(cp, codepage); codeset = cp; } return (*conv)(codeset); #elif defined HAVE_LANGINFO_H char *codeset; codeset = nl_langinfo(CODESET); return (*conv)(codeset); #else return ENCINDEX_US_ASCII; #endif }
char *nl_langinfo(nl_item item) { const char *codeset; if (item != CODESET) return NULL; codeset = nl_langinfo_codeset(); if (!codeset) codeset = C_CODESET; return (char *)codeset; }