type
UnicodeTextRead=Class
private
UnicodeText:TFileStream;
FLineStart: Int64;
FEof: Boolean;
procedure SetLineStart(const Value: Int64);
protected
property LineStart:Int64 Read FLineStart Write SetLineStart;
public
property fileEof:Boolean Read FEof default false;
function fetchline:WideString; //按行读出Unicode文本
constructor Create(fn:TFileName);
destructor Destroy;override;
end;
implementation
constructor UnicodeTextRead.Create(fn: TFileName);
begin
UnicodeText:=TFileStream.Create(fn,fmOpenRead);
LineStart:=2; //Unicode 头两个字节为标识符
end;
destructor UnicodeTextRead.Destroy;
begin
UnicodeText.Free;
inherited;
end;
function UnicodeTextRead.fetchline: WideString;
var
buf:array [0..1] of WideChar;
len:Integer;
Line:array of WideChar;
i:Integer;
begin
Result:='';
while UnicodeText.Read(buf,4)=4 do
begin
if UnicodeText.Position>=UnicodeText.Size then
begin
FEof:=True;
end;
if (buf[0]=WideChar($D))And(buf[1]=WideChar($A)) then
begin
len:=(UnicodeText.Position-4-LineStart) div 2;
UnicodeText.Position:=LineStart;
SetLength(Line,len);
UnicodeText.Read(Pointer(Line)^,len*2);
for i:=low(Line) to High(Line) do
begin
Result:=Result+Line[i];
end;
SetLength(Line,0);
LineStart:=LineStart+len*2+4;
exit;
end
else begin
UnicodeText.Position:=UnicodeText.Position-2;
end;
end;
end;
procedure UnicodeTextRead.SetLineStart(const Value: Int64);
begin
if Value<=UnicodeText.Size then
begin
FLineStart := Value;
UnicodeText.Position:=Value;
end;
end;