[Lazarus] TStrings, Windows and Unicode

Marcos Douglas md at delfire.net
Wed Oct 24 13:40:59 CEST 2012


On Wed, Oct 24, 2012 at 7:39 AM, Marc Weustink <marc.weustink at cuperus.nl> wrote:
> Howard Page-Clark wrote:
>>
>> On 24/10/12 2:25, Marcos Douglas wrote:
>>>
>>> In the example below, running on Windows:
>>> The name of file, after saved, is "atenção.txt" but the content is
>>> "atenção".
>>> I understand the filename, i.e, I need to use UTF8ToSys but I do not
>>> understand the valid content.
>>>
>>> procedure TForm1.Button1Click(Sender: TObject);
>>> var
>>>    lStrings: TStrings;
>>> begin
>>>    lStrings := TStringList.Create;
>>>    lStrings.Text := 'atenção';
>>>    lStrings.SaveToFile('c:\atenção.txt');
>>>    lStrings.Free;
>>> end;
>>
>>
>> Internally the LCL uses only UTF8 encoding, so stringlists etc. all
>> expect strings in that encoding and process them correctly when supplied
>
>
> Nope.
> A stringlist is not LCL and it does not expect anything but strings.

I agree...

> If you write the contents of a stringlist to a file you get bytes you have
> put into the list. Nothing more nothing less.

Yes, but...

> If you want a UTF8 text file written, make sure you put UTF8 in it.
> Same for reading.
> When using filenames, you need to convert the name using the LCL function
> LCLtoSys (or something like that)

...in my example I didn't do that and worked, why?

For me, the same example should be written like that:

procedure TForm1.Button1Click(Sender: TObject);
var
  lStrings: TStrings;
begin
  lStrings := TStringList.Create;
  lStrings.Text := UTF8ToSys('atenção');
  lStrings.SaveToFile(UTF8ToSys('c:\atenção.txt'));
  lStrings.Free;
end;

But, as I said, I have not used the UTF8ToSys function in contents of
strings, so this be should broken, right?

BTW, I'm using FPC 2.6.1 and Lazarus trunk on Windows.

Marcos Douglas




More information about the Lazarus mailing list