delphi中.dfm文件类型识别与转换 ( 积分: 100 )

  • 主题发起人 主题发起人 动力汽车
  • 开始时间 开始时间

动力汽车

Unregistered / Unconfirmed
GUEST, unregistred user!
[blue]delphi5中,有些.dfm用文本编辑器打开为纯文本格式,而有些则不是,我知道在delphi6中提供了一个转换程序convert.exe,可以把非纯文本格式的转换为纯文本格式。我现在的需求是遍历一个目录,找到这些不是纯文本格式的.dfm文件,然后把他们在列表中列出来。有哪位高手知道convert.exe的运行原理或者代码的?或者实现类似识别与转换代码的也可以,请多多帮忙。[/blue]
 
Procedure ObjBinToTxt(Input, Output: tstream);
Begin
Input.Position := 0;
Input.ReadResHeader;
ObjectBinaryToText(Input, Output);
End;

Procedure ObjTxtToBin(Input, Output: tstream);
Var
Len: Byte;
Tmp: LongInt;
MemSize: LongInt;
Header: Array[0..79] Of char;
MS: TMemoryStream;
Begin
MS := TMemoryStream.Create;
Input.Position := 0;
objecttexttobinary(Input, MS);
MemSize := MS.Size;
FillChar(Header, SizeOf(Header), 0);
MS.Position := SizeOf(LongInt);
MS.Read(Len, 1);
If Len And $F0 = $F0 Then
Begin
If ffChildPos In TFilerFlags(Len And $F0) Then
Begin
MS.Read(Len, 1);
Case TValueType(Len) Of
vaInt8: Len := 1;
vaInt16: Len := 2;
vaInt32: Len := 4;
End;
MS.Read(Tmp, Len);
End;
MS.Read(Len, 1);
End;
MS.Read(Header[3], Len);
StrUpper(@Header[3]);
Byte((@Header[0])^) := $FF;
word((@Header[1])^) := 10;
word((@Header[Len + 4])^) := $1030;
LongInt((@Header[Len + 6])^) := MemSize;
Output.Write(Header, Len + 10);
Output.Write(MS.memory^, MemSize);
MS.Free;
End;

Function DfmToText(Const DfmFile, TxtFile: String): Boolean;
Var
FS: tstream;
MS: tstream;
FileHead: SmallInt;
Begin
Result := False;
If Not FileExists(DfmFile) Then Exit;
If Length(TxtFile) < 4 then Exit;
FS := TFileStream.Create(DfmFile, fmOpenRead);
MS := TMemoryStream.Create;
FS.Position := 0;
FS.Read(FileHead, 2);
If FileHead = $0AFF Then
ObjBinToTxt(FS, MS)
Else
(MS As TMemoryStream).LoadFromStream(FS);
MS.Position := 0;
(MS As TMemoryStream).SaveToFile(TxtFile);
Result := True;
MS.Free;
FS.Free;
End;

Function TxtToDfm(Const TxtFile, DfmFile: String): Boolean;
Var
FS: tstream;
MS: tstream;
FileHead: SmallInt;
Begin
Result := False;
If Not FileExists(TxtFile) Then Exit;
If Length(TxtFile) < 4 then Exit;
FS := TFileStream.Create(TxtFile, fmOpenRead);
MS := TMemoryStream.Create;
FS.Position := 0;
FS.Read(FileHead, 2);
If FileHead <> $0AFF Then
ObjTxtToBin(FS, MS)
Else
(MS As TMemoryStream).LoadFromStream(FS);
MS.Position := 0;
(MS As TMemoryStream).SaveToFile(DfmFile);
Result := True;
MS.Free;
FS.Free;
End;
 
后退
顶部