Commit e9e55b08 authored by Denis Krjuchkov's avatar Denis Krjuchkov

text_input_stream: convert to class

parent 49a38451
......@@ -70,7 +70,7 @@ mpd_headers = \
src/gcc.h \
src/decoder/pcm_decoder_plugin.h \
src/input_stream.h \
src/text_input_stream.h \
src/TextInputStream.hxx \
src/ls.h \
src/mixer_plugin.h \
src/daemon.h \
......@@ -238,7 +238,7 @@ src_mpd_SOURCES = \
src/tag_handler.c src/tag_handler.h \
src/TagFile.cxx src/TagFile.hxx \
src/TextFile.cxx src/TextFile.hxx \
src/text_input_stream.c \
src/TextInputStream.cxx \
src/Volume.cxx src/Volume.hxx \
src/SongFilter.cxx src/SongFilter.hxx \
src/SongPointer.hxx \
......@@ -1108,7 +1108,7 @@ test_dump_text_file_SOURCES = test/dump_text_file.cxx \
test/stdbin.h \
src/IOThread.cxx \
src/Tag.cxx src/TagNames.c src/TagPool.cxx \
src/text_input_stream.c \
src/TextInputStream.cxx \
src/fd_util.c
test_dump_playlist_LDADD = \
......@@ -1130,7 +1130,7 @@ test_dump_playlist_SOURCES = test/dump_playlist.cxx \
src/Song.cxx src/Tag.cxx src/TagNames.c src/TagPool.cxx src/TagSave.cxx \
src/tag_handler.c src/TagFile.cxx \
src/audio_check.c \
src/text_input_stream.c \
src/TextInputStream.cxx \
src/cue/CueParser.cxx src/cue/CueParser.hxx \
src/fd_util.c
......
......@@ -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;
}
......@@ -17,36 +17,43 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_TEXT_INPUT_STREAM_H
#define MPD_TEXT_INPUT_STREAM_H
#ifndef MPD_TEXT_INPUT_STREAM_HXX
#define MPD_TEXT_INPUT_STREAM_HXX
#include <string>
struct input_stream;
struct text_input_stream;
struct fifo_buffer;
/**
* 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);
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 #text_input_stream object. Does not close or free the
* underlying #input_stream.
*/
void
text_input_stream_free(struct text_input_stream *tis);
/**
* Frees the #TextInputStream object. Does not close or free the
* underlying #input_stream.
*/
~TextInputStream();
/**
* 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);
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
......@@ -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;
......
......@@ -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;
}
......
......@@ -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[] = {
......
......@@ -23,10 +23,7 @@
#include "InputStream.hxx"
#include "conf.h"
#include "stdbin.h"
extern "C" {
#include "text_input_stream.h"
}
#include "TextInputStream.hxx"
#ifdef ENABLE_ARCHIVE
#include "ArchiveList.hxx"
......@@ -49,11 +46,11 @@ my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level,
}
static void
dump_text_file(struct text_input_stream *is)
dump_text_file(TextInputStream &is)
{
const char *line;
while ((line = text_input_stream_read(is)) != NULL)
printf("'%s'\n", line);
std::string line;
while (is.ReadLine(line))
printf("'%s'\n", line.c_str());
}
static int
......@@ -77,11 +74,10 @@ dump_input_stream(struct input_stream *is)
/* read data and tags from the stream */
input_stream_unlock(is);
struct text_input_stream *tis = text_input_stream_new(is);
dump_text_file(tis);
text_input_stream_free(tis);
{
TextInputStream tis(is);
dump_text_file(tis);
}
input_stream_lock(is);
if (!input_stream_check(is, &error)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment