お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





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

"TStringListの拡張"

この発言に対し以下のコメントが寄せられています
#00719 佐藤 充男   さん RE:TStringListの拡張

こんにちは、佐藤 充男です。 ☆説明  TStringListはテキストファイルを操作するときに非常に便利ですが、  Strings[]は行末のCRLFがカットされて不便な場合があります。  そこで機能を拡張してCRLFを取りこむようにします。 ☆準備  1.フォームにButton1 を置きます。  2.Button1 のイベントハンドラとForm1 のソースをサンプルのように   入力してください。 ☆補足  ・Delphi3.1 でテスト。  ・TStringListと比較するサンプルです。テキストの最終行に改行を入れない   "Test.txt"を作成して比較してください。 [サンプルソース] ---------------------------------------------------------------------- unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TStringListEx = class(TStringList) private protected function GetTextStr: string; override; procedure SetTextStr(const Value: string); override; public end; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private 宣言 } public { Public 宣言 } strlstex: TStringListEx; strlst: TStringList; end; var Form1: TForm1; implementation {$R *.DFM} { TStringListEx } // 文字列取り出し function TStringListEx.GetTextStr: string; var I, L, Size, Count: Integer; P: PChar; S: string; begin Count := GetCount; Size := 0; { オリジナルコード for I := 0 to Count - 1 do Inc(Size, Length(Get(I)) + 2); } // 変更開始 for I := 0 to Count - 1 do Inc(Size, Length(Get(I))); // 変更終了 SetString(Result, nil, Size); P := Pointer(Result); for I := 0 to Count - 1 do begin S := Get(I); L := Length(S); if L <> 0 then begin System.Move(Pointer(S)^, P^, L); Inc(P, L); end; { オリジナルコード:CRLF追加は不要 P^ := #13; Inc(P); P^ := #10; Inc(P); } end; end; // 文字列分割 procedure TStringListEx.SetTextStr(const Value: string); var P, Start: PChar; S: string; begin BeginUpdate; try Clear; P := Pointer(Value); if P <> nil then while P^ <> #0 do begin Start := P; while not (P^ in [#0, #10, #13]) do Inc(P); { オリジナルコード SetString(S, Start, P - Start); Add(S); if P^ = #13 then Inc(P); if P^ = #10 then Inc(P); } // 変更開始 // TStringListでCR,LFを含めて文字列を分割する if ( P^ = #13 ) and ( (P+1)^ = #10 ) then Inc(P, 2) else begin if P^ = #13 then Inc(P); if P^ = #10 then Inc(P); end; SetString(S, Start, P - Start); Add(S); // 変更終了 end; finally EndUpdate; end; end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject); var strlst1: TStringListEx; strlst2: TStringList; begin strlst1 := TStringListEx.Create; strlst2 := TStringList.Create; strlst1.LoadFromFile('Test.txt'); strlst2.LoadFromFile('Test.txt'); ShowMessage('TStringListEx : ' + strlst1.Strings[0]); ShowMessage('TStringList : ' + strlst2.Strings[0]); strlst1.SaveToFile('Test01.txt'); strlst2.SaveToFile('Test02.txt'); strlst1.Free; strlst2.Free; end; end. ---------------------------------------------------------------------- #これができるからDelphiが好きなんです(^^)v 98/11/10(火) 00:56pm LDM03756 佐藤 充男 Original document by 佐藤 充男   氏 ID:(LDM03756)



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

Copyright 1996-2002 Delphi Users' Forum