*** chklocale.c	Wed Oct  3 17:15:02 2007
--- chklocale.c.fix	Wed Oct  3 16:36:26 2007
***************
*** 27,34 ****
  #include "mb/pg_wchar.h"
  
  
- #if defined(HAVE_LANGINFO_H) && defined(CODESET)
- 
  /*
   * This table needs to recognize all the CODESET spellings for supported
   * backend encodings, as well as frontend-only encodings where possible
--- 27,32 ----
*************** static const struct encoding_match encod
*** 49,71 ****
--- 47,73 ----
  	{PG_EUC_JP, "eucJP"},
  	{PG_EUC_JP, "IBM-eucJP"},
  	{PG_EUC_JP, "sdeckanji"},
+ 	{PG_EUC_JP, "CP20932"},
  
  	{PG_EUC_CN, "EUC-CN"},
  	{PG_EUC_CN, "eucCN"},
  	{PG_EUC_CN, "IBM-eucCN"},
  	{PG_EUC_CN, "GB2312"},
  	{PG_EUC_CN, "dechanzi"},
+ 	{PG_EUC_CN, "CP20936"},
  
  	{PG_EUC_KR, "EUC-KR"},
  	{PG_EUC_KR, "eucKR"},
  	{PG_EUC_KR, "IBM-eucKR"},
  	{PG_EUC_KR, "deckorean"},
  	{PG_EUC_KR, "5601"},
+ 	{PG_EUC_KR, "CP51949"},	/* or 20949 ? */
  
  	{PG_EUC_TW, "EUC-TW"},
  	{PG_EUC_TW, "eucTW"},
  	{PG_EUC_TW, "IBM-eucTW"},
  	{PG_EUC_TW, "cns11643"},
+ 	/* No codepage for EUC-TW ? */
  
  	{PG_UTF8, "UTF-8"},
  	{PG_UTF8, "utf8"},
*************** static const struct encoding_match encod
*** 111,116 ****
--- 113,119 ----
  	{PG_LATIN10, "iso885916"},
  
  	{PG_KOI8R, "KOI8-R"},
+ 	{PG_KOI8R, "CP20866"},
  
  	{PG_WIN1252, "CP1252"},
  	{PG_WIN1253, "CP1253"},
*************** static const struct encoding_match encod
*** 143,165 ****
--- 146,197 ----
  
  	{PG_SJIS, "SJIS"},
  	{PG_SJIS, "PCK"},
+ 	{PG_SJIS, "CP932"},
  
  	{PG_BIG5, "BIG5"},
  	{PG_BIG5, "BIG5HKSCS"},
+ 	{PG_SJIS, "CP950"},
  
  	{PG_GBK, "GBK"},
+ 	{PG_SJIS, "CP936"},
  
  	{PG_UHC, "UHC"},
  
  	{PG_JOHAB, "JOHAB"},
+ 	{PG_JOHAB, "CP1361"},
  
  	{PG_GB18030, "GB18030"},
+ 	{PG_GB18030, "CP54936"},
  
  	{PG_SHIFT_JIS_2004, "SJIS_2004"},
  
  	{PG_SQL_ASCII, NULL}		/* end marker */
  };
  
+ #ifdef WIN32
+ static char *
+ win32_langinfo(const char* ctype)
+ {
+ 	char	   *r;
+ 	char	   *codepage;
+ 	int			ln;
+ 
+ 	/*
+ 	 * Locale format on Win32 is <Language>_<Country>.<CodePage> .
+ 	 * For example, English_USA.1252.
+ 	 */
+ 	if (!ctype || (codepage = strrchr(ctype, '.')) == NULL)
+ 		return NULL;
+ 	codepage++;
+ 	ln = strlen(codepage);
+ 	r = malloc(ln + 3);
+ 	sprintf(r, "CP%s", codepage);
+ 
+ 	return r;
+ }
+ #endif
+ 
+ #if (defined(HAVE_LANGINFO_H) && defined(CODESET)) || defined(WIN32)
  
  /*
   * Given a setting for LC_CTYPE, return the Postgres ID of the associated
*************** pg_get_encoding_from_locale(const char *
*** 181,186 ****
--- 213,219 ----
  	if (ctype)
  	{
  		char	   *save;
+ 		char	   *name;
  
  		save = setlocale(LC_CTYPE, NULL);
  		if (!save)
*************** pg_get_encoding_from_locale(const char *
*** 190,204 ****
  		if (!save)
  			return PG_SQL_ASCII;		/* out of memory; unlikely */
  
! 		if (!setlocale(LC_CTYPE, ctype))
  		{
  			free(save);
  			return PG_SQL_ASCII;		/* bogus ctype passed in? */
  		}
  
  		sys = nl_langinfo(CODESET);
  		if (sys)
  			sys = strdup(sys);
  
  		setlocale(LC_CTYPE, save);
  		free(save);
--- 223,242 ----
  		if (!save)
  			return PG_SQL_ASCII;		/* out of memory; unlikely */
  
! 		name = setlocale(LC_CTYPE, ctype);
! 		if (!name)
  		{
  			free(save);
  			return PG_SQL_ASCII;		/* bogus ctype passed in? */
  		}
  
+ #ifndef WIN32
  		sys = nl_langinfo(CODESET);
  		if (sys)
  			sys = strdup(sys);
+ #else
+ 		sys = win32_langinfo(name);
+ #endif
  
  		setlocale(LC_CTYPE, save);
  		free(save);
*************** pg_get_encoding_from_locale(const char *
*** 209,217 ****
--- 247,259 ----
  		ctype = setlocale(LC_CTYPE, NULL);
  		if (!ctype)
  			return PG_SQL_ASCII;		/* setlocale() broken? */
+ #ifndef WIN32
  		sys = nl_langinfo(CODESET);
  		if (sys)
  			sys = strdup(sys);
+ #else
+ 		sys = win32_langinfo(ctype);
+ #endif
  	}
  
  	if (!sys)
*************** pg_get_encoding_from_locale(const char *
*** 268,274 ****
  	return PG_SQL_ASCII;
  }
  
! #else /* !(HAVE_LANGINFO_H && CODESET) */
  
  /*
   * stub if no platform support
--- 310,316 ----
  	return PG_SQL_ASCII;
  }
  
! #else /* (HAVE_LANGINFO_H && CODESET) || WIN32 */
  
  /*
   * stub if no platform support
*************** pg_get_encoding_from_locale(const char *
*** 279,282 ****
  	return PG_SQL_ASCII;
  }
  
! #endif /* HAVE_LANGINFO_H && CODESET */
--- 321,324 ----
  	return PG_SQL_ASCII;
  }
  
! #endif /* (HAVE_LANGINFO_H && CODESET) || WIN32 */