Login | ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/fastphp/trunk/EditorMain.pas
(Generate patch)

Comparing trunk/EditorMain.pas (file contents):
Revision 78 by daniel-marschall, Mon Aug 17 07:32:28 2020 UTC vs.
Revision 79 by daniel-marschall, Mon Aug 24 08:22:10 2020 UTC

# Line 1064 | Line 1064 | end;
1064  
1065   procedure TForm1.SaveToFile(filename: string);
1066   var
1067 +  ss: TStringStream;
1068    ms: TMemoryStream;
1069    fs: TFileStream;
1070 <  temp: array[0..2] of byte;
1070 >  eolStyle: string;
1071 >  str: string;
1072   begin
1073    ms := TMemoryStream.Create;
1074 +  ss := TStringStream.Create('');
1075    fs := TFileStream.Create(filename, fmCreate);
1076    try
1077 <    // Save everything in a memory stream
1078 <    // This should preserve LF / CRLF line endings
1077 >    // Save everything in a memory stream and then to a string
1078 >    // in comparison to "str := SynEdit1.Lines.Text;",
1079 >    // This approach should preserve LF / CRLF line endings
1080      SynEdit1.Lines.SaveToStream(ms);
1081 +    ms.Position := 0;
1082 +    ss.CopyFrom(ms, ms.Size);
1083 +    ss.Position := 0;
1084 +    str := ss.ReadString(ss.Size);
1085 +    ss.Size := 0; // clear string-stream, because we need it later again
1086  
1087 <    // Remove trailing linebreaks
1088 <    // (SynEdit1.Lines.TrailingLineBreak requires Delphi 10.1)
1080 <    while ms.Size > 0 do
1087 >    // Detect current line-endings
1088 >    if Copy(str, 1, 2) = '#!' then
1089      begin
1090 <      ms.Position := ms.Size-1;
1091 <      ms.Read(temp[0], 1);
1092 <      if (temp[0] = $0D) or (temp[0] = $0A) then
1093 <      begin
1094 <        ms.Size := ms.Size - 1;
1095 <      end
1090 >      // Shebang. Use ONLY Linux LF
1091 >      str := StringReplace(str, #13#10, #10, [rfReplaceAll]);
1092 >      eolStyle := #10 // Linux LF
1093 >    end
1094 >    else
1095 >    begin
1096 >      if Pos(#13#10, str) > 0 then
1097 >        eolStyle := #13#10 // Windows CRLF
1098 >      else if Pos(#10, str) > 0 then
1099 >        eolStyle := #10 // Linux LF
1100        else
1101        begin
1102 <        break;
1102 >        if DefaultTextLineBreakStyle = tlbsLF then
1103 >          eolStyle := #10 // Linux LF
1104 >        else if DefaultTextLineBreakStyle = tlbsCRLF then
1105 >          eolStyle := #13#10 // Windows CRLF
1106 >        //else if DefaultTextLineBreakStyle = tlbsCR then
1107 >        //  eolStyle := #13 // Old Mac CR
1108 >        else
1109 >          eolStyle := #13#10; // (Should not happen)
1110        end;
1111      end;
1112  
1113 +    // Unitfy line-endings
1114 +    str := StringReplace(str, #13#10, eolStyle, [rfReplaceAll]);
1115 +    str := StringReplace(str, #10, eolStyle, [rfReplaceAll]);
1116 +    str := StringReplace(str, #13, '', [rfReplaceAll]);
1117 +
1118 +    // Replace all trailing linebreaks by a single line break
1119 +    // Note: Removing all line breaks is not good, since Linux's "nano" will
1120 +    //       re-add a linebreak at the end of the file
1121 +    str := TrimRight(str) + eolStyle;
1122 +
1123      // Old versions of Delphi/SynEdit write an UTF-8 BOM, which makes problems
1124      // e.g. with AJAX handlers (because AJAX reponses must not have a BOM).
1125      // So we try to avoid that.
1126      // Note that the output is still UTF-8 encoded if the input file was UTF-8 encoded
1127 <    ms.Position := 0;
1099 <    ms.Read(temp[0], 3);
1100 <    if (temp[0] <> $EF) or (temp[1] <> $BB) or (temp[2] <> $BF) then
1101 <    begin
1102 <      ms.Position := 0;
1103 <    end;
1127 >    if Copy(str,1,3) = #$EF#$BB#$BF then Delete(str, 1, 3);
1128  
1129      // Now save to the file
1130 <    fs.CopyFrom(ms, ms.Size-ms.Position);
1130 >    ss.WriteString(str);
1131 >    ss.Position := 0;
1132 >    fs.CopyFrom(ss, ss.Size-ss.Position);
1133    finally
1134      FreeAndNil(ms);
1135 +    FreeAndNil(ss);
1136      FreeAndNil(fs);
1137    end;
1138   end;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines