diff options
Diffstat (limited to 'delphi/Awkward/utils/playlist.pas')
-rw-r--r-- | delphi/Awkward/utils/playlist.pas | 431 |
1 files changed, 0 insertions, 431 deletions
diff --git a/delphi/Awkward/utils/playlist.pas b/delphi/Awkward/utils/playlist.pas deleted file mode 100644 index d1fb552..0000000 --- a/delphi/Awkward/utils/playlist.pas +++ /dev/null @@ -1,431 +0,0 @@ -{Playlist process}
-unit playlist;
-
-interface
-
-type
- tPlaylist = class
- private
- fShuffle :boolean;
- PlSize :cardinal; // playlist entries
- PlCapacity:cardinal;
- base :pWideChar;
- name :pWideChar;
- descr :pWideChar;
- PlStrings :array of PWideChar;
- CurElement:cardinal;
- PlOrder :array of cardinal;
- CurOrder :cardinal;
-
- procedure SetShuffle(value:boolean);
- function GetShuffle:boolean;
- procedure DoShuffle;
-
- function GetTrackNumber:integer;
- procedure SetTrackNumber(value:integer);
-
- procedure AddLine(name,descr:pWideChar;new:boolean=true);
- function ProcessElement(num:integer=-1):PWideChar; //virtual;
-
- public
- constructor Create(fName:pWideChar);
- destructor Free;
-
- procedure SetBasePath(path:pWideChar);
-
- function GetSong(number:integer=-1):pWideChar;
- function GetCount:integer;
-
- function Next :pWideChar;
- function Previous:pWideChar;
-
- property Track :integer read GetTrackNumber write SetTrackNumber;
- property Shuffle:boolean read GetShuffle write SetShuffle;
- end;
-
-function isPlaylist(fname:pWideChar):integer;
-function CreatePlaylist(fname:pWideChar):tPlaylist;
-
-implementation
-
-uses windows, common, io;//, m_api, mirutils;
-
-const
- plSizeStart = 2048;
- plSizeStep = 256;
-const
- pltM3OLD = $100;
- pltM3UTF = $200;
-
-type
- tM3UPlaylist = class(tPlayList)
- private
- public
- constructor Create(fName:pWideChar);
- end;
-
- tPLSPlaylist = class(tPlayList)
- private
- public
- constructor Create(fName:pWideChar);
- end;
-
-function isPlaylist(fname:pWideChar):integer;
-var
- ext:array [0..7] of WideChar;
-begin
- GetExt(fname,ext,7);
- if StrCmpW(ext,'M3U',3)=0 then result:=1
- else if StrCmpW(ext,'PLS' )=0 then result:=2
- else result:=0;
-end;
-
-function CreatePlaylist(fname:pWideChar):tPlaylist;
-begin
- case isPlaylist(fname) of
- 1: result:=tM3UPlaylist.Create(fName);
- 2: result:=tPLSPlaylist.Create(fName);
- else result:=nil;
- end;
-end;
-
-//----- -----
-
-function SkipLine(var p:PWideChar):bool;
-begin
- while p^>=' ' do inc(p);
- while p^<=' ' do // Skip spaces too
- begin
- if p^=#0 then
- begin
- result:=false;
- exit;
- end;
- p^:=#0;
- inc(p);
- end;
- result:=true;
-end;
-
-constructor tM3UPlaylist.Create(fName:pWideChar);
-var
- f:THANDLE;
- i:integer;
- p:PAnsiChar;
- pp,pd:pWideChar;
- plBuf:pAnsiChar;
- plBufW:pWideChar;
- pltNew:boolean;
- lname,ldescr:pWideChar;
- finish:boolean;
-begin
- inherited;
-
- // Load into mem
- f:=Reset(fName);
- if dword(f)<>INVALID_HANDLE_VALUE then
- begin
- i:=integer(FileSize(f));
- if i=-1 then
- i:=integer(GetFSize(fName));
- if i<>-1 then
- begin
- mGetMem(PlBuf,i+1);
- BlockRead(f,PlBuf^,i);
- CloseHandle(f);
- PlBuf[i]:=#0;
-
- p:=PlBuf;
- if (pdword(p)^ and $00FFFFFF)=$00BFBBEF then
- begin
- inc(p,3);
- UTF8ToWide(p,plBufW)
- end
- else
- AnsiToWide(p,plBufW);
-
- mFreeMem(plBuf);
-
- pp:=plBufW;
- pltNew:=StrCmpW(pp,'#EXTM3U',7)=0;
- if pltNew then SkipLine(pp);
-
- repeat
- if pltNew then
- begin
- pd:=StrScanW(pp,',');
- if pd<>nil then
- begin
- ldescr:=pd+1;
- if not SkipLine(pp) then break;
- end;
- end;
- lname:=pp;
- finish:=SkipLine(pp);
- AddLine(lname,ldescr);
- until not finish;
-
- mFreeMem(plBufW);
- end;
- end;
-
-end;
-
-//----- -----
-
-constructor tPLSPlaylist.Create(fName:pWideChar);
-var
- buf:array [0..MAX_PATH-1] of AnsiChar;
- lname,ldescr:pWideChar;
- ffile,ftitle:array [0..31] of AnsiChar;
- plName:array [0..127] of AnsiChar;
- f,t:pAnsiChar;
- i,size:integer;
- plFile:pAnsiChar;
-begin
- inherited;
-
- WideToAnsi(fName,PlFile);
- GetPrivateProfileSectionNamesA(buf,127,PlFile);
- StrCopy(plName,buf);
- size:=GetPrivateProfileIntA(PlName,'NumberOfEntries',0,PlFile);
- f:=StrCopyE(ffile ,'File');
- t:=StrCopyE(ftitle,'Title');
- for i:=1 to size do
- begin
- IntToStr(f,i);
- GetPrivateProfileStringA(PlName,ffile,'',buf,SizeOf(buf),PlFile);
- AnsiToWide(buf,lname);
-
- IntToStr(t,i);
- GetPrivateProfileStringA(PlName,ftitle,'',buf,SizeOf(buf),PlFile);
- AnsiToWide(buf,ldescr);
-
- AddLine(lname,ldescr,false);
- end;
- mFreeMem(plFile);
-end;
-
-//----- -----
-
-constructor tPlaylist.Create(fName:pWideChar);
-begin
- CurElement:=0;
- base:=nil;
- name:=nil;
- descr:=nil;
- Shuffle:=false;
- plSize:=0;
-
- SetBasePath(fname);
-end;
-
-destructor tPlaylist.Free;
-var
- i:integer;
-begin
- PlOrder:=nil;
-
- mFreeMem(base);
- mFreeMem(name);
- mFreeMem(descr);
-
- for i:=0 to PlSize-1 do
- begin
- mFreeMem(plStrings[i*2]);
- mFreeMem(plStrings[i*2+1]);
- end;
- PlStrings:=nil;
-end;
-
-procedure tPlaylist.AddLine(name,descr:pWideChar;new:boolean=true);
-begin
- if PlCapacity=0 then
- begin
- PlCapacity:=plSizeStart;
- SetLength(PlStrings,plSizeStart*2);
- fillChar(plStrings[0],plSizeStart*2*SizeOf(pWideChar),0);
- end
- else if plSize=PlCapacity then
- begin
- inc(plCapacity,plSizeStep);
- SetLength(PlStrings,plCapacity*2);
- fillChar(plStrings[plSize],plSizeStep*2*SizeOf(pWideChar),0);
- end;
- if new then
- begin
- StrDupW(plStrings[plSize*2 ],name);
- StrDupW(plStrings[plSize*2+1],descr);
- end
- else
- begin
- plStrings[plSize*2 ]:=name;
- plStrings[plSize*2+1]:=descr;
- end;
- inc(plSize);
-end;
-
-procedure tPlaylist.SetBasePath(path:pWideChar);
-var
- buf:array [0..MAX_PATH-1] of WideChar;
- p,pp:pWideChar;
-begin
- mFreeMem(base);
-
- pp:=ExtractW(path,false);
- p:=StrCopyEW(buf,pp);
- mFreeMem(pp);
-
- if ((p-1)^<>'\') and ((p-1)^<>'/') then
- begin
- if StrScanW(buf,'/')<>nil then
- p^:='/'
- else
- p^:='\';
- inc(p);
- end;
- p^:=#0;
- StrDupW(base,buf);
-end;
-
-function tPlaylist.GetCount:integer;
-begin
- result:=PlSize;
-end;
-
-function tPlaylist.GetTrackNumber:integer;
-begin
- if fShuffle then
- result:=CurOrder
- else
- result:=CurElement;
-end;
-
-procedure tPlaylist.SetTrackNumber(value:integer);
-begin
- if value<0 then
- value:=0
- else if value>=Integer(PlSize) then
- value:=PlSize-1;
-
- if fShuffle then
- CurOrder:=value
- else
- CurElement:=value;
-end;
-
-function tPlaylist.ProcessElement(num:integer=-1):pWideChar;
-begin
- if num<0 then
- num:=Track
- else if num>=integer(PlSize) then
- num:=PlSize-1;
-
- result:=plStrings[num*2];
-end;
-
-function tPlaylist.GetSong(number:integer=-1):PWideChar;
-var
- buf:array [0..MAX_PATH-1] of WideChar;
-begin
- result:=ProcessElement(number);
-
- if (result<>nil) and not isPathAbsolute(result) and (base<>nil) then
- begin
- StrCopyW(StrCopyEW(buf,base),result);
- mFreeMem(result);
- StrDupW(result,buf);
- end;
-end;
-
-procedure tPlaylist.SetShuffle(value:boolean);
-begin
- if value then
- begin
-// if not fShuffle then // need to set Shuffle
- DoShuffle;
- end;
-
- fShuffle:=value;
-end;
-
-function tPlaylist.GetShuffle:boolean;
-begin
- result:=fShuffle;
-end;
-
-procedure tPlaylist.DoShuffle;
-var
- i,RandInx: cardinal;
- SwapItem: cardinal;
-begin
- SetLength(PlOrder,PlSize);
- Randomize;
- for i:=0 to PlSize-1 do
- PlOrder[i]:=i;
- if PlSize>1 then
- begin
- for i:=0 to PlSize-2 do
- begin
- RandInx:=cardinal(Random(PlSize-i));
- SwapItem:=PlOrder[i];
- PlOrder[i ]:=PlOrder[RandInx];
- PlOrder[RandInx]:=SwapItem;
- end;
- end;
- CurOrder:=0;
-end;
-
-function tPlaylist.Next:PWideChar;
-begin
- if PlSize<>0 then
- begin
- if not Shuffle then
- begin
- inc(CurElement);
- if CurElement=PlSize then
- CurElement:=0;
- end
- else // if mode=plShuffle then
- begin
- inc(CurOrder);
- if CurOrder=PlSize then
- begin
- DoShuffle;
- CurOrder:=0;
- end;
- CurElement:=PlOrder[CurOrder];
- end;
- result:=GetSong;
- end
- else
- result:=nil;
-end;
-
-function tPlaylist.Previous:PWideChar;
-begin
- if PlSize<>0 then
- begin
- if not Shuffle then
- begin
- if CurElement=0 then
- CurElement:=PlSize;
- Dec(CurElement);
- end
- else // if mode=plShuffle then
- begin
- if CurOrder=0 then
- begin
- DoShuffle;
- CurOrder:=PlSize;
- end;
- dec(CurOrder);
- CurElement:=PlOrder[CurOrder];
- end;
- result:=GetSong;
- end
- else
- result:=nil;
-end;
-
-end.
|