input/rewind: convert to C++

This commit is contained in:
Max Kellermann 2013-01-21 17:36:19 +01:00
parent 5cffe693b0
commit 9c870e4276
4 changed files with 86 additions and 83 deletions

View File

@ -80,7 +80,6 @@ mpd_headers = \
src/input_plugin.h \ src/input_plugin.h \
src/input_stream.h \ src/input_stream.h \
src/input/ffmpeg_input_plugin.h \ src/input/ffmpeg_input_plugin.h \
src/input/rewind_input_plugin.h \
src/input/despotify_input_plugin.h \ src/input/despotify_input_plugin.h \
src/input/cdio_paranoia_input_plugin.h \ src/input/cdio_paranoia_input_plugin.h \
src/despotify_utils.h \ src/despotify_utils.h \
@ -711,7 +710,7 @@ libinput_a_SOURCES = \
src/InputRegistry.cxx src/InputRegistry.hxx \ src/InputRegistry.cxx src/InputRegistry.hxx \
src/InputStream.cxx \ src/InputStream.cxx \
src/input_internal.c src/input_internal.h \ src/input_internal.c src/input_internal.h \
src/input/rewind_input_plugin.c \ src/input/RewindInputPlugin.cxx src/input/RewindInputPlugin.hxx \
src/input/FileInputPlugin.cxx src/input/FileInputPlugin.hxx src/input/FileInputPlugin.cxx src/input/FileInputPlugin.hxx
libinput_a_CPPFLAGS = $(AM_CPPFLAGS) \ libinput_a_CPPFLAGS = $(AM_CPPFLAGS) \

View File

