在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
网络摘抄 //刚封装的ini文件类。保存为unicode的。解决delphi xe的TiniFile保存后不为unicode的问题
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. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论