お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"TStrings 高速ファイル読み込み"

この発言に対し以下のコメントが寄せられています
#00790 本田勝彦 さん RE:TStrings 高速ファイル読み込み

 みなさんこんにちは。nifty:FDELPHI/MES/10/#4505 の高速ファイル読み込みの バッファ利用判です。↑では、通常の TStrings.LoadFromFile と同様、 ファイルサイズの倍のメモリを必要とするので、スワップが始まると悲惨な 結果になります。以下は、ファイルサイズ+8Kバイトのバッファを利用 する方式です。 procedure LoadFromFile(Strings: TStrings; const FileName: String); const BufferSize = $2000; var I, ReadCount, LineCount: Integer; LineRemained: Boolean; S, Str: String; Buffer, P, Start: PChar; Fs: TFileStream; begin Strings.BeginUpdate; try Strings.Clear; Fs := TFileStream.Create(FileName, fmOpenRead); try Buffer := StrAlloc(BufferSize + 1); try // バッファに読み込みながら、#10 をカウントアップ LineCount := 0; LineRemained := False; repeat ReadCount := Fs.Read(Buffer^, BufferSize); if ReadCount > 0 then LineRemained := False; Buffer[ReadCount] := #0; P := Buffer; while P^ <> #0 do begin while not (P^ in [#0, #10, #13]) do Inc(P); if P^ <> #0 then Inc(LineCount) else LineRemained := True; if P^ = #13 then Inc(P); if P^ = #10 then Inc(P); end; until ReadCount = 0; if LineRemained then Inc(LineCount); // 取得した行数分の領域を確保 for I := 0 to LineCount - 1 do Strings.Add(''); // バッファを利用して読み込み Fs.Seek(0, 0); LineCount := 0; Str := ''; repeat ReadCount := Fs.Read(Buffer^, BufferSize); Buffer[ReadCount] := #0; P := Buffer; while P^ <> #0 do begin Start := P; while not (P^ in [#0, #10, #13]) do Inc(P); SetString(S, Start, P - Start); if P^ = #0 then Str := S else begin if Str <> '' then begin S := Str + S; Str := ''; end; Strings[LineCount] := S; Inc(LineCount); end; if P^ = #13 then Inc(P); if P^ = #10 then Inc(P); end; until ReadCount = 0; if Str <> '' then Strings[LineCount] := Str; finally StrDispose(Buffer); end; finally Fs.Free; end; finally Strings.EndUpdate; end; end; 利用法 var List: TStringList; FileName: String; begin if OpenDialog1.Execute then begin FileName := OpenDialog1.FileName; // LoadFromFile(Memo1.Lines, FileName); // なども可能 // LoadFromFile(ListBox1.Items, FileName); // 〃 List := TStringList.Create; try LoadFromFile(List, FileName); // 処理 finally List.Free; end; end; end; 本田勝彦 Original document by 本田勝彦 氏 ID:(VYR01647)



ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum