📄 adinidb.pas
字号:
F := UpperCase(S);
{see if this field can, in fact, be indexed}
for I := 0 to Pred(FieldList.Count) do begin
Fld := TDBFieldInfo(FieldList.Items[I]);
if (UpperCase(Fld.Name) = F) and Fld.IsStr and (Fld.Len <= MaxIndexLen) then begin
FIndexedField := S;
{reopen the database, if necessary}
if Open then
Open := True;
Exit;
end;
end;
{raise an exception if the field name passed for the index was bad}
raise EBadFieldForIndex.Create(ecBadFieldForIndex, False);
end;
procedure TApdCustomIniDBase.SetSortedIndex(const NewSorted : Boolean);
{-Set the Sorted property of the record list}
begin
if (NewSorted = FSortedIndex) then
Exit;
FSortedIndex := NewSorted;
if csDesigning in ComponentState then
Exit;
FRecordList.Sorted := FSortedIndex;
Changed := True;
end;
function TApdCustomIniDBase.GetRecordList : TStrings;
{-Returns the key strings for each record in the database}
var
IndexRec : PChar;
Temp : PChar;
BufSize : Integer;
begin
AssureOpen;
if Changed then begin
FRecordList.Clear;
if (NumRecs > 0) then begin
{allocate memory for the index}
CheckException(Self, iAllocIniIndexRec(DB, IndexRec, BufSize));
{load the buffer with the strings}
CheckException(Self, iLoadIniIndex(DB, IndexRec, BufSize));
{put the strings into the list}
Temp := IndexRec;
while (Temp^ <> #0) do begin
FRecordList.Add(StrPas(Temp));
Inc(Temp, StrLen(Temp) + 1);
end;
{deallocate the index}
iDeallocIniIndexRec(DB, IndexRec, BufSize);
end;
Changed := False;
end;
Result := FRecordList;
end;
function TApdCustomIniDBase.GetNumRecs : Integer;
{-Returns the number of records in the database}
begin
AssureOpen;
Result := iNumIniRecs(DB);
end;
procedure TApdCustomIniDBase.AssureOpen;
{-Make sure that the database is open, otherwise raise an exception}
begin
if not Open then
Open := True;
end;
procedure TApdCustomIniDBase.ClearFieldList;
{-Remove all fields in the field list}
var
I : Word;
begin
if (FieldList.Count > 0) then begin
for I := 0 to Pred(FieldList.Count) do
TDBFieldInfo(FieldList.Items[I]).Free;
FieldList.Clear;
end;
end;
procedure TApdCustomIniDBase.DefaultIndexed;
{-Default the FIndexedField property to the first indexable field in the list}
var
I : Word;
begin
for I := 0 to Pred(FieldList.Count) do
with TDBFieldInfo(FieldList.Items[I]) do
if IsStr then begin
IndexedField := Name;
Break;
end;
end;
procedure TApdCustomIniDBase.ReadFields(Reader : TReader);
{-Reads the database field list from a stream}
var
Fld : TDBFieldInfo;
begin
{remove all existing fields, if any}
ClearFieldList;
Reader.ReadListBegin;
while not Reader.EndOfList do begin
Fld := TDBFieldInfo.Create;
Fld.Len := Reader.ReadInteger;
Fld.IsStr := Reader.ReadBoolean;
Fld.Name := Reader.ReadString;
FieldList.Add(Fld);
end;
Reader.ReadListEnd;
end;
procedure TApdCustomIniDBase.WriteFields(Writer : TWriter);
{-Writes the database field list to a stream}
var
I : Integer;
Fld : TDBFieldInfo;
begin
Writer.WriteListBegin;
for I := 0 to Pred(FieldList.Count) do begin
Fld := FieldList.Items[I];
Writer.WriteInteger(Fld.Len);
Writer.WriteBoolean(Fld.IsStr);
Writer.WriteString(Fld.Name);
end;
Writer.WriteListEnd;
end;
procedure TApdCustomIniDBase.DefineProperties(Filer : TFiler);
{-Define methods for reading and writing field list}
begin
inherited DefineProperties(Filer);
if not CustComponent then
Filer.DefineProperty('FakeProperty', ReadFields, WriteFields, True);
end;
constructor TApdCustomIniDBase.Create(AOwner : TComponent);
{-Create an INI database}
var
DefFld : TDBFieldInfo;
begin
CustComponent := False;
inherited Create(AOwner);
DB := nil;
Scratch := nil;
Changed := True;
FOpen := False;
FSortedIndex := DefSortedIndex;
FRecordList := nil;
FFileName := DefDBName;
FFieldList := TDBFieldList.Create;
if not (csDesigning in ComponentState) then
{create the object for returning record names}
FRecordList := TStringList.Create;
{create default values}
FIndexedField := 'Default';
{create a default field}
DefFld := TDBFieldInfo.CreateString('Default', 20);
FieldList.Add(DefFld);
end;
destructor TApdCustomIniDBase.Destroy;
{-Destroy an INI database}
begin
Open := False;
if Assigned(Scratch) then
FreeMem(Scratch, DB^.RecordSize);
if Assigned(FRecordList) then
FRecordList.Free;
ClearFieldList;
FieldList.Free;
inherited Destroy;
end;
function TApdCustomIniDBase.KeyExists(const Key : TDBKeyStr) : Boolean;
{-Return TRUE if an entry with an index of 'Name' exists}
var
P : array[0..MaxIndexLen] of Char;
begin
AssureOpen;
StrPCopy(P, Key);
Result := iKeyExists(DB, P);
end;
procedure TApdCustomIniDBase.AddRecord(var Rec);
{-Add a record to the database}
begin
AssureOpen;
FieldList.StrsToPChars(Rec, Scratch^);
CheckException(Self, iAddIniRecord(DB, Scratch^));
Changed := True;
end;
procedure TApdCustomIniDBase.UpdRecord(const Key : TDBKeyStr; var Rec);
{-Update a record in the database}
var
Temp : array[0..MaxIndexLen] of Char;
begin
AssureOpen;
FieldList.StrsToPChars(Rec, Scratch^);
CheckException(Self, iUpdIniRecord(DB, StrPCopy(Temp, Key), Scratch^));
Changed := True;
end;
procedure TApdCustomIniDBase.DelRecord(const Key : TDBKeyStr);
{-Remove a record from the database}
var
Temp : array[0..MaxIndexLen] of Char;
begin
AssureOpen;
CheckException(Self, iDelIniRecord(DB, StrPCopy(Temp, Key)));
Changed := True;
end;
procedure TApdCustomIniDBase.GetRecord(const Key : TDBKeyStr; var Rec);
{-Get a record from the database}
var
Temp : array[0..MaxIndexLen] of Char;
begin
AssureOpen;
CheckException(Self, iGetIniRecord(DB, StrPCopy(Temp, Key), Scratch^));
FieldList.PCharsToStrs(Scratch^, Rec);
end;
procedure TApdCustomIniDBase.WriteToIni(var Rec; const Section, IniFile : String);
{-Write the record to a user-specified .INI file}
var
TempSec : array[0..255] of Char;
TempIni : array[0..255] of Char;
begin
AssureOpen;
FieldList.StrsToPChars(Rec, Scratch^);
CheckException(Self, iWriteToIni(DB, Scratch^, StrPCopy(TempSec, Section), StrPCopy(TempIni, IniFile)));
end;
procedure TApdCustomIniDBase.ReadFromIni(var Rec; const Section, IniFile : String);
{-Read the record from a user-specified .INI file}
var
TempSec : array[0..255] of Char;
TempIni : array[0..255] of Char;
begin
AssureOpen;
CheckException(Self, iReadFromIni(DB, Scratch^, StrPCopy(TempSec, Section), StrPCopy(TempIni, IniFile)));
FieldList.PCharsToStrs(Scratch^, Rec);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -