playlist/SoundCloud: move enum key into struct, make strictly-typed

This commit is contained in:
Max Kellermann 2017-01-20 17:13:20 +01:00
parent 2886766fb5
commit da9657aac4

View File

@ -87,13 +87,6 @@ soundcloud_resolve(const char* uri)
/* YAJL parser for track data from both /tracks/ and /playlists/ JSON */ /* YAJL parser for track data from both /tracks/ and /playlists/ JSON */
enum key {
Duration,
Title,
Stream_URL,
Other,
};
static const char *const key_str[] = { static const char *const key_str[] = {
"duration", "duration",
"title", "title",
@ -102,7 +95,14 @@ static const char *const key_str[] = {
}; };
struct SoundCloudJsonData { struct SoundCloudJsonData {
int key; enum class Key {
DURATION,
TITLE,
STREAM_URL,
OTHER,
};
Key key;
std::string stream_url; std::string stream_url;
long duration; long duration;
std::string title; std::string title;
@ -117,7 +117,7 @@ handle_integer(void *ctx, long long intval)
auto *data = (SoundCloudJsonData *) ctx; auto *data = (SoundCloudJsonData *) ctx;
switch (data->key) { switch (data->key) {
case Duration: case SoundCloudJsonData::Key::DURATION:
data->duration = intval; data->duration = intval;
break; break;
default: default:
@ -134,13 +134,15 @@ handle_string(void *ctx, const unsigned char *stringval, size_t stringlen)
const char *s = (const char *) stringval; const char *s = (const char *) stringval;
switch (data->key) { switch (data->key) {
case Title: case SoundCloudJsonData::Key::TITLE:
data->title.assign(s, stringlen); data->title.assign(s, stringlen);
break; break;
case Stream_URL:
case SoundCloudJsonData::Key::STREAM_URL:
data->stream_url.assign(s, stringlen); data->stream_url.assign(s, stringlen);
data->got_url = 1; data->got_url = 1;
break; break;
default: default:
break; break;
} }
@ -153,16 +155,12 @@ handle_mapkey(void *ctx, const unsigned char *stringval, size_t stringlen)
{ {
auto *data = (SoundCloudJsonData *) ctx; auto *data = (SoundCloudJsonData *) ctx;
int i; const auto *i = key_str;
data->key = Other; while (*i != nullptr &&
!StringStartsWith(*i, {(const char *)stringval, stringlen}))
for (i = 0; i < Other; ++i) { ++i;
if (StringStartsWith(key_str[i], {(const char *)stringval, stringlen})) {
data->key = i;
break;
}
}
data->key = SoundCloudJsonData::Key(i - key_str);
return 1; return 1;
} }