How-to: input/output "Japanese Characters"?
Carlos Smith
carlos-smith at sympatico.ca
Sun May 3 08:21:56 PDT 2009
"Georg Wrede" <georg.wrede at iki.fi> a écrit dans le message de news:
> import std.stdio;
>
> void main()
> {
> auto lin = readln();
> writeln(lin);
> }
>
> Works with your Japanese strings. It just works(tm).
I am trying this on a North American WinXP station.
I am using DMD V1.
You will find a small program at the end of this
message who does not work for me.
The console use Lucida COnsole font. The CP is 65001.
cmd.exe can read/write utf strings, but not my
program.
I want to be able to read an utf string from stdin.
That string will contains characters > 256, ie glyphs
you can only see with an unicode font.
I have read some documentation on MSDN that confirm
it can't work. So why does it work for you ?
Can anyone, explain to me what am i doing wrong ?
Thanks a lot...
--------- program ----------
//
// readln.d
// testing readln()
//
// This file saved with BOM: FF FE
// Edit with Lucida Console font
//
import std.stdio;
import std.c.windows.windows;
import std.c.locale;
import std.utf;
extern( Windows )
{
HANDLE GetStdHandle( DWORD nStdHandle );
BOOL WriteConsoleW(
HANDLE hConsoleOutput,
in VOID* lpBuffer,
DWORD nNumberOfCharsToWrite,
DWORD* lpNumberOfCharsWritten,
void* lpReserved = null );
BOOL WriteConsoleA(
HANDLE hConsoleOutput,
in VOID* lpBuffer,
DWORD nNumberOfCharsToWrite,
DWORD* lpNumberOfCharsWritten,
void* lpReserved = null );
BOOL ReadConsoleW( HANDLE hConsoleInput, LPVOID lpBuffer, uint
nNumberOfCharsToRead,
uint* lpNumberOfCharsRead, void* pInputControl = null );
BOOL ReadConsoleA( HANDLE hConsoleInput, LPVOID lpBuffer, uint
nNumberOfCharsToRead,
uint* lpNumberOfCharsRead, void* pInputControl = null );
version (UNICODE)
alias ReadConsoleA ReadConsole;
else
alias ReadConsoleW ReadConsole;
}
UINT CP, OCP;
void DisplayCP()
{
writef("Console CP: %s\n", GetConsoleCP());
writef("Console Output CP: %s\n", GetConsoleOutputCP());
}
void SaveCP()
{
CP = GetConsoleCP();
OCP = GetConsoleOutputCP();
}
void SetUnicode()
{
SetConsoleCP( 65001 );
SetConsoleOutputCP( 65001 );
}
void ResetCP()
{
SetConsoleCP( CP );
SetConsoleOutputCP( OCP );
}
int main( )
{
HANDLE cout;
DWORD nw;
cout = GetStdHandle(STD_OUTPUT_HANDLE);
DisplayCP();
SaveCP();
SetUnicode();
DisplayCP();
// writef() do print Unicode string constant
// next 5 lines contains utf chars.
wchar[] utfstr = "←↑→↔↨∂∆∏∑−√∞∟∩∫≈═┼┴⌠⌐≥≤≡≠ѕєіїњљ\n";
writef("1: %s","←↑→↔↨∂∆∏∑−√∞∟∩∫≈═┼┴⌠⌐≥≤≡≠ѕєіїњљ\n");
writef("2: %s",utfstr);
// so do WriteConsoleW()
wchar[] utfstr3 = "3: ←↑→↔↨∂∆∏∑−√∞∟∩∫≈═┼┴⌠⌐≥≤≡≠ѕєіїњљ\n";
WriteConsoleW(cout, utfstr3.ptr, utfstr3.length, &nw);
// but not readln()`ed strings
writef("Type a line of Unicode characters:\n");
auto line = readln();
writef("line len: %s\n", line.length);
writef("%s\n", "Next line is what You typed:");
writef("%s\n",line);
writef("%s\n", "Your line again (with WriteConsoleA):");
WriteConsoleA(cout, line.ptr, line.length, &nw);
writef("%s\n", "Your line again (with WriteConsoleW):");
WriteConsoleW(cout, line.ptr, line.length, &nw);
writef("Done. Resetting code page.\n");
ResetCP();
DisplayCP();
return 0;
}
--------- eof ---------
More information about the Digitalmars-d-learn
mailing list