Недавно добавленные исходники

•  DeLiKaTeS Tetris (Тетрис)  3 679

•  TDictionary Custom Sort  5 812

•  Fast Watermark Sources  5 613

•  3D Designer  8 237

•  Sik Screen Capture  5 924

•  Patch Maker  6 396

•  Айболит (remote control)  6 389

•  ListBox Drag & Drop  5 246

•  Доска для игры Реверси  94 574

•  Графические эффекты  6 580

•  Рисование по маске  5 656

•  Перетаскивание изображений  4 769

•  Canvas Drawing  5 144

•  Рисование Луны  4 877

•  Поворот изображения  4 424

•  Рисование стержней  3 126

•  Paint on Shape  2 369

•  Генератор кроссвордов  3 238

•  Головоломка Paletto  2 559

•  Теорема Монжа об окружностях  3 337

•  Пазл Numbrix  2 210

•  Заборы и коммивояжеры  2 857

•  Игра HIP  1 831

•  Игра Go (Го)  1 751

•  Симулятор лифта  2 080

•  Программа укладки плитки  1 808

•  Генератор лабиринта  2 249

•  Проверка числового ввода  1 934

•  HEX View  2 236

•  Физический маятник  1 919

 
скрыть

  Форум  

Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

Сканируем файл в поисках текста




function ScanFile(const FileName: string; 
  const forString: string; 
  caseSensitive: Boolean): Longint; 
  { returns position of string in file or -1, if not found } 
const 
  BufferSize = $8001;  { 32K+1 bytes } 
var 
  pBuf, pEnd, pScan, pPos: PChar; 
  filesize: LongInt; 
  bytesRemaining: LongInt; 
  bytesToRead: Word; 
  F: file; 
  SearchFor: PChar; 
  oldMode: Word; 
begin 
  Result := -1;  { assume failure } 
  if (Length(forString) = 0) or (Length(FileName) = 0) then Exit; 
  SearchFor := nil; 
  pBuf      := nil; 

  { open file as binary, 1 byte recordsize } 
  AssignFile(F, FileName); 
  oldMode  := FileMode; 
  FileMode := 0;    { read-only access } 
  Reset(F, 1); 
  FileMode := oldMode; 
  try { allocate memory for buffer and pchar search string } 
    SearchFor := StrAlloc(Length(forString) + 1); 
    StrPCopy(SearchFor, forString); 
    if not caseSensitive then  { convert to upper case } 
      AnsiUpper(SearchFor); 
    GetMem(pBuf, BufferSize); 
    filesize       := System.Filesize(F); 
    bytesRemaining := filesize; 
    pPos           := nil; 
    while bytesRemaining > 0 do  
    begin 
      { calc how many bytes to read this round } 
      if bytesRemaining >= BufferSize then 
        bytesToRead := Pred(BufferSize) 
      else 
        bytesToRead := bytesRemaining; 

      { read a buffer full and zero-terminate the buffer } 
      BlockRead(F, pBuf^, bytesToRead, bytesToRead); 
      pEnd  := @pBuf[bytesToRead]; 
      pEnd^ := #0; 
       { scan the buffer. Problem: buffer may contain #0 chars! So we 
         treat it as a concatenation of zero-terminated strings. } 
      pScan := pBuf; 
      while pScan < pEnd do  
      begin 
        if not caseSensitive then { convert to upper case } 
          AnsiUpper(pScan); 
        pPos := StrPos(pScan, SearchFor);  { search for substring } 
        if pPos <> nil then  
        begin { Found it! } 
          Result := FileSize - bytesRemaining + 
            Longint(pPos) - Longint(pBuf); 
          Break; 
        end; 
        pScan := StrEnd(pScan); 
        Inc(pScan); 
      end; 
      if pPos <> nil then Break; 
      bytesRemaining := bytesRemaining - bytesToRead; 
      if bytesRemaining > 0 then  
      begin 
       { no luck in this buffers load. We need to handle the case of 
         the search string spanning two chunks of file now. We simply 
         go back a bit in the file and read from there, thus inspecting 
         some characters twice 
       } 
        Seek(F, FilePos(F) - Length(forString)); 
        bytesRemaining := bytesRemaining + Length(forString); 
      end; 
    end; { While } 
  finally 
    CloseFile(F); 
    if SearchFor <> nil then StrDispose(SearchFor); 
    if pBuf <> nil then FreeMem(pBuf, BufferSize); 
  end; 
end; { ScanFile }





Похожие по теме исходники

Чтение PSD файлов

Шифратор файлов

Разбиение файла на части

Поиск файлов

 

FileMan (менеджер файлов)

Поиск открытых файлов

Текст внутри файла




Copyright © 2004-2025 "Delphi Sources" by BrokenByte Software. Delphi World FAQ

Группа ВКонтакте