Index » Empathy Jukebox : Blob aaa72d / albuminfo.pas
unit albuminfo;


interface
uses windows,graphics,sysutils,classes,jpeg,main,aboutform,
{$IFNDEF kiosk}
dialogs,
{$ENDIF}


global;

const
filename = 'info.dat';
fileheader = 'EMPATHYINFOFILEFORMAT';

  type tinfo = record
  genre : string;
  artist_type : string;
  year : integer;
  decade : integer;
  description : string;
  recordlabel : string;
  compilation : boolean;
  keywords : string;
  front : tbitmap;
  rear : tbitmap;
  inside : tbitmap;
  additional : tbitmap;
  notes : string;
  catno: string;
  urlartist : string;
  urlalbum : string;
  urladditional : string;
  end;

type tfilterbytearray = array of byte;




var
infos : array of tinfo;
infoscount : integer;
recordlabels : array of string;
recordlabelscount : integer;
filter : tfilter;

procedure dofilter(filter : tfilter);
procedure checkandaddrecordlabels(rl : string);
function info_saverecord(dirpath : string;inf : tinfo) : integer;
function info_openrecord(dirpath : string;var inf : tinfo;loadpics : boolean) : integer;
procedure loadalbuminformation;
procedure loadoriginallist;
procedure extractkeywordstrings(inp : string;var destination : tfilterstrings);
function bytearraytofilter(ba : tfilterbytearray) : tfilter;
function tfiltertobytearray(filt : tfilter) : tfilterbytearray;


implementation

function bytearraytofilter(ba : tfilterbytearray) : tfilter;
var
n : integer;
currentpos : integer;
   procedure movebytearraytoint(var op : integer;pos : integer);
   var b1,b2,b3,b4 : byte;
   begin
    b1:=ba[pos];
    b2:=ba[pos+1];
    b3:=ba[pos+2];
    b4:=ba[pos+3];

    op:=b1;
    op:=op shl 8;
    op:=op or b2;
    op:=op shl 8;
    op:=op or b3;
    op:=op shl 8;
    op:=op or b4;
   end;


procedure readsizeandstring(var s : string);
var
sz : integer;
i : integer;
begin
movebytearraytoint(sz,currentpos);
currentpos:=currentpos+(sizeof(integer));
s:='';
for i:=1 to sz do begin
  s:=s+chr(ba[currentpos]);
  inc(currentpos);
end;
end;

begin
  movebytearraytoint(result.genre.count,0);
  movebytearraytoint(result.artist.count,4);
  movebytearraytoint(result.keywords.count,8);
  movebytearraytoint(result.recordlabel.count,12);
  movebytearraytoint(result.yearlower,16);
  movebytearraytoint(result.yearupper,20);
  movebytearraytoint(result.decadelower,24);
  movebytearraytoint(result.decadeupper,28);
  if ba[32]=1 then result.excludecompilations:=true else result.excludecompilations:=false;
  if ba[33]=1 then result.excludenoncompilations:=true else result.excludenoncompilations:=false;

  currentpos:=34;

 if result.genre.count>-1 then begin
  setlength(result.genre.data,result.genre.count);
   for n:=0 to result.genre.count-1 do begin
      readsizeandstring(result.genre.data[n]);
  end;
 end;

 if result.artist.count>-1 then begin
  setlength(result.artist.data,result.artist.count);
  for n:=0 to result.artist.count-1 do begin
      readsizeandstring(result.artist.data[n]);
  end;
 end;

 if result.keywords.count>-1 then begin
  setlength(result.keywords.data,result.keywords.count);
   for n:=0 to result.keywords.count-1 do begin
      readsizeandstring(result.keywords.data[n]);
  end;
 end;

 if result.recordlabel.count>-1 then begin
  setlength(result.recordlabel.data,result.recordlabel.count);
  for n:=0 to result.recordlabel.count-1 do begin
      readsizeandstring(result.recordlabel.data[n]);
  end;
 end;

end;


