| 導購 | 订阅 | 在线投稿
分享
 
 
 

用DGL庫實現的操作ini文件的類TFastIniFile

來源:互聯網網民  2007-01-29 12:06:56  評論

DGL庫下載 安裝方法:將庫解壓到一個目錄,然後在Delphi中添加搜索路徑;

DGL簡要介紹:http://blog.csdn.net/housisong/archive/2005/10/17/505286.aspx

//========================================================================

//Delphi自帶的TIniFile太慢了,文件稍大一點根本就不能用,而且還有bug;

unit UnitFastIniFile;

//利用DGL庫實現的操作Ini文件的類TFastIniFile (兼容TIniFile )

//by HouSisong 2005.10

interface

uses

IniFiles,Classes

,_DGLMap_StringCaseInsensitive_Integer,DGL_StringCaseInsensitive;

type

//Ini文件操作類

//作爲Section和Key的字符串不區分大小寫

TFastIniFile= class(TCustomIniFile)

private

FSectionMap : TCIStrIntMap; //map: str <-> TCIStrHashMap

procedure privateLoadFromFile(const SrcFileName: string);

procedure SetFormStrings(SrcStrings: TStrings);

function AddSection(const SectionName: string): TCIStrHashMap;

function FindSection(const SectionName: string): TCIStrHashMap;

procedure SaveToStrings(DstStrings: TStrings);

procedure _ReadSectionValues(const Section: string; Strings: TStrings;

const IsReadValue: boolean);

procedure Clear();

public

constructor Create(const DstFileName: string);//FileName can ''

procedure LoadFromFile(const SrcFileName: string);

destructor Destroy; override;

function ReadString(const Section, Key, DefaultValue: string): string; override;

procedure WriteString(const Section, Key, Value: String); override;

procedure ReadSection(const Section: string; Strings: TStrings); override;

procedure ReadSections(DstSectionNames: TStrings); override;

procedure ReadSectionValues(const Section: string; Strings: TStrings); override;

procedure EraseSection(const Section: string); override;

procedure DeleteKey(const Section, Key: String); override;

procedure UpdateFile; override;

function SectionExists(const Section: string): Boolean;

function ValueExists(const Section, Key: string): Boolean;

end;

implementation

uses

SysUtils;

function TFastIniFile.AddSection(const SectionName:string):TCIStrHashMap;

var

itS : ICIStrIntMapIterator;

begin

result:=self.FindSection(SectionName);

if result=nil then

begin

result:=TCIStrHashMap.Create();

FSectionMap.Insert(SectionName,integer(result));

end;

end;

function TFastIniFile.FindSection(const SectionName:string):TCIStrHashMap;

var

itS : ICIStrIntMapIterator;

begin

itS:=self.FSectionMap.Find(SectionName);

if not itS.IsEqual(FSectionMap.ItEnd) then

result:=TCIStrHashMap(itS.Value)

else

result:=nil;

end;

procedure TFastIniFile.DeleteKey(const Section, Key: String);

var

st : TCIStrHashMap;

begin

st:=FindSection(Section);

if st<>nil then

st.EraseKey(Key);

end;

destructor TFastIniFile.Destroy;

begin

UpdateFile;

Clear();

inherited Destroy;

end;

procedure TFastIniFile.EraseSection(const Section: string);

var

st : TCIStrHashMap;

begin

st:=FindSection(Section);

if st<>nil then

begin

st.Free();

self.FSectionMap.EraseKey(Section);

end;

end;

procedure TFastIniFile.ReadSection(const Section: string;

Strings: TStrings);

begin

_ReadSectionValues(Section,Strings,false);

end;

procedure TFastIniFile.ReadSections(DstSectionNames: TStrings);

var

i : integer;

itS : ICIStrIntMapIterator;

VS : TCIStrVector;

begin

DstSectionNames.BeginUpdate;

VS:=nil;

try

DstSectionNames.Clear();

if (FSectionMap.Size=0) then exit;

VS :=TCIStrVector.Create();

itS:=FSectionMap.ItBegin;

for i:=0 to FSectionMap.Size-1 do

begin

VS.PushBack(itS.Key);

itS.Next();

end;

TCIStrAlgorithms.Sort(VS.ItBegin,Vs.ItEnd);

for i:=0 to VS.Size-1 do

DstSectionNames.Add(VS.Items[i]);

finally

DstSectionNames.EndUpdate;

end;

end;

procedure TFastIniFile._ReadSectionValues(const Section: string;

Strings: TStrings;const IsReadValue:boolean);

var

st : TCIStrHashMap;

it : ICIStrMapIterator;

i : integer;

VS : TCIStrVector;

begin

Strings.BeginUpdate;

VS:=nil;

try

Strings.Clear();

st:=FindSection(Section);

if (st=nil) or (st.Size=0) then exit;

VS :=TCIStrVector.Create();

it:=st.ItBegin;

for i:=0 to st.Size-1 do

begin

if IsReadValue then

VS.PushBack(it.Key+'='+it.Value)

else

VS.PushBack(it.Key);

it.Next;

end;

//key排序

TCIStrAlgorithms.Sort(VS.ItBegin,Vs.ItEnd);

for i:=0 to VS.Size-1 do

Strings.Add(VS.Items[i]);

finally

Strings.EndUpdate;

Vs.Free();

end;

end;

procedure TFastIniFile.ReadSectionValues(const Section: string;

Strings: TStrings);

begin

_ReadSectionValues(Section,Strings,true);

end;

function TFastIniFile.ReadString(const Section, Key, DefaultValue: string): string;

var

st : TCIStrHashMap;

it : ICIStrMapIterator;

begin

st:=FindSection(Section);

if st=nil then

result:=DefaultValue

else

begin

it:=st.Find(Key);

if it.IsEqual(st.ItEnd) then

result:=DefaultValue

else

result:=it.Value;

end;

end;

procedure TFastIniFile.SaveToStrings(DstStrings:TStrings);

var

I, J: Integer;

Strings: TCIStrHashMap;

itS : ICIStrIntMapIterator;

it : ICIStrMapIterator;

VS : TCIStrVector;

begin

DstStrings.BeginUpdate;

VS:=nil;

try

VS :=TCIStrVector.Create();

itS:=FSectionMap.ItBegin;

for I := 0 to self.FSectionMap.Size - 1 do

begin

DstStrings.Add('[' + itS.Key + ']');

Strings := TCIStrHashMap(its.Value);

VS.Clear();

it:=Strings.ItBegin;

for J := 0 to Strings.Size - 1 do

begin

VS.PushBack(' '+it.Key+'='+it.Value);

it.Next();

end;

TCIStrAlgorithms.Sort(VS.ItBegin,Vs.ItEnd);

for J:=0 to VS.Size-1 do

DstStrings.Add(VS.Items[J]);

DstStrings.Add('');

itS.Next();

end;

finally

DstStrings.EndUpdate;

VS.Free();

end;

end;

procedure TFastIniFile.UpdateFile;

var

List: TStringList;

begin

if FileName='' then exit;

List := TStringList.Create;

try

SaveToStrings(List);

List.SaveToFile(FileName);

finally

List.Free;

end;

end;

procedure TFastIniFile.WriteString(const Section, Key, Value: String);

var

st : TCIStrHashMap;

begin

st:=AddSection(Section);

st.Insert(Key,Value);

end;

procedure TFastIniFile.Clear;

var

i : integer;

itS : ICIStrIntMapIterator;

begin

if FSectionMap.Size=0 then exit;

itS:=FSectionMap.ItBegin;

for i:=0 to FSectionMap.Size-1 do

begin

TCIStrHashMap(itS.Value).Free;

itS.Next();

end;

FSectionMap.Clear();

end;

procedure TFastIniFile.SetFormStrings(SrcStrings:TStrings);

var

I, J: Integer;

S: string;

Strings: TCIStrHashMap;

begin

Clear;

Strings := nil;

for I := 0 to SrcStrings.Count - 1 do

begin

S := Trim(SrcStrings[I]);

if (S <> '') and (S[1] <> ';') then

begin

if (S[1] = '[') and (S[Length(S)] = ']') then

begin

Delete(S, 1, 1);

SetLength(S, Length(S)-1);

Strings := AddSection(Trim(S));

end

else

begin

if Strings <> nil then

begin

J := Pos('=', S);

if J > 0 then // remove spaces before and after '='

Strings.Insert(Trim(Copy(S, 1, J-1)) , Trim(Copy(S, J+1, MaxInt)) )

else

Strings.Insert(S,'');

end;

end;

end;

end;

end;

procedure TFastIniFile.privateLoadFromFile(const SrcFileName: string);

var

List: TStringList;

begin

if (SrcFileName <> '') and FileExists(SrcFileName) then

begin

List := TStringList.Create;

try

List.LoadFromFile(SrcFileName);

SetFormStrings(List);

finally

List.Free;

end;

end

else

Clear();

end;

constructor TFastIniFile.Create(const DstFileName: string);

begin

inherited Create(DstFileName);

FSectionMap:=TCIStrIntMap.Create();

privateLoadFromFile(DstFileName);

end;

function TFastIniFile.SectionExists(const Section: string): Boolean;

begin

result:=self.FindSection(Section)<>nil;

end;

function TFastIniFile.ValueExists(const Section, Key: string): Boolean;

var

st : TCIStrHashMap;

begin

st:=FindSection(Section);

if st<>nil then

result:=not st.Find(Key).IsEqual(st.ItEnd)

else

result:=false;

end;

procedure TFastIniFile.LoadFromFile(const SrcFileName: string);

begin

self.privateLoadFromFile(SrcFileName);

end;

end.

 
特别声明:以上内容(如有图片或视频亦包括在内)为网络用户发布,本站仅提供信息存储服务。
 
[url=http://cosoft.org.cn/projects/dgl/]DGL庫下載[/url] 安裝方法:將庫解壓到一個目錄,然後在Delphi中添加搜索路徑; DGL簡要介紹:[url=http://blog.csdn.net/housisong/archive/2005/10/17/505286.aspx]http://blog.csdn.net/housisong/archive/2005/10/17/505286.aspx[/url] //======================================================================== //Delphi自帶的TIniFile太慢了,文件稍大一點根本就不能用,而且還有bug; unit UnitFastIniFile; //利用DGL庫實現的操作Ini文件的類TFastIniFile (兼容TIniFile ) //by HouSisong 2005.10 interface uses IniFiles,Classes ,_DGLMap_StringCaseInsensitive_Integer,DGL_StringCaseInsensitive; type //Ini文件操作類 //作爲Section和Key的字符串不區分大小寫 TFastIniFile= class(TCustomIniFile) private FSectionMap : TCIStrIntMap; //map: str <-> TCIStrHashMap procedure privateLoadFromFile(const SrcFileName: string); procedure SetFormStrings(SrcStrings: TStrings); function AddSection(const SectionName: string): TCIStrHashMap; function FindSection(const SectionName: string): TCIStrHashMap; procedure SaveToStrings(DstStrings: TStrings); procedure _ReadSectionValues(const Section: string; Strings: TStrings; const IsReadValue: boolean); procedure Clear(); public constructor Create(const DstFileName: string);//FileName can '' procedure LoadFromFile(const SrcFileName: string); destructor Destroy; override; function ReadString(const Section, Key, DefaultValue: string): string; override; procedure WriteString(const Section, Key, Value: String); override; procedure ReadSection(const Section: string; Strings: TStrings); override; procedure ReadSections(DstSectionNames: TStrings); override; procedure ReadSectionValues(const Section: string; Strings: TStrings); override; procedure EraseSection(const Section: string); override; procedure DeleteKey(const Section, Key: String); override; procedure UpdateFile; override; function SectionExists(const Section: string): Boolean; function ValueExists(const Section, Key: string): Boolean; end; implementation uses SysUtils; function TFastIniFile.AddSection(const SectionName:string):TCIStrHashMap; var itS : ICIStrIntMapIterator; begin result:=self.FindSection(SectionName); if result=nil then begin result:=TCIStrHashMap.Create(); FSectionMap.Insert(SectionName,integer(result)); end; end; function TFastIniFile.FindSection(const SectionName:string):TCIStrHashMap; var itS : ICIStrIntMapIterator; begin itS:=self.FSectionMap.Find(SectionName); if not itS.IsEqual(FSectionMap.ItEnd) then result:=TCIStrHashMap(itS.Value) else result:=nil; end; procedure TFastIniFile.DeleteKey(const Section, Key: String); var st : TCIStrHashMap; begin st:=FindSection(Section); if st<>nil then st.EraseKey(Key); end; destructor TFastIniFile.Destroy; begin UpdateFile; Clear(); inherited Destroy; end; procedure TFastIniFile.EraseSection(const Section: string); var st : TCIStrHashMap; begin st:=FindSection(Section); if st<>nil then begin st.Free(); self.FSectionMap.EraseKey(Section); end; end; procedure TFastIniFile.ReadSection(const Section: string; Strings: TStrings); begin _ReadSectionValues(Section,Strings,false); end; procedure TFastIniFile.ReadSections(DstSectionNames: TStrings); var i : integer; itS : ICIStrIntMapIterator; VS : TCIStrVector; begin DstSectionNames.BeginUpdate; VS:=nil; try DstSectionNames.Clear(); if (FSectionMap.Size=0) then exit; VS :=TCIStrVector.Create(); itS:=FSectionMap.ItBegin; for i:=0 to FSectionMap.Size-1 do begin VS.PushBack(itS.Key); itS.Next(); end; TCIStrAlgorithms.Sort(VS.ItBegin,Vs.ItEnd); for i:=0 to VS.Size-1 do DstSectionNames.Add(VS.Items[i]); finally DstSectionNames.EndUpdate; end; end; procedure TFastIniFile._ReadSectionValues(const Section: string; Strings: TStrings;const IsReadValue:boolean); var st : TCIStrHashMap; it : ICIStrMapIterator; i : integer; VS : TCIStrVector; begin Strings.BeginUpdate; VS:=nil; try Strings.Clear(); st:=FindSection(Section); if (st=nil) or (st.Size=0) then exit; VS :=TCIStrVector.Create(); it:=st.ItBegin; for i:=0 to st.Size-1 do begin if IsReadValue then VS.PushBack(it.Key+'='+it.Value) else VS.PushBack(it.Key); it.Next; end; //key排序 TCIStrAlgorithms.Sort(VS.ItBegin,Vs.ItEnd); for i:=0 to VS.Size-1 do Strings.Add(VS.Items[i]); finally Strings.EndUpdate; Vs.Free(); end; end; procedure TFastIniFile.ReadSectionValues(const Section: string; Strings: TStrings); begin _ReadSectionValues(Section,Strings,true); end; function TFastIniFile.ReadString(const Section, Key, DefaultValue: string): string; var st : TCIStrHashMap; it : ICIStrMapIterator; begin st:=FindSection(Section); if st=nil then result:=DefaultValue else begin it:=st.Find(Key); if it.IsEqual(st.ItEnd) then result:=DefaultValue else result:=it.Value; end; end; procedure TFastIniFile.SaveToStrings(DstStrings:TStrings); var I, J: Integer; Strings: TCIStrHashMap; itS : ICIStrIntMapIterator; it : ICIStrMapIterator; VS : TCIStrVector; begin DstStrings.BeginUpdate; VS:=nil; try VS :=TCIStrVector.Create(); itS:=FSectionMap.ItBegin; for I := 0 to self.FSectionMap.Size - 1 do begin DstStrings.Add('[' + itS.Key + ']'); Strings := TCIStrHashMap(its.Value); VS.Clear(); it:=Strings.ItBegin; for J := 0 to Strings.Size - 1 do begin VS.PushBack(' '+it.Key+'='+it.Value); it.Next(); end; TCIStrAlgorithms.Sort(VS.ItBegin,Vs.ItEnd); for J:=0 to VS.Size-1 do DstStrings.Add(VS.Items[J]); DstStrings.Add(''); itS.Next(); end; finally DstStrings.EndUpdate; VS.Free(); end; end; procedure TFastIniFile.UpdateFile; var List: TStringList; begin if FileName='' then exit; List := TStringList.Create; try SaveToStrings(List); List.SaveToFile(FileName); finally List.Free; end; end; procedure TFastIniFile.WriteString(const Section, Key, Value: String); var st : TCIStrHashMap; begin st:=AddSection(Section); st.Insert(Key,Value); end; procedure TFastIniFile.Clear; var i : integer; itS : ICIStrIntMapIterator; begin if FSectionMap.Size=0 then exit; itS:=FSectionMap.ItBegin; for i:=0 to FSectionMap.Size-1 do begin TCIStrHashMap(itS.Value).Free; itS.Next(); end; FSectionMap.Clear(); end; procedure TFastIniFile.SetFormStrings(SrcStrings:TStrings); var I, J: Integer; S: string; Strings: TCIStrHashMap; begin Clear; Strings := nil; for I := 0 to SrcStrings.Count - 1 do begin S := Trim(SrcStrings[I]); if (S <> '') and (S[1] <> ';') then begin if (S[1] = '[') and (S[Length(S)] = ']') then begin Delete(S, 1, 1); SetLength(S, Length(S)-1); Strings := AddSection(Trim(S)); end else begin if Strings <> nil then begin J := Pos('=', S); if J > 0 then // remove spaces before and after '=' Strings.Insert(Trim(Copy(S, 1, J-1)) , Trim(Copy(S, J+1, MaxInt)) ) else Strings.Insert(S,''); end; end; end; end; end; procedure TFastIniFile.privateLoadFromFile(const SrcFileName: string); var List: TStringList; begin if (SrcFileName <> '') and FileExists(SrcFileName) then begin List := TStringList.Create; try List.LoadFromFile(SrcFileName); SetFormStrings(List); finally List.Free; end; end else Clear(); end; constructor TFastIniFile.Create(const DstFileName: string); begin inherited Create(DstFileName); FSectionMap:=TCIStrIntMap.Create(); privateLoadFromFile(DstFileName); end; function TFastIniFile.SectionExists(const Section: string): Boolean; begin result:=self.FindSection(Section)<>nil; end; function TFastIniFile.ValueExists(const Section, Key: string): Boolean; var st : TCIStrHashMap; begin st:=FindSection(Section); if st<>nil then result:=not st.Find(Key).IsEqual(st.ItEnd) else result:=false; end; procedure TFastIniFile.LoadFromFile(const SrcFileName: string); begin self.privateLoadFromFile(SrcFileName); end; end.
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有