diff --git a/Makefile.am b/Makefile.am index 9c3d85dc2..3e956279d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -243,6 +243,7 @@ CURL_SOURCES = \ src/lib/curl/Slist.hxx YAJL_SOURCES = \ + src/lib/yajl/ParseInputStream.cxx src/lib/yajl/ParseInputStream.hxx \ src/lib/yajl/Handle.hxx UPNP_SOURCES = \ diff --git a/src/lib/yajl/ParseInputStream.cxx b/src/lib/yajl/ParseInputStream.cxx new file mode 100644 index 000000000..722012957 --- /dev/null +++ b/src/lib/yajl/ParseInputStream.cxx @@ -0,0 +1,43 @@ +/* + * Copyright 2003-2018 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "ParseInputStream.hxx" +#include "Handle.hxx" +#include "input/InputStream.hxx" + +void +Yajl::ParseInputStream(Handle &handle, InputStream &is) +{ + const std::lock_guard protect(is.mutex); + + bool done = false; + + while (!done) { + unsigned char buffer[4096]; + const size_t nbytes = is.Read(buffer, sizeof(buffer)); + if (nbytes == 0) + done = true; + + if (done) { + handle.CompleteParse(); + } else + handle.Parse(buffer, nbytes); + } +} diff --git a/src/lib/yajl/ParseInputStream.hxx b/src/lib/yajl/ParseInputStream.hxx new file mode 100644 index 000000000..a37a2a84f --- /dev/null +++ b/src/lib/yajl/ParseInputStream.hxx @@ -0,0 +1,34 @@ +/* + * Copyright 2003-2018 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef YAJL_PARSE_INPUT_STREAM_HXX +#define YAJL_PARSE_INPUT_STREAM_HXX + +class InputStream; + +namespace Yajl { + +class Handle; + +void +ParseInputStream(Handle &handle, InputStream &is); + +} // namespace Yajl + +#endif diff --git a/src/playlist/plugins/SoundCloudPlaylistPlugin.cxx b/src/playlist/plugins/SoundCloudPlaylistPlugin.cxx index 9813c58ed..ad88868b1 100644 --- a/src/playlist/plugins/SoundCloudPlaylistPlugin.cxx +++ b/src/playlist/plugins/SoundCloudPlaylistPlugin.cxx @@ -22,6 +22,7 @@ #include "../PlaylistPlugin.hxx" #include "../MemorySongEnumerator.hxx" #include "lib/yajl/Handle.hxx" +#include "lib/yajl/ParseInputStream.hxx" #include "config/Block.hxx" #include "input/InputStream.hxx" #include "tag/Builder.hxx" @@ -227,23 +228,7 @@ soundcloud_parse_json(const char *url, Yajl::Handle &handle, Mutex &mutex, Cond &cond) { auto input_stream = InputStream::OpenReady(url, mutex, cond); - - const std::lock_guard protect(mutex); - - bool done = false; - - while (!done) { - unsigned char buffer[4096]; - const size_t nbytes = - input_stream->Read(buffer, sizeof(buffer)); - if (nbytes == 0) - done = true; - - if (done) { - handle.CompleteParse(); - } else - handle.Parse(buffer, nbytes); - } + Yajl::ParseInputStream(handle, *input_stream); } /**