aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_playlists/playlistsXML.cpp
blob: ef8359b451eba70e658fa94da1f9f986178d0a47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "Main.h"
#include "playlistsXML.h"
#include "../nu/AutoChar.h"

void PlaylistsXML::StartTag(const wchar_t *xmlpath, const wchar_t *xmltag, api_xmlreaderparams *params)
{
	const wchar_t *filename = params->getItemValue(L"filename");
	const wchar_t *title = params->getItemValue(L"title");
	const wchar_t *countString = params->getItemValue(L"songs");

	int numItems = 0;
	if (countString && *countString)
		numItems = _wtoi(countString);

	const wchar_t *lengthString = params->getItemValue(L"seconds");
	int length = -1000;
	if (lengthString && *lengthString)
		length = _wtoi(lengthString);

	AddPlaylist(title, filename, true, numItems, length);
}

void PlaylistsXML::LoadFile(const wchar_t *filename)
{
	if (!parser)
		return ; // no sense in continuing if there's no parser available

	HANDLE file = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
	if (file == INVALID_HANDLE_VALUE)
		return ;

	char data[1024] = {0};
	DWORD bytesRead;
	while (true)
	{
		if (ReadFile(file, data, 1024, &bytesRead, NULL) && bytesRead)
		{
			if (parser->xmlreader_feed(data, bytesRead) != API_XML_SUCCESS)
			{
				CloseHandle(file);
				return;
			}
		}
		else
			break;
	}

	CloseHandle(file);
	parser->xmlreader_feed(0, 0);
}

PlaylistsXML::PlaylistsXML(): parser(0), parserFactory(0)
{
	parserFactory = WASABI_API_SVC->service_getServiceByGuid(api_xmlGUID);
	if (parserFactory)
		parser = (api_xml *)parserFactory->getInterface();

	if (parser)
	{
		parser->xmlreader_registerCallback(L"Winamp:Playlists\fplaylist", this);
		parser->xmlreader_registerCallback(L"playlists\fplaylist", this);
		parser->xmlreader_open();
	}
}

PlaylistsXML::~PlaylistsXML()
{
	if (parser)
	{
		parser->xmlreader_unregisterCallback(this);
		parser->xmlreader_close();
	}

	if (parserFactory && parser)
		parserFactory->releaseInterface(parser);

	parserFactory = 0;
	parser = 0;
}

#ifdef CBCLASS
#undef CBCLASS
#endif

#define CBCLASS PlaylistsXML
START_DISPATCH;
VCB(ONSTARTELEMENT, StartTag)
END_DISPATCH;