Ruby  2.4.2p198(2017-09-14revision59899)
localeinit.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  localeinit.c -
4 
5  $Author$
6  created at: Thu Jul 11 22:09:57 JST 2013
7 
8  Copyright (C) 2013 Yukihiro Matsumoto
9 
10 **********************************************************************/
11 
12 #include "internal.h"
13 #include "encindex.h"
14 #ifdef __CYGWIN__
15 #include <windows.h>
16 #endif
17 #ifdef HAVE_LANGINFO_H
18 #include <langinfo.h>
19 #endif
20 
21 #if defined _WIN32 || defined __CYGWIN__
22 #define SIZEOF_CP_NAME ((sizeof(UINT) * 8 / 3) + 4)
23 #define CP_FORMAT(buf, codepage) snprintf(buf, sizeof(buf), "CP%u", (codepage))
24 #endif
25 
26 static VALUE
27 locale_charmap(VALUE (*conv)(const char *))
28 {
29 #if defined NO_LOCALE_CHARMAP
30 # error NO_LOCALE_CHARMAP defined
31 #elif defined _WIN32 || defined __CYGWIN__
32  const char *codeset = 0;
33  char cp[SIZEOF_CP_NAME];
34 # ifdef __CYGWIN__
35  const char *nl_langinfo_codeset(void);
36  codeset = nl_langinfo_codeset();
37 # endif
38  if (!codeset) {
39  UINT codepage = GetConsoleCP();
40  if (!codepage) codepage = GetACP();
41  CP_FORMAT(cp, codepage);
42  codeset = cp;
43  }
44  return (*conv)(codeset);
45 #elif defined HAVE_LANGINFO_H
46  char *codeset;
47  codeset = nl_langinfo(CODESET);
48  return (*conv)(codeset);
49 #else
50  return ENCINDEX_US_ASCII;
51 #endif
52 }
53 
54 /*
55  * call-seq:
56  * Encoding.locale_charmap -> string
57  *
58  * Returns the locale charmap name.
59  * It returns nil if no appropriate information.
60  *
61  * Debian GNU/Linux
62  * LANG=C
63  * Encoding.locale_charmap #=> "ANSI_X3.4-1968"
64  * LANG=ja_JP.EUC-JP
65  * Encoding.locale_charmap #=> "EUC-JP"
66  *
67  * SunOS 5
68  * LANG=C
69  * Encoding.locale_charmap #=> "646"
70  * LANG=ja
71  * Encoding.locale_charmap #=> "eucJP"
72  *
73  * The result is highly platform dependent.
74  * So Encoding.find(Encoding.locale_charmap) may cause an error.
75  * If you need some encoding object even for unknown locale,
76  * Encoding.find("locale") can be used.
77  *
78  */
79 VALUE
81 {
83 }
84 
85 static VALUE
86 enc_find_index(const char *name)
87 {
88  return (VALUE)rb_enc_find_index(name);
89 }
90 
91 int
93 {
94  return (int)locale_charmap(enc_find_index);
95 }
96 
97 int
99 {
100  int idx;
101 #if defined NO_LOCALE_CHARMAP
102 # error NO_LOCALE_CHARMAP defined
103 #elif defined _WIN32
104  char cp[SIZEOF_CP_NAME];
105  CP_FORMAT(cp, AreFileApisANSI() ? GetACP() : GetOEMCP());
106  idx = rb_enc_find_index(cp);
107  if (idx < 0) idx = ENCINDEX_ASCII;
108 #elif defined __CYGWIN__
109  idx = ENCINDEX_UTF_8;
110 #else
112 #endif
113  return idx;
114 }
#define ENCINDEX_US_ASCII
Definition: encindex.h:44
const char * nl_langinfo_codeset(void)
Definition: langinfo.c:64
#define ENCINDEX_ASCII
Definition: encindex.h:42
#define ENCINDEX_UTF_8
Definition: encindex.h:43
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1425
int rb_enc_to_index(rb_encoding *enc)
Definition: encoding.c:126
static VALUE enc_find_index(const char *name)
Definition: localeinit.c:86
unsigned long VALUE
Definition: ruby.h:85
int rb_locale_charmap_index(void)
Definition: localeinit.c:92
VALUE rb_locale_charmap(VALUE klass)
Definition: localeinit.c:80
int Init_enc_set_filesystem_encoding(void)
Definition: localeinit.c:98
const char * name
Definition: nkf.c:208
int rb_enc_find_index(const char *name)
Definition: encoding.c:704
static VALUE locale_charmap(VALUE(*conv)(const char *))
Definition: localeinit.c:27
VALUE rb_usascii_str_new_cstr(const char *)
Definition: string.c:777