lib/yajl/Callbacks: OO wrapper for yajl_callbacks
This commit is contained in:
parent
0211c7f7f3
commit
55d1473918
@ -244,6 +244,7 @@ CURL_SOURCES = \
|
|||||||
|
|
||||||
YAJL_SOURCES = \
|
YAJL_SOURCES = \
|
||||||
src/lib/yajl/ParseInputStream.cxx src/lib/yajl/ParseInputStream.hxx \
|
src/lib/yajl/ParseInputStream.cxx src/lib/yajl/ParseInputStream.hxx \
|
||||||
|
src/lib/yajl/Callbacks.hxx \
|
||||||
src/lib/yajl/Handle.hxx
|
src/lib/yajl/Handle.hxx
|
||||||
|
|
||||||
UPNP_SOURCES = \
|
UPNP_SOURCES = \
|
||||||
|
77
src/lib/yajl/Callbacks.hxx
Normal file
77
src/lib/yajl/Callbacks.hxx
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 Max Kellermann <max.kellermann@gmail.com>
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* - Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* - Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||||
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef YAJL_CALLBACKS_HXX
|
||||||
|
#define YAJL_CALLBACKS_HXX
|
||||||
|
|
||||||
|
#include "util/Cast.hxx"
|
||||||
|
#include "util/StringView.hxx"
|
||||||
|
|
||||||
|
#include <yajl/yajl_parse.h>
|
||||||
|
|
||||||
|
namespace Yajl {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper template which allows implementing callbacks as regular
|
||||||
|
* methods. The "ctx" parameter is casted to the enclosing class.
|
||||||
|
*/
|
||||||
|
template<class T>
|
||||||
|
struct CallbacksWrapper {
|
||||||
|
static T &Cast(void *ctx) {
|
||||||
|
return *(T *)ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int Integer(void *ctx, long long integerVal) noexcept {
|
||||||
|
return Cast(ctx).Integer(integerVal);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int String(void *ctx, const unsigned char *stringVal,
|
||||||
|
size_t stringLen) noexcept {
|
||||||
|
return Cast(ctx).String(StringView((const char *)stringVal,
|
||||||
|
stringLen));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int StartMap(void *ctx) noexcept {
|
||||||
|
return Cast(ctx).StartMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
static int MapKey(void *ctx, const unsigned char *key,
|
||||||
|
size_t stringLen) noexcept {
|
||||||
|
return Cast(ctx).MapKey(StringView((const char *)key,
|
||||||
|
stringLen));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int EndMap(void *ctx) noexcept {
|
||||||
|
return Cast(ctx).EndMap();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Yajl
|
||||||
|
|
||||||
|
#endif
|
@ -22,6 +22,7 @@
|
|||||||
#include "../PlaylistPlugin.hxx"
|
#include "../PlaylistPlugin.hxx"
|
||||||
#include "../MemorySongEnumerator.hxx"
|
#include "../MemorySongEnumerator.hxx"
|
||||||
#include "lib/yajl/Handle.hxx"
|
#include "lib/yajl/Handle.hxx"
|
||||||
|
#include "lib/yajl/Callbacks.hxx"
|
||||||
#include "lib/yajl/ParseInputStream.hxx"
|
#include "lib/yajl/ParseInputStream.hxx"
|
||||||
#include "config/Block.hxx"
|
#include "config/Block.hxx"
|
||||||
#include "input/InputStream.hxx"
|
#include "input/InputStream.hxx"
|
||||||
@ -109,111 +110,106 @@ struct SoundCloudJsonData {
|
|||||||
int got_url = 0; /* nesting level of last stream_url */
|
int got_url = 0; /* nesting level of last stream_url */
|
||||||
|
|
||||||
std::forward_list<DetachedSong> songs;
|
std::forward_list<DetachedSong> songs;
|
||||||
|
|
||||||
|
bool Integer(long long value) noexcept;
|
||||||
|
bool String(StringView value) noexcept;
|
||||||
|
bool StartMap() noexcept;
|
||||||
|
bool MapKey(StringView value) noexcept;
|
||||||
|
bool EndMap() noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int
|
inline bool
|
||||||
handle_integer(void *ctx, long long intval)
|
SoundCloudJsonData::Integer(long long intval) noexcept
|
||||||
{
|
{
|
||||||
auto *data = (SoundCloudJsonData *) ctx;
|
switch (key) {
|
||||||
|
|
||||||
switch (data->key) {
|
|
||||||
case SoundCloudJsonData::Key::DURATION:
|
case SoundCloudJsonData::Key::DURATION:
|
||||||
data->duration = intval;
|
duration = intval;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
inline bool
|
||||||
handle_string(void *ctx, const unsigned char *stringval, size_t stringlen)
|
SoundCloudJsonData::String(StringView value) noexcept
|
||||||
{
|
{
|
||||||
auto *data = (SoundCloudJsonData *) ctx;
|
switch (key) {
|
||||||
const char *s = (const char *) stringval;
|
|
||||||
|
|
||||||
switch (data->key) {
|
|
||||||
case SoundCloudJsonData::Key::TITLE:
|
case SoundCloudJsonData::Key::TITLE:
|
||||||
data->title.assign(s, stringlen);
|
title.assign(value.data, value.size);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SoundCloudJsonData::Key::STREAM_URL:
|
case SoundCloudJsonData::Key::STREAM_URL:
|
||||||
data->stream_url.assign(s, stringlen);
|
stream_url.assign(value.data, value.size);
|
||||||
data->got_url = 1;
|
got_url = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
inline bool
|
||||||
handle_mapkey(void *ctx, const unsigned char *stringval, size_t stringlen)
|
SoundCloudJsonData::MapKey(StringView value) noexcept
|
||||||
{
|
{
|
||||||
auto *data = (SoundCloudJsonData *) ctx;
|
|
||||||
|
|
||||||
const auto *i = key_str;
|
const auto *i = key_str;
|
||||||
while (*i != nullptr &&
|
while (*i != nullptr && !StringStartsWith(*i, value))
|
||||||
!StringStartsWith(*i, {(const char *)stringval, stringlen}))
|
|
||||||
++i;
|
++i;
|
||||||
|
|
||||||
data->key = SoundCloudJsonData::Key(i - key_str);
|
key = SoundCloudJsonData::Key(i - key_str);
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
inline bool
|
||||||
handle_start_map(void *ctx)
|
SoundCloudJsonData::StartMap() noexcept
|
||||||
{
|
{
|
||||||
auto *data = (SoundCloudJsonData *) ctx;
|
if (got_url > 0)
|
||||||
|
got_url++;
|
||||||
|
|
||||||
if (data->got_url > 0)
|
return true;
|
||||||
data->got_url++;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
inline bool
|
||||||
handle_end_map(void *ctx)
|
SoundCloudJsonData::EndMap() noexcept
|
||||||
{
|
{
|
||||||
auto *data = (SoundCloudJsonData *) ctx;
|
if (got_url > 1) {
|
||||||
|
got_url--;
|
||||||
if (data->got_url > 1) {
|
|
||||||
data->got_url--;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data->got_url == 0)
|
if (got_url == 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
/* got_url == 1, track finished, make it into a song */
|
/* got_url == 1, track finished, make it into a song */
|
||||||
data->got_url = 0;
|
got_url = 0;
|
||||||
|
|
||||||
const std::string u = data->stream_url + "?client_id=" +
|
const std::string u = stream_url + "?client_id=" +
|
||||||
soundcloud_config.apikey;
|
soundcloud_config.apikey;
|
||||||
|
|
||||||
TagBuilder tag;
|
TagBuilder tag;
|
||||||
tag.SetDuration(SignedSongTime::FromMS(data->duration));
|
tag.SetDuration(SignedSongTime::FromMS(duration));
|
||||||
if (!data->title.empty())
|
if (!title.empty())
|
||||||
tag.AddItem(TAG_NAME, data->title.c_str());
|
tag.AddItem(TAG_NAME, title.c_str());
|
||||||
|
|
||||||
data->songs.emplace_front(u.c_str(), tag.Commit());
|
songs.emplace_front(u.c_str(), tag.Commit());
|
||||||
|
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using Wrapper = Yajl::CallbacksWrapper<SoundCloudJsonData>;
|
||||||
static constexpr yajl_callbacks parse_callbacks = {
|
static constexpr yajl_callbacks parse_callbacks = {
|
||||||
nullptr,
|
nullptr,
|
||||||
nullptr,
|
nullptr,
|
||||||
handle_integer,
|
Wrapper::Integer,
|
||||||
nullptr,
|
nullptr,
|
||||||
nullptr,
|
nullptr,
|
||||||
handle_string,
|
Wrapper::String,
|
||||||
handle_start_map,
|
Wrapper::StartMap,
|
||||||
handle_mapkey,
|
Wrapper::MapKey,
|
||||||
handle_end_map,
|
Wrapper::EndMap,
|
||||||
nullptr,
|
nullptr,
|
||||||
nullptr,
|
nullptr,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user