1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
unit consoleoutput;
(**************************************************************)
(* *)
(* (C) Copyright by Lars B. Dybdahl *)
(* E-mail: Lars@dybdahl.dk, phone +45 70201241 *)
(* You may distribute and modify this file as you wish *)
(* for free *)
(* *)
(* See http://dybdahl.dk/dxgettext/ for more information *)
(* *)
(**************************************************************)
interface
// This writes to standard console output.
// On Linux, and if no language is set, system.write() will be used.
// Otherwise, the Windows API will be used, with Unicode if possible
procedure Write (ws:widestring);
procedure Writeln (ws:widestring='');
implementation
{$ifdef MSWINDOWS}
uses
windows, sysutils,
gnugettext;
{$endif}
{$ifdef MSWINDOWS}
function ConWriteW(con:THandle;outstr:WideString):Boolean;
var
len,written:Cardinal;
begin
len:=length(outstr);
if len>0 then
WriteConsoleW(con, PWideChar(@outstr[1]), len, written, nil);
result:=written=len;
end;
function ConWriteA(con:THandle;outstr:Ansistring):Boolean;
var
len,written:Cardinal;
begin
len:=length(outstr);
if len>0 then
WriteConsole(con, PChar(@outstr[1]), len, written, nil);
result:=written=len;
end;
procedure Write (ws:widestring);
var
output: THandle;
lang:string;
success:boolean;
begin
lang:=lowercase(GetCurrentLanguage);
if (lang='') or (lang='c') or (lang='en') then begin
system.write (ws);
end else begin
success:=false;
// First, try Unicode output to screen
output := CreateFileW('CONOUT$', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if output <> INVALID_HANDLE_VALUE then
try
success:=ConWriteW(output, ws);
finally
CloseHandle(output);
end;
if not success then begin
output := CreateFile('CONOUT$', GENERIC_READ or GENERIC_WRITE, FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
if output <> INVALID_HANDLE_VALUE then
try
success:=ConWriteA(output, ws);
finally
CloseHandle(output);
end;
end;
if not success then begin
// Output failed or not implemented - using writeln() instead
system.write (ws);
end;
end;
end;
{$endif}
{$ifdef LINUX}
procedure Write (ws: widestring);
begin
system.write (ws);
end;
{$endif}
procedure Writeln (ws:widestring='');
begin
Write (ws+sLinebreak);
end;
end.
|