aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/TextInputStream.cxx (renamed from src/text_input_stream.c)75
-rw-r--r--src/TextInputStream.hxx59
-rw-r--r--src/playlist/CuePlaylistPlugin.cxx16
-rw-r--r--src/playlist/ExtM3uPlaylistPlugin.cxx38
-rw-r--r--src/playlist/M3uPlaylistPlugin.cxx27
-rw-r--r--src/text_input_stream.h52
6 files changed, 125 insertions, 142 deletions
diff --git a/src/text_input_stream.c b/src/TextInputStream.cxx
index 4a2eeb817..5e963ae01 100644
--- a/src/text_input_stream.c
+++ b/src/TextInputStream.cxx
@@ -18,7 +18,7 @@
*/
#include "config.h"
-#include "text_input_stream.h"
+#include "TextInputStream.hxx"
#include "input_stream.h"
#include "util/fifo_buffer.h"
@@ -27,85 +27,66 @@
#include <assert.h>
#include <string.h>
-struct text_input_stream {
- struct input_stream *is;
-
- struct fifo_buffer *buffer;
-
- char *line;
-};
-
-struct text_input_stream *
-text_input_stream_new(struct input_stream *is)
+TextInputStream::TextInputStream(struct input_stream *_is)
+ : is(_is),
+ buffer(fifo_buffer_new(4096))
{
- struct text_input_stream *tis = g_new(struct text_input_stream, 1);
-
- tis->is = is;
- tis->buffer = fifo_buffer_new(4096);
- tis->line = NULL;
-
- return tis;
}
-void
-text_input_stream_free(struct text_input_stream *tis)
+TextInputStream::~TextInputStream()
{
- fifo_buffer_free(tis->buffer);
- g_free(tis->line);
- g_free(tis);
+ fifo_buffer_free(buffer);
}
-const char *
-text_input_stream_read(struct text_input_stream *tis)
+bool TextInputStream::ReadLine(std::string &line)
{
- GError *error = NULL;
+ GError *error = nullptr;
void *dest;
const char *src, *p;
size_t length, nbytes;
- g_free(tis->line);
- tis->line = NULL;
-
do {
- dest = fifo_buffer_write(tis->buffer, &length);
- if (dest != NULL && length >= 2) {
+ dest = fifo_buffer_write(buffer, &length);
+ if (dest != nullptr && length >= 2) {
/* reserve one byte for the null terminator if
the last line is not terminated by a
newline character */
--length;
- nbytes = input_stream_lock_read(tis->is, dest, length,
+ nbytes = input_stream_lock_read(is, dest, length,
&error);
if (nbytes > 0)
- fifo_buffer_append(tis->buffer, nbytes);
- else if (error != NULL) {
+ fifo_buffer_append(buffer, nbytes);
+ else if (error != nullptr) {
g_warning("%s", error->message);
g_error_free(error);
- return NULL;
+ return false;
}
} else
nbytes = 0;
- src = fifo_buffer_read(tis->buffer, &length);
- if (src == NULL)
- return NULL;
+ auto src_p = fifo_buffer_read(buffer, &length);
+ src = reinterpret_cast<const char *>(src_p);
+
+ if (src == nullptr)
+ return false;
- p = memchr(src, '\n', length);
- if (p == NULL && nbytes == 0) {
+ p = reinterpret_cast<const char*>(memchr(src, '\n', length));
+ if (p == nullptr && nbytes == 0) {
/* end of file (or line too long): terminate
the current line */
- dest = fifo_buffer_write(tis->buffer, &nbytes);
- assert(dest != NULL);
+ dest = fifo_buffer_write(buffer, &nbytes);
+ assert(dest != nullptr);
*(char *)dest = '\n';
- fifo_buffer_append(tis->buffer, 1);
+ fifo_buffer_append(buffer, 1);
}
- } while (p == NULL);
+ } while (p == nullptr);
length = p - src + 1;
while (p > src && g_ascii_isspace(p[-1]))
--p;
- tis->line = g_strndup(src, p - src);
- fifo_buffer_consume(tis->buffer, length);
- return tis->line;
+ line = std::string(src, p - src);
+ fifo_buffer_consume(buffer, length);
+ return true;
}
diff --git a/src/TextInputStream.hxx b/src/TextInputStream.hxx
new file mode 100644
index 000000000..2608184e2
--- /dev/null
+++ b/src/TextInputStream.hxx
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2003-2011 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 MPD_TEXT_INPUT_STREAM_HXX
+#define MPD_TEXT_INPUT_STREAM_HXX
+
+#include <string>
+
+struct input_stream;
+struct fifo_buffer;
+
+class TextInputStream {
+ struct input_stream *is;
+ struct fifo_buffer *buffer;
+public:
+ /**
+ * Wraps an existing #input_stream object into a #TextInputStream,
+ * to read its contents as text lines.
+ *
+ * @param _is an open #input_stream object
+ */
+ explicit TextInputStream(struct input_stream *_is);
+
+ /**
+ * Frees the #TextInputStream object. Does not close or free the
+ * underlying #input_stream.
+ */
+ ~TextInputStream();
+
+ TextInputStream(const TextInputStream &) = delete;
+ TextInputStream& operator=(const TextInputStream &) = delete;
+
+ /**
+ * Reads the next line from the stream with newline character stripped.
+ *
+ * @param line a string to put result to
+ * @return true if line is read successfully, false on end of file
+ * or error
+ */
+ bool ReadLine(std::string &line);
+};
+
+#endif
diff --git a/src/playlist/CuePlaylistPlugin.cxx b/src/playlist/CuePlaylistPlugin.cxx
index 730ef783b..3d8098672 100644
--- a/src/playlist/CuePlaylistPlugin.cxx
+++ b/src/playlist/CuePlaylistPlugin.cxx
@@ -24,10 +24,7 @@
#include "song.h"
#include "input_stream.h"
#include "cue/CueParser.hxx"
-
-extern "C" {
-#include "text_input_stream.h"
-}
+#include "TextInputStream.hxx"
#include <glib.h>
#include <assert.h>
@@ -40,16 +37,15 @@ struct CuePlaylist {
struct playlist_provider base;
struct input_stream *is;
- struct text_input_stream *tis;
+ TextInputStream tis;
CueParser parser;
CuePlaylist(struct input_stream *_is)
- :is(_is), tis(text_input_stream_new(is)) {
+ :is(_is), tis(is) {
playlist_provider_init(&base, &cue_playlist_plugin);
}
~CuePlaylist() {
- text_input_stream_free(tis);
}
};
@@ -76,9 +72,9 @@ cue_playlist_read(struct playlist_provider *_playlist)
if (song != NULL)
return song;
- const char *line;
- while ((line = text_input_stream_read(playlist->tis)) != NULL) {
- playlist->parser.Feed(line);
+ std::string line;
+ while (playlist->tis.ReadLine(line)) {
+ playlist->parser.Feed(line.c_str());
song = playlist->parser.Get();
if (song != NULL)
return song;
diff --git a/src/playlist/ExtM3uPlaylistPlugin.cxx b/src/playlist/ExtM3uPlaylistPlugin.cxx
index 2043bea52..923536ea9 100644
--- a/src/playlist/ExtM3uPlaylistPlugin.cxx
+++ b/src/playlist/ExtM3uPlaylistPlugin.cxx
@@ -23,10 +23,7 @@
#include "song.h"
#include "tag.h"
#include "util/StringUtil.hxx"
-
-extern "C" {
-#include "text_input_stream.h"
-}
+#include "TextInputStream.hxx"
#include <glib.h>
@@ -36,20 +33,21 @@ extern "C" {
struct ExtM3uPlaylist {
struct playlist_provider base;
- struct text_input_stream *tis;
+ TextInputStream *tis;
};
static struct playlist_provider *
extm3u_open_stream(struct input_stream *is)
{
ExtM3uPlaylist *playlist = g_new(ExtM3uPlaylist, 1);
- playlist->tis = text_input_stream_new(is);
+ playlist->tis = new TextInputStream(is);
- const char *line = text_input_stream_read(playlist->tis);
- if (line == NULL || strcmp(line, "#EXTM3U") != 0) {
+ std::string line;
+ if (!playlist->tis->ReadLine(line)
+ || strcmp(line.c_str(), "#EXTM3U") != 0) {
/* no EXTM3U header: fall back to the plain m3u
plugin */
- text_input_stream_free(playlist->tis);
+ delete playlist->tis;
g_free(playlist);
return NULL;
}
@@ -63,7 +61,7 @@ extm3u_close(struct playlist_provider *_playlist)
{
ExtM3uPlaylist *playlist = (ExtM3uPlaylist *)_playlist;
- text_input_stream_free(playlist->tis);
+ delete playlist->tis;
g_free(playlist);
}
@@ -112,29 +110,31 @@ extm3u_read(struct playlist_provider *_playlist)
{
ExtM3uPlaylist *playlist = (ExtM3uPlaylist *)_playlist;
struct tag *tag = NULL;
- const char *line;
+ std::string line;
+ const char *line_s;
struct song *song;
do {
- line = text_input_stream_read(playlist->tis);
- if (line == NULL) {
+ if (!playlist->tis->ReadLine(line)) {
if (tag != NULL)
tag_free(tag);
return NULL;
}
+
+ line_s = line.c_str();
- if (g_str_has_prefix(line, "#EXTINF:")) {
+ if (g_str_has_prefix(line_s, "#EXTINF:")) {
if (tag != NULL)
tag_free(tag);
- tag = extm3u_parse_tag(line + 8);
+ tag = extm3u_parse_tag(line_s + 8);
continue;
}
- while (*line != 0 && g_ascii_isspace(*line))
- ++line;
- } while (line[0] == '#' || *line == 0);
+ while (*line_s != 0 && g_ascii_isspace(*line_s))
+ ++line_s;
+ } while (line_s[0] == '#' || *line_s == 0);
- song = song_remote_new(line);
+ song = song_remote_new(line_s);
song->tag = tag;
return song;
}
diff --git a/src/playlist/M3uPlaylistPlugin.cxx b/src/playlist/M3uPlaylistPlugin.cxx
index eeecd2779..e1e0a803b 100644
--- a/src/playlist/M3uPlaylistPlugin.cxx
+++ b/src/playlist/M3uPlaylistPlugin.cxx
@@ -21,17 +21,14 @@
#include "M3uPlaylistPlugin.hxx"
#include "PlaylistPlugin.hxx"
#include "song.h"
-
-extern "C" {
-#include "text_input_stream.h"
-}
+#include "TextInputStream.hxx"
#include <glib.h>
struct M3uPlaylist {
struct playlist_provider base;
- struct text_input_stream *tis;
+ TextInputStream *tis;
};
static struct playlist_provider *
@@ -40,7 +37,7 @@ m3u_open_stream(struct input_stream *is)
M3uPlaylist *playlist = g_new(M3uPlaylist, 1);
playlist_provider_init(&playlist->base, &m3u_playlist_plugin);
- playlist->tis = text_input_stream_new(is);
+ playlist->tis = new TextInputStream(is);
return &playlist->base;
}
@@ -50,7 +47,7 @@ m3u_close(struct playlist_provider *_playlist)
{
M3uPlaylist *playlist = (M3uPlaylist *)_playlist;
- text_input_stream_free(playlist->tis);
+ delete playlist->tis;
g_free(playlist);
}
@@ -58,18 +55,20 @@ static struct song *
m3u_read(struct playlist_provider *_playlist)
{
M3uPlaylist *playlist = (M3uPlaylist *)_playlist;
- const char *line;
+ std::string line;
+ const char *line_s;
do {
- line = text_input_stream_read(playlist->tis);
- if (line == NULL)
+ if (!playlist->tis->ReadLine(line))
return NULL;
- while (*line != 0 && g_ascii_isspace(*line))
- ++line;
- } while (line[0] == '#' || *line == 0);
+ line_s = line.c_str();
+
+ while (*line_s != 0 && g_ascii_isspace(*line_s))
+ ++line_s;
+ } while (line_s[0] == '#' || *line_s == 0);
- return song_remote_new(line);
+ return song_remote_new(line_s);
}
static const char *const m3u_suffixes[] = {
diff --git a/src/text_input_stream.h b/src/text_input_stream.h
deleted file mode 100644
index 9b3245689..000000000
--- a/src/text_input_stream.h
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright (C) 2003-2011 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 MPD_TEXT_INPUT_STREAM_H
-#define MPD_TEXT_INPUT_STREAM_H
-
-struct input_stream;
-struct text_input_stream;
-
-/**
- * Wraps an existing #input_stream object into a #text_input_stream,
- * to read its contents as text lines.
- *
- * @param is an open #input_stream object
- * @return the new #text_input_stream object
- */
-struct text_input_stream *
-text_input_stream_new(struct input_stream *is);
-
-/**
- * Frees the #text_input_stream object. Does not close or free the
- * underlying #input_stream.
- */
-void
-text_input_stream_free(struct text_input_stream *tis);
-
-/**
- * Reads the next line from the stream.
- *
- * @return a line (newline character stripped), or NULL on end of file
- * or error
- */
-const char *
-text_input_stream_read(struct text_input_stream *tis);
-
-#endif