封装的ini文件类。保存为unicode的。解决delphi xe的TiniFile保存后不为unicode的问题

网络摘抄

//刚封装的ini文件类。保存为unicode的。解决delphi xe的TiniFile保存后不为unicode的问题
//今天发现delphi xe的TiniFile类保存后的文件不是以unicode存储的,
//所以就自己写了个和他功能类似的类,可能比原版的TIniFile少了一些方法。
//其他的方法属性等我的工程都没用到,所以就没写其他的函数。函数只有如下,正常情况下够用了
//考虑到时间问题就就用了tstringlist类了。所以效率不怎么样。很一般。
//很垃圾的代码,不嫌弃的人拿去用吧!
//
function ReadString(const cSection, cIdent, cDefault: string): string;
procedure WriteString(const cSection, cIdent, cValue: String);
function ReadInteger(const cSection, cIdent: string; const cDefault: Longint): Longint;
procedure WriteInteger(const cSection, cIdent: string; const cValue: Longint);
function ReadBool(const cSection, cIdent: string; const cDefault: Boolean): Boolean;
procedure WriteBool(const cSection, cIdent: string; const cValue: Boolean);
//
procedure ReadSection(const cSection: string; vStrings: TStrings);
procedure EraseSection(const cSection: string);
//Unicode TUnicodeIniFile For Delphi XE By X.S.F EMail: X-S-F@Live.Com;

 

unit u_UnicodeIniFiles;

 

 

inte不合格字符ce

 

uses Windows, SysUtils, Classes;

 

type

TUnicodeIniFile = class(TObject)

private

IniFileName : string;

Encoding : TEncoding;

public

//

constructor Create(const cIniFileName: string);

destructor Destroy; override;

//

function ReadString(const cSection, cIdent, cDefault: string): string;

procedure WriteString(const cSection, cIdent, cValue: String);

function ReadInteger(const cSection, cIdent: string; const cDefault: Longint): Longint;

procedure WriteInteger(const cSection, cIdent: string; const cValue: Longint);

function ReadBool(const cSection, cIdent: string; const cDefault: Boolean): Boolean;

procedure WriteBool(const cSection, cIdent: string; const cValue: Boolean);

//

procedure ReadSection(const cSection: string; vStrings: TStrings);

procedure EraseSection(const cSection: string);

end;

 

 

implementation

 

function IsSectionFormat(const cSectionFormatStr: string): Boolean;

var

vLen : Integer;

begin

vLen := Length(cSectionFormatStr);

Result := (vLen >= 3{Length('[x]')}) and

(cSectionFormatStr[1] = '[') and

(cSectionFormatStr[vLen] = ']') ;

end;

 

function IsSection(const cSection, cSectionFormatStr: string): Boolean;

var

vLen : Integer;

begin //查询Section

vLen := Length(cSectionFormatStr);

Result := (Length(cSection) = vLen - 2{减去[]}) and

(vLen >= 3{Length('[x]')}) and

(cSectionFormatStr[1] = '[') and

(cSectionFormatStr[vLen] = ']') and

(Pos(UpperCase(cSection), UpperCase(cSectionFormatStr)) = 2);

end;

 

function IsIdentFormat(const cIdentFormatStr: string): Boolean;

var

vLen : Integer;

begin

vLen := Length(cIdentFormatStr);

Result := not IsSectionFormat(cIdentFormatStr) and

(vLen > 2{Length('x=')}) and

(Pos('=', cIdentFormatStr) >= 2) ;

end;

 

//判断是否为Ident是的话且获取对应的value

function IsIdent_Read(const cIdent, cIdentFormatStr: string; var vValue: string): Boolean;

var

vLen : Integer;

vPos : Integer;

begin

vLen := Length(cIdentFormatStr);

vPos := Pos('=', cIdentFormatStr);

if (vLen > 2{Length('x=')}) and

(vPos >= 2) and

(UpperCase(Copy(cIdentFormatStr, 1, vPos - 1)) = UpperCase(cIdent))

then

begin

Result := True;

vValue := Copy(cIdentFormatStr, vPos + 1, vLen - vPos);

end;

end;

 

//判断是否为Ident是的话且写入对应的value

function IsIdent_Write(const cIdent: string; var vIdentFormatStr: string; const cValue: string): Boolean;

var

vLen : Integer;

vPos : Integer;

begin

vLen := Length(vIdentFormatStr);

vPos := Pos('=', vIdentFormatStr);

if (vLen > 2{Length('x=')}) and

(vPos >= 2) and

(UpperCase(Copy(vIdentFormatStr, 1, vPos - 1)) = UpperCase(cIdent))

then

begin

Result := True;

vIdentFormatStr := Copy(vIdentFormatStr, 1, vPos) + cValue;

end;

end;

 

function CharToBool(const cChar: Char): Boolean;

begin

Result := cChar <> '0';

end;

 

function BoolToChar(const cBool: Boolean): Char;

begin

if cBool then

Result := '1'

else

Result := '0';

end;

 

 

{TUnicodeIniFile}

 

constructor TUnicodeIniFile.Create(const cIniFileName: string);

begin

Self.Encoding := TEncoding.Create;

Self.IniFileName := cIniFileName;

end;

 

destructor TUnicodeIniFile.Destroy;

begin

Self.Encoding.Free;

end;

 

function TUnicodeIniFile.ReadString(const cSection, cIdent, cDefault: string): string;

var

vStrList: TStringList;

vIndex: DWORD;

vFindIdent : Boolean;

begin

if FileExists(Self.IniFileName) then

begin

