Subversion Repositories fastphp

Rev

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

Rev 77 Rev 78
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
  sl: TStringList;
1067
  ms: TMemoryStream;
1068
  fil: TextFile;
1068
  fs: TFileStream;
1069
  i: Integer;
1069
  temp: array[0..2] of byte;
1070
begin
1070
begin
1071
  sl := TStringList.Create;
1071
  ms := TMemoryStream.Create;
-
 
1072
  fs := TFileStream.Create(filename, fmCreate);
1072
  try
1073
  try
-
 
1074
    // Save everything in a memory stream
-
 
1075
    // This should preserve LF / CRLF line endings
1073
    sl.Assign(SynEdit1.Lines); // Save without BOM
1076
    SynEdit1.Lines.SaveToStream(ms);
1074
 
1077
 
-
 
1078
    // Remove trailing linebreaks
1075
    while (sl.Count > 0) and (sl.Strings[sl.Count-1] = '') do
1079
    // (SynEdit1.Lines.TrailingLineBreak requires Delphi 10.1)
-
 
1080
    while ms.Size > 0 do
1076
    begin
1081
    begin
-
 
1082
      ms.Position := ms.Size-1;
1077
      sl.Delete(sl.Count-1);
1083
      ms.Read(temp[0], 1);
-
 
1084
      if (temp[0] = $0D) or (temp[0] = $0A) then
-
 
1085
      begin
-
 
1086
        ms.Size := ms.Size - 1;
-
 
1087
      end
-
 
1088
      else
-
 
1089
      begin
-
 
1090
        break;
-
 
1091
      end;
1078
    end;
1092
    end;
1079
 
1093
 
-
 
1094
    // 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).
-
 
1096
    // So we try to avoid that.
-
 
1097
    // Note that the output is still UTF-8 encoded if the input file was UTF-8 encoded
1080
    (*
1098
    ms.Position := 0;
-
 
1099
    ms.Read(temp[0], 3);
1081
    sl.TrailingLineBreak := false; // requires Delphi 10.1
1100
    if (temp[0] <> $EF) or (temp[1] <> $BB) or (temp[2] <> $BF) then
-
 
1101
    begin
1082
    sl.SaveToFile(filename);
1102
      ms.Position := 0;
1083
    *)
1103
    end;
1084
 
1104
 
1085
    // This code does the same, but works with older versions of Delphi, too.
-
 
1086
    AssignFile(fil, filename);
-
 
1087
    Rewrite(fil);
-
 
1088
    for i := 0 to sl.Count-2 do
1105
    // Now save to the file
1089
      writeln(fil, sl.Strings[i]);
-
 
1090
    write(fil, sl.Strings[sl.Count-1]);
1106
    fs.CopyFrom(ms, ms.Size-ms.Position);
1091
    CloseFile(fil);
-
 
1092
  finally
1107
  finally
-
 
1108
    FreeAndNil(ms);
1093
    sl.Free;
1109
    FreeAndNil(fs);
1094
  end;
1110
  end;
1095
end;
1111
end;
1096
 
1112
 
1097
procedure TForm1.StartCodeExplorer;
1113
procedure TForm1.StartCodeExplorer;
1098
begin
1114
begin