Hi Hrvoje,

 On Tuesday, February 21, 2006 at 21:35:24 +0100, Hrvoje Niksic wrote:

> Valery Kondakoff <[EMAIL PROTECTED]> writes:
>> wrong ANSI/OEM character encoding
> What are the steps a Windows "console" program needs to do to perform
> this conversion correctly?

    Call setlocale(LC_ALL, ".OCP") which will select the default OEM
charset of the current Windows language. OCP means "OEM Code Page", and
console apps by default need to use this OEM charset: Probably CP-852
for you, CP-850 for me, and so on. Here this setlocale .OCP returns
"French_France.850".

    Another possibly better way, able to follow the current charset of
the console (not only the default): Call GetConsoleOutputCP(), get
example 850, build a string ".850" with the dot, and call
setlocale(LC_ALL, ".850"). Problem: Not every combination of language,
country, and charset is possible. So deal with errors (setlocale returns
NULL), and fallback to ".OCP".

    Finally if GetConsoleOutputCP() fails returning 0, call GetACP()
instead, as a fallback. This might eventually suit graphic frontends,
which would need an ANSI codepage output.


    I don't have what's needed to compile wget on Windows, otherwise I
would have done a patch. MinGW32 and MSYS can't build wget, right?
Anyway I attach a demo program:

| C:\home\ab>chcp
| Page de codes active : 850            # French console default
|
| C:\home\ab>win32-console-locale.exe
| locale=French_France.850
| codepage=850
| thousands_sep=" " (code FF)           # no-break space in CP-850
|
| C:\home\ab>chcp 28591                 # that's Latin-1 code page
| Page de codes activeá: 28591
|
| C:\home\ab>win32-console-locale.exe
| locale=French_France.28591
| codepage=28591
| thousands_sep="á" (code A0)           # no-break space in Latin-1


Bye!    Alain.
-- 
When you post a new message, beginning a new topic, use the "mail" or
"post" or "new message" functions.
When you reply or followup, use the "reply" or "followup" functions.
Do not do the one for the other, this breaks or hijacks threads.
#include <stdio.h>
#include <locale.h>
#include <windows.h>

Set_the_locale_for_the_fine_win32_console () {
  char *locale;
  int codepage;
  char param[42];

  codepage=GetConsoleOutputCP();
  if (codepage) {
    sprintf(param, ".%d", codepage);
    locale=setlocale(LC_ALL, param);            /* use current console OEM 
charset */
    if (locale == NULL) {
      locale=setlocale(LC_ALL, ".OCP");         /* use system default OEM 
charset */
    }
  }
  else {
    locale=setlocale(LC_ALL, "");               /* use ANSI charset (for 
graphic apps) */
  }

  printf("locale=%s\ncodepage=%d\n", locale, codepage ? codepage : GetACP());
}

main () {
  struct lconv *lconv;

  Set_the_locale_for_the_fine_win32_console();

  lconv=localeconv();
  printf("thousands_sep=\"%s\" (code %02X)\n",
        lconv->thousands_sep,
        (unsigned char)lconv->thousands_sep[0]);
}

Reply via email to