lib/expat: use C++ exceptions instead of class Error

This commit is contained in:
Max Kellermann
2016-02-06 08:45:19 +01:00
parent cd2f65aafc
commit 6c5bc9b4a3
11 changed files with 108 additions and 174 deletions

View File

@@ -36,16 +36,11 @@ ExpatParser::SetError(Error &error)
XML_ErrorString(code));
}
bool
ExpatParser::Parse(const char *data, size_t length, bool is_final,
Error &error)
void
ExpatParser::Parse(const char *data, size_t length, bool is_final)
{
bool success = XML_Parse(parser, data, length,
is_final) == XML_STATUS_OK;
if (!success)
SetError(error);
return success;
if (XML_Parse(parser, data, length, is_final) != XML_STATUS_OK)
throw ExpatError(parser);
}
bool
@@ -59,14 +54,14 @@ ExpatParser::Parse(InputStream &is, Error &error)
if (nbytes == 0)
break;
if (!Parse(buffer, nbytes, false, error))
return false;
Parse(buffer, nbytes, false);
}
if (error.IsDefined())
return false;
return Parse("", 0, true, error);
Parse("", 0, true);
return true;
}
const char *

View File

@@ -25,9 +25,20 @@
#include <expat.h>
#include <stdexcept>
class InputStream;
class Error;
class ExpatError final : public std::runtime_error {
public:
ExpatError(XML_Error code)
:std::runtime_error(XML_ErrorString(code)) {}
ExpatError(XML_Parser parser)
:ExpatError(XML_GetErrorCode(parser)) {}
};
class ExpatParser final {
const XML_Parser parser;
@@ -53,8 +64,7 @@ public:
XML_SetCharacterDataHandler(parser, charhndl);
}
bool Parse(const char *data, size_t length, bool is_final,
Error &error);
void Parse(const char *data, size_t length, bool is_final);
bool Parse(InputStream &is, Error &error);
@@ -83,9 +93,8 @@ public:
parser.SetCharacterDataHandler(CharacterData);
}
bool Parse(const char *data, size_t length, bool is_final,
Error &error) {
return parser.Parse(data, length, is_final, error);
void Parse(const char *data, size_t length, bool is_final) {
parser.Parse(data, length, is_final);
}
bool Parse(InputStream &is, Error &error) {

View File

@@ -76,17 +76,14 @@ public:
/** Read a container's children list into dirbuf.
*
* @param objectId the UPnP object Id for the container. Root has Id "0"
* @param[out] dirbuf stores the entries we read.
*/
bool readDir(UpnpClient_Handle handle,
const char *objectId, UPnPDirContent &dirbuf,
Error &error) const;
UPnPDirContent readDir(UpnpClient_Handle handle,
const char *objectId) const;
bool readDirSlice(UpnpClient_Handle handle,
void readDirSlice(UpnpClient_Handle handle,
const char *objectId, unsigned offset,
unsigned count, UPnPDirContent& dirbuf,
unsigned &didread, unsigned &total,
Error &error) const;
unsigned &didread, unsigned &total) const;
/** Search the content directory service.
*
@@ -96,22 +93,17 @@ public:
* @param searchstring an UPnP searchcriteria string. Check the
* UPnP document: UPnP-av-ContentDirectory-v1-Service-20020625.pdf
* section 2.5.5. Maybe we'll provide an easier way some day...
* @param[out] dirbuf stores the entries we read.
*/
bool search(UpnpClient_Handle handle,
const char *objectId, const char *searchstring,
UPnPDirContent &dirbuf,
Error &error) const;
UPnPDirContent search(UpnpClient_Handle handle,
const char *objectId,
const char *searchstring) const;
/** Read metadata for a given node.
*
* @param objectId the UPnP object Id. Root has Id "0"
* @param[out] dirbuf stores the entries we read. At most one entry will be
* returned.
*/
bool getMetadata(UpnpClient_Handle handle,
const char *objectId, UPnPDirContent &dirbuf,
Error &error) const;
UPnPDirContent getMetadata(UpnpClient_Handle handle,
const char *objectId) const;
/** Retrieve search capabilities
*

View File

@@ -21,7 +21,6 @@
#include "Device.hxx"
#include "Util.hxx"
#include "lib/expat/ExpatParser.hxx"
#include "util/Error.hxx"
#include <stdlib.h>
@@ -100,15 +99,12 @@ protected:
}
};
bool
UPnPDevice::Parse(const std::string &url, const char *description,
Error &error)
void
UPnPDevice::Parse(const std::string &url, const char *description)
{
{
UPnPDeviceParser mparser(*this);
if (!mparser.Parse(description, strlen(description),
true, error))
return false;
mparser.Parse(description, strlen(description), true);
}
if (URLBase.empty()) {
@@ -129,6 +125,4 @@ UPnPDevice::Parse(const std::string &url, const char *description,
}
}
}
return true;
}

View File

@@ -23,8 +23,6 @@
#include <vector>
#include <string>
class Error;
/**
* UPnP Description phase: interpreting the device description which we
* downloaded from the URL obtained by the discovery phase.
@@ -81,8 +79,7 @@ public:
* @param url where the description came from
* @param description the xml device description
*/
bool Parse(const std::string &url, const char *description,
Error &error);
void Parse(const std::string &url, const char *description);
};
#endif /* _UPNPDEV_HXX_INCLUDED_ */

View File

@@ -135,13 +135,10 @@ UPnPDeviceDirectory::Explore()
ContentDirectoryDescriptor d(std::move(tsk->device_id),
MonotonicClockS(), tsk->expires);
{
Error error2;
bool success = d.Parse(tsk->url, buf, error2);
if (!success) {
LogError(error2);
continue;
}
try {
d.Parse(tsk->url, buf);
} catch (const std::exception &e) {
LogError(e);
}
LockAdd(std::move(d));

View File

@@ -87,9 +87,8 @@ class UPnPDeviceDirectory final : UpnpCallback {
unsigned last, int exp)
:id(std::move(_id)), expires(last + exp + 20) {}
bool Parse(const std::string &url, const char *description,
Error &_error) {
return device.Parse(url, description, _error);
void Parse(const std::string &url, const char *description) {
device.Parse(url, description);
}
};