vStrList:= TStringList.Create;

try

vFindIdent:= False;

 

vStrList.LoadFromFile(Self.IniFileName, Self.Encoding.Unicode);

 

if vStrList.Count > 0 then

for vIndex := 0 to vStrList.Count - 1 do

if not vFindIdent then

vFindIdent:= IsSection(cSection, vStrList[vIndex])

else

if IsIdentFormat(vStrList[vIndex]) then

begin

if IsIdent_Read(cIdent, vStrList[vIndex], Result) then

Exit;

end

else

Break;

finally

vStrList.Free;

end;

end;

//

Result := cDefault;

end;

 

procedure TUnicodeIniFile.WriteString(const cSection, cIdent, cValue: String);

var

vStrList: TStringList;

vIndex: DWORD;

vFindIdent : Boolean;

vStr: string;

begin

vStrList:= TStringList.Create;

try

if FileExists(Self.IniFileName) then

BEGIN

vFindIdent:= False;

 

vStrList.LoadFromFile(Self.IniFileName, Self.Encoding.Unicode);

 

if vStrList.Count > 0 then

for vIndex := 0 to vStrList.Count - 1 do

if not vFindIdent then

vFindIdent:= IsSection(cSection, vStrList[vIndex])

else

if IsIdentFormat(vStrList[vIndex]) then

begin

vStr := vStrList[vIndex];

if IsIdent_Write(cIdent, vStr, cValue) then

begin

vStrList[vIndex]:= vStr;

Break;

end

else

if vStrList.Count - 1 = vIndex then

begin

vStrList.Insert(vIndex + 1, cIdent + '=' + cValue);

Break;

end;

end

else

begin

vStrList.Insert(vIndex, cIdent + '=' + cValue);

Break;

end;

if not vFindIdent then

BEGIN

//自己添加节点

vStrList.Add('[' + cSection + ']') ;

vStrList.Add(cIdent + '=' + cValue) ;

END;

END

else

BEGIN

//自己添加节点

vStrList.Add('[' + cSection + ']') ;

vStrList.Add(cIdent + '=' + cValue) ;

END;

 

vStrList.SaveToFile(Self.IniFileName, Self.Encoding.Unicode);

 

finally

vStrList.Free;

end;

end;

 

function TUnicodeIniFile.ReadInteger(const cSection, cIdent: string; const cDefault: Longint): Longint;

var

vValue: string;

begin

vValue:= IntToStr(cDefault);

vValue:= Self.ReadString(cSection, cIdent, vValue) ;

Result := StrToInt(vValue) ;

end;

 

procedure TUnicodeIniFile.WriteInteger(const cSection, cIdent: string; const cValue: Longint);

var

vValue: string;

begin

Self.WriteString(cSection, cIdent, IntToStr(cValue)) ;

end;

 

function TUnicodeIniFile.ReadBool(const cSection, cIdent: string; const cDefault: Boolean): Boolean;

var

vValue: string;

begin

vValue:= BoolToChar(cDefault) ;

vValue:= Self.ReadString(cSection, cIdent, vValue) ;

Result := CharToBool(vValue[1])

end;

 

procedure TUnicodeIniFile.WriteBool(const cSection, cIdent: string; const cValue: Boolean);

begin

Self.WriteString(cSection, cIdent, BoolToChar(cValue));

end;

 

//

 

procedure TUnicodeIniFile.ReadSection(const cSection: string; vStrings: TStrings);

var

vStrList: TStringList;

vIndex: DWORD;

vFindIdent : Boolean;

begin

if FileExists(Self.IniFileName) then

begin

vStrList:= TStringList.Create;

try

if vStrings.Count > 0 then vStrings.Clear;

 

vFindIdent:= False;

 

vStrList.LoadFromFile(Self.IniFileName, Self.Encoding.Unicode);

 

if vStrList.Count > 0 then

for vIndex := 0 to vStrList.Count - 1 do

if not vFindIdent then

vFindIdent:= IsSection(cSection, vStrList[vIndex])

else

if IsIdentFormat(vStrList[vIndex]) then

vStrings.Add(Copy(vStrList[vIndex], 1, Pos('=', vStrList[vIndex]) - 1))

else

Break;

 

finally

vStrList.Free;

end;

end;

end;

 

procedure TUnicodeIniFile.EraseSection(const cSection: string);

var

vStrList: TStringList;

vIndex: DWORD;

vFindIdent : Boolean;

vBeginIndex, vEndIndex: Integer;

begin

if FileExists(Self.IniFileName) then

begin

vStrList:= TStringList.Create;

try

vFindIdent:= False;

 

vStrList.LoadFromFile(Self.IniFileName, Self.Encoding.Unicode);

 

if vStrList.Count > 0 then

begin

vBeginIndex:= -1;

 

for vIndex := 0 to vStrList.Count - 1 do

if not vFindIdent then

begin

vFindIdent:= IsSection(cSection, vStrList[vIndex]) ;

if vFindIdent then

begin

vBeginIndex:= vIndex;

vEndIndex := vIndex;

end;

end

else

if IsIdentFormat(vStrList[vIndex]) then

vEndIndex:= vIndex

else

Break;

 

if vBeginIndex <> -1 then

for vIndex := vEndIndex downto vBeginIndex do

vStrList.Delete(vIndex);

 

vStrList.SaveToFile(Self.IniFileName, Self.Encoding.Unicode);

 

end;

 

finally

vStrList.Free;

end;

end;

end;

 

end.

posted @ 2012-08-30 13:27  马儿快跑  阅读(2202)  评论(1编辑  收藏  举报