Subversion Repositories delphiutils

Rev

Rev 5 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. program DosLineConv;
  2.  
  3. uses
  4.   SysUtils,
  5.   Classes,
  6.   Dialogs,
  7.   Windows,
  8.   BinarySafeReplace in 'BinarySafeReplace.pas',
  9.   PosEx in 'PosEx.pas';
  10.  
  11. {$R *.res}
  12.  
  13. var
  14.   f: string;
  15.  
  16. const
  17.   target_format = #13#10;
  18.  
  19. type
  20.   EMoveError = class(Exception);
  21.  
  22. resourcestring
  23.   lng_file_not_found = 'The file "%s" was not found!';
  24.   lng_is_a_dir = 'MD5 checksum can only calculated for files.';
  25.   lng_binary_error = 'File "%s" has binary contents! You should only edit ASCII files.';
  26.   lng_syntax = 'Syntax: %s filename';
  27.   lng_error = 'An error occoured! Probably the file can''t be overwritten.';
  28.  
  29. procedure NormalizeLineBreaks(f, seq: string);
  30. var
  31.   t: string;
  32. begin
  33.   t := f+'~'; // Zwischenschritte schonen die Originaldatei (z.B. bei Fehlern)
  34.   DeleteFile(PChar(t));
  35.   BinarySafeReplaceFileContents(f, t, #13#10, #13); // Windows format
  36.   // BinarySafeReplaceFileContents(t, t, #10#13, #13);
  37.   BinarySafeReplaceFileContents(t, t, #10, #13); // MAC format
  38.   BinarySafeReplaceFileContents(t, t, #13, seq); // Linux format
  39.   DeleteFile(PChar(f));
  40.   if not MoveFile(PChar(t), PChar(f)) then
  41.   begin
  42.     DeleteFile(PChar(t));
  43.     raise EMoveError.Create(lng_error);
  44.   end;
  45. end;
  46.  
  47. function IsBinaryFile(f: string): boolean;
  48. var
  49.   Stream: TStream;
  50.   b: Byte;
  51. begin
  52.   result := false;
  53.   Stream := TFileStream.Create(f, fmOpenRead);
  54.   try
  55.     while Stream.Read(b, SizeOf(b)) > 0 do
  56.     begin
  57.       if (b <= 31) and (b <> 9) and (b <> 10) and (b <> 13) then
  58.       begin
  59.         result := true;
  60.         Exit;
  61.       end;
  62.     end;
  63.   finally
  64.     Stream.Free;
  65.   end;
  66. end;
  67.  
  68. var
  69.   i: integer;
  70.  
  71. begin
  72.   if ParamCount() < 1 then
  73.   begin
  74.     ShowMessageFmt(lng_syntax, [ExtractFileName(ParamStr(0))]);
  75.     Exit;
  76.   end;
  77.  
  78.   for i := 1 to ParamCount() do
  79.   begin
  80.     f := ParamStr(i);
  81.  
  82.     if DirectoryExists(f) then
  83.     begin
  84.       ShowMessage(lng_is_a_dir);
  85.     end
  86.     else
  87.     begin
  88.       if not FileExists(f) then
  89.       begin
  90.         ShowMessageFmt(lng_file_not_found, [f]);
  91.       end
  92.       else
  93.       begin
  94.         if IsBinaryFile(f) then
  95.         begin
  96.           ShowMessageFmt(lng_binary_error, [f]);
  97.         end
  98.         else
  99.         begin
  100.           try
  101.             NormalizeLineBreaks(f, #13#10);
  102.           except
  103.             ShowMessage(lng_error);
  104.           end;
  105.         end;
  106.       end;
  107.     end;
  108.   end;
  109. end.
  110.