Commit f097952b authored by Max Kellermann's avatar Max Kellermann

lib/upnp: use std::unique_ptr

parent 224d5116
......@@ -244,6 +244,7 @@ UPNP_SOURCES = \
src/lib/upnp/ixmlwrap.cxx src/lib/upnp/ixmlwrap.hxx \
src/lib/upnp/Callback.hxx \
src/lib/upnp/Util.cxx src/lib/upnp/Util.hxx \
src/lib/upnp/UniqueIxml.hxx \
src/lib/upnp/WorkQueue.hxx \
src/lib/upnp/Action.hxx
......
......@@ -19,6 +19,7 @@
#include "config.h"
#include "ContentDirectoryService.hxx"
#include "UniqueIxml.hxx"
#include "Domain.hxx"
#include "Device.hxx"
#include "ixmlwrap.hxx"
......@@ -56,20 +57,18 @@ ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl,
{
assert(result.empty());
IXML_Document *request =
UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(),
0,
nullptr, nullptr);
if (request == 0) {
UniqueIxmlDocument request(UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(),
0,
nullptr, nullptr));
if (!request) {
error.Set(upnp_domain, "UpnpMakeAction() failed");
return false;
}
IXML_Document *response;
IXML_Document *_response;
auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
m_serviceType.c_str(),
0 /*devUDN*/, request, &response);
ixmlDocument_free(request);
0 /*devUDN*/, request.get(), &_response);
if (code != UPNP_E_SUCCESS) {
error.Format(upnp_domain, code,
"UpnpSendAction() failed: %s",
......@@ -77,18 +76,17 @@ ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl,
return false;
}
const char *s = ixmlwrap::getFirstElementValue(response, "SearchCaps");
if (s == nullptr || *s == 0) {
ixmlDocument_free(response);
UniqueIxmlDocument response(_response);
const char *s = ixmlwrap::getFirstElementValue(response.get(),
"SearchCaps");
if (s == nullptr || *s == 0)
return true;
}
bool success = true;
if (!csvToStrings(s, result)) {
error.Set(upnp_domain, "Bad response");
success = false;
return false;
}
ixmlDocument_free(response);
return success;
return true;
}
/*
* 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 "UniqueIxml.hxx"
namespace ixmlwrap {
const char *
getFirstElementValue(IXML_Document *doc, const char *name)
{
const char *ret = nullptr;
IXML_NodeList *nodes =
ixmlDocument_getElementsByTagName(doc, name);
UniqueIxmlNodeList nodes(ixmlDocument_getElementsByTagName(doc, name));
if (!nodes)
return nullptr;
if (nodes) {
IXML_Node *first = ixmlNodeList_item(nodes, 0);
if (first) {
IXML_Node *dnode = ixmlNode_getFirstChild(first);
if (dnode) {
ret = ixmlNode_getNodeValue(dnode);
}
}
IXML_Node *first = ixmlNodeList_item(nodes.get(), 0);
if (!first)
return nullptr;
ixmlNodeList_free(nodes);
}
IXML_Node *dnode = ixmlNode_getFirstChild(first);
if (!dnode)
return nullptr;
return ret;
return ixmlNode_getNodeValue(dnode);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment