Autor Beitrag
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 26.08.03 14:40 
Hi,

ich schreibe seit heute Mittag an einem kleinen XML-Parser, der XML-Dateien in ihre Bestandteile zerlegen soll. Warum ich das mache? Ich will aus dem Parser eine Standalone-EXE in Turbo-Pascal machen (Dateigröße).
Schon nach 200 Zeilen ein Problem: "Ungültige Zeigeroperation". Im Quelltext ist die Stelle markiert, an der der Fehler auftritt. Meine Frage: warum meint der Debugger, dass auf Input[0] nicht zugegriffen werden kann? Das Auslesen der Datei sowie das Analysieren der anderen Zeilen auf Leerzeilen funktioniert perfekt. Wo liegt der Hund begraben?

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
unit dsxml;

interface

uses Classes;

type
  TAttribute = record
    Name, Value: String;
    end;
    
  TAttributeList = Array of TAttribute;

procedure Initialize;
procedure Finalize;
function GetLastDSError: String;
procedure LoadFile(Filename: String);
//Internal Parsing procedures
procedure DeleteEmptyLines;
procedure DeleteComments;
procedure DeleteSpaces;
procedure ParseFirstLine;
function ReadAttributes(s: String; Tagname: String = '?xml'): TAttributeList;

var
  Input: TStringList = nil;
  LastError: String;
  XMLVersion, Encoding: String;
  Standalone: Boolean;

const
  ENotInit = 'Not initialized';
  ENoCommentEnd = 'End of comment not found';
  EFileError = 'Error opening file; IOResult';
  ENoFirstLine = 'First line missing';
  EInvalidLineStart = 'Line doesn''t start with <';
  EInvalidTagname = 'Invalid Tagname';
  EInvalidLineEnd = 'Line doesn''t end with >';
  EInvalidAttribute = 'Invalid attribute';

implementation

procedure Initialize;
begin
  Input := TStringList.Create;
  Input.Capacity := $7FFFFFFF;
end;

procedure Finalize;
begin
  if Input = nil then begin
    LastError := ENotInit;
    exit;
    end;
  Input.Destroy;
end;

function GetLastDSError: String;
begin
  Result := LastError;
end;

procedure DeleteEmptyLines;
var
  i: Integer;
begin
  if Input = nil then begin
    LastError := ENotInit;
    exit;
    end;
  for i := Input.Count - 1 downto 0 do begin
    if Input[i] = '' then //hier tritt der Fehler auf (wenn i 0 ist)
      Input.Delete(i);
    end;
end;

procedure DeleteComments;
var
  dummy: String;
begin
  if Input = nil then begin
    LastError := ENotInit;
    exit;
    end;
  dummy := Input.Text;
  repeat
    if Pos('<!--', dummy) = 0 then
      break
    else begin
      if Pos('-->', dummy) = 0 then begin
        LastError := ENoCommentEnd;
        exit;
      end else
        Delete(dummy, Pos('<!--', dummy), Pos('-->', dummy) - Pos('<!--', dummy) + 3);
      end;
  until
    false;
  Input.Text := dummy;
end;

procedure DeleteSpaces;
var
  i: Integer;
  dummy: String;
  breaked: Boolean;
begin
  if Input = nil then begin
    LastError := ENotInit;
    exit;
    end;
  for i := Input.Count - 1 downto 0 do begin
    breaked := false;
    dummy := Input[i];
    repeat
      if dummy = '' then begin
        Input.Delete(i);
        breaked := true;
        break;
        end;
      if (dummy[1] = '#9') or (dummy[1] = ' ') then
        Delete(dummy, 1, 1)
      else
        break;
    until
      false;
    if not(breaked) then
      Input[i] := dummy;
    end;
end;

procedure LoadFile(Filename: String);
var
  f: TextFile;
  dummy, ior: String;
begin
  Input.Clear;
  {$I-}
  AssignFile(f, Filename);
  Reset(f);
  while not(eof(f)) do begin
    ReadLn(f, dummy);
    Input.Add(dummy);
    end;
  CloseFile(f);
  {$I+}
  Str(IOResult, ior);
  if ior <> '0' then begin
    LastError := EFileError + ' ' + ior;
    exit;
    end;
  DeleteEmptyLines;
  DeleteComments;
  DeleteSpaces;
end;

function ReadAttributes(s: String; Tagname: String = '?xml'): TAttributeList;
var
  dummy: String;
  i: Integer;
begin
  dummy := s;
  SetLength(Result, 1);
  i := 1;
  if dummy[1] <> '<' then begin
    LastError := EInvalidLineStart;
    exit; 
    end;
  if Copy(dummy, 1, Length(Tagname)) <> Tagname then begin
    LastError := EInvalidTagname;
    exit;
    end;
  if dummy[Length(dummy)] <> '>' then begin
    LastError := EInvalidLineEnd;
    exit;
    end;
  if (dummy[Length(dummy) - 1] <> '?') and (Tagname = '?xml') then begin
    LastError := EInvalidLineEnd;
    exit;
    end;
  repeat
    if dummy = '' then
      break;
    try
      while dummy[1] = ' ' do
        Delete(dummy, 1, 1);
    except
      break;
      end;
    if dummy = '' then
      break;
    if Pos('=', dummy) = 0 then begin
      LastError := EInvalidAttribute;
      exit;
      end;
    Inc(i);
    SetLength(Result, i);
    Result[i - 1].Name := Copy(dummy, 1, Pos('=', dummy));
    Delete(dummy, 1, Pos('=', dummy) + 1);
    if dummy[1] <> '"' then begin
      LastError := EInvalidAttribute;
      exit;
      end;
    if Pos('"', dummy) = 0 then begin
      LastError := EInvalidAttribute;
      exit;
      end;
    Delete(dummy, 1, 1);
    Result[i - 1].Value := Copy(dummy, 1, Pos('"', dummy) - 1);
    Delete(dummy, 1, Pos('"', dummy));
  until
    false;