@ -21,9 +21,9 @@
#include "input_stream.h" #include "input_stream.h"
#include "InputRegistry.hxx" #include "InputRegistry.hxx"
#include "input_plugin.h" #include "input_plugin.h"
#include "input/RewindInputPlugin.hxx"
extern "C" { extern "C" {
#include "input/rewind_input_plugin.h"
#include "uri.h" #include "uri.h"
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -18,7 +18,7 @@
*/ */
#include "config.h" #include "config.h"
#include "input/rewind_input_plugin.h" #include "RewindInputPlugin.hxx"
#include "input_internal.h" #include "input_internal.h"
#include "input_plugin.h" #include "input_plugin.h"
#include "tag.h" #include "tag.h"
@ -31,14 +31,16 @@
#undef G_LOG_DOMAIN #undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "input_rewind" #define G_LOG_DOMAIN "input_rewind"
struct input_rewind { extern const struct input_plugin rewind_input_plugin;
struct RewindInputStream {
struct input_stream base; struct input_stream base;
struct input_stream *input; struct input_stream *input;
/** /**
* The read position within the buffer. Undefined as long as * The read position within the buffer. Undefined as long as
* reading_from_buffer() returns false. * ReadingFromBuffer() returns false.
*/ */
size_t head; size_t head;
@ -56,16 +58,25 @@ struct input_rewind {
* stream (offset 0). * stream (offset 0).
*/ */
char buffer[64 * 1024]; char buffer[64 * 1024];
};
RewindInputStream(input_stream *_input)
:input(_input), tail(0) {
input_stream_init(&base, &rewind_input_plugin, input->uri,
input->mutex, input->cond);
}
~RewindInputStream() {
input_stream_close(input);
input_stream_deinit(&base);
}
/** /**
* Are we currently reading from the buffer, and does the buffer * Are we currently reading from the buffer, and does the
* contain more data for the next read operation? * buffer contain more data for the next read operation?
*/ */
static bool bool ReadingFromBuffer() const {
reading_from_buffer(const struct input_rewind *r) return tail > 0 && base.offset < input->offset;
{
return r->tail > 0 && r->base.offset < r->input->offset;
} }
/** /**
@ -74,11 +85,9 @@ reading_from_buffer(const struct input_rewind *r)
* the underlying stream has returned, which may have modified these * the underlying stream has returned, which may have modified these
* attributes. * attributes.
*/ */
static void void CopyAttributes() {
copy_attributes(struct input_rewind *r) struct input_stream *dest = &base;
{ const struct input_stream *src = input;
struct input_stream *dest = &r->base;
const struct input_stream *src = r->input;
assert(dest != src); assert(dest != src);
assert(src->mime == NULL || dest->mime != src->mime); assert(src->mime == NULL || dest->mime != src->mime);
@ -95,22 +104,20 @@ copy_attributes(struct input_rewind *r)
dest->mime = g_strdup(src->mime); dest->mime = g_strdup(src->mime);
} }
} }
};
static void static void
input_rewind_close(struct input_stream *is) input_rewind_close(struct input_stream *is)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
input_stream_close(r->input); delete r;
input_stream_deinit(&r->base);
g_free(r);
} }
static bool static bool
input_rewind_check(struct input_stream *is, GError **error_r) input_rewind_check(struct input_stream *is, GError **error_r)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
return input_stream_check(r->input, error_r); return input_stream_check(r->input, error_r);
} }
@ -118,16 +125,16 @@ input_rewind_check(struct input_stream *is, GError **error_r)
static void static void
input_rewind_update(struct input_stream *is) input_rewind_update(struct input_stream *is)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
if (!reading_from_buffer(r)) if (!r->ReadingFromBuffer())
copy_attributes(r); r->CopyAttributes();
} }
static struct tag * static struct tag *
input_rewind_tag(struct input_stream *is) input_rewind_tag(struct input_stream *is)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
return input_stream_tag(r->input); return input_stream_tag(r->input);
} }
@ -135,7 +142,7 @@ input_rewind_tag(struct input_stream *is)
static bool static bool
input_rewind_available(struct input_stream *is) input_rewind_available(struct input_stream *is)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
return input_stream_available(r->input); return input_stream_available(r->input);
} }
@ -144,9 +151,9 @@ static size_t
input_rewind_read(struct input_stream *is, void *ptr, size_t size, input_rewind_read(struct input_stream *is, void *ptr, size_t size,
GError **error_r) GError **error_r)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
if (reading_from_buffer(r)) { if (r->ReadingFromBuffer()) {
/* buffered read */ /* buffered read */
assert(r->head == (size_t)is->offset); assert(r->head == (size_t)is->offset);
@ -177,7 +184,7 @@ input_rewind_read(struct input_stream *is, void *ptr, size_t size,
assert(r->tail == (size_t)r->input->offset); assert(r->tail == (size_t)r->input->offset);
} }
copy_attributes(r); r->CopyAttributes();
return nbytes; return nbytes;
} }
@ -186,23 +193,23 @@ input_rewind_read(struct input_stream *is, void *ptr, size_t size,
static bool static bool
input_rewind_eof(struct input_stream *is) input_rewind_eof(struct input_stream *is)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
return !reading_from_buffer(r) && input_stream_eof(r->input); return !r->ReadingFromBuffer() && input_stream_eof(r->input);
} }
static bool static bool
input_rewind_seek(struct input_stream *is, goffset offset, int whence, input_rewind_seek(struct input_stream *is, goffset offset, int whence,
GError **error_r) GError **error_r)
{ {
struct input_rewind *r = (struct input_rewind *)is; RewindInputStream *r = (RewindInputStream *)is;
assert(is->ready); assert(is->ready);
if (whence == SEEK_SET && r->tail > 0 && offset <= (goffset)r->tail) { if (whence == SEEK_SET && r->tail > 0 && offset <= (goffset)r->tail) {
/* buffered seek */ /* buffered seek */
assert(!reading_from_buffer(r) || assert(!r->ReadingFromBuffer() ||
r->head == (size_t)is->offset); r->head == (size_t)is->offset);
assert(r->tail == (size_t)r->input->offset); assert(r->tail == (size_t)r->input->offset);
@ -213,7 +220,7 @@ input_rewind_seek(struct input_stream *is, goffset offset, int whence,
} else { } else {
bool success = input_stream_seek(r->input, offset, whence, bool success = input_stream_seek(r->input, offset, whence,
error_r); error_r);
copy_attributes(r); r->CopyAttributes();
/* disable the buffer, because r->input has left the /* disable the buffer, because r->input has left the
buffered range now */ buffered range now */
@ -223,22 +230,24 @@ input_rewind_seek(struct input_stream *is, goffset offset, int whence,
} }
} }
static const struct input_plugin rewind_input_plugin = { const struct input_plugin rewind_input_plugin = {
.close = input_rewind_close, nullptr,
.check = input_rewind_check, nullptr,
.update = input_rewind_update, nullptr,
.tag = input_rewind_tag, nullptr,
.available = input_rewind_available, input_rewind_close,
.read = input_rewind_read, input_rewind_check,
.eof = input_rewind_eof, input_rewind_update,
.seek = input_rewind_seek, input_rewind_tag,
input_rewind_available,
input_rewind_read,
input_rewind_eof,
input_rewind_seek,
}; };
struct input_stream * struct input_stream *
input_rewind_open(struct input_stream *is) input_rewind_open(struct input_stream *is)
{ {
struct input_rewind *c;
assert(is != NULL); assert(is != NULL);
assert(is->offset == 0); assert(is->offset == 0);
@ -246,11 +255,6 @@ input_rewind_open(struct input_stream *is)
/* seekable resources don't need this plugin */ /* seekable resources don't need this plugin */
return is; return is;
c = g_new(struct input_rewind, 1); RewindInputStream *c = new RewindInputStream(is);
input_stream_init(&c->base, &rewind_input_plugin, is->uri,
is->mutex, is->cond);
c->tail = 0;
c->input = is;
return &c->base; return &c->base;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -24,8 +24,8 @@
* each decoder plugin peek a portion from the stream). * each decoder plugin peek a portion from the stream).
*/ */
#ifndef MPD_INPUT_REWIND_H #ifndef MPD_INPUT_REWIND_HXX
#define MPD_INPUT_REWIND_H #define MPD_INPUT_REWIND_HXX
#include "check.h" #include "check.h"