db/upnp/ixmlwrap: getFirstElementValue() returns const char *

Eliminate the std::string bloat.
This commit is contained in:
Max Kellermann 2014-01-18 15:01:19 +01:00
parent e569f82dd3
commit 6e55552292
5 changed files with 33 additions and 27 deletions

View File

@ -59,6 +59,16 @@ public:
} }
}; };
static bool
ReadResultTag(UPnPDirContent &dirbuf, IXML_Document *response, Error &error)
{
const char *p = ixmlwrap::getFirstElementValue(response, "Result");
if (p == nullptr)
p = "";
return dirbuf.parse(p, error);
}
bool bool
ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl, ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
const char *objectId, int offset, const char *objectId, int offset,
@ -100,9 +110,9 @@ ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
DirBResFree cleaner(&response); DirBResFree cleaner(&response);
int didread = -1; int didread = -1;
std::string tbuf = ixmlwrap::getFirstElementValue(response, "NumberReturned"); const char *value = ixmlwrap::getFirstElementValue(response, "NumberReturned");
if (!tbuf.empty()) if (value != nullptr)
didread = atoi(tbuf.c_str()); didread = atoi(value);
if (count == -1 || count == 0) { if (count == -1 || count == 0) {
// TODO: what's this? // TODO: what's this?
@ -110,13 +120,11 @@ ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
return false; return false;
} }
tbuf = ixmlwrap::getFirstElementValue(response, "TotalMatches"); value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
if (!tbuf.empty()) if (value != nullptr)
*totalp = atoi(tbuf.c_str()); *totalp = atoi(value);
tbuf = ixmlwrap::getFirstElementValue(response, "Result"); if (!ReadResultTag(dirbuf, response, error))
if (!dirbuf.parse(tbuf, error))
return false; return false;
*didreadp = didread; *didreadp = didread;
@ -189,10 +197,10 @@ ContentDirectoryService::search(UpnpClient_Handle hdl,
DirBResFree cleaner(&response); DirBResFree cleaner(&response);
int count = -1; int count = -1;
std::string tbuf = const char *value =
ixmlwrap::getFirstElementValue(response, "NumberReturned"); ixmlwrap::getFirstElementValue(response, "NumberReturned");
if (!tbuf.empty()) if (value != nullptr)
count = atoi(tbuf.c_str()); count = atoi(value);
if (count == -1 || count == 0) { if (count == -1 || count == 0) {
// TODO: what's this? // TODO: what's this?
@ -202,13 +210,11 @@ ContentDirectoryService::search(UpnpClient_Handle hdl,
offset += count; offset += count;
tbuf = ixmlwrap::getFirstElementValue(response, "TotalMatches"); value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
if (!tbuf.empty()) if (value != nullptr)
total = atoi(tbuf.c_str()); total = atoi(value);
tbuf = ixmlwrap::getFirstElementValue(response, "Result"); if (!ReadResultTag(dirbuf, response, error))
if (!dirbuf.parse(tbuf, error))
return false; return false;
} }
@ -291,7 +297,7 @@ ContentDirectoryService::getMetadata(UpnpClient_Handle hdl,
return false; return false;
} }
std::string tbuf = ixmlwrap::getFirstElementValue(response, "Result"); bool success = ReadResultTag(dirbuf, response, error);
ixmlDocument_free(response); ixmlDocument_free(response);
return dirbuf.parse(tbuf, error); return success;
} }

View File

@ -186,8 +186,8 @@ protected:
}; };
bool bool
UPnPDirContent::parse(const std::string &input, Error &error) UPnPDirContent::parse(const char *input, Error &error)
{ {
UPnPDirParser parser(*this); UPnPDirParser parser(*this);
return parser.Parse(input.data(), input.length(), true, error); return parser.Parse(input, strlen(input), true, error);
} }

View File

@ -58,7 +58,7 @@ public:
* actually global, nothing really bad will happen if you mix * actually global, nothing really bad will happen if you mix
* up... * up...
*/ */
bool parse(const std::string &didltext, Error &error); bool parse(const char *didltext, Error &error);
}; };
#endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */ #endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */

View File

@ -19,10 +19,10 @@
namespace ixmlwrap { namespace ixmlwrap {
std::string const char *
getFirstElementValue(IXML_Document *doc, const char *name) getFirstElementValue(IXML_Document *doc, const char *name)
{ {
std::string ret; const char *ret = nullptr;
IXML_NodeList *nodes = IXML_NodeList *nodes =
ixmlDocument_getElementsByTagName(doc, name); ixmlDocument_getElementsByTagName(doc, name);

View File

@ -24,10 +24,10 @@
namespace ixmlwrap { namespace ixmlwrap {
/** /**
* Retrieve the text content for the first element of given * Retrieve the text content for the first element of given
* name. Returns an empty string if the element does not * name. Returns nullptr if the element does not
* contain a text node * contain a text node
*/ */
std::string getFirstElementValue(IXML_Document *doc, const char *getFirstElementValue(IXML_Document *doc,
const char *name); const char *name);
}; };