Subversion Repositories fastphp

Rev

Rev 78 | Rev 80 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 78 Rev 79
Line 1062... Line 1062...
1062
  end;
1062
  end;
1063
end;
1063
end;
1064
 
1064
 
1065
procedure TForm1.SaveToFile(filename: string);
1065
procedure TForm1.SaveToFile(filename: string);
1066
var
1066
var
-
 
1067
  ss: TStringStream;
1067
  ms: TMemoryStream;
1068
  ms: TMemoryStream;
1068
  fs: TFileStream;
1069
  fs: TFileStream;
1069
  temp: array[0..2] of byte;
1070
  eolStyle: string;
-
 
1071
  str: string;
1070
begin
1072
begin
1071
  ms := TMemoryStream.Create;
1073
  ms := TMemoryStream.Create;
-
 
1074
  ss := TStringStream.Create('');
1072
  fs := TFileStream.Create(filename, fmCreate);
1075
  fs := TFileStream.Create(filename, fmCreate);
1073
  try
1076
  try
1074
    // Save everything in a memory stream
1077
    // Save everything in a memory stream and then to a string
-
 
1078
    // in comparison to "str := SynEdit1.Lines.Text;",
1075
    // This should preserve LF / CRLF line endings
1079
    // This approach should preserve LF / CRLF line endings
1076
    SynEdit1.Lines.SaveToStream(ms);
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
1077
 
1086
 
1078
    // Remove trailing linebreaks
1087
    // Detect current line-endings
1079
    // (SynEdit1.Lines.TrailingLineBreak requires Delphi 10.1)
-
 
1080
    while ms.Size > 0 do
-
 
1081
    begin
-
 
1082
      ms.Position := ms.Size-1;
-
 
1083
      ms.Read(temp[0], 1);
-
 
1084
      if (temp[0] = $0D) or (temp[0] = $0A) then
1088
    if Copy(str, 1, 2) = '#!' then
1085
      begin
1089
    begin
-
 
1090
      // Shebang. Use ONLY Linux LF
-
 
1091
      str := StringReplace(str, #13#10, #10, [rfReplaceAll]);
1086
        ms.Size := ms.Size - 1;
1092
      eolStyle := #10 // Linux LF
1087
      end
1093
    end
1088
      else
1094
    else
1089
      begin
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
        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
1090
        break;
1108
        else
-
 
1109
          eolStyle := #13#10; // (Should not happen)
1091
      end;
1110
      end;
1092
    end;
1111
    end;
1093
 
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
 
1094
    // Old versions of Delphi/SynEdit write an UTF-8 BOM, which makes problems
1123
    // Old versions of Delphi/SynEdit write an UTF-8 BOM, which makes problems
1095
    // e.g. with AJAX handlers (because AJAX reponses must not have a BOM).
1124
    // e.g. with AJAX handlers (because AJAX reponses must not have a BOM).
1096
    // So we try to avoid that.
1125
    // So we try to avoid that.
1097
    // Note that the output is still UTF-8 encoded if the input file was UTF-8 encoded
1126
    // Note that the output is still UTF-8 encoded if the input file was UTF-8 encoded
1098
    ms.Position := 0;
-
 
1099
    ms.Read(temp[0], 3);
-
 
1100
    if (temp[0] <> $EF) or (temp[1] <> $BB) or (temp[2] <> $BF) then
1127
    if Copy(str,1,3) = #$EF#$BB#$BF then Delete(str, 1, 3);
1101
    begin
-
 
1102
      ms.Position := 0;
-
 
1103
    end;
-
 
1104
 
1128
 
1105
    // Now save to the file
1129
    // Now save to the file
-
 
1130
    ss.WriteString(str);
-
 
1131
    ss.Position := 0;
1106
    fs.CopyFrom(ms, ms.Size-ms.Position);
1132
    fs.CopyFrom(ss, ss.Size-ss.Position);
1107
  finally
1133
  finally
1108
    FreeAndNil(ms);
1134
    FreeAndNil(ms);
-
 
1135
    FreeAndNil(ss);
1109
    FreeAndNil(fs);
1136
    FreeAndNil(fs);
1110
  end;
1137
  end;
1111
end;
1138
end;
1112
 
1139
 
1113
procedure TForm1.StartCodeExplorer;
1140
procedure TForm1.StartCodeExplorer;