text_input_stream: convert to class

This commit is contained in:
Denis Krjuchkov 2013-05-12 20:02:27 +06:00
parent 49a3845135
commit e9e55b0812
8 changed files with 138 additions and 159 deletions

View File

@ -70,7 +70,7 @@ mpd_headers = \
src/gcc.h \ src/gcc.h \
src/decoder/pcm_decoder_plugin.h \ src/decoder/pcm_decoder_plugin.h \
src/input_stream.h \ src/input_stream.h \
src/text_input_stream.h \ src/TextInputStream.hxx \
src/ls.h \ src/ls.h \
src/mixer_plugin.h \ src/mixer_plugin.h \
src/daemon.h \ src/daemon.h \
@ -238,7 +238,7 @@ src_mpd_SOURCES = \
src/tag_handler.c src/tag_handler.h \ src/tag_handler.c src/tag_handler.h \
src/TagFile.cxx src/TagFile.hxx \ src/TagFile.cxx src/TagFile.hxx \
src/TextFile.cxx src/TextFile.hxx \ src/TextFile.cxx src/TextFile.hxx \
src/text_input_stream.c \ src/TextInputStream.cxx \
src/Volume.cxx src/Volume.hxx \ src/Volume.cxx src/Volume.hxx \
src/SongFilter.cxx src/SongFilter.hxx \ src/SongFilter.cxx src/SongFilter.hxx \
src/SongPointer.hxx \ src/SongPointer.hxx \
@ -1108,7 +1108,7 @@ test_dump_text_file_SOURCES = test/dump_text_file.cxx \
test/stdbin.h \ test/stdbin.h \
src/IOThread.cxx \ src/IOThread.cxx \
src/Tag.cxx src/TagNames.c src/TagPool.cxx \ src/Tag.cxx src/TagNames.c src/TagPool.cxx \
src/text_input_stream.c \ src/TextInputStream.cxx \
src/fd_util.c src/fd_util.c
test_dump_playlist_LDADD = \ 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/Song.cxx src/Tag.cxx src/TagNames.c src/TagPool.cxx src/TagSave.cxx \
src/tag_handler.c src/TagFile.cxx \ src/tag_handler.c src/TagFile.cxx \
src/audio_check.c \ src/audio_check.c \
src/text_input_stream.c \ src/TextInputStream.cxx \
src/cue/CueParser.cxx src/cue/CueParser.hxx \ src/cue/CueParser.cxx src/cue/CueParser.hxx \
src/fd_util.c src/fd_util.c

View File

