source: trunk/Packages/Common/UXMLUtils.pas

Last change on this file was 2, checked in by chronos, 9 years ago
  • Added: TPakFile class for extraction PAK files from original game.
File size: 5.6 KB
Line 
1unit UXMLUtils;
2
3{$mode delphi}
4
5interface
6
7uses
8 {$IFDEF WINDOWS}Windows,{$ENDIF}
9 Classes, SysUtils, DateUtils, XMLRead, XMLWrite, DOM;
10
11function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
12function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString;
13procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
14procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
15procedure WriteString(Node: TDOMNode; Name: string; Value: string);
16function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
17function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
18function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
19
20
21implementation
22
23function GetTimeZoneBias: Integer;
24{$IFDEF WINDOWS}
25var
26 TimeZoneInfo: TTimeZoneInformation;
27begin
28 case GetTimeZoneInformation(TimeZoneInfo) of
29 TIME_ZONE_ID_STANDARD: Result := TimeZoneInfo.Bias + TimeZoneInfo.StandardBias;
30 TIME_ZONE_ID_DAYLIGHT: Result := TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias;
31 else
32 Result := 0;
33 end;
34end;
35{$ELSE}
36begin
37 Result := 0;
38end;
39{$ENDIF}
40
41function LeftCutString(var Source: string; out Output: string; Delimiter: string; Allowed: string = ''): Boolean;
42var
43 I, J: Integer;
44 Matched: Boolean;
45begin
46 I := 1;
47 Matched := True;
48 while (I < Length(Source)) and Matched do begin
49 Matched := True;
50 if (Source[I] = Delimiter) then Matched := False;
51 //for J := 1 to Length(Allowed) do
52 // if Source[I] = Allowed[J] then Matched := True;
53 if Matched then Inc(I);
54 end;
55 if (Delimiter = Copy(Source, I, Length(Delimiter))) or (I = Length(Source)) then begin
56 Output := Copy(Source, 1, I - 1);
57 Delete(Source, 1, Length(Output) + Length(Delimiter));
58 Result := True;
59 end else begin
60 Output := '';
61 Result := False;
62 end;
63end;
64
65function XMLTimeToDateTime(XMLDateTime: string): TDateTime;
66var
67 Part: string;
68 Year: Integer;
69 Month: Integer;
70 Day: Integer;
71 Hour: Integer;
72 Minute: Integer;
73 Second: Integer;
74 Millisecond: Integer;
75begin
76 if LeftCutString(XMLDateTime, Part, '-') then
77 Year := StrToInt(Part);
78 if LeftCutString(XMLDateTime, Part, '-') then
79 Month := StrToInt(Part);
80 if Pos('T', XMLDateTime) > 0 then begin
81 if LeftCutString(XMLDateTime, Part, 'T') then
82 Day := StrToInt(Part);
83 if LeftCutString(XMLDateTime, Part, ':') then
84 Hour := StrToInt(Part);
85 if LeftCutString(XMLDateTime, Part, ':') then
86 Minute := StrToInt(Part);
87 if Pos('.', XMLDateTime) > 0 then begin
88 if LeftCutString(XMLDateTime, Part, '.') then
89 Second := StrToInt(Part);
90 if Pos('+', XMLDateTime) > 0 then
91 LeftCutString(XMLDateTime, Part, '+') else
92 if Pos('-', XMLDateTime) > 0 then
93 LeftCutString(XMLDateTime, Part, '-') else
94 if Pos('Z', XMLDateTime) > 0 then
95 LeftCutString(XMLDateTime, Part, 'Z');
96 Millisecond := StrToInt(Part);
97 end else begin
98 if Pos('+', XMLDateTime) > 0 then
99 LeftCutString(XMLDateTime, Part, '+') else
100 if Pos('-', XMLDateTime) > 0 then
101 LeftCutString(XMLDateTime, Part, '-') else
102 if Pos('Z', XMLDateTime) > 0 then
103 LeftCutString(XMLDateTime, Part, 'Z');
104 Second := StrToInt(Part);
105 Millisecond := 0;
106 end;
107 end else begin
108 Day := StrToInt(XMLDateTime);
109 end;
110 Result := EncodeDateTime(Year, Month, Day, Hour, Minute, Second, Millisecond);
111 // TODO: Correct time by zone bias
112end;
113
114function DateTimeToXMLTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString;
115const
116 Neg: array[Boolean] of string = ('+', '-');
117var
118 Bias: Integer;
119begin
120 Result := FormatDateTime('yyyy''-''mm''-''dd''T''hh'':''nn'':''ss''.''zzz', Value); { Do not localize }
121 Bias := GetTimeZoneBias;
122 if (Bias <> 0) and ApplyLocalBias then
123 begin
124 Result := Format('%s%s%.2d:%.2d', [Result, Neg[Bias > 0], { Do not localize }
125 Abs(Bias) div MinsPerHour,
126 Abs(Bias) mod MinsPerHour]);
127 end else
128 Result := Result + 'Z'; { Do not localize }
129end;
130
131procedure WriteInteger(Node: TDOMNode; Name: string; Value: Integer);
132var
133 NewNode: TDOMNode;
134begin
135 NewNode := Node.OwnerDocument.CreateElement(Name);
136 NewNode.TextContent := IntToStr(Value);
137 Node.AppendChild(NewNode);
138end;
139
140procedure WriteBoolean(Node: TDOMNode; Name: string; Value: Boolean);
141var
142 NewNode: TDOMNode;
143begin
144 NewNode := Node.OwnerDocument.CreateElement(Name);
145 NewNode.TextContent := BoolToStr(Value);
146 Node.AppendChild(NewNode);
147end;
148
149procedure WriteString(Node: TDOMNode; Name: string; Value: string);
150var
151 NewNode: TDOMNode;
152begin
153 NewNode := Node.OwnerDocument.CreateElement(Name);
154 NewNode.TextContent := Value;
155 Node.AppendChild(NewNode);
156end;
157
158function ReadInteger(Node: TDOMNode; Name: string; DefaultValue: Integer): Integer;
159var
160 NewNode: TDOMNode;
161begin
162 Result := DefaultValue;
163 NewNode := Node.FindNode(Name);
164 if Assigned(NewNode) then
165 Result := StrToInt(NewNode.TextContent);
166end;
167
168function ReadBoolean(Node: TDOMNode; Name: string; DefaultValue: Boolean): Boolean;
169var
170 NewNode: TDOMNode;
171begin
172 Result := DefaultValue;
173 NewNode := Node.FindNode(Name);
174 if Assigned(NewNode) then
175 Result := StrToBool(NewNode.TextContent);
176end;
177
178function ReadString(Node: TDOMNode; Name: string; DefaultValue: string): string;
179var
180 NewNode: TDOMNode;
181begin
182 Result := DefaultValue;
183 NewNode := Node.FindNode(Name);
184 if Assigned(NewNode) then
185 Result := NewNode.TextContent;
186end;
187
188end.
189
Note: See TracBrowser for help on using the repository browser.