playlistsXML.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "Main.h"
  2. #include "playlistsXML.h"
  3. #include "../nu/AutoChar.h"
  4. void PlaylistsXML::StartTag(const wchar_t *xmlpath, const wchar_t *xmltag, api_xmlreaderparams *params)
  5. {
  6. const wchar_t *filename = params->getItemValue(L"filename");
  7. const wchar_t *title = params->getItemValue(L"title");
  8. const wchar_t *countString = params->getItemValue(L"songs");
  9. int numItems = 0;
  10. if (countString && *countString)
  11. numItems = _wtoi(countString);
  12. const wchar_t *lengthString = params->getItemValue(L"seconds");
  13. int length = -1000;
  14. if (lengthString && *lengthString)
  15. length = _wtoi(lengthString);
  16. AddPlaylist(title, filename, true, numItems, length);
  17. }
  18. void PlaylistsXML::LoadFile(const wchar_t *filename)
  19. {
  20. if (!parser)
  21. return ; // no sense in continuing if there's no parser available
  22. HANDLE file = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
  23. if (file == INVALID_HANDLE_VALUE)
  24. return ;
  25. char data[1024] = {0};
  26. DWORD bytesRead;
  27. while (true)
  28. {
  29. if (ReadFile(file, data, 1024, &bytesRead, NULL) && bytesRead)
  30. {
  31. if (parser->xmlreader_feed(data, bytesRead) != API_XML_SUCCESS)
  32. {
  33. CloseHandle(file);
  34. return;
  35. }
  36. }
  37. else
  38. break;
  39. }
  40. CloseHandle(file);
  41. parser->xmlreader_feed(0, 0);
  42. }
  43. PlaylistsXML::PlaylistsXML(): parser(0), parserFactory(0)
  44. {
  45. parserFactory = WASABI_API_SVC->service_getServiceByGuid(api_xmlGUID);
  46. if (parserFactory)
  47. parser = (api_xml *)parserFactory->getInterface();
  48. if (parser)
  49. {
  50. parser->xmlreader_registerCallback(L"Winamp:Playlists\fplaylist", this);
  51. parser->xmlreader_registerCallback(L"playlists\fplaylist", this);
  52. parser->xmlreader_open();
  53. }
  54. }
  55. PlaylistsXML::~PlaylistsXML()
  56. {
  57. if (parser)
  58. {
  59. parser->xmlreader_unregisterCallback(this);
  60. parser->xmlreader_close();
  61. }
  62. if (parserFactory && parser)
  63. parserFactory->releaseInterface(parser);
  64. parserFactory = 0;
  65. parser = 0;
  66. }
  67. #ifdef CBCLASS
  68. #undef CBCLASS
  69. #endif
  70. #define CBCLASS PlaylistsXML
  71. START_DISPATCH;
  72. VCB(ONSTARTELEMENT, StartTag)
  73. END_DISPATCH;