From 0ddcb9489ec40b8b59ec3026c245f2fe1b83e2c2 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 18 Jan 2014 06:56:30 +0000 Subject: localeinit.c: codepage format * localeinit.c (SIZEOF_CP_NAME, CP_FORMAT): extract to share codepage formatting code. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44635 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- localeinit.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'localeinit.c') diff --git a/localeinit.c b/localeinit.c index 369013fc26..420484a463 100644 --- a/localeinit.c +++ b/localeinit.c @@ -19,6 +19,11 @@ #include #endif +#if defined _WIN32 +#define SIZEOF_CP_NAME ((sizeof(UINT) * 8 / 3) + 4) +#define CP_FORMAT(buf, codepage) snprintf(buf, sizeof(buf), "CP%u", (codepage)) +#endif + VALUE rb_locale_charmap(VALUE klass) { @@ -26,7 +31,7 @@ rb_locale_charmap(VALUE klass) # error NO_LOCALE_CHARMAP defined #elif defined _WIN32 || defined __CYGWIN__ const char *codeset = 0; - char cp[sizeof(int) * 3 + 4]; + char cp[SIZEOF_CP_NAME]; # ifdef __CYGWIN__ const char *nl_langinfo_codeset(void); codeset = nl_langinfo_codeset(); @@ -34,7 +39,7 @@ rb_locale_charmap(VALUE klass) if (!codeset) { UINT codepage = GetConsoleCP(); if (!codepage) codepage = GetACP(); - snprintf(cp, sizeof(cp), "CP%d", codepage); + CP_FORMAT(cp, codepage); codeset = cp; } return rb_usascii_str_new2(codeset); @@ -54,8 +59,8 @@ Init_enc_set_filesystem_encoding(void) #if defined NO_LOCALE_CHARMAP # error NO_LOCALE_CHARMAP defined #elif defined _WIN32 || defined __CYGWIN__ - char cp[sizeof(int) * 8 / 3 + 4]; - snprintf(cp, sizeof cp, "CP%d", AreFileApisANSI() ? GetACP() : GetOEMCP()); + char cp[SIZEOF_CP_NAME]; + CP_FORMAT(cp, AreFileApisANSI() ? GetACP() : GetOEMCP()); idx = rb_enc_find_index(cp); if (idx < 0) idx = ENCINDEX_ASCII; #else -- cgit v1.2.3