A conversion from DFM (binary Delphi Form file) to a text file is done easily using the function ObjectResourceToText. For the opposite direction, use ObjectTextToResource.
The following code sample packs the whole task in a handy function.
Usage:
// convert a DFM file to TXT
ConvertFormOrText('e:\temp\unit11.dfm', ConvertToText);
// vice versa, extension is not necessary
ConvertFormOrText('e:\temp\unit11', ConvertToDFM);
type
TDFMorTXT = (ConvertToForm, ConvertToText);
{ Given a file name this routine will convert the file from either
1. A text file to a DFM file or
2. A DFM file to a text file
The output file name is built from the input file name }
function ConvertFormOrText(FileToConvertFrom : string;
ConversionType : TDFMorTXT) : boolean;
var
InputStream, OutputStream : TFileStream;
FileToConvertTo : string;
begin
Result := True;
FileToConvertTo := FileToConvertFrom;
{ change file extensions as appropriate }
case ConversionType of
ConvertToForm:
begin
FileToConvertFrom := ChangeFileext(FileToConvertFrom, '.TXT');
FileToConvertTo := ChangeFileext(FileToConvertFrom, '.DFM');
end;
ConvertToText:
begin
FileToConvertFrom := ChangeFileext(FileToConvertFrom, '.DFM');
FileToConvertTo := ChangeFileext(FileToConvertFrom, '.TXT');
end;
end;
try
try
{ Create a file stream for the specified file }
InputStream := TFileStream.Create(FileToConvertFrom, fmOpenRead);
OutputStream := TFileStream.Create(FileToConvertTo, fmCreate);
{ Now perform the selected conversion }
case ConversionType of
ConvertToForm : ObjectTextToResource(InputStream, OutputStream);
ConvertToText : ObjectResourceToText(InputStream, OutputStream);
end;
except
On EStreamError do Result := False;
end;
finally
InputStream.Free;
OutputStream.Free;
end;
end;