Talk:DOS2UNIX/de
From Lazarus wiki
Jump to navigationJump to search
Why not simply us the AdjustLineBreak function to handle this (no need to re-invent the wheel)?
program neol;
{$Mode ObjFpc}
{$H+}
uses SysUtils, Classes;
procedure CheckParams;
begin
if (ParamCount = 0) or (CompareText(ParamStr(1),'--help') = 0) then
begin
writeln('neol Converts end-of-lines in a textfile to native eol style.');
writeln('Usage:');
writeln('neol file1 [file2 [file3 [...]]]');
writeln('neol --help');
writeln;
writeln(' file1, file2, file3: files to convert.');
writeln(' Note: no backups are made, changes are applied to the original file.');
writeln;
writeln(' --help: show this help screen.');
halt;
end;
end;
procedure ConvertEol(const Fn: String);
var
FS: TFileStream;
Sz: Int64;
TheString: String;
begin
try
FS := TFileStream.Create(Fn, fmOpenReadWrite or fmShareExclusive);
try
FS.Position := 0;
Sz := FS.Size;
{$ifdef debug_neol}
writeln('FS.Size = ',fs.size);
{$endif}
SetLength(TheString, Sz);
if FS.Read(TheString[1], Sz) <> Sz then
Raise EStreamError.CreateFmt('Error reading from %s',[Fn]);
{$ifdef debug_neol}
writeln('Length(TheString) = ',Length(TheString));
{$endif}
TheString := AdjustLineBreaks(TheString);
{$ifdef debug_neol}
writeln('Length(TheString) = ',Length(TheString));
{$endif}
FS.Position := 0;
if FS.Write(TheString[1], Length(TheString)) <> Length(TheString) then
Raise EStreamError.CreateFmt('Error writing to %s',[Fn]);
finally
if Assigned(FS) then FS.Free;
end;
except
on E: EStreamError do
begin
writeln('An error occurred: ',E.Message);
end;
end;
end;
var
i: Integer;
begin
CheckParams;
for i := 1 to ParamCount do ConvertEol(ParamStr(i));
end.