function tfiltertobytearray(filt : tfilter) : tfilterbytearray;
var
currentsize : integer;
currentpos : integer;
n : integer;

        procedure moveinttobytearray(s : integer;pos : integer);
        var
        a : byte;
        begin

        asm
         mov EAX,s;
         mov a,al;
        end;
         result[pos+3]:=a;

       asm
         mov EAX,s;
         SHR EAX,8;
         mov a,al;
        end;
         result[pos+2]:=a;

       asm
         mov EAX,s;
         SHR EAX,16;
         mov a,al;
        end;
         result[pos+1]:=a;

         asm
         mov EAX,s;
         SHR EAX,24;
         mov a,al;
        end;
         result[pos]:=a;

        end;

        procedure writesizeandstring(s : string);
         var
          i : integer;
          sz : integer;
         begin
          sz:=length(s);
          currentsize:=currentsize+(sizeof(integer)+sz);
          setlength(result,currentsize);
          moveinttobytearray(sz,currentpos);
          currentpos:=currentpos+sizeof(integer);
          for i:=1 to sz do begin
            result[currentpos]:=ord(s[i]);
            inc(currentpos);
          end;

         end;

begin
  currentsize:=(sizeof(integer)*8)+(sizeof(byte)*2);
  setlength(result,currentsize);

  currentpos:=0;
  moveinttobytearray(filt.genre.count,0);
  moveinttobytearray(filt.artist.count,4);
  moveinttobytearray(filt.keywords.count,8);
  moveinttobytearray(filt.recordlabel.count,12);
  moveinttobytearray(filt.yearlower,16);
  moveinttobytearray(filt.yearupper,20);
  moveinttobytearray(filt.decadelower,24);
  moveinttobytearray(filt.decadeupper,28);
  if (filt.excludecompilations=true) then result[32]:=1 else result[32]:=0;
  if (filt.excludenoncompilations=true) then result[33]:=1 else result[33]:=0;
  currentpos:=34;


  for n:=0 to filt.genre.count-1 do begin
      writesizeandstring(filt.genre.data[n]);
  end;
  for n:=0 to filt.artist.count-1 do begin
      writesizeandstring(filt.artist.data[n]);
  end;
    for n:=0 to filt.keywords.count-1 do begin
      writesizeandstring(filt.keywords.data[n]);
  end;
    for n:=0 to filt.recordlabel.count-1 do begin
      writesizeandstring(filt.recordlabel.data[n]);
  end;
end;

procedure extractkeywordstrings(inp : string;var destination : tfilterstrings);
var
n : integer;
wrd : string;
begin
wrd:='';
destination.count:=0;
for n:=1 to length(inp) do begin
    if (inp[n]<>' ') then wrd:=wrd+inp[n] else begin
      setlength(destination.data,destination.count+1);
      destination.data[destination.count]:=wrd;
      inc(destination.count);
      wrd:='';
    end;
end;
if wrd<>'' then begin
      setlength(destination.data,destination.count+1);
      destination.data[destination.count]:=wrd;
      inc(destination.count);
      end;
if destination.count=0 then destination.count:=-1;
end;


procedure loadoriginallist;
var
n : integer;
begin
setlength(albumref, albumrefonloadcount+1);
if albumrefonloadcount>0 then begin
for n:=0 to albumrefonloadcount do begin
albumref[n]:=albumrefonload[n];
end;
end;
albumcount:=albumrefonloadcount;
thumbsdone:=false;
end;

function comparelists(ls : tfilterstrings;rec : string) : boolean;
var
n : integer;
begin
result:=false;
if ls.count=0 then begin
   result:=true;
   exit;
end;
for n:=0 to ls.count-1 do begin
    if uppercase(ls.data[n])=uppercase(rec) then result:=true;
end;
end;

function comparetwolists(ls : tfilterstrings;rec : tfilterstrings) : boolean;
var
n : integer;
begin
result:=false;
if (ls.count=0) or (rec.count=0) then begin
   result:=true;
   exit;
end;

for n:=0 to ls.count-1 do begin
    if comparelists(rec,ls.data[n]) then result:=true;
end;
end;


