From f097952b42e2d43a975df9d67457e27e2b5f6d6c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 6 Feb 2016 23:57:29 +0100 Subject: [PATCH] lib/upnp: use std::unique_ptr --- Makefile.am | 1 + src/lib/upnp/ContentDirectoryService.cxx | 32 +++++++++---------- src/lib/upnp/UniqueIxml.hxx | 40 ++++++++++++++++++++++++ src/lib/upnp/ixmlwrap.cxx | 25 +++++++-------- 4 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 src/lib/upnp/UniqueIxml.hxx diff --git a/Makefile.am b/Makefile.am index b5e1083a1..e2af42fa6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/src/lib/upnp/ContentDirectoryService.cxx b/src/lib/upnp/ContentDirectoryService.cxx index 0636505ab..8509300b3 100644 --- a/src/lib/upnp/ContentDirectoryService.cxx +++ b/src/lib/upnp/ContentDirectoryService.cxx @@ -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); - return true; - } + 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; } diff --git a/src/lib/upnp/UniqueIxml.hxx b/src/lib/upnp/UniqueIxml.hxx new file mode 100644 index 000000000..f0356f0ac --- /dev/null +++ b/src/lib/upnp/UniqueIxml.hxx @@ -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 + +#include + +struct UpnpIxmlDeleter { + void operator()(IXML_Document *doc) { + ixmlDocument_free(doc); + } + + void operator()(IXML_NodeList *nl) { + ixmlNodeList_free(nl); + } +}; + +typedef std::unique_ptr UniqueIxmlDocument; +typedef std::unique_ptr UniqueIxmlNodeList; + +#endif diff --git a/src/lib/upnp/ixmlwrap.cxx b/src/lib/upnp/ixmlwrap.cxx index 6a2829cf9..fbcc30b28 100644 --- a/src/lib/upnp/ixmlwrap.cxx +++ b/src/lib/upnp/ixmlwrap.cxx @@ -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); } }