unit ObjLstFiler;
interface
uses
Contnrs;
procedure SaveList( AList : TObjectList;
const AFileName :
string; Delimiter : Char );
procedure LoadList( AList : TObjectList; AItemClass : TClass;
const AFileName :
string; Delimiter : Char );
function ObjToStr( AInstance : TObject; Delimiter : Char ) :
string;
procedure ObjFromStr( AInstance : TObject;
const AStr :
string; Delimiter : Char );
implementation
uses
TypInfo, Classes;
function ObjToStr( AInstance : TObject; Delimiter : Char ) :
string;
var
LPropName, LPropValue :
string;
LPropInfo : PPropInfo;
LPropCount : Integer;
LPropList : PPropList;
LPropType : PPTypeInfo;
LIdx : Integer;
LTemp : TStrings;
begin
LPropCount := GetPropList( AInstance, LPropList );
LTemp := TStringList.Create;
try
LTemp.Delimiter := Delimiter;
for LIdx := 0
to Pred( LPropCount )
do
begin
LPropInfo := LPropList^[LIdx];
LPropType := LPropInfo^.PropType;
LPropName :=
string( LPropInfo.
Name );
case LPropType^.Kind
of
tkInteger, tkChar, tkEnumeration, tkFloat, tkString, tkSet, tkWChar, tkLString, tkWString, tkInt64
{$IFDEF UNICODE}, tkUString
{$ENDIF} :
begin
// ReadOnly-Property
if ( LPropInfo.SetProc =
nil )
then
Continue;
LPropValue := GetPropValue( AInstance, LPropName );
LTemp.Add( LPropValue );
end;
end;
end;
Result := LTemp.DelimitedText;
finally
LTemp.Free;
end;
end;
procedure ObjFromStr( AInstance : TObject;
const AStr :
string; Delimiter : Char );
var
LPropName, LPropValue :
string;
LPropInfo : PPropInfo;
LPropCount : Integer;
LPropList : PPropList;
LPropType : PPTypeInfo;
LIdx : Integer;
LTemp : TStringList;
LTempIdx : Integer;
begin
LPropCount := GetPropList( AInstance, LPropList );
LTemp := TStringList.Create;
try
LTemp.Delimiter := Delimiter;
LTemp.DelimitedText := AStr;
LTempIdx := 0;
for LIdx := 0
to Pred( LPropCount )
do
begin
LPropInfo := LPropList^[LIdx];
LPropType := LPropInfo^.PropType;
LPropName :=
string( LPropInfo.
Name );
case LPropType^.Kind
of
tkInteger, tkChar, tkEnumeration, tkFloat, tkString, tkSet, tkWChar, tkLString, tkWString, tkInt64
{$IFDEF UNICODE}, tkUString
{$ENDIF} :
begin
// ReadOnly-Property
if ( LPropInfo.SetProc =
nil )
then
Continue;
LPropValue := LTemp[LTempIdx];
Inc( LTempIdx );
SetPropValue( AInstance, LPropName, LPropValue );
end;
end;
end;
finally
LTemp.Free;
end;
end;
procedure SaveList( AList : TObjectList;
const AFileName :
string; Delimiter : Char );
var
LData : TStrings;
LIdx : Integer;
begin
LData := TStringList.Create;
try
for LIdx := 0
to Pred( AList.Count )
do
LData.Add( ObjToStr( AList[LIdx], Delimiter ) );
LData.SaveToFile( AFileName );
finally
LData.Free;
end;
end;
procedure LoadList( AList : TObjectList; AItemClass : TClass;
const AFileName :
string; Delimiter : Char );
var
LData : TStrings;
LIdx : Integer;
LObj : TObject;
begin
AList.Clear;
LData := TStringList.Create;
try
LData.LoadFromFile( AFileName );
for LIdx := 0
to Pred( LData.Count )
do
begin
LObj := AItemClass.Create;
try
ObjFromStr( LObj, LData[LIdx], Delimiter );
AList.Add( LObj );
LObj :=
nil;
finally
LObj.Free;
end;
end;
finally
LData.Free;
end;
end;
end.