📄 inffiles.~pas
字号:
{ *************************************************************************** }
{ }
{ Delphi and Kylix Cross-Platform Visual Component Library }
{ }
{ Copyright (c) 1995, 2001 Borland Software Corporation }
{ }
{ *************************************************************************** }
unit IniFiles;
{$R-,T-,H+,X+}
interface
uses SysUtils, Classes;
type
EIniFileException = class(Exception);
TCustomIniFile = class(TObject)
private
FFileName: string;
public
constructor Create(const FileName: string);
function SectionExists(const Section: string): Boolean;
function ReadString(const Section, Ident, Default: string): string; virtual; abstract;
procedure WriteString(const Section, Ident, Value: String); virtual; abstract;
function ReadInteger(const Section, Ident: string; Default: Longint): Longint; virtual;
procedure WriteInteger(const Section, Ident: string; Value: Longint); virtual;
function ReadBool(const Section, Ident: string; Default: Boolean): Boolean; virtual;
procedure WriteBool(const Section, Ident: string; Value: Boolean); virtual;
function ReadBinaryStream(const Section, Name: string; Value: TStream): Integer; virtual;
function ReadDate(const Section, Name: string; Default: TDateTime): TDateTime; virtual;
function ReadDateTime(const Section, Name: string; Default: TDateTime): TDateTime; virtual;
function ReadFloat(const Section, Name: string; Default: Double): Double; virtual;
function ReadTime(const Section, Name: string; Default: TDateTime): TDateTime; virtual;
procedure WriteBinaryStream(const Section, Name: string; Value: TStream); virtual;
procedure WriteDate(const Section, Name: string; Value: TDateTime); virtual;
procedure WriteDateTime(const Section, Name: string; Value: TDateTime); virtual;
procedure WriteFloat(const Section, Name: string; Value: Double); virtual;
procedure WriteTime(const Section, Name: string; Value: TDateTime); virtual;
procedure ReadSection(const Section: string; Strings: TStrings); virtual; abstract;
procedure ReadSections(Strings: TStrings); virtual; abstract;
procedure ReadSectionValues(const Section: string; Strings: TStrings); virtual; abstract;
procedure EraseSection(const Section: string); virtual; abstract;
procedure DeleteKey(const Section, Ident: String); virtual; abstract;
procedure UpdateFile; virtual; abstract;
function ValueExists(const Section, Ident: string): Boolean;
property FileName: string read FFileName;
end;
{ TStringHash - used internally by TMemIniFile to optimize searches. }
PPHashItem = ^PHashItem;
PHashItem = ^THashItem;
THashItem = record
Next: PHashItem;
Key: string;
Value: Integer;
end;
TStringHash = class
private
Buckets: array of PHashItem;
protected
function Find(const Key: string): PPHashItem;
function HashOf(const Key: string): Cardinal; virtual;
public
constructor Create(Size: Integer = 256);
destructor Destroy; override;
procedure Add(const Key: string; Value: Integer);
procedure Clear;
procedure Remove(const Key: string);
function Modify(const Key: string; Value: Integer): Boolean;
function ValueOf(const Key: string): Integer;
end;
{ THashedStringList - A TStringList that uses TStringHash to improve the
speed of Find }
THashedStringList = class(TStringList)
private
FValueHash: TStringHash;
FNameHash: TStringHash;
FValueHashValid: Boolean;
FNameHashValid: Boolean;
procedure UpdateValueHash;
procedure UpdateNameHash;
protected
procedure Changed; override;
public
destructor Destroy; override;
function IndexOf(const S: string): Integer; override;
function IndexOfName(const Name: string): Integer; override;
end;
{ TMemIniFile - loads and entire ini file into memory and allows all
operations to be performed on the memory image. The image can then
be written out to the disk file }
TMemIniFile = class(TCustomIniFile)
private
FSections: TStringList;
function AddSection(const Section: string): TStrings;
function GetCaseSensitive: Boolean;
procedure LoadValues;
procedure SetCaseSensitive(Value: Boolean);
public
constructor Create(const FileName: string);
destructor Destroy; override;
procedure Clear;
procedure DeleteKey(const Section, Ident: String); override;
procedure EraseSection(const Section: string); override;
procedure GetStrings(List: TStrings);
procedure ReadSection(const Section: string; Strings: TStrings); override;
procedure ReadSections(Strings: TStrings); override;
procedure ReadSectionValues(const Section: string; Strings: TStrings); override;
function ReadString(const Section, Ident, Default: string): string; override;
procedure Rename(const FileName: string; Reload: Boolean);
procedure SetStrings(List: TStrings);
procedure UpdateFile; override;
procedure WriteString(const Section, Ident, Value: String); override;
property CaseSensitive: Boolean read GetCaseSensitive write SetCaseSensitive;
end;
{$IFDEF MSWINDOWS}
{ TIniFile - Encapsulates the Windows INI file interface
(Get/SetPrivateProfileXXX functions) }
TIniFile = class(TCustomIniFile)
public
destructor Destroy; override;
function ReadString(const Section, Ident, Default: string): string; override;
procedure WriteString(const Section, Ident, Value: String); override;
procedure ReadSection(const Section: string; Strings: TStrings); override;
procedure ReadSections(Strings: TStrings); override;
procedure ReadSectionValues(const Section: string; Strings: TStrings); override;
procedure EraseSection(const Section: string); override;
procedure DeleteKey(const Section, Ident: String); override;
procedure UpdateFile; override;
end;
{$ELSE}
TIniFile = class(TMemIniFile);
{$ENDIF}
implementation
uses RTLConsts
{$IFDEF MSWINDOWS}
, Windows
{$ENDIF};
{ TCustomIniFile }
constructor TCustomIniFile.Create(const FileName: string);
begin
FFileName := FileName;
end;
function TCustomIniFile.SectionExists(const Section: string): Boolean;
var
S: TStrings;
begin
S := TStringList.Create;
try
ReadSection(Section, S);
Result := S.Count > 0;
finally
S.Free;
end;
end;
function TCustomIniFile.ReadInteger(const Section, Ident: string;
Default: Longint): Longint;
var
IntStr: string;
begin
IntStr := ReadString(Section, Ident, '');
if (Length(IntStr) > 2) and (IntStr[1] = '0') and
((IntStr[2] = 'X') or (IntStr[2] = 'x')) then
IntStr := '$' + Copy(IntStr, 3, Maxint);
Result := StrToIntDef(IntStr, Default);
end;
procedure TCustomIniFile.WriteInteger(const Section, Ident: string; Value: Longint);
begin
WriteString(Section, Ident, IntToStr(Value));
end;
function TCustomIniFile.ReadBool(const Section, Ident: string;
Default: Boolean): Boolean;
begin
Result := ReadInteger(Section, Ident, Ord(Default)) <> 0;
end;
function TCustomIniFile.ReadDate(const Section, Name: string; Default: TDateTime): TDateTime;
var
DateStr: string;
begin
DateStr := ReadString(Section, Name, '');
Result := Default;
if DateStr <> '' then
try
Result := StrToDate(DateStr);
except
on EConvertError do
else raise;
end;
end;
function TCustomIniFile.ReadDateTime(const Section, Name: string; Default: TDateTime): TDateTime;
var
DateStr: string;
begin
DateStr := ReadString(Section, Name, '');
Result := Default;
if DateStr <> '' then
try
Result := StrToDateTime(DateStr);
except
on EConvertError do
else raise;
end;
end;
function TCustomIniFile.ReadFloat(const Section, Name: string; Default: Double): Double;
var
FloatStr: string;
begin
FloatStr := ReadString(Section, Name, '');
Result := Default;
if FloatStr <> '' then
try
Result := StrToFloat(FloatStr);
except
on EConvertError do
else raise;
end;
end;
function TCustomIniFile.ReadTime(const Section, Name: string; Default: TDateTime): TDateTime;
var
TimeStr: string;
begin
TimeStr := ReadString(Section, Name, '');
Result := Default;
if TimeStr <> '' then
try
Result := StrToTime(TimeStr);
except
on EConvertError do
else raise;
end;
end;
procedure TCustomIniFile.WriteDate(const Section, Name: string; Value: TDateTime);
begin
WriteString(Section, Name, DateToStr(Value));
end;
procedure TCustomIniFile.WriteDateTime(const Section, Name: string; Value: TDateTime);
begin
WriteString(Section, Name, DateTimeToStr(Value));
end;
procedure TCustomIniFile.WriteFloat(const Section, Name: string; Value: Double);
begin
WriteString(Section, Name, FloatToStr(Value));
end;
procedure TCustomIniFile.WriteTime(const Section, Name: string; Value: TDateTime);
begin
WriteString(Section, Name, TimeToStr(Value));
end;
procedure TCustomIniFile.WriteBool(const Section, Ident: string; Value: Boolean);
const
Values: array[Boolean] of string = ('0', '1');
begin
WriteString(Section, Ident, Values[Value]);
end;
function TCustomIniFile.ValueExists(const Section, Ident: string): Boolean;
var
S: TStrings;
begin
S := TStringList.Create;
try
ReadSection(Section, S);
Result := S.IndexOf(Ident) > -1;
finally
S.Free;
end;
end;
function TCustomIniFile.ReadBinaryStream(const Section, Name: string;
Value: TStream): Integer;
var
Text: string;
Stream: TMemoryStream;
Pos: Integer;
begin
Text := ReadString(Section, Name, '');
if Text <> '' then
begin
if Value is TMemoryStream then
Stream := TMemoryStream(Value)
else Stream := TMemoryStream.Create;
try
Pos := Stream.Position;
Stream.SetSize(Stream.Size + Length(Text) div 2);
HexToBin(PChar(Text), PChar(Integer(Stream.Memory) + Stream.Position), Length(Text) div 2);
Stream.Position := Pos;
if Value <> Stream then Value.CopyFrom(Stream, Length(Text) div 2);
Result := Stream.Size - Pos;
finally
if Value <> Stream then Stream.Free;
end;
end else Result := 0;
end;
procedure TCustomIniFile.WriteBinaryStream(const Section, Name: string;
Value: TStream);
var
Text: string;
Stream: TMemoryStream;
begin
SetLength(Text, (Value.Size - Value.Position) * 2);
if Length(Text) > 0 then
begin
if Value is TMemoryStream then
Stream := TMemoryStream(Value)
else Stream := TMemoryStream.Create;
try
if Stream <> Value then
begin
Stream.CopyFrom(Value, Value.Size - Value.Position);
Stream.Position := 0;
end;
BinToHex(PChar(Integer(Stream.Memory) + Stream.Position), PChar(Text),
Stream.Size - Stream.Position);
finally
if Value <> Stream then Stream.Free;
end;
end;
WriteString(Section, Name, Text);
end;
{ TStringHash }
procedure TStringHash.Add(const Key: string; Value: Integer);
var
Hash: Integer;
Bucket: PHashItem;
begin
Hash := HashOf(Key) mod Cardinal(Length(Buckets));
New(Bucket);
Bucket^.Key := Key;
Bucket^.Value := Value;
Bucket^.Next := Buckets[Hash];
Buckets[Hash] := Bucket;
end;
procedure TStringHash.Clear;
var
I: Integer;
P, N: PHashItem;
begin
for I := 0 to Length(Buckets) - 1 do
begin
P := Buckets[I];
while P <> nil do
begin
N := P^.Next;
Dispose(P);
P := N;
end;
Buckets[I] := nil;
end;
end;
constructor TStringHash.Create(Size: Integer);
begin
inherited Create;
SetLength(Buckets, Size);
end;
destructor TStringHash.Destroy;
begin
Clear;
inherited;
end;
function TStringHash.Find(const Key: string): PPHashItem;
var
Hash: Integer;
begin
Hash := HashOf(Key) mod Cardinal(Length(Buckets));
Result := @Buckets[Hash];
while Result^ <> nil do
begin
if Result^.Key = Key then
Exit
else
Result := @Result^.Next;
end;
end;
function TStringHash.HashOf(const Key: string): Cardinal;
var
I: Integer;
begin
Result := 0;
for I := 1 to Length(Key) do
Result := ((Result shl 2) or (Result shr (SizeOf(Result) * 8 - 2))) xor
Ord(Key[I]);
end;
function TStringHash.Modify(const Key: string; Value: Integer): Boolean;
var
P: PHashItem;
begin
P := Find(Key)^;
if P <> nil then
begin
Result := True;
P^.Value := Value;
end
else
Result := False;
end;
procedure TStringHash.Remove(const Key: string);
var
P: PHashItem;
Prev: PPHashItem;
begin
Prev := Find(Key);
P := Prev^;
if P <> nil then
begin
Prev^ := P^.Next;
Dispose(P);
end;
end;
function TStringHash.ValueOf(const Key: string): Integer;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -