From 131d85ed01db156cd4f9d42a8a3b810832a52123 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@otacoo.com>
Date: Wed, 23 Mar 2016 16:15:58 +0900
Subject: [PATCH 4/4] Fix code page calculation for Visual Studio 2015

Visual Studio 2015 is missing the declaration of field lc_codepage in
_locale_t, causing the existing code, introduced with VS 2012 to become
incompatible and causing compilation failures. This patch changes the
code path a bit so as GetLocaleInfoEx is used to fetch a code page
where available when compiling the code with VS2015, note that this
is compatible only with Windows 2k8/Vista and newer versions but it is
unlikely that a version of Postgres compiled with VS2015 would run on
platforms older than that, Windows XP being already out of support.
---
 src/port/chklocale.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/port/chklocale.c b/src/port/chklocale.c
index a551fdc..9bd18c6 100644
--- a/src/port/chklocale.c
+++ b/src/port/chklocale.c
@@ -196,6 +196,12 @@ static const struct encoding_match encoding_match_list[] = {
  * locale machinery determine the code page.  See comments at IsoLocaleName().
  * For other compilers, follow the locale's predictable format.
  *
+ * Visual Studio 2015 should still be able to do the same, but the declaration
+ * of lc_codepage is missing in _locale_t, causing this code compilation to
+ * fail, hence this falls back instead on GetLocaleInfoEx. VS 2015 may be an
+ * exception and post-VS2015 versions should be able to handle properly the
+ * codepage number using _create_locale().
+ *
  * Returns a malloc()'d string for the caller to free.
  */
 static char *
@@ -203,7 +209,7 @@ win32_langinfo(const char *ctype)
 {
 	char	   *r = NULL;
 
-#if (_MSC_VER >= 1700)
+#if (_MSC_VER >= 1700) && (_MSC_VER < 1900)
 	_locale_t	loct = NULL;
 
 	loct = _create_locale(LC_CTYPE, ctype);
@@ -215,8 +221,24 @@ win32_langinfo(const char *ctype)
 		_free_locale(loct);
 	}
 #else
+
 	char	   *codepage;
 
+#if (_MSC_VER >= 1900)
+	uint32	cp;
+
+	if (GetLocaleInfoEx(ctype,
+			LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
+			(LPWSTR) &cp, sizeof(cp) / sizeof(WCHAR)) > 0)
+	{
+		r = malloc(16);			/* excess */
+		if (r != NULL)
+			sprintf(r, "CP%u", cp);
+	}
+	else
+	{
+#endif
+
 	/*
 	 * Locale format on Win32 is <Language>_<Country>.<CodePage> . For
 	 * example, English_United States.1252.
@@ -232,6 +254,10 @@ win32_langinfo(const char *ctype)
 		if (r != NULL)
 			sprintf(r, "CP%s", codepage);
 	}
+
+#if (_MSC_VER >= 1900)
+	}
+#endif
 #endif
 
 	return r;
-- 
2.7.3

