Ruby  2.5.0dev(2017-10-22revision60238)
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 
25 extern UINT ruby_w32_codepage[2];
26 #endif
27 
28 #ifndef NO_LOCALE_CHARMAP
29 # if defined _WIN32 || defined __CYGWIN__ || defined HAVE_LANGINFO_H
30 # define NO_LOCALE_CHARMAP 0
31 # else
32 # define NO_LOCALE_CHARMAP 1
33 # endif
34 #endif
35 
36 #if !NO_LOCALE_CHARMAP
37 static VALUE
38 locale_charmap(VALUE (*conv)(const char *))
39 {
40  const char *codeset = 0;
41 #if defined _WIN32 || defined __CYGWIN__
42  char cp[SIZEOF_CP_NAME];
43 # ifdef __CYGWIN__
44  const char *nl_langinfo_codeset(void);
45  codeset = nl_langinfo_codeset();
46 # endif
47  if (!codeset) {
48  UINT codepage = ruby_w32_codepage[0];
49  if (!codepage) codepage = GetConsoleCP();
50  if (!codepage) codepage = GetACP();
51  CP_FORMAT(cp, codepage);
52  codeset = cp;
53  }
54 #elif defined HAVE_LANGINFO_H
55  codeset = nl_langinfo(CODESET);
56  ASSUME(codeset);
57 #else
58 # error locale_charmap() is not implemented
59 #endif
60  return (*conv)(codeset);
61 }
62 #endif
63 
64 /*
65  * call-seq:
66  * Encoding.locale_charmap -> string
67  *
68  * Returns the locale charmap name.
69  * It returns nil if no appropriate information.
70  *
71  * Debian GNU/Linux
72  * LANG=C
73  * Encoding.locale_charmap #=> "ANSI_X3.4-1968"
74  * LANG=ja_JP.EUC-JP
75  * Encoding.locale_charmap #=> "EUC-JP"
76  *
77  * SunOS 5
78  * LANG=C
79  * Encoding.locale_charmap #=> "646"
80  * LANG=ja
81  * Encoding.locale_charmap #=> "eucJP"
82  *
83  * The result is highly platform dependent.
84  * So Encoding.find(Encoding.locale_charmap) may cause an error.
85  * If you need some encoding object even for unknown locale,
86  * Encoding.find("locale") can be used.
87  *
88  */
89 VALUE
91 {
92 #if NO_LOCALE_CHARMAP
93  return rb_usascii_str_new_cstr("US-ASCII");
94 #else
95  return locale_charmap(rb_usascii_str_new_cstr);
96 #endif
97 }
98 
99 #if !NO_LOCALE_CHARMAP
100 static VALUE
101 enc_find_index(const char *name)
102 {
103  return (VALUE)rb_enc_find_index(name);
104 }
105 #endif
106 
107 int
109 {
110 #if NO_LOCALE_CHARMAP
111  return ENCINDEX_US_ASCII;
112 #else
113  return (int)locale_charmap(enc_find_index);
114 #endif
115 }
116 
117 int
119 {
120  int idx;
121 #if NO_LOCALE_CHARMAP
122  idx = ENCINDEX_US_ASCII;
123 #elif defined _WIN32
124  char cp[SIZEOF_CP_NAME];
125  const UINT codepage = ruby_w32_codepage[1] ? ruby_w32_codepage[1] :
126  AreFileApisANSI() ? GetACP() : GetOEMCP();
127  CP_FORMAT(cp, codepage);
128  idx = rb_enc_find_index(cp);
129  if (idx < 0) idx = ENCINDEX_ASCII;
130 #elif defined __CYGWIN__
131  idx = ENCINDEX_UTF_8;
132 #else
134 #endif
135  return idx;
136 }
#define ENCINDEX_US_ASCII
Definition: encindex.h:44
#define ASSUME(x)
Definition: ruby.h:42
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
unsigned long VALUE
Definition: ruby.h:85
int rb_locale_charmap_index(void)
Definition: localeinit.c:108
VALUE rb_locale_charmap(VALUE klass)
Definition: localeinit.c:90
int Init_enc_set_filesystem_encoding(void)
Definition: localeinit.c:118
const char * name
Definition: nkf.c:208
int rb_enc_find_index(const char *name)
Definition: encoding.c:704
VALUE rb_usascii_str_new_cstr(const char *)
Definition: string.c:778