forked from andriy-gerasika/dfm2xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfm2xmlImpl.pas
executable file
·305 lines (281 loc) · 7.56 KB
/
dfm2xmlImpl.pas
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
unit dfm2xmlImpl;
interface
uses
System.Classes;
procedure ObjectBinaryToXml(Input, Output: TStream); overload;
procedure ObjectResourceToXml(Input, Output: TStream); overload;
implementation
uses
System.RTLConsts,
System.TypInfo,
System.SysUtils,
System.StrUtils;
{ Binary to xml conversion }
procedure ObjectBinaryToXml(Input, Output: TStream);
var
NestingLevel: Integer;
Reader: TReader;
Writer: TWriter;
ObjectName, PropName: string;
UTF8Idents: Boolean;
MemoryStream: TMemoryStream;
LFormatSettings: TFormatSettings;
procedure WriteIndent;
const
Blanks: array[0..0] of AnsiChar = (#9); //'<tab>';
var
I: Integer;
begin
for I := 1 to NestingLevel do Writer.Write(Blanks, SizeOf(Blanks));
end;
procedure WriteStr(const S: RawByteString); overload;
begin
Writer.Write(S[1], Length(S));
end;
procedure WriteStr(const S: UnicodeString); overload; inline;
begin
WriteStr(AnsiString(S));
end;
procedure WriteUTF8Str(const S: string);
var
Ident: UTF8String;
begin
Ident := UTF8Encode(S);
if not UTF8Idents and (Length(Ident) > Length(S)) then
UTF8Idents := True;
WriteStr(Ident);
end;
procedure NewLine;
begin
WriteStr(sLineBreak);
WriteIndent;
end;
procedure ConvertValue; forward;
procedure ConvertHeader;
var
ClassName: string;
Flags: TFilerFlags;
Position: Integer;
begin
Reader.ReadPrefix(Flags, Position);
ClassName := Reader.ReadStr;
ObjectName := Reader.ReadStr;
WriteIndent;
if ffInherited in Flags then
WriteStr('<object instance="inherited"')
else if ffInline in Flags then
WriteStr('<object instance="inline"')
else
WriteStr('<object');
if ObjectName <> '' then
begin
WriteStr(' name="');
WriteUTF8Str(ObjectName);
WriteStr('"');
end;
WriteStr(' class="');
WriteUTF8Str(ClassName);
WriteStr('"');
if ffChildPos in Flags then
begin
WriteStr(' index="');
WriteStr(IntToStr(Position));
WriteStr('"');
end;
if ObjectName = '' then
ObjectName := ClassName; // save for error reporting
WriteStr('>');
WriteStr(sLineBreak);
end;
procedure ConvertBinary;
const
BytesPerLine = 32;
var
MultiLine: Boolean;
I: Integer;
Count: Longint;
Buffer: array[0..BytesPerLine - 1] of AnsiChar;
Text: array[0..BytesPerLine * 2 - 1] of AnsiChar;
begin
Reader.ReadValue;
WriteStr('{');
Inc(NestingLevel);
Reader.Read(Count, SizeOf(Count));
MultiLine := Count >= BytesPerLine;
while Count > 0 do
begin
if MultiLine then NewLine;
if Count >= 32 then I := 32 else I := Count;
Reader.Read(Buffer, I);
BinToHex(Buffer, Text, I);
Writer.Write(Text, I * 2);
Dec(Count, I);
end;
Dec(NestingLevel);
WriteStr('}');
end;
procedure ConvertProperty; forward;
procedure ConvertValue;
const
LineLength = 64;
var
I, J, K, L: Integer;
S: AnsiString;
W: UnicodeString;
LineBreak: Boolean;
begin
case Reader.NextValue of
vaList:
begin
Reader.ReadValue;
WriteStr('(');
Inc(NestingLevel);
while not Reader.EndOfList do
begin
NewLine;
ConvertValue;
end;
Reader.ReadListEnd;
Dec(NestingLevel);
WriteStr(')');
end;
vaInt8, vaInt16, vaInt32:
WriteStr(IntToStr(Reader.ReadInteger));
vaExtended, vaDouble:
WriteStr(FloatToStrF(Reader.ReadFloat, ffFixed, 16, 18, LFormatSettings));
vaSingle:
WriteStr(FloatToStr(Reader.ReadSingle, LFormatSettings) + 's');
vaCurrency:
WriteStr(FloatToStr(Reader.ReadCurrency * 10000, LFormatSettings) + 'c');
vaDate:
WriteStr(FloatToStr(Reader.ReadDate, LFormatSettings) + 'd');
vaWString, vaUTF8String:
begin
W := Reader.ReadWideString;
W := ReplaceStr(W, '&', '&');
W := ReplaceStr(W, '"', '"');
W := ReplaceStr(W, '<', '<');
W := ReplaceStr(W, '>', '>');
WriteUTF8Str(W);
end;
vaString, vaLString:
begin
S := AnsiString(Reader.ReadString);
S := AnsiReplaceStr(S, '&', '&');
S := AnsiReplaceStr(S, '"', '"');
S := AnsiReplaceStr(S, '<', '<');
S := AnsiReplaceStr(S, '>', '>');
WriteStr(S);
end;
vaIdent, vaFalse, vaTrue, vaNil, vaNull:
WriteUTF8Str(Reader.ReadIdent);
vaBinary:
ConvertBinary;
vaSet:
begin
Reader.ReadValue;
WriteStr('[');
I := 0;
while True do
begin
S := AnsiString(Reader.ReadStr);
if S = '' then Break;
if I > 0 then WriteStr(', ');
WriteStr(S);
Inc(I);
end;
WriteStr(']');
end;
vaCollection:
begin
Reader.ReadValue;
///WriteStr('<');
Inc(NestingLevel);
while not Reader.EndOfList do
begin
NewLine;
WriteStr('item');
if Reader.NextValue in [vaInt8, vaInt16, vaInt32] then
begin
WriteStr(' [');
ConvertValue;
WriteStr(']');
end;
WriteStr(sLineBreak);
Reader.CheckValue(vaList);
Inc(NestingLevel);
while not Reader.EndOfList do
ConvertProperty;
Reader.ReadListEnd;
Dec(NestingLevel);
WriteIndent;
WriteStr('end');
end;
Reader.ReadListEnd;
Dec(NestingLevel);
///WriteStr('>');
end;
vaInt64:
WriteStr(IntToStr(Reader.ReadInt64));
else
raise EReadError.CreateResFmt(@sPropertyException,
[ObjectName, DotSep, PropName, IntToStr(Ord(Reader.NextValue))]);
end;
end;
procedure ConvertProperty;
begin
WriteIndent;
PropName := Reader.ReadStr; // save for error reporting
WriteStr('<property name="');
WriteUTF8Str(PropName);
WriteStr('" value="');
ConvertValue;
WriteStr('"/>');
WriteStr(sLineBreak);
end;
procedure ConvertObject;
begin
ConvertHeader;
Inc(NestingLevel);
while not Reader.EndOfList do
ConvertProperty;
Reader.ReadListEnd;
while not Reader.EndOfList do
ConvertObject;
Reader.ReadListEnd;
Dec(NestingLevel);
WriteIndent;
WriteStr('</object>' + sLineBreak);
end;
begin
NestingLevel := 0;
UTF8Idents := False;
Reader := TReader.Create(Input, 4096);
LFormatSettings := TFormatSettings.Create('en-US'); // do not localize
LFormatSettings.DecimalSeparator := AnsiChar('.');
try
MemoryStream := TMemoryStream.Create;
try
Writer := TWriter.Create(MemoryStream, 4096);
try
Reader.ReadSignature;
ConvertObject;
finally
Writer.Free;
end;
if UTF8Idents then
Output.Write(TEncoding.UTF8.GetPreamble[0], 3);
Output.Write(MemoryStream.Memory^, MemoryStream.Size);
finally
MemoryStream.Free;
end;
finally
Reader.Free;
end;
end;
procedure ObjectResourceToXml(Input, Output: TStream);
begin
Input.ReadResHeader;
ObjectBinaryToXml(Input, Output);
end;
end.