1 /**********************************************************************
6 created at: Thu Jul 11 22:09:57 JST 2013
8 Copyright (C) 2013 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/encoding.h"
18 #ifdef HAVE_LANGINFO_H
22 #if defined _WIN32 || defined __CYGWIN__
23 #define SIZEOF_CP_NAME ((sizeof(UINT) * 8 / 3) + 4)
24 #define CP_FORMAT(buf, codepage) snprintf(buf, sizeof(buf), "CP%u", (codepage))
26 extern UINT ruby_w32_codepage
[2];
29 #ifndef NO_LOCALE_CHARMAP
30 # if defined _WIN32 || defined __CYGWIN__ || defined HAVE_LANGINFO_H
31 # define NO_LOCALE_CHARMAP 0
33 # define NO_LOCALE_CHARMAP 1
37 #if !NO_LOCALE_CHARMAP
39 locale_charmap(VALUE (*conv
)(const char *))
41 const char *codeset
= 0;
42 #if defined _WIN32 || defined __CYGWIN__
43 char cp
[SIZEOF_CP_NAME
];
45 const char *nl_langinfo_codeset(void);
46 codeset
= nl_langinfo_codeset();
49 UINT codepage
= ruby_w32_codepage
[0];
50 if (!codepage
) codepage
= GetConsoleCP();
51 if (!codepage
) codepage
= GetACP();
52 CP_FORMAT(cp
, codepage
);
55 #elif defined HAVE_LANGINFO_H
56 codeset
= nl_langinfo(CODESET
);
59 # error locale_charmap() is not implemented
61 return (*conv
)(codeset
);
67 * Encoding.locale_charmap -> string
69 * Returns the locale charmap name.
70 * It returns nil if no appropriate information.
74 * Encoding.locale_charmap #=> "ANSI_X3.4-1968"
76 * Encoding.locale_charmap #=> "EUC-JP"
80 * Encoding.locale_charmap #=> "646"
82 * Encoding.locale_charmap #=> "eucJP"
84 * The result is highly platform dependent.
85 * So Encoding.find(Encoding.locale_charmap) may cause an error.
86 * If you need some encoding object even for unknown locale,
87 * Encoding.find("locale") can be used.
91 rb_locale_charmap(VALUE klass
)
94 return rb_usascii_str_new_cstr("US-ASCII");
96 return locale_charmap(rb_usascii_str_new_cstr
);
100 #if !NO_LOCALE_CHARMAP
102 enc_find_index(const char *name
)
104 return (VALUE
)rb_enc_find_index(name
);
109 rb_locale_charmap_index(void)
111 #if NO_LOCALE_CHARMAP
112 return ENCINDEX_US_ASCII
;
114 return (int)locale_charmap(enc_find_index
);
119 Init_enc_set_filesystem_encoding(void)
122 #if NO_LOCALE_CHARMAP
123 idx
= ENCINDEX_US_ASCII
;
125 char cp
[SIZEOF_CP_NAME
];
126 const UINT codepage
= ruby_w32_codepage
[1];
127 if (!codepage
) return ENCINDEX_UTF_8
;
129 CP_FORMAT(cp
, codepage
);
130 idx
= rb_enc_find_index(cp
);
131 if (idx
< 0) idx
= ENCINDEX_ASCII
;
132 #elif defined __CYGWIN__
133 idx
= ENCINDEX_UTF_8
;
135 idx
= rb_enc_to_index(rb_default_external_encoding());