procedure dofilter(filter : tfilter);
var
n : integer;
match : boolean;
newalbumlistcount : integer;
newalbumlist : albumtype;
keys : tfilterstrings;
begin
newalbumlistcount :=0;
for n:=1 to albumrefonloadcount do begin
    match:=true;
    if filter.genre.count<>-1 then begin
      if comparelists(filter.genre,infos[n].genre)<>true then match:=false;
    end;
    if filter.artist.count<>-1 then begin
      if comparelists(filter.artist,infos[n].artist_type)<>true then match:=false;
    end;
    if filter.keywords.count<>-1 then begin
      extractkeywordstrings(infos[n].keywords,keys);
      if comparetwolists(filter.keywords,keys)<>true then match:=false;
    end;
    if filter.recordlabel.count<>-1 then begin
      if comparelists(filter.recordlabel,infos[n].recordlabel)<>true then match:=false;
    end;
    if filter.yearlower<>-1 then begin
      if (infos[n].year<filter.yearlower) or (infos[n].year>filter.yearupper) then match:=false;
    end;
    if filter.decadelower<>-1 then begin
      if (infos[n].decade<filter.decadelower) or (infos[n].decade>filter.decadeupper) then match:=false;
    end;
   if filter.excludecompilations=true then begin
      if infos[n].compilation=true then match:=false;
   end;
   if filter.excludenoncompilations=true then begin
      if infos[n].compilation=false then match:=false;
   end;

   if match=true then begin
     inc(newalbumlistcount);
     setlength(newalbumlist,newalbumlistcount+1);
     newalbumlist[newalbumlistcount].album:=albumrefonload[n].album;
     newalbumlist[newalbumlistcount].artist:=albumrefonload[n].artist;
   end;

end;
albumcount:=0;
for n:=1 to newalbumlistcount do begin
     inc(albumcount);
     setlength(albumref,albumcount+1);
     albumref[n].album:=newalbumlist[n].album;
     albumref[n].artist:=newalbumlist[n].artist;
end;

end;

procedure checkandaddrecordlabels(rl : string);
var
n : integer;
found : boolean;
begin
found:=false;
for n:=0 to recordlabelscount do begin
    if uppercase(recordlabels[n])=uppercase(rl) then begin;
                                                     found:=true;
                                                     break;
                                                     end;
end;
if (found=false) and (trim(rl)<>'') then begin
   recordlabels[recordlabelscount]:=rl;
   inc(recordlabelscount);
   setlength(recordlabels,recordlabelscount+1);
   end;
end;

procedure loadalbuminformation;
var
n : integer;
path : string;
begin
recordlabelscount:=0;
setlength(recordlabels,recordlabelscount+1);
infoscount:=0;
if kiosk=true then exit;
initmessage('Loading Album Information....','','');
for n:=1 to albumcount do begin
    path:=config.path+albumref[n].path;
    inc(infoscount);
    setlength(infos,infoscount+1);
      if fileexists(path+'\'+filename) then begin
      info_openrecord(path,infos[n],false);
      checkandaddrecordlabels(infos[n].recordlabel);
      if about.visible=true then begin
      about.albs.caption:=inttostr(albumcount);
      about.albslabel.visible:=true;
      about.albs.visible:=true;
      end;
     end
    else begin
    with infos[n] do begin
      genre :='0';
      artist_type :='0';
      year :=0;
      decade :=0;
      description :='';
      recordlabel :='';
      compilation :=false;
      keywords := '';
      front.free;
      notes:='';
    end;
    end;
end;
     initmessage('Finished..','','');
end;



function info_saverecord(dirpath : string;inf : tinfo) : integer;
var
fn : string;
fl : tfilestream;
size : longint;
    function writesizeanddata(str : string) : integer;overload;
    begin
      result:=0;
      try
      size:=length(str);
      fl.write(size,sizeof(size));
      fl.write(pointer(str)^,length(str));
      except
      badfile(fn,'writing');
      result:=-1;
      end;
    end;

    function writesizeandjpeg(bm : tbitmap) : integer;overload;
    var
    tmfl : tmemorystream;
    pic : tjpegimage;
    begin
      result:=0;
      tmfl:=tmemorystream.create;
      pic:=tjpegimage.create;
      try
      pic.assign(bm);
      pic.JPEGneeded;
      pic.compress;
      pic.savetostream(tmfl);
      size:=tmfl.size;
      fl.write(size,sizeof(size));
      tmfl.savetostream(fl);
      bm.assign(pic);
      except
      result:=-1;
      badfile(fn,'writing');
      exit;
      deletefile(fn);
      pic.free;
      tmfl.free;
      end;
      pic.free;
      tmfl.free;
    end;


begin
result:=0;
fn:=dirpath+'\'+filename;
if fileexists(fn) then begin if deletefile(fn)=false then begin
  badfile(fn,'removing old file (write protected?) ');
  exit;
  end;
end;
try
fl:=tfilestream.create(fn,fmcreate);
fl.Write(fileheader,length(fileheader));
writesizeanddata(inf.genre);
writesizeanddata(inf.artist_type);
fl.write(inf.year,sizeof(inf.year));
fl.write(inf.decade,sizeof(inf.decade));
writesizeanddata(inf.description);
writesizeanddata(inf.recordlabel);
fl.write(byte(inf.compilation),1);
writesizeanddata(inf.keywords);
writesizeandjpeg(inf.rear);
writesizeandjpeg(inf.inside);
writesizeandjpeg(inf.additional);
writesizeanddata(inf.notes);
writesizeanddata(inf.catno);
writesizeanddata(inf.urlalbum);
writesizeanddata(inf.urlartist);
writesizeanddata(inf.urladditional);
fl.free;
except
badfile(fn,'writing');
result:=-1;
fl.free;
end;
end;

function info_openrecord(dirpath : string;var inf : tinfo;loadpics : boolean) : integer;
var
fn : string;
fl : tfilestream;
size : longint;
scrap : string;

    function readsizeanddata : string;overload;
    var
    count : integer;
    begin
      try
      count:=sizeof(size);
      fl.read(size,count);
      if (count<sizeof(size)) then begin; result:=''; exit; end;
      setlength(result,size);
      fl.read(pointer(result)^,size);
      except
      badfile(fn,'reading');
      result:='';
      exit;
      end;
    end;

    function readsizeandjpeg(var bm : tbitmap) : integer;overload;
    var
    pic : tjpegimage;
    mfl : tmemorystream;
    count : integer;
    begin
    result:=0;
    pic:=nil;
    mfl:=nil;
    try
      count:=sizeof(size);
      fl.read(size,count);
      if size=0 then  exit;
      if (count<sizeof(size)) then begin; result:=-1; exit; end;
      if loadpics = false then begin
      fl.seek(size,soFromCurrent);
      exit;
      end;
      try
      mfl:=tmemorystream.create;
      mfl.size:=size;
      mfl.CopyFrom(fl,size);
      mfl.seek(0,soFromBeginning);
      pic:=tjpegimage.create;
      pic.loadfromstream(mfl);
      except;
      badfile(fn,'reading');
      if pic<>nil then pic.free;
      if mfl<>nil then mfl.free;
      exit;
      end;
      bm.assign(pic);
      pic.free;
      mfl.free;
    except
    result:=-1;
    end;
    end;


begin
if loadpics=true then begin
inf.rear.assign(nil);
inf.inside.assign(nil);
inf.additional.assign(nil);
end;
result:=0;


if fileexists(dirpath+'\'+filename)=false then
 begin
   with inf do begin
     genre :='0';
     artist_type :='0';
     year :=0;
     decade :=0;
     description :='';
     recordlabel :='';
     compilation :=false;
     keywords :='';
     notes :='';
  end;
  result:=-1;
  exit;
 end else
 begin
{debug if pos('Beatles',dirpath)>0 then begin
beep;
end;}
fn:=dirpath+'\'+filename;
if fileexists(fn)=false  then exit;
try
fl:=tfilestream.create(fn,fmOpenRead or fmShareDenyWrite);
setlength(scrap,length(fileheader));
fl.read(pointer(scrap)^,length(fileheader));
if scrap<>fileheader then begin
deletefile(fn);
exit;
end;
inf.genre:=readsizeanddata;
inf.artist_type:=readsizeanddata;
fl.read(inf.year,sizeof(inf.year));
fl.read(inf.decade,sizeof(inf.decade));
inf.description:=readsizeanddata;
inf.recordlabel:=readsizeanddata;
fl.read(byte(inf.compilation),1);
inf.keywords:=readsizeanddata;
readsizeandjpeg(inf.rear);
readsizeandjpeg(inf.inside);
readsizeandjpeg(inf.additional);
inf.notes:=readsizeanddata;
inf.catno:=readsizeanddata;
inf.urlalbum:=readsizeanddata;
inf.urlartist:=readsizeanddata;
inf.urladditional:=readsizeanddata;
fl.free;
except;
if fl<>nil then fl.free;
result:=-1;
end;
end;


end;

end.