严黎斌
unit ID3Kernel;
interface
type
Tid3v1= record
Tag: array[0..2] of char;
//00..02 , ='TAG'
Title:array[0..$1d] of char;
//03..20
Artist:array[0..$1d] of char;
//21..3e
Album:array[0..$1d] of char;
//3f..5c
Year:array[0..3] of char;
//5d..60
Comment:array[0..$1c] of char;
//61..7d
Track:byte;
//7e
Genre:byte;
//7f
end;
function ReadID3v1(strFile:string;var pid3v1:Tid3v1):integer;
function WriteID3v1(strFile:string;var pid3v1:Tid3v1):integer;
function DeleteID3v1(strFile:string):integer;
implementation
function ReadID3v1(strFile:string;var pid3v1:Tid3v1):integer;
var
f1:file of byte;
bytAll: array [0..$7f] of byte;
i: integer;
begin
result:=1;
if strFile='' then
exit;
AssignFile(f1,strFile);
FileMode:=0;
Reset(f1);
if FileSize(f1)<=$80 then
exit;
Seek(f1, FileSize(f1)-$80);
for i:=0 to $7fdo
Read(f1,bytAll);
if (bytAll[0]<>ord('T')) and (bytAll[1]<>ord('A'))
and (bytAll[2]<>ord('G')) then
exit;
// no 'TAG' found
Move(bytAll,pid3v1,$80);
CloseFile(f1);
result:=0;
end;
function WriteID3v1(strFile:string;var pid3v1:Tid3v1):integer;
var
f1:file of byte;
bytAll: array [0..$7f] of byte;
i: integer;
begin
result:=1;
AssignFile(f1,strFile);
FileMode:=2;
Reset(f1);
if FileSize(f1)<=$80 then
exit;
Seek(f1, FileSize(f1)-$80);
for i:=0 to $2do
Read(f1,bytAll);
// test if 'TAG' exists
if (bytAll[0]=ord('T')) and (bytAll[1]=ord('A'))
and (bytAll[2]=ord('G'))
then
Seek(f1,FileSize(f1)-$80)
else
Seek(f1,FileSize(f1));
Move(pid3v1,bytAll,$80);
for i:=0 to $7fdo
Write(f1,bytAll);
CloseFile(f1);
result:=0;
end;
function DeleteID3v1(strFile:string):integer;
var
f1:file of byte;
bytAll: array [0..$7f] of byte;
i: integer;
begin
Result:=1;
AssignFile(f1,strFile);
FileMode:=2;
Reset(f1);
if FileSize(f1)<=$80 then
exit;
Seek(f1, FileSize(f1)-$80);
for i:=0 to $2do
Read(f1,bytAll);
// test if 'TAG' exists
if (bytAll[0]=ord('T')) and (bytAll[1]=ord('A'))
and (bytAll[2]=ord('G'))
then
begin
Seek(f1,FileSize(f1)-$80);
Truncate(f1)
end;
CloseFile(f1);
Result:=0;
end;
end.