Автор: Maks150988
Дата сообщения: 25.04.2011 18:24
ShIvADeSt
Вопрос снят. Сделал так. Через Pos немного удобнее, благо нативно, да и без приведения к LPWSTR/LPTSTR обходится вслучае lstrcmp.
[more=Далее]
unit PlayList;
interface
uses
Windows, F_SysUtils;
type
TPLSItem = packed record
pszTitle: WideString;
pszURL : WideString;
end;
TPLSType = (ptUnknown, ptM3U, ptM3U8, ptPLS);
TPLSSave = (psM3U, psM3U8, psPLS);
TPLSItems = Array of TPLSItem;
function GetPlayListType(pszPath: WideString): TPLSType;
function GetPlayListM3U(pszPath: WideString): TPLSItems;
implementation
type
TDynArray = Array of WideString;
//
function GetPlayListType(pszPath: WideString): TPLSType;
var
pszText: WideString;
begin
pszText := ExtractFileExtW(pszPath);
CharLowerBuffW(LPWSTR(pszText), Length(pszText));
if (Pos('.m3u', pszText) > 0) then
Result := ptM3U
else
if (Pos('.m3u8', pszText) > 0) then
Result := ptM3U8
else
if (Pos('.pls', pszText) > 0) then
Result := ptPLS
else
Result := ptUnknown;
end;
//
function GetPlayListData(pszPath: WideString): TDynArray;
const
IsUTF8: Array [Boolean] of Integer = (CP_ACP, CP_UTF8);
var
hf : HFILE;
dwRet : DWORD;
hGlob : HGLOBAL;
pData : Pointer;
dwBytesRead: DWORD;
bRet : Boolean;
pszAnsi : AnsiString;
pszText : WideString;
iPosEof : Integer;
begin
SetLength(Result, 0);
hf := CreateFileW(LPWSTR(pszPath), GENERIC_READ, FILE_SHARE_READ, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hf <> INVALID_HANDLE_VALUE) then
try
dwRet := GetFileSize(hf, nil);
if (dwRet <> 0) then
begin
hGlob := GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT, dwRet);
if (hGlob <> 0) then
try
pData := GlobalLock(hGlob);
if (pData <> nil) then
try
bRet := ReadFile(hf, pData^, dwRet - 1, dwBytesRead, nil);
if bRet then
begin
pszAnsi := LPTSTR(pData);
bRet := (pszAnsi[1] = #$EF) and (pszAnsi[2] = #$BB) and
(pszAnsi[3] = #$BF);
if bRet then
Delete(pszAnsi, 1, 3);
dwRet := MultiByteToWideChar(IsUTF8[bRet], 0, LPTSTR(pszAnsi),
Length(pszAnsi), nil, 0);
SetLength(pszText, dwRet);
MultiByteToWideChar(IsUTF8[bRet], 0, LPTSTR(pszAnsi),
Length(pszAnsi), LPWSTR(pszText), dwRet);
pszText := #10 + pszText + #10;
while (Length(pszText) > 0) do
begin
iPosEof := Pos(#10, pszText);
bRet := (iPosEof > 1) and (Length(pszText) > 0);
if bRet then
begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := Copy(pszText, 1, iPosEof - 1);
end;
Delete(pszText, 1, iPosEof);
end;
end;
finally
GlobalUnlock(HGLOBAL(pData));
end;
finally
GlobalFree(hGlob);
end;
end;
finally
CloseHandle(hf);
end;
end;
//
function GetPlayListM3U(pszPath: WideString): TPLSItems;
const
EXTM3U: WideString = '#EXTM3U';
EXTINF: WideString = '#EXTINF:';
var
OutPls: TDynArray;
idItem: Integer;
iPosEx: Integer;
begin
SetLength(Result, 0);
OutPls := GetPlayListData(pszPath);
if (Length(OutPls) > 0) then
begin
for idItem := Low(OutPls) to High(OutPls) do
begin
iPosEx := Pos(EXTM3U, OutPls[idItem]);
if (iPosEx <> 1) then
begin
iPosEx := Pos(EXTINF, OutPls[idItem]);
if (iPosEx = 1) then
begin
SetLength(Result, Length(Result) + 1);
iPosEx := Pos(#44, OutPls[idItem]);
Result[High(Result)].pszTitle := TrimW(Copy(OutPls[idItem],
iPosEx + 1, Length(OutPls[idItem]) - iPosEx - 1));
end
else
begin
Result[High(Result)].pszURL := TrimW(OutPls[idItem]);
end;
end;
end;
end;
end;
end.
[/more]
Если у кого-то есть какие-то замечания к коду - прошу комментарии, хочется удостовериться что код лишен неожиданных изъянов.