Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.168 Beiträge
 
Delphi 12 Athens
 
#2

Re: Zeilen einer Textdatei zählen

  Alt 8. Jun 2009, 22:25
etwas gekürzt:
Delphi-Quellcode:
function csCountLines(const Filepath : String) : Integer;

const
  BUFSIZE = 65536;

var
  fs : TFileStream;
  sBuf : array [1..BUFSIZE] of AnsiChar;
  iRead : Integer;
  i : Integer;

begin
  Result := 0;
  fs := TFileStream.Create(Filepath, fmOpenRead);
  try
    if fs.Size > 0 then Inc(Result);
    repeat
      iRead := fs.Read(sBuf, BUFSIZE);
      for i := 1 to iRead do
        if sBuf[i] = #10 then Inc(Result);
    until iRead <> BUFSIZE;
  finally
    fs.Free;
  end;
end;
Sollte alle #13-#10-Kombinationen erkennen > #10, #13 und #13#10
Delphi-Quellcode:
function csCountLines(const Filepath : String) : Integer;

const
  BUFSIZE = 65536;

var
  fs : TFileStream;
  sBuf : array [1..BUFSIZE + 1] of AnsiChar;
  iRead : Integer;
  iOffset : Integer;
  i : Integer;

begin
  Result := 0;
  iOffset := 0;
  fs := TFileStream.Create(Filepath, fmOpenRead);
  try
    if fs.Size > 0 then Inc(Result);
    repeat
      iRead := fs.Read(sBuf[iOffset + 1], BUFSIZE) + iOffset;
      iOffset := 0;
      i := 1;
      while i <= iRead do begin
        case sBuf[i] of
          #10: Inc(Result);
          #13: if i < iRead then begin
                 Inc(Result);
                 if sBuf[i + 1] = #10 then Inc(i);
               end else if iRead < BUFSIZE then
                 Inc(Result)
               else begin
                 sBuf[1] := sBuf[iRead];
                 iOffset := 1;
               end;
        end;
        Inc(i);
      end;
    until iRead <> BUFSIZE;
  finally
    fs.Free;
  end;
end;

und noch eine spezielle Version für Unicode-Dateien:
(die vorherigen Versionen sind für Ansi-Dateien)
Delphi-Quellcode:
function csCountLines(const Filepath : String) : Integer;

const
  BUFSIZE = 65536;

var
  fs : TFileStream;
  sBuf : array [1..BUFSIZE + 1] of WideChar;
  iRead : Integer;
  iOffset : Integer;
  i : Integer;

begin
  Result := 0;
  iOffset := 0;
  fs := TFileStream.Create(Filepath, fmOpenRead);
  try
    if fs.Size > 0 then Inc(Result);
    repeat
      iRead := fs.Read(sBuf[iOffset + 1], BUFSIZE) + iOffset;
      iOffset := 0;
      i := 1;
      while i <= iRead do begin
        case sBuf[i] of
          #$0A, #$0085, #$2028: Inc(Result);
          #$0D: if i < iRead then begin
                  Inc(Result);
                  if (sBuf[i + 1] = #$0A) or (sBuf[i + 1] = #$0085) then Inc(i);
                end else if iRead < BUFSIZE then
                  Inc(Result)
                else begin
                  sBuf[1] := sBuf[iRead];
                  iOffset := 1;
                end;
        end;
        Inc(i);
      end;
    until iRead <> BUFSIZE;
  finally
    fs.Free;
  end;
end;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat