分享
 
 
 

用DGL库实现的操作ini文件的类TFastIniFile

王朝other·作者佚名  2007-01-29
窄屏简体版  字體: |||超大  

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有