Ruby  2.1.10p492(2016-04-01revision54464)
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 "ruby/ruby.h"
13 #include "ruby/encoding.h"
14 #include "internal.h"
15 #ifdef __CYGWIN__
16 #include <windows.h>
17 #endif
18 #ifdef HAVE_LANGINFO_H
19 #include <langinfo.h>
20 #endif
21 
22 VALUE
24 {
25 #if defined NO_LOCALE_CHARMAP
26 # error NO_LOCALE_CHARMAP defined
27 #elif defined _WIN32 || defined __CYGWIN__
28  const char *codeset = 0;
29  char cp[sizeof(int) * 3 + 4];
30 # ifdef __CYGWIN__
31  const char *nl_langinfo_codeset(void);
32  codeset = nl_langinfo_codeset();
33 # endif
34  if (!codeset) {
35  UINT codepage = GetConsoleCP();
36  if (!codepage) codepage = GetACP();
37  snprintf(cp, sizeof(cp), "CP%d", codepage);
38  codeset = cp;
39  }
40  return rb_usascii_str_new2(codeset);
41 #elif defined HAVE_LANGINFO_H
42  char *codeset;
43  codeset = nl_langinfo(CODESET);
44  return rb_usascii_str_new2(codeset);
45 #else
46  return Qnil;
47 #endif
48 }
49 
50 int
52 {
53  int idx;
54 #if defined NO_LOCALE_CHARMAP
55 # error NO_LOCALE_CHARMAP defined
56 #elif defined _WIN32 || defined __CYGWIN__
57  char cp[sizeof(int) * 8 / 3 + 4];
58  snprintf(cp, sizeof cp, "CP%d", AreFileApisANSI() ? GetACP() : GetOEMCP());
59  idx = rb_enc_find_index(cp);
60  if (idx < 0) idx = ENCINDEX_ASCII;
61 #else
63 #endif
64  return idx;
65 }
#define rb_usascii_str_new2
Definition: intern.h:846
const char * nl_langinfo_codeset(void)
Definition: langinfo.c:64
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1366
#define snprintf
Definition: subst.h:6
#define Qnil
Definition: ruby.h:427
unsigned long VALUE
Definition: ruby.h:88
VALUE rb_locale_charmap(VALUE klass)
Definition: localeinit.c:23
int Init_enc_set_filesystem_encoding(void)
Definition: localeinit.c:51
#define rb_enc_to_index(enc)
Definition: encoding.h:77
int rb_enc_find_index(const char *name)
Definition: encoding.c:684