2013-07-14 21:47:25 +04:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
localeinit.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Thu Jul 11 22:09:57 JST 2013
|
|
|
|
|
|
|
|
Copyright (C) 2013 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2018-01-09 09:24:11 +03:00
|
|
|
#include "ruby/encoding.h"
|
2013-07-14 21:47:25 +04:00
|
|
|
#include "internal.h"
|
2015-09-15 08:57:30 +03:00
|
|
|
#include "encindex.h"
|
2013-07-14 21:47:25 +04:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LANGINFO_H
|
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2014-01-26 03:42:16 +04:00
|
|
|
#if defined _WIN32 || defined __CYGWIN__
|
2014-01-18 10:56:30 +04:00
|
|
|
#define SIZEOF_CP_NAME ((sizeof(UINT) * 8 / 3) + 4)
|
|
|
|
#define CP_FORMAT(buf, codepage) snprintf(buf, sizeof(buf), "CP%u", (codepage))
|
|
|
|
|
2017-06-06 06:45:02 +03:00
|
|
|
extern UINT ruby_w32_codepage[2];
|
2017-03-31 03:35:02 +03:00
|
|
|
#endif
|
|
|
|
|
2017-03-30 18:20:52 +03:00
|
|
|
#ifndef NO_LOCALE_CHARMAP
|
|
|
|
# if defined _WIN32 || defined __CYGWIN__ || defined HAVE_LANGINFO_H
|
|
|
|
# define NO_LOCALE_CHARMAP 0
|
|
|
|
# else
|
|
|
|
# define NO_LOCALE_CHARMAP 1
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !NO_LOCALE_CHARMAP
|
2015-08-24 10:01:22 +03:00
|
|
|
static VALUE
|
|
|
|
locale_charmap(VALUE (*conv)(const char *))
|
2013-07-14 21:47:25 +04:00
|
|
|
{
|
|
|
|
const char *codeset = 0;
|
2017-03-30 18:20:52 +03:00
|
|
|
#if defined _WIN32 || defined __CYGWIN__
|
2014-01-18 10:56:30 +04:00
|
|
|
char cp[SIZEOF_CP_NAME];
|
2013-07-14 21:47:25 +04:00
|
|
|
# ifdef __CYGWIN__
|
|
|
|
const char *nl_langinfo_codeset(void);
|
|
|
|
codeset = nl_langinfo_codeset();
|
|
|
|
# endif
|
|
|
|
if (!codeset) {
|
2017-06-06 06:45:02 +03:00
|
|
|
UINT codepage = ruby_w32_codepage[0];
|
2017-03-31 03:35:02 +03:00
|
|
|
if (!codepage) codepage = GetConsoleCP();
|
2013-07-14 21:47:25 +04:00
|
|
|
if (!codepage) codepage = GetACP();
|
2014-01-18 10:56:30 +04:00
|
|
|
CP_FORMAT(cp, codepage);
|
2013-07-14 21:47:25 +04:00
|
|
|
codeset = cp;
|
|
|
|
}
|
|
|
|
#elif defined HAVE_LANGINFO_H
|
|
|
|
codeset = nl_langinfo(CODESET);
|
2017-03-30 18:20:52 +03:00
|
|
|
ASSUME(codeset);
|
2013-07-14 21:47:25 +04:00
|
|
|
#else
|
2017-03-30 18:20:52 +03:00
|
|
|
# error locale_charmap() is not implemented
|
2013-07-14 21:47:25 +04:00
|
|
|
#endif
|
2017-03-30 18:20:52 +03:00
|
|
|
return (*conv)(codeset);
|
2013-07-14 21:47:25 +04:00
|
|
|
}
|
2017-03-30 18:20:52 +03:00
|
|
|
#endif
|
2013-07-14 21:47:25 +04:00
|
|
|
|
2015-09-07 08:47:34 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2015-08-24 10:01:22 +03:00
|
|
|
VALUE
|
|
|
|
rb_locale_charmap(VALUE klass)
|
|
|
|
{
|
2017-03-30 18:20:52 +03:00
|
|
|
#if NO_LOCALE_CHARMAP
|
|
|
|
return rb_usascii_str_new_cstr("US-ASCII");
|
|
|
|
#else
|
2015-08-24 10:01:22 +03:00
|
|
|
return locale_charmap(rb_usascii_str_new_cstr);
|
2017-03-30 18:20:52 +03:00
|
|
|
#endif
|
2015-08-24 10:01:22 +03:00
|
|
|
}
|
|
|
|
|
2017-03-30 18:20:52 +03:00
|
|
|
#if !NO_LOCALE_CHARMAP
|
2015-08-24 10:01:22 +03:00
|
|
|
static VALUE
|
|
|
|
enc_find_index(const char *name)
|
|
|
|
{
|
|
|
|
return (VALUE)rb_enc_find_index(name);
|
|
|
|
}
|
2017-03-30 18:20:52 +03:00
|
|
|
#endif
|
2015-08-24 10:01:22 +03:00
|
|
|
|
|
|
|
int
|
2016-03-26 00:35:39 +03:00
|
|
|
rb_locale_charmap_index(void)
|
2015-08-24 10:01:22 +03:00
|
|
|
{
|
2017-03-30 18:20:52 +03:00
|
|
|
#if NO_LOCALE_CHARMAP
|
|
|
|
return ENCINDEX_US_ASCII;
|
|
|
|
#else
|
2015-08-24 10:01:22 +03:00
|
|
|
return (int)locale_charmap(enc_find_index);
|
2017-03-30 18:20:52 +03:00
|
|
|
#endif
|
2015-08-24 10:01:22 +03:00
|
|
|
}
|
|
|
|
|
2013-07-14 21:47:25 +04:00
|
|
|
int
|
|
|
|
Init_enc_set_filesystem_encoding(void)
|
|
|
|
{
|
|
|
|
int idx;
|
2017-03-30 18:20:52 +03:00
|
|
|
#if NO_LOCALE_CHARMAP
|
|
|
|
idx = ENCINDEX_US_ASCII;
|
2016-06-19 09:01:26 +03:00
|
|
|
#elif defined _WIN32
|
2014-01-18 10:56:30 +04:00
|
|
|
char cp[SIZEOF_CP_NAME];
|
2020-12-19 20:16:31 +03:00
|
|
|
const UINT codepage = ruby_w32_codepage[1];
|
|
|
|
if (!codepage) return ENCINDEX_UTF_8;
|
|
|
|
/* for debugging */
|
2017-03-31 03:35:02 +03:00
|
|
|
CP_FORMAT(cp, codepage);
|
2013-07-14 21:47:25 +04:00
|
|
|
idx = rb_enc_find_index(cp);
|
2022-07-13 12:54:08 +03:00
|
|
|
if (idx < 0) idx = ENCINDEX_ASCII_8BIT;
|
2016-06-19 09:01:25 +03:00
|
|
|
#elif defined __CYGWIN__
|
|
|
|
idx = ENCINDEX_UTF_8;
|
2013-07-14 21:47:25 +04:00
|
|
|
#else
|
|
|
|
idx = rb_enc_to_index(rb_default_external_encoding());
|
|
|
|
#endif
|
|
|
|
return idx;
|
|
|
|
}
|