Commit f08041f0 authored by Max Kellermann's avatar Max Kellermann

input_stream: added struct input_plugin

Instead of managing a set of method pointers in each input_stream struct, move these into the new input_plugin struct. Each input_stream has only a pointer to the plugin struct. Pointers to all implementations are kept in the array "input_plugins".
parent dbc7e9ba
......@@ -452,7 +452,8 @@ input_curl_seek(struct input_stream *is, mpd_unused long offset,
return 0;
}
bool input_curl_open(struct input_stream *is, char *url)
static bool
input_curl_open(struct input_stream *is, const char *url)
{
struct input_curl *c;
bool ret;
......@@ -483,11 +484,14 @@ bool input_curl_open(struct input_stream *is, char *url)
return false;
}
is->seekFunc = input_curl_seek;
is->closeFunc = input_curl_close;
is->readFunc = input_curl_read;
is->atEOFFunc = input_curl_eof;
is->bufferFunc = input_curl_buffer;
return true;
}
const struct input_plugin input_plugin_curl = {
.open = input_curl_open,
.close = input_curl_close,
.buffer = input_curl_buffer,
.read = input_curl_read,
.eof = input_curl_eof,
.seek = input_curl_seek,
};
......@@ -19,14 +19,10 @@
#ifndef MPD_INPUT_CURL_H
#define MPD_INPUT_CURL_H
#include <stdbool.h>
struct input_stream;
extern const struct input_plugin input_plugin_curl;
void input_curl_global_init(void);
void input_curl_global_finish(void);
bool input_curl_open(struct input_stream *is, char *url);
#endif
......@@ -21,30 +21,15 @@
#include "log.h"
#include "os_compat.h"
static int
input_file_seek(struct input_stream *is, long offset, int whence);
static size_t
input_file_read(struct input_stream *is, void *ptr, size_t size);
static int
input_file_close(struct input_stream *is);
static int
input_file_eof(struct input_stream *is);
static int
input_file_buffer(struct input_stream *is);
int
input_file_open(struct input_stream *is, char *filename)
static bool
input_file_open(struct input_stream *is, const char *filename)
{
FILE *fp;
fp = fopen(filename, "r");
if (!fp) {
is->error = errno;
return -1;
return false;
}
is->seekable = 1;
......@@ -58,15 +43,9 @@ input_file_open(struct input_stream *is, char *filename)
#endif
is->data = fp;
is->seekFunc = input_file_seek;
is->closeFunc = input_file_close;
is->readFunc = input_file_read;
is->atEOFFunc = input_file_eof;
is->bufferFunc = input_file_buffer;
is->ready = 1;
return 0;
return true;
}
static int
......@@ -128,3 +107,12 @@ input_file_buffer(mpd_unused struct input_stream *is)
{
return 0;
}
const struct input_plugin input_plugin_file = {
.open = input_file_open,
.close = input_file_close,
.buffer = input_file_buffer,
.read = input_file_read,
.eof = input_file_eof,
.seek = input_file_seek,
};
......@@ -21,7 +21,6 @@
#include "input_stream.h"
int
input_file_open(struct input_stream *is, char *filename);
extern const struct input_plugin input_plugin_file;
#endif
......@@ -27,6 +27,16 @@
#include <stdlib.h>
static const struct input_plugin *const input_plugins[] = {
&input_plugin_file,
#ifdef HAVE_CURL
&input_plugin_curl,
#endif
};
static const unsigned num_input_plugins =
sizeof(input_plugins) / sizeof(input_plugins[0]);
void input_stream_global_init(void)
{
#ifdef HAVE_CURL
......@@ -52,26 +62,27 @@ int input_stream_open(struct input_stream *is, char *url)
is->meta_name = NULL;
is->meta_title = NULL;
if (input_file_open(is, url) == 0)
return 0;
for (unsigned i = 0; i < num_input_plugins; ++i) {
const struct input_plugin *plugin = input_plugins[i];
#ifdef HAVE_CURL
if (input_curl_open(is, url))
return 0;
#endif
if (plugin->open(is, url)) {
is->plugin = plugin;
return 0;
}
}
return -1;
}
int input_stream_seek(struct input_stream *is, long offset, int whence)
{
return is->seekFunc(is, offset, whence);
return is->plugin->seek(is, offset, whence);
}
size_t
input_stream_read(struct input_stream *is, void *ptr, size_t size)
{
return is->readFunc(is, ptr, size);
return is->plugin->read(is, ptr, size);
}
int input_stream_close(struct input_stream *is)
......@@ -83,15 +94,15 @@ int input_stream_close(struct input_stream *is)
if (is->meta_title)
free(is->meta_title);
return is->closeFunc(is);
return is->plugin->close(is);
}
int input_stream_eof(struct input_stream *is)
{
return is->atEOFFunc(is);
return is->plugin->eof(is);
}
int input_stream_buffer(struct input_stream *is)
{
return is->bufferFunc(is);
return is->plugin->buffer(is);
}
......@@ -20,8 +20,23 @@
#define INPUT_STREAM_H
#include <stddef.h>
#include <stdbool.h>
struct input_stream;
struct input_plugin {
bool (*open)(struct input_stream *is, const char *url);
int (*close)(struct input_stream *is);
int (*buffer)(struct input_stream *is);
size_t (*read)(struct input_stream *is, void *ptr, size_t size);
int (*eof)(struct input_stream *is);
int (*seek)(struct input_stream *is, long offset, int whence);
};
struct input_stream {
const struct input_plugin *plugin;
int ready;
int error;
......@@ -30,12 +45,6 @@ struct input_stream {
char *mime;
int seekable;
int (*seekFunc)(struct input_stream *is, long offset, int whence);
size_t (*readFunc)(struct input_stream *is, void *ptr, size_t size);
int (*closeFunc)(struct input_stream *is);
int (*atEOFFunc)(struct input_stream *is);
int (*bufferFunc)(struct input_stream *is);
void *data;
char *meta_name;
char *meta_title;
......
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