laz_xmlread: improved EXMLError to hold position

git-svn-id: trunk@22169 -
This commit is contained in:
mattias 2009-10-14 10:29:10 +00:00
parent dd3e06155d
commit 01bda7a61b

View File

@ -25,11 +25,16 @@ interface
uses uses
{$IFDEF MEM_CHECK}MemCheck,{$ENDIF} {$IFDEF MEM_CHECK}MemCheck,{$ENDIF}
SysUtils, Classes, Laz_DOM, FileProcs; SysUtils, Classes, types, Laz_DOM, FileProcs;
type type
EXMLReadError = class(Exception); EXMLReadError = class(Exception)
public
Position: PtrInt;
LineCol: TPoint;
Descr: string;
end;
procedure ReadXMLFile(out ADoc: TXMLDocument; const AFilename: String); overload; procedure ReadXMLFile(out ADoc: TXMLDocument; const AFilename: String); overload;
@ -195,6 +200,7 @@ type
protected protected
buf, BufStart: PChar; buf, BufStart: PChar;
Filename: String; Filename: String;
function BufPosToLineCol(p: PChar): TPoint;
function BufPosToStr(p: PChar): string; function BufPosToStr(p: PChar): string;
procedure RaiseExc(const descr: String); procedure RaiseExc(const descr: String);
function SkipWhitespace: Boolean; function SkipWhitespace: Boolean;
@ -253,8 +259,7 @@ begin
inherited Create(ADocument); inherited Create(ADocument);
end; end;
function TXMLReader.BufPosToLineCol(p: PChar): TPoint;
function TXMLReader.BufPosToStr(p: PChar): string;
var var
apos: PChar; apos: PChar;
x: Integer; x: Integer;
@ -274,12 +279,31 @@ begin
Inc(x); Inc(x);
Inc(apos); Inc(apos);
end; end;
Result:=IntToStr(y)+','+IntToStr(x); Result.X:=X;
Result.Y:=Y;
end;
function TXMLReader.BufPosToStr(p: PChar): string;
var
LineCol: TPoint;
begin
// find out the line in which the error occured
LineCol:=BufPosToLineCol(BufStart);
Result:=IntToStr(LineCol.y)+','+IntToStr(LineCol.x);
end; end;
procedure TXMLReader.RaiseExc(const descr: String); procedure TXMLReader.RaiseExc(const descr: String);
var
Err: EXMLReadError;
LineCol: TPoint;
begin begin
raise EXMLReadError.Create(Filename+'('+BufPosToStr(buf)+') Error: ' + descr); LineCol:=BufPosToLineCol(buf);
Err:=EXMLReadError.Create(
Filename+'('+IntToStr(LineCol.y)+','+IntToStr(LineCol.x)+') Error: ' + descr);
Err.Position:=buf-BufStart;
Err.LineCol:=LineCol;
Err.Descr:=descr;
raise Err;
end; end;
function TXMLReader.SkipWhitespace: Boolean; function TXMLReader.SkipWhitespace: Boolean;