@ -18,7 +18,7 @@
*/ */
#include "config.h" #include "config.h"
#include "text_input_stream.h" #include "TextInputStream.hxx"
#include "input_stream.h" #include "input_stream.h"
#include "util/fifo_buffer.h" #include "util/fifo_buffer.h"
@ -27,85 +27,66 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
struct text_input_stream { TextInputStream::TextInputStream(struct input_stream *_is)
struct input_stream *is; : is(_is),
buffer(fifo_buffer_new(4096))
struct fifo_buffer *buffer;
char *line;
};
struct text_input_stream *
text_input_stream_new(struct input_stream *is)
{ {
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 TextInputStream::~TextInputStream()
text_input_stream_free(struct text_input_stream *tis)
{ {
fifo_buffer_free(tis->buffer); fifo_buffer_free(buffer);
g_free(tis->line);
g_free(tis);
} }
const char * bool TextInputStream::ReadLine(std::string &line)
text_input_stream_read(struct text_input_stream *tis)
{ {
GError *error = NULL; GError *error = nullptr;
void *dest; void *dest;
const char *src, *p; const char *src, *p;
size_t length, nbytes; size_t length, nbytes;
g_free(tis->line);
tis->line = NULL;
do { do {
dest = fifo_buffer_write(tis->buffer, &length); dest = fifo_buffer_write(buffer, &length);
if (dest != NULL && length >= 2) { if (dest != nullptr && length >= 2) {
/* reserve one byte for the null terminator if /* reserve one byte for the null terminator if
the last line is not terminated by a the last line is not terminated by a
newline character */ newline character */
--length; --length;
nbytes = input_stream_lock_read(tis->is, dest, length, nbytes = input_stream_lock_read(is, dest, length,
&error); &error);
if (nbytes > 0) if (nbytes > 0)
fifo_buffer_append(tis->buffer, nbytes); fifo_buffer_append(buffer, nbytes);
else if (error != NULL) { else if (error != nullptr) {
g_warning("%s", error->message); g_warning("%s", error->message);
g_error_free(error); g_error_free(error);
return NULL; return false;
} }
} else } else
nbytes = 0; nbytes = 0;
src = fifo_buffer_read(tis->buffer, &length); auto src_p = fifo_buffer_read(buffer, &length);
if (src == NULL) src = reinterpret_cast<const char *>(src_p);
return NULL;
p = memchr(src, '\n', length); if (src == nullptr)
if (p == NULL && nbytes == 0) { return false;
p = reinterpret_cast<const char*>(memchr(src, '\n', length));
if (p == nullptr && nbytes == 0) {
/* end of file (or line too long): terminate /* end of file (or line too long): terminate
the current line */ the current line */
dest = fifo_buffer_write(tis->buffer, &nbytes); dest = fifo_buffer_write(buffer, &nbytes);
assert(dest != NULL); assert(dest != nullptr);
*(char *)dest = '\n'; *(char *)dest = '\n';
fifo_buffer_append(tis->buffer, 1); fifo_buffer_append(buffer, 1);
} }
} while (p == NULL); } while (p == nullptr);
length = p - src + 1; length = p - src + 1;
while (p > src && g_ascii_isspace(p[-1])) while (p > src && g_ascii_isspace(p[-1]))
--p; --p;
tis->line = g_strndup(src, p - src); line = std::string(src, p - src);
fifo_buffer_consume(tis->buffer, length); fifo_buffer_consume(buffer, length);
return tis->line; return true;
} }

59
src/TextInputStream.hxx Normal file
View File

@ -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

View File

@ -24,10 +24,7 @@
#include "song.h" #include "song.h"
#include "input_stream.h" #include "input_stream.h"
#include "cue/CueParser.hxx" #include "cue/CueParser.hxx"
#include "TextInputStream.hxx"
extern "C" {
#include "text_input_stream.h"
}
#include <glib.h> #include <glib.h>
#include <assert.h> #include <assert.h>
@ -40,16 +37,15 @@ struct CuePlaylist {
struct playlist_provider base; struct playlist_provider base;
struct input_stream *is; struct input_stream *is;
struct text_input_stream *tis; TextInputStream tis;
CueParser parser; CueParser parser;
CuePlaylist(struct input_stream *_is) CuePlaylist(struct input_stream *_is)
:is(_is), tis(text_input_stream_new(is)) { :is(_is), tis(is) {
playlist_provider_init(&base, &cue_playlist_plugin); playlist_provider_init(&base, &cue_playlist_plugin);
} }
~CuePlaylist() { ~CuePlaylist() {
text_input_stream_free(tis);
} }
}; };
@ -76,9 +72,9 @@ cue_playlist_read(struct playlist_provider *_playlist)
if (song != NULL) if (song != NULL)
return song; return song;
const char *line; std::string line;
while ((line = text_input_stream_read(playlist->tis)) != NULL) { while (playlist->tis.ReadLine(line)) {
playlist->parser.Feed(line); playlist->parser.Feed(line.c_str());
song = playlist->parser.Get(); song = playlist->parser.Get();
if (song != NULL) if (song != NULL)
return song; return song;

View File

@ -23,10 +23,7 @@
#include "song.h" #include "song.h"
#include "tag.h" #include "tag.h"
#include "util/StringUtil.hxx" #include "util/StringUtil.hxx"
#include "TextInputStream.hxx"
extern "C" {
#include "text_input_stream.h"
}
#include <glib.h> #include <glib.h>
@ -36,20 +33,21 @@ extern "C" {
struct ExtM3uPlaylist { struct ExtM3uPlaylist {
struct playlist_provider base; struct playlist_provider base;
struct text_input_stream *tis; TextInputStream *tis;
}; };
static struct playlist_provider * static struct playlist_provider *
extm3u_open_stream(struct input_stream *is) extm3u_open_stream(struct input_stream *is)
{ {
ExtM3uPlaylist *playlist = g_new(ExtM3uPlaylist, 1); 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); std::string line;
if (line == NULL || strcmp(line, "#EXTM3U") != 0) { if (!playlist->tis->ReadLine(line)
|| strcmp(line.c_str(), "#EXTM3U") != 0) {
/* no EXTM3U header: fall back to the plain m3u /* no EXTM3U header: fall back to the plain m3u
plugin */ plugin */
text_input_stream_free(playlist->tis); delete playlist->tis;
g_free(playlist); g_free(playlist);
return NULL; return NULL;
} }
@ -63,7 +61,7 @@ extm3u_close(struct playlist_provider *_playlist)
{ {
ExtM3uPlaylist *playlist = (ExtM3uPlaylist *)_playlist; ExtM3uPlaylist *playlist = (ExtM3uPlaylist *)_playlist;
text_input_stream_free(playlist->tis); delete playlist->tis;
g_free(playlist); g_free(playlist);
} }
@ -112,29 +110,31 @@ extm3u_read(struct playlist_provider *_playlist)
{ {
ExtM3uPlaylist *playlist = (ExtM3uPlaylist *)_playlist; ExtM3uPlaylist *playlist = (ExtM3uPlaylist *)_playlist;
struct tag *tag = NULL; struct tag *tag = NULL;
const char *line; std::string line;
const char *line_s;
struct song *song; struct song *song;
do { do {
line = text_input_stream_read(playlist->tis); if (!playlist->tis->ReadLine(line)) {
if (line == NULL) {
if (tag != NULL) if (tag != NULL)
tag_free(tag); tag_free(tag);
return NULL; 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) if (tag != NULL)
tag_free(tag); tag_free(tag);
tag = extm3u_parse_tag(line + 8); tag = extm3u_parse_tag(line_s + 8);
continue; continue;
} }
while (*line != 0 && g_ascii_isspace(*line)) while (*line_s != 0 && g_ascii_isspace(*line_s))
++line; ++line_s;
} while (line[0] == '#' || *line == 0); } while (line_s[0] == '#' || *line_s == 0);
song = song_remote_new(line); song = song_remote_new(line_s);
song->tag = tag; song->tag = tag;
return song; return song;
} }

View File

@ -21,17 +21,14 @@
#include "M3uPlaylistPlugin.hxx" #include "M3uPlaylistPlugin.hxx"
#include "PlaylistPlugin.hxx" #include "PlaylistPlugin.hxx"
#include "song.h" #include "song.h"
#include "TextInputStream.hxx"
extern "C" {
#include "text_input_stream.h"
}
#include <glib.h> #include <glib.h>
struct M3uPlaylist { struct M3uPlaylist {
struct playlist_provider base; struct playlist_provider base;
struct text_input_stream *tis; TextInputStream *tis;
}; };
static struct playlist_provider * static struct playlist_provider *
@ -40,7 +37,7 @@ m3u_open_stream(struct input_stream *is)
M3uPlaylist *playlist = g_new(M3uPlaylist, 1); M3uPlaylist *playlist = g_new(M3uPlaylist, 1);
playlist_provider_init(&playlist->base, &m3u_playlist_plugin); playlist_provider_init(&playlist->base, &m3u_playlist_plugin);
playlist->tis = text_input_stream_new(is); playlist->tis = new TextInputStream(is);
return &playlist->base; return &playlist->base;
} }
@ -50,7 +47,7 @@ m3u_close(struct playlist_provider *_playlist)
{ {
M3uPlaylist *playlist = (M3uPlaylist *)_playlist; M3uPlaylist *playlist = (M3uPlaylist *)_playlist;
text_input_stream_free(playlist->tis); delete playlist->tis;
g_free(playlist); g_free(playlist);
} }
@ -58,18 +55,20 @@ static struct song *
m3u_read(struct playlist_provider *_playlist) m3u_read(struct playlist_provider *_playlist)
{ {
M3uPlaylist *playlist = (M3uPlaylist *)_playlist; M3uPlaylist *playlist = (M3uPlaylist *)_playlist;
const char *line; std::string line;
const char *line_s;
do { do {
line = text_input_stream_read(playlist->tis); if (!playlist->tis->ReadLine(line))
if (line == NULL)
return NULL; return NULL;
while (*line != 0 && g_ascii_isspace(*line)) line_s = line.c_str();
++line;
} while (line[0] == '#' || *line == 0);
return song_remote_new(line); while (*line_s != 0 && g_ascii_isspace(*line_s))
++line_s;
} while (line_s[0] == '#' || *line_s == 0);
return song_remote_new(line_s);
} }
static const char *const m3u_suffixes[] = { static const char *const m3u_suffixes[] = {

View File

@ -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

View File

@ -23,10 +23,7 @@
#include "InputStream.hxx" #include "InputStream.hxx"
#include "conf.h" #include "conf.h"
#include "stdbin.h" #include "stdbin.h"
#include "TextInputStream.hxx"
extern "C" {
#include "text_input_stream.h"
}
#ifdef ENABLE_ARCHIVE #ifdef ENABLE_ARCHIVE
#include "ArchiveList.hxx" #include "ArchiveList.hxx"
@ -49,11 +46,11 @@ my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level,
} }
static void static void
dump_text_file(struct text_input_stream *is) dump_text_file(TextInputStream &is)
{ {
const char *line; std::string line;
while ((line = text_input_stream_read(is)) != NULL) while (is.ReadLine(line))
printf("'%s'\n", line); printf("'%s'\n", line.c_str());
} }
static int static int
@ -77,11 +74,10 @@ dump_input_stream(struct input_stream *is)
/* read data and tags from the stream */ /* read data and tags from the stream */
input_stream_unlock(is); input_stream_unlock(is);
{
struct text_input_stream *tis = text_input_stream_new(is); TextInputStream tis(is);
dump_text_file(tis); dump_text_file(tis);
text_input_stream_free(tis); }
input_stream_lock(is); input_stream_lock(is);
if (!input_stream_check(is, &error)) { if (!input_stream_check(is, &error)) {