Recent

Author Topic: Console Application  (Read 3151 times)

jcmontherock

  • Full Member
  • ***
  • Posts: 126
Console Application
« on: December 29, 2021, 05:21:37 pm »
For a console application under W11/64 UTF-8 encoded with Laz 220RC2.
Is somebody knows how to display accented characters with Writeln or something else ?

Bart

  • Hero Member
  • *****
  • Posts: 4709
    • Bart en Mariska's Webstek
Re: Console Application
« Reply #1 on: December 29, 2021, 05:36:33 pm »
Code: Pascal  [Select][+][-]
  1.   SetTextCodePage(Output, CP_UTF8);
  2.   SetConsoleOutputCP(CP_UTF8);

Bart

jcmontherock

  • Full Member
  • ***
  • Posts: 126
Re: Console Application
« Reply #2 on: December 29, 2021, 06:16:53 pm »
Thanks, but how do you assign Text in SetTextCodePage ?

jwdietrich

  • Hero Member
  • *****
  • Posts: 1197
    • formatio reticularis
Re: Console Application
« Reply #3 on: December 29, 2021, 07:53:05 pm »
Thanks, but how do you assign Text in SetTextCodePage ?

Bart's code should be sufficient. SetTextCodePage sets the code page for a file that is passed by reference as the first parameter. Here, it is the virtual file "Output", i.e. the standard output to the terminal window. So, subsequent calls to write or writeln should use the specified code page.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.12 | FPC 3.2.0 | PPC, Intel, ARM | macOS, Windows, Linux

jcmontherock

  • Full Member
  • ***
  • Posts: 126
Re: Console Application
« Reply #4 on: December 30, 2021, 11:12:50 am »
Finally, I found what it happen: it's coming from the unit Crt.pp. I did not found the error, in that unit. I cannot do the test it with a non-utf-8 windows version. Could somebody do it, to check that's is the same ?

small test:

Code: Pascal  [Select][+][-]
  1. program tstConsole;
  2. {$mode objfpc}{$H+}
  3. {$APPTYPE CONSOLE}
  4. // {$CODEPAGE UTF-8}
  5. uses
  6.   Classes, SysUtils, LConvEncoding
  7.   //, Crt  // <===  uncomment this line, then it will not work
  8.   ;
  9. var
  10.   txt: String;
  11. begin
  12.   //SetConsoleOutputCP(CP_UTF8);
  13.   txt := 'éééééééàààààààà  ©';
  14.   Writeln(txt);
  15.   //txt := AnsiToUtf8('éà  ©');
  16.   //Writeln(txt);
  17. end.
  18.  
« Last Edit: December 30, 2021, 11:30:30 am by jcmontherock »

Fred vS

  • Hero Member
  • *****
  • Posts: 2570
    • StrumPract is the musicians best friend
Re: Console Application
« Reply #5 on: December 30, 2021, 02:49:35 pm »
Hello.

If you want to access keyboard on console application, you may have more luck using "keyboard" unit vs "crt" unit.

See here: https://forum.lazarus.freepascal.org/index.php/topic,57284.msg426272.html#msg426272

[EDIT] Of course, you should adapt (or remove) the const CHARS with your desire.

Fre;D
« Last Edit: December 30, 2021, 02:58:52 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs

jcmontherock

  • Full Member
  • ***
  • Posts: 126
Re: Console Application
« Reply #6 on: December 30, 2021, 04:26:38 pm »
Thanks a lot.  :D
It solves my problem with Crt.pp.

Bart

  • Hero Member
  • *****
  • Posts: 4709
    • Bart en Mariska's Webstek
Re: Console Application
« Reply #7 on: December 30, 2021, 05:33:47 pm »
Finally, I found what it happen: it's coming from the unit Crt.pp. I did not found the error, in that unit.

Crt unit has 2 procedures that influence what it does when it writes to console (by default it will reset the consoles codepage on each write to the default value): SetSafeCPSwitching() and  SetUseACP(), both procedures take a boolean as their parameter.
Just play around with these 2 (only 4 possible combinations, I'm too lazy to test).

Bart

 

TinyPortal © 2005-2018