lib/upnp: use std::unique_ptr
This commit is contained in:
parent
224d511616
commit
f097952b42
@ -244,6 +244,7 @@ UPNP_SOURCES = \
|
|||||||
src/lib/upnp/ixmlwrap.cxx src/lib/upnp/ixmlwrap.hxx \
|
src/lib/upnp/ixmlwrap.cxx src/lib/upnp/ixmlwrap.hxx \
|
||||||
src/lib/upnp/Callback.hxx \
|
src/lib/upnp/Callback.hxx \
|
||||||
src/lib/upnp/Util.cxx src/lib/upnp/Util.hxx \
|
src/lib/upnp/Util.cxx src/lib/upnp/Util.hxx \
|
||||||
|
src/lib/upnp/UniqueIxml.hxx \
|
||||||
src/lib/upnp/WorkQueue.hxx \
|
src/lib/upnp/WorkQueue.hxx \
|
||||||
src/lib/upnp/Action.hxx
|
src/lib/upnp/Action.hxx
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "ContentDirectoryService.hxx"
|
#include "ContentDirectoryService.hxx"
|
||||||
|
#include "UniqueIxml.hxx"
|
||||||
#include "Domain.hxx"
|
#include "Domain.hxx"
|
||||||
#include "Device.hxx"
|
#include "Device.hxx"
|
||||||
#include "ixmlwrap.hxx"
|
#include "ixmlwrap.hxx"
|
||||||
@ -56,20 +57,18 @@ ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl,
|
|||||||
{
|
{
|
||||||
assert(result.empty());
|
assert(result.empty());
|
||||||
|
|
||||||
IXML_Document *request =
|
UniqueIxmlDocument request(UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(),
|
||||||
UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(),
|
0,
|
||||||
0,
|
nullptr, nullptr));
|
||||||
nullptr, nullptr);
|
if (!request) {
|
||||||
if (request == 0) {
|
|
||||||
error.Set(upnp_domain, "UpnpMakeAction() failed");
|
error.Set(upnp_domain, "UpnpMakeAction() failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
IXML_Document *response;
|
IXML_Document *_response;
|
||||||
auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
|
auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
|
||||||
m_serviceType.c_str(),
|
m_serviceType.c_str(),
|
||||||
0 /*devUDN*/, request, &response);
|
0 /*devUDN*/, request.get(), &_response);
|
||||||
ixmlDocument_free(request);
|
|
||||||
if (code != UPNP_E_SUCCESS) {
|
if (code != UPNP_E_SUCCESS) {
|
||||||
error.Format(upnp_domain, code,
|
error.Format(upnp_domain, code,
|
||||||
"UpnpSendAction() failed: %s",
|
"UpnpSendAction() failed: %s",
|
||||||
@ -77,18 +76,17 @@ ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *s = ixmlwrap::getFirstElementValue(response, "SearchCaps");
|
UniqueIxmlDocument response(_response);
|
||||||
if (s == nullptr || *s == 0) {
|
|
||||||
ixmlDocument_free(response);
|
const char *s = ixmlwrap::getFirstElementValue(response.get(),
|
||||||
return true;
|
"SearchCaps");
|
||||||
}
|
if (s == nullptr || *s == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
bool success = true;
|
|
||||||
if (!csvToStrings(s, result)) {
|
if (!csvToStrings(s, result)) {
|
||||||
error.Set(upnp_domain, "Bad response");
|
error.Set(upnp_domain, "Bad response");
|
||||||
success = false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ixmlDocument_free(response);
|
return true;
|
||||||
return success;
|
|
||||||
}
|
}
|
||||||
|
40
src/lib/upnp/UniqueIxml.hxx
Normal file
40
src/lib/upnp/UniqueIxml.hxx
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2016 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MPD_UPNP_UNIQUE_XML_HXX
|
||||||
|
#define MPD_UPNP_UNIQUE_XML_HXX
|
||||||
|
|
||||||
|
#include <upnp/ixml.h>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
struct UpnpIxmlDeleter {
|
||||||
|
void operator()(IXML_Document *doc) {
|
||||||
|
ixmlDocument_free(doc);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()(IXML_NodeList *nl) {
|
||||||
|
ixmlNodeList_free(nl);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef std::unique_ptr<IXML_Document, UpnpIxmlDeleter> UniqueIxmlDocument;
|
||||||
|
typedef std::unique_ptr<IXML_NodeList, UpnpIxmlDeleter> UniqueIxmlNodeList;
|
||||||
|
|
||||||
|
#endif
|
@ -16,29 +16,26 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ixmlwrap.hxx"
|
#include "ixmlwrap.hxx"
|
||||||
|
#include "UniqueIxml.hxx"
|
||||||
|
|
||||||
namespace ixmlwrap {
|
namespace ixmlwrap {
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
getFirstElementValue(IXML_Document *doc, const char *name)
|
getFirstElementValue(IXML_Document *doc, const char *name)
|
||||||
{
|
{
|
||||||
const char *ret = nullptr;
|
UniqueIxmlNodeList nodes(ixmlDocument_getElementsByTagName(doc, name));
|
||||||
IXML_NodeList *nodes =
|
if (!nodes)
|
||||||
ixmlDocument_getElementsByTagName(doc, name);
|
return nullptr;
|
||||||
|
|
||||||
if (nodes) {
|
IXML_Node *first = ixmlNodeList_item(nodes.get(), 0);
|
||||||
IXML_Node *first = ixmlNodeList_item(nodes, 0);
|
if (!first)
|
||||||
if (first) {
|
return nullptr;
|
||||||
IXML_Node *dnode = ixmlNode_getFirstChild(first);
|
|
||||||
if (dnode) {
|
|
||||||
ret = ixmlNode_getNodeValue(dnode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ixmlNodeList_free(nodes);
|
IXML_Node *dnode = ixmlNode_getFirstChild(first);
|
||||||
}
|
if (!dnode)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
return ret;
|
return ixmlNode_getNodeValue(dnode);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user