unit FilenameBuilder;
interface
uses
Classes,
SysUtils;
type
TMP3Meta =
record
Interpret :
string;
Titel :
string;
Album :
string;
CdNr : Integer;
TrackNr : Integer;
end;
IFilenamePart =
interface
['
{281FA130-B641-454D-8E9A-58D0C02F4C14}']
function Build(
const AData : TMP3Meta ) :
string;
end;
TFilenameBuilder =
class
private
FParts : TInterfaceList;
public
constructor Create;
destructor Destroy;
override;
procedure AddPart( APart : IFilenamePart );
procedure Clear;
function GetFilename(
const AData : TMP3Meta ) :
string;
end;
type
TFilenamePart =
class( TInterfacedObject, IFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
virtual;
abstract;
end;
TInterpretPart =
class( TFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
override;
end;
TTitelPart =
class( TFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
override;
end;
TAlbumPart =
class( TFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
override;
end;
TCdNrPart =
class( TFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
override;
end;
TTrackNrPart =
class( TFilenamePart )
protected
function Build(
const AData : TMP3Meta ) :
string;
override;
end;
implementation
{ TFilenameBuilder }
procedure TFilenameBuilder.AddPart( APart : IFilenamePart );
begin
FParts.Add( APart );
end;
procedure TFilenameBuilder.Clear;
begin
FParts.Clear;
end;
constructor TFilenameBuilder.Create;
begin
inherited Create;
FParts := TInterfaceList.Create;
end;
destructor TFilenameBuilder.Destroy;
begin
FParts.Free;
inherited;
end;
function TFilenameBuilder.GetFilename(
const AData : TMP3Meta ) :
string;
var
LBuilder : TStringBuilder;
LIdx : Integer;
begin
LBuilder := TStringBuilder.Create;
try
for LIdx := 0
to FParts.Count - 1
do
begin
if LIdx > 0
then
LBuilder.Append( '
-' );
LBuilder.Append( ( FParts[LIdx]
as IFilenamePart ).Build( AData ) );
end;
Result := LBuilder.ToString;
finally
LBuilder.Free;
end;
end;
{ TInterpretPart }
function TInterpretPart.Build(
const AData : TMP3Meta ) :
string;
begin
Result := AData.Interpret;
end;
{ TTitelPart }
function TTitelPart.Build(
const AData : TMP3Meta ) :
string;
begin
Result := AData.Titel;
end;
{ TAlbumPart }
function TAlbumPart.Build(
const AData : TMP3Meta ) :
string;
begin
Result := AData.Album;
end;
{ TCdNrPart }
function TCdNrPart.Build(
const AData : TMP3Meta ) :
string;
begin
Result := IntToStr( AData.CdNr );
end;
{ TTrackNrPart }
function TTrackNrPart.Build(
const AData : TMP3Meta ) :
string;
begin
Result := IntToStr( AData.TrackNr );
end;
end.