본문 바로가기

카테고리 없음

[일반/컴포넌트] 파일에서 특정 Byte 모두 찾기

반응형
// AFilename : 파일명
// AByte: 검색할 Byte
// AList: 찾은 위치 리스트
// 리턴값: 찾은 갯수
function FindByteInFile(AFilename: string; AByte: byte; AList: TList): Integer;
var
  contents: string;
  curPos:  PChar;
  p:       integer;
  theByte: char;
  theFile: TFileStream;
begin
  theFile := TFileStream.Create(AFileName, fmOpenRead);
  try
    if theFile.Size > 0 then
    begin
      theByte := char(AByte);
      SetLength(contents, theFile.Size);
      theFile.Read(contents[1], theFile.Size);
      curPos := PChar(contents);

      while (curPos - PChar(contents) < theFile.Size) do
      begin
        p := Pos(theByte, curPos);

        if p > 0 then begin
          AList.Add(Pointer( (p + (curPos - PChar(contents)) - 1) ));
          curPos := @curPos[p + 1];
        end
        else
          Break;
      end;
    end;
  finally
    theFile.Free;
  end;

  Result := AList.Count;
end;
반응형