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); 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 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; end;
end. |