[fpc-devel] TStringList.LoadFromFile and SavetoFile - file encoding support
Michael Van Canneyt
michael at freepascal.org
Tue Feb 3 13:14:30 CET 2009
On Tue, 3 Feb 2009, Sergei Gorelkin wrote:
> Graeme Geldenhuys wrote:
>
> > The point is that you will probably have a File Open dialog that gives
> > the filename to TMemo.LoadFromFile. The the file dialog could collect
> > the filename and optional encoding to pass on to LoadFromFile.
> >
> > Even if TStringList has the optional encoding parameter, prior source
> > code should still work as-is without the encoding parameter. No code
> > would be broken.
> >
> > I agree with Marco that auto detecting encodings is probably not a
> > good idea in the RTL, but at least enable the option of a encoding
> > parameter in TStringList, which could help things along. As in the
> > case of the bug report.
> >
>
> There is no need for TStrings.LoadFromFile method at all.
> The container is one matter, and its serialization is a separate one. Just
> introduce a decoding stream and you can write e.g:
>
> Strings.LoadFromStream(TDecodingStream.Create(TFileStream.Create('myfile'),
> 'cp866', 'utf-8'));
>
> This approach isn't limited to decoding, you can do decrypting, compressing,
> etc.
> In reality, of course, you have to finalize all the stuff - that means typing
> more than one line. C++ language that automatically finalizes on-stack objects
> is more friendly in this respect.
That's easily done:
- make TDecodingStream descendent of TOwnerStream (exists) and TFileStream will
be freed.
- Add a second parameter to LoadFromStream(AStream : TStream; FreeStream = False)
and your call becomes
Strings.LoadFromStream(TDecodingStream.Create(TFileStream.Create('myfile'),'cp866', 'utf-8'),True);
Michael.
More information about the fpc-devel
mailing list