123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #include "main.h"
- #include "B4SLoader.h"
- #include "../nu/AutoChar.h"
- #include "..\Components\wac_network\wac_network_http_receiver_api.h"
- #include <strsafe.h>
- B4SLoader::B4SLoader() : parser(0), parserFactory(0)
- {
- parserFactory = WASABI_API_SVC->service_getServiceByGuid(obj_xmlGUID);
- if (parserFactory)
- parser = (obj_xml *)parserFactory->getInterface();
- if (parser)
- {
- parser->xmlreader_registerCallback(L"WasabiXML\fplaylist\fentry\fname", &b4sXml.title);
- parser->xmlreader_registerCallback(L"WinampXML\fplaylist\fentry\fname", &b4sXml.title);
- parser->xmlreader_registerCallback(L"WasabiXML\fplaylist\fentry\flength", &b4sXml.length);
- parser->xmlreader_registerCallback(L"WinampXML\fplaylist\fentry\flength", &b4sXml.length);
- //parser->xmlreader_registerCallback(L"WasabiXML\fplaylist", this);
- //parser->xmlreader_registerCallback(L"WinampXML\fplaylist", this);
- parser->xmlreader_registerCallback(L"WasabiXML\fplaylist\fentry", &b4sXml);
- parser->xmlreader_registerCallback(L"WinampXML\fplaylist\fentry", &b4sXml);
- parser->xmlreader_open();
- //parser->xmlreader_setEncoding(L"UTF-8");
- }
- }
- B4SXML::B4SXML()
- {
- filename[0]=0;
- playlist = 0;
- }
- B4SLoader::~B4SLoader()
- {
- if (parser)
- {
- parser->xmlreader_unregisterCallback(&b4sXml);
- parser->xmlreader_unregisterCallback(&b4sXml.length);
- parser->xmlreader_unregisterCallback(&b4sXml.title);
- parser->xmlreader_close();
- }
- if (parserFactory && parser)
- parserFactory->releaseInterface(parser);
- parserFactory = 0;
- parser = 0;
- }
- #define HTTP_BUFFER_SIZE 1024
- static int FeedXMLHTTP(api_httpreceiver *http, obj_xml *parser, bool *noData)
- {
- char downloadedData[HTTP_BUFFER_SIZE] = {0};
- int xmlResult = API_XML_SUCCESS;
- int downloadSize = http->get_bytes(downloadedData, HTTP_BUFFER_SIZE);
- if (downloadSize)
- {
- xmlResult = parser->xmlreader_feed((void *)downloadedData, downloadSize);
- *noData=false;
- }
- else
- *noData = true;
- return xmlResult;
- }
- static void RunXMLDownload(api_httpreceiver *http, obj_xml *parser)
- {
- int ret;
- bool noData;
- do
- {
- Sleep(50);
- ret = http->run();
- if (FeedXMLHTTP(http, parser, &noData) != API_XML_SUCCESS)
- return ;
- }
- while (ret == HTTPRECEIVER_RUN_OK);
- // finish off the data
- do
- {
- if (FeedXMLHTTP(http, parser, &noData) != API_XML_SUCCESS)
- return ;
- } while (!noData);
- parser->xmlreader_feed(0, 0);
- }
- void B4SXML::StartTag(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
- {
- if (!_wcsicmp(xmltag, L"entry"))
- {
- const wchar_t *track = params->getItemValue(L"playstring");
- if (!_wcsnicmp(track, L"file://", 7))
- track+=7;
- else if (!_wcsnicmp(track, L"file:", 5))
- track+=5;
- StringCchCopyW(filename, FILENAME_SIZE, track);
- }
- }
- void B4SXML::EndTag(const wchar_t *xmlpath, const wchar_t *xmltag)
- {
- if (!_wcsicmp(xmltag, L"entry") && filename[0])
- {
- int lengthSeconds=-1;
- const wchar_t *trackLength = length.GetString();
- if (trackLength && *trackLength)
- lengthSeconds = _wtoi(trackLength) / 1000;
- const wchar_t *trackTitle = title.GetString();
- // TODO: deal with relative pathnames
- if (trackTitle && *trackTitle)
- playlist->OnFile(filename, trackTitle, lengthSeconds, 0); // TODO: extended info
- else
- playlist->OnFile(filename, 0, lengthSeconds, 0);// TODO: extended info
- filename[0]=0;
- length.Reset();
- title.Reset();
- }
- }
- #if 0 // TOOD: reimplement
- void B4SLoader::LoadURL(const char *url)
- {
- if (!parser)
- return ; // no sense in continuing if there's no parser available
- api_httpreceiver *http = 0;
- waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(httpreceiverGUID);
- if (sf)
- http = (api_httpreceiver *)sf->getInterface();
- if (!http)
- return ;
- http->AllowCompression();
- http->open(API_DNS_AUTODNS, HTTP_BUFFER_SIZE, GetProxy());
- SetUserAgent(http);
- http->connect(url);
- int ret;
- bool first = true;
- do
- {
- Sleep(50);
- ret = http->run();
- if (ret == -1) // connection failed
- break;
- // ---- check our reply code ----
- int replycode = http->GetReplyCode();
- switch (replycode)
- {
- case 0:
- case 100:
- break;
- case 200:
- {
- RunXMLDownload(http, parser);
- sf->releaseInterface(http);
- return ;
- }
- break;
- default:
- sf->releaseInterface(http);
- return ;
- }
- }
- while (ret == HTTPRECEIVER_RUN_OK);
- const char *er = http->geterrorstr();
- sf->releaseInterface(http);
- return ;
- }
- void B4SLoader::LoadFile(const char *filename)
- {
- if (!parser)
- return ; // no sense in continuing if there's no parser available
- HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
- if (file == INVALID_HANDLE_VALUE)
- return ;
- char data[1024] = {0};
- while (true)
- {
- DWORD bytesRead = 0;
- if (ReadFile(file, data, 1024, &bytesRead, NULL) && bytesRead)
- {
- parser->xmlreader_feed(data, bytesRead);
- }
- else
- break;
- }
- CloseHandle(file);
- parser->xmlreader_feed(0, 0);
- }
- #endif
- int B4SLoader::Load(const wchar_t *filename, ifc_playlistloadercallback *playlist)
- {
- if (!parser)
- return IFC_PLAYLISTLOADER_FAILED; // no sense in continuing if there's no parser available
- b4sXml.playlist=playlist;
- HANDLE file = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
- if (file == INVALID_HANDLE_VALUE)
- return IFC_PLAYLISTLOADER_FAILED;
- while (true)
- {
- char data[1024] = {0};
- DWORD bytesRead = 0;
- if (ReadFile(file, data, 1024, &bytesRead, NULL) && bytesRead)
- {
- parser->xmlreader_feed(data, bytesRead);
- }
- else
- break;
- }
- CloseHandle(file);
- parser->xmlreader_feed(0, 0);
- return IFC_PLAYLISTLOADER_SUCCESS;
- }
- #ifdef CBCLASS
- #undef CBCLASS
- #endif
- #define CBCLASS B4SXML
- START_DISPATCH;
- VCB(ONSTARTELEMENT, StartTag)
- VCB(ONENDELEMENT, EndTag)
- END_DISPATCH;
- #ifdef CBCLASS
- #undef CBCLASS
- #endif
- #define CBCLASS B4SLoader
- START_DISPATCH;
- CB(IFC_PLAYLISTLOADER_LOAD, Load)
- END_DISPATCH;
|