end;

procedure ParseFirstLine;
var
  dummy: TAttributeList;
begin
  if Input = nil then begin
    LastError := ENotInit;
    exit;
    end;
  if Input[1] = '' then begin
    LastError := ENoFirstLine;
    exit;
    end;
  LastError := '';
  dummy := ReadAttributes(Input[1]);
  if LastError <> '' then
    exit;
  //da kommt noch was ;)
end;

end.


THX AXMD

PS.: Wer andere Fehler findet, sei aufgefordert, diese zu melden ;)
barfuesser
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 324



BeitragVerfasst: Di 26.08.03 15:44 
Hallo,

kann es sein, daß beim Eintritt in die Schleife Input.Count = 0 ist?

baarfuessser
FloSch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 129



BeitragVerfasst: Di 26.08.03 15:47 
Hallo,

ausblenden Delphi-Quelltext
1:
2:
3:
4:
  for i := Input.Count - 1 downto 0 do begin
    if Input[i] = '' then //hier tritt der Fehler auf (wenn i 0 ist)
      Input.Delete(i);
    end;

probiere doch mal anstatt Input[i] Input.Strings[i] zu benutzen.

Gruß
Florian

_________________
Windows 200 Prof. - Debian Sarge
D5 Prof & D6 PE - Kylix 3 Professional - Free Pascal
ErnestoChe
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 528

Win 2000 pro, CRUX 2.0
Delphi 6 Pers, Open K3
BeitragVerfasst: Di 26.08.03 15:47 
Hi,

probiers mal mit:

ausblenden Delphi-Quelltext
1:
input.strings[i]					


MFG

- Ernesto -
FloSch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 129



BeitragVerfasst: Di 26.08.03 15:50 
Hallo,

leider liegt es daran nicht.

OH hat folgendes geschrieben:

Strings is the default property of string list objects. The Strings identifier can be omitted when accessing the Strings property of a string list object. For example, the following two lines of code are both acceptable and do the same thing:

MyStringList.Strings[0] := 'This is the first string';
MyStringList[0] := 'This is the first string';


Gruß,
Florian

_________________
Windows 200 Prof. - Debian Sarge
D5 Prof & D6 PE - Kylix 3 Professional - Free Pascal
barfuesser
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 324



BeitragVerfasst: Di 26.08.03 15:52 
Noch einmal meine Frage: kann es sein, daß beim Eintritt in die Schleife Input.Count = 0 ist?

barfuesser
AXMD Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 26.08.03 15:54 
Geht nicht :(

AXMD

PS.: Wer's selbst testen will: Unit einbinden und beliebige XML-Datei so öffnen:

ausblenden Delphi-Quelltext
1:
2:
Initialize;
LoadFile('C:\bla.xml');


//EDIT: @barfuesser: kann u.U. sein, war in bei meiner Testdatei aber nicht so
FloSch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 129



BeitragVerfasst: Di 26.08.03 15:59 
Hallo,

bei mir gibt es leider hier schon eine Exception:

ausblenden Quelltext
1:
2:
3:
4:
  while not(eof(f)) do begin
    ReadLn(f, dummy);
    Input.Add(dummy);
    end;


Warum benutzt du kein TFileStream?

Gruß,
Florian

_________________
Windows 200 Prof. - Debian Sarge
D5 Prof & D6 PE - Kylix 3 Professional - Free Pascal
AXMD Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 26.08.03 16:06 
Im Ernst??? :shock: bei mir nicht. Sicher, dass die von dir angegebene Datei in LoadFile (bla.xml) gültige XML-Daten enthält?

AXMD

PS.: Meine Datei sieht testweise so aus:

ausblenden Quelltext
1:
2:
3:
4:
5:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<Bla>
  <Blub Bla="Blub"/>
</Bla>
FloSch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 129



BeitragVerfasst: Di 26.08.03 16:10 
Hallo,

natürlich bin ich mir sicher das das eine XML Datei ist :roll:

Gruß,
Florian

_________________
Windows 200 Prof. - Debian Sarge
D5 Prof & D6 PE - Kylix 3 Professional - Free Pascal
barfuesser
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 324



BeitragVerfasst: Mi 27.08.03 11:41 
Hallo,

der Fehler liegt an:
ausblenden Quelltext
1:
Input.Capacity := $7FFFFFFF;					

die Zeile raus und das ganze funktioniert. Mir ist allerdings nicht ganz klar, was da intern passiert. Wenn Capacity gesetzt wird, sind die ersten Zeilen von TStrings nicht definiert.

Warum lädst Du eigentlich die Datei so umständlich in die TStringList. Benutze einfach:
ausblenden Quelltext
1:
Input.LoadFromFile(FileName)					

barfuesser