[Example] Loading UTF-8 files

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 17521
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[Example] Loading UTF-8 files

Post by Sergey Tkachenko »

Very simple example - how to load Unicode UTF-8 file:

Code: Select all

procedure LoadUTF8(rv: TCustomRichView; const FileName: String; 
  StyleNo, ParaNo: Integer); 
var Stream: TFileStream; 
    s: TRVRawByteString; 
    ws: TRVUnicodeString; 
begin 
  Stream := TFileStream.Create(FileName, fmOpenRead); 
  SetLength(s, Stream.Size); 
  Stream.ReadBuffer(PRVAnsiChar(s)^, Stream.Size); 
  Stream.Free; 
  rv.Clear; 
  ws := UTF8Decode(s); 
  rv.AddTextNLW(ws, StyleNo, ParaNo, ParaNo, False); 
end;
Call:

Code: Select all

LoadUTF8(RichViewEdit1, 'test.txt', 0, 0);
RichViewEdit1.Format;
(If the StyleNo-th style has Unicode property = True, the file will be
loaded as it is. If not, it will be converted to ANSI text)

(2008-Dec-9: updated for compatibility with TRichView 11 and Delphi 2009)
Post Reply