db/upnp/Directory: parse duration

Don't put all <res/> attributes to the attributes map; parse the
"duration" attribute as soon as we receive it, and store it in an
integer attribute.  This reduces bloat.
This commit is contained in:
Max Kellermann 2014-01-10 10:17:30 +01:00
parent b50d79542c
commit f031eb1ef2
3 changed files with 22 additions and 28 deletions

View File

@ -229,16 +229,6 @@ titleToPathElt(const std::string &in)
return out; return out;
} }
gcc_pure
static int
upnpDurationToSeconds(const std::string &duration)
{
const auto v = stringToTokens(duration, ":");
if (v.size() != 3)
return 0;
return atoi(v[0].c_str())*3600 + atoi(v[1].c_str())*60 + atoi(v[2].c_str());
}
// If uri is empty, we use the object's url instead. This happens // If uri is empty, we use the object's url instead. This happens
// when the target of a Visit() is a song, which only happens when // when the target of a Visit() is a song, which only happens when
// "add"ing AFAIK. Visit() calls us with a null uri so that the url // "add"ing AFAIK. Visit() calls us with a null uri so that the url
@ -253,9 +243,8 @@ upnpItemToSong(const UPnPDirObject &dirent, const char *uri)
TagBuilder tag; TagBuilder tag;
const char *duration = dirent.getprop("duration"); if (dirent.duration > 0)
if (duration != nullptr) tag.SetTime(dirent.duration);
tag.SetTime(upnpDurationToSeconds(duration));
tag.AddItem(TAG_TITLE, titleToPathElt(dirent.m_title).c_str()); tag.AddItem(TAG_TITLE, titleToPathElt(dirent.m_title).c_str());

View File

@ -36,16 +36,6 @@ static const char *const upnptags[] = {
nullptr, nullptr,
}; };
static const char *const res_attributes[] = {
"protocolInfo",
"size",
"bitrate",
"duration",
"sampleFrequency",
"nrAudioChannels",
nullptr,
};
gcc_pure gcc_pure
static UPnPDirObject::ItemClass static UPnPDirObject::ItemClass
ParseItemClass(const char *name) ParseItemClass(const char *name)
@ -58,6 +48,16 @@ ParseItemClass(const char *name)
return UPnPDirObject::ItemClass::UNKNOWN; return UPnPDirObject::ItemClass::UNKNOWN;
} }
gcc_pure
static int
ParseDuration(const std::string &duration)
{
const auto v = stringToTokens(duration, ":");
if (v.size() != 3)
return 0;
return atoi(v[0].c_str())*3600 + atoi(v[1].c_str())*60 + atoi(v[2].c_str());
}
/** /**
* An XML parser which builds directory contents from DIDL lite input. * An XML parser which builds directory contents from DIDL lite input.
*/ */
@ -114,11 +114,10 @@ protected:
// bitrate="24576" duration="00:03:35" sampleFrequency="44100" // bitrate="24576" duration="00:03:35" sampleFrequency="44100"
// nrAudioChannels="2"> // nrAudioChannels="2">
for (auto i = res_attributes; *i != nullptr; ++i) { const char *duration =
const char *value = GetAttribute(attrs, *i); GetAttribute(attrs, "duration");
if (value != nullptr) if (duration != nullptr)
m_tobj.m_props.emplace(*i, value); m_tobj.duration = ParseDuration(duration);
}
} }
break; break;

View File

@ -58,6 +58,11 @@ public:
// The map keys are the XML tag or attribute names. // The map keys are the XML tag or attribute names.
std::map<std::string, std::string> m_props; std::map<std::string, std::string> m_props;
/**
* Song duration in seconds. 0 if unknown.
*/
int duration;
/** Get named property /** Get named property
* @param property name (e.g. upnp:artist, upnp:album, * @param property name (e.g. upnp:artist, upnp:album,
* upnp:originalTrackNumber, upnp:genre). Use m_title instead * upnp:originalTrackNumber, upnp:genre). Use m_title instead
@ -80,6 +85,7 @@ public:
type = Type::UNKNOWN; type = Type::UNKNOWN;
item_class = ItemClass::UNKNOWN; item_class = ItemClass::UNKNOWN;
m_props.clear(); m_props.clear();
duration = -1;
} }
}; };