Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/base/UFiles.pas
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,60 @@ procedure ResetSingTemp;
//--------------------
// Saves a Song
//--------------------
function DetectLineBreak(const Name: IPath): RawByteString;
var
FileStream: TBinaryFileStream;
FileContent: RawByteString;
Index: integer;
CRLFCount: integer;
LFCount: integer;
CRCount: integer;
begin
Result := AnsiString(#13#10);
if not Name.IsFile then
Exit;

try
FileStream := TBinaryFileStream.Create(Name, fmOpenRead);
try
SetLength(FileContent, FileStream.Size);
if Length(FileContent) > 0 then
FileStream.ReadBuffer(FileContent[1], Length(FileContent));
finally
FileStream.Free;
end;
except
Exit;
end;

CRLFCount := 0;
LFCount := 0;
CRCount := 0;
Index := 1;
while Index <= Length(FileContent) do
begin
if FileContent[Index] = #13 then
begin
if (Index < Length(FileContent)) and (FileContent[Index + 1] = #10) then
begin
Inc(CRLFCount);
Inc(Index);
end
else
Inc(CRCount);
end
else if FileContent[Index] = #10 then
Inc(LFCount);

Inc(Index);
end;

if (LFCount > CRLFCount) and (LFCount >= CRCount) then
Result := AnsiString(#10)
else if CRCount > CRLFCount then
Result := AnsiString(#13);
end;

function SaveSong(const Song: TSong; const Tracks: array of TLines; const Name: IPath; Relative: boolean): TSaveSongResult;
var
CurrentLine: integer;
Expand Down Expand Up @@ -125,6 +179,8 @@ function SaveSong(const Song: TSong; const Tracks: array of TLines; const Name:
try
SongFile := TMemTextFileStream.Create(Name, fmCreate);
try
SongFile.LineBreak := DetectLineBreak(Name);

if Song.FormatVersion.MinVersion(1,0,0,true) then
begin
// Only save version if it is at least 1.0.0
Expand Down
Loading