unit classIni;
interface
uses
SysUtils, IniFiles;
type
TMemIniFile =
class(IniFiles.TMemIniFile)
private
FModified, FReadOnlyMode: Boolean;
function IsNumeric(
const aString:
string;
const bAcceptNegativeNumbers: Boolean = True): Boolean;
procedure FlushModifications;
public
constructor Create(
const FileName:
string;
const ReadOnlyMode: Boolean = False);
overload;
constructor Create(
const FileName:
string;
const Encoding: TEncoding;
const ReadOnlyMode: Boolean = False);
overload;
destructor Destroy;
{* reintroduce; *} override;
procedure UpdateFile;
override;
procedure WriteString(
const Section, Ident, Value:
string);
reintroduce;
overload;
procedure WriteInteger(
const Section, Ident:
string; Value: Integer);
reintroduce;
overload;
procedure WriteBool(
const Section, Ident:
string; Value: Boolean);
reintroduce;
overload;
function ReadInt64(
const Section, Ident:
string;
Default: Int64): Int64;
procedure WriteInt64(
const Section:
string;
const Ident:
string; Value: Int64);
end;
implementation
// Helper
// ==============================================================================================================================================
function TMemIniFile.IsNumeric(
const aString:
string;
const bAcceptNegativeNumbers: Boolean = True): Boolean;
var
bRes: Boolean;
begin
bRes := StrToInt64Def(aString, 0) = StrToInt64Def(aString, 1);
if bRes
and (
not bAcceptNegativeNumbers)
and (StrToInt64(aString) < 0)
then
bRes := False;
Result := bRes;
end;
procedure TMemIniFile.FlushModifications;
begin
// Flush the modifications to disk
if (
not FReadOnlyMode)
and FModified
then
begin
inherited UpdateFile;
FModified := False;
end;
end;
// ==============================================================================================================================================
// Create / Update
// ==============================================================================================================================================
constructor TMemIniFile.Create(
const FileName:
string;
const ReadOnlyMode: Boolean = False);
begin
inherited Create(FileName);
FReadOnlyMode := ReadOnlyMode;
FModified := False;
end;
constructor TMemIniFile.Create(
const FileName:
string;
const Encoding: TEncoding;
const ReadOnlyMode: Boolean = False);
begin
inherited Create(FileName, Encoding);
FReadOnlyMode := ReadOnlyMode;
FModified := False;
end;
destructor TMemIniFile.Destroy;
begin
// If not already flushed to disk, flush modifications now
FlushModifications;
inherited Destroy;
end;
procedure TMemIniFile.UpdateFile;
begin
// Flush the modifications to disk
FlushModifications;
end;
// ==============================================================================================================================================
// Writes / Reads
// ==============================================================================================================================================
procedure TMemIniFile.WriteString(
const Section, Ident, Value:
string);
begin
if not FReadOnlyMode
then
begin
inherited WriteString(Section, Ident, Value);
FModified := True;
end;
end;
procedure TMemIniFile.WriteInteger(
const Section, Ident:
string; Value: Integer);
begin
WriteString(Section, Ident, IntToStr(Value));
end;
procedure TMemIniFile.WriteBool(
const Section, Ident:
string; Value: Boolean);
const
Values:
array [Boolean]
of string = ('
0', '
1');
begin
WriteString(Section, Ident, Values[Value]);
end;
function TMemIniFile.ReadInt64(
const Section:
string;
const Ident:
string;
Default: Int64): Int64;
var
sTmp:
string;
begin
sTmp := ReadString(Section, Ident, SysUtils.IntToStr(
Default));
if IsNumeric(sTmp, True)
then
Result := StrToInt64(sTmp)
else
Result :=
Default;
end;
procedure TMemIniFile.WriteInt64(
const Section:
string;
const Ident:
string; Value: Int64);
begin
WriteString(Section, Ident, SysUtils.IntToStr(Value));
end;
// ==============================================================================================================